/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */ #if !defined(MediaDecoderReader_h_) #define MediaDecoderReader_h_ #include "mozilla/EnumSet.h" #include "mozilla/MozPromise.h" #include "nsAutoPtr.h" #include "AbstractMediaDecoder.h" #include "MediaInfo.h" #include "MediaData.h" #include "MediaResult.h" #include "MediaMetadataManager.h" #include "MediaQueue.h" #include "MediaTimer.h" #include "AudioCompactor.h" #include "Intervals.h" #include "TimeUnits.h" #include "SeekTarget.h" namespace mozilla { class CDMProxy; class MediaDecoderReader; struct WaitForDataRejectValue { enum Reason { SHUTDOWN, CANCELED }; WaitForDataRejectValue(MediaData::Type aType, Reason aReason) :mType(aType), mReason(aReason) {} MediaData::Type mType; Reason mReason; }; class MetadataHolder { public: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataHolder) MediaInfo mInfo; nsAutoPtr mTags; private: virtual ~MetadataHolder() {} }; // Encapsulates the decoding and reading of media data. Reading can either // synchronous and done on the calling "decode" thread, or asynchronous and // performed on a background thread, with the result being returned by // callback. Never hold the decoder monitor when calling into this class. // Unless otherwise specified, methods and fields of this class can only // be accessed on the decode task queue. class MediaDecoderReader { friend class ReRequestVideoWithSkipTask; friend class ReRequestAudioTask; static const bool IsExclusive = true; public: using TrackSet = EnumSet; using MetadataPromise = MozPromise, MediaResult, IsExclusive>; using MediaDataPromise = MozPromise, MediaResult, IsExclusive>; using SeekPromise = MozPromise; // Note that, conceptually, WaitForData makes sense in a non-exclusive sense. // But in the current architecture it's only ever used exclusively (by MDSM), // so we mark it that way to verify our assumptions. If you have a use-case // for multiple WaitForData consumers, feel free to flip the exclusivity here. using WaitForDataPromise = MozPromise; using BufferedUpdatePromise = MozPromise; NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDecoderReader) // The caller must ensure that Shutdown() is called before aDecoder is // destroyed. explicit MediaDecoderReader(AbstractMediaDecoder* aDecoder); // Initializes the reader, returns NS_OK on success, or NS_ERROR_FAILURE // on failure. virtual nsresult Init() { return NS_OK; } // Called by MDSM in dormant state to release resources allocated by this // reader. The reader can resume decoding by calling Seek() to a specific // position. virtual void ReleaseResources() {} // Destroys the decoding state. The reader cannot be made usable again. // This is different from ReleaseMediaResources() as it is irreversable, // whereas ReleaseMediaResources() is. Must be called on the decode // thread. virtual RefPtr Shutdown(); virtual bool OnTaskQueue() const { return OwnerThread()->IsCurrentThreadIn(); } // Resets all state related to decoding, emptying all buffers etc. // Cancels all pending Request*Data() request callbacks, rejects any // outstanding seek promises, and flushes the decode pipeline. The // decoder must not call any of the callbacks for outstanding // Request*Data() calls after this is called. Calls to Request*Data() // made after this should be processed as usual. // // Normally this call preceedes a Seek() call, or shutdown. // // The first samples of every stream produced after a ResetDecode() call // *must* be marked as "discontinuities". If it's not, seeking work won't // properly! // // aParam is a set of TrackInfo::TrackType enums specifying which // queues need to be reset, defaulting to both audio and video tracks. virtual nsresult ResetDecode(TrackSet aTracks = TrackSet(TrackInfo::kAudioTrack, TrackInfo::kVideoTrack)); // Requests one audio sample from the reader. // // The decode should be performed asynchronously, and the promise should // be resolved when it is complete. Don't hold the decoder // monitor while calling this, as the implementation may try to wait // on something that needs the monitor and deadlock. virtual RefPtr RequestAudioData(); // Requests one video sample from the reader. // // Don't hold the decoder monitor while calling this, as the implementation // may try to wait on something that needs the monitor and deadlock. // If aSkipToKeyframe is true, the decode should skip ahead to the // the next keyframe at or after aTimeThreshold microseconds. virtual RefPtr RequestVideoData(bool aSkipToNextKeyframe, int64_t aTimeThreshold); // By default, the state machine polls the reader once per second when it's // in buffering mode. Some readers support a promise-based mechanism by which // they notify the state machine when the data arrives. virtual bool IsWaitForDataSupported() const { return false; } virtual RefPtr WaitForData(MediaData::Type aType) { MOZ_CRASH(); } // By default, the reader return the decoded data. Some readers support // retuning demuxed data. virtual bool IsDemuxOnlySupported() const { return false; } // Configure the reader to return demuxed or decoded data // upon calls to Request{Audio,Video}Data. virtual void SetDemuxOnly(bool /*aDemuxedOnly*/) {} // The default implementation of AsyncReadMetadata is implemented in terms of // synchronous ReadMetadata() calls. Implementations may also // override AsyncReadMetadata to create a more proper async implementation. virtual RefPtr AsyncReadMetadata(); // Fills aInfo with the latest cached data required to present the media, // ReadUpdatedMetadata will always be called once ReadMetadata has succeeded. virtual void ReadUpdatedMetadata(MediaInfo* aInfo) {} // Moves the decode head to aTime microseconds. aEndTime denotes the end // time of the media in usecs. This is only needed for OggReader, and should // probably be removed somehow. virtual RefPtr Seek(SeekTarget aTarget, int64_t aEndTime) = 0; // Called to move the reader into idle state. When the reader is // created it is assumed to be active (i.e. not idle). When the media // element is paused and we don't need to decode any more data, the state // machine calls SetIdle() to inform the reader that its decoder won't be // needed for a while. The reader can use these notifications to enter // a low power state when the decoder isn't needed, if desired. // This is most useful on mobile. // Note: DecodeVideoFrame, DecodeAudioData, ReadMetadata and Seek should // activate the decoder if necessary. The state machine only needs to know // when to call SetIdle(). virtual void SetIdle() {} virtual void SetCDMProxy(CDMProxy* aProxy) {} // Tell the reader that the data decoded are not for direct playback, so it // can accept more files, in particular those which have more channels than // available in the audio output. void SetIgnoreAudioOutputFormat() { mIgnoreAudioOutputFormat = true; } // MediaSourceReader opts out of the start-time-guessing mechanism. virtual bool ForceZeroStartTime() const { return false; } // The MediaDecoderStateMachine uses various heuristics that assume that // raw media data is arriving sequentially from a network channel. This // makes sense in the