blob: cd6eef9b6482d82a5a5e1ff92b42980911679779 [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
11#include "webrtc/media/engine/videodecodersoftwarefallbackwrapper.h"
12
13#include <string>
14
15#include "webrtc/base/logging.h"
magjeddd407022016-12-01 00:27:27 -080016#include "webrtc/media/engine/internaldecoderfactory.h"
magjedf6acc2a2016-11-22 01:43:03 -080017#include "webrtc/modules/video_coding/include/video_error_codes.h"
18
19namespace webrtc {
20
magjedf6acc2a2016-11-22 01:43:03 -080021VideoDecoderSoftwareFallbackWrapper::VideoDecoderSoftwareFallbackWrapper(
22 VideoCodecType codec_type,
23 VideoDecoder* decoder)
magjeddd407022016-12-01 00:27:27 -080024 : codec_type_(codec_type), decoder_(decoder), callback_(nullptr) {}
magjedf6acc2a2016-11-22 01:43:03 -080025
26int32_t VideoDecoderSoftwareFallbackWrapper::InitDecode(
27 const VideoCodec* codec_settings,
28 int32_t number_of_cores) {
29 codec_settings_ = *codec_settings;
30 number_of_cores_ = number_of_cores;
31 return decoder_->InitDecode(codec_settings, number_of_cores);
32}
33
34bool VideoDecoderSoftwareFallbackWrapper::InitFallbackDecoder() {
magjeddd407022016-12-01 00:27:27 -080035 RTC_CHECK(codec_type_ != kVideoCodecUnknown)
magjedf6acc2a2016-11-22 01:43:03 -080036 << "Decoder requesting fallback to codec not supported in software.";
37 LOG(LS_WARNING) << "Decoder falling back to software decoding.";
magjeddd407022016-12-01 00:27:27 -080038 cricket::InternalDecoderFactory internal_decoder_factory;
39 fallback_decoder_.reset(
40 internal_decoder_factory.CreateVideoDecoder(codec_type_));
magjedf6acc2a2016-11-22 01:43:03 -080041 if (fallback_decoder_->InitDecode(&codec_settings_, number_of_cores_) !=
42 WEBRTC_VIDEO_CODEC_OK) {
43 LOG(LS_ERROR) << "Failed to initialize software-decoder fallback.";
44 fallback_decoder_.reset();
45 return false;
46 }
47 if (callback_)
48 fallback_decoder_->RegisterDecodeCompleteCallback(callback_);
49 fallback_implementation_name_ =
50 std::string(fallback_decoder_->ImplementationName()) +
51 " (fallback from: " + decoder_->ImplementationName() + ")";
52 return true;
53}
54
55int32_t VideoDecoderSoftwareFallbackWrapper::Decode(
56 const EncodedImage& input_image,
57 bool missing_frames,
58 const RTPFragmentationHeader* fragmentation,
59 const CodecSpecificInfo* codec_specific_info,
60 int64_t render_time_ms) {
61 // Try decoding with the provided decoder on every keyframe or when there's no
62 // fallback decoder. This is the normal case.
63 if (!fallback_decoder_ || input_image._frameType == kVideoFrameKey) {
64 int32_t ret = decoder_->Decode(input_image, missing_frames, fragmentation,
65 codec_specific_info, render_time_ms);
66 if (ret == WEBRTC_VIDEO_CODEC_OK) {
67 if (fallback_decoder_) {
68 // Decode OK -> stop using fallback decoder.
69 fallback_decoder_->Release();
70 fallback_decoder_.reset();
71 return WEBRTC_VIDEO_CODEC_OK;
72 }
73 }
74 if (ret != WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE)
75 return ret;
76 if (!fallback_decoder_) {
77 // Try to initialize fallback decoder.
78 if (!InitFallbackDecoder())
79 return ret;
80 }
81 }
82 return fallback_decoder_->Decode(input_image, missing_frames, fragmentation,
83 codec_specific_info, render_time_ms);
84}
85
86int32_t VideoDecoderSoftwareFallbackWrapper::RegisterDecodeCompleteCallback(
87 DecodedImageCallback* callback) {
88 callback_ = callback;
89 int32_t ret = decoder_->RegisterDecodeCompleteCallback(callback);
90 if (fallback_decoder_)
91 return fallback_decoder_->RegisterDecodeCompleteCallback(callback);
92 return ret;
93}
94
95int32_t VideoDecoderSoftwareFallbackWrapper::Release() {
96 if (fallback_decoder_)
97 fallback_decoder_->Release();
98 return decoder_->Release();
99}
100
101bool VideoDecoderSoftwareFallbackWrapper::PrefersLateDecoding() const {
102 if (fallback_decoder_)
103 return fallback_decoder_->PrefersLateDecoding();
104 return decoder_->PrefersLateDecoding();
105}
106
107const char* VideoDecoderSoftwareFallbackWrapper::ImplementationName() const {
108 if (fallback_decoder_)
109 return fallback_implementation_name_.c_str();
110 return decoder_->ImplementationName();
111}
112
113} // namespace webrtc