Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | #if !defined(MediaDecoderReader_h_) |
michael@0 | 7 | #define MediaDecoderReader_h_ |
michael@0 | 8 | |
michael@0 | 9 | #include "AbstractMediaDecoder.h" |
michael@0 | 10 | #include "MediaInfo.h" |
michael@0 | 11 | #include "MediaData.h" |
michael@0 | 12 | #include "MediaQueue.h" |
michael@0 | 13 | #include "AudioCompactor.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | |
michael@0 | 17 | namespace dom { |
michael@0 | 18 | class TimeRanges; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | // Encapsulates the decoding and reading of media data. Reading can only be |
michael@0 | 22 | // done on the decode thread. Never hold the decoder monitor when |
michael@0 | 23 | // calling into this class. Unless otherwise specified, methods and fields of |
michael@0 | 24 | // this class can only be accessed on the decode thread. |
michael@0 | 25 | class MediaDecoderReader { |
michael@0 | 26 | public: |
michael@0 | 27 | MediaDecoderReader(AbstractMediaDecoder* aDecoder); |
michael@0 | 28 | virtual ~MediaDecoderReader(); |
michael@0 | 29 | |
michael@0 | 30 | // Initializes the reader, returns NS_OK on success, or NS_ERROR_FAILURE |
michael@0 | 31 | // on failure. |
michael@0 | 32 | virtual nsresult Init(MediaDecoderReader* aCloneDonor) = 0; |
michael@0 | 33 | |
michael@0 | 34 | // True if this reader is waiting media resource allocation |
michael@0 | 35 | virtual bool IsWaitingMediaResources() { return false; } |
michael@0 | 36 | // True when this reader need to become dormant state |
michael@0 | 37 | virtual bool IsDormantNeeded() { return false; } |
michael@0 | 38 | // Release media resources they should be released in dormant state |
michael@0 | 39 | virtual void ReleaseMediaResources() {}; |
michael@0 | 40 | // Release the decoder during shutdown |
michael@0 | 41 | virtual void ReleaseDecoder() {}; |
michael@0 | 42 | |
michael@0 | 43 | // Resets all state related to decoding, emptying all buffers etc. |
michael@0 | 44 | virtual nsresult ResetDecode(); |
michael@0 | 45 | |
michael@0 | 46 | // Decodes an unspecified amount of audio data, enqueuing the audio data |
michael@0 | 47 | // in mAudioQueue. Returns true when there's more audio to decode, |
michael@0 | 48 | // false if the audio is finished, end of file has been reached, |
michael@0 | 49 | // or an un-recoverable read error has occured. |
michael@0 | 50 | virtual bool DecodeAudioData() = 0; |
michael@0 | 51 | |
michael@0 | 52 | // Reads and decodes one video frame. Packets with a timestamp less |
michael@0 | 53 | // than aTimeThreshold will be decoded (unless they're not keyframes |
michael@0 | 54 | // and aKeyframeSkip is true), but will not be added to the queue. |
michael@0 | 55 | virtual bool DecodeVideoFrame(bool &aKeyframeSkip, |
michael@0 | 56 | int64_t aTimeThreshold) = 0; |
michael@0 | 57 | |
michael@0 | 58 | virtual bool HasAudio() = 0; |
michael@0 | 59 | virtual bool HasVideo() = 0; |
michael@0 | 60 | |
michael@0 | 61 | // Read header data for all bitstreams in the file. Fills aInfo with |
michael@0 | 62 | // the data required to present the media, and optionally fills *aTags |
michael@0 | 63 | // with tag metadata from the file. |
michael@0 | 64 | // Returns NS_OK on success, or NS_ERROR_FAILURE on failure. |
michael@0 | 65 | virtual nsresult ReadMetadata(MediaInfo* aInfo, |
michael@0 | 66 | MetadataTags** aTags) = 0; |
michael@0 | 67 | |
michael@0 | 68 | // Stores the presentation time of the first frame we'd be able to play if |
michael@0 | 69 | // we started playback at the current position. Returns the first video |
michael@0 | 70 | // frame, if we have video. |
michael@0 | 71 | virtual VideoData* FindStartTime(int64_t& aOutStartTime); |
michael@0 | 72 | |
michael@0 | 73 | // Moves the decode head to aTime microseconds. aStartTime and aEndTime |
michael@0 | 74 | // denote the start and end times of the media in usecs, and aCurrentTime |
michael@0 | 75 | // is the current playback position in microseconds. |
michael@0 | 76 | virtual nsresult Seek(int64_t aTime, |
michael@0 | 77 | int64_t aStartTime, |
michael@0 | 78 | int64_t aEndTime, |
michael@0 | 79 | int64_t aCurrentTime) = 0; |
michael@0 | 80 | |
michael@0 | 81 | // Called to move the reader into idle/active state. When the reader is |
michael@0 | 82 | // created it is assumed to be active (i.e. not idle). When the media |
michael@0 | 83 | // element is paused and we don't need to decode any more data, the state |
michael@0 | 84 | // machine calls SetIdle() to inform the reader that its decoder won't be |
michael@0 | 85 | // needed for a while. When we need to decode data again, the state machine |
michael@0 | 86 | // calls SetActive() to activate the decoder. The reader can use these |
michael@0 | 87 | // notifications to enter/exit a low power state when the decoder isn't |
michael@0 | 88 | // needed, if desired. This is most useful on mobile. |
michael@0 | 89 | virtual void SetIdle() { } |
michael@0 | 90 | virtual void SetActive() { } |
michael@0 | 91 | |
michael@0 | 92 | // Tell the reader that the data decoded are not for direct playback, so it |
michael@0 | 93 | // can accept more files, in particular those which have more channels than |
michael@0 | 94 | // available in the audio output. |
michael@0 | 95 | void SetIgnoreAudioOutputFormat() |
michael@0 | 96 | { |
michael@0 | 97 | mIgnoreAudioOutputFormat = true; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | protected: |
michael@0 | 101 | // Queue of audio frames. This queue is threadsafe, and is accessed from |
michael@0 | 102 | // the audio, decoder, state machine, and main threads. |
michael@0 | 103 | MediaQueue<AudioData> mAudioQueue; |
michael@0 | 104 | |
michael@0 | 105 | // Queue of video frames. This queue is threadsafe, and is accessed from |
michael@0 | 106 | // the decoder, state machine, and main threads. |
michael@0 | 107 | MediaQueue<VideoData> mVideoQueue; |
michael@0 | 108 | |
michael@0 | 109 | // An adapter to the audio queue which first copies data to buffers with |
michael@0 | 110 | // minimal allocation slop and then pushes them to the queue. This is |
michael@0 | 111 | // useful for decoders working with formats that give awkward numbers of |
michael@0 | 112 | // frames such as mp3. |
michael@0 | 113 | AudioCompactor mAudioCompactor; |
michael@0 | 114 | |
michael@0 | 115 | public: |
michael@0 | 116 | // Populates aBuffered with the time ranges which are buffered. aStartTime |
michael@0 | 117 | // must be the presentation time of the first frame in the media, e.g. |
michael@0 | 118 | // the media time corresponding to playback time/position 0. This function |
michael@0 | 119 | // is called on the main, decode, and state machine threads. |
michael@0 | 120 | // |
michael@0 | 121 | // This base implementation in MediaDecoderReader estimates the time ranges |
michael@0 | 122 | // buffered by interpolating the cached byte ranges with the duration |
michael@0 | 123 | // of the media. Reader subclasses should override this method if they |
michael@0 | 124 | // can quickly calculate the buffered ranges more accurately. |
michael@0 | 125 | // |
michael@0 | 126 | // The primary advantage of this implementation in the reader base class |
michael@0 | 127 | // is that it's a fast approximation, which does not perform any I/O. |
michael@0 | 128 | // |
michael@0 | 129 | // The OggReader relies on this base implementation not performing I/O, |
michael@0 | 130 | // since in FirefoxOS we can't do I/O on the main thread, where this is |
michael@0 | 131 | // called. |
michael@0 | 132 | virtual nsresult GetBuffered(dom::TimeRanges* aBuffered, |
michael@0 | 133 | int64_t aStartTime); |
michael@0 | 134 | |
michael@0 | 135 | // Returns the number of bytes of memory allocated by structures/frames in |
michael@0 | 136 | // the video queue. |
michael@0 | 137 | size_t SizeOfVideoQueueInBytes() const; |
michael@0 | 138 | |
michael@0 | 139 | // Returns the number of bytes of memory allocated by structures/frames in |
michael@0 | 140 | // the audio queue. |
michael@0 | 141 | size_t SizeOfAudioQueueInBytes() const; |
michael@0 | 142 | |
michael@0 | 143 | // Only used by WebMReader and MediaOmxReader for now, so stub here rather |
michael@0 | 144 | // than in every reader than inherits from MediaDecoderReader. |
michael@0 | 145 | virtual void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) {} |
michael@0 | 146 | |
michael@0 | 147 | virtual MediaQueue<AudioData>& AudioQueue() { return mAudioQueue; } |
michael@0 | 148 | virtual MediaQueue<VideoData>& VideoQueue() { return mVideoQueue; } |
michael@0 | 149 | |
michael@0 | 150 | // Returns a pointer to the decoder. |
michael@0 | 151 | AbstractMediaDecoder* GetDecoder() { |
michael@0 | 152 | return mDecoder; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | AudioData* DecodeToFirstAudioData(); |
michael@0 | 156 | VideoData* DecodeToFirstVideoData(); |
michael@0 | 157 | |
michael@0 | 158 | // Decodes samples until we reach frames required to play at time aTarget |
michael@0 | 159 | // (usecs). This also trims the samples to start exactly at aTarget, |
michael@0 | 160 | // by discarding audio samples and adjusting start times of video frames. |
michael@0 | 161 | nsresult DecodeToTarget(int64_t aTarget); |
michael@0 | 162 | |
michael@0 | 163 | MediaInfo GetMediaInfo() { return mInfo; } |
michael@0 | 164 | |
michael@0 | 165 | protected: |
michael@0 | 166 | |
michael@0 | 167 | // Reference to the owning decoder object. |
michael@0 | 168 | AbstractMediaDecoder* mDecoder; |
michael@0 | 169 | |
michael@0 | 170 | // Stores presentation info required for playback. |
michael@0 | 171 | MediaInfo mInfo; |
michael@0 | 172 | |
michael@0 | 173 | // Whether we should accept media that we know we can't play |
michael@0 | 174 | // directly, because they have a number of channel higher than |
michael@0 | 175 | // what we support. |
michael@0 | 176 | bool mIgnoreAudioOutputFormat; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | } // namespace mozilla |
michael@0 | 180 | |
michael@0 | 181 | #endif |