michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef AbstractMediaDecoder_h_ michael@0: #define AbstractMediaDecoder_h_ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsISupports.h" michael@0: #include "nsDataHashtable.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: namespace mozilla michael@0: { michael@0: michael@0: namespace layers michael@0: { michael@0: class ImageContainer; michael@0: } michael@0: class MediaResource; michael@0: class ReentrantMonitor; michael@0: class VideoFrameContainer; michael@0: class TimedMetadata; michael@0: class MediaDecoderOwner; michael@0: michael@0: typedef nsDataHashtable MetadataTags; michael@0: michael@0: static inline bool IsCurrentThread(nsIThread* aThread) { michael@0: return NS_GetCurrentThread() == aThread; michael@0: } michael@0: michael@0: /** michael@0: * The AbstractMediaDecoder class describes the public interface for a media decoder michael@0: * and is used by the MediaReader classes. michael@0: */ michael@0: class AbstractMediaDecoder : public nsISupports michael@0: { michael@0: public: michael@0: // Returns the monitor for other threads to synchronise access to michael@0: // state. michael@0: virtual ReentrantMonitor& GetReentrantMonitor() = 0; michael@0: michael@0: // Returns true if the decoder is shut down. michael@0: virtual bool IsShutdown() const = 0; michael@0: michael@0: virtual bool OnStateMachineThread() const = 0; michael@0: michael@0: virtual bool OnDecodeThread() const = 0; michael@0: michael@0: // Get the current MediaResource being used. Its URI will be returned michael@0: // by currentSrc. Returns what was passed to Load(), if Load() has been called. michael@0: virtual MediaResource* GetResource() const = 0; michael@0: michael@0: // Called by the decode thread to keep track of the number of bytes read michael@0: // from the resource. michael@0: virtual void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset) = 0; michael@0: michael@0: // Increments the parsed and decoded frame counters by the passed in counts. michael@0: // Can be called on any thread. michael@0: virtual void NotifyDecodedFrames(uint32_t aParsed, uint32_t aDecoded) = 0; michael@0: michael@0: // Returns the end time of the last sample in the media. Note that a media michael@0: // can have a non-zero start time, so the end time may not necessarily be michael@0: // the same as the duration (i.e. duration is (end_time - start_time)). michael@0: virtual int64_t GetEndMediaTime() const = 0; michael@0: michael@0: // Return the duration of the media in microseconds. michael@0: virtual int64_t GetMediaDuration() = 0; michael@0: michael@0: // Set the duration of the media in microseconds. michael@0: virtual void SetMediaDuration(int64_t aDuration) = 0; michael@0: michael@0: // Sets the duration of the media in microseconds. The MediaDecoder michael@0: // fires a durationchange event to its owner (e.g., an HTML audio michael@0: // tag). michael@0: virtual void UpdateEstimatedMediaDuration(int64_t aDuration) = 0; michael@0: michael@0: // Set the media as being seekable or not. michael@0: virtual void SetMediaSeekable(bool aMediaSeekable) = 0; michael@0: michael@0: // Set the transport level as being seekable or not. michael@0: virtual void SetTransportSeekable(bool aTransportSeekable) = 0; michael@0: michael@0: virtual VideoFrameContainer* GetVideoFrameContainer() = 0; michael@0: virtual mozilla::layers::ImageContainer* GetImageContainer() = 0; michael@0: michael@0: // Return true if the media layer supports seeking. michael@0: virtual bool IsTransportSeekable() = 0; michael@0: michael@0: // Return true if the transport layer supports seeking. michael@0: virtual bool IsMediaSeekable() = 0; michael@0: michael@0: virtual void MetadataLoaded(int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0; michael@0: virtual void QueueMetadata(int64_t aTime, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0; michael@0: michael@0: // Set the media end time in microseconds michael@0: virtual void SetMediaEndTime(int64_t aTime) = 0; michael@0: michael@0: // Make the decoder state machine update the playback position. Called by michael@0: // the reader on the decoder thread (Assertions for this checked by michael@0: // mDecoderStateMachine). This must be called with the decode monitor michael@0: // held. michael@0: virtual void UpdatePlaybackPosition(int64_t aTime) = 0; michael@0: michael@0: // May be called by the reader to notify this decoder that the metadata from michael@0: // the media file has been read. Call on the decode thread only. michael@0: virtual void OnReadMetadataCompleted() = 0; michael@0: michael@0: // Returns the owner of this media decoder. The owner should only be used michael@0: // on the main thread. michael@0: virtual MediaDecoderOwner* GetOwner() = 0; michael@0: michael@0: // May be called by the reader to notify the decoder that the resources michael@0: // required to begin playback have been acquired. Can be called on any thread. michael@0: virtual void NotifyWaitingForResourcesStatusChanged() = 0; michael@0: michael@0: // Called by Reader if the current audio track can be offloaded michael@0: virtual void SetCanOffloadAudio(bool aCanOffloadAudio) {} michael@0: michael@0: // Called from HTMLMediaElement when owner document activity changes michael@0: virtual void SetElementVisibility(bool aIsVisible) {} michael@0: michael@0: // Stack based class to assist in notifying the frame statistics of michael@0: // parsed and decoded frames. Use inside video demux & decode functions michael@0: // to ensure all parsed and decoded frames are reported on all return paths. michael@0: class AutoNotifyDecoded { michael@0: public: michael@0: AutoNotifyDecoded(AbstractMediaDecoder* aDecoder, uint32_t& aParsed, uint32_t& aDecoded) michael@0: : mDecoder(aDecoder), mParsed(aParsed), mDecoded(aDecoded) {} michael@0: ~AutoNotifyDecoded() { michael@0: mDecoder->NotifyDecodedFrames(mParsed, mDecoded); michael@0: } michael@0: private: michael@0: AbstractMediaDecoder* mDecoder; michael@0: uint32_t& mParsed; michael@0: uint32_t& mDecoded; michael@0: }; michael@0: }; michael@0: michael@0: class AudioMetadataEventRunner : public nsRunnable michael@0: { michael@0: private: michael@0: nsRefPtr mDecoder; michael@0: public: michael@0: AudioMetadataEventRunner(AbstractMediaDecoder* aDecoder, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) michael@0: : mDecoder(aDecoder), michael@0: mChannels(aChannels), michael@0: mRate(aRate), michael@0: mHasAudio(aHasAudio), michael@0: mHasVideo(aHasVideo), michael@0: mTags(aTags) michael@0: {} michael@0: michael@0: NS_IMETHOD Run() MOZ_OVERRIDE michael@0: { michael@0: mDecoder->MetadataLoaded(mChannels, mRate, mHasAudio, mHasVideo, mTags); michael@0: return NS_OK; michael@0: } michael@0: michael@0: int mChannels; michael@0: int mRate; michael@0: bool mHasAudio; michael@0: bool mHasVideo; michael@0: MetadataTags* mTags; michael@0: }; michael@0: michael@0: michael@0: } michael@0: michael@0: #endif michael@0: