Chromium Code Reviews| Index: content/renderer/media/rtc_video_decoder.cc |
| diff --git a/content/renderer/media/rtc_video_decoder.cc b/content/renderer/media/rtc_video_decoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..14a45bdcca34be1268f26704443cf5d9051fc15c |
| --- /dev/null |
| +++ b/content/renderer/media/rtc_video_decoder.cc |
| @@ -0,0 +1,717 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/media/rtc_video_decoder.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "base/safe_numerics.h" |
| +#include "base/stl_util.h" |
| +#include "base/task_runner_util.h" |
| +#include "content/child/child_thread.h" |
| +#include "content/renderer/media/native_handle_impl.h" |
| +#include "media/base/bind_to_loop.h" |
| +#include "third_party/webrtc/common_video/interface/texture_video_frame.h" |
| +#include "third_party/webrtc/system_wrappers/interface/ref_count.h" |
| + |
| +namespace content { |
| + |
| +static const int32 ID_LAST = 0x3FFFFFFF; // maximum bitstream buffer id |
| +static const int32 ID_HALF = 0x20000000; |
| + |
| +// Maximum number of concurrent VDA::Decode() operations RVD will maintain. |
| +// Higher values allow better pipelining in the GPU, but also require more |
| +// resources. |
| +static const size_t kMaxInFlightDecodes = 8; |
| + |
| +// Size of shared-memory segments we allocate. Since we reuse them we let them |
| +// be on the beefy side. |
| +static const size_t kSharedMemorySegmentBytes = 100 << 10; |
| + |
| +// Maximum number of allocated shared-memory segments. |
| +static const int kMaxNumSharedMemorySegments = 16; |
| + |
| +// Maximum number of pending WebRTC buffers that are waiting for the shared |
| +// memory. 10 seconds for 30 fps. |
| +static const size_t kMaxNumOfPendingBuffers = 300; |
| + |
| +// A shared memory segment and its allocated size. This class has the ownership |
| +// of |shm|. |
| +class RTCVideoDecoder::SHMBuffer { |
| + public: |
| + SHMBuffer(base::SharedMemory* shm, size_t size); |
| + ~SHMBuffer(); |
| + base::SharedMemory* const shm; |
| + const size_t size; |
| +}; |
| + |
| +RTCVideoDecoder::SHMBuffer::SHMBuffer(base::SharedMemory* shm, size_t size) |
| + : shm(shm), size(size) {} |
| + |
| +RTCVideoDecoder::SHMBuffer::~SHMBuffer() { shm->Close(); } |
| + |
| +// Metadata of a bitstream buffer. |
| +struct RTCVideoDecoder::BufferData { |
| + BufferData(int32 bitstream_buffer_id, |
| + uint32_t timestamp, |
| + int width, |
| + int height, |
| + size_t size); |
| + ~BufferData(); |
| + int32 bitstream_buffer_id; |
| + uint32_t timestamp; // in 90KHz |
| + uint32_t width; |
| + uint32_t height; |
| + size_t size; // buffer size |
| +}; |
| + |
| +RTCVideoDecoder::BufferData::BufferData(int32 bitstream_buffer_id, |
| + uint32_t timestamp, |
| + int width, |
| + int height, |
| + size_t size) |
| + : bitstream_buffer_id(bitstream_buffer_id), |
| + timestamp(timestamp), |
| + width(width), |
| + height(height), |
| + size(size) {} |
| + |
| +RTCVideoDecoder::BufferData::~BufferData() {} |
| + |
| +RTCVideoDecoder::RTCVideoDecoder( |
| + const scoped_refptr<media::GpuVideoDecoder::Factories>& factories) |
| + : weak_factory_(this), |
| + factories_(factories), |
| + vda_loop_proxy_(factories_->GetMessageLoop()), |
| + decoder_texture_target_(0), |
| + next_picture_buffer_id_(0), |
| + state_(UNINITIALIZED), |
| + decode_complete_callback_(NULL), |
| + num_shm_buffers_(0), |
| + next_bitstream_buffer_id_(0), |
| + reset_bitstream_buffer_id_(0) { |
| + // Initialize directly if |vda_loop_proxy_| is the renderer thread. |
| + base::WaitableEvent compositor_loop_async_waiter(false, false); |
| + if (vda_loop_proxy_->BelongsToCurrentThread()) { |
| + Initialize(&compositor_loop_async_waiter); |
| + return; |
| + } |
| + // Post the task if |vda_loop_proxy_| is the compositor thread. Waiting here |
| + // is safe because the compositor thread will not be stopped until the |
| + // renderer thread shuts down. |
| + vda_loop_proxy_->PostTask(FROM_HERE, |
| + base::Bind(&RTCVideoDecoder::Initialize, |
| + base::Unretained(this), |
| + &compositor_loop_async_waiter)); |
| + compositor_loop_async_waiter.Wait(); |
| +} |
| + |
| +RTCVideoDecoder::~RTCVideoDecoder() { |
| + DVLOG(2) << "~RTCVideoDecoder"; |
| + // Delete vda and remove |this| from the observer if vda thread is alive. |
| + if (vda_loop_proxy_->BelongsToCurrentThread()) { |
| + base::MessageLoop::current()->RemoveDestructionObserver(this); |
| + DestroyVDA(); |
| + } else { |
| + // VDA should have been destroyed in WillDestroyCurrentMessageLoop. |
| + DCHECK(!vda_); |
| + } |
| + |
| + // Delete all shared memories. |
| + STLDeleteElements(&available_shm_segments_); |
| + STLDeleteValues(&bitstream_buffers_in_decoder_); |
| + STLDeleteContainerPairFirstPointers(buffers_to_be_decoded_.begin(), |
| + buffers_to_be_decoded_.end()); |
| + buffers_to_be_decoded_.clear(); |
| + |
| + // Delete WebRTC input buffers. |
| + for (std::deque<std::pair<webrtc::EncodedImage, BufferData> >::iterator it = |
| + webrtc_buffers_.begin(); |
| + it != webrtc_buffers_.end(); |
| + ++it) { |
| + delete it->first._buffer; |
| + } |
| +} |
| + |
| +scoped_ptr<RTCVideoDecoder> RTCVideoDecoder::Create( |
| + const scoped_refptr<media::GpuVideoDecoder::Factories>& factories) { |
| + scoped_ptr<RTCVideoDecoder> decoder(new RTCVideoDecoder(factories)); |
| + decoder->vda_.reset(factories->CreateVideoDecodeAccelerator( |
| + media::VP8PROFILE_MAIN, decoder.get())); |
| + // vda can be NULL if VP8 is not supported. |
| + if (decoder->vda_ != NULL) { |
| + decoder->state_ = INITIALIZED; |
| + } else { |
| + factories->GetMessageLoop()->DeleteSoon(FROM_HERE, decoder.release()); |
| + } |
| + return decoder.Pass(); |
| +} |
| + |
| +int32_t RTCVideoDecoder::InitDecode(const webrtc::VideoCodec* codecSettings, |
| + int32_t /*numberOfCores*/) { |
| + DVLOG(2) << "InitDecode"; |
| + DCHECK_EQ(codecSettings->codecType, webrtc::kVideoCodecVP8); |
| + if (codecSettings->codecSpecific.VP8.feedbackModeOn) { |
| + LOG(ERROR) << "Feedback mode not supported"; |
| + return WEBRTC_VIDEO_CODEC_ERROR; |
| + } |
| + |
| + base::AutoLock auto_lock(lock_); |
| + if (state_ == UNINITIALIZED || state_ == DECODE_ERROR) { |
| + LOG(ERROR) << "VDA is not initialized. state=" << state_; |
| + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| + } |
| + // Create a shared memory if the queue is empty. |
| + if (available_shm_segments_.size() == 0) { |
| + content::ChildThread::current()->message_loop() |
| + ->PostTask(FROM_HERE, |
| + base::Bind(&RTCVideoDecoder::CreateSHM, |
| + weak_this_, |
| + kSharedMemorySegmentBytes)); |
| + } |
| + return WEBRTC_VIDEO_CODEC_OK; |
| +} |
| + |
| +int32_t RTCVideoDecoder::Decode( |
| + const webrtc::EncodedImage& inputImage, |
| + bool missingFrames, |
| + const webrtc::RTPFragmentationHeader* /*fragmentation*/, |
| + const webrtc::CodecSpecificInfo* /*codecSpecificInfo*/, |
| + int64_t /*renderTimeMs*/) { |
| + DVLOG(3) << "Decode"; |
| + |
| + int bitstream_buffer_id; |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + if (state_ == UNINITIALIZED || decode_complete_callback_ == NULL) { |
| + LOG(ERROR) << "The decoder has not initialized."; |
| + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| + } |
| + if (state_ == DECODE_ERROR) { |
| + LOG(ERROR) << "Decoding error occurred."; |
| + return WEBRTC_VIDEO_CODEC_ERROR; |
| + } |
| + bitstream_buffer_id = next_bitstream_buffer_id_; |
| + // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. |
| + next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & ID_LAST; |
| + } |
| + if (missingFrames || !inputImage._completeFrame) { |
| + DLOG(ERROR) << "Missing or incomplete frames."; |
| + // Unlike the SW decoder in libvpx, hw decoder cannot handle broken frames. |
| + // Return an error to request a key frame. |
| + return WEBRTC_VIDEO_CODEC_ERROR; |
| + } |
| + if (inputImage._frameType == webrtc::kKeyFrame) |
| + frame_size_.SetSize(inputImage._encodedWidth, inputImage._encodedHeight); |
| + |
| + // Create buffer metadata. |
| + BufferData buffer_data(bitstream_buffer_id, |
| + inputImage._timeStamp, |
| + frame_size_.width(), |
| + frame_size_.height(), |
| + inputImage._length); |
| + |
| + // The buffers should be sent to VDA in order. Send the pending buffers first. |
| + SendPendingBuffersForDecode(); |
| + |
| + // If the shared memory is available and there are no pending buffers, send |
| + // the buffer for decode. If not, save the buffer in the queue for decode |
| + // later. |
| + SHMBuffer* shm_buffer = NULL; |
| + if (webrtc_buffers_.size() == 0) |
| + shm_buffer = GetSHM(inputImage._length); |
| + |
| + if (shm_buffer != NULL) { |
| + SendBufferForDecode(inputImage, shm_buffer, buffer_data); |
| + return WEBRTC_VIDEO_CODEC_OK; |
| + } |
| + |
| + return SaveToPendingBuffers(inputImage, buffer_data); |
| +} |
| + |
| +int32_t RTCVideoDecoder::RegisterDecodeCompleteCallback( |
| + webrtc::DecodedImageCallback* callback) { |
| + DVLOG(2) << "RegisterDecodeCompleteCallback"; |
| + base::AutoLock auto_lock(lock_); |
| + decode_complete_callback_ = callback; |
| + return WEBRTC_VIDEO_CODEC_OK; |
| +} |
| + |
| +int32_t RTCVideoDecoder::Release() { |
| + DVLOG(2) << "Release"; |
| + // Do not destroy VDA because the decoder will be recycled by |
| + // RTCVideoDecoderFactory. Just reset VDA. |
| + return Reset(); |
| +} |
| + |
| +int32_t RTCVideoDecoder::Reset() { |
| + DVLOG(2) << "Reset"; |
| + base::AutoLock auto_lock(lock_); |
| + if (state_ == UNINITIALIZED) { |
| + LOG(ERROR) << "Decoder not initialized."; |
| + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| + } |
| + reset_bitstream_buffer_id_ = next_bitstream_buffer_id_; |
| + // If VDA is already resetting, no need to request the reset again. |
| + if (state_ != RESETTING) { |
| + state_ = RESETTING; |
| + vda_loop_proxy_->PostTask( |
| + FROM_HERE, base::Bind(&RTCVideoDecoder::ResetInternal, weak_this_)); |
| + } |
| + return WEBRTC_VIDEO_CODEC_OK; |
| +} |
| + |
| +void RTCVideoDecoder::NotifyInitializeDone() { |
| + DVLOG(2) << "NotifyInitializeDone"; |
| + NOTREACHED(); |
| +} |
| + |
| +void RTCVideoDecoder::ProvidePictureBuffers(uint32 count, |
| + const gfx::Size& size, |
| + uint32 texture_target) { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + DVLOG(3) << "ProvidePictureBuffers. texture_target=" << texture_target; |
| + |
| + if (!vda_) |
| + return; |
| + |
| + std::vector<uint32> texture_ids; |
| + decoder_texture_target_ = texture_target; |
| + if (!factories_->CreateTextures( |
| + count, size, &texture_ids, decoder_texture_target_)) { |
| + NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
| + return; |
| + } |
| + DCHECK_EQ(count, texture_ids.size()); |
| + |
| + std::vector<media::PictureBuffer> picture_buffers; |
| + for (size_t i = 0; i < texture_ids.size(); ++i) { |
| + picture_buffers.push_back( |
| + media::PictureBuffer(next_picture_buffer_id_++, size, texture_ids[i])); |
| + bool inserted = assigned_picture_buffers_.insert(std::make_pair( |
| + picture_buffers.back().id(), picture_buffers.back())).second; |
| + DCHECK(inserted); |
| + } |
| + vda_->AssignPictureBuffers(picture_buffers); |
| +} |
| + |
| +void RTCVideoDecoder::DismissPictureBuffer(int32 id) { |
| + DVLOG(3) << "DismissPictureBuffer. id=" << id; |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + |
| + std::map<int32, media::PictureBuffer>::iterator it = |
| + assigned_picture_buffers_.find(id); |
| + if (it == assigned_picture_buffers_.end()) { |
| + NOTREACHED() << "Missing picture buffer: " << id; |
| + return; |
| + } |
| + |
| + media::PictureBuffer buffer_to_dismiss = it->second; |
| + assigned_picture_buffers_.erase(it); |
| + |
| + std::set<int32>::iterator at_display_it = |
| + picture_buffers_at_display_.find(id); |
| + |
| + if (at_display_it == picture_buffers_at_display_.end()) { |
| + // We can delete the texture immediately as it's not being displayed. |
| + factories_->DeleteTexture(buffer_to_dismiss.texture_id()); |
| + } else { |
| + // Texture in display. Postpone deletion until after it's returned to us. |
| + bool inserted = dismissed_picture_buffers_ |
| + .insert(std::make_pair(id, buffer_to_dismiss)).second; |
| + DCHECK(inserted); |
| + } |
| +} |
| + |
| +void RTCVideoDecoder::PictureReady(const media::Picture& picture) { |
| + DVLOG(3) << "PictureReady"; |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + |
| + std::map<int32, media::PictureBuffer>::iterator it = |
| + assigned_picture_buffers_.find(picture.picture_buffer_id()); |
| + if (it == assigned_picture_buffers_.end()) { |
| + NOTREACHED() << "Missing picture buffer: " << picture.picture_buffer_id(); |
| + NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
| + return; |
| + } |
| + const media::PictureBuffer& pb = it->second; |
| + |
| + // Create a media::VideoFrame. |
| + uint32_t timestamp = 0, width = 0, height = 0; |
| + size_t size = 0; |
| + GetBufferData( |
| + picture.bitstream_buffer_id(), ×tamp, &width, &height, &size); |
| + scoped_refptr<media::VideoFrame> frame = |
| + CreateVideoFrame(picture, pb, timestamp, width, height, size); |
| + bool inserted = |
| + picture_buffers_at_display_.insert(picture.picture_buffer_id()).second; |
| + DCHECK(inserted); |
| + |
| + // Create a WebRTC video frame. |
| + webrtc::RefCountImpl<NativeHandleImpl>* handle = |
| + new webrtc::RefCountImpl<NativeHandleImpl>(); |
| + handle->SetHandle(frame.get()); |
| + webrtc::TextureVideoFrame decoded_image(width, height, timestamp, 0, handle); |
|
wuchengli
2013/06/28 15:08:45
malloc has been removed. I'll update webrtc CL soo
|
| + |
| + // Invoke decode callback. WebRTC expects no frame callback after Release. |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + DCHECK(decode_complete_callback_ != NULL); |
| + if (IsBufferAfterReset(picture.bitstream_buffer_id(), |
| + reset_bitstream_buffer_id_)) { |
| + decode_complete_callback_->Decoded(decoded_image); |
| + } |
| + } |
| +} |
| + |
| +scoped_refptr<media::VideoFrame> RTCVideoDecoder::CreateVideoFrame( |
| + const media::Picture& picture, |
| + const media::PictureBuffer& pb, |
| + uint32_t timestamp, |
| + uint32_t width, |
| + uint32_t height, |
| + size_t size) { |
| + gfx::Rect visible_rect(width, height); |
| + gfx::Size natural_size(width, height); |
| + DCHECK(decoder_texture_target_); |
| + // Convert timestamp from 90KHz to ms. |
| + base::TimeDelta timestamp_ms = base::TimeDelta::FromInternalValue( |
| + base::checked_numeric_cast<uint64_t>(timestamp) * 1000 / 90); |
| + return media::VideoFrame::WrapNativeTexture( |
| + pb.texture_id(), |
| + decoder_texture_target_, |
| + pb.size(), |
| + visible_rect, |
| + natural_size, |
| + timestamp_ms, |
| + base::Bind(&media::GpuVideoDecoder::Factories::ReadPixels, |
| + factories_, |
| + pb.texture_id(), |
| + decoder_texture_target_, |
| + natural_size), |
| + media::BindToCurrentLoop(base::Bind(&RTCVideoDecoder::ReusePictureBuffer, |
| + weak_this_, |
| + picture.picture_buffer_id()))); |
| +} |
| + |
| +void RTCVideoDecoder::NotifyEndOfBitstreamBuffer(int32 id) { |
| + DVLOG(3) << "NotifyEndOfBitstreamBuffer. id=" << id; |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + |
| + std::map<int32, SHMBuffer*>::iterator it = |
| + bitstream_buffers_in_decoder_.find(id); |
| + if (it == bitstream_buffers_in_decoder_.end()) { |
| + NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
| + NOTREACHED() << "Missing bitstream buffer: " << id; |
| + return; |
| + } |
| + |
| + PutSHM(it->second); |
| + bitstream_buffers_in_decoder_.erase(it); |
| + |
| + RequestBufferDecode(); |
| +} |
| + |
| +void RTCVideoDecoder::NotifyFlushDone() { |
| + DVLOG(3) << "NotifyFlushDone"; |
| + NOTREACHED() << "Unexpected flush done notification."; |
| +} |
| + |
| +void RTCVideoDecoder::NotifyResetDone() { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + DVLOG(3) << "NotifyResetDone"; |
| + |
| + if (!vda_) |
| + return; |
| + |
| + input_buffer_data_.clear(); |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + state_ = INITIALIZED; |
| + } |
| + // Send the pending buffers for decoding. |
| + RequestBufferDecode(); |
| +} |
| + |
| +void RTCVideoDecoder::NotifyError(media::VideoDecodeAccelerator::Error error) { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + if (!vda_) |
| + return; |
| + |
| + DLOG(ERROR) << "VDA Error:" << error; |
| + DestroyVDA(); |
| + |
| + base::AutoLock auto_lock(lock_); |
| + state_ = DECODE_ERROR; |
| +} |
| + |
| +void RTCVideoDecoder::WillDestroyCurrentMessageLoop() { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + factories_->Abort(); |
| + weak_factory_.InvalidateWeakPtrs(); |
| + DestroyVDA(); |
| +} |
| + |
| +void RTCVideoDecoder::Initialize(base::WaitableEvent* waiter) { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + base::MessageLoop::current()->AddDestructionObserver(this); |
| + weak_this_ = weak_factory_.GetWeakPtr(); |
| + waiter->Signal(); |
| +} |
| + |
| +void RTCVideoDecoder::RequestBufferDecode() { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + if (!vda_) |
| + return; |
| + |
| + while (CanMoreDecodeWorkBeDone()) { |
| + // Get a buffer and data from the queue. |
| + std::pair<SHMBuffer*, BufferData>* buffer_pair; |
| + SHMBuffer* shm_buffer = NULL; |
| + BufferData* buffer_data = NULL; |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + // Do not request decode if VDA is resetting. |
| + if (buffers_to_be_decoded_.size() == 0 || state_ == RESETTING) |
| + return; |
| + buffer_pair = &buffers_to_be_decoded_.front(); |
| + buffers_to_be_decoded_.pop_front(); |
| + shm_buffer = buffer_pair->first; |
| + buffer_data = &buffer_pair->second; |
| + // Drop the buffers before Reset or Release is called. |
| + if (!IsBufferAfterReset(buffer_data->bitstream_buffer_id, |
| + reset_bitstream_buffer_id_)) { |
| + available_shm_segments_.push_back(shm_buffer); |
| + continue; |
| + } |
| + } |
| + |
| + // Create a BitstreamBuffer and send to VDA to decode. |
| + media::BitstreamBuffer bitstream_buffer(buffer_data->bitstream_buffer_id, |
| + shm_buffer->shm->handle(), |
| + buffer_data->size); |
| + bool inserted = bitstream_buffers_in_decoder_ |
| + .insert(std::make_pair(bitstream_buffer.id(), shm_buffer)).second; |
| + DCHECK(inserted); |
| + RecordBufferData(*buffer_data); |
| + vda_->Decode(bitstream_buffer); |
| + } |
| +} |
| + |
| +bool RTCVideoDecoder::CanMoreDecodeWorkBeDone() { |
| + return bitstream_buffers_in_decoder_.size() < kMaxInFlightDecodes; |
| +} |
| + |
| +bool RTCVideoDecoder::IsBufferAfterReset(int32 id_buffer, int32 id_reset) { |
|
Ami GONE FROM CHROMIUM
2013/06/28 17:04:00
If Reset/Release have never been called, id_reset
wuchengli
2013/06/29 05:01:30
Right. Suppose we add sentinel value -1. When id_r
Ami GONE FROM CHROMIUM
2013/06/29 05:23:03
My point was that the question of whether |id_buff
wuchengli
2013/07/02 10:34:25
Done. Added ID_INVALID=-1 as sentinel value.
|
| + int32 diff = id_buffer - id_reset; |
| + if (diff < 0) |
| + diff += ID_LAST + 1; |
| + return diff < ID_HALF; |
|
Ami GONE FROM CHROMIUM
2013/06/28 17:04:00
worth a test for the interesting edge conditions?
wuchengli
2013/07/02 10:34:25
Done.
|
| +} |
| + |
| +void RTCVideoDecoder::SendBufferForDecode( |
| + const webrtc::EncodedImage& input_image, |
| + SHMBuffer* shm_buffer, |
| + const BufferData& buffer_data) { |
| + memcpy(shm_buffer->shm->memory(), input_image._buffer, input_image._length); |
| + std::pair<SHMBuffer*, BufferData> buffer_pair = |
| + std::make_pair(shm_buffer, buffer_data); |
| + |
| + // Store the buffer and the metadata to the queue. |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + buffers_to_be_decoded_.push_back(buffer_pair); |
| + } |
| + vda_loop_proxy_->PostTask( |
| + FROM_HERE, base::Bind(&RTCVideoDecoder::RequestBufferDecode, weak_this_)); |
| +} |
| + |
| +void RTCVideoDecoder::SendPendingBuffersForDecode() { |
| + DVLOG(2) << "SendPendingBuffersForDecode"; |
| + while (webrtc_buffers_.size() > 0) { |
| + // Get a WebRTC buffer from the queue. |
| + const std::pair<webrtc::EncodedImage, BufferData>& buffer_pair = |
| + webrtc_buffers_.front(); |
| + const webrtc::EncodedImage& input_image = buffer_pair.first; |
| + const BufferData& buffer_data = buffer_pair.second; |
| + |
| + // Drop the frame if it comes before Reset or Release. |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + if (!IsBufferAfterReset(buffer_data.bitstream_buffer_id, |
| + reset_bitstream_buffer_id_)) { |
| + webrtc_buffers_.pop_front(); |
| + delete input_image._buffer; |
| + continue; |
| + } |
| + } |
| + |
| + // Get shared memory and send it for decode. |
| + SHMBuffer* shm_buffer = GetSHM(input_image._length); |
| + if (!shm_buffer) |
| + return; |
| + SendBufferForDecode(input_image, shm_buffer, buffer_data); |
| + webrtc_buffers_.pop_front(); |
| + delete input_image._buffer; |
| + } |
| +} |
| + |
| +int RTCVideoDecoder::SaveToPendingBuffers( |
| + const webrtc::EncodedImage& input_image, |
| + const BufferData& buffer_data) { |
| + DVLOG(2) << "SaveToPendingBuffers"; |
| + // Queued too many buffers. Something goes wrong. |
|
Ami GONE FROM CHROMIUM
2013/06/28 17:04:00
This is not a super-satisfying block of code...
IW
wuchengli
2013/06/29 05:01:30
I saw apprtc loopback showed the self image runnin
Ami GONE FROM CHROMIUM
2013/06/29 05:23:03
I'd rather have the bug manifest so it can be trac
wuchengli
2013/07/02 10:34:25
I added error log here. I'm not sure the issue sti
|
| + if (webrtc_buffers_.size() >= kMaxNumOfPendingBuffers) |
| + return WEBRTC_VIDEO_CODEC_ERROR; |
| + |
| + // Clone the input image and save it to the queue. |
| + uint8_t* buffer = static_cast<uint8_t*>(malloc(input_image._length)); |
| + memcpy(buffer, input_image._buffer, input_image._length); |
|
Ami GONE FROM CHROMIUM
2013/06/28 17:04:00
Add a TODO to extend the Decode() interface to tak
wuchengli
2013/07/02 10:34:25
Done.
|
| + webrtc::EncodedImage encoded_image( |
| + buffer, input_image._length, input_image._length); |
| + std::pair<webrtc::EncodedImage, BufferData> buffer_pair = |
| + std::make_pair(encoded_image, buffer_data); |
| + webrtc_buffers_.push_back(buffer_pair); |
| + return WEBRTC_VIDEO_CODEC_OK; |
| +} |
| + |
| +void RTCVideoDecoder::ResetInternal() { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + DVLOG(2) << "ResetInternal"; |
| + if (vda_) |
| + vda_->Reset(); |
| +} |
| + |
| +void RTCVideoDecoder::ReusePictureBuffer(int64 picture_buffer_id) { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + DVLOG(3) << "ReusePictureBuffer. id=" << picture_buffer_id; |
| + |
| + if (!vda_) |
| + return; |
| + |
| + CHECK(!picture_buffers_at_display_.empty()); |
| + |
| + size_t num_erased = picture_buffers_at_display_.erase(picture_buffer_id); |
| + DCHECK(num_erased); |
| + |
| + std::map<int32, media::PictureBuffer>::iterator it = |
| + assigned_picture_buffers_.find(picture_buffer_id); |
| + |
| + if (it == assigned_picture_buffers_.end()) { |
| + // This picture was dismissed while in display, so we postponed deletion. |
| + it = dismissed_picture_buffers_.find(picture_buffer_id); |
| + DCHECK(it != dismissed_picture_buffers_.end()); |
| + factories_->DeleteTexture(it->second.texture_id()); |
| + dismissed_picture_buffers_.erase(it); |
| + return; |
| + } |
| + |
| + vda_->ReusePictureBuffer(picture_buffer_id); |
| +} |
| + |
| +void RTCVideoDecoder::DestroyTextures() { |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + std::map<int32, media::PictureBuffer>::iterator it; |
| + |
| + for (it = assigned_picture_buffers_.begin(); |
| + it != assigned_picture_buffers_.end(); |
| + ++it) { |
| + factories_->DeleteTexture(it->second.texture_id()); |
| + } |
| + assigned_picture_buffers_.clear(); |
| + |
| + for (it = dismissed_picture_buffers_.begin(); |
| + it != dismissed_picture_buffers_.end(); |
| + ++it) { |
| + factories_->DeleteTexture(it->second.texture_id()); |
| + } |
| + dismissed_picture_buffers_.clear(); |
| +} |
| + |
| +void RTCVideoDecoder::DestroyVDA() { |
| + DVLOG(2) << "DestroyVDA"; |
| + DCHECK(vda_loop_proxy_->BelongsToCurrentThread()); |
| + if (vda_) |
| + vda_.release()->Destroy(); |
| + DestroyTextures(); |
| +} |
| + |
| +RTCVideoDecoder::SHMBuffer* RTCVideoDecoder::GetSHM(size_t min_size) { |
| + // Reuse a SHM if possible. |
| + SHMBuffer* ret = NULL; |
| + base::AutoLock auto_lock(lock_); |
| + if (!available_shm_segments_.empty() && |
| + available_shm_segments_.back()->size >= min_size) { |
| + ret = available_shm_segments_.back(); |
| + available_shm_segments_.pop_back(); |
| + } |
| + // Post to the child thread to create shared memory if SHM cannot be reused |
| + // or the queue is almost empty. |
| + if (num_shm_buffers_ < kMaxNumSharedMemorySegments && |
| + (ret == NULL || available_shm_segments_.size() <= 1)) { |
| + content::ChildThread::current()->message_loop()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&RTCVideoDecoder::CreateSHM, weak_this_, min_size)); |
| + } |
| + return ret; |
| +} |
| + |
| +void RTCVideoDecoder::CreateSHM(size_t min_size) { |
| + DCHECK(base::MessageLoop::current() == |
| + content::ChildThread::current()->message_loop()); |
| + DVLOG(2) << "CreateSharedMemory. size=" << min_size; |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + if (num_shm_buffers_ >= kMaxNumSharedMemorySegments) |
| + return; |
| + } |
| + size_t size_to_allocate = std::max(min_size, kSharedMemorySegmentBytes); |
| + base::SharedMemory* shm = factories_->CreateSharedMemory(size_to_allocate); |
| + if (shm != NULL) { |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + num_shm_buffers_++; |
| + } |
| + PutSHM(new SHMBuffer(shm, size_to_allocate)); |
| + } |
| +} |
| + |
| +void RTCVideoDecoder::PutSHM(SHMBuffer* shm_buffer) { |
| + base::AutoLock auto_lock(lock_); |
| + available_shm_segments_.push_back(shm_buffer); |
|
Ami GONE FROM CHROMIUM
2013/06/28 17:04:00
does it make sense to SendPendingBuffersForDecode
wuchengli
2013/06/29 05:01:30
PutSHM can be called by the compositor thread or t
Ami GONE FROM CHROMIUM
2013/06/29 05:23:03
My point is that if there are pending decodes that
wuchengli
2013/07/02 10:34:25
I've changed the code. Now there are two buffer qu
|
| +} |
| + |
| +void RTCVideoDecoder::RecordBufferData(const BufferData& buffer_data) { |
| + input_buffer_data_.push_front(buffer_data); |
| + // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but |
| + // that's too small for some pathological B-frame test videos. The cost of |
| + // using too-high a value is low (192 bits per extra slot). |
| + static const size_t kMaxInputBufferDataSize = 128; |
| + // Pop from the back of the list, because that's the oldest and least likely |
| + // to be useful in the future data. |
| + if (input_buffer_data_.size() > kMaxInputBufferDataSize) |
| + input_buffer_data_.pop_back(); |
| +} |
| + |
| +void RTCVideoDecoder::GetBufferData(int32 bitstream_buffer_id, |
| + uint32_t* timestamp, |
| + uint32_t* width, |
| + uint32_t* height, |
| + size_t* size) { |
| + for (std::list<BufferData>::iterator it = input_buffer_data_.begin(); |
| + it != input_buffer_data_.end(); |
| + ++it) { |
| + if (it->bitstream_buffer_id != bitstream_buffer_id) |
| + continue; |
| + *timestamp = it->timestamp; |
| + *width = it->width; |
| + *height = it->height; |
| + return; |
| + } |
| + NOTREACHED() << "Missing bitstream buffer id: " << bitstream_buffer_id; |
| +} |
| + |
| +} // namespace content |