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