Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: content/renderer/media/gpu/rtc_video_decoder.h

Issue 2418613002: Proxy RtcVideoDecoder calls to a media::VideoDecoder.
Patch Set: Comments addressed and unittests updated. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/renderer/media/gpu/rtc_video_decoder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 CONTENT_RENDERER_MEDIA_GPU_RTC_VIDEO_DECODER_H_ 5 #ifndef CONTENT_RENDERER_MEDIA_GPU_RTC_VIDEO_DECODER_H_
6 #define CONTENT_RENDERER_MEDIA_GPU_RTC_VIDEO_DECODER_H_ 6 #define CONTENT_RENDERER_MEDIA_GPU_RTC_VIDEO_DECODER_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <deque> 11 #include <deque>
12 #include <list>
13 #include <map>
14 #include <memory> 12 #include <memory>
15 #include <set> 13 #include <unordered_map>
16 #include <utility> 14 #include <utility>
17 15
18 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
19 #include "base/macros.h" 17 #include "base/macros.h"
20 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
21 #include "base/synchronization/lock.h" 19 #include "base/synchronization/lock.h"
22 #include "base/threading/thread.h" 20 #include "base/threading/thread_checker.h"
23 #include "content/common/content_export.h" 21 #include "content/common/content_export.h"
24 #include "media/base/bitstream_buffer.h"
25 #include "media/base/video_decoder.h" 22 #include "media/base/video_decoder.h"
26 #include "media/video/picture.h" 23 #include "media/base/video_decoder_config.h"
27 #include "media/video/video_decode_accelerator.h"
28 #include "third_party/webrtc/modules/video_coding/include/video_codec_interface. h" 24 #include "third_party/webrtc/modules/video_coding/include/video_codec_interface. h"
29 #include "ui/gfx/geometry/rect.h"
30 25
31 namespace base { 26 namespace base {
32 class WaitableEvent; 27 class WaitableEvent;
33 }; 28 }
34 29
35 namespace media { 30 namespace media {
36 class DecoderBuffer; 31 class DecoderBuffer;
37 class GpuVideoAcceleratorFactories;
38 }
39
40 namespace gpu {
41 struct SyncToken;
42 } 32 }
43 33
44 namespace content { 34 namespace content {
45 35
46 // This class uses hardware accelerated video decoder to decode video for 36 // This class proxies webrtc::VideoDecoder calls to an owned instance of
47 // WebRTC. Lives on the media thread, where VDA::Client methods run on. 37 // media::VideoDecoder, which is created during initialization. Usually, this
48 // webrtc::VideoDecoder methods run on WebRTC DecodingThread or 38 // decoder will in turn proxy calls to a remote hardware-accelerated decoder
49 // Chrome_libJingle_WorkerThread, which are trampolined to the media thread. 39 // (ex. MojoVideoDecoder, GpuVideoDecoder), but a local media::VideoDecoder can
50 // Decode() is non-blocking and queues the buffers. Decoded frames are 40 // also be used.
51 // delivered to WebRTC on the media task runner. 41 //
42 // THREADING: webrtc::VideoDecoder methods run on the WebRTC DecodingThread or
43 // the Chrome_libJingle_WorkerThread, and are proxied to the media::VideoDecoder
44 // on |video_decoder_thread|, which is provided in Create().
52 class CONTENT_EXPORT RTCVideoDecoder 45 class CONTENT_EXPORT RTCVideoDecoder
53 : NON_EXPORTED_BASE(public webrtc::VideoDecoder), 46 : NON_EXPORTED_BASE(public webrtc::VideoDecoder) {
54 public media::VideoDecodeAccelerator::Client {
55 public: 47 public:
56 ~RTCVideoDecoder() override; 48 ~RTCVideoDecoder() override;
57 49
58 // Creates a RTCVideoDecoder on the message loop of |factories|. Returns NULL 50 // A factory closure which can be run to create a media::VideoDecoder.
59 // if failed. The video decoder will run on the message loop of |factories|. 51 using CreateVideoDecoderCB =
52 base::Callback<std::unique_ptr<media::VideoDecoder>()>;
53
54 // Can be called on any thread. Creates and returns an RTCVideoDecoder for the
55 // given codec |type|. |create_video_decoder_cb| is Run() on
56 // |decoder_task_runner| asynchronously to create and initialize an instance
57 // of media::VideoDecoder.
60 static std::unique_ptr<RTCVideoDecoder> Create( 58 static std::unique_ptr<RTCVideoDecoder> Create(
61 webrtc::VideoCodecType type, 59 webrtc::VideoCodecType type,
62 media::GpuVideoAcceleratorFactories* factories); 60 const CreateVideoDecoderCB& create_video_decoder_cb,
63 // Destroys |decoder| on the loop of |factories| 61 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner);
64 static void Destroy(webrtc::VideoDecoder* decoder, 62
65 media::GpuVideoAcceleratorFactories* factories); 63 // Destroys |decoder| on |decoder_task_runner|. This function takes ownership
64 // of |decoder|, which should not be used after this function is called.
65 static void Destroy(
66 webrtc::VideoDecoder* decoder,
67 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner);
66 68
67 // webrtc::VideoDecoder implementation. 69 // webrtc::VideoDecoder implementation.
68 // Called on WebRTC DecodingThread. 70 // Called on WebRTC DecodingThread.
69 int32_t InitDecode(const webrtc::VideoCodec* codecSettings, 71 int32_t InitDecode(const webrtc::VideoCodec* codecSettings,
70 int32_t numberOfCores) override; 72 int32_t numberOfCores) override;
71 // Called on WebRTC DecodingThread. 73 // Called on WebRTC DecodingThread.
72 int32_t Decode(const webrtc::EncodedImage& inputImage, 74 int32_t Decode(const webrtc::EncodedImage& inputImage,
73 bool missingFrames, 75 bool missingFrames,
74 const webrtc::RTPFragmentationHeader* fragmentation, 76 const webrtc::RTPFragmentationHeader* fragmentation,
75 const webrtc::CodecSpecificInfo* codecSpecificInfo = NULL, 77 const webrtc::CodecSpecificInfo* codecSpecificInfo = NULL,
76 int64_t renderTimeMs = -1) override; 78 int64_t renderTimeMs = -1) override;
77 // Called on WebRTC DecodingThread. 79 // Called on WebRTC DecodingThread.
78 int32_t RegisterDecodeCompleteCallback( 80 int32_t RegisterDecodeCompleteCallback(
79 webrtc::DecodedImageCallback* callback) override; 81 webrtc::DecodedImageCallback* callback) override;
82
80 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while 83 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
81 // this runs. 84 // this runs.
82 int32_t Release() override; 85 int32_t Release() override;
83 86
84 // VideoDecodeAccelerator::Client implementation.
85 void ProvidePictureBuffers(uint32_t count,
86 media::VideoPixelFormat format,
87 uint32_t textures_per_buffer,
88 const gfx::Size& size,
89 uint32_t texture_target) override;
90 void DismissPictureBuffer(int32_t id) override;
91 void PictureReady(const media::Picture& picture) override;
92 void NotifyEndOfBitstreamBuffer(int32_t id) override;
93 void NotifyFlushDone() override;
94 void NotifyResetDone() override;
95 void NotifyError(media::VideoDecodeAccelerator::Error error) override;
96
97 private: 87 private:
98 // Metadata of a bitstream buffer.
99 struct BufferData {
100 BufferData(int32_t bitstream_buffer_id,
101 uint32_t timestamp,
102 size_t size,
103 const gfx::Rect& visible_rect);
104 BufferData();
105 ~BufferData();
106 int32_t bitstream_buffer_id;
107 uint32_t timestamp; // in 90KHz
108 size_t size; // buffer size
109 gfx::Rect visible_rect;
110 };
111
112 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset); 88 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
113 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsFirstBufferAfterReset); 89 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsFirstBufferAfterReset);
114 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, GetVDAErrorCounterForTesting); 90 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest,
91 GetDecoderErrorCounterForTesting);
115 92
116 RTCVideoDecoder(webrtc::VideoCodecType type, 93 // |create_video_decoder_cb| is Run() on |decoder_task_runner| asynchronously
117 media::GpuVideoAcceleratorFactories* factories); 94 // to create and initialize an instance of media::VideoDecoder.
95 RTCVideoDecoder(
96 webrtc::VideoCodecType type,
97 const CreateVideoDecoderCB& create_video_decoder_cb,
98 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner);
118 99
119 // Requests a buffer to be decoded by VDA. 100 // Create |decoder_| by running |create_decoder_cb_|, then Initialize()
101 // |decoder_| using |config|. |decoder_initialized| will be bound to the
102 // callback passed to the decoder. Must be called on |decoder_task_runner|.
103 void InitializeDecoder(base::WaitableEvent* decoder_initialized,
104 const media::VideoDecoderConfig& config);
105
106 // Called by |decoder_| once it has been initialized. After setting |state_|
107 // appropriately, signal |decoder_initialized|. Must be called on
108 // |decoder_task_runner_|.
109 void OnDecoderInitialized(base::WaitableEvent* decoder_initialized,
110 bool success);
111
112 // Called by |decoder_| when a decoded frame is ready for consumption. This
113 // call will be proxied to |decode_complete_callback_|.
114 void OnFrameReady(const scoped_refptr<media::VideoFrame>& frame);
115
116 // Requests a buffer to be decoded by |decoder_|. Must be called on
117 // |decoder_task_runner|.
120 void RequestBufferDecode(); 118 void RequestBufferDecode();
121 119
120 // Returns true if |decoder_| can decode another buffer right now.
122 bool CanMoreDecodeWorkBeDone(); 121 bool CanMoreDecodeWorkBeDone();
123 122
124 // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|. 123 int GetDecoderErrorCounterForTesting();
125 // This handles the wraparound.
126 bool IsBufferAfterReset(int32_t id_buffer, int32_t id_reset);
127 124
128 // Returns true if bitstream buffer |id_buffer| is the first buffer after 125 // Saves a DecoderBuffer buffer in |pending_buffers_| waiting to be sent to
129 // |id_reset|. 126 // the decoder. Returns true on success.
130 bool IsFirstBufferAfterReset(int32_t id_buffer, int32_t id_reset); 127 bool SaveToPendingBuffers_Locked(
128 int32_t decoder_buffer_id,
129 scoped_refptr<media::DecoderBuffer> decoder_buffer);
131 130
132 int GetVDAErrorCounterForTesting() { return vda_error_counter_; } 131 // |decoder_| will call this on |decoder_task_runner| when it has finshed
132 // a Reset().
133 void OnResetDone();
133 134
134 // Saves a WebRTC buffer in |decode_buffers_| for decode. 135 // |decoder_| will call this when it has finished decoding the buffer
135 void SaveToDecodeBuffers_Locked( 136 // identified by |decoder_buffer_id|. |decoder_| will also use this to bubble
136 const webrtc::EncodedImage& input_image, 137 // up decoding errors, by setting |status| to DECODE_ERROR. Will be called on
137 std::unique_ptr<base::SharedMemory> shm_buffer, 138 // |decoder_task_runner|.
138 const BufferData& buffer_data); 139 void OnBufferDecoded(int32_t decoder_buffer_id, media::DecodeStatus status);
139
140 // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
141 // Returns true on success.
142 bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
143 const BufferData& buffer_data);
144
145 // Gets SHM and moves pending buffers to decode buffers.
146 void MovePendingBuffersToDecodeBuffers();
147
148 scoped_refptr<media::VideoFrame> CreateVideoFrame(
149 const media::Picture& picture,
150 const media::PictureBuffer& pb,
151 uint32_t timestamp,
152 const gfx::Rect& visible_rect,
153 media::VideoPixelFormat pixel_format);
154
155 // Resets VDA.
156 void ResetInternal();
157
158 // Static method is to allow it to run even after RVD is deleted.
159 static void ReleaseMailbox(base::WeakPtr<RTCVideoDecoder> decoder,
160 media::GpuVideoAcceleratorFactories* factories,
161 int64_t picture_buffer_id,
162 uint32_t texture_id,
163 const gpu::SyncToken& release_sync_token);
164 // Tells VDA that a picture buffer can be recycled.
165 void ReusePictureBuffer(int64_t picture_buffer_id);
166
167 // Creates |vda_| on the media thread.
168 void CreateVDA(media::VideoCodecProfile profile, base::WaitableEvent* waiter);
169
170 void DestroyTextures();
171 void DestroyVDA();
172
173 // Gets a shared-memory segment of at least |min_size| bytes from
174 // |available_shm_segments_|. Returns NULL if there is no buffer or the
175 // buffer is not big enough.
176 std::unique_ptr<base::SharedMemory> GetSHM_Locked(size_t min_size);
177
178 // Returns a shared-memory segment to the available pool.
179 void PutSHM_Locked(std::unique_ptr<base::SharedMemory> shm_buffer);
180
181 // Allocates |count| shared memory buffers of |size| bytes.
182 void CreateSHM(size_t count, size_t size);
183
184 // Stores the buffer metadata to |input_buffer_data_|.
185 void RecordBufferData(const BufferData& buffer_data);
186 // Gets the buffer metadata from |input_buffer_data_|.
187 void GetBufferData(int32_t bitstream_buffer_id,
188 uint32_t* timestamp,
189 gfx::Rect* visible_rect);
190 140
191 // Records the result of InitDecode to UMA and returns |status|. 141 // Records the result of InitDecode to UMA and returns |status|.
192 int32_t RecordInitDecodeUMA(int32_t status); 142 int32_t RecordInitDecodeUMA(int32_t status);
193 143
194 // Asserts the contract that this class is operated on the right thread. 144 // Clears the |pending_buffers_| queue. |lock_| must be acquired before
195 void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const; 145 // calling this.
196 146 void ClearPendingBuffers_Locked();
197 // Queries factories_ whether |profile| is supported and return true is so,
198 // false otherwise. If true, also set resolution limits for |profile|
199 // in min/max_resolution_.
200 bool IsProfileSupported(media::VideoCodecProfile profile);
201
202 // Clears the pending_buffers_ queue, freeing memory.
203 void ClearPendingBuffers();
204 147
205 enum State { 148 enum State {
206 UNINITIALIZED, // The decoder has not initialized. 149 UNINITIALIZED = 0, // The decoder has not initialized.
207 INITIALIZED, // The decoder has initialized. 150 INITIALIZED, // The decoder has initialized.
208 RESETTING, // The decoder is being reset. 151 RESETTING, // The decoder is being reset.
209 DECODE_ERROR, // Decoding error happened. 152 DECODE_ERROR, // Decoding error happened.
210 }; 153 };
211 154
212 static const int32_t ID_LAST; // maximum bitstream buffer id 155 // The hardware video decoder.
213 static const int32_t ID_HALF; // half of the maximum bitstream buffer id 156 std::unique_ptr<::media::VideoDecoder> decoder_;
214 static const int32_t ID_INVALID; // indicates Reset or Release never occurred
215 157
216 // The hardware video decoder. 158 // Number of times that |decoder_| notified of an error.
217 std::unique_ptr<media::VideoDecodeAccelerator> vda_; 159 uint32_t decoder_error_counter_;
218
219 media::VideoCodecProfile vda_codec_profile_;
220
221 // Number of times that |vda_| notified of an error.
222 uint32_t vda_error_counter_;
223 160
224 // The video codec type, as reported by WebRTC. 161 // The video codec type, as reported by WebRTC.
225 const webrtc::VideoCodecType video_codec_type_; 162 const webrtc::VideoCodecType video_codec_type_;
226 163
227 // The size of the incoming video frames. 164 // The size of the incoming video frames.
228 gfx::Size frame_size_; 165 gfx::Size frame_size_;
229 166
230 media::GpuVideoAcceleratorFactories* const factories_; 167 // The factory callback and task runner used to create the media::VideoDecoder
168 const CreateVideoDecoderCB create_video_decoder_cb_;
169 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_;
231 170
232 // The texture target used for decoded pictures. 171 // Protects |state_|, |decode_complete_callback_|, |pending_buffers_|,
233 uint32_t decoder_texture_target_; 172 // |next_decoder_buffer_id_|, |reset_bitstream_buffer_id_|, and
234 173 // |decoder_error_counter_|.
235 // The format of the decoded pictures.
236 media::VideoPixelFormat pixel_format_;
237
238 // Metadata of the buffers that have been sent for decode.
239 std::list<BufferData> input_buffer_data_;
240
241 // A map from bitstream buffer IDs to bitstream buffers that are being
242 // processed by VDA.
243 std::map<int32_t, std::unique_ptr<base::SharedMemory>>
244 bitstream_buffers_in_decoder_;
245
246 // A map from picture buffer IDs to texture-backed picture buffers.
247 std::map<int32_t, media::PictureBuffer> assigned_picture_buffers_;
248
249 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
250 // as VideoFrames to be rendered via read_cb_, and which will be returned
251 // to us via ReusePictureBuffer.
252 typedef std::map<int32_t /* picture_buffer_id */, uint32_t /* texture_id */>
253 PictureBufferTextureMap;
254 PictureBufferTextureMap picture_buffers_at_display_;
255
256 // The id that will be given to the next picture buffer.
257 int32_t next_picture_buffer_id_;
258
259 // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
260 // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
261 // |next_bitstream_buffer_id_|, |reset_bitstream_buffer_id_| and
262 // |vda_error_counter_|.
263 base::Lock lock_; 174 base::Lock lock_;
264 175
265 // The state of RTCVideoDecoder. Guarded by |lock_|. 176 // The state of RTCVideoDecoder. Guarded by |lock_|.
266 State state_; 177 State state_;
267 178
268 // Guarded by |lock_|. 179 // Guarded by |lock_|.
269 webrtc::DecodedImageCallback* decode_complete_callback_; 180 webrtc::DecodedImageCallback* decode_complete_callback_;
270 181
271 // Total number of allocated SHM buffers. Guarded by |lock_|. 182 // A queue storing decoder buffers that are waiting to be sent to |decoder_|.
272 size_t num_shm_buffers_; 183 // Guarded by |lock_|.
184 std::deque<std::pair<int32_t, scoped_refptr<media::DecoderBuffer>>>
185 pending_buffers_;
273 186
274 // Shared-memory buffer pool. Since allocating SHM segments requires a 187 // A map of the buffers currently being decoded by |decoder_|.
275 // round-trip to the browser process, we keep allocation out of the 188 std::unordered_map<int32_t /* decoder_buffer_id */,
276 // steady-state of the decoder. Guarded by |lock_|. 189 scoped_refptr<media::DecoderBuffer>>
277 std::vector<std::unique_ptr<base::SharedMemory>> available_shm_segments_; 190 buffers_in_decoder_;
278
279 // A queue storing WebRTC encoding images (and their metadata) that are
280 // waiting for the shared memory. Guarded by |lock_|.
281 std::deque<std::pair<webrtc::EncodedImage, BufferData>> pending_buffers_;
282
283 // A queue storing buffers (and their metadata) that will be sent to VDA for
284 // decode. Guarded by |lock_|.
285 std::deque<std::pair<std::unique_ptr<base::SharedMemory>, BufferData>>
286 decode_buffers_;
287 191
288 // The id that will be given to the next bitstream buffer. Guarded by |lock_|. 192 // The id that will be given to the next bitstream buffer. Guarded by |lock_|.
289 int32_t next_bitstream_buffer_id_; 193 int32_t next_decoder_buffer_id_;
290 194
291 // A buffer that has an id less than this should be dropped because Reset or 195 // A buffer that has an id less than this should be dropped because Reset or
292 // Release has been called. Guarded by |lock_|. 196 // Release has been called. Guarded by |lock_|.
293 int32_t reset_bitstream_buffer_id_; 197 int32_t reset_decoder_buffer_id_;
294 198
295 // Minimum and maximum supported resolutions for the current profile/VDA. 199 // This will be used to verify that the proper set of calls are made on the
296 gfx::Size min_resolution_; 200 // webrtc DecodingThread.
297 gfx::Size max_resolution_; 201 base::ThreadChecker webrtc_decoding_thread_checker_;
298 202
299 // Must be destroyed, or invalidated, on the media thread. 203 // Must be destroyed, or invalidated, on the media thread.
300 // NOTE: Weak pointers must be invalidated before all other member variables. 204 // NOTE: Weak pointers must be invalidated before all other member variables.
301 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_; 205 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_;
302 206
303 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder); 207 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder);
304 }; 208 };
305 209
306 } // namespace content 210 } // namespace content
307 211
308 #endif // CONTENT_RENDERER_MEDIA_GPU_RTC_VIDEO_DECODER_H_ 212 #endif // CONTENT_RENDERER_MEDIA_GPU_RTC_VIDEO_DECODER_H_
OLDNEW
« no previous file with comments | « no previous file | content/renderer/media/gpu/rtc_video_decoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698