OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMECAST_PUBLIC_MEDIA_MEDIA_PIPELINE_BACKEND_H_ | 5 #ifndef CHROMECAST_PUBLIC_MEDIA_MEDIA_PIPELINE_BACKEND_H_ |
6 #define CHROMECAST_PUBLIC_MEDIA_MEDIA_PIPELINE_BACKEND_H_ | 6 #define CHROMECAST_PUBLIC_MEDIA_MEDIA_PIPELINE_BACKEND_H_ |
7 | 7 |
| 8 #include "decoder_config.h" |
| 9 |
8 namespace chromecast { | 10 namespace chromecast { |
| 11 struct Size; |
| 12 |
9 namespace media { | 13 namespace media { |
| 14 class CastDecoderBuffer; |
| 15 class DecryptContext; |
10 | 16 |
11 class AudioPipelineDevice; | 17 // Interface for platform-specific output of media. |
12 class MediaClockDevice; | |
13 struct MediaPipelineDeviceParams; | |
14 class VideoPipelineDevice; | |
15 | |
16 // Interface for creating and managing ownership of platform-specific clock, | |
17 // audio and video devices. cast_shell owns the MediaPipelineBackend for | |
18 // as long as it is needed; the implementation is responsible for | |
19 // tearing down the individual components correctly when it is destroyed. | |
20 // A new MediaPipelineBackend will be instantiated for each media player | 18 // A new MediaPipelineBackend will be instantiated for each media player |
21 // instance. | 19 // instance and raw audio stream. If a backend has both video and audio |
| 20 // decoders, they must be synchronized. |
| 21 // If more backends are requested than the platform supports, the unsupported |
| 22 // extra backends may return nullptr for CreateAudioDecoder/CreateVideoDecoder. |
| 23 // The basic usage pattern is: |
| 24 // * Decoder objects created, then Initialize called |
| 25 // * Start/Stop/Pause/Resume used to manage playback state |
| 26 // * Decoder objects are used to pass actual stream data buffers |
| 27 // * Backend must make appropriate callbacks on the provided Delegate |
| 28 // All functions will be called on the media thread. Delegate callbacks |
| 29 // must be made on this thread also (using provided TaskRunner if necessary). |
22 class MediaPipelineBackend { | 30 class MediaPipelineBackend { |
23 public: | 31 public: |
| 32 // Return code for PushBuffer |
| 33 enum BufferStatus { |
| 34 kBufferSuccess, |
| 35 kBufferFailed, |
| 36 kBufferPending, |
| 37 }; |
| 38 |
| 39 class Decoder { |
| 40 public: |
| 41 typedef MediaPipelineBackend::BufferStatus BufferStatus; |
| 42 |
| 43 // Statistics (computed since pipeline last started playing). |
| 44 // For video, a sample is defined as a frame. |
| 45 struct Statistics { |
| 46 uint64_t decoded_bytes; |
| 47 uint64_t decoded_samples; |
| 48 uint64_t dropped_samples; |
| 49 }; |
| 50 |
| 51 // Pushes a buffer of data for decoding and output. If the implementation |
| 52 // cannot push the buffer now, it must store the buffer, return |
| 53 // |kBufferPending| and execute the push at a later time when it becomes |
| 54 // possible to do so. The implementation must then invoke |
| 55 // Client::OnPushComplete. Pushing a pending buffer should be aborted if |
| 56 // Stop is called; OnPushAudioComplete need not be invoked in this case. |
| 57 // If |kBufferPending| is returned, the pipeline will stop pushing any |
| 58 // further buffers until OnPushComplete is invoked. |
| 59 // OnPushComplete should be only be invoked to indicate completion of a |
| 60 // pending buffer push - not for the immediate |kBufferSuccess| return case. |
| 61 // The decrypt_context and buffer's lifetimes are managed by the caller code |
| 62 // - they MUST NOT be deleted by the MediaPipelineBackend implementation, |
| 63 // and MUST NOT be dereferenced after completion of buffer push (i.e. |
| 64 // kBufferSuccess/kBufferFailure for synchronous completion, OnPushComplete |
| 65 // for kBufferPending case). |
| 66 virtual BufferStatus PushBuffer(DecryptContext* decrypt_context, |
| 67 CastDecoderBuffer* buffer) = 0; |
| 68 |
| 69 // Returns the playback statistics since this decoder's creation. Only |
| 70 // called when playing or paused. |
| 71 virtual void GetStatistics(Statistics* statistics) = 0; |
| 72 |
| 73 protected: |
| 74 virtual ~Decoder() {} |
| 75 }; |
| 76 |
| 77 class AudioDecoder : public Decoder { |
| 78 public: |
| 79 // Info on pipeline latency: amount of data in pipeline not rendered yet, |
| 80 // and timestamp of system clock (must be CLOCK_MONOTONIC) at which delay |
| 81 // measurement was taken. Both times in microseconds. |
| 82 struct RenderingDelay { |
| 83 RenderingDelay() |
| 84 : delay_microseconds(INT64_MIN), timestamp_microseconds(INT64_MIN) {} |
| 85 RenderingDelay(int64_t delay_microseconds_in, |
| 86 int64_t timestamp_microseconds_in) |
| 87 : delay_microseconds(delay_microseconds_in), |
| 88 timestamp_microseconds(timestamp_microseconds_in) {} |
| 89 int64_t delay_microseconds; |
| 90 int64_t timestamp_microseconds; |
| 91 }; |
| 92 |
| 93 // Provides the audio configuration. Called once before the backend is |
| 94 // initialized, and again any time the configuration changes (in any state). |
| 95 // Returns true if the configuration is a supported configuration. |
| 96 virtual bool SetConfig(const AudioConfig& config) = 0; |
| 97 |
| 98 // Sets the volume multiplier for this audio stream. |
| 99 // The multiplier is in the range [0.0, 1.0]. If not called, a default |
| 100 // multiplier of 1.0 is assumed. Returns true if successful. |
| 101 // Only called after the backend has been initialized. |
| 102 virtual bool SetVolume(float multiplier) = 0; |
| 103 |
| 104 // Returns the pipeline latency: i.e. the amount of data |
| 105 // in the pipeline that have not been rendered yet, in microseconds. |
| 106 // Returns delay = INT64_MIN if the latency is not available. |
| 107 // Only called when the backend is playing. |
| 108 virtual RenderingDelay GetRenderingDelay() = 0; |
| 109 |
| 110 protected: |
| 111 ~AudioDecoder() override {} |
| 112 }; |
| 113 |
| 114 class VideoDecoder : public Decoder { |
| 115 public: |
| 116 // Provides the video configuration. Called once before the backend is |
| 117 // initialized, and again any time the configuration changes (in any state). |
| 118 // Returns true if the configuration is a supported configuration. |
| 119 virtual bool SetConfig(const VideoConfig& config) = 0; |
| 120 |
| 121 protected: |
| 122 ~VideoDecoder() override {} |
| 123 }; |
| 124 |
| 125 // Delegate methods must be called on the main CMA thread. |
| 126 class Delegate { |
| 127 public: |
| 128 // Must be called when video resolution change is detected by decoder. |
| 129 virtual void OnVideoResolutionChanged(VideoDecoder* decoder, |
| 130 const Size& size) = 0; |
| 131 |
| 132 // See comments on PushBuffer. Must not be called with kBufferPending. |
| 133 virtual void OnPushBufferComplete(Decoder* decoder, |
| 134 BufferStatus status) = 0; |
| 135 |
| 136 // Must be called after an end-of-stream buffer has been rendered (ie, the |
| 137 // last real buffer has been sent to the output hardware). |
| 138 virtual void OnEndOfStream(Decoder* decoder) = 0; |
| 139 |
| 140 // May be called if a decoder error occurs. No more calls to PushBuffer() |
| 141 // will be made after this is called. |
| 142 virtual void OnDecoderError(Decoder* decoder) = 0; |
| 143 |
| 144 protected: |
| 145 virtual ~Delegate() {} |
| 146 }; |
| 147 |
24 virtual ~MediaPipelineBackend() {} | 148 virtual ~MediaPipelineBackend() {} |
25 | 149 |
26 // Returns the platform-specific pipeline clock. | 150 // Creates a new AudioDecoder attached to this pipeline. MediaPipelineBackend |
27 virtual MediaClockDevice* GetClock() = 0; | 151 // maintains ownership of the decoder object (and must not delete before it's |
| 152 // destroyed). Will be called zero or more times, all calls made before |
| 153 // Initialize. May return nullptr if the platform implementation cannot |
| 154 // support any additional simultaneous playback at this time. |
| 155 virtual AudioDecoder* CreateAudioDecoder() = 0; |
28 | 156 |
29 // Returns the platform-specific audio backend. | 157 // Creates a new VideoDecoder attached to this pipeline. MediaPipelineBackend |
30 virtual AudioPipelineDevice* GetAudio() = 0; | 158 // maintains ownership of the decoder object (and must not delete before it's |
| 159 // destroyed). Will be called zero or more times, all calls made before |
| 160 // Initialize. Note: Even if your backend only supports audio, you must |
| 161 // provide a default implementation of VideoDecoder; one way to do this is to |
| 162 // inherit from MediaPipelineBackendDefault. May return nullptr if the |
| 163 // platform implementation cannot support any additional simultaneous playback |
| 164 // at this time. |
| 165 virtual VideoDecoder* CreateVideoDecoder() = 0; |
31 | 166 |
32 // Returns the platform-specific video backend. | 167 // Initializes the backend. This will be called once, after Decoder creation |
33 virtual VideoPipelineDevice* GetVideo() = 0; | 168 // but before all other functions. Hardware resources for all decoders should |
| 169 // be acquired here. Backend is then considered in Initialized state. |
| 170 // Returns false for failure. |
| 171 virtual bool Initialize(Delegate* delegate) = 0; |
| 172 |
| 173 // Places pipeline into playing state. Playback will start at given time once |
| 174 // buffers are pushed. Called only when in Initialized state. |start_pts| is |
| 175 // the start playback timestamp in microseconds. |
| 176 virtual bool Start(int64_t start_pts) = 0; |
| 177 |
| 178 // Returns pipeline to 'Initialized' state. May be called while playing or |
| 179 // paused. Buffers cannot be pushed in Initialized state. |
| 180 virtual bool Stop() = 0; |
| 181 |
| 182 // Pauses media playback. Called only when in playing state. |
| 183 virtual bool Pause() = 0; |
| 184 |
| 185 // Resumes media playback. Called only when in paused state. |
| 186 virtual bool Resume() = 0; |
| 187 |
| 188 // Gets the current playback timestamp in microseconds. |
| 189 virtual int64_t GetCurrentPts() = 0; |
| 190 |
| 191 // Sets the playback rate. |rate| > 0. If this is not called, a default rate |
| 192 // of 1.0 is assumed. Returns true if successful. |
| 193 virtual bool SetPlaybackRate(float rate) = 0; |
34 }; | 194 }; |
35 | 195 |
36 } // namespace media | 196 } // namespace media |
37 } // namespace chromecast | 197 } // namespace chromecast |
38 | 198 |
39 #endif // CHROMECAST_MEDIA_CMA_BACKEND_MEDIA_PIPELINE_DEVICE_FACTORY_H_ | 199 #endif // CHROMECAST_PUBLIC_MEDIA_MEDIA_PIPELINE_BACKEND_H_ |
OLD | NEW |