| Index: content/renderer/media/gpu/rtc_video_decoder.cc
|
| diff --git a/content/renderer/media/gpu/rtc_video_decoder.cc b/content/renderer/media/gpu/rtc_video_decoder.cc
|
| index 47aae3eee3646c58c554b1d42920a3190bd869db..a0b4603df87c1965a171aeec6fceaf5cf5fa4617 100644
|
| --- a/content/renderer/media/gpu/rtc_video_decoder.cc
|
| +++ b/content/renderer/media/gpu/rtc_video_decoder.cc
|
| @@ -11,13 +11,13 @@
|
| #include "base/memory/ref_counted.h"
|
| #include "base/metrics/histogram_macros.h"
|
| #include "base/numerics/safe_conversions.h"
|
| +#include "base/stl_util.h"
|
| #include "base/synchronization/waitable_event.h"
|
| #include "base/task_runner_util.h"
|
| #include "content/renderer/media/webrtc/webrtc_video_frame_adapter.h"
|
| -#include "gpu/command_buffer/common/mailbox_holder.h"
|
| -#include "media/base/bind_to_current_loop.h"
|
| -#include "media/renderers/gpu_video_accelerator_factories.h"
|
| -#include "third_party/skia/include/core/SkBitmap.h"
|
| +#include "media/base/decoder_buffer.h"
|
| +#include "media/base/encryption_scheme.h"
|
| +#include "media/base/video_decoder.h"
|
| #include "third_party/webrtc/base/bind.h"
|
| #include "third_party/webrtc/base/refcount.h"
|
| #include "third_party/webrtc/modules/video_coding/codecs/h264/include/h264.h"
|
| @@ -30,69 +30,127 @@
|
| #endif // defined(OS_WIN)
|
|
|
| namespace content {
|
| +namespace {
|
|
|
| -const int32_t RTCVideoDecoder::ID_LAST = 0x3FFFFFFF;
|
| -const int32_t RTCVideoDecoder::ID_HALF = 0x20000000;
|
| -const int32_t RTCVideoDecoder::ID_INVALID = -1;
|
| +const int32_t ID_LAST = 0x3FFFFFFF;
|
| +const int32_t ID_HALF = 0x20000000;
|
| +const int32_t ID_INVALID = -1;
|
|
|
| -// Number of consecutive frames that can be lost due to a VDA error before
|
| +// Number of consecutive frames that can be lost due to decoder errors before
|
| // falling back to SW implementation.
|
| -const uint32_t kNumVDAErrorsBeforeSWFallback = 5;
|
| -
|
| -// 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;
|
| -
|
| -// Number of allocated shared memory segments.
|
| -static const size_t kNumSharedMemorySegments = 16;
|
| +const uint32_t kNumDecoderErrorsBeforeSWFallback = 5;
|
| +
|
| +// Maximum number of pending buffers that are waiting to be sent to the remote
|
| +// decoder.
|
| +const size_t kMaxNumOfPendingBuffers = 8;
|
| +
|
| +// These values will be used to initialize the decoder before we know what the
|
| +// size of the video will be. These were chosen to match the default values in
|
| +// media/video/video_decode_accelerator.h.
|
| +const int kDefaultVideoWidth = 320;
|
| +const int kDefaultVideoHeight = 240;
|
| +
|
| +// Copy the data from |encoded_image|, creating a new media::DecoderBuffer.
|
| +scoped_refptr<media::DecoderBuffer> CreateDecoderBuffer(
|
| + const webrtc::EncodedImage& encoded_image) {
|
| + auto decoder_buffer = media::DecoderBuffer::CopyFrom(encoded_image._buffer,
|
| + encoded_image._length);
|
| + decoder_buffer->set_timestamp(
|
| + base::TimeDelta::FromInternalValue(encoded_image._timeStamp));
|
| + return decoder_buffer;
|
| +}
|
| +
|
| +// Return a media::VideoDecoderConfig for the given WebRTC |codec_type| and
|
| +// |size|. This object will be used to initialize the remote video decoder. If
|
| +// |codec_type| is not supported, returns an invalid config object.
|
| +media::VideoDecoderConfig GetVideoDecoderConfigForCodec(
|
| + webrtc::VideoCodecType codec_type,
|
| + const gfx::Size& size) {
|
| + media::VideoCodecProfile profile;
|
| + media::VideoCodec codec;
|
| + switch (codec_type) {
|
| + case webrtc::kVideoCodecVP8:
|
| + profile = media::VP8PROFILE_ANY;
|
| + codec = media::kCodecVP8;
|
| + break;
|
| + case webrtc::kVideoCodecH264:
|
| + profile = media::H264PROFILE_MAIN;
|
| + codec = media::kCodecH264;
|
| + break;
|
| + case webrtc::kVideoCodecVP9:
|
| + profile = media::VP9PROFILE_PROFILE0;
|
| + codec = media::kCodecVP9;
|
| + break;
|
| + default:
|
| + return media::VideoDecoderConfig();
|
| + }
|
|
|
| -// Maximum number of pending WebRTC buffers that are waiting for shared memory.
|
| -static const size_t kMaxNumOfPendingBuffers = 8;
|
| + return media::VideoDecoderConfig(
|
| + codec, profile, media::PIXEL_FORMAT_ARGB, /* pixel_format */
|
| + media::COLOR_SPACE_UNSPECIFIED, /* color_space */
|
| + size, /* coded_size */
|
| + gfx::Rect(size), /* visible_rect */
|
| + size, /* natural_size */
|
| + std::vector<uint8_t>(), /* extra_data */
|
| + media::EncryptionScheme());
|
| +}
|
|
|
| -RTCVideoDecoder::BufferData::BufferData(int32_t bitstream_buffer_id,
|
| - uint32_t timestamp,
|
| - size_t size,
|
| - const gfx::Rect& visible_rect)
|
| - : bitstream_buffer_id(bitstream_buffer_id),
|
| - timestamp(timestamp),
|
| - size(size),
|
| - visible_rect(visible_rect) {}
|
| +bool IsBufferAfterReset(int32_t id_buffer, int32_t id_reset) {
|
| + DVLOG(2) << __func__;
|
| + if (id_reset == ID_INVALID)
|
| + return true;
|
| + int32_t diff = id_buffer - id_reset;
|
| + if (diff <= 0)
|
| + diff += ID_LAST + 1;
|
| + return diff < ID_HALF;
|
| +}
|
|
|
| -RTCVideoDecoder::BufferData::BufferData() {}
|
| +bool IsFirstBufferAfterReset(int32_t id_buffer, int32_t id_reset) {
|
| + DVLOG(2) << __func__;
|
| + if (id_reset == ID_INVALID)
|
| + return id_buffer == 0;
|
| + return id_buffer == ((id_reset + 1) & ID_LAST);
|
| +}
|
|
|
| -RTCVideoDecoder::BufferData::~BufferData() {}
|
| +} // namespace
|
|
|
| -RTCVideoDecoder::RTCVideoDecoder(webrtc::VideoCodecType type,
|
| - media::GpuVideoAcceleratorFactories* factories)
|
| - : vda_error_counter_(0),
|
| +RTCVideoDecoder::RTCVideoDecoder(
|
| + webrtc::VideoCodecType type,
|
| + const CreateVideoDecoderCB& create_video_decoder_cb,
|
| + const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner)
|
| + : decoder_error_counter_(0),
|
| video_codec_type_(type),
|
| - factories_(factories),
|
| - decoder_texture_target_(0),
|
| - pixel_format_(media::PIXEL_FORMAT_UNKNOWN),
|
| - next_picture_buffer_id_(0),
|
| + create_video_decoder_cb_(create_video_decoder_cb),
|
| + decoder_task_runner_(decoder_task_runner),
|
| state_(UNINITIALIZED),
|
| decode_complete_callback_(nullptr),
|
| - num_shm_buffers_(0),
|
| - next_bitstream_buffer_id_(0),
|
| - reset_bitstream_buffer_id_(ID_INVALID),
|
| + next_decoder_buffer_id_(0),
|
| + reset_decoder_buffer_id_(ID_INVALID),
|
| weak_factory_(this) {
|
| - DCHECK(!factories_->GetTaskRunner()->BelongsToCurrentThread());
|
| + DCHECK(!decoder_task_runner_->BelongsToCurrentThread());
|
| +
|
| + // This object can be created on any thread, so detach now.
|
| + webrtc_decoding_thread_checker_.DetachFromThread();
|
| }
|
|
|
| RTCVideoDecoder::~RTCVideoDecoder() {
|
| - DVLOG(2) << "~RTCVideoDecoder";
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - DestroyVDA();
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
|
|
| - // Delete all shared memories.
|
| - ClearPendingBuffers();
|
| + base::AutoLock auto_lock(lock_);
|
| + ClearPendingBuffers_Locked();
|
| }
|
|
|
| // static
|
| std::unique_ptr<RTCVideoDecoder> RTCVideoDecoder::Create(
|
| webrtc::VideoCodecType type,
|
| - media::GpuVideoAcceleratorFactories* factories) {
|
| + const CreateVideoDecoderCB& create_video_decoder_cb,
|
| + const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner) {
|
| + // This method posts a task to |decoder_task_runner| and blocks until it
|
| + // returns, so this method must not be called on the decoder thread to prevent
|
| + // deadlock.
|
| + DCHECK(!decoder_task_runner->BelongsToCurrentThread());
|
| +
|
| std::unique_ptr<RTCVideoDecoder> decoder;
|
| // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5717.
|
| #if defined(OS_WIN)
|
| @@ -104,47 +162,54 @@ std::unique_ptr<RTCVideoDecoder> RTCVideoDecoder::Create(
|
| return decoder;
|
| }
|
| #endif // defined(OS_WIN)
|
| - // Convert WebRTC codec type to media codec profile.
|
| - media::VideoCodecProfile profile;
|
| - switch (type) {
|
| - case webrtc::kVideoCodecVP8:
|
| - profile = media::VP8PROFILE_ANY;
|
| - break;
|
| - case webrtc::kVideoCodecH264:
|
| - profile = media::H264PROFILE_MAIN;
|
| - break;
|
| - default:
|
| - DVLOG(2) << "Video codec not supported:" << type;
|
| - return decoder;
|
| +
|
| + // Convert WebRTC codec type to media decoder config and initialize the
|
| + // decoder. Since we cannot query the remote decoder's capabilities to
|
| + // determine whether |type| is supported, we need to initialize the decoder
|
| + // with an arbitrary size.
|
| + // TODO(slan): Remove this call when it is possible to synchronously query the
|
| + // media::VideoDecoder's capabilities (crbug.com/658349)
|
| + const media::VideoDecoderConfig config = GetVideoDecoderConfigForCodec(
|
| + type, gfx::Size(kDefaultVideoWidth, kDefaultVideoHeight));
|
| + if (!config.IsValidConfig()) {
|
| + LOG(ERROR) << "Video codec not supported:" << type;
|
| + return decoder;
|
| + }
|
| +
|
| + decoder.reset(
|
| + new RTCVideoDecoder(type, create_video_decoder_cb, decoder_task_runner));
|
| +
|
| + // Block this thread until |decoder_| has been initialized.
|
| + base::WaitableEvent decoder_initialized(
|
| + base::WaitableEvent::ResetPolicy::MANUAL,
|
| + base::WaitableEvent::InitialState::NOT_SIGNALED);
|
| + decoder_task_runner->PostTask(FROM_HERE,
|
| + base::Bind(&RTCVideoDecoder::InitializeDecoder,
|
| + base::Unretained(decoder.get()),
|
| + &decoder_initialized, config));
|
| + decoder_initialized.Wait();
|
| + DCHECK(decoder->state_ == UNINITIALIZED || decoder->state_ == INITIALIZED);
|
| + if (decoder->state_ == UNINITIALIZED) {
|
| + LOG(ERROR) << "Decoder did not initialize properly!";
|
| + decoder_task_runner->DeleteSoon(FROM_HERE, decoder.release());
|
| }
|
|
|
| - base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::MANUAL,
|
| - base::WaitableEvent::InitialState::NOT_SIGNALED);
|
| - decoder.reset(new RTCVideoDecoder(type, factories));
|
| - decoder->factories_->GetTaskRunner()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(&RTCVideoDecoder::CreateVDA,
|
| - base::Unretained(decoder.get()),
|
| - profile,
|
| - &waiter));
|
| - waiter.Wait();
|
| - // |decoder->vda_| is nullptr if the codec is not supported.
|
| - if (decoder->vda_)
|
| - decoder->state_ = INITIALIZED;
|
| - else
|
| - factories->GetTaskRunner()->DeleteSoon(FROM_HERE, decoder.release());
|
| return decoder;
|
| }
|
|
|
| // static
|
| -void RTCVideoDecoder::Destroy(webrtc::VideoDecoder* decoder,
|
| - media::GpuVideoAcceleratorFactories* factories) {
|
| - factories->GetTaskRunner()->DeleteSoon(FROM_HERE, decoder);
|
| +void RTCVideoDecoder::Destroy(
|
| + webrtc::VideoDecoder* decoder,
|
| + const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner) {
|
| + CHECK(decoder_task_runner->DeleteSoon(FROM_HERE, decoder));
|
| }
|
|
|
| int32_t RTCVideoDecoder::InitDecode(const webrtc::VideoCodec* codecSettings,
|
| int32_t /*numberOfCores*/) {
|
| - DVLOG(2) << "InitDecode";
|
| + DVLOG(2) << __func__;
|
| + DCHECK(!decoder_task_runner_->BelongsToCurrentThread());
|
| + DCHECK(webrtc_decoding_thread_checker_.CalledOnValidThread());
|
| +
|
| DCHECK_EQ(video_codec_type_, codecSettings->codecType);
|
| if (codecSettings->codecType == webrtc::kVideoCodecVP8 &&
|
| codecSettings->codecSpecific.VP8.feedbackModeOn) {
|
| @@ -152,12 +217,37 @@ int32_t RTCVideoDecoder::InitDecode(const webrtc::VideoCodec* codecSettings,
|
| return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_ERROR);
|
| }
|
|
|
| + {
|
| + base::AutoLock auto_lock(lock_);
|
| + if (state_ == DECODE_ERROR) {
|
| + LOG(ERROR) << "Decoder is in a bad state: " << state_;
|
| + return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_ERROR);
|
| + } else if (state_ == RESETTING) {
|
| + LOG(ERROR) << "A Reset call is pending. OK.";
|
| + return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_OK);
|
| + }
|
| + }
|
| +
|
| + // Re-initialize the decoder with the proper frame size, even if the decoder
|
| + // has already been initialized.
|
| + const media::VideoDecoderConfig config = GetVideoDecoderConfigForCodec(
|
| + codecSettings->codecType,
|
| + gfx::Size(codecSettings->width, codecSettings->height));
|
| + DCHECK(config.IsValidConfig());
|
| + base::WaitableEvent decoder_initialized(
|
| + base::WaitableEvent::ResetPolicy::MANUAL,
|
| + base::WaitableEvent::InitialState::NOT_SIGNALED);
|
| + decoder_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&RTCVideoDecoder::InitializeDecoder, base::Unretained(this),
|
| + &decoder_initialized, config));
|
| + decoder_initialized.Wait();
|
| +
|
| base::AutoLock auto_lock(lock_);
|
| - if (state_ == UNINITIALIZED || state_ == DECODE_ERROR) {
|
| - LOG(ERROR) << "VDA is not initialized. state=" << state_;
|
| + if (state_ == UNINITIALIZED) {
|
| + LOG(ERROR) << "Decoder failed to reinitialize correctly!";
|
| return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_UNINITIALIZED);
|
| }
|
| -
|
| return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_OK);
|
| }
|
|
|
| @@ -167,24 +257,27 @@ int32_t RTCVideoDecoder::Decode(
|
| const webrtc::RTPFragmentationHeader* /*fragmentation*/,
|
| const webrtc::CodecSpecificInfo* /*codecSpecificInfo*/,
|
| int64_t /*renderTimeMs*/) {
|
| - DVLOG(3) << "Decode";
|
| + DVLOG(2) << __func__;
|
| + DCHECK(!decoder_task_runner_->BelongsToCurrentThread());
|
| + DCHECK(webrtc_decoding_thread_checker_.CalledOnValidThread());
|
|
|
| base::AutoLock auto_lock(lock_);
|
|
|
| - if (state_ == UNINITIALIZED || !decode_complete_callback_) {
|
| + if (!decode_complete_callback_) {
|
| LOG(ERROR) << "The decoder has not initialized.";
|
| return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
|
| }
|
|
|
| if (state_ == DECODE_ERROR) {
|
| LOG(ERROR) << "Decoding error occurred.";
|
| - // Try reseting the session up to |kNumVDAErrorsHandled| times.
|
| + // Try reseting the session up to |kNumDecoderErrorsHandled| times.
|
| // Check if SW H264 implementation is available before falling back.
|
| - if (vda_error_counter_ > kNumVDAErrorsBeforeSWFallback &&
|
| + if (decoder_error_counter_ > kNumDecoderErrorsBeforeSWFallback &&
|
| (video_codec_type_ != webrtc::kVideoCodecH264 ||
|
| webrtc::H264Decoder::IsSupported())) {
|
| - DLOG(ERROR) << vda_error_counter_
|
| - << " errors reported by VDA, falling back to software decode";
|
| + DLOG(ERROR)
|
| + << decoder_error_counter_
|
| + << " errors reported by decoder, falling back to software decode";
|
| return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
|
| }
|
| base::AutoUnlock auto_unlock(lock_);
|
| @@ -193,7 +286,7 @@ int32_t RTCVideoDecoder::Decode(
|
| }
|
|
|
| if (missingFrames || !inputImage._completeFrame) {
|
| - DLOG(ERROR) << "Missing or incomplete frames.";
|
| + LOG(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;
|
| @@ -204,673 +297,290 @@ int32_t RTCVideoDecoder::Decode(
|
| // change gracefully need to have their clients cover for them, and we do that
|
| // here.
|
| #ifdef ANDROID
|
| - const bool kVDACanHandleMidstreamResize = false;
|
| + const bool kDecoderCanHandleMidstreamResize = false;
|
| #else
|
| - const bool kVDACanHandleMidstreamResize = true;
|
| + const bool kDecoderCanHandleMidstreamResize = true;
|
| #endif
|
|
|
| bool need_to_reset_for_midstream_resize = false;
|
| if (inputImage._frameType == webrtc::kVideoFrameKey) {
|
| const gfx::Size new_frame_size(inputImage._encodedWidth,
|
| inputImage._encodedHeight);
|
| - DVLOG(2) << "Got key frame. size=" << new_frame_size.ToString();
|
| + DVLOG(1) << "Got key frame. size=" << new_frame_size.ToString();
|
|
|
| - if (new_frame_size.width() > max_resolution_.width() ||
|
| - new_frame_size.width() < min_resolution_.width() ||
|
| - new_frame_size.height() > max_resolution_.height() ||
|
| - new_frame_size.height() < min_resolution_.height()) {
|
| - DVLOG(1) << "Resolution unsupported, falling back to software decode";
|
| - return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
|
| - }
|
| + // TODO(slan): Query beckend for capabilities and fallback to software
|
| + // immediately if the resolution is out of bounds. Currently, we'll have to
|
| + // wait until the decoder calls back with a DECODE_ERROR to tell us this.
|
|
|
| gfx::Size prev_frame_size = frame_size_;
|
| frame_size_ = new_frame_size;
|
| - if (!kVDACanHandleMidstreamResize && !prev_frame_size.IsEmpty() &&
|
| + if (!kDecoderCanHandleMidstreamResize && !prev_frame_size.IsEmpty() &&
|
| prev_frame_size != frame_size_) {
|
| need_to_reset_for_midstream_resize = true;
|
| }
|
| - } else if (IsFirstBufferAfterReset(next_bitstream_buffer_id_,
|
| - reset_bitstream_buffer_id_)) {
|
| - // TODO(wuchengli): VDA should handle it. Remove this when
|
| - // http://crosbug.com/p/21913 is fixed.
|
| -
|
| - // If we're are in an error condition, increase the counter.
|
| - vda_error_counter_ += vda_error_counter_ ? 1 : 0;
|
| -
|
| - DVLOG(1) << "The first frame should be a key frame. Drop this.";
|
| + } else if (IsFirstBufferAfterReset(next_decoder_buffer_id_,
|
| + reset_decoder_buffer_id_)) {
|
| + // The first frame after a reset should be a key frame. If we are in an
|
| + // error condition, increase the counter.
|
| + LOG(ERROR) << "The first frame should be a key frame. Drop this.";
|
| + decoder_error_counter_ += decoder_error_counter_ ? 1 : 0;
|
| return WEBRTC_VIDEO_CODEC_ERROR;
|
| }
|
|
|
| - // Create buffer metadata.
|
| - BufferData buffer_data(next_bitstream_buffer_id_,
|
| - inputImage._timeStamp,
|
| - inputImage._length,
|
| - gfx::Rect(frame_size_));
|
| + const int32_t decoder_buffer_id = next_decoder_buffer_id_;
|
| + auto decoder_buffer = CreateDecoderBuffer(inputImage);
|
| +
|
| // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
|
| - next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & ID_LAST;
|
| -
|
| - // If a shared memory segment is available, there are no pending buffers, and
|
| - // this isn't a mid-stream resolution change, then send the buffer for decode
|
| - // immediately. Otherwise, save the buffer in the queue for later decode.
|
| - std::unique_ptr<base::SharedMemory> shm_buffer;
|
| - if (!need_to_reset_for_midstream_resize && pending_buffers_.empty())
|
| - shm_buffer = GetSHM_Locked(inputImage._length);
|
| - if (!shm_buffer) {
|
| - if (!SaveToPendingBuffers_Locked(inputImage, buffer_data)) {
|
| - // We have exceeded the pending buffers count, we are severely behind.
|
| - // Since we are returning ERROR, WebRTC will not be interested in the
|
| - // remaining buffers, and will provide us with a new keyframe instead.
|
| - // Better to drop any pending buffers and start afresh to catch up faster.
|
| - DVLOG(1) << "Exceeded maximum pending buffer count, dropping";
|
| - ClearPendingBuffers();
|
| - return WEBRTC_VIDEO_CODEC_ERROR;
|
| - }
|
| + next_decoder_buffer_id_ = (next_decoder_buffer_id_ + 1) & ID_LAST;
|
| +
|
| + // Try to enqueue the image to be decoded. These frames will be consumed on
|
| + // the decoder thread. It would be ideal to post this to the decoder thread to
|
| + // avoid the lock, but we need to return an error from this function if
|
| + // |pending_buffers_| is full. So use a lock instead.
|
| + if (!SaveToPendingBuffers_Locked(decoder_buffer_id,
|
| + std::move(decoder_buffer))) {
|
| + // We have exceeded the pending buffers count, we are severely behind.
|
| + // Since we are returning ERROR, WebRTC will not be interested in the
|
| + // remaining buffers, and will provide us with a new keyframe instead.
|
| + // Better to drop any pending buffers and start afresh to catch up faster.
|
| + LOG(ERROR) << "Exceeded maximum pending buffer count, dropping";
|
| + ClearPendingBuffers_Locked();
|
| + return WEBRTC_VIDEO_CODEC_ERROR;
|
| + }
|
|
|
| - if (need_to_reset_for_midstream_resize) {
|
| - base::AutoUnlock auto_unlock(lock_);
|
| - Release();
|
| - }
|
| - return WEBRTC_VIDEO_CODEC_OK;
|
| + if (need_to_reset_for_midstream_resize) {
|
| + base::AutoUnlock auto_unlock(lock_);
|
| + Release();
|
| }
|
|
|
| - SaveToDecodeBuffers_Locked(inputImage, std::move(shm_buffer), buffer_data);
|
| - factories_->GetTaskRunner()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(&RTCVideoDecoder::RequestBufferDecode,
|
| - weak_factory_.GetWeakPtr()));
|
| + // If the decoder has been initialized, request a decode.
|
| + if (state_ == INITIALIZED) {
|
| + decoder_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&RTCVideoDecoder::RequestBufferDecode,
|
| + weak_factory_.GetWeakPtr()));
|
| + }
|
| return WEBRTC_VIDEO_CODEC_OK;
|
| }
|
|
|
| int32_t RTCVideoDecoder::RegisterDecodeCompleteCallback(
|
| webrtc::DecodedImageCallback* callback) {
|
| - DVLOG(2) << "RegisterDecodeCompleteCallback";
|
| + DVLOG(2) << __func__;
|
| + DCHECK(webrtc_decoding_thread_checker_.CalledOnValidThread());
|
| DCHECK(callback);
|
| +
|
| 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 WebRTC can call InitDecode and start decoding
|
| - // again.
|
| + DVLOG(2) << __func__;
|
| + DCHECK(!decoder_task_runner_->BelongsToCurrentThread());
|
| +
|
| base::AutoLock auto_lock(lock_);
|
| if (state_ == UNINITIALIZED) {
|
| LOG(ERROR) << "Decoder not initialized.";
|
| return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
|
| }
|
| - if (next_bitstream_buffer_id_ != 0)
|
| - reset_bitstream_buffer_id_ = next_bitstream_buffer_id_ - 1;
|
| +
|
| + if (next_decoder_buffer_id_ != 0)
|
| + reset_decoder_buffer_id_ = next_decoder_buffer_id_ - 1;
|
| else
|
| - reset_bitstream_buffer_id_ = ID_LAST;
|
| - // If VDA is already resetting, no need to request the reset again.
|
| + reset_decoder_buffer_id_ = ID_LAST;
|
| +
|
| + // If the decoder is already resetting, no need to request the reset again.
|
| if (state_ != RESETTING) {
|
| state_ = RESETTING;
|
| - factories_->GetTaskRunner()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(&RTCVideoDecoder::ResetInternal,
|
| - weak_factory_.GetWeakPtr()));
|
| - }
|
| - return WEBRTC_VIDEO_CODEC_OK;
|
| -}
|
| -
|
| -void RTCVideoDecoder::ProvidePictureBuffers(uint32_t count,
|
| - media::VideoPixelFormat format,
|
| - uint32_t textures_per_buffer,
|
| - const gfx::Size& size,
|
| - uint32_t texture_target) {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - DVLOG(3) << "ProvidePictureBuffers. texture_target=" << texture_target;
|
| - DCHECK_EQ(1u, textures_per_buffer);
|
| -
|
| - if (!vda_)
|
| - return;
|
| -
|
| - std::vector<uint32_t> texture_ids;
|
| - std::vector<gpu::Mailbox> texture_mailboxes;
|
| - decoder_texture_target_ = texture_target;
|
| -
|
| - if (format == media::PIXEL_FORMAT_UNKNOWN)
|
| - format = media::PIXEL_FORMAT_ARGB;
|
|
|
| - if ((pixel_format_ != media::PIXEL_FORMAT_UNKNOWN) &&
|
| - (format != pixel_format_)) {
|
| - NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
|
| - return;
|
| - }
|
| -
|
| - pixel_format_ = format;
|
| - if (!factories_->CreateTextures(count,
|
| - size,
|
| - &texture_ids,
|
| - &texture_mailboxes,
|
| - decoder_texture_target_)) {
|
| - NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
|
| - return;
|
| + // Reset |decoder_| on |decoder_task_runner_|. Until the callback returns,
|
| + // no more calls should be made on |decoder_|.
|
| + decoder_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&media::VideoDecoder::Reset,
|
| + base::Unretained(decoder_.get()),
|
| + base::Bind(&RTCVideoDecoder::OnResetDone,
|
| + weak_factory_.GetWeakPtr())));
|
| }
|
| - DCHECK_EQ(count, texture_ids.size());
|
| - DCHECK_EQ(count, texture_mailboxes.size());
|
| -
|
| - std::vector<media::PictureBuffer> picture_buffers;
|
| - for (size_t i = 0; i < texture_ids.size(); ++i) {
|
| - media::PictureBuffer::TextureIds ids;
|
| - ids.push_back(texture_ids[i]);
|
| - std::vector<gpu::Mailbox> mailboxes;
|
| - mailboxes.push_back(texture_mailboxes[i]);
|
| -
|
| - picture_buffers.push_back(
|
| - media::PictureBuffer(next_picture_buffer_id_++, size, ids, mailboxes));
|
| - 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_t id) {
|
| - DVLOG(3) << "DismissPictureBuffer. id=" << id;
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| -
|
| - std::map<int32_t, 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);
|
| -
|
| - if (!picture_buffers_at_display_.count(id)) {
|
| - // We can delete the texture immediately as it's not being displayed.
|
| - factories_->DeleteTexture(buffer_to_dismiss.texture_ids()[0]);
|
| - return;
|
| - }
|
| - // Not destroying a texture in display in |picture_buffers_at_display_|.
|
| - // Postpone deletion until after it's returned to us.
|
| + return WEBRTC_VIDEO_CODEC_OK;
|
| }
|
|
|
| -void RTCVideoDecoder::PictureReady(const media::Picture& picture) {
|
| - DVLOG(3) << "PictureReady";
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| -
|
| - std::map<int32_t, 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;
|
| - }
|
| -
|
| - uint32_t timestamp = 0;
|
| - gfx::Rect visible_rect;
|
| - GetBufferData(picture.bitstream_buffer_id(), ×tamp, &visible_rect);
|
| - if (!picture.visible_rect().IsEmpty())
|
| - visible_rect = picture.visible_rect();
|
| -
|
| - const media::PictureBuffer& pb = it->second;
|
| - if (visible_rect.IsEmpty() || !gfx::Rect(pb.size()).Contains(visible_rect)) {
|
| - LOG(ERROR) << "Invalid picture size: " << visible_rect.ToString()
|
| - << " should fit in " << pb.size().ToString();
|
| - NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
|
| - return;
|
| - }
|
| -
|
| - scoped_refptr<media::VideoFrame> frame =
|
| - CreateVideoFrame(picture, pb, timestamp, visible_rect, pixel_format_);
|
| - if (!frame) {
|
| - NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
|
| - return;
|
| - }
|
| - bool inserted = picture_buffers_at_display_
|
| - .insert(std::make_pair(picture.picture_buffer_id(),
|
| - pb.texture_ids()[0]))
|
| - .second;
|
| - DCHECK(inserted);
|
| -
|
| - // Create a WebRTC video frame.
|
| - webrtc::VideoFrame decoded_image(
|
| - new rtc::RefCountedObject<WebRtcVideoFrameAdapter>(frame), timestamp, 0,
|
| - webrtc::kVideoRotation_0);
|
| -
|
| - // Invoke decode callback. WebRTC expects no callback after Release.
|
| +void RTCVideoDecoder::OnResetDone() {
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
| {
|
| base::AutoLock auto_lock(lock_);
|
| - DCHECK(decode_complete_callback_);
|
| - if (IsBufferAfterReset(picture.bitstream_buffer_id(),
|
| - reset_bitstream_buffer_id_)) {
|
| - decode_complete_callback_->Decoded(decoded_image);
|
| - }
|
| - // Reset error counter as we successfully decoded a frame.
|
| - vda_error_counter_ = 0;
|
| + state_ = INITIALIZED;
|
| }
|
| -}
|
|
|
| -scoped_refptr<media::VideoFrame> RTCVideoDecoder::CreateVideoFrame(
|
| - const media::Picture& picture,
|
| - const media::PictureBuffer& pb,
|
| - uint32_t timestamp,
|
| - const gfx::Rect& visible_rect,
|
| - media::VideoPixelFormat pixel_format) {
|
| - DCHECK(decoder_texture_target_);
|
| - // Convert timestamp from 90KHz to ms.
|
| - base::TimeDelta timestamp_ms = base::TimeDelta::FromInternalValue(
|
| - base::checked_cast<uint64_t>(timestamp) * 1000 / 90);
|
| - // TODO(mcasas): The incoming data may actually be in a YUV format, but may be
|
| - // labelled as ARGB. This may or may not be reported by VDA, depending on
|
| - // whether it provides an implementation of VDA::GetOutputFormat().
|
| - // This prevents the compositor from messing with it, since the underlying
|
| - // platform can handle the former format natively. Make sure the
|
| - // correct format is used and everyone down the line understands it.
|
| - gpu::MailboxHolder holders[media::VideoFrame::kMaxPlanes] = {
|
| - gpu::MailboxHolder(pb.texture_mailbox(0), gpu::SyncToken(),
|
| - decoder_texture_target_)};
|
| - scoped_refptr<media::VideoFrame> frame =
|
| - media::VideoFrame::WrapNativeTextures(
|
| - pixel_format, holders,
|
| - media::BindToCurrentLoop(base::Bind(
|
| - &RTCVideoDecoder::ReleaseMailbox, weak_factory_.GetWeakPtr(),
|
| - factories_, picture.picture_buffer_id(), pb.texture_ids()[0])),
|
| - pb.size(), visible_rect, visible_rect.size(), timestamp_ms);
|
| - if (frame && picture.allow_overlay()) {
|
| - frame->metadata()->SetBoolean(media::VideoFrameMetadata::ALLOW_OVERLAY,
|
| - true);
|
| - }
|
| - return frame;
|
| + // Send any pending buffers for decoding.
|
| + RequestBufferDecode();
|
| }
|
|
|
| -void RTCVideoDecoder::NotifyEndOfBitstreamBuffer(int32_t id) {
|
| - DVLOG(3) << "NotifyEndOfBitstreamBuffer. id=" << id;
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| -
|
| - auto it = bitstream_buffers_in_decoder_.find(id);
|
| - if (it == bitstream_buffers_in_decoder_.end()) {
|
| - NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
|
| - NOTREACHED() << "Missing bitstream buffer: " << id;
|
| +void RTCVideoDecoder::OnBufferDecoded(int32_t decoder_buffer_id,
|
| + media::DecodeStatus status) {
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
| +
|
| + // This buffer is no longer in flight. Remove it from the in-flight buffers.
|
| + bool erased = buffers_in_decoder_.erase(decoder_buffer_id);
|
| + DCHECK(erased);
|
| +
|
| + // This is only called when |decoder_| is torn down while buffers are in
|
| + // flight. This probably indicates that something has gone wrong, so let's
|
| + // do NOTREACHED() now and figure out how to handle this later.
|
| + if (status == media::DecodeStatus::ABORTED) {
|
| + NOTREACHED();
|
| + } else if (status == media::DecodeStatus::OK) {
|
| + decoder_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&RTCVideoDecoder::RequestBufferDecode,
|
| + weak_factory_.GetWeakPtr()));
|
| return;
|
| }
|
|
|
| - {
|
| - base::AutoLock auto_lock(lock_);
|
| - PutSHM_Locked(std::move(it->second));
|
| - }
|
| - bitstream_buffers_in_decoder_.erase(it);
|
| -
|
| - RequestBufferDecode();
|
| -}
|
| -
|
| -void RTCVideoDecoder::NotifyFlushDone() {
|
| - DVLOG(3) << "NotifyFlushDone";
|
| - NOTREACHED() << "Unexpected flush done notification.";
|
| -}
|
| -
|
| -void RTCVideoDecoder::NotifyResetDone() {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - DVLOG(3) << "NotifyResetDone";
|
| + // If we hit here, |decoder_| is bubbling up an error from the remote decoder.
|
| + DCHECK(status == media::DecodeStatus::DECODE_ERROR);
|
| + LOG(ERROR) << "Decoder encountered an error.";
|
|
|
| - if (!vda_)
|
| - return;
|
| + // TODO(slan): Do we need an UMA stat?
|
|
|
| - input_buffer_data_.clear();
|
| + // Re-enqueue patches in-flight so we can attempt to decode them again. Insert
|
| + // them back into the queue in their original order.
|
| + std::map<int32_t, scoped_refptr<media::DecoderBuffer>> sorted(
|
| + buffers_in_decoder_.begin(), buffers_in_decoder_.end());
|
| {
|
| base::AutoLock auto_lock(lock_);
|
| - state_ = INITIALIZED;
|
| - }
|
| - // Send the pending buffers for decoding.
|
| - RequestBufferDecode();
|
| -}
|
| + for (auto rit = sorted.rbegin(); rit != sorted.rend(); ++rit)
|
| + pending_buffers_.push_front(std::make_pair(rit->first, rit->second));
|
|
|
| -void RTCVideoDecoder::NotifyError(media::VideoDecodeAccelerator::Error error) {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - if (!vda_)
|
| - return;
|
| -
|
| - LOG(ERROR) << "VDA Error:" << error;
|
| - UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoDecoderError", error,
|
| - media::VideoDecodeAccelerator::ERROR_MAX + 1);
|
| - DestroyVDA();
|
| -
|
| - base::AutoLock auto_lock(lock_);
|
| - state_ = DECODE_ERROR;
|
| - ++vda_error_counter_;
|
| + state_ = DECODE_ERROR;
|
| + ++decoder_error_counter_;
|
| + }
|
| }
|
|
|
| void RTCVideoDecoder::RequestBufferDecode() {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - if (!vda_)
|
| - return;
|
| -
|
| - MovePendingBuffersToDecodeBuffers();
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
|
|
| + // If less than the maximum possible number of decode requests are in flight,
|
| + // we might be able to push more into the decoder.
|
| while (CanMoreDecodeWorkBeDone()) {
|
| - // Get a buffer and data from the queue.
|
| - std::unique_ptr<base::SharedMemory> shm_buffer;
|
| - BufferData buffer_data;
|
| + int32_t decoder_buffer_id = ID_INVALID;
|
| + scoped_refptr<media::DecoderBuffer> decoder_buffer;
|
| {
|
| base::AutoLock auto_lock(lock_);
|
| - // Do not request decode if VDA is resetting.
|
| - if (decode_buffers_.empty() || state_ == RESETTING)
|
| +
|
| + // Do not request decode if decoder_ is resetting or if there are no
|
| + // buffers to decode.
|
| + if (state_ == RESETTING || pending_buffers_.empty())
|
| return;
|
| - shm_buffer = std::move(decode_buffers_.front().first);
|
| - buffer_data = decode_buffers_.front().second;
|
| - decode_buffers_.pop_front();
|
| - // Drop the buffers before Release is called.
|
| - if (!IsBufferAfterReset(buffer_data.bitstream_buffer_id,
|
| - reset_bitstream_buffer_id_)) {
|
| - PutSHM_Locked(std::move(shm_buffer));
|
| - continue;
|
| - }
|
| +
|
| + // Remove the buffer from the queue.
|
| + decoder_buffer_id = pending_buffers_.front().first;
|
| + decoder_buffer = std::move(pending_buffers_.front().second);
|
| + pending_buffers_.pop_front();
|
| }
|
|
|
| - // Create a BitstreamBuffer and send to VDA to decode.
|
| - media::BitstreamBuffer bitstream_buffer(
|
| - buffer_data.bitstream_buffer_id, shm_buffer->handle(), buffer_data.size,
|
| - 0, base::TimeDelta::FromInternalValue(buffer_data.timestamp));
|
| - const bool inserted = bitstream_buffers_in_decoder_
|
| - .insert(std::make_pair(bitstream_buffer.id(),
|
| - std::move(shm_buffer)))
|
| - .second;
|
| - DCHECK(inserted) << "bitstream_buffer_id " << bitstream_buffer.id()
|
| - << " existed already in bitstream_buffers_in_decoder_";
|
| - RecordBufferData(buffer_data);
|
| - vda_->Decode(bitstream_buffer);
|
| - }
|
| -}
|
| + // If the buffer is from before the last call to Release(), drop it on
|
| + // the floor and keep going.
|
| + if (!IsBufferAfterReset(decoder_buffer_id, reset_decoder_buffer_id_)) {
|
| + continue;
|
| + }
|
|
|
| -bool RTCVideoDecoder::CanMoreDecodeWorkBeDone() {
|
| - return bitstream_buffers_in_decoder_.size() < kMaxInFlightDecodes;
|
| -}
|
| + // Save a reference to |decoder_buffer| in |buffers_in_decoder_|.
|
| + DCHECK(!base::ContainsKey(buffers_in_decoder_, decoder_buffer_id));
|
| + buffers_in_decoder_[decoder_buffer_id] = decoder_buffer;
|
|
|
| -bool RTCVideoDecoder::IsBufferAfterReset(int32_t id_buffer, int32_t id_reset) {
|
| - if (id_reset == ID_INVALID)
|
| - return true;
|
| - int32_t diff = id_buffer - id_reset;
|
| - if (diff <= 0)
|
| - diff += ID_LAST + 1;
|
| - return diff < ID_HALF;
|
| + // Push the buffer to the decoder.
|
| + decoder_->Decode(std::move(decoder_buffer),
|
| + base::Bind(&RTCVideoDecoder::OnBufferDecoded,
|
| + weak_factory_.GetWeakPtr(), decoder_buffer_id));
|
| + }
|
| }
|
|
|
| -bool RTCVideoDecoder::IsFirstBufferAfterReset(int32_t id_buffer,
|
| - int32_t id_reset) {
|
| - if (id_reset == ID_INVALID)
|
| - return id_buffer == 0;
|
| - return id_buffer == ((id_reset + 1) & ID_LAST);
|
| +bool RTCVideoDecoder::CanMoreDecodeWorkBeDone() {
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
| + return (static_cast<int>(buffers_in_decoder_.size()) <
|
| + decoder_->GetMaxDecodeRequests());
|
| }
|
|
|
| -void RTCVideoDecoder::SaveToDecodeBuffers_Locked(
|
| - const webrtc::EncodedImage& input_image,
|
| - std::unique_ptr<base::SharedMemory> shm_buffer,
|
| - const BufferData& buffer_data) {
|
| - memcpy(shm_buffer->memory(), input_image._buffer, input_image._length);
|
| -
|
| - // Store the buffer and the metadata to the queue.
|
| - decode_buffers_.emplace_back(std::move(shm_buffer), buffer_data);
|
| +int RTCVideoDecoder::GetDecoderErrorCounterForTesting() {
|
| + base::AutoLock auto_lock(lock_);
|
| + return decoder_error_counter_;
|
| }
|
|
|
| bool RTCVideoDecoder::SaveToPendingBuffers_Locked(
|
| - const webrtc::EncodedImage& input_image,
|
| - const BufferData& buffer_data) {
|
| - DVLOG(2) << "SaveToPendingBuffers_Locked"
|
| - << ". pending_buffers size=" << pending_buffers_.size()
|
| - << ". decode_buffers_ size=" << decode_buffers_.size()
|
| - << ". available_shm size=" << available_shm_segments_.size();
|
| - // Queued too many buffers. Something goes wrong.
|
| + int32_t decoder_buffer_id,
|
| + scoped_refptr<media::DecoderBuffer> decoder_buffer) {
|
| + DVLOG(2) << __func__ << " pending_buffers size=" << pending_buffers_.size();
|
| + lock_.AssertAcquired();
|
| +
|
| + // Queued too many buffers. Something has gone wrong.
|
| if (pending_buffers_.size() >= kMaxNumOfPendingBuffers) {
|
| LOG(WARNING) << "Too many pending buffers!";
|
| return false;
|
| }
|
|
|
| - // Clone the input image and save it to the queue.
|
| - uint8_t* buffer = new uint8_t[input_image._length];
|
| - // TODO(wuchengli): avoid memcpy. Extend webrtc::VideoDecoder::Decode()
|
| - // interface to take a non-const ptr to the frame and add a method to the
|
| - // frame that will swap buffers with another.
|
| - memcpy(buffer, input_image._buffer, input_image._length);
|
| - 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);
|
| -
|
| - pending_buffers_.push_back(buffer_pair);
|
| + // Enqueue the buffer, so that it may be consumed by the decoder.
|
| + pending_buffers_.push_back(
|
| + std::make_pair(decoder_buffer_id, std::move(decoder_buffer)));
|
| return true;
|
| }
|
|
|
| -void RTCVideoDecoder::MovePendingBuffersToDecodeBuffers() {
|
| - base::AutoLock auto_lock(lock_);
|
| - while (pending_buffers_.size() > 0) {
|
| - // Get a pending buffer from the queue.
|
| - const webrtc::EncodedImage& input_image = pending_buffers_.front().first;
|
| - const BufferData& buffer_data = pending_buffers_.front().second;
|
| -
|
| - // Drop the frame if it comes before Release.
|
| - if (!IsBufferAfterReset(buffer_data.bitstream_buffer_id,
|
| - reset_bitstream_buffer_id_)) {
|
| - delete[] input_image._buffer;
|
| - pending_buffers_.pop_front();
|
| - continue;
|
| - }
|
| - // Get shared memory and save it to decode buffers.
|
| - std::unique_ptr<base::SharedMemory> shm_buffer =
|
| - GetSHM_Locked(input_image._length);
|
| - if (!shm_buffer)
|
| - return;
|
| - SaveToDecodeBuffers_Locked(input_image, std::move(shm_buffer), buffer_data);
|
| - delete[] input_image._buffer;
|
| - pending_buffers_.pop_front();
|
| - }
|
| -}
|
| -
|
| -void RTCVideoDecoder::ResetInternal() {
|
| - DVLOG(2) << __func__;
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| -
|
| - if (vda_) {
|
| - vda_->Reset();
|
| - } else {
|
| - CreateVDA(vda_codec_profile_, nullptr);
|
| - if (vda_) {
|
| - base::AutoLock auto_lock(lock_);
|
| - state_ = INITIALIZED;
|
| - }
|
| - }
|
| -}
|
| -
|
| -// static
|
| -void RTCVideoDecoder::ReleaseMailbox(
|
| - base::WeakPtr<RTCVideoDecoder> decoder,
|
| - media::GpuVideoAcceleratorFactories* factories,
|
| - int64_t picture_buffer_id,
|
| - uint32_t texture_id,
|
| - const gpu::SyncToken& release_sync_token) {
|
| - DCHECK(factories->GetTaskRunner()->BelongsToCurrentThread());
|
| - factories->WaitSyncToken(release_sync_token);
|
| -
|
| - if (decoder) {
|
| - decoder->ReusePictureBuffer(picture_buffer_id);
|
| - return;
|
| - }
|
| - // It's the last chance to delete the texture after display,
|
| - // because RTCVideoDecoder was destructed.
|
| - factories->DeleteTexture(texture_id);
|
| -}
|
| -
|
| -void RTCVideoDecoder::ReusePictureBuffer(int64_t picture_buffer_id) {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - DVLOG(3) << "ReusePictureBuffer. id=" << picture_buffer_id;
|
| -
|
| - DCHECK(!picture_buffers_at_display_.empty());
|
| - PictureBufferTextureMap::iterator display_iterator =
|
| - picture_buffers_at_display_.find(picture_buffer_id);
|
| - uint32_t texture_id = display_iterator->second;
|
| - DCHECK(display_iterator != picture_buffers_at_display_.end());
|
| - picture_buffers_at_display_.erase(display_iterator);
|
| -
|
| - if (!assigned_picture_buffers_.count(picture_buffer_id)) {
|
| - // This picture was dismissed while in display, so we postponed deletion.
|
| - factories_->DeleteTexture(texture_id);
|
| - return;
|
| - }
|
| -
|
| - // DestroyVDA() might already have been called.
|
| - if (vda_)
|
| - vda_->ReusePictureBuffer(picture_buffer_id);
|
| -}
|
| -
|
| -bool RTCVideoDecoder::IsProfileSupported(media::VideoCodecProfile profile) {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - media::VideoDecodeAccelerator::Capabilities capabilities =
|
| - factories_->GetVideoDecodeAcceleratorCapabilities();
|
| +void RTCVideoDecoder::InitializeDecoder(
|
| + base::WaitableEvent* decoder_initialized,
|
| + const media::VideoDecoderConfig& config) {
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
|
|
| - for (const auto& supported_profile : capabilities.supported_profiles) {
|
| - if (profile == supported_profile.profile) {
|
| - min_resolution_ = supported_profile.min_resolution;
|
| - max_resolution_ = supported_profile.max_resolution;
|
| - return true;
|
| - }
|
| - }
|
| + if (!decoder_)
|
| + decoder_ = create_video_decoder_cb_.Run();
|
|
|
| - return false;
|
| + decoder_->Initialize(
|
| + config, true /* low_delay */, nullptr /* cdm_context */,
|
| + base::Bind(&RTCVideoDecoder::OnDecoderInitialized,
|
| + weak_factory_.GetWeakPtr(), decoder_initialized),
|
| + base::Bind(&RTCVideoDecoder::OnFrameReady, weak_factory_.GetWeakPtr()));
|
| }
|
|
|
| -void RTCVideoDecoder::CreateVDA(media::VideoCodecProfile profile,
|
| - base::WaitableEvent* waiter) {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| -
|
| - if (!IsProfileSupported(profile)) {
|
| - DVLOG(1) << "Unsupported profile " << profile;
|
| - } else {
|
| - vda_ = factories_->CreateVideoDecodeAccelerator();
|
| -
|
| - media::VideoDecodeAccelerator::Config config(profile);
|
| - if (vda_ && !vda_->Initialize(config, this))
|
| - vda_.release()->Destroy();
|
| - vda_codec_profile_ = profile;
|
| - }
|
| -
|
| - if (waiter)
|
| - waiter->Signal();
|
| -}
|
| -
|
| -void RTCVideoDecoder::DestroyTextures() {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| -
|
| - // Not destroying PictureBuffers in |picture_buffers_at_display_| yet, since
|
| - // their textures may still be in use by the user of this RTCVideoDecoder.
|
| - for (const auto& picture_buffer_at_display : picture_buffers_at_display_)
|
| - assigned_picture_buffers_.erase(picture_buffer_at_display.first);
|
| -
|
| - for (const auto& assigned_picture_buffer : assigned_picture_buffers_)
|
| - factories_->DeleteTexture(assigned_picture_buffer.second.texture_ids()[0]);
|
| -
|
| - assigned_picture_buffers_.clear();
|
| -}
|
| -
|
| -void RTCVideoDecoder::DestroyVDA() {
|
| - DVLOG(2) << "DestroyVDA";
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - if (vda_)
|
| - vda_.release()->Destroy();
|
| - DestroyTextures();
|
| -
|
| - base::AutoLock auto_lock(lock_);
|
| -
|
| - // Put the buffers back in case we restart the decoder.
|
| - for (auto& buffer : bitstream_buffers_in_decoder_)
|
| - PutSHM_Locked(std::move(buffer.second));
|
| - bitstream_buffers_in_decoder_.clear();
|
| -
|
| - state_ = UNINITIALIZED;
|
| -}
|
| -
|
| -std::unique_ptr<base::SharedMemory> RTCVideoDecoder::GetSHM_Locked(
|
| - size_t min_size) {
|
| - // Reuse a SHM if possible.
|
| - if (!available_shm_segments_.empty() &&
|
| - available_shm_segments_.back()->mapped_size() >= min_size) {
|
| - std::unique_ptr<base::SharedMemory> buffer =
|
| - std::move(available_shm_segments_.back());
|
| - available_shm_segments_.pop_back();
|
| - return buffer;
|
| - }
|
| -
|
| - if (available_shm_segments_.size() != num_shm_buffers_) {
|
| - // Either available_shm_segments_ is empty (and we already have some SHM
|
| - // buffers allocated), or the size of available segments is not large
|
| - // enough. In the former case we need to wait for buffers to be returned,
|
| - // in the latter we need to wait for all buffers to be returned to drop
|
| - // them and reallocate with a new size.
|
| - return NULL;
|
| - }
|
| +void RTCVideoDecoder::OnDecoderInitialized(
|
| + base::WaitableEvent* decoder_initialized,
|
| + bool success) {
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
|
|
| - if (num_shm_buffers_ != 0) {
|
| - available_shm_segments_.clear();
|
| - num_shm_buffers_ = 0;
|
| + {
|
| + base::AutoLock lock(lock_);
|
| + state_ = success ? INITIALIZED : UNINITIALIZED;
|
| + LOG_IF(ERROR, !success) << "Decoder failed to initialize!";
|
| }
|
|
|
| - // Create twice as large buffers as required, to avoid frequent reallocation.
|
| - factories_->GetTaskRunner()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(&RTCVideoDecoder::CreateSHM, weak_factory_.GetWeakPtr(),
|
| - kNumSharedMemorySegments, min_size * 2));
|
| -
|
| - // We'll be called again after the shared memory is created.
|
| - return NULL;
|
| + // Unblock the waiting thread.
|
| + decoder_initialized->Signal();
|
| }
|
|
|
| -void RTCVideoDecoder::PutSHM_Locked(
|
| - std::unique_ptr<base::SharedMemory> shm_buffer) {
|
| - lock_.AssertAcquired();
|
| - available_shm_segments_.push_back(std::move(shm_buffer));
|
| -}
|
| +void RTCVideoDecoder::OnFrameReady(
|
| + const scoped_refptr<media::VideoFrame>& frame) {
|
| + DVLOG(2) << __func__;
|
| + DCHECK(decoder_task_runner_->BelongsToCurrentThread());
|
|
|
| -void RTCVideoDecoder::CreateSHM(size_t count, size_t size) {
|
| - DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
|
| - DVLOG(2) << "CreateSHM. count=" << count << ", size=" << size;
|
| -
|
| - for (size_t i = 0; i < count; i++) {
|
| - std::unique_ptr<base::SharedMemory> shm =
|
| - factories_->CreateSharedMemory(size);
|
| - if (!shm) {
|
| - LOG(ERROR) << "Failed allocating shared memory of size=" << size;
|
| - NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
|
| - return;
|
| - }
|
| + // Create a WebRTC video frame.
|
| + webrtc::VideoFrame decoded_image(
|
| + new rtc::RefCountedObject<WebRtcVideoFrameAdapter>(frame),
|
| + frame->timestamp().ToInternalValue(), 0, webrtc::kVideoRotation_0);
|
|
|
| + // Lock and pass the frame up to the WebRTC client class.
|
| + {
|
| base::AutoLock auto_lock(lock_);
|
| - PutSHM_Locked(std::move(shm));
|
| - ++num_shm_buffers_;
|
| - }
|
| -
|
| - // Kick off the decoding.
|
| - RequestBufferDecode();
|
| -}
|
| -
|
| -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();
|
| -}
|
| + DCHECK(decode_complete_callback_);
|
| + decode_complete_callback_->Decoded(decoded_image);
|
|
|
| -void RTCVideoDecoder::GetBufferData(int32_t bitstream_buffer_id,
|
| - uint32_t* timestamp,
|
| - gfx::Rect* visible_rect) {
|
| - for (const auto& buffer_data : input_buffer_data_) {
|
| - if (buffer_data.bitstream_buffer_id != bitstream_buffer_id)
|
| - continue;
|
| - *timestamp = buffer_data.timestamp;
|
| - *visible_rect = buffer_data.visible_rect;
|
| - return;
|
| + // Reset error counter as we successfully decoded a frame.
|
| + decoder_error_counter_ = 0;
|
| }
|
| - NOTREACHED() << "Missing bitstream buffer id: " << bitstream_buffer_id;
|
| }
|
|
|
| int32_t RTCVideoDecoder::RecordInitDecodeUMA(int32_t status) {
|
| + DVLOG(2) << __func__;
|
| +
|
| // Logging boolean is enough to know if HW decoding has been used. Also,
|
| // InitDecode is less likely to return an error so enum is not used here.
|
| bool sample = (status == WEBRTC_VIDEO_CODEC_OK) ? true : false;
|
| @@ -878,15 +588,10 @@ int32_t RTCVideoDecoder::RecordInitDecodeUMA(int32_t status) {
|
| return status;
|
| }
|
|
|
| -void RTCVideoDecoder::DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent()
|
| - const {
|
| - DCHECK(factories_->GetTaskRunner()->BelongsToCurrentThread());
|
| -}
|
| +void RTCVideoDecoder::ClearPendingBuffers_Locked() {
|
| + DVLOG(2) << __func__;
|
|
|
| -void RTCVideoDecoder::ClearPendingBuffers() {
|
| - // Delete WebRTC input buffers.
|
| - for (const auto& pending_buffer : pending_buffers_)
|
| - delete[] pending_buffer.first._buffer;
|
| + lock_.AssertAcquired();
|
| pending_buffers_.clear();
|
| }
|
|
|
|
|