OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 <utility> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "base/synchronization/waitable_event.h" | |
15 #include "content/common/content_export.h" | |
16 #include "media/base/bitstream_buffer.h" | |
17 #include "media/base/video_decoder.h" | |
18 #include "media/base/video_decoder_config.h" | |
19 #include "media/filters/gpu_video_decoder.h" | |
20 #include "media/video/picture.h" | |
21 #include "media/video/video_decode_accelerator.h" | |
22 #include "third_party/webrtc/modules/video_coding/codecs/interface/video_codec_i nterface.h" | |
23 | |
24 namespace base { | |
25 class MessageLoopProxy; | |
26 }; | |
27 | |
28 namespace media { | |
29 class DecoderBuffer; | |
30 } | |
31 | |
32 namespace content { | |
33 | |
34 // This class uses hardware accelerated video decoder to decode video for | |
35 // WebRTC. The message loop of RendererGpuVideoDecoderFactories is stored | |
36 // as |vda_message_loop_|. It is the compositor thread, or the renderer thread | |
37 // if threaded compositing is disabled. VDA::Client methods run on | |
38 // |vda_message_loop_|. webrtc::VideoDecoder methods run on WebRTC | |
39 // DecodingThread or Chrome_libJingle_WorkerThread, which are trampolined to | |
40 // |vda_message_loop_|. Decode() is non-blocking and queues the buffers. | |
41 // Decoded frames are delivered on |vda_message_loop_|. | |
42 class CONTENT_EXPORT RTCVideoDecoder | |
43 : NON_EXPORTED_BASE(public webrtc::VideoDecoder), | |
44 public media::VideoDecodeAccelerator::Client { | |
45 public: | |
46 RTCVideoDecoder( | |
47 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories); | |
wuchengli
2013/05/28 15:01:00
Should GpuVideoDecoder::Factories be moved out of
Ami GONE FROM CHROMIUM
2013/05/29 21:11:46
That would be a reasonable thing to do, but it doe
| |
48 virtual ~RTCVideoDecoder(); | |
49 | |
50 // Initialize VDA. Return false if the codec type is not supported. | |
51 // Called on Chrome_libJingle_WorkerThread | |
52 bool Initialize(webrtc::VideoCodecType type); | |
53 | |
54 // webrtc::VideoDecoder implementation. | |
55 // Called on WebRTC DecodingThread. | |
56 virtual int32_t InitDecode( | |
57 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. | |
70 virtual int32_t Release() OVERRIDE; | |
71 // Called on Chrome_libJingle_WorkerThread. | |
72 virtual int32_t Reset() OVERRIDE; | |
73 | |
74 // VideoDecodeAccelerator::Client implementation. | |
75 virtual void NotifyInitializeDone() OVERRIDE; | |
76 virtual void ProvidePictureBuffers(uint32 count, | |
77 const gfx::Size& size, | |
78 uint32 texture_target) OVERRIDE; | |
79 virtual void DismissPictureBuffer(int32 id) OVERRIDE; | |
80 virtual void PictureReady(const media::Picture& picture) OVERRIDE; | |
81 virtual void NotifyEndOfBitstreamBuffer(int32 id) OVERRIDE; | |
82 virtual void NotifyFlushDone() OVERRIDE; | |
83 virtual void NotifyResetDone() OVERRIDE; | |
84 virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE; | |
85 | |
86 private: | |
87 // A shared memory segment and its allocated size. | |
88 struct SHMBuffer { | |
89 SHMBuffer(base::SharedMemory* m, size_t s); | |
90 ~SHMBuffer(); | |
91 base::SharedMemory* shm; | |
92 size_t size; | |
93 }; | |
94 | |
95 struct BufferData { | |
96 BufferData(int32 bbid, uint32_t ts, int w, int h, size_t s); | |
97 ~BufferData(); | |
98 int32 bitstream_buffer_id; | |
99 uint32_t timestamp; | |
100 uint32_t width; | |
101 uint32_t height; | |
102 size_t size; | |
103 }; | |
104 | |
105 void InitWeakPtr(); | |
106 void ResetInternal(); | |
107 void Destroy(); | |
108 void ReusePictureBuffer(int64 picture_buffer_id); | |
109 | |
110 // Request a shared-memory segment of at least |min_size| bytes. Will | |
111 // allocate as necessary. Caller does not own returned pointer. | |
112 SHMBuffer* GetSHM(size_t min_size); | |
113 | |
114 // Return a shared-memory segment to the available pool. | |
115 void PutSHM(SHMBuffer* shm_buffer); | |
116 | |
117 void CreateVideoDecodeAccelerator(media::VideoCodecProfile profile); | |
118 void DestroyTextures(); | |
119 void DestroyVDA(); | |
120 void RecordBufferData(const BufferData& buffer_data); | |
121 void GetBufferData(int32 id, uint32_t* timestamp, uint32_t* width, | |
122 uint32_t* height, size_t *size); | |
123 bool CanMoreDecodeWorkBeDone(); | |
124 void RequestBufferDecode(); | |
125 | |
126 enum State { | |
127 kUninitialized, // The decoder has not initialized. | |
128 kInitialized, // The decoder has initialized. | |
129 kDecodeError, // Decoding error happened. | |
130 }; | |
131 | |
132 scoped_ptr<media::VideoDecodeAccelerator> vda_; | |
133 | |
134 // Guarded by |lock_|. | |
135 webrtc::DecodedImageCallback* decode_complete_callback_; | |
136 | |
137 // Used to wait for VDA calls to complete in InitDecode(), Release() and | |
138 // Reset(). | |
139 base::WaitableEvent decoder_waiter_; | |
140 | |
141 // The size of the video frames. | |
142 int32_t frame_width_; | |
143 int32_t frame_height_; | |
144 | |
145 // Protects |state_|, |decode_complete_callback_| , and | |
146 // |available_shm_segments_|; | |
147 base::Lock lock_; | |
148 | |
149 // The state of RTCVideoDecoder (guarded by |lock_|). | |
150 State state_; | |
151 | |
152 // The weak pointer should live and die on the |decoder_message_loop_|; | |
153 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_; | |
154 base::WeakPtr<RTCVideoDecoder> weak_this_; | |
155 | |
156 scoped_refptr<media::GpuVideoDecoder::Factories> factories_; | |
157 | |
158 // The message loop to run callbacks on. This is from |factories|. | |
159 scoped_refptr<base::MessageLoopProxy> vda_loop_proxy_; | |
160 | |
161 // The texture target used for decoded pictures. | |
162 uint32 decoder_texture_target_; | |
163 | |
164 // Shared-memory buffer pool. Since allocating SHM segments requires a | |
165 // round-trip to the browser process, we keep allocation out of the | |
166 // steady-state of the decoder. Guarded by |lock_|. | |
167 std::vector<SHMBuffer*> available_shm_segments_; | |
168 | |
169 // Book-keeping variables. | |
170 std::list<BufferData> input_buffer_data_; | |
171 std::deque<std::pair<SHMBuffer*, BufferData> > buffers_to_be_decoded; | |
172 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_; | |
173 std::map<int32, media::PictureBuffer> picture_buffers_in_decoder_; | |
174 | |
175 int32 next_picture_buffer_id_; | |
176 int32 next_bitstream_buffer_id_; | |
177 | |
178 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder); | |
179 }; | |
180 | |
181 } // namespace content | |
182 | |
183 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_ | |
OLD | NEW |