content/media/AbstractMediaDecoder.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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
michael@0 7 #ifndef AbstractMediaDecoder_h_
michael@0 8 #define AbstractMediaDecoder_h_
michael@0 9
michael@0 10 #include "mozilla/Attributes.h"
michael@0 11 #include "nsISupports.h"
michael@0 12 #include "nsDataHashtable.h"
michael@0 13 #include "nsThreadUtils.h"
michael@0 14
michael@0 15 namespace mozilla
michael@0 16 {
michael@0 17
michael@0 18 namespace layers
michael@0 19 {
michael@0 20 class ImageContainer;
michael@0 21 }
michael@0 22 class MediaResource;
michael@0 23 class ReentrantMonitor;
michael@0 24 class VideoFrameContainer;
michael@0 25 class TimedMetadata;
michael@0 26 class MediaDecoderOwner;
michael@0 27
michael@0 28 typedef nsDataHashtable<nsCStringHashKey, nsCString> MetadataTags;
michael@0 29
michael@0 30 static inline bool IsCurrentThread(nsIThread* aThread) {
michael@0 31 return NS_GetCurrentThread() == aThread;
michael@0 32 }
michael@0 33
michael@0 34 /**
michael@0 35 * The AbstractMediaDecoder class describes the public interface for a media decoder
michael@0 36 * and is used by the MediaReader classes.
michael@0 37 */
michael@0 38 class AbstractMediaDecoder : public nsISupports
michael@0 39 {
michael@0 40 public:
michael@0 41 // Returns the monitor for other threads to synchronise access to
michael@0 42 // state.
michael@0 43 virtual ReentrantMonitor& GetReentrantMonitor() = 0;
michael@0 44
michael@0 45 // Returns true if the decoder is shut down.
michael@0 46 virtual bool IsShutdown() const = 0;
michael@0 47
michael@0 48 virtual bool OnStateMachineThread() const = 0;
michael@0 49
michael@0 50 virtual bool OnDecodeThread() const = 0;
michael@0 51
michael@0 52 // Get the current MediaResource being used. Its URI will be returned
michael@0 53 // by currentSrc. Returns what was passed to Load(), if Load() has been called.
michael@0 54 virtual MediaResource* GetResource() const = 0;
michael@0 55
michael@0 56 // Called by the decode thread to keep track of the number of bytes read
michael@0 57 // from the resource.
michael@0 58 virtual void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset) = 0;
michael@0 59
michael@0 60 // Increments the parsed and decoded frame counters by the passed in counts.
michael@0 61 // Can be called on any thread.
michael@0 62 virtual void NotifyDecodedFrames(uint32_t aParsed, uint32_t aDecoded) = 0;
michael@0 63
michael@0 64 // Returns the end time of the last sample in the media. Note that a media
michael@0 65 // can have a non-zero start time, so the end time may not necessarily be
michael@0 66 // the same as the duration (i.e. duration is (end_time - start_time)).
michael@0 67 virtual int64_t GetEndMediaTime() const = 0;
michael@0 68
michael@0 69 // Return the duration of the media in microseconds.
michael@0 70 virtual int64_t GetMediaDuration() = 0;
michael@0 71
michael@0 72 // Set the duration of the media in microseconds.
michael@0 73 virtual void SetMediaDuration(int64_t aDuration) = 0;
michael@0 74
michael@0 75 // Sets the duration of the media in microseconds. The MediaDecoder
michael@0 76 // fires a durationchange event to its owner (e.g., an HTML audio
michael@0 77 // tag).
michael@0 78 virtual void UpdateEstimatedMediaDuration(int64_t aDuration) = 0;
michael@0 79
michael@0 80 // Set the media as being seekable or not.
michael@0 81 virtual void SetMediaSeekable(bool aMediaSeekable) = 0;
michael@0 82
michael@0 83 // Set the transport level as being seekable or not.
michael@0 84 virtual void SetTransportSeekable(bool aTransportSeekable) = 0;
michael@0 85
michael@0 86 virtual VideoFrameContainer* GetVideoFrameContainer() = 0;
michael@0 87 virtual mozilla::layers::ImageContainer* GetImageContainer() = 0;
michael@0 88
michael@0 89 // Return true if the media layer supports seeking.
michael@0 90 virtual bool IsTransportSeekable() = 0;
michael@0 91
michael@0 92 // Return true if the transport layer supports seeking.
michael@0 93 virtual bool IsMediaSeekable() = 0;
michael@0 94
michael@0 95 virtual void MetadataLoaded(int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0;
michael@0 96 virtual void QueueMetadata(int64_t aTime, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0;
michael@0 97
michael@0 98 // Set the media end time in microseconds
michael@0 99 virtual void SetMediaEndTime(int64_t aTime) = 0;
michael@0 100
michael@0 101 // Make the decoder state machine update the playback position. Called by
michael@0 102 // the reader on the decoder thread (Assertions for this checked by
michael@0 103 // mDecoderStateMachine). This must be called with the decode monitor
michael@0 104 // held.
michael@0 105 virtual void UpdatePlaybackPosition(int64_t aTime) = 0;
michael@0 106
michael@0 107 // May be called by the reader to notify this decoder that the metadata from
michael@0 108 // the media file has been read. Call on the decode thread only.
michael@0 109 virtual void OnReadMetadataCompleted() = 0;
michael@0 110
michael@0 111 // Returns the owner of this media decoder. The owner should only be used
michael@0 112 // on the main thread.
michael@0 113 virtual MediaDecoderOwner* GetOwner() = 0;
michael@0 114
michael@0 115 // May be called by the reader to notify the decoder that the resources
michael@0 116 // required to begin playback have been acquired. Can be called on any thread.
michael@0 117 virtual void NotifyWaitingForResourcesStatusChanged() = 0;
michael@0 118
michael@0 119 // Called by Reader if the current audio track can be offloaded
michael@0 120 virtual void SetCanOffloadAudio(bool aCanOffloadAudio) {}
michael@0 121
michael@0 122 // Called from HTMLMediaElement when owner document activity changes
michael@0 123 virtual void SetElementVisibility(bool aIsVisible) {}
michael@0 124
michael@0 125 // Stack based class to assist in notifying the frame statistics of
michael@0 126 // parsed and decoded frames. Use inside video demux & decode functions
michael@0 127 // to ensure all parsed and decoded frames are reported on all return paths.
michael@0 128 class AutoNotifyDecoded {
michael@0 129 public:
michael@0 130 AutoNotifyDecoded(AbstractMediaDecoder* aDecoder, uint32_t& aParsed, uint32_t& aDecoded)
michael@0 131 : mDecoder(aDecoder), mParsed(aParsed), mDecoded(aDecoded) {}
michael@0 132 ~AutoNotifyDecoded() {
michael@0 133 mDecoder->NotifyDecodedFrames(mParsed, mDecoded);
michael@0 134 }
michael@0 135 private:
michael@0 136 AbstractMediaDecoder* mDecoder;
michael@0 137 uint32_t& mParsed;
michael@0 138 uint32_t& mDecoded;
michael@0 139 };
michael@0 140 };
michael@0 141
michael@0 142 class AudioMetadataEventRunner : public nsRunnable
michael@0 143 {
michael@0 144 private:
michael@0 145 nsRefPtr<AbstractMediaDecoder> mDecoder;
michael@0 146 public:
michael@0 147 AudioMetadataEventRunner(AbstractMediaDecoder* aDecoder, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags)
michael@0 148 : mDecoder(aDecoder),
michael@0 149 mChannels(aChannels),
michael@0 150 mRate(aRate),
michael@0 151 mHasAudio(aHasAudio),
michael@0 152 mHasVideo(aHasVideo),
michael@0 153 mTags(aTags)
michael@0 154 {}
michael@0 155
michael@0 156 NS_IMETHOD Run() MOZ_OVERRIDE
michael@0 157 {
michael@0 158 mDecoder->MetadataLoaded(mChannels, mRate, mHasAudio, mHasVideo, mTags);
michael@0 159 return NS_OK;
michael@0 160 }
michael@0 161
michael@0 162 int mChannels;
michael@0 163 int mRate;
michael@0 164 bool mHasAudio;
michael@0 165 bool mHasVideo;
michael@0 166 MetadataTags* mTags;
michael@0 167 };
michael@0 168
michael@0 169
michael@0 170 }
michael@0 171
michael@0 172 #endif
michael@0 173

mercurial