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