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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 return VideoEncoder::kH264; | 57 return VideoEncoder::kH264; |
58 case kVideoCodecVP8: | 58 case kVideoCodecVP8: |
59 return VideoEncoder::kVp8; | 59 return VideoEncoder::kVp8; |
60 case kVideoCodecVP9: | 60 case kVideoCodecVP9: |
61 return VideoEncoder::kVp9; | 61 return VideoEncoder::kVp9; |
62 default: | 62 default: |
63 return VideoEncoder::kUnsupportedCodec; | 63 return VideoEncoder::kUnsupportedCodec; |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper( | |
68 VideoCodecType codec_type, | |
69 webrtc::VideoEncoder* encoder) | |
70 : rates_set_(false), | |
71 channel_parameters_set_(false), | |
72 encoder_type_(CodecToEncoderType(codec_type)), | |
73 encoder_(encoder), | |
74 callback_(nullptr) {} | |
75 | |
76 bool VideoEncoderSoftwareFallbackWrapper::InitFallbackEncoder() { | |
77 if (!VideoEncoder::IsSupportedSoftware(encoder_type_)) { | |
78 LOG(LS_WARNING) | |
79 << "Encoder requesting fallback to codec not supported in software."; | |
80 return false; | |
81 } | |
82 fallback_encoder_.reset(VideoEncoder::Create(encoder_type_)); | |
83 if (fallback_encoder_->InitEncode(&codec_settings_, number_of_cores_, | |
84 max_payload_size_) != | |
85 WEBRTC_VIDEO_CODEC_OK) { | |
86 LOG(LS_ERROR) << "Failed to initialize software-encoder fallback."; | |
87 fallback_encoder_->Release(); | |
88 fallback_encoder_.reset(); | |
89 return false; | |
90 } | |
91 // Replay callback, rates, and channel parameters. | |
92 if (callback_) | |
93 fallback_encoder_->RegisterEncodeCompleteCallback(callback_); | |
94 if (rates_set_) | |
95 fallback_encoder_->SetRates(bitrate_, framerate_); | |
96 if (channel_parameters_set_) | |
97 fallback_encoder_->SetChannelParameters(packet_loss_, rtt_); | |
98 | |
99 fallback_implementation_name_ = | |
100 std::string(fallback_encoder_->ImplementationName()) + | |
101 " (fallback from: " + encoder_->ImplementationName() + ")"; | |
102 // Since we're switching to the fallback encoder, Release the real encoder. It | |
103 // may be re-initialized via InitEncode later, and it will continue to get | |
104 // Set calls for rates and channel parameters in the meantime. | |
105 encoder_->Release(); | |
106 return true; | |
107 } | |
108 | |
109 int32_t VideoEncoderSoftwareFallbackWrapper::InitEncode( | |
110 const VideoCodec* codec_settings, | |
111 int32_t number_of_cores, | |
112 size_t max_payload_size) { | |
113 // Store settings, in case we need to dynamically switch to the fallback | |
114 // encoder after a failed Encode call. | |
115 codec_settings_ = *codec_settings; | |
116 number_of_cores_ = number_of_cores; | |
117 max_payload_size_ = max_payload_size; | |
118 // Clear stored rate/channel parameters. | |
119 rates_set_ = false; | |
120 channel_parameters_set_ = false; | |
121 | |
122 int32_t ret = | |
123 encoder_->InitEncode(codec_settings, number_of_cores, max_payload_size); | |
124 if (ret == WEBRTC_VIDEO_CODEC_OK || encoder_type_ == kUnsupportedCodec) { | |
125 if (fallback_encoder_) | |
126 fallback_encoder_->Release(); | |
127 fallback_encoder_.reset(); | |
128 if (callback_) | |
129 encoder_->RegisterEncodeCompleteCallback(callback_); | |
130 return ret; | |
131 } | |
132 // Try to instantiate software codec. | |
133 if (InitFallbackEncoder()) { | |
134 return WEBRTC_VIDEO_CODEC_OK; | |
135 } | |
136 // Software encoder failed, use original return code. | |
137 return ret; | |
138 } | |
139 | |
140 int32_t VideoEncoderSoftwareFallbackWrapper::RegisterEncodeCompleteCallback( | |
141 EncodedImageCallback* callback) { | |
142 callback_ = callback; | |
143 int32_t ret = encoder_->RegisterEncodeCompleteCallback(callback); | |
144 if (fallback_encoder_) | |
145 return fallback_encoder_->RegisterEncodeCompleteCallback(callback); | |
146 return ret; | |
147 } | |
148 | |
149 int32_t VideoEncoderSoftwareFallbackWrapper::Release() { | |
150 // If the fallback_encoder_ is non-null, it means it was created via | |
151 // InitFallbackEncoder which has Release()d encoder_, so we should only ever | |
152 // need to Release() whichever one is active. | |
153 if (fallback_encoder_) | |
154 return fallback_encoder_->Release(); | |
155 return encoder_->Release(); | |
156 } | |
157 | |
158 int32_t VideoEncoderSoftwareFallbackWrapper::Encode( | |
159 const VideoFrame& frame, | |
160 const CodecSpecificInfo* codec_specific_info, | |
161 const std::vector<FrameType>* frame_types) { | |
162 if (fallback_encoder_) | |
163 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); | |
164 int32_t ret = encoder_->Encode(frame, codec_specific_info, frame_types); | |
165 // If requested, try a software fallback. | |
166 if (ret == WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE && InitFallbackEncoder()) { | |
167 if (frame.video_frame_buffer()->native_handle() && | |
168 !fallback_encoder_->SupportsNativeHandle()) { | |
169 LOG(LS_WARNING) << "Fallback encoder doesn't support native frames, " | |
170 << "dropping one frame."; | |
171 return WEBRTC_VIDEO_CODEC_ERROR; | |
172 } | |
173 | |
174 // Fallback was successful, so start using it with this frame. | |
175 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); | |
176 } | |
177 return ret; | |
178 } | |
179 | |
180 int32_t VideoEncoderSoftwareFallbackWrapper::SetChannelParameters( | |
181 uint32_t packet_loss, | |
182 int64_t rtt) { | |
183 channel_parameters_set_ = true; | |
184 packet_loss_ = packet_loss; | |
185 rtt_ = rtt; | |
186 int32_t ret = encoder_->SetChannelParameters(packet_loss, rtt); | |
187 if (fallback_encoder_) | |
188 return fallback_encoder_->SetChannelParameters(packet_loss, rtt); | |
189 return ret; | |
190 } | |
191 | |
192 int32_t VideoEncoderSoftwareFallbackWrapper::SetRates(uint32_t bitrate, | |
193 uint32_t framerate) { | |
194 rates_set_ = true; | |
195 bitrate_ = bitrate; | |
196 framerate_ = framerate; | |
197 int32_t ret = encoder_->SetRates(bitrate, framerate); | |
198 if (fallback_encoder_) | |
199 return fallback_encoder_->SetRates(bitrate, framerate); | |
200 return ret; | |
201 } | |
202 | |
203 void VideoEncoderSoftwareFallbackWrapper::OnDroppedFrame() { | |
204 if (fallback_encoder_) | |
205 return fallback_encoder_->OnDroppedFrame(); | |
206 return encoder_->OnDroppedFrame(); | |
207 } | |
208 | |
209 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const { | |
210 if (fallback_encoder_) | |
211 return fallback_encoder_->SupportsNativeHandle(); | |
212 return encoder_->SupportsNativeHandle(); | |
213 } | |
214 | |
215 } // namespace webrtc | 67 } // namespace webrtc |
OLD | NEW |