Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / rtc_video_decoder.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_
7
8 #include <deque>
9 #include <list>
10 #include <map>
11 #include <set>
12 #include <utility>
13
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"
25
26 namespace base {
27 class WaitableEvent;
28 class MessageLoopProxy;
29 };
30
31 namespace media {
32 class DecoderBuffer;
33 class GpuVideoAcceleratorFactories;
34 }
35
36 namespace content {
37
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   virtual ~RTCVideoDecoder();
49
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);
55
56   // webrtc::VideoDecoder implementation.
57   // Called on WebRTC DecodingThread.
58   virtual int32_t InitDecode(const webrtc::VideoCodec* codecSettings,
59                              int32_t numberOfCores) OVERRIDE;
60   // Called on WebRTC DecodingThread.
61   virtual int32_t Decode(
62       const webrtc::EncodedImage& inputImage,
63       bool missingFrames,
64       const webrtc::RTPFragmentationHeader* fragmentation,
65       const webrtc::CodecSpecificInfo* codecSpecificInfo = NULL,
66       int64_t renderTimeMs = -1) OVERRIDE;
67   // Called on WebRTC DecodingThread.
68   virtual int32_t RegisterDecodeCompleteCallback(
69       webrtc::DecodedImageCallback* callback) OVERRIDE;
70   // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
71   // this runs.
72   virtual int32_t Release() OVERRIDE;
73   // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while
74   // this runs.
75   virtual int32_t Reset() OVERRIDE;
76
77   // VideoDecodeAccelerator::Client implementation.
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  private:
89   class SHMBuffer;
90   // Metadata of a bitstream buffer.
91   struct BufferData {
92     BufferData(int32 bitstream_buffer_id,
93                uint32_t timestamp,
94                int width,
95                int height,
96                size_t size);
97     BufferData();
98     ~BufferData();
99     int32 bitstream_buffer_id;
100     uint32_t timestamp;  // in 90KHz
101     uint32_t width;
102     uint32_t height;
103     size_t size;  // buffer size
104   };
105
106   FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsBufferAfterReset);
107   FRIEND_TEST_ALL_PREFIXES(RTCVideoDecoderTest, IsFirstBufferAfterReset);
108
109   RTCVideoDecoder(
110       const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories);
111
112   // Requests a buffer to be decoded by VDA.
113   void RequestBufferDecode();
114
115   bool CanMoreDecodeWorkBeDone();
116
117   // Returns true if bitstream buffer id |id_buffer| comes after |id_reset|.
118   // This handles the wraparound.
119   bool IsBufferAfterReset(int32 id_buffer, int32 id_reset);
120
121   // Returns true if bitstream buffer |id_buffer| is the first buffer after
122   // |id_reset|.
123   bool IsFirstBufferAfterReset(int32 id_buffer, int32 id_reset);
124
125   // Saves a WebRTC buffer in |decode_buffers_| for decode.
126   void SaveToDecodeBuffers_Locked(const webrtc::EncodedImage& input_image,
127                                   scoped_ptr<SHMBuffer> shm_buffer,
128                                   const BufferData& buffer_data);
129
130   // Saves a WebRTC buffer in |pending_buffers_| waiting for SHM available.
131   // Returns true on success.
132   bool SaveToPendingBuffers_Locked(const webrtc::EncodedImage& input_image,
133                                    const BufferData& buffer_data);
134
135   // Gets SHM and moves pending buffers to decode buffers.
136   void MovePendingBuffersToDecodeBuffers();
137
138   scoped_refptr<media::VideoFrame> CreateVideoFrame(
139       const media::Picture& picture,
140       const media::PictureBuffer& pb,
141       uint32_t timestamp,
142       uint32_t width,
143       uint32_t height,
144       size_t size);
145
146   // Resets VDA.
147   void ResetInternal();
148
149   // Static method is to allow it to run even after RVD is deleted.
150   static void ReleaseMailbox(
151       base::WeakPtr<RTCVideoDecoder> decoder,
152       const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories,
153       int64 picture_buffer_id,
154       uint32 texture_id,
155       const std::vector<uint32>& release_sync_points);
156   // Tells VDA that a picture buffer can be recycled.
157   void ReusePictureBuffer(int64 picture_buffer_id);
158
159   // Create |vda_| on |vda_loop_proxy_|.
160   void CreateVDA(media::VideoCodecProfile profile, base::WaitableEvent* waiter);
161
162   void DestroyTextures();
163   void DestroyVDA();
164
165   // Gets a shared-memory segment of at least |min_size| bytes from
166   // |available_shm_segments_|. Returns NULL if there is no buffer or the
167   // buffer is not big enough.
168   scoped_ptr<SHMBuffer> GetSHM_Locked(size_t min_size);
169
170   // Returns a shared-memory segment to the available pool.
171   void PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer);
172
173   // Allocates |number| shared memory of at least |min_size| bytes.
174   void CreateSHM(int number, size_t min_size);
175
176   // Stores the buffer metadata to |input_buffer_data_|.
177   void RecordBufferData(const BufferData& buffer_data);
178   // Gets the buffer metadata from |input_buffer_data_|.
179   void GetBufferData(int32 bitstream_buffer_id,
180                      uint32_t* timestamp,
181                      uint32_t* width,
182                      uint32_t* height,
183                      size_t* size);
184
185   // Records the result of InitDecode to UMA and returns |status|.
186   int32_t RecordInitDecodeUMA(int32_t status);
187
188   // Assert the contract that this class is operated on the right thread.
189   void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
190
191   enum State {
192     UNINITIALIZED,  // The decoder has not initialized.
193     INITIALIZED,    // The decoder has initialized.
194     RESETTING,      // The decoder is being reset.
195     DECODE_ERROR,   // Decoding error happened.
196   };
197
198   static const int32 ID_LAST;     // maximum bitstream buffer id
199   static const int32 ID_HALF;     // half of the maximum bitstream buffer id
200   static const int32 ID_INVALID;  // indicates Reset or Release never occurred
201
202   // The hardware video decoder.
203   scoped_ptr<media::VideoDecodeAccelerator> vda_;
204
205   // The size of the incoming video frames.
206   gfx::Size frame_size_;
207
208   scoped_refptr<media::GpuVideoAcceleratorFactories> factories_;
209
210   // The texture target used for decoded pictures.
211   uint32 decoder_texture_target_;
212
213   // Metadata of the buffers that have been sent for decode.
214   std::list<BufferData> input_buffer_data_;
215
216   // A map from bitstream buffer IDs to bitstream buffers that are being
217   // processed by VDA. The map owns SHM buffers.
218   std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_;
219
220   // A map from picture buffer IDs to texture-backed picture buffers.
221   std::map<int32, media::PictureBuffer> assigned_picture_buffers_;
222
223   // PictureBuffers given to us by VDA via PictureReady, which we sent forward
224   // as VideoFrames to be rendered via read_cb_, and which will be returned
225   // to us via ReusePictureBuffer.
226   typedef std::map<int32 /* picture_buffer_id */, uint32 /* texture_id */>
227       PictureBufferTextureMap;
228   PictureBufferTextureMap picture_buffers_at_display_;
229
230   // The id that will be given to the next picture buffer.
231   int32 next_picture_buffer_id_;
232
233   // Protects |state_|, |decode_complete_callback_| , |num_shm_buffers_|,
234   // |available_shm_segments_|, |pending_buffers_|, |decode_buffers_|,
235   // |next_bitstream_buffer_id_| and |reset_bitstream_buffer_id_|.
236   base::Lock lock_;
237
238   // The state of RTCVideoDecoder. Guarded by |lock_|.
239   State state_;
240
241   // Guarded by |lock_|.
242   webrtc::DecodedImageCallback* decode_complete_callback_;
243
244   // Total number of allocated SHM buffers. Guarded by |lock_|.
245   int num_shm_buffers_;
246
247   // Shared-memory buffer pool.  Since allocating SHM segments requires a
248   // round-trip to the browser process, we keep allocation out of the
249   // steady-state of the decoder. The vector owns SHM buffers. Guarded by
250   // |lock_|.
251   std::vector<SHMBuffer*> available_shm_segments_;
252
253   // A queue storing WebRTC encoding images (and their metadata) that are
254   // waiting for the shared memory. Guarded by |lock_|.
255   std::deque<std::pair<webrtc::EncodedImage, BufferData> > pending_buffers_;
256
257   // A queue storing buffers (and their metadata) that will be sent to VDA for
258   // decode. The queue owns SHM buffers. Guarded by |lock_|.
259   std::deque<std::pair<SHMBuffer*, BufferData> > decode_buffers_;
260
261   // The id that will be given to the next bitstream buffer. Guarded by |lock_|.
262   int32 next_bitstream_buffer_id_;
263
264   // A buffer that has an id less than this should be dropped because Reset or
265   // Release has been called. Guarded by |lock_|.
266   int32 reset_bitstream_buffer_id_;
267
268   // Must be destroyed, or invalidated, on |vda_loop_proxy_|
269   // NOTE: Weak pointers must be invalidated before all other member variables.
270   base::WeakPtrFactory<RTCVideoDecoder> weak_factory_;
271
272   DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder);
273 };
274
275 }  // namespace content
276
277 #endif  // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_