blob: 9d6274d1a59f87648a13ab33a333dd5de17b27cf [file] [log] [blame]
magjedf6acc2a2016-11-22 01:43:03 -08001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/engine/videodecodersoftwarefallbackwrapper.h"
magjedf6acc2a2016-11-22 01:43:03 -080012
13#include <string>
Steve Antone78bcb92017-10-31 09:53:08 -070014#include <utility>
magjedf6acc2a2016-11-22 01:43:03 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/engine/internaldecoderfactory.h"
17#include "modules/video_coding/include/video_error_codes.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/trace_event.h"
magjedf6acc2a2016-11-22 01:43:03 -080021
22namespace webrtc {
23
magjedf6acc2a2016-11-22 01:43:03 -080024VideoDecoderSoftwareFallbackWrapper::VideoDecoderSoftwareFallbackWrapper(
25 VideoCodecType codec_type,
andersc063f0c02017-09-11 11:50:51 -070026 std::unique_ptr<VideoDecoder> decoder)
Peter Boströmf812e452017-02-13 17:11:08 -050027 : codec_type_(codec_type),
andersc063f0c02017-09-11 11:50:51 -070028 decoder_(std::move(decoder)),
Peter Boströmf812e452017-02-13 17:11:08 -050029 decoder_initialized_(false),
30 callback_(nullptr) {}
magjedf6acc2a2016-11-22 01:43:03 -080031
32int32_t VideoDecoderSoftwareFallbackWrapper::InitDecode(
33 const VideoCodec* codec_settings,
34 int32_t number_of_cores) {
Peter Boströmf812e452017-02-13 17:11:08 -050035 RTC_DCHECK(!fallback_decoder_) << "Fallback decoder should never be "
36 "initialized here, it should've been "
37 "released.";
magjedf6acc2a2016-11-22 01:43:03 -080038 codec_settings_ = *codec_settings;
39 number_of_cores_ = number_of_cores;
Peter Boströmf812e452017-02-13 17:11:08 -050040 int32_t ret = decoder_->InitDecode(codec_settings, number_of_cores);
Emircan Uysaler874d9a52017-10-31 17:15:22 -070041 if (ret == WEBRTC_VIDEO_CODEC_OK) {
42 decoder_initialized_ = true;
Peter Boströmf812e452017-02-13 17:11:08 -050043 return ret;
44 }
45 decoder_initialized_ = false;
46
47 // Try to initialize fallback decoder.
48 if (InitFallbackDecoder())
49 return WEBRTC_VIDEO_CODEC_OK;
50 return ret;
magjedf6acc2a2016-11-22 01:43:03 -080051}
52
53bool VideoDecoderSoftwareFallbackWrapper::InitFallbackDecoder() {
magjeddd407022016-12-01 00:27:27 -080054 RTC_CHECK(codec_type_ != kVideoCodecUnknown)
magjedf6acc2a2016-11-22 01:43:03 -080055 << "Decoder requesting fallback to codec not supported in software.";
56 LOG(LS_WARNING) << "Decoder falling back to software decoding.";
magjeddd407022016-12-01 00:27:27 -080057 cricket::InternalDecoderFactory internal_decoder_factory;
58 fallback_decoder_.reset(
59 internal_decoder_factory.CreateVideoDecoder(codec_type_));
magjedf6acc2a2016-11-22 01:43:03 -080060 if (fallback_decoder_->InitDecode(&codec_settings_, number_of_cores_) !=
61 WEBRTC_VIDEO_CODEC_OK) {
62 LOG(LS_ERROR) << "Failed to initialize software-decoder fallback.";
63 fallback_decoder_.reset();
64 return false;
65 }
66 if (callback_)
67 fallback_decoder_->RegisterDecodeCompleteCallback(callback_);
68 fallback_implementation_name_ =
69 std::string(fallback_decoder_->ImplementationName()) +
70 " (fallback from: " + decoder_->ImplementationName() + ")";
71 return true;
72}
73
74int32_t VideoDecoderSoftwareFallbackWrapper::Decode(
75 const EncodedImage& input_image,
76 bool missing_frames,
77 const RTPFragmentationHeader* fragmentation,
78 const CodecSpecificInfo* codec_specific_info,
79 int64_t render_time_ms) {
tommidb23ea62017-03-03 07:21:18 -080080 TRACE_EVENT0("webrtc", "VideoDecoderSoftwareFallbackWrapper::Decode");
Peter Boströmf812e452017-02-13 17:11:08 -050081 // Try initializing and decoding with the provided decoder on every keyframe
82 // or when there's no fallback decoder. This is the normal case.
magjedf6acc2a2016-11-22 01:43:03 -080083 if (!fallback_decoder_ || input_image._frameType == kVideoFrameKey) {
Peter Boströmf812e452017-02-13 17:11:08 -050084 int32_t ret = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
85 // Try reinitializing the decoder if it had failed before.
86 if (!decoder_initialized_) {
87 decoder_initialized_ =
88 decoder_->InitDecode(&codec_settings_, number_of_cores_) ==
89 WEBRTC_VIDEO_CODEC_OK;
90 }
91 if (decoder_initialized_) {
92 ret = decoder_->Decode(input_image, missing_frames, fragmentation,
93 codec_specific_info, render_time_ms);
94 }
magjedf6acc2a2016-11-22 01:43:03 -080095 if (ret == WEBRTC_VIDEO_CODEC_OK) {
96 if (fallback_decoder_) {
97 // Decode OK -> stop using fallback decoder.
brandtr955d7f12017-08-24 05:19:57 -070098 LOG(LS_WARNING)
Peter Boströmf812e452017-02-13 17:11:08 -050099 << "Decode OK, no longer using the software fallback decoder.";
magjedf6acc2a2016-11-22 01:43:03 -0800100 fallback_decoder_->Release();
101 fallback_decoder_.reset();
102 return WEBRTC_VIDEO_CODEC_OK;
103 }
104 }
105 if (ret != WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE)
106 return ret;
107 if (!fallback_decoder_) {
108 // Try to initialize fallback decoder.
109 if (!InitFallbackDecoder())
110 return ret;
111 }
112 }
113 return fallback_decoder_->Decode(input_image, missing_frames, fragmentation,
114 codec_specific_info, render_time_ms);
115}
116
117int32_t VideoDecoderSoftwareFallbackWrapper::RegisterDecodeCompleteCallback(
118 DecodedImageCallback* callback) {
119 callback_ = callback;
120 int32_t ret = decoder_->RegisterDecodeCompleteCallback(callback);
121 if (fallback_decoder_)
122 return fallback_decoder_->RegisterDecodeCompleteCallback(callback);
123 return ret;
124}
125
126int32_t VideoDecoderSoftwareFallbackWrapper::Release() {
Peter Boströmf812e452017-02-13 17:11:08 -0500127 if (fallback_decoder_) {
128 LOG(LS_INFO) << "Releasing software fallback decoder.";
magjedf6acc2a2016-11-22 01:43:03 -0800129 fallback_decoder_->Release();
Peter Boströmf812e452017-02-13 17:11:08 -0500130 fallback_decoder_.reset();
131 }
132 decoder_initialized_ = false;
magjedf6acc2a2016-11-22 01:43:03 -0800133 return decoder_->Release();
134}
135
136bool VideoDecoderSoftwareFallbackWrapper::PrefersLateDecoding() const {
137 if (fallback_decoder_)
138 return fallback_decoder_->PrefersLateDecoding();
139 return decoder_->PrefersLateDecoding();
140}
141
magjedf6acc2a2016-11-22 01:43:03 -0800142const char* VideoDecoderSoftwareFallbackWrapper::ImplementationName() const {
143 if (fallback_decoder_)
144 return fallback_implementation_name_.c_str();
145 return decoder_->ImplementationName();
146}
147
148} // namespace webrtc