MediaSink.h (5879B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MediaSink_h_ 8 #define MediaSink_h_ 9 10 #include "MediaInfo.h" 11 #include "mozilla/MozPromise.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/dom/MediaDebugInfoBinding.h" 14 #include "nsISupportsImpl.h" 15 16 class AudioDeviceInfo; 17 18 namespace mozilla { 19 20 class TimeStamp; 21 class VideoFrameContainer; 22 23 /** 24 * A consumer of audio/video data which plays audio and video tracks and 25 * manages A/V sync between them. 26 * 27 * A typical sink sends audio/video outputs to the speaker and screen. 28 * However, there are also sinks which capture the output of an media element 29 * and send the output to a MediaStream. 30 * 31 * This class is used to move A/V sync management and audio/video rendering 32 * out of MDSM so it is possible for subclasses to do external rendering using 33 * specific hardware which is required by TV projects and CDM. 34 * 35 * Note this class is not thread-safe and should be called from the state 36 * machine thread only. 37 */ 38 class MediaSink { 39 public: 40 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaSink); 41 typedef mozilla::TrackInfo::TrackType TrackType; 42 43 // EndedPromise needs to be a non-exclusive promise as it is shared between 44 // both the AudioSink and VideoSink. 45 typedef MozPromise<bool, nsresult, /* IsExclusive = */ false> EndedPromise; 46 47 // Return a promise which is resolved when the track finishes 48 // or null if no such track. 49 // Must be called after Start(). 50 // Returns null if the TrackType does not exist or if Stop() has been called. 51 virtual RefPtr<EndedPromise> OnEnded(TrackType aType) = 0; 52 53 // Return the end time of the audio/video data that has been consumed 54 // or 0 if no such track. 55 // Must be called after playback starts. 56 virtual media::TimeUnit GetEndTime(TrackType aType) const = 0; 57 58 // Return playback position for the media data. 59 // Since A/V sync is always maintained by this sink, there is no need to 60 // specify whether we want to get audio or video position. 61 // aTimeStamp returns the timeStamp corresponding to the returned position 62 // which is used by the compositor to derive the render time of video frames. 63 // Must be called after playback starts. 64 virtual media::TimeUnit GetPosition(TimeStamp* aTimeStamp = nullptr) = 0; 65 66 // Return true if there are data consumed but not played yet. 67 // Can be called in any state. 68 virtual bool HasUnplayedFrames(TrackType aType) const = 0; 69 70 // Return the duration of data consumed but not played yet. 71 // Can be called in any state. 72 virtual media::TimeUnit UnplayedDuration(TrackType aType) const = 0; 73 74 // Set volume of the audio track. 75 // Do nothing if this sink has no audio track. 76 // Can be called in any state. 77 virtual void SetVolume(double aVolume) {} 78 79 // Set the audio stream name. 80 // Does nothing if this sink has no audio stream. 81 // Can be called in any state. 82 virtual void SetStreamName(const nsAString& aStreamName) {} 83 84 // Set the playback rate. 85 // Can be called in any state. 86 virtual void SetPlaybackRate(double aPlaybackRate) {} 87 88 // Whether to preserve pitch of the audio track. 89 // Do nothing if this sink has no audio track. 90 // Can be called in any state. 91 virtual void SetPreservesPitch(bool aPreservesPitch) {} 92 93 // Pause/resume the playback. Only work after playback starts. 94 virtual void SetPlaying(bool aPlaying) = 0; 95 96 // Set the audio output device. aDevice == nullptr indicates the default 97 // device. The returned promise is resolved when the previous device is no 98 // longer in use and an attempt to open the new device completes 99 // (successfully or not) or is deemed unnecessary because the device is not 100 // required for output at this time. The promise will be resolved whether 101 // or not opening the cubeb_stream succeeds because the aDevice is 102 // considered the new underlying device and will be used when it becomes 103 // available. 104 virtual RefPtr<GenericPromise> SetAudioDevice( 105 RefPtr<AudioDeviceInfo> aDevice) = 0; 106 107 // Get the playback rate. 108 // Can be called in any state. 109 virtual double PlaybackRate() const = 0; 110 111 // Single frame rendering operation may need to be done before playback 112 // started (1st frame) or right after seek completed or playback stopped. 113 // Do nothing if this sink has no video track. Can be called in any state. 114 virtual void Redraw(const VideoInfo& aInfo) {}; 115 116 // Begin a playback session with the provided start time in the media data 117 // and media info. Must be called when playback is stopped. aStartTime is 118 // compared with MediaData::mTime and continues to increase when looping, 119 // unless decoding is reset. 120 virtual nsresult Start(const media::TimeUnit& aStartTime, 121 const MediaInfo& aInfo) = 0; 122 123 // Finish a playback session. 124 // Must be called after playback starts. 125 virtual void Stop() = 0; 126 127 // Return true if playback has started. 128 // Can be called in any state. 129 virtual bool IsStarted() const = 0; 130 131 // Return true if playback is started and not paused otherwise false. 132 // Can be called in any state. 133 virtual bool IsPlaying() const = 0; 134 135 // Called on the state machine thread to shut down the sink. All resources 136 // allocated by this sink should be released. 137 // Must be called after playback stopped. 138 virtual void Shutdown() {} 139 140 virtual void SetSecondaryVideoContainer(VideoFrameContainer* aSecondary) {} 141 142 virtual void GetDebugInfo(dom::MediaSinkDebugInfo& aInfo) {} 143 144 virtual void SetVideoQueueSendToCompositorSize(const uint32_t aSize) {} 145 146 protected: 147 virtual ~MediaSink() = default; 148 }; 149 150 } // namespace mozilla 151 152 #endif // MediaSink_h_