OLD | NEW |
---|---|
(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/memory/weak_ptr.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "base/synchronization/waitable_event.h" | |
16 #include "base/threading/thread.h" | |
17 #include "content/common/content_export.h" | |
18 #include "media/base/bitstream_buffer.h" | |
19 #include "media/base/video_decoder.h" | |
20 #include "media/filters/gpu_video_decoder.h" | |
21 #include "media/video/picture.h" | |
22 #include "media/video/video_decode_accelerator.h" | |
23 #include "third_party/webrtc/modules/video_coding/codecs/interface/video_codec_i nterface.h" | |
24 | |
25 namespace base { | |
26 class MessageLoopProxy; | |
27 }; | |
28 | |
29 namespace media { | |
30 class DecoderBuffer; | |
31 } | |
32 | |
33 namespace content { | |
34 | |
35 // This class uses hardware accelerated video decoder to decode video for | |
36 // WebRTC. The message loop of RendererGpuVideoDecoderFactories is stored as | |
37 // |vda_message_loop_|. It is the compositor thread, or the renderer thread if | |
38 // threaded compositing is disabled. VDA::Client methods run on | |
39 // |vda_message_loop_|. webrtc::VideoDecoder methods run on WebRTC | |
40 // DecodingThread or Chrome_libJingle_WorkerThread, which are trampolined to | |
41 // |vda_message_loop_|. Decode() is non-blocking and queues the buffers. Decoded | |
42 // frames are delivered on |vda_message_loop_|. | |
43 class CONTENT_EXPORT RTCVideoDecoder | |
44 : NON_EXPORTED_BASE(public webrtc::VideoDecoder), | |
45 public media::VideoDecodeAccelerator::Client, | |
46 public base::MessageLoop::DestructionObserver { | |
47 public: | |
48 virtual ~RTCVideoDecoder(); | |
49 | |
50 // Creates a RTCVideoDecoder. Returns NULL if failed. | |
51 static scoped_ptr<RTCVideoDecoder> Create( | |
52 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories); | |
53 | |
54 // webrtc::VideoDecoder implementation. | |
55 // Called on WebRTC DecodingThread. | |
56 virtual int32_t InitDecode(const webrtc::VideoCodec* codecSettings, | |
57 int32_t numberOfCores) OVERRIDE; | |
58 // Called on WebRTC DecodingThread. | |
59 virtual int32_t Decode( | |
60 const webrtc::EncodedImage& inputImage, | |
61 bool missingFrames, | |
62 const webrtc::RTPFragmentationHeader* fragmentation, | |
63 const webrtc::CodecSpecificInfo* codecSpecificInfo = NULL, | |
64 int64_t renderTimeMs = -1) OVERRIDE; | |
65 // Called on WebRTC DecodingThread. | |
66 virtual int32_t RegisterDecodeCompleteCallback( | |
67 webrtc::DecodedImageCallback* callback) OVERRIDE; | |
68 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while | |
69 // this runs. | |
70 virtual int32_t Release() OVERRIDE; | |
71 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while | |
72 // this runs. | |
73 virtual int32_t Reset() OVERRIDE; | |
74 | |
75 // VideoDecodeAccelerator::Client implementation. | |
76 virtual void NotifyInitializeDone() OVERRIDE; | |
77 virtual void ProvidePictureBuffers(uint32 count, | |
78 const gfx::Size& size, | |
79 uint32 texture_target) OVERRIDE; | |
80 virtual void DismissPictureBuffer(int32 id) OVERRIDE; | |
81 virtual void PictureReady(const media::Picture& picture) OVERRIDE; | |
82 virtual void NotifyEndOfBitstreamBuffer(int32 id) OVERRIDE; | |
83 virtual void NotifyFlushDone() OVERRIDE; | |
84 virtual void NotifyResetDone() OVERRIDE; | |
85 virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE; | |
86 | |
87 // base::DestructionObserver implementation. Called when |vda_message_loop_| | |
88 // is stopped. | |
89 virtual void WillDestroyCurrentMessageLoop(); | |
90 | |
91 private: | |
92 struct SHMBuffer; | |
93 struct BufferData; | |
94 | |
95 RTCVideoDecoder( | |
96 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories); | |
97 | |
98 void Initialize(base::WaitableEvent* waiter); | |
99 | |
100 // Requests a buffer to be decoded by VDA. | |
101 void RequestBufferDecode(); | |
102 | |
103 bool CanMoreDecodeWorkBeDone(); | |
104 | |
105 // Returns true if bitstream buffer id |later| comes after |earlier|. This | |
106 // handles the wraparound. | |
107 bool IsBufferAfterReset(int32 id_buffer, int32 id_reset); | |
108 | |
109 // Sends the WebRTC buffer for decode on |vda_loop_proxy_|. | |
110 void SendBufferForDecode(const webrtc::EncodedImage& input_image, | |
111 SHMBuffer* shm_buffer, | |
112 const BufferData& buffer_data); | |
113 | |
114 // Sends all WebRTC buffers for decode on |vda_loop_proxy_|. | |
115 void SendPendingBuffersForDecode(); | |
116 | |
117 // Saves a WebRTC buffer for decode later. Returns WEBRTC_VIDEO_CODEC_OK on | |
118 // success. Returns WEBRTC_VIDEO_CODEC_ERROR on failure. | |
119 int SaveToPendingBuffers(const webrtc::EncodedImage& input_image, | |
Ami GONE FROM CHROMIUM
2013/06/28 17:04:00
returning int where a bool will do is a webrtc'ism
wuchengli
2013/07/02 10:34:25
Done.
| |
120 const BufferData& buffer_data); | |
121 | |
122 scoped_refptr<media::VideoFrame> CreateVideoFrame( | |
123 const media::Picture& picture, | |
124 const media::PictureBuffer& pb, | |
125 uint32_t timestamp, | |
126 uint32_t width, | |
127 uint32_t height, | |
128 size_t size); | |
129 | |
130 // Resets VDA. | |
131 void ResetInternal(); | |
132 | |
133 // Tells VDA that a picture buffer can be recycled. | |
134 void ReusePictureBuffer(int64 picture_buffer_id); | |
135 | |
136 void DestroyTextures(); | |
137 void DestroyVDA(); | |
138 | |
139 // Gets a shared-memory segment of at least |min_size| bytes from | |
140 // |available_shm_segments_|. Returns NULL if there is no buffer or the | |
141 // buffer is not big enough. | |
142 SHMBuffer* GetSHM(size_t min_size); | |
143 | |
144 // Allocates shared memory of at least |min_size| bytes. | |
145 void CreateSHM(size_t min_size); | |
146 | |
147 // Returns a shared-memory segment to the available pool. | |
148 void PutSHM(SHMBuffer* shm_buffer); | |
149 | |
150 // Stores the buffer metadata to |input_buffer_data_|. | |
151 void RecordBufferData(const BufferData& buffer_data); | |
152 // Gets the buffer metadata from |input_buffer_data_|. | |
153 void GetBufferData(int32 bitstream_buffer_id, | |
154 uint32_t* timestamp, | |
155 uint32_t* width, | |
156 uint32_t* height, | |
157 size_t* size); | |
158 | |
159 enum State { | |
160 UNINITIALIZED, // The decoder has not initialized. | |
161 INITIALIZED, // The decoder has initialized. | |
162 RESETTING, // The decoder is being reset. | |
163 DECODE_ERROR, // Decoding error happened. | |
164 }; | |
165 | |
166 // The hardware video decoder. | |
167 scoped_ptr<media::VideoDecodeAccelerator> vda_; | |
168 | |
169 // The size of the incoming video frames. | |
170 gfx::Size frame_size_; | |
171 | |
172 // The weak pointer should live and die on the |vda_loop_proxy_|; | |
173 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_; | |
174 base::WeakPtr<RTCVideoDecoder> weak_this_; | |
175 | |
176 scoped_refptr<media::GpuVideoDecoder::Factories> factories_; | |
177 | |
178 // The message loop to run callbacks on. This is should be the same as the one | |
179 // of |factories_|. | |
180 scoped_refptr<base::MessageLoopProxy> vda_loop_proxy_; | |
181 | |
182 // The texture target used for decoded pictures. | |
183 uint32 decoder_texture_target_; | |
184 | |
185 // A queue storing WebRTC encoding images (and their metadata) that are | |
186 // waiting for the shared memory. Only accessed by WebRTC DecodingThread. | |
187 std::deque<std::pair<webrtc::EncodedImage, BufferData> > webrtc_buffers_; | |
188 | |
189 // Metadata of the buffers that have been sent for decode. | |
190 std::list<BufferData> input_buffer_data_; | |
191 | |
192 // A map from bitstream buffer IDs to bitstream buffers that are being | |
193 // processed by VDA. The map owns SHM buffers. | |
194 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_; | |
195 | |
196 // A map from picture buffer IDs to texture-backed picture buffers. | |
197 std::map<int32, media::PictureBuffer> assigned_picture_buffers_; | |
198 | |
199 // Picture buffers that are dismissed but not deleted yet. | |
200 std::map<int32, media::PictureBuffer> dismissed_picture_buffers_; | |
201 | |
202 // PictureBuffers given to us by VDA via PictureReady, which we sent forward | |
203 // as VideoFrames to be rendered via read_cb_, and which will be returned | |
204 // to us via ReusePictureBuffer. | |
205 std::set<int32> picture_buffers_at_display_; | |
206 | |
207 // The id that will be given to the next picture buffer. | |
208 int32 next_picture_buffer_id_; | |
209 | |
210 // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|, | |
211 // |available_shm_segments_|, |buffers_to_be_decoded_|, | |
212 // |next_bitstream_buffer_id_| and |reset_bitstream_buffer_id_|. | |
213 base::Lock lock_; | |
214 | |
215 // The state of RTCVideoDecoder. Guarded by |lock_|. | |
216 State state_; | |
217 | |
218 // Guarded by |lock_|. | |
219 webrtc::DecodedImageCallback* decode_complete_callback_; | |
220 | |
221 // Total number of allocated SHM buffers. Guarded by |lock_|. | |
222 int num_shm_buffers_; | |
223 | |
224 // Shared-memory buffer pool. Since allocating SHM segments requires a | |
225 // round-trip to the browser process, we keep allocation out of the | |
226 // steady-state of the decoder. The vector owns SHM buffers. Guarded by | |
227 // |lock_|. | |
228 std::vector<SHMBuffer*> available_shm_segments_; | |
229 | |
230 // A queue storing buffers (and their metadata) that will be sent to VDA for | |
231 // decode. The queue owns SHM buffers. Guarded by |lock_|. | |
232 std::deque<std::pair<SHMBuffer*, BufferData> > buffers_to_be_decoded_; | |
233 | |
234 // The id that will be given to the next bitstream buffer. Guarded by |lock_|. | |
235 int32 next_bitstream_buffer_id_; | |
236 | |
237 // A buffer that has an id less than this should be dropped because Reset or | |
238 // Release has been called. Guarded by |lock_|. | |
239 int32 reset_bitstream_buffer_id_; | |
240 | |
241 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder); | |
242 }; | |
243 | |
244 } // namespace content | |
245 | |
246 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_ | |
OLD | NEW |