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 RTCVideoDecoder( |
| 49 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories); |
| 50 virtual ~RTCVideoDecoder(); |
| 51 |
| 52 // Initializes VDA. True if successful. |
| 53 virtual bool InitVideoDecodeAccelerator(); |
| 54 |
| 55 // webrtc::VideoDecoder implementation. |
| 56 // Called on WebRTC DecodingThread. |
| 57 virtual int32_t InitDecode(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. The child thread is blocked while |
| 70 // this runs. |
| 71 virtual int32_t Release() OVERRIDE; |
| 72 // Called on Chrome_libJingle_WorkerThread. The child thread is blocked while |
| 73 // this runs. |
| 74 virtual int32_t Reset() OVERRIDE; |
| 75 |
| 76 // VideoDecodeAccelerator::Client implementation. |
| 77 virtual void NotifyInitializeDone() OVERRIDE; |
| 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 // base::DestructionObserver implementation. Called when |vda_message_loop_| |
| 89 // is stopped. |
| 90 virtual void WillDestroyCurrentMessageLoop(); |
| 91 |
| 92 private: |
| 93 struct SHMBuffer; |
| 94 struct BufferData; |
| 95 |
| 96 void Initialize(); |
| 97 |
| 98 // Requests a buffer to be decoded. |
| 99 void RequestBufferDecode(); |
| 100 |
| 101 bool CanMoreDecodeWorkBeDone(); |
| 102 |
| 103 // Resets VDA. |
| 104 void ResetInternal(); |
| 105 |
| 106 // Tells VDA that a picture buffer can be recycled. |
| 107 void ReusePictureBuffer(int64 picture_buffer_id); |
| 108 |
| 109 void DestroyTextures(); |
| 110 void DestroyVDA(); |
| 111 void ClearBufferQueue(std::deque<std::pair<SHMBuffer*, BufferData> >& queue); |
| 112 void RecycleBufferQueue( |
| 113 std::deque<std::pair<SHMBuffer*, BufferData> >& queue); |
| 114 |
| 115 // Requests a shared-memory segment of at least |min_size| bytes. Will |
| 116 // allocate as necessary. Caller does not own returned pointer. |
| 117 SHMBuffer* GetSHM(size_t min_size); |
| 118 |
| 119 // Returns a shared-memory segment to the available pool. |
| 120 void PutSHM(SHMBuffer* shm_buffer); |
| 121 |
| 122 // Stores the buffer metadata to |input_buffer_data_|. |
| 123 void RecordBufferData(const BufferData& buffer_data); |
| 124 // Gets the buffer metadata from |input_buffer_data_|. |
| 125 void GetBufferData(int32 bitstream_buffer_id, |
| 126 uint32_t* timestamp, |
| 127 uint32_t* width, |
| 128 uint32_t* height, |
| 129 size_t* size); |
| 130 |
| 131 enum State { |
| 132 UNINITIALIZED, // The decoder has not initialized. |
| 133 INITIALIZED, // The decoder has initialized. |
| 134 RESETTING, // The decoder is being reset. |
| 135 DECODE_ERROR, // Decoding error happened. |
| 136 }; |
| 137 |
| 138 // The hardware video decoder. |
| 139 scoped_ptr<media::VideoDecodeAccelerator> vda_; |
| 140 |
| 141 // This event is signaled by asynchronous tasks posted to the compositor |
| 142 // message loop to indicate their completion. |
| 143 base::WaitableEvent compositor_loop_async_waiter_; |
| 144 |
| 145 // The size of the incoming video frames. |
| 146 gfx::Size frame_size_; |
| 147 |
| 148 // Protects |state_|, |decode_complete_callback_| , |available_shm_segments_|, |
| 149 // |buffers_to_be_decoded_|, and |buffers_resetting_|. |
| 150 base::Lock lock_; |
| 151 |
| 152 // The state of RTCVideoDecoder. Guarded by |lock_|. |
| 153 State state_; |
| 154 |
| 155 // Guarded by |lock_|. |
| 156 webrtc::DecodedImageCallback* decode_complete_callback_; |
| 157 |
| 158 // Shared-memory buffer pool. Since allocating SHM segments requires a |
| 159 // round-trip to the browser process, we keep allocation out of the |
| 160 // steady-state of the decoder. Guarded by |lock_|. |
| 161 std::vector<SHMBuffer*> available_shm_segments_; |
| 162 |
| 163 // The weak pointer should live and die on the |vda_loop_proxy_|; |
| 164 base::WeakPtrFactory<RTCVideoDecoder> weak_factory_; |
| 165 base::WeakPtr<RTCVideoDecoder> weak_this_; |
| 166 |
| 167 scoped_refptr<media::GpuVideoDecoder::Factories> factories_; |
| 168 |
| 169 // The message loop to run callbacks on. This is should be the same as the one |
| 170 // of |factories_|. |
| 171 scoped_refptr<base::MessageLoopProxy> vda_loop_proxy_; |
| 172 |
| 173 // The texture target used for decoded pictures. |
| 174 uint32 decoder_texture_target_; |
| 175 |
| 176 // Metadata of the buffers that have been sent for decode. |
| 177 std::list<BufferData> input_buffer_data_; |
| 178 |
| 179 // A queue storing buffers (and their metadata) that will be sent to VDA for |
| 180 // decode. Guarded by |lock_|. |
| 181 std::deque<std::pair<SHMBuffer*, BufferData> > buffers_to_be_decoded_; |
| 182 |
| 183 // A queue storing buffers (and their metadata) that arrives when VDA is |
| 184 // resetting. They will be sent for decoding after reset is done. Guarded by |
| 185 // |lock_|. |
| 186 std::deque<std::pair<SHMBuffer*, BufferData> > buffers_delayed_; |
| 187 |
| 188 // A map from bitstream buffer IDs to bitstream buffers that are being |
| 189 // processed by VDA. |
| 190 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_; |
| 191 |
| 192 // A map from picture buffer IDs to texture-backed picture buffers. |
| 193 std::map<int32, media::PictureBuffer> assigned_picture_buffers_; |
| 194 |
| 195 // Picture buffers that are dismissed but not deleted yet. |
| 196 std::map<int32, media::PictureBuffer> dismissed_picture_buffers_; |
| 197 |
| 198 // PictureBuffers given to us by VDA via PictureReady, which we sent forward |
| 199 // as VideoFrames to be rendered via read_cb_, and which will be returned |
| 200 // to us via ReusePictureBuffer. |
| 201 std::set<int32> picture_buffers_at_display_; |
| 202 |
| 203 int32 next_picture_buffer_id_; |
| 204 int32 next_bitstream_buffer_id_; |
| 205 |
| 206 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder); |
| 207 }; |
| 208 |
| 209 } // namespace content |
| 210 |
| 211 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_ |
OLD | NEW |