Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: webrtc/video/video_encoder.cc

Issue 2263043003: Make MediaCodecEncoder fallback to a software encoder on failure. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Changes according to Stefan's comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/api/android/jni/androidmediaencoder_jni.cc ('k') | webrtc/video_encoder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "webrtc/video_encoder.h" 11 #include "webrtc/video_encoder.h"
12 12
13 #include "webrtc/base/checks.h" 13 #include "webrtc/base/checks.h"
14 #include "webrtc/base/logging.h" 14 #include "webrtc/base/logging.h"
15 #include "webrtc/modules/video_coding/codecs/h264/include/h264.h" 15 #include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
16 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" 16 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
17 #include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h" 17 #include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
18 18
19 namespace webrtc { 19 namespace webrtc {
20 VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) { 20 VideoEncoder* VideoEncoder::Create(VideoEncoder::EncoderType codec_type) {
21 RTC_DCHECK(IsSupportedSoftware(codec_type));
21 switch (codec_type) { 22 switch (codec_type) {
22 case kH264: 23 case kH264:
23 RTC_DCHECK(H264Encoder::IsSupported());
24 return H264Encoder::Create(); 24 return H264Encoder::Create();
25 case kVp8: 25 case kVp8:
26 return VP8Encoder::Create(); 26 return VP8Encoder::Create();
27 case kVp9: 27 case kVp9:
28 RTC_DCHECK(VP9Encoder::IsSupported());
29 return VP9Encoder::Create(); 28 return VP9Encoder::Create();
30 case kUnsupportedCodec: 29 case kUnsupportedCodec:
31 RTC_NOTREACHED(); 30 RTC_NOTREACHED();
32 return nullptr; 31 return nullptr;
33 } 32 }
34 RTC_NOTREACHED(); 33 RTC_NOTREACHED();
35 return nullptr; 34 return nullptr;
36 } 35 }
37 36
38 VideoEncoder::EncoderType CodecToEncoderType(VideoCodecType codec_type) { 37 bool VideoEncoder::IsSupportedSoftware(EncoderType codec_type) {
38 switch (codec_type) {
39 case kH264:
40 return H264Encoder::IsSupported();
41 case kVp8:
42 return true;
43 case kVp9:
44 return VP9Encoder::IsSupported();
45 case kUnsupportedCodec:
46 RTC_NOTREACHED();
47 return false;
48 }
49 RTC_NOTREACHED();
50 return false;
51 }
52
53 VideoEncoder::EncoderType VideoEncoder::CodecToEncoderType(
54 VideoCodecType codec_type) {
39 switch (codec_type) { 55 switch (codec_type) {
40 case kVideoCodecH264: 56 case kVideoCodecH264:
41 return VideoEncoder::kH264; 57 return VideoEncoder::kH264;
42 case kVideoCodecVP8: 58 case kVideoCodecVP8:
43 return VideoEncoder::kVp8; 59 return VideoEncoder::kVp8;
44 case kVideoCodecVP9: 60 case kVideoCodecVP9:
45 return VideoEncoder::kVp9; 61 return VideoEncoder::kVp9;
46 default: 62 default:
47 return VideoEncoder::kUnsupportedCodec; 63 return VideoEncoder::kUnsupportedCodec;
48 } 64 }
49 } 65 }
50 66
51 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper( 67 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper(
52 VideoCodecType codec_type, 68 VideoCodecType codec_type,
53 webrtc::VideoEncoder* encoder) 69 webrtc::VideoEncoder* encoder)
54 : rates_set_(false), 70 : rates_set_(false),
55 channel_parameters_set_(false), 71 channel_parameters_set_(false),
56 encoder_type_(CodecToEncoderType(codec_type)), 72 encoder_type_(CodecToEncoderType(codec_type)),
57 encoder_(encoder), 73 encoder_(encoder),
58 callback_(nullptr) {} 74 callback_(nullptr) {}
59 75
60 bool VideoEncoderSoftwareFallbackWrapper::InitFallbackEncoder() { 76 bool VideoEncoderSoftwareFallbackWrapper::InitFallbackEncoder() {
61 RTC_CHECK(encoder_type_ != kUnsupportedCodec) 77 if (!VideoEncoder::IsSupportedSoftware(encoder_type_)) {
62 << "Encoder requesting fallback to codec not supported in software."; 78 LOG(LS_WARNING)
79 << "Encoder requesting fallback to codec not supported in software.";
80 return false;
81 }
63 fallback_encoder_.reset(VideoEncoder::Create(encoder_type_)); 82 fallback_encoder_.reset(VideoEncoder::Create(encoder_type_));
64 if (fallback_encoder_->InitEncode(&codec_settings_, number_of_cores_, 83 if (fallback_encoder_->InitEncode(&codec_settings_, number_of_cores_,
65 max_payload_size_) != 84 max_payload_size_) !=
66 WEBRTC_VIDEO_CODEC_OK) { 85 WEBRTC_VIDEO_CODEC_OK) {
67 LOG(LS_ERROR) << "Failed to initialize software-encoder fallback."; 86 LOG(LS_ERROR) << "Failed to initialize software-encoder fallback.";
68 fallback_encoder_->Release(); 87 fallback_encoder_->Release();
69 fallback_encoder_.reset(); 88 fallback_encoder_.reset();
70 return false; 89 return false;
71 } 90 }
72 // Replay callback, rates, and channel parameters. 91 // Replay callback, rates, and channel parameters.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 157
139 int32_t VideoEncoderSoftwareFallbackWrapper::Encode( 158 int32_t VideoEncoderSoftwareFallbackWrapper::Encode(
140 const VideoFrame& frame, 159 const VideoFrame& frame,
141 const CodecSpecificInfo* codec_specific_info, 160 const CodecSpecificInfo* codec_specific_info,
142 const std::vector<FrameType>* frame_types) { 161 const std::vector<FrameType>* frame_types) {
143 if (fallback_encoder_) 162 if (fallback_encoder_)
144 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); 163 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types);
145 int32_t ret = encoder_->Encode(frame, codec_specific_info, frame_types); 164 int32_t ret = encoder_->Encode(frame, codec_specific_info, frame_types);
146 // If requested, try a software fallback. 165 // If requested, try a software fallback.
147 if (ret == WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE && InitFallbackEncoder()) { 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
148 // Fallback was successful, so start using it with this frame. 174 // Fallback was successful, so start using it with this frame.
149 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types); 175 return fallback_encoder_->Encode(frame, codec_specific_info, frame_types);
150 } 176 }
151 return ret; 177 return ret;
152 } 178 }
153 179
154 int32_t VideoEncoderSoftwareFallbackWrapper::SetChannelParameters( 180 int32_t VideoEncoderSoftwareFallbackWrapper::SetChannelParameters(
155 uint32_t packet_loss, 181 uint32_t packet_loss,
156 int64_t rtt) { 182 int64_t rtt) {
157 channel_parameters_set_ = true; 183 channel_parameters_set_ = true;
(...skipping 22 matching lines...) Expand all
180 return encoder_->OnDroppedFrame(); 206 return encoder_->OnDroppedFrame();
181 } 207 }
182 208
183 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const { 209 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const {
184 if (fallback_encoder_) 210 if (fallback_encoder_)
185 return fallback_encoder_->SupportsNativeHandle(); 211 return fallback_encoder_->SupportsNativeHandle();
186 return encoder_->SupportsNativeHandle(); 212 return encoder_->SupportsNativeHandle();
187 } 213 }
188 214
189 } // namespace webrtc 215 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/api/android/jni/androidmediaencoder_jni.cc ('k') | webrtc/video_encoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698