Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / rtc_video_decoder.h
blob9a92aaca1687653574f16164b7224ef44e086618
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;
30 namespace media {
31 class DecoderBuffer;
32 class GpuVideoAcceleratorFactories;
35 namespace content {
37 // This class uses hardware accelerated video decoder to decode video for
38 // WebRTC. |vda_message_loop_| is the message loop proxy of the media thread,
39 // which VDA::Client methods run on. 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 to WebRTC on |vda_message_loop_|.
43 class CONTENT_EXPORT RTCVideoDecoder
44 : NON_EXPORTED_BASE(public webrtc::VideoDecoder),
45 public media::VideoDecodeAccelerator::Client {
46 public:
47 ~RTCVideoDecoder() override;
49 // Creates a RTCVideoDecoder. Returns NULL if failed. The video decoder will
50 // run on the message loop of |factories|.
51 static scoped_ptr<RTCVideoDecoder> Create(
52 webrtc::VideoCodecType type,
53 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories);
55 // webrtc::VideoDecoder implementation.
56 // Called on WebRTC DecodingThread.
57 int32_t InitDecode(const webrtc::VideoCodec* codecSettings,
58 int32_t numberOfCores) override;
59 // Called on WebRTC DecodingThread.
60 int32_t Decode(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 int32_t RegisterDecodeCompleteCallback(
67 webrtc::DecodedImageCallback* callback) override;
68 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
69 // this runs.
70 int32_t Release() override;
71 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
72 // this runs.
73 int32_t Reset() override;
75 // VideoDecodeAccelerator::Client implementation.
76 void ProvidePictureBuffers(uint32 count,
77 const gfx::Size& size,
78 uint32 texture_target) override;
79 void DismissPictureBuffer(int32 id) override;
80 void PictureReady(const media::Picture& picture) override;
81 void NotifyEndOfBitstreamBuffer(int32 id) override;
82 void NotifyFlushDone() override;
83 void NotifyResetDone() override;
84 void NotifyError(media::VideoDecodeAccelerator::Error error) override;
86 private:
87 class SHMBuffer;
88 // Metadata of a bitstream buffer.
89 struct BufferData {
90 BufferData(int32 bitstream_buffer_id,
91 uint32_t timestamp,
92 size_t size);
93 BufferData();
94 ~BufferData();
95 int32 bitstream_buffer_id;
96 uint32_t timestamp; // in 90KHz
97 size_t size; // buffer size
100 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
101 FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsFirstBufferAfterReset);
103 RTCVideoDecoder(
104 webrtc::VideoCodecType type,
105 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories);
107 // Requests a buffer to be decoded by VDA.
108 void RequestBufferDecode();
110 bool CanMoreDecodeWorkBeDone();
112 // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|.
113 // This handles the wraparound.
114 bool IsBufferAfterReset(int32 id_buffer, int32 id_reset);
116 // Returns true if bitstream buffer |id_buffer| is the first buffer after
117 // |id_reset|.
118 bool IsFirstBufferAfterReset(int32 id_buffer, int32 id_reset);
120 // Saves a WebRTC buffer in |decode_buffers_| for decode.
121 void SaveToDecodeBuffers_Locked(const webrtc::EncodedImage& input_image,
122 scoped_ptr<SHMBuffer> shm_buffer,
123 const BufferData& buffer_data);
125 // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
126 // Returns true on success.
127 bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
128 const BufferData& buffer_data);
130 // Gets SHM and moves pending buffers to decode buffers.
131 void MovePendingBuffersToDecodeBuffers();
133 scoped_refptr<media::VideoFrame> CreateVideoFrame(
134 const media::Picture& picture,
135 const media::PictureBuffer& pb,
136 uint32_t timestamp);
138 // Resets VDA.
139 void ResetInternal();
141 // Static method is to allow it to run even after RVD is deleted.
142 static void ReleaseMailbox(
143 base::WeakPtr<RTCVideoDecoder> decoder,
144 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories,
145 int64 picture_buffer_id,
146 uint32 texture_id,
147 uint32 release_sync_point);
148 // Tells VDA that a picture buffer can be recycled.
149 void ReusePictureBuffer(int64 picture_buffer_id);
151 // Create |vda_| on |vda_loop_proxy_|.
152 void CreateVDA(media::VideoCodecProfile profile, base::WaitableEvent* waiter);
154 void DestroyTextures();
155 void DestroyVDA();
157 // Gets a shared-memory segment of at least |min_size| bytes from
158 // |available_shm_segments_|. Returns NULL if there is no buffer or the
159 // buffer is not big enough.
160 scoped_ptr<SHMBuffer> GetSHM_Locked(size_t min_size);
162 // Returns a shared-memory segment to the available pool.
163 void PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer);
165 // Allocates |count| shared memory buffers of |size| bytes.
166 void CreateSHM(size_t count, size_t size);
168 // Stores the buffer metadata to |input_buffer_data_|.
169 void RecordBufferData(const BufferData& buffer_data);
170 // Gets the buffer metadata from |input_buffer_data_|.
171 void GetBufferData(int32 bitstream_buffer_id, uint32_t* timestamp);
173 // Records the result of InitDecode to UMA and returns |status|.
174 int32_t RecordInitDecodeUMA(int32_t status);
176 // Assert the contract that this class is operated on the right thread.
177 void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
179 // Query factories_ whether |profile| is supported and return true is so,
180 // false otherwise. If true, also set resolution limits for |profile|
181 // in min/max_resolution_.
182 bool IsProfileSupported(media::VideoCodecProfile profile);
184 // Clear the pending_buffers_ queue, freeing memory.
185 void ClearPendingBuffers();
187 enum State {
188 UNINITIALIZED, // The decoder has not initialized.
189 INITIALIZED, // The decoder has initialized.
190 RESETTING, // The decoder is being reset.
191 DECODE_ERROR, // Decoding error happened.
194 static const int32 ID_LAST; // maximum bitstream buffer id
195 static const int32 ID_HALF; // half of the maximum bitstream buffer id
196 static const int32 ID_INVALID; // indicates Reset or Release never occurred
198 // The hardware video decoder.
199 scoped_ptr<media::VideoDecodeAccelerator> vda_;
201 // The video codec type, as reported by WebRTC.
202 const webrtc::VideoCodecType video_codec_type_;
204 // The size of the incoming video frames.
205 gfx::Size frame_size_;
207 scoped_refptr<media::GpuVideoAcceleratorFactories> factories_;
209 // The texture target used for decoded pictures.
210 uint32 decoder_texture_target_;
212 // Metadata of the buffers that have been sent for decode.
213 std::list<BufferData> input_buffer_data_;
215 // A map from bitstream buffer IDs to bitstream buffers that are being
216 // processed by VDA. The map owns SHM buffers.
217 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_;
219 // A map from picture buffer IDs to texture-backed picture buffers.
220 std::map<int32, media::PictureBuffer> assigned_picture_buffers_;
222 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
223 // as VideoFrames to be rendered via read_cb_, and which will be returned
224 // to us via ReusePictureBuffer.
225 typedef std::map<int32 /* picture_buffer_id */, uint32 /* texture_id */>
226 PictureBufferTextureMap;
227 PictureBufferTextureMap picture_buffers_at_display_;
229 // The id that will be given to the next picture buffer.
230 int32 next_picture_buffer_id_;
232 // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
233 // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
234 // |next_bitstream_buffer_id_| and |reset_bitstream_buffer_id_|.
235 base::Lock lock_;
237 // The state of RTCVideoDecoder. Guarded by |lock_|.
238 State state_;
240 // Guarded by |lock_|.
241 webrtc::DecodedImageCallback* decode_complete_callback_;
243 // Total number of allocated SHM buffers. Guarded by |lock_|.
244 size_t num_shm_buffers_;
246 // Shared-memory buffer pool. Since allocating SHM segments requires a
247 // round-trip to the browser process, we keep allocation out of the
248 // steady-state of the decoder. The vector owns SHM buffers. Guarded by
249 // |lock_|.
250 std::vector<SHMBuffer*> available_shm_segments_;
252 // A queue storing WebRTC encoding images (and their metadata) that are
253 // waiting for the shared memory. Guarded by |lock_|.
254 std::deque<std::pair<webrtc::EncodedImage, BufferData> > pending_buffers_;
256 // A queue storing buffers (and their metadata) that will be sent to VDA for
257 // decode. The queue owns SHM buffers. Guarded by |lock_|.
258 std::deque<std::pair<SHMBuffer*, BufferData> > decode_buffers_;
260 // The id that will be given to the next bitstream buffer. Guarded by |lock_|.
261 int32 next_bitstream_buffer_id_;
263 // A buffer that has an id less than this should be dropped because Reset or
264 // Release has been called. Guarded by |lock_|.
265 int32 reset_bitstream_buffer_id_;
267 // Minimum and maximum supported resolutions for the current profile/VDA.
268 gfx::Size min_resolution_;
269 gfx::Size max_resolution_;
271 // Must be destroyed, or invalidated, on |vda_loop_proxy_|
272 // NOTE: Weak pointers must be invalidated before all other member variables.
273 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_;
275 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder);
278 } // namespace content
280 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_