1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/AbstractMediaDecoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef AbstractMediaDecoder_h_ 1.11 +#define AbstractMediaDecoder_h_ 1.12 + 1.13 +#include "mozilla/Attributes.h" 1.14 +#include "nsISupports.h" 1.15 +#include "nsDataHashtable.h" 1.16 +#include "nsThreadUtils.h" 1.17 + 1.18 +namespace mozilla 1.19 +{ 1.20 + 1.21 +namespace layers 1.22 +{ 1.23 + class ImageContainer; 1.24 +} 1.25 +class MediaResource; 1.26 +class ReentrantMonitor; 1.27 +class VideoFrameContainer; 1.28 +class TimedMetadata; 1.29 +class MediaDecoderOwner; 1.30 + 1.31 +typedef nsDataHashtable<nsCStringHashKey, nsCString> MetadataTags; 1.32 + 1.33 +static inline bool IsCurrentThread(nsIThread* aThread) { 1.34 + return NS_GetCurrentThread() == aThread; 1.35 +} 1.36 + 1.37 +/** 1.38 + * The AbstractMediaDecoder class describes the public interface for a media decoder 1.39 + * and is used by the MediaReader classes. 1.40 + */ 1.41 +class AbstractMediaDecoder : public nsISupports 1.42 +{ 1.43 +public: 1.44 + // Returns the monitor for other threads to synchronise access to 1.45 + // state. 1.46 + virtual ReentrantMonitor& GetReentrantMonitor() = 0; 1.47 + 1.48 + // Returns true if the decoder is shut down. 1.49 + virtual bool IsShutdown() const = 0; 1.50 + 1.51 + virtual bool OnStateMachineThread() const = 0; 1.52 + 1.53 + virtual bool OnDecodeThread() const = 0; 1.54 + 1.55 + // Get the current MediaResource being used. Its URI will be returned 1.56 + // by currentSrc. Returns what was passed to Load(), if Load() has been called. 1.57 + virtual MediaResource* GetResource() const = 0; 1.58 + 1.59 + // Called by the decode thread to keep track of the number of bytes read 1.60 + // from the resource. 1.61 + virtual void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset) = 0; 1.62 + 1.63 + // Increments the parsed and decoded frame counters by the passed in counts. 1.64 + // Can be called on any thread. 1.65 + virtual void NotifyDecodedFrames(uint32_t aParsed, uint32_t aDecoded) = 0; 1.66 + 1.67 + // Returns the end time of the last sample in the media. Note that a media 1.68 + // can have a non-zero start time, so the end time may not necessarily be 1.69 + // the same as the duration (i.e. duration is (end_time - start_time)). 1.70 + virtual int64_t GetEndMediaTime() const = 0; 1.71 + 1.72 + // Return the duration of the media in microseconds. 1.73 + virtual int64_t GetMediaDuration() = 0; 1.74 + 1.75 + // Set the duration of the media in microseconds. 1.76 + virtual void SetMediaDuration(int64_t aDuration) = 0; 1.77 + 1.78 + // Sets the duration of the media in microseconds. The MediaDecoder 1.79 + // fires a durationchange event to its owner (e.g., an HTML audio 1.80 + // tag). 1.81 + virtual void UpdateEstimatedMediaDuration(int64_t aDuration) = 0; 1.82 + 1.83 + // Set the media as being seekable or not. 1.84 + virtual void SetMediaSeekable(bool aMediaSeekable) = 0; 1.85 + 1.86 + // Set the transport level as being seekable or not. 1.87 + virtual void SetTransportSeekable(bool aTransportSeekable) = 0; 1.88 + 1.89 + virtual VideoFrameContainer* GetVideoFrameContainer() = 0; 1.90 + virtual mozilla::layers::ImageContainer* GetImageContainer() = 0; 1.91 + 1.92 + // Return true if the media layer supports seeking. 1.93 + virtual bool IsTransportSeekable() = 0; 1.94 + 1.95 + // Return true if the transport layer supports seeking. 1.96 + virtual bool IsMediaSeekable() = 0; 1.97 + 1.98 + virtual void MetadataLoaded(int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0; 1.99 + virtual void QueueMetadata(int64_t aTime, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0; 1.100 + 1.101 + // Set the media end time in microseconds 1.102 + virtual void SetMediaEndTime(int64_t aTime) = 0; 1.103 + 1.104 + // Make the decoder state machine update the playback position. Called by 1.105 + // the reader on the decoder thread (Assertions for this checked by 1.106 + // mDecoderStateMachine). This must be called with the decode monitor 1.107 + // held. 1.108 + virtual void UpdatePlaybackPosition(int64_t aTime) = 0; 1.109 + 1.110 + // May be called by the reader to notify this decoder that the metadata from 1.111 + // the media file has been read. Call on the decode thread only. 1.112 + virtual void OnReadMetadataCompleted() = 0; 1.113 + 1.114 + // Returns the owner of this media decoder. The owner should only be used 1.115 + // on the main thread. 1.116 + virtual MediaDecoderOwner* GetOwner() = 0; 1.117 + 1.118 + // May be called by the reader to notify the decoder that the resources 1.119 + // required to begin playback have been acquired. Can be called on any thread. 1.120 + virtual void NotifyWaitingForResourcesStatusChanged() = 0; 1.121 + 1.122 + // Called by Reader if the current audio track can be offloaded 1.123 + virtual void SetCanOffloadAudio(bool aCanOffloadAudio) {} 1.124 + 1.125 + // Called from HTMLMediaElement when owner document activity changes 1.126 + virtual void SetElementVisibility(bool aIsVisible) {} 1.127 + 1.128 + // Stack based class to assist in notifying the frame statistics of 1.129 + // parsed and decoded frames. Use inside video demux & decode functions 1.130 + // to ensure all parsed and decoded frames are reported on all return paths. 1.131 + class AutoNotifyDecoded { 1.132 + public: 1.133 + AutoNotifyDecoded(AbstractMediaDecoder* aDecoder, uint32_t& aParsed, uint32_t& aDecoded) 1.134 + : mDecoder(aDecoder), mParsed(aParsed), mDecoded(aDecoded) {} 1.135 + ~AutoNotifyDecoded() { 1.136 + mDecoder->NotifyDecodedFrames(mParsed, mDecoded); 1.137 + } 1.138 + private: 1.139 + AbstractMediaDecoder* mDecoder; 1.140 + uint32_t& mParsed; 1.141 + uint32_t& mDecoded; 1.142 + }; 1.143 +}; 1.144 + 1.145 +class AudioMetadataEventRunner : public nsRunnable 1.146 +{ 1.147 + private: 1.148 + nsRefPtr<AbstractMediaDecoder> mDecoder; 1.149 + public: 1.150 + AudioMetadataEventRunner(AbstractMediaDecoder* aDecoder, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) 1.151 + : mDecoder(aDecoder), 1.152 + mChannels(aChannels), 1.153 + mRate(aRate), 1.154 + mHasAudio(aHasAudio), 1.155 + mHasVideo(aHasVideo), 1.156 + mTags(aTags) 1.157 + {} 1.158 + 1.159 + NS_IMETHOD Run() MOZ_OVERRIDE 1.160 + { 1.161 + mDecoder->MetadataLoaded(mChannels, mRate, mHasAudio, mHasVideo, mTags); 1.162 + return NS_OK; 1.163 + } 1.164 + 1.165 + int mChannels; 1.166 + int mRate; 1.167 + bool mHasAudio; 1.168 + bool mHasVideo; 1.169 + MetadataTags* mTags; 1.170 +}; 1.171 + 1.172 + 1.173 +} 1.174 + 1.175 +#endif 1.176 +