2012-09-29 16:29:04 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
|
|
|
|
/* This Source Code Form Is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* DASH - Dynamic Adaptive Streaming over HTTP
|
|
|
|
*
|
|
|
|
* DASH is an adaptive bitrate streaming technology where a multimedia file is
|
|
|
|
* partitioned into one or more segments and delivered to a client using HTTP.
|
|
|
|
*
|
2012-11-14 11:46:40 -08:00
|
|
|
* see DASHDecoder.cpp for info on DASH interaction with the media engine.*/
|
2012-09-29 16:29:04 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
#if !defined(DASHRepDecoder_h_)
|
|
|
|
#define DASHRepDecoder_h_
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
#include "Representation.h"
|
|
|
|
#include "ImageLayers.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "DASHDecoder.h"
|
|
|
|
#include "WebMDecoder.h"
|
|
|
|
#include "WebMReader.h"
|
|
|
|
#include "MediaDecoder.h"
|
2012-09-29 16:29:04 -07:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class DASHDecoder;
|
2012-12-06 15:27:08 -08:00
|
|
|
class DASHRepReader;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class DASHRepDecoder : public MediaDecoder
|
2012-09-29 16:29:04 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef mozilla::net::Representation Representation;
|
|
|
|
typedef mozilla::net::SegmentBase SegmentBase;
|
|
|
|
typedef mozilla::layers::ImageContainer ImageContainer;
|
|
|
|
|
|
|
|
// Constructor takes a ptr to the main decoder.
|
2012-11-14 11:46:40 -08:00
|
|
|
DASHRepDecoder(DASHDecoder* aMainDecoder) :
|
2012-09-29 16:29:04 -07:00
|
|
|
mMainDecoder(aMainDecoder),
|
|
|
|
mMPDRepresentation(nullptr),
|
|
|
|
mMetadataChunkCount(0),
|
|
|
|
mCurrentByteRange(),
|
2012-12-06 15:27:08 -08:00
|
|
|
mSubsegmentIdx(-1),
|
2012-09-29 16:29:04 -07:00
|
|
|
mReader(nullptr)
|
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_CTOR(DASHRepDecoder);
|
2012-09-29 16:29:04 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
~DASHRepDecoder()
|
2012-09-29 16:29:04 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_DTOR(DASHRepDecoder);
|
2012-09-29 16:29:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone not supported; just return nullptr.
|
2012-11-14 11:46:40 -08:00
|
|
|
virtual MediaDecoder* Clone() { return nullptr; }
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Called by the main decoder at creation time; points to the main state
|
|
|
|
// machine managed by the main decoder. Called on the main thread only.
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult SetStateMachine(MediaDecoderStateMachine* aSM);
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Overridden to return the ptr set by SetStateMachine. Called on the main
|
|
|
|
// thread only.
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaDecoderStateMachine* CreateStateMachine();
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
// Called by DASHDecoder at creation time; points to the media resource
|
2012-09-29 16:29:04 -07:00
|
|
|
// for this decoder's |Representation|. Called on the main thread only.
|
|
|
|
void SetResource(MediaResource* aResource);
|
|
|
|
|
|
|
|
// Sets the |Representation| object for this decoder. Called on the main
|
|
|
|
// thread.
|
|
|
|
void SetMPDRepresentation(Representation const * aRep);
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Called from DASHDecoder on main thread; Starts media stream download.
|
2012-09-29 16:29:04 -07:00
|
|
|
nsresult Load(MediaResource* aResource = nullptr,
|
|
|
|
nsIStreamListener** aListener = nullptr,
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaDecoder* aCloneDonor = nullptr);
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Loads the next byte range (or first one on first call). Called on the main
|
|
|
|
// thread only.
|
|
|
|
void LoadNextByteRange();
|
|
|
|
|
2012-12-13 11:42:45 -08:00
|
|
|
// Returns true if the subsegment is already in the media cache.
|
|
|
|
bool IsSubsegmentCached(int32_t aSubsegmentIdx);
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Calls from DASHRepDecoder. Called on the main thread only.
|
|
|
|
void SetReader(WebMReader* aReader);
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Called if the media file encounters a network error. Call on the main
|
|
|
|
// thread only.
|
|
|
|
void NetworkError();
|
|
|
|
|
2013-01-28 18:34:27 -08:00
|
|
|
// Called from reader during ReadMetadata. This should be ignored here, and
|
|
|
|
// instead, duration should be set following MPD parsing.
|
|
|
|
void SetMediaDuration(int64_t aDuration) MOZ_OVERRIDE { };
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Set the duration of the media resource in units of seconds.
|
|
|
|
// This is called via a channel listener if it can pick up the duration
|
|
|
|
// from a content header. Must be called from the main thread only.
|
|
|
|
virtual void SetDuration(double aDuration);
|
|
|
|
|
|
|
|
// Set media stream as infinite. Called on the main thread only.
|
|
|
|
void SetInfinite(bool aInfinite);
|
|
|
|
|
|
|
|
// Sets media stream as seekable. Called on main thread only.
|
2012-11-30 05:17:54 -08:00
|
|
|
void SetMediaSeekable(bool aSeekable);
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Fire progress events if needed according to the time and byte
|
|
|
|
// constraints outlined in the specification. aTimer is true
|
|
|
|
// if the method is called as a result of the progress timer rather
|
|
|
|
// than the result of downloaded data.
|
|
|
|
void Progress(bool aTimer);
|
|
|
|
|
|
|
|
// Called as data arrives on the stream and is read into the cache. Called
|
|
|
|
// on the main thread only.
|
|
|
|
void NotifyDataArrived(const char* aBuffer,
|
|
|
|
uint32_t aLength,
|
|
|
|
int64_t aOffset);
|
|
|
|
|
|
|
|
// Called by MediaResource when some data has been received.
|
|
|
|
// Call on the main thread only.
|
|
|
|
void NotifyBytesDownloaded();
|
|
|
|
|
|
|
|
// Notify that a byte range request has been completed by the media resource.
|
|
|
|
// Called on the main thread only.
|
|
|
|
void NotifyDownloadEnded(nsresult aStatus);
|
|
|
|
|
2012-12-13 11:42:45 -08:00
|
|
|
// Called asynchronously by |LoadNextByteRange| if the data is already in the
|
|
|
|
// media cache. This will call NotifyDownloadEnded on the main thread with
|
|
|
|
// |aStatus| of NS_OK.
|
|
|
|
void DoNotifyDownloadEnded();
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Called by MediaResource when the "cache suspended" status changes.
|
|
|
|
// If MediaResource::IsSuspendedByCache returns true, then the decoder
|
|
|
|
// should stop buffering or otherwise waiting for download progress and
|
|
|
|
// start consuming data, if possible, because the cache is full.
|
|
|
|
void NotifySuspendedStatusChanged();
|
|
|
|
|
2012-12-13 11:42:45 -08:00
|
|
|
// Increments the parsed and decoded frame counters by the passed in counts.
|
|
|
|
// Can be called on any thread.
|
|
|
|
void NotifyDecodedFrames(uint32_t aParsed, uint32_t aDecoded) MOZ_OVERRIDE {
|
|
|
|
if (mMainDecoder) {mMainDecoder->NotifyDecodedFrames(aParsed, aDecoded); }
|
|
|
|
}
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Gets a byte range containing the byte offset. Call on main thread only.
|
|
|
|
nsresult GetByteRangeForSeek(int64_t const aOffset,
|
|
|
|
MediaByteRange& aByteRange);
|
|
|
|
|
2012-12-06 15:27:08 -08:00
|
|
|
// Gets the number of data byte ranges (not inc. metadata).
|
|
|
|
uint32_t GetNumDataByteRanges() {
|
|
|
|
return mByteRanges.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify that a switch is about to happen. Called on the main thread.
|
|
|
|
void PrepareForSwitch();
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// Returns true if the current thread is the state machine thread.
|
2013-01-28 18:34:28 -08:00
|
|
|
bool OnStateMachineThread() const MOZ_OVERRIDE;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Returns true if the current thread is the decode thread.
|
2013-01-28 18:34:28 -08:00
|
|
|
bool OnDecodeThread() const MOZ_OVERRIDE;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Returns main decoder's monitor for synchronised access.
|
2012-12-06 15:27:08 -08:00
|
|
|
ReentrantMonitor& GetReentrantMonitor() MOZ_OVERRIDE;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Called on the decode thread from WebMReader.
|
2013-01-28 18:34:28 -08:00
|
|
|
ImageContainer* GetImageContainer() MOZ_OVERRIDE;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Called when Metadata has been read; notifies that index data is read.
|
|
|
|
// Called on the decode thread only.
|
2012-12-06 15:27:08 -08:00
|
|
|
void OnReadMetadataCompleted() MOZ_OVERRIDE;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
2013-01-28 18:34:28 -08:00
|
|
|
// Stop updating the bytes downloaded for progress notifications. Called
|
|
|
|
// when seeking to prevent wild changes to the progress notification.
|
|
|
|
// Must be called with the decoder monitor held.
|
|
|
|
void StopProgressUpdates() MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
// Allow updating the bytes downloaded for progress notifications. Must
|
|
|
|
// be called with the decoder monitor held.
|
|
|
|
void StartProgressUpdates() MOZ_OVERRIDE;
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Overridden to cleanup ref to |DASHDecoder|. Called on main thread only.
|
2012-09-29 16:29:04 -07:00
|
|
|
void Shutdown() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2012-12-13 11:42:45 -08:00
|
|
|
// Remove ref to state machine before |MediaDecoder|::|Shutdown|, since
|
|
|
|
// |DASHDecoder| is responsible for its shutdown.
|
|
|
|
mDecoderStateMachine = nullptr;
|
2012-09-29 16:29:04 -07:00
|
|
|
// Call parent class shutdown.
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaDecoder::Shutdown();
|
2012-12-23 20:45:57 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(mShuttingDown);
|
2012-09-29 16:29:04 -07:00
|
|
|
// Cleanup ref to main decoder.
|
|
|
|
mMainDecoder = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop reference to state machine and mReader (owned by state machine).
|
|
|
|
// Only called during shutdown dance.
|
|
|
|
void ReleaseStateMachine();
|
|
|
|
|
|
|
|
// Notifies the element that decoding has failed.
|
|
|
|
void DecodeError();
|
|
|
|
|
|
|
|
private:
|
2012-12-06 15:27:08 -08:00
|
|
|
// Populates |mByteRanges| by calling |GetIndexByteRanges| from |mReader|.
|
|
|
|
// Called on the main thread only.
|
|
|
|
nsresult PopulateByteRanges();
|
|
|
|
|
2012-09-29 16:29:04 -07:00
|
|
|
// The main decoder.
|
2012-11-14 11:46:40 -08:00
|
|
|
nsRefPtr<DASHDecoder> mMainDecoder;
|
2012-09-29 16:29:04 -07:00
|
|
|
// This decoder's MPD |Representation| object.
|
|
|
|
Representation const * mMPDRepresentation;
|
|
|
|
|
|
|
|
// Countdown var for loading metadata byte ranges.
|
|
|
|
uint16_t mMetadataChunkCount;
|
|
|
|
|
|
|
|
// All the byte ranges for this |Representation|.
|
|
|
|
nsTArray<MediaByteRange> mByteRanges;
|
|
|
|
|
|
|
|
// Byte range for the init and index bytes.
|
|
|
|
MediaByteRange mInitByteRange;
|
|
|
|
MediaByteRange mIndexByteRange;
|
|
|
|
|
|
|
|
// The current byte range being requested.
|
|
|
|
MediaByteRange mCurrentByteRange;
|
2012-12-06 15:27:08 -08:00
|
|
|
// Index of the current byte range. Initialized to -1.
|
|
|
|
int32_t mSubsegmentIdx;
|
2012-09-29 16:29:04 -07:00
|
|
|
|
|
|
|
// Ptr to the reader object for this |Representation|. Owned by state
|
|
|
|
// machine.
|
2012-12-06 15:27:08 -08:00
|
|
|
DASHRepReader* mReader;
|
2012-09-29 16:29:04 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
#endif //DASHRepDecoder_h_
|