2016-11-22 01:43:03 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "media/engine/videodecodersoftwarefallbackwrapper.h"
|
2016-11-22 01:43:03 -08:00
|
|
|
|
|
|
|
|
#include <string>
|
2017-10-31 09:53:08 -07:00
|
|
|
#include <utility>
|
2016-11-22 01:43:03 -08:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "media/engine/internaldecoderfactory.h"
|
|
|
|
|
#include "modules/video_coding/include/video_error_codes.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
|
|
|
|
#include "rtc_base/trace_event.h"
|
2016-11-22 01:43:03 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
VideoDecoderSoftwareFallbackWrapper::VideoDecoderSoftwareFallbackWrapper(
|
2017-11-06 17:41:54 +01:00
|
|
|
std::unique_ptr<VideoDecoder> sw_fallback_decoder,
|
|
|
|
|
std::unique_ptr<VideoDecoder> hw_decoder)
|
|
|
|
|
: use_hw_decoder_(true),
|
|
|
|
|
hw_decoder_(std::move(hw_decoder)),
|
|
|
|
|
hw_decoder_initialized_(false),
|
|
|
|
|
fallback_decoder_(std::move(sw_fallback_decoder)),
|
|
|
|
|
fallback_implementation_name_(
|
|
|
|
|
std::string(fallback_decoder_->ImplementationName()) +
|
|
|
|
|
" (fallback from: " + hw_decoder_->ImplementationName() + ")"),
|
2017-02-13 17:11:08 -05:00
|
|
|
callback_(nullptr) {}
|
2016-11-22 01:43:03 -08:00
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::InitDecode(
|
|
|
|
|
const VideoCodec* codec_settings,
|
|
|
|
|
int32_t number_of_cores) {
|
2017-11-06 17:41:54 +01:00
|
|
|
// Always try to use the HW decoder in this state.
|
|
|
|
|
use_hw_decoder_ = true;
|
2016-11-22 01:43:03 -08:00
|
|
|
codec_settings_ = *codec_settings;
|
|
|
|
|
number_of_cores_ = number_of_cores;
|
2017-11-06 17:41:54 +01:00
|
|
|
int32_t ret = hw_decoder_->InitDecode(codec_settings, number_of_cores);
|
2017-10-31 17:15:22 -07:00
|
|
|
if (ret == WEBRTC_VIDEO_CODEC_OK) {
|
2017-11-06 17:41:54 +01:00
|
|
|
hw_decoder_initialized_ = true;
|
2017-02-13 17:11:08 -05:00
|
|
|
return ret;
|
|
|
|
|
}
|
2017-11-06 17:41:54 +01:00
|
|
|
hw_decoder_initialized_ = false;
|
2017-02-13 17:11:08 -05:00
|
|
|
|
|
|
|
|
// Try to initialize fallback decoder.
|
|
|
|
|
if (InitFallbackDecoder())
|
|
|
|
|
return WEBRTC_VIDEO_CODEC_OK;
|
2017-11-06 17:41:54 +01:00
|
|
|
|
2017-02-13 17:11:08 -05:00
|
|
|
return ret;
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VideoDecoderSoftwareFallbackWrapper::InitFallbackDecoder() {
|
|
|
|
|
LOG(LS_WARNING) << "Decoder falling back to software decoding.";
|
|
|
|
|
if (fallback_decoder_->InitDecode(&codec_settings_, number_of_cores_) !=
|
|
|
|
|
WEBRTC_VIDEO_CODEC_OK) {
|
|
|
|
|
LOG(LS_ERROR) << "Failed to initialize software-decoder fallback.";
|
2017-11-06 17:41:54 +01:00
|
|
|
use_hw_decoder_ = true;
|
2016-11-22 01:43:03 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (callback_)
|
|
|
|
|
fallback_decoder_->RegisterDecodeCompleteCallback(callback_);
|
2017-11-06 17:41:54 +01:00
|
|
|
use_hw_decoder_ = false;
|
2016-11-22 01:43:03 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::Decode(
|
|
|
|
|
const EncodedImage& input_image,
|
|
|
|
|
bool missing_frames,
|
|
|
|
|
const RTPFragmentationHeader* fragmentation,
|
|
|
|
|
const CodecSpecificInfo* codec_specific_info,
|
|
|
|
|
int64_t render_time_ms) {
|
2017-03-03 07:21:18 -08:00
|
|
|
TRACE_EVENT0("webrtc", "VideoDecoderSoftwareFallbackWrapper::Decode");
|
2017-02-13 17:11:08 -05:00
|
|
|
// Try initializing and decoding with the provided decoder on every keyframe
|
|
|
|
|
// or when there's no fallback decoder. This is the normal case.
|
2017-11-06 17:41:54 +01:00
|
|
|
if (use_hw_decoder_ || input_image._frameType == kVideoFrameKey) {
|
2017-02-13 17:11:08 -05:00
|
|
|
int32_t ret = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
|
|
|
|
|
// Try reinitializing the decoder if it had failed before.
|
2017-11-06 17:41:54 +01:00
|
|
|
if (!hw_decoder_initialized_) {
|
|
|
|
|
hw_decoder_initialized_ =
|
|
|
|
|
hw_decoder_->InitDecode(&codec_settings_, number_of_cores_) ==
|
2017-02-13 17:11:08 -05:00
|
|
|
WEBRTC_VIDEO_CODEC_OK;
|
|
|
|
|
}
|
2017-11-06 17:41:54 +01:00
|
|
|
if (hw_decoder_initialized_) {
|
|
|
|
|
ret = hw_decoder_->Decode(input_image, missing_frames, fragmentation,
|
2017-02-13 17:11:08 -05:00
|
|
|
codec_specific_info, render_time_ms);
|
|
|
|
|
}
|
2016-11-22 01:43:03 -08:00
|
|
|
if (ret == WEBRTC_VIDEO_CODEC_OK) {
|
2017-11-06 17:41:54 +01:00
|
|
|
if (!use_hw_decoder_) {
|
2016-11-22 01:43:03 -08:00
|
|
|
// Decode OK -> stop using fallback decoder.
|
2017-08-24 05:19:57 -07:00
|
|
|
LOG(LS_WARNING)
|
2017-02-13 17:11:08 -05:00
|
|
|
<< "Decode OK, no longer using the software fallback decoder.";
|
2017-11-06 17:41:54 +01:00
|
|
|
use_hw_decoder_ = true;
|
2016-11-22 01:43:03 -08:00
|
|
|
return WEBRTC_VIDEO_CODEC_OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ret != WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE)
|
|
|
|
|
return ret;
|
2017-11-06 17:41:54 +01:00
|
|
|
if (use_hw_decoder_) {
|
2016-11-22 01:43:03 -08:00
|
|
|
// Try to initialize fallback decoder.
|
|
|
|
|
if (!InitFallbackDecoder())
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fallback_decoder_->Decode(input_image, missing_frames, fragmentation,
|
|
|
|
|
codec_specific_info, render_time_ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::RegisterDecodeCompleteCallback(
|
|
|
|
|
DecodedImageCallback* callback) {
|
|
|
|
|
callback_ = callback;
|
2017-11-06 17:41:54 +01:00
|
|
|
int32_t ret = hw_decoder_->RegisterDecodeCompleteCallback(callback);
|
|
|
|
|
if (!use_hw_decoder_)
|
2016-11-22 01:43:03 -08:00
|
|
|
return fallback_decoder_->RegisterDecodeCompleteCallback(callback);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::Release() {
|
2017-11-06 17:41:54 +01:00
|
|
|
if (!use_hw_decoder_) {
|
2017-02-13 17:11:08 -05:00
|
|
|
LOG(LS_INFO) << "Releasing software fallback decoder.";
|
2016-11-22 01:43:03 -08:00
|
|
|
fallback_decoder_->Release();
|
2017-02-13 17:11:08 -05:00
|
|
|
}
|
2017-11-06 17:41:54 +01:00
|
|
|
hw_decoder_initialized_ = false;
|
|
|
|
|
return hw_decoder_->Release();
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VideoDecoderSoftwareFallbackWrapper::PrefersLateDecoding() const {
|
2017-11-06 17:41:54 +01:00
|
|
|
return use_hw_decoder_ ? hw_decoder_->PrefersLateDecoding()
|
|
|
|
|
: fallback_decoder_->PrefersLateDecoding();
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* VideoDecoderSoftwareFallbackWrapper::ImplementationName() const {
|
2017-11-06 17:41:54 +01:00
|
|
|
return use_hw_decoder_ ? hw_decoder_->ImplementationName()
|
|
|
|
|
: fallback_implementation_name_.c_str();
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|