Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / rtc_video_decoder.h
blob29adee642e687c14f4a0222ea4171848a3717b73
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.
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
8 #include <deque>
9 #include <list>
10 #include <map>
11 #include <set>
12 #include <utility>
14 #include "base/basictypes.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/synchronization/lock.h"
18 #include "base/threading/thread.h"
19 #include "content/common/content_export.h"
20 #include "media/base/bitstream_buffer.h"
21 #include "media/base/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_interface.h"
26 namespace base {
27 class WaitableEvent;
28 class MessageLoopProxy;
31 namespace media {
32 class DecoderBuffer;
33 class GpuVideoAcceleratorFactories;
36 namespace content {
38 // This class uses hardware accelerated video decoder to decode video for
39 // WebRTC. |vda_message_loop_| is the message loop proxy of the media thread,
40 // which VDA::Client methods run on. 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 to WebRTC on |vda_message_loop_|.
44 class CONTENT_EXPORT RTCVideoDecoder
45 : NON_EXPORTED_BASE(public webrtc::VideoDecoder),
46 public media::VideoDecodeAccelerator::Client {
47 public:
48 ~RTCVideoDecoder() override;
50 // Creates a RTCVideoDecoder. Returns NULL if failed. The video decoder will
51 // run on the message loop of |factories|.
52 static scoped_ptr<RTCVideoDecoder> Create(
53 webrtc::VideoCodecType type,
54 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories);
56 // webrtc::VideoDecoder implementation.
57 // Called on WebRTC DecodingThread.
58 int32_t InitDecode(const webrtc::VideoCodec* codecSettings,
59 int32_t numberOfCores) override;
60 // Called on WebRTC DecodingThread.
61 int32_t Decode(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 int32_t RegisterDecodeCompleteCallback(
68 webrtc::DecodedImageCallback* callback) override;
69 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
70 // this runs.
71 int32_t Release() override;
72 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
73 // this runs.
74 int32_t Reset() override;
76 // VideoDecodeAccelerator::Client implementation.
77 void ProvidePictureBuffers(uint32 count,
78 const gfx::Size& size,
79 uint32 texture_target) override;
80 void DismissPictureBuffer(int32 id) override;
81 void PictureReady(const media::Picture& picture) override;
82 void NotifyEndOfBitstreamBuffer(int32 id) override;
83 void NotifyFlushDone() override;
84 void NotifyResetDone() override;
85 void NotifyError(media::VideoDecodeAccelerator::Error error) override;
87 private:
88 class SHMBuffer;
89 // Metadata of a bitstream buffer.
90 struct BufferData {
91 BufferData(int32 bitstream_buffer_id,
92 uint32_t timestamp,
93 size_t size);
94 BufferData();
95 ~BufferData();
96 int32 bitstream_buffer_id;
97 uint32_t timestamp; // in 90KHz
98 size_t size; // buffer size
101 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
102 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsFirstBufferAfterReset);
104 RTCVideoDecoder(
105 webrtc::VideoCodecType type,
106 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories);
108 // Requests a buffer to be decoded by VDA.
109 void RequestBufferDecode();
111 bool CanMoreDecodeWorkBeDone();
113 // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|.
114 // This handles the wraparound.
115 bool IsBufferAfterReset(int32 id_buffer, int32 id_reset);
117 // Returns true if bitstream buffer |id_buffer| is the first buffer after
118 // |id_reset|.
119 bool IsFirstBufferAfterReset(int32 id_buffer, int32 id_reset);
121 // Saves a WebRTC buffer in |decode_buffers_| for decode.
122 void SaveToDecodeBuffers_Locked(const webrtc::EncodedImage& input_image,
123 scoped_ptr<SHMBuffer> shm_buffer,
124 const BufferData& buffer_data);
126 // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
127 // Returns true on success.
128 bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
129 const BufferData& buffer_data);
131 // Gets SHM and moves pending buffers to decode buffers.
132 void MovePendingBuffersToDecodeBuffers();
134 scoped_refptr<media::VideoFrame> CreateVideoFrame(
135 const media::Picture& picture,
136 const media::PictureBuffer& pb,
137 uint32_t timestamp);
139 // Resets VDA.
140 void ResetInternal();
142 // Static method is to allow it to run even after RVD is deleted.
143 static void ReleaseMailbox(
144 base::WeakPtr<RTCVideoDecoder> decoder,
145 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories,
146 int64 picture_buffer_id,
147 uint32 texture_id,
148 uint32 release_sync_point);
149 // Tells VDA that a picture buffer can be recycled.
150 void ReusePictureBuffer(int64 picture_buffer_id);
152 // Create |vda_| on |vda_loop_proxy_|.
153 void CreateVDA(media::VideoCodecProfile profile, base::WaitableEvent* waiter);
155 void DestroyTextures();
156 void DestroyVDA();
158 // Gets a shared-memory segment of at least |min_size| bytes from
159 // |available_shm_segments_|. Returns NULL if there is no buffer or the
160 // buffer is not big enough.
161 scoped_ptr<SHMBuffer> GetSHM_Locked(size_t min_size);
163 // Returns a shared-memory segment to the available pool.
164 void PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer);
166 // Allocates |number| shared memory of at least |min_size| bytes.
167 void CreateSHM(int number, size_t min_size);
169 // Stores the buffer metadata to |input_buffer_data_|.
170 void RecordBufferData(const BufferData& buffer_data);
171 // Gets the buffer metadata from |input_buffer_data_|.
172 void GetBufferData(int32 bitstream_buffer_id, uint32_t* timestamp);
174 // Records the result of InitDecode to UMA and returns |status|.
175 int32_t RecordInitDecodeUMA(int32_t status);
177 // Assert the contract that this class is operated on the right thread.
178 void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
180 enum State {
181 UNINITIALIZED, // The decoder has not initialized.
182 INITIALIZED, // The decoder has initialized.
183 RESETTING, // The decoder is being reset.
184 DECODE_ERROR, // Decoding error happened.
187 static const int32 ID_LAST; // maximum bitstream buffer id
188 static const int32 ID_HALF; // half of the maximum bitstream buffer id
189 static const int32 ID_INVALID; // indicates Reset or Release never occurred
191 // The hardware video decoder.
192 scoped_ptr<media::VideoDecodeAccelerator> vda_;
194 // The video codec type, as reported by WebRTC.
195 const webrtc::VideoCodecType video_codec_type_;
197 // The size of the incoming video frames.
198 gfx::Size frame_size_;
200 scoped_refptr<media::GpuVideoAcceleratorFactories> factories_;
202 // The texture target used for decoded pictures.
203 uint32 decoder_texture_target_;
205 // Metadata of the buffers that have been sent for decode.
206 std::list<BufferData> input_buffer_data_;
208 // A map from bitstream buffer IDs to bitstream buffers that are being
209 // processed by VDA. The map owns SHM buffers.
210 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_;
212 // A map from picture buffer IDs to texture-backed picture buffers.
213 std::map<int32, media::PictureBuffer> assigned_picture_buffers_;
215 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
216 // as VideoFrames to be rendered via read_cb_, and which will be returned
217 // to us via ReusePictureBuffer.
218 typedef std::map<int32 /* picture_buffer_id */, uint32 /* texture_id */>
219 PictureBufferTextureMap;
220 PictureBufferTextureMap picture_buffers_at_display_;
222 // The id that will be given to the next picture buffer.
223 int32 next_picture_buffer_id_;
225 // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
226 // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
227 // |next_bitstream_buffer_id_| and |reset_bitstream_buffer_id_|.
228 base::Lock lock_;
230 // The state of RTCVideoDecoder. Guarded by |lock_|.
231 State state_;
233 // Guarded by |lock_|.
234 webrtc::DecodedImageCallback* decode_complete_callback_;
236 // Total number of allocated SHM buffers. Guarded by |lock_|.
237 int num_shm_buffers_;
239 // Shared-memory buffer pool. Since allocating SHM segments requires a
240 // round-trip to the browser process, we keep allocation out of the
241 // steady-state of the decoder. The vector owns SHM buffers. Guarded by
242 // |lock_|.
243 std::vector<SHMBuffer*> available_shm_segments_;
245 // A queue storing WebRTC encoding images (and their metadata) that are
246 // waiting for the shared memory. Guarded by |lock_|.
247 std::deque<std::pair<webrtc::EncodedImage, BufferData> > pending_buffers_;
249 // A queue storing buffers (and their metadata) that will be sent to VDA for
250 // decode. The queue owns SHM buffers. Guarded by |lock_|.
251 std::deque<std::pair<SHMBuffer*, BufferData> > decode_buffers_;
253 // The id that will be given to the next bitstream buffer. Guarded by |lock_|.
254 int32 next_bitstream_buffer_id_;
256 // A buffer that has an id less than this should be dropped because Reset or
257 // Release has been called. Guarded by |lock_|.
258 int32 reset_bitstream_buffer_id_;
260 // Must be destroyed, or invalidated, on |vda_loop_proxy_|
261 // NOTE: Weak pointers must be invalidated before all other member variables.
262 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_;
264 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder);
267 } // namespace content
269 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_