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