OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 case kVideoCodecVP9: | 43 case kVideoCodecVP9: |
44 return VideoEncoder::kVp9; | 44 return VideoEncoder::kVp9; |
45 default: | 45 default: |
46 return VideoEncoder::kUnsupportedCodec; | 46 return VideoEncoder::kUnsupportedCodec; |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper( | 50 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper( |
51 VideoCodecType codec_type, | 51 VideoCodecType codec_type, |
52 webrtc::VideoEncoder* encoder) | 52 webrtc::VideoEncoder* encoder) |
53 : encoder_type_(CodecToEncoderType(codec_type)), | 53 : rates_set_(false), |
| 54 channel_parameters_set_(false), |
| 55 encoder_type_(CodecToEncoderType(codec_type)), |
54 encoder_(encoder), | 56 encoder_(encoder), |
55 callback_(nullptr) { | 57 callback_(nullptr) {} |
| 58 |
| 59 bool VideoEncoderSoftwareFallbackWrapper::InitFallbackEncoder() { |
| 60 RTC_CHECK(encoder_type_ != kUnsupportedCodec) |
| 61 << "Encoder requesting fallback to codec not supported in software."; |
| 62 fallback_encoder_.reset(VideoEncoder::Create(encoder_type_)); |
| 63 if (fallback_encoder_->InitEncode(&codec_settings_, number_of_cores_, |
| 64 max_payload_size_) != |
| 65 WEBRTC_VIDEO_CODEC_OK) { |
| 66 LOG(LS_ERROR) << "Failed to initialize software-encoder fallback."; |
| 67 fallback_encoder_->Release(); |
| 68 fallback_encoder_.reset(); |
| 69 return false; |
| 70 } |
| 71 // Replay callback, rates, and channel parameters. |
| 72 if (callback_) |
| 73 fallback_encoder_->RegisterEncodeCompleteCallback(callback_); |
| 74 if (rates_set_) |
| 75 fallback_encoder_->SetRates(bitrate_, framerate_); |
| 76 if (channel_parameters_set_) |
| 77 fallback_encoder_->SetChannelParameters(packet_loss_, rtt_); |
| 78 |
| 79 // Since we're switching to the fallback encoder, Release the real encoder. It |
| 80 // may be re-initialized via InitEncode later, and it will continue to get |
| 81 // Set calls for rates and channel parameters in the meantime. |
| 82 encoder_->Release(); |
| 83 return true; |
56 } | 84 } |
57 | 85 |
58 int32_t VideoEncoderSoftwareFallbackWrapper::InitEncode( | 86 int32_t VideoEncoderSoftwareFallbackWrapper::InitEncode( |
59 const VideoCodec* codec_settings, | 87 const VideoCodec* codec_settings, |
60 int32_t number_of_cores, | 88 int32_t number_of_cores, |
61 size_t max_payload_size) { | 89 size_t max_payload_size) { |
| 90 // Store settings, in case we need to dynamically switch to the fallback |
| 91 // encoder after a failed Encode call. |
| 92 codec_settings_ = *codec_settings; |
| 93 number_of_cores_ = number_of_cores; |
| 94 max_payload_size_ = max_payload_size; |
| 95 // Clear stored rate/channel parameters. |
| 96 rates_set_ = false; |
| 97 channel_parameters_set_ = false; |
| 98 |
62 int32_t ret = | 99 int32_t ret = |
63 encoder_->InitEncode(codec_settings, number_of_cores, max_payload_size); | 100 encoder_->InitEncode(codec_settings, number_of_cores, max_payload_size); |
64 if (ret == WEBRTC_VIDEO_CODEC_OK || encoder_type_ == kUnsupportedCodec) { | 101 if (ret == WEBRTC_VIDEO_CODEC_OK || encoder_type_ == kUnsupportedCodec) { |
| 102 if (fallback_encoder_) |
| 103 fallback_encoder_->Release(); |
65 fallback_encoder_.reset(); | 104 fallback_encoder_.reset(); |
66 if (callback_) | 105 if (callback_) |
67 encoder_->RegisterEncodeCompleteCallback(callback_); | 106 encoder_->RegisterEncodeCompleteCallback(callback_); |
68 return ret; | 107 return ret; |
69 } | 108 } |
70 // Try to instantiate software codec. | 109 // Try to instantiate software codec. |
71 fallback_encoder_.reset(VideoEncoder::Create(encoder_type_)); | 110 if (InitFallbackEncoder()) { |
72 if (fallback_encoder_->InitEncode(codec_settings, number_of_cores, | |
73 max_payload_size) == | |
74 WEBRTC_VIDEO_CODEC_OK) { | |
75 if (callback_) | |
76 fallback_encoder_->RegisterEncodeCompleteCallback(callback_); | |
77 return WEBRTC_VIDEO_CODEC_OK; | 111 return WEBRTC_VIDEO_CODEC_OK; |
78 } | 112 } |
79 // Software encoder failed, reset and use original return code. | 113 // Software encoder failed, use original return code. |
80 fallback_encoder_.reset(); | |
81 return ret; | 114 return ret; |
82 } | 115 } |
83 | 116 |
84 int32_t VideoEncoderSoftwareFallbackWrapper::RegisterEncodeCompleteCallback( | 117 int32_t VideoEncoderSoftwareFallbackWrapper::RegisterEncodeCompleteCallback( |
85 EncodedImageCallback* callback) { | 118 EncodedImageCallback* callback) { |
86 callback_ = callback; | 119 callback_ = callback; |
87 int32_t ret = encoder_->RegisterEncodeCompleteCallback(callback); | 120 int32_t ret = encoder_->RegisterEncodeCompleteCallback(callback); |
88 if (fallback_encoder_) | 121 if (fallback_encoder_) |
89 return fallback_encoder_->RegisterEncodeCompleteCallback(callback); | 122 return fallback_encoder_->RegisterEncodeCompleteCallback(callback); |
90 return ret; | 123 return ret; |
91 } | 124 } |
92 | 125 |
93 int32_t VideoEncoderSoftwareFallbackWrapper::Release() { | 126 int32_t VideoEncoderSoftwareFallbackWrapper::Release() { |
| 127 // If the fallback_encoder_ is non-null, it means it was created via |
| 128 // InitFallbackEncoder which has Release()d encoder_, so we should only ever |
| 129 // need to Release() whichever one is active. |
94 if (fallback_encoder_) | 130 if (fallback_encoder_) |
95 return fallback_encoder_->Release(); | 131 return fallback_encoder_->Release(); |
96 return encoder_->Release(); | 132 return encoder_->Release(); |
97 } | 133 } |
98 | 134 |
99 int32_t VideoEncoderSoftwareFallbackWrapper::Encode( | 135 int32_t VideoEncoderSoftwareFallbackWrapper::Encode( |
100 const VideoFrame& frame, | 136 const VideoFrame& frame, |
101 const CodecSpecificInfo* codec_specific_info, | 137 const CodecSpecificInfo* codec_specific_info, |
102 const std::vector<FrameType>* frame_types) { | 138 const std::vector<FrameType>* frame_types) { |
103 if (fallback_encoder_) | 139 if (fallback_encoder_) |
104 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); | 140 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); |
105 return encoder_->Encode(frame, codec_specific_info, frame_types); | 141 int32_t ret = encoder_->Encode(frame, codec_specific_info, frame_types); |
| 142 // If requested, try a software fallback. |
| 143 if (ret == WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE && InitFallbackEncoder()) { |
| 144 // Fallback was successful, so start using it with this frame. |
| 145 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); |
| 146 } |
| 147 return ret; |
106 } | 148 } |
107 | 149 |
108 int32_t VideoEncoderSoftwareFallbackWrapper::SetChannelParameters( | 150 int32_t VideoEncoderSoftwareFallbackWrapper::SetChannelParameters( |
109 uint32_t packet_loss, | 151 uint32_t packet_loss, |
110 int64_t rtt) { | 152 int64_t rtt) { |
| 153 channel_parameters_set_ = true; |
| 154 packet_loss_ = packet_loss; |
| 155 rtt_ = rtt; |
111 int32_t ret = encoder_->SetChannelParameters(packet_loss, rtt); | 156 int32_t ret = encoder_->SetChannelParameters(packet_loss, rtt); |
112 if (fallback_encoder_) | 157 if (fallback_encoder_) |
113 return fallback_encoder_->SetChannelParameters(packet_loss, rtt); | 158 return fallback_encoder_->SetChannelParameters(packet_loss, rtt); |
114 return ret; | 159 return ret; |
115 } | 160 } |
116 | 161 |
117 int32_t VideoEncoderSoftwareFallbackWrapper::SetRates(uint32_t bitrate, | 162 int32_t VideoEncoderSoftwareFallbackWrapper::SetRates(uint32_t bitrate, |
118 uint32_t framerate) { | 163 uint32_t framerate) { |
| 164 rates_set_ = true; |
| 165 bitrate_ = bitrate; |
| 166 framerate_ = framerate; |
119 int32_t ret = encoder_->SetRates(bitrate, framerate); | 167 int32_t ret = encoder_->SetRates(bitrate, framerate); |
120 if (fallback_encoder_) | 168 if (fallback_encoder_) |
121 return fallback_encoder_->SetRates(bitrate, framerate); | 169 return fallback_encoder_->SetRates(bitrate, framerate); |
122 return ret; | 170 return ret; |
123 } | 171 } |
124 | 172 |
125 void VideoEncoderSoftwareFallbackWrapper::OnDroppedFrame() { | 173 void VideoEncoderSoftwareFallbackWrapper::OnDroppedFrame() { |
126 if (fallback_encoder_) | 174 if (fallback_encoder_) |
127 return fallback_encoder_->OnDroppedFrame(); | 175 return fallback_encoder_->OnDroppedFrame(); |
128 return encoder_->OnDroppedFrame(); | 176 return encoder_->OnDroppedFrame(); |
129 } | 177 } |
130 | 178 |
131 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const { | 179 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const { |
132 if (fallback_encoder_) | 180 if (fallback_encoder_) |
133 return fallback_encoder_->SupportsNativeHandle(); | 181 return fallback_encoder_->SupportsNativeHandle(); |
134 return encoder_->SupportsNativeHandle(); | 182 return encoder_->SupportsNativeHandle(); |
135 } | 183 } |
136 | 184 |
137 int VideoEncoderSoftwareFallbackWrapper::GetTargetFramerate() { | 185 int VideoEncoderSoftwareFallbackWrapper::GetTargetFramerate() { |
138 if (fallback_encoder_) | 186 if (fallback_encoder_) |
139 return fallback_encoder_->GetTargetFramerate(); | 187 return fallback_encoder_->GetTargetFramerate(); |
140 return encoder_->GetTargetFramerate(); | 188 return encoder_->GetTargetFramerate(); |
141 } | 189 } |
142 | 190 |
143 } // namespace webrtc | 191 } // namespace webrtc |
OLD | NEW |