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

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: address review comments 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();
91
92 private:
93 struct SHMBuffer;
94 struct BufferData;
95
96 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
97
98 RTCVideoDecoder(
99 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories);
100
101 void Initialize(base::WaitableEvent* waiter);
102
103 // Requests a buffer to be decoded by VDA.
104 void RequestBufferDecode();
105
106 bool CanMoreDecodeWorkBeDone();
107
108 // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|.
109 // This handles the wraparound.
110 bool IsBufferAfterReset(int32 id_buffer, int32 id_reset);
111
112 // Saves a WebRTC buffer in |decode_buffers_| for decode.
113 void SaveToDecodeBuffers_Locked(const webrtc::EncodedImage& input_image,
114 scoped_ptr<SHMBuffer> shm_buffer,
115 const BufferData& buffer_data);
116
117 // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
118 // Returns true on success.
119 bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
120 const BufferData& buffer_data);
121
122 // Gets SHM and moves pending buffers to decode buffers.
123 void MovePendingBuffersToDecodeBuffers();
124
125 scoped_refptr<media::VideoFrame> CreateVideoFrame(
126 const media::Picture& picture,
127 const media::PictureBuffer& pb,
128 uint32_t timestamp,
129 uint32_t width,
130 uint32_t height,
131 size_t size);
132
133 // Resets VDA.
134 void ResetInternal();
135
136 // Tells VDA that a picture buffer can be recycled.
137 void ReusePictureBuffer(int64 picture_buffer_id, uint32 sync_point);
138
139 void DestroyTextures();
140 void DestroyVDA();
141
142 // Gets a shared-memory segment of at least |min_size| bytes from
143 // |available_shm_segments_|. Returns NULL if there is no buffer or the
144 // buffer is not big enough.
145 scoped_ptr<SHMBuffer> GetSHM_Locked(size_t min_size);
146
147 // Returns a shared-memory segment to the available pool.
148 void PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer);
149
150 // Allocates |number| shared memory of at least |min_size| bytes.
151 void CreateSHM(int number, size_t min_size);
152
153 // Stores the buffer metadata to |input_buffer_data_|.
154 void RecordBufferData(const BufferData& buffer_data);
155 // Gets the buffer metadata from |input_buffer_data_|.
156 void GetBufferData(int32 bitstream_buffer_id,
157 uint32_t* timestamp,
158 uint32_t* width,
159 uint32_t* height,
160 size_t* size);
161
162 enum State {
163 UNINITIALIZED, // The decoder has not initialized.
164 INITIALIZED, // The decoder has initialized.
165 RESETTING, // The decoder is being reset.
166 DECODE_ERROR, // Decoding error happened.
167 };
168
169 static const int32 ID_LAST; // maximum bitstream buffer id
170 static const int32 ID_HALF; // half of the maximum bitstream buffer id
171 static const int32 ID_INVALID; // indicates Reset or Release never occurred
172
173 // The hardware video decoder.
174 scoped_ptr<media::VideoDecodeAccelerator> vda_;
175
176 // The size of the incoming video frames.
177 gfx::Size frame_size_;
178
179 // The weak pointer should live and die on the |vda_loop_proxy_|;
180 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_;
181 base::WeakPtr<RTCVideoDecoder> weak_this_;
182
183 scoped_refptr<media::GpuVideoDecoder::Factories> factories_;
184
185 // The message loop to run callbacks on. This is should be the same as the one
186 // of |factories_|.
187 scoped_refptr<base::MessageLoopProxy> vda_loop_proxy_;
188
189 // The thread to create shared memory. Factories::CreateSharedMemory is
190 // trampolined to the child thread. When |vda_loop_proxy_| is the compositor
191 // thread, blocking on the child thread will deadlock. During WebRTC hang up,
192 // the child thread waits for Chrome_libJingle_WorkerThread. libJingle thread
193 // cannot finish when DecodingThread holds a WebRTC lock and blocks on the
194 // child thread. So we need to call CreateSharedMemory asynchronously from a
195 // different thread.
196 base::Thread create_shm_thread_;
197
198 // The texture target used for decoded pictures.
199 uint32 decoder_texture_target_;
200
201 // Metadata of the buffers that have been sent for decode.
202 std::list<BufferData> input_buffer_data_;
203
204 // A map from bitstream buffer IDs to bitstream buffers that are being
205 // processed by VDA. The map owns SHM buffers.
206 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_;
207
208 // A map from picture buffer IDs to texture-backed picture buffers.
209 std::map<int32, media::PictureBuffer> assigned_picture_buffers_;
210
211 // Picture buffers that are dismissed but not deleted yet.
212 std::map<int32, media::PictureBuffer> dismissed_picture_buffers_;
213
214 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
215 // as VideoFrames to be rendered via read_cb_, and which will be returned
216 // to us via ReusePictureBuffer.
217 std::set<int32> picture_buffers_at_display_;
218
219 // The id that will be given to the next picture buffer.
220 int32 next_picture_buffer_id_;
221
222 // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
223 // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
224 // |next_bitstream_buffer_id_| and |reset_bitstream_buffer_id_|.
225 base::Lock lock_;
226
227 // The state of RTCVideoDecoder. Guarded by |lock_|.
228 State state_;
229
230 // Guarded by |lock_|.
231 webrtc::DecodedImageCallback* decode_complete_callback_;
232
233 // Total number of allocated SHM buffers. Guarded by |lock_|.
234 int num_shm_buffers_;
235
236 // Shared-memory buffer pool. Since allocating SHM segments requires a
237 // round-trip to the browser process, we keep allocation out of the
238 // steady-state of the decoder. The vector owns SHM buffers. Guarded by
239 // |lock_|.
240 std::vector<SHMBuffer*> available_shm_segments_;
241
242 // A queue storing WebRTC encoding images (and their metadata) that are
243 // waiting for the shared memory. Guarded by |lock_|.
244 std::deque<std::pair<webrtc::EncodedImage, BufferData> > pending_buffers_;
245
246 // A queue storing buffers (and their metadata) that will be sent to VDA for
247 // decode. The queue owns SHM buffers. Guarded by |lock_|.
248 std::deque<std::pair<SHMBuffer*, BufferData> > decode_buffers_;
249
250 // The id that will be given to the next bitstream buffer. Guarded by |lock_|.
251 int32 next_bitstream_buffer_id_;
252
253 // A buffer that has an id less than this should be dropped because Reset or
254 // Release has been called. Guarded by |lock_|.
255 int32 reset_bitstream_buffer_id_;
256
257 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder);
258 };
259
260 } // namespace content
261
262 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698