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 |
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(const cricket::VideoCodec& codec) { |
21 RTC_DCHECK(IsSupportedSoftware(codec_type)); | 21 RTC_DCHECK(IsSupportedSoftware(codec)); |
22 switch (codec_type) { | 22 switch (cricket::CodecTypeFromName(codec.name)) { |
23 case kH264: | 23 case kVideoCodecH264: |
24 return H264Encoder::Create(); | 24 return H264Encoder::Create(codec); |
25 case kVp8: | 25 case kVideoCodecVP8: |
26 return VP8Encoder::Create(); | 26 return VP8Encoder::Create(); |
27 case kVp9: | 27 case kVideoCodecVP9: |
28 return VP9Encoder::Create(); | 28 return VP9Encoder::Create(); |
29 case kUnsupportedCodec: | 29 default: |
30 RTC_NOTREACHED(); | 30 RTC_NOTREACHED(); |
31 return nullptr; | 31 return nullptr; |
32 } | 32 } |
33 RTC_NOTREACHED(); | |
34 return nullptr; | |
35 } | 33 } |
36 | 34 |
37 bool VideoEncoder::IsSupportedSoftware(EncoderType codec_type) { | 35 bool VideoEncoder::IsSupportedSoftware(const cricket::VideoCodec& codec) { |
38 switch (codec_type) { | 36 switch (cricket::CodecTypeFromName(codec.name)) { |
39 case kH264: | 37 case kVideoCodecH264: |
40 return H264Encoder::IsSupported(); | 38 return H264Encoder::IsSupported(); |
41 case kVp8: | 39 case kVideoCodecVP8: |
42 return true; | 40 return true; |
43 case kVp9: | 41 case kVideoCodecVP9: |
44 return VP9Encoder::IsSupported(); | 42 return VP9Encoder::IsSupported(); |
45 case kUnsupportedCodec: | 43 default: |
46 RTC_NOTREACHED(); | |
47 return false; | 44 return false; |
48 } | 45 } |
49 RTC_NOTREACHED(); | |
50 return false; | |
51 } | |
52 | |
53 VideoEncoder::EncoderType VideoEncoder::CodecToEncoderType( | |
54 VideoCodecType codec_type) { | |
55 switch (codec_type) { | |
56 case kVideoCodecH264: | |
57 return VideoEncoder::kH264; | |
58 case kVideoCodecVP8: | |
59 return VideoEncoder::kVp8; | |
60 case kVideoCodecVP9: | |
61 return VideoEncoder::kVp9; | |
62 default: | |
63 return VideoEncoder::kUnsupportedCodec; | |
64 } | |
65 } | 46 } |
66 | 47 |
67 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper( | 48 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper( |
68 VideoCodecType codec_type, | 49 const cricket::VideoCodec& codec, |
69 webrtc::VideoEncoder* encoder) | 50 webrtc::VideoEncoder* encoder) |
70 : rates_set_(false), | 51 : rates_set_(false), |
71 channel_parameters_set_(false), | 52 channel_parameters_set_(false), |
72 encoder_type_(CodecToEncoderType(codec_type)), | 53 codec_(codec), |
73 encoder_(encoder), | 54 encoder_(encoder), |
74 callback_(nullptr) {} | 55 callback_(nullptr) {} |
75 | 56 |
76 bool VideoEncoderSoftwareFallbackWrapper::InitFallbackEncoder() { | 57 bool VideoEncoderSoftwareFallbackWrapper::InitFallbackEncoder() { |
77 if (!VideoEncoder::IsSupportedSoftware(encoder_type_)) { | 58 if (!VideoEncoder::IsSupportedSoftware(codec_)) { |
78 LOG(LS_WARNING) | 59 LOG(LS_WARNING) |
79 << "Encoder requesting fallback to codec not supported in software."; | 60 << "Encoder requesting fallback to codec not supported in software."; |
80 return false; | 61 return false; |
81 } | 62 } |
82 fallback_encoder_.reset(VideoEncoder::Create(encoder_type_)); | 63 fallback_encoder_.reset(VideoEncoder::Create(codec_)); |
83 if (fallback_encoder_->InitEncode(&codec_settings_, number_of_cores_, | 64 if (fallback_encoder_->InitEncode(&codec_settings_, number_of_cores_, |
84 max_payload_size_) != | 65 max_payload_size_) != |
85 WEBRTC_VIDEO_CODEC_OK) { | 66 WEBRTC_VIDEO_CODEC_OK) { |
86 LOG(LS_ERROR) << "Failed to initialize software-encoder fallback."; | 67 LOG(LS_ERROR) << "Failed to initialize software-encoder fallback."; |
87 fallback_encoder_->Release(); | 68 fallback_encoder_->Release(); |
88 fallback_encoder_.reset(); | 69 fallback_encoder_.reset(); |
89 return false; | 70 return false; |
90 } | 71 } |
91 // Replay callback, rates, and channel parameters. | 72 // Replay callback, rates, and channel parameters. |
92 if (callback_) | 73 if (callback_) |
(...skipping 21 matching lines...) Expand all Loading... |
114 // encoder after a failed Encode call. | 95 // encoder after a failed Encode call. |
115 codec_settings_ = *codec_settings; | 96 codec_settings_ = *codec_settings; |
116 number_of_cores_ = number_of_cores; | 97 number_of_cores_ = number_of_cores; |
117 max_payload_size_ = max_payload_size; | 98 max_payload_size_ = max_payload_size; |
118 // Clear stored rate/channel parameters. | 99 // Clear stored rate/channel parameters. |
119 rates_set_ = false; | 100 rates_set_ = false; |
120 channel_parameters_set_ = false; | 101 channel_parameters_set_ = false; |
121 | 102 |
122 int32_t ret = | 103 int32_t ret = |
123 encoder_->InitEncode(codec_settings, number_of_cores, max_payload_size); | 104 encoder_->InitEncode(codec_settings, number_of_cores, max_payload_size); |
124 if (ret == WEBRTC_VIDEO_CODEC_OK || encoder_type_ == kUnsupportedCodec) { | 105 if (ret == WEBRTC_VIDEO_CODEC_OK || codec_.name.empty()) { |
125 if (fallback_encoder_) | 106 if (fallback_encoder_) |
126 fallback_encoder_->Release(); | 107 fallback_encoder_->Release(); |
127 fallback_encoder_.reset(); | 108 fallback_encoder_.reset(); |
128 if (callback_) | 109 if (callback_) |
129 encoder_->RegisterEncodeCompleteCallback(callback_); | 110 encoder_->RegisterEncodeCompleteCallback(callback_); |
130 return ret; | 111 return ret; |
131 } | 112 } |
132 // Try to instantiate software codec. | 113 // Try to instantiate software codec. |
133 if (InitFallbackEncoder()) { | 114 if (InitFallbackEncoder()) { |
134 return WEBRTC_VIDEO_CODEC_OK; | 115 return WEBRTC_VIDEO_CODEC_OK; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return encoder_->OnDroppedFrame(); | 187 return encoder_->OnDroppedFrame(); |
207 } | 188 } |
208 | 189 |
209 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const { | 190 bool VideoEncoderSoftwareFallbackWrapper::SupportsNativeHandle() const { |
210 if (fallback_encoder_) | 191 if (fallback_encoder_) |
211 return fallback_encoder_->SupportsNativeHandle(); | 192 return fallback_encoder_->SupportsNativeHandle(); |
212 return encoder_->SupportsNativeHandle(); | 193 return encoder_->SupportsNativeHandle(); |
213 } | 194 } |
214 | 195 |
215 } // namespace webrtc | 196 } // namespace webrtc |
OLD | NEW |