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

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

Issue 13890012: Integrate VDA with WebRTC. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix try bots errors Created 7 years, 5 months 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
OLDNEW
(Empty)
1 // Use of this source code is governed by a BSD-style license that can be
2 // found in the LICENSE file.
3
4 #ifndef CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
5 #define CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
6
7 #include <deque>
8 #include <set>
9 #include <utility>
10
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop.h"
15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "content/common/content_export.h"
19 #include "media/base/bitstream_buffer.h"
20 #include "media/base/video_decoder.h"
21 #include "media/filters/gpu_video_decoder.h"
22 #include "media/video/picture.h"
23 #include "media/video/video_decode_accelerator.h"
24 #include "third_party/webrtc/modules/video_coding/codecs/interface/video_codec_i nterface.h"
25
26 namespace base {
27 class MessageLoopProxy;
28 };
29
30 namespace media {
31 class DecoderBuffer;
32 }
33
34 namespace content {
35
36 // This class uses hardware accelerated video decoder to decode video for
37 // WebRTC. The message loop of RendererGpuVideoDecoderFactories is stored as
38 // |vda_message_loop_|. It is the compositor thread, or the renderer thread if
39 // threaded compositing is disabled. VDA::Client methods run on
40 // |vda_message_loop_|. webrtc::VideoDecoder methods run on WebRTC
41 // DecodingThread or Chrome_libJingle_WorkerThread, which are trampolined to
42 // |vda_message_loop_|. Decode() is non-blocking and queues the buffers. Decoded
43 // frames are delivered on |vda_message_loop_|.
44 class CONTENT_EXPORT RTCVideoDecoder
45 : NON_EXPORTED_BASE(public webrtc::VideoDecoder),
46 public media::VideoDecodeAccelerator::Client,
47 public base::MessageLoop::DestructionObserver {
48 public:
49 virtual ~RTCVideoDecoder();
50
51 // Creates a RTCVideoDecoder. Returns NULL if failed.
52 static scoped_ptr<RTCVideoDecoder> Create(
53 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories);
54
55 // webrtc::VideoDecoder implementation.
56 // Called on WebRTC DecodingThread.
57 virtual int32_t InitDecode(const webrtc::VideoCodec* codecSettings,
58 int32_t numberOfCores) OVERRIDE;
59 // Called on WebRTC DecodingThread.
60 virtual int32_t Decode(
61 const webrtc::EncodedImage& inputImage,
62 bool missingFrames,
63 const webrtc::RTPFragmentationHeader* fragmentation,
64 const webrtc::CodecSpecificInfo* codecSpecificInfo = NULL,
65 int64_t renderTimeMs = -1) OVERRIDE;
66 // Called on WebRTC DecodingThread.
67 virtual int32_t RegisterDecodeCompleteCallback(
68 webrtc::DecodedImageCallback* callback) OVERRIDE;
69 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
70 // this runs.
71 virtual int32_t Release() OVERRIDE;
72 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
73 // this runs.
74 virtual int32_t Reset() OVERRIDE;
75
76 // VideoDecodeAccelerator::Client implementation.
77 virtual void NotifyInitializeDone() OVERRIDE;
78 virtual void ProvidePictureBuffers(uint32 count,
79 const gfx::Size& size,
80 uint32 texture_target) OVERRIDE;
81 virtual void DismissPictureBuffer(int32 id) OVERRIDE;
82 virtual void PictureReady(const media::Picture& picture) OVERRIDE;
83 virtual void NotifyEndOfBitstreamBuffer(int32 id) OVERRIDE;
84 virtual void NotifyFlushDone() OVERRIDE;
85 virtual void NotifyResetDone() OVERRIDE;
86 virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE;
87
88 // base::DestructionObserver implementation. Called when |vda_message_loop_|
89 // is stopped.
90 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
91
92 private:
93 class SHMBuffer;
94 // Metadata of a bitstream buffer.
95 struct BufferData {
96 BufferData(int32 bitstream_buffer_id,
97 uint32_t timestamp,
98 int width,
99 int height,
100 size_t size);
101 BufferData();
102 ~BufferData();
103 int32 bitstream_buffer_id;
104 uint32_t timestamp; // in 90KHz
105 uint32_t width;
106 uint32_t height;
107 size_t size; // buffer size
108 };
109
110 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
111
112 RTCVideoDecoder(
113 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories);
114
115 void Initialize(base::WaitableEvent* waiter);
116
117 // Requests a buffer to be decoded by VDA.
118 void RequestBufferDecode();
119
120 bool CanMoreDecodeWorkBeDone();
121
122 // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|.
123 // This handles the wraparound.
124 bool IsBufferAfterReset(int32 id_buffer, int32 id_reset);
125
126 // Saves a WebRTC buffer in |decode_buffers_| for decode.
127 void SaveToDecodeBuffers_Locked(const webrtc::EncodedImage& input_image,
128 scoped_ptr<SHMBuffer> shm_buffer,
129 const BufferData& buffer_data);
130
131 // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
132 // Returns true on success.
133 bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
134 const BufferData& buffer_data);
135
136 // Gets SHM and moves pending buffers to decode buffers.
137 void MovePendingBuffersToDecodeBuffers();
138
139 scoped_refptr<media::VideoFrame> CreateVideoFrame(
140 const media::Picture& picture,
141 const media::PictureBuffer& pb,
142 uint32_t timestamp,
143 uint32_t width,
144 uint32_t height,
145 size_t size);
146
147 // Resets VDA.
148 void ResetInternal();
149
150 // Tells VDA that a picture buffer can be recycled.
151 void ReusePictureBuffer(int64 picture_buffer_id, uint32 sync_point);
152
153 void DestroyTextures();
154 void DestroyVDA();
155
156 // Gets a shared-memory segment of at least |min_size| bytes from
157 // |available_shm_segments_|. Returns NULL if there is no buffer or the
158 // buffer is not big enough.
159 scoped_ptr<SHMBuffer> GetSHM_Locked(size_t min_size);
160
161 // Returns a shared-memory segment to the available pool.
162 void PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer);
163
164 // Allocates |number| shared memory of at least |min_size| bytes.
165 void CreateSHM(int number, size_t min_size);
166
167 // Stores the buffer metadata to |input_buffer_data_|.
168 void RecordBufferData(const BufferData& buffer_data);
169 // Gets the buffer metadata from |input_buffer_data_|.
170 void GetBufferData(int32 bitstream_buffer_id,
171 uint32_t* timestamp,
172 uint32_t* width,
173 uint32_t* height,
174 size_t* size);
175
176 enum State {
177 UNINITIALIZED, // The decoder has not initialized.
178 INITIALIZED, // The decoder has initialized.
179 RESETTING, // The decoder is being reset.
180 DECODE_ERROR, // Decoding error happened.
181 };
182
183 static const int32 ID_LAST; // maximum bitstream buffer id
184 static const int32 ID_HALF; // half of the maximum bitstream buffer id
185 static const int32 ID_INVALID; // indicates Reset or Release never occurred
186
187 // The hardware video decoder.
188 scoped_ptr<media::VideoDecodeAccelerator> vda_;
189
190 // The size of the incoming video frames.
191 gfx::Size frame_size_;
192
193 // The weak pointer should live and die on the |vda_loop_proxy_|;
194 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_;
195 base::WeakPtr<RTCVideoDecoder> weak_this_;
196
197 scoped_refptr<media::GpuVideoDecoder::Factories> factories_;
198
199 // The message loop to run callbacks on. This is should be the same as the one
200 // of |factories_|.
201 scoped_refptr<base::MessageLoopProxy> vda_loop_proxy_;
202
203 // The thread to create shared memory. Factories::CreateSharedMemory is
204 // trampolined to the child thread. When |vda_loop_proxy_| is the compositor
205 // thread, blocking on the child thread will deadlock. During WebRTC hang up,
206 // the child thread waits for Chrome_libJingle_WorkerThread. libJingle thread
207 // cannot finish when DecodingThread holds a WebRTC lock and blocks on the
208 // child thread. So we need to call CreateSharedMemory asynchronously from a
209 // different thread.
210 base::Thread create_shm_thread_;
211
212 // The texture target used for decoded pictures.
213 uint32 decoder_texture_target_;
214
215 // Metadata of the buffers that have been sent for decode.
216 std::list<BufferData> input_buffer_data_;
217
218 // A map from bitstream buffer IDs to bitstream buffers that are being
219 // processed by VDA. The map owns SHM buffers.
220 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_;
221
222 // A map from picture buffer IDs to texture-backed picture buffers.
223 std::map<int32, media::PictureBuffer> assigned_picture_buffers_;
224
225 // Picture buffers that are dismissed but not deleted yet.
226 std::map<int32, media::PictureBuffer> dismissed_picture_buffers_;
227
228 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
229 // as VideoFrames to be rendered via read_cb_, and which will be returned
230 // to us via ReusePictureBuffer.
231 std::set<int32> picture_buffers_at_display_;
232
233 // The id that will be given to the next picture buffer.
234 int32 next_picture_buffer_id_;
235
236 // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
237 // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
238 // |next_bitstream_buffer_id_| and |reset_bitstream_buffer_id_|.
239 base::Lock lock_;
240
241 // The state of RTCVideoDecoder. Guarded by |lock_|.
242 State state_;
243
244 // Guarded by |lock_|.
245 webrtc::DecodedImageCallback* decode_complete_callback_;
246
247 // Total number of allocated SHM buffers. Guarded by |lock_|.
248 int num_shm_buffers_;
249
250 // Shared-memory buffer pool. Since allocating SHM segments requires a
251 // round-trip to the browser process, we keep allocation out of the
252 // steady-state of the decoder. The vector owns SHM buffers. Guarded by
253 // |lock_|.
254 std::vector<SHMBuffer*> available_shm_segments_;
255
256 // A queue storing WebRTC encoding images (and their metadata) that are
257 // waiting for the shared memory. Guarded by |lock_|.
258 std::deque<std::pair<webrtc::EncodedImage, BufferData> > pending_buffers_;
259
260 // A queue storing buffers (and their metadata) that will be sent to VDA for
261 // decode. The queue owns SHM buffers. Guarded by |lock_|.
262 std::deque<std::pair<SHMBuffer*, BufferData> > decode_buffers_;
263
264 // The id that will be given to the next bitstream buffer. Guarded by |lock_|.
265 int32 next_bitstream_buffer_id_;
266
267 // A buffer that has an id less than this should be dropped because Reset or
268 // Release has been called. Guarded by |lock_|.
269 int32 reset_bitstream_buffer_id_;
270
271 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder);
272 };
273
274 } // namespace content
275
276 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698