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 "modules/video_coding/include/video_error_codes.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2018-02-23 09:05:49 +01:00
|
|
|
#include "rtc_base/system/fallthrough.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/trace_event.h"
|
2016-11-22 01:43:03 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
VideoDecoderSoftwareFallbackWrapper::VideoDecoderSoftwareFallbackWrapper(
|
2017-11-09 13:43:42 +01:00
|
|
|
std::unique_ptr<VideoDecoder> sw_fallback_decoder,
|
|
|
|
|
std::unique_ptr<VideoDecoder> hw_decoder)
|
2018-02-23 09:05:49 +01:00
|
|
|
: decoder_type_(DecoderType::kNone),
|
2017-11-09 13:43:42 +01:00
|
|
|
hw_decoder_(std::move(hw_decoder)),
|
|
|
|
|
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) {}
|
2018-04-05 11:42:24 +02:00
|
|
|
VideoDecoderSoftwareFallbackWrapper::~VideoDecoderSoftwareFallbackWrapper() =
|
|
|
|
|
default;
|
2016-11-22 01:43:03 -08:00
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::InitDecode(
|
|
|
|
|
const VideoCodec* codec_settings,
|
|
|
|
|
int32_t number_of_cores) {
|
|
|
|
|
codec_settings_ = *codec_settings;
|
|
|
|
|
number_of_cores_ = number_of_cores;
|
2018-02-23 09:05:49 +01:00
|
|
|
|
|
|
|
|
int32_t status = InitHwDecoder();
|
|
|
|
|
if (status == WEBRTC_VIDEO_CODEC_OK) {
|
|
|
|
|
return WEBRTC_VIDEO_CODEC_OK;
|
2017-02-13 17:11:08 -05:00
|
|
|
}
|
|
|
|
|
|
2018-02-23 09:05:49 +01:00
|
|
|
RTC_DCHECK(decoder_type_ == DecoderType::kNone);
|
|
|
|
|
if (InitFallbackDecoder()) {
|
2017-02-13 17:11:08 -05:00
|
|
|
return WEBRTC_VIDEO_CODEC_OK;
|
2018-02-23 09:05:49 +01:00
|
|
|
}
|
2017-11-09 13:43:42 +01:00
|
|
|
|
2018-02-23 09:05:49 +01:00
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::InitHwDecoder() {
|
|
|
|
|
RTC_DCHECK(decoder_type_ == DecoderType::kNone);
|
|
|
|
|
int32_t status = hw_decoder_->InitDecode(&codec_settings_, number_of_cores_);
|
|
|
|
|
if (status != WEBRTC_VIDEO_CODEC_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decoder_type_ = DecoderType::kHardware;
|
|
|
|
|
if (callback_)
|
|
|
|
|
hw_decoder_->RegisterDecodeCompleteCallback(callback_);
|
|
|
|
|
return status;
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VideoDecoderSoftwareFallbackWrapper::InitFallbackDecoder() {
|
2018-02-23 09:05:49 +01:00
|
|
|
RTC_DCHECK(decoder_type_ == DecoderType::kNone ||
|
|
|
|
|
decoder_type_ == DecoderType::kHardware);
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "Decoder falling back to software decoding.";
|
2018-02-23 09:05:49 +01:00
|
|
|
int32_t status =
|
|
|
|
|
fallback_decoder_->InitDecode(&codec_settings_, number_of_cores_);
|
|
|
|
|
if (status != WEBRTC_VIDEO_CODEC_OK) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Failed to initialize software-decoder fallback.";
|
2016-11-22 01:43:03 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-23 09:05:49 +01:00
|
|
|
|
|
|
|
|
if (decoder_type_ == DecoderType::kHardware) {
|
|
|
|
|
hw_decoder_->Release();
|
|
|
|
|
}
|
|
|
|
|
decoder_type_ = DecoderType::kFallback;
|
|
|
|
|
|
2016-11-22 01:43:03 -08:00
|
|
|
if (callback_)
|
|
|
|
|
fallback_decoder_->RegisterDecodeCompleteCallback(callback_);
|
|
|
|
|
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) {
|
2018-02-23 09:05:49 +01:00
|
|
|
TRACE_EVENT0("webrtc", "VideoDecoderSoftwareFallbackWrapper::Decode");
|
|
|
|
|
switch (decoder_type_) {
|
|
|
|
|
case DecoderType::kNone:
|
|
|
|
|
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
|
|
|
|
|
case DecoderType::kHardware: {
|
|
|
|
|
int32_t ret = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
|
2017-11-09 13:43:42 +01:00
|
|
|
ret = hw_decoder_->Decode(input_image, missing_frames, fragmentation,
|
2018-02-23 09:05:49 +01:00
|
|
|
codec_specific_info, render_time_ms);
|
|
|
|
|
if (ret != WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE) {
|
|
|
|
|
return ret;
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
2018-02-23 09:05:49 +01:00
|
|
|
|
|
|
|
|
// HW decoder returned WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE or
|
|
|
|
|
// initialization failed, fallback to software.
|
|
|
|
|
if (!InitFallbackDecoder()) {
|
2016-11-22 01:43:03 -08:00
|
|
|
return ret;
|
2018-02-23 09:05:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback decoder initialized, fall-through.
|
|
|
|
|
RTC_FALLTHROUGH();
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
2018-02-23 09:05:49 +01:00
|
|
|
case DecoderType::kFallback:
|
|
|
|
|
return fallback_decoder_->Decode(input_image, missing_frames,
|
|
|
|
|
fragmentation, codec_specific_info,
|
|
|
|
|
render_time_ms);
|
|
|
|
|
default:
|
|
|
|
|
RTC_NOTREACHED();
|
|
|
|
|
return WEBRTC_VIDEO_CODEC_ERROR;
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::RegisterDecodeCompleteCallback(
|
|
|
|
|
DecodedImageCallback* callback) {
|
|
|
|
|
callback_ = callback;
|
2018-02-23 09:05:49 +01:00
|
|
|
return active_decoder().RegisterDecodeCompleteCallback(callback);
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t VideoDecoderSoftwareFallbackWrapper::Release() {
|
2018-02-23 09:05:49 +01:00
|
|
|
int32_t status;
|
|
|
|
|
switch (decoder_type_) {
|
|
|
|
|
case DecoderType::kHardware:
|
|
|
|
|
status = hw_decoder_->Release();
|
|
|
|
|
break;
|
|
|
|
|
case DecoderType::kFallback:
|
|
|
|
|
RTC_LOG(LS_INFO) << "Releasing software fallback decoder.";
|
|
|
|
|
status = fallback_decoder_->Release();
|
|
|
|
|
break;
|
|
|
|
|
case DecoderType::kNone:
|
|
|
|
|
status = WEBRTC_VIDEO_CODEC_OK;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
RTC_NOTREACHED();
|
|
|
|
|
status = WEBRTC_VIDEO_CODEC_ERROR;
|
2017-02-13 17:11:08 -05:00
|
|
|
}
|
2018-02-23 09:05:49 +01:00
|
|
|
|
|
|
|
|
decoder_type_ = DecoderType::kNone;
|
|
|
|
|
return status;
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VideoDecoderSoftwareFallbackWrapper::PrefersLateDecoding() const {
|
2018-02-23 09:05:49 +01:00
|
|
|
return active_decoder().PrefersLateDecoding();
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* VideoDecoderSoftwareFallbackWrapper::ImplementationName() const {
|
2018-02-23 09:05:49 +01:00
|
|
|
return decoder_type_ == DecoderType::kFallback
|
|
|
|
|
? fallback_implementation_name_.c_str()
|
|
|
|
|
: hw_decoder_->ImplementationName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoDecoder& VideoDecoderSoftwareFallbackWrapper::active_decoder() const {
|
|
|
|
|
return decoder_type_ == DecoderType::kFallback ? *fallback_decoder_
|
|
|
|
|
: *hw_decoder_;
|
2016-11-22 01:43:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|