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 <utility> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "content/common/content_export.h" |
| 17 #include "media/base/bitstream_buffer.h" |
| 18 #include "media/base/video_decoder.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_|, which is a new thread. VDA::Client methods run on |
| 37 // |vda_message_loop_|. webrtc::VideoDecoder methods run on WebRTC |
| 38 // DecodingThread or Chrome_libJingle_WorkerThread, which are trampolined to |
| 39 // |vda_message_loop_|. Decode() is non-blocking and queues the buffers. |
| 40 // Decoded frames are delivered on |vda_message_loop_|. |
| 41 class CONTENT_EXPORT RTCVideoDecoder |
| 42 : NON_EXPORTED_BASE(public webrtc::VideoDecoder), |
| 43 public media::VideoDecodeAccelerator::Client, |
| 44 public base::MessageLoop::DestructionObserver { |
| 45 public: |
| 46 RTCVideoDecoder( |
| 47 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories, |
| 48 base::Thread* vda_thread); |
| 49 virtual ~RTCVideoDecoder(); |
| 50 |
| 51 // Initializes VDA. True if successful. |
| 52 virtual bool InitVideoDecodeAccelerator(webrtc::VideoCodecType); |
| 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. |
| 69 virtual int32_t Release() OVERRIDE; |
| 70 // Called on Chrome_libJingle_WorkerThread. |
| 71 virtual int32_t Reset() OVERRIDE; |
| 72 |
| 73 // VideoDecodeAccelerator::Client implementation. |
| 74 virtual void NotifyInitializeDone() OVERRIDE; |
| 75 virtual void ProvidePictureBuffers(uint32 count, |
| 76 const gfx::Size& size, |
| 77 uint32 texture_target) OVERRIDE; |
| 78 virtual void DismissPictureBuffer(int32 id) OVERRIDE; |
| 79 virtual void PictureReady(const media::Picture& picture) OVERRIDE; |
| 80 virtual void NotifyEndOfBitstreamBuffer(int32 id) OVERRIDE; |
| 81 virtual void NotifyFlushDone() OVERRIDE; |
| 82 virtual void NotifyResetDone() OVERRIDE; |
| 83 virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE; |
| 84 |
| 85 // base::DestructionObserver implementation. Called when |vda_message_loop_| |
| 86 // is stopped. |
| 87 virtual void WillDestroyCurrentMessageLoop(); |
| 88 |
| 89 private: |
| 90 struct SHMBuffer; |
| 91 struct BufferData; |
| 92 |
| 93 void InitWeakPtr(); |
| 94 |
| 95 // Requests a buffer to be decoded. |
| 96 void RequestBufferDecode(); |
| 97 |
| 98 bool CanMoreDecodeWorkBeDone(); |
| 99 |
| 100 // Resets VDA. |
| 101 void ResetInternal(); |
| 102 |
| 103 // Destroys VDA and the textures. |
| 104 void ReleaseInternal(); |
| 105 |
| 106 // Tells VDA that a picture buffer can be recycled. |
| 107 void ReusePictureBuffer(int64 picture_buffer_id); |
| 108 |
| 109 // Waits for |decoder_waiter_| or |aborted_waiter_| to be signaled. |
| 110 void Wait(); |
| 111 |
| 112 void DestroyTextures(); |
| 113 void DestroyVDA(); |
| 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 data to |input_buffer_data_|. |
| 123 void RecordBufferData(const BufferData& buffer_data); |
| 124 // Gets the buffer data and removes it from |input_buffer_data_|. |
| 125 void GetBufferData(int32 bitstream_buffer_id, uint32_t* timestamp, |
| 126 uint32_t* width, uint32_t* height, size_t *size); |
| 127 |
| 128 enum State { |
| 129 kUninitialized, // The decoder has not initialized. |
| 130 kInitialized, // The decoder has initialized. |
| 131 kDecodeError, // Decoding error happened. |
| 132 }; |
| 133 |
| 134 // The hardware video decoder. |
| 135 scoped_ptr<media::VideoDecodeAccelerator> vda_; |
| 136 |
| 137 // Used to wait for VDA calls to complete in InitDecode(), Release() and |
| 138 // Reset(). |
| 139 base::WaitableEvent decoder_waiter_; |
| 140 |
| 141 // An event signaled when the thread running |vda_loop_proxy_| is stopped. |
| 142 base::WaitableEvent aborted_waiter_; |
| 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 // and |buffers_to_be_decoded_|; |
| 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 // The data of the buffers that are in VDA. |
| 176 std::list<BufferData> input_buffer_data_; |
| 177 |
| 178 // A queue storing buffers (and their data) 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 map from bitstream buffer IDs to bitstream buffers that are being |
| 183 // processed by VDA. |
| 184 std::map<int32, SHMBuffer*> bitstream_buffers_in_decoder_; |
| 185 |
| 186 // A map from picture buffer IDs to texture-backed picture buffers. |
| 187 std::map<int32, media::PictureBuffer> picture_buffers_in_decoder_; |
| 188 |
| 189 int32 next_picture_buffer_id_; |
| 190 int32 next_bitstream_buffer_id_; |
| 191 |
| 192 DISALLOW_COPY_AND_ASSIGN(RTCVideoDecoder); |
| 193 }; |
| 194 |
| 195 } // namespace content |
| 196 |
| 197 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_DECODER_H_ |
OLD | NEW |