Skip to content

Commit 10165ab

Browse files
Hnoo112233Commit bot
authored and
Commit bot
committed
Unify VideoCodecType to/from string functionality
BUG=None Review-Url: https://codereview.webrtc.org/2509273002 Cr-Commit-Position: refs/heads/master@{#15200}
1 parent 2d60e53 commit 10165ab

13 files changed

+58
-105
lines changed

webrtc/api/android/jni/androidmediaencoder_jni.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ int32_t MediaCodecVideoEncoder::InitEncode(
393393
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
394394
}
395395
// Factory should guard against other codecs being used with us.
396-
const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name);
396+
const VideoCodecType codec_type = webrtc::PayloadNameToCodecType(codec_.name)
397+
.value_or(webrtc::kVideoCodecUnknown);
397398
RTC_CHECK(codec_settings->codecType == codec_type)
398399
<< "Unsupported codec " << codec_settings->codecType << " for "
399400
<< codec_type;
@@ -552,7 +553,8 @@ int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread(
552553
JNIEnv* jni = AttachCurrentThreadIfNeeded();
553554
ScopedLocalRefFrame local_ref_frame(jni);
554555

555-
const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name);
556+
const VideoCodecType codec_type = webrtc::PayloadNameToCodecType(codec_.name)
557+
.value_or(webrtc::kVideoCodecUnknown);
556558
ALOGD << "InitEncodeOnCodecThread Type: " << (int)codec_type << ", " << width
557559
<< " x " << height << ". Bitrate: " << kbps << " kbps. Fps: " << fps;
558560
if (kbps == 0) {
@@ -1067,7 +1069,9 @@ bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) {
10671069
}
10681070

10691071
// Callback - return encoded frame.
1070-
const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name);
1072+
const VideoCodecType codec_type =
1073+
webrtc::PayloadNameToCodecType(codec_.name)
1074+
.value_or(webrtc::kVideoCodecUnknown);
10711075
webrtc::EncodedImageCallback::Result callback_result(
10721076
webrtc::EncodedImageCallback::Result::OK);
10731077
if (callback_) {

webrtc/common_types.cc

+23-17
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
* be found in the AUTHORS file in the root of the source tree.
99
*/
1010

11-
#include "webrtc/base/checks.h"
1211
#include "webrtc/common_types.h"
1312

1413
#include <limits>
1514
#include <string.h>
1615

16+
#include "webrtc/base/checks.h"
17+
#include "webrtc/base/stringutils.h"
18+
1719
namespace webrtc {
1820

1921
StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {}
@@ -101,41 +103,45 @@ static const char* kPayloadNameRED = "RED";
101103
static const char* kPayloadNameULPFEC = "ULPFEC";
102104
static const char* kPayloadNameGeneric = "Generic";
103105

104-
rtc::Optional<std::string> CodecTypeToPayloadName(VideoCodecType type) {
106+
static bool CodecNamesEq(const char* name1, const char* name2) {
107+
return _stricmp(name1, name2) == 0;
108+
}
109+
110+
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type) {
105111
switch (type) {
106112
case kVideoCodecVP8:
107-
return rtc::Optional<std::string>(kPayloadNameVp8);
113+
return rtc::Optional<const char*>(kPayloadNameVp8);
108114
case kVideoCodecVP9:
109-
return rtc::Optional<std::string>(kPayloadNameVp9);
115+
return rtc::Optional<const char*>(kPayloadNameVp9);
110116
case kVideoCodecH264:
111-
return rtc::Optional<std::string>(kPayloadNameH264);
117+
return rtc::Optional<const char*>(kPayloadNameH264);
112118
case kVideoCodecI420:
113-
return rtc::Optional<std::string>(kPayloadNameI420);
119+
return rtc::Optional<const char*>(kPayloadNameI420);
114120
case kVideoCodecRED:
115-
return rtc::Optional<std::string>(kPayloadNameRED);
121+
return rtc::Optional<const char*>(kPayloadNameRED);
116122
case kVideoCodecULPFEC:
117-
return rtc::Optional<std::string>(kPayloadNameULPFEC);
123+
return rtc::Optional<const char*>(kPayloadNameULPFEC);
118124
case kVideoCodecGeneric:
119-
return rtc::Optional<std::string>(kPayloadNameGeneric);
125+
return rtc::Optional<const char*>(kPayloadNameGeneric);
120126
default:
121-
return rtc::Optional<std::string>();
127+
return rtc::Optional<const char*>();
122128
}
123129
}
124130

125131
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
126-
if (name == kPayloadNameVp8)
132+
if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
127133
return rtc::Optional<VideoCodecType>(kVideoCodecVP8);
128-
if (name == kPayloadNameVp9)
134+
if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
129135
return rtc::Optional<VideoCodecType>(kVideoCodecVP9);
130-
if (name == kPayloadNameH264)
136+
if (CodecNamesEq(name.c_str(), kPayloadNameH264))
131137
return rtc::Optional<VideoCodecType>(kVideoCodecH264);
132-
if (name == kPayloadNameI420)
138+
if (CodecNamesEq(name.c_str(), kPayloadNameI420))
133139
return rtc::Optional<VideoCodecType>(kVideoCodecI420);
134-
if (name == kPayloadNameRED)
140+
if (CodecNamesEq(name.c_str(), kPayloadNameRED))
135141
return rtc::Optional<VideoCodecType>(kVideoCodecRED);
136-
if (name == kPayloadNameULPFEC)
142+
if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
137143
return rtc::Optional<VideoCodecType>(kVideoCodecULPFEC);
138-
if (name == kPayloadNameGeneric)
144+
if (CodecNamesEq(name.c_str(), kPayloadNameGeneric))
139145
return rtc::Optional<VideoCodecType>(kVideoCodecGeneric);
140146
return rtc::Optional<VideoCodecType>();
141147
}

webrtc/common_types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ enum VideoCodecType {
573573
};
574574

575575
// Translates from name of codec to codec type and vice versa.
576-
rtc::Optional<std::string> CodecTypeToPayloadName(VideoCodecType type);
576+
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type);
577577
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name);
578578

579579
union VideoCodecUnion {

webrtc/media/base/codec.cc

-11
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,6 @@ bool CodecNamesEq(const char* name1, const char* name2) {
324324
return _stricmp(name1, name2) == 0;
325325
}
326326

327-
webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
328-
if (CodecNamesEq(name.c_str(), kVp8CodecName)) {
329-
return webrtc::kVideoCodecVP8;
330-
} else if (CodecNamesEq(name.c_str(), kVp9CodecName)) {
331-
return webrtc::kVideoCodecVP9;
332-
} else if (CodecNamesEq(name.c_str(), kH264CodecName)) {
333-
return webrtc::kVideoCodecH264;
334-
}
335-
return webrtc::kVideoCodecUnknown;
336-
}
337-
338327
const VideoCodec* FindMatchingCodec(
339328
const std::vector<VideoCodec>& supported_codecs,
340329
const VideoCodec& codec) {

webrtc/media/base/codec.h

-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
212212

213213
bool CodecNamesEq(const std::string& name1, const std::string& name2);
214214
bool CodecNamesEq(const char* name1, const char* name2);
215-
webrtc::VideoCodecType CodecTypeFromName(const std::string& name);
216215
bool HasNack(const Codec& codec);
217216
bool HasRemb(const Codec& codec);
218217
bool HasTransportCc(const Codec& codec);

webrtc/media/engine/internalencoderfactory.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ InternalEncoderFactory::~InternalEncoderFactory() {}
6464
// WebRtcVideoEncoderFactory implementation.
6565
webrtc::VideoEncoder* InternalEncoderFactory::CreateVideoEncoder(
6666
const cricket::VideoCodec& codec) {
67-
switch (CodecTypeFromName(codec.name)) {
67+
const webrtc::VideoCodecType codec_type =
68+
webrtc::PayloadNameToCodecType(codec.name)
69+
.value_or(webrtc::kVideoCodecUnknown);
70+
switch (codec_type) {
6871
case webrtc::kVideoCodecH264:
6972
return webrtc::H264Encoder::Create();
7073
case webrtc::kVideoCodecVP8:

webrtc/media/engine/webrtcvideoencoderfactory.cc

+8-17
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,14 @@
1010

1111
#include "webrtc/media/engine/webrtcvideoencoderfactory.h"
1212

13-
#include "webrtc/media/base/mediaconstants.h"
14-
15-
static const char* NameFromCodecType(webrtc::VideoCodecType type) {
16-
switch (type) {
17-
case webrtc::kVideoCodecVP8:
18-
return cricket::kVp8CodecName;
19-
case webrtc::kVideoCodecVP9:
20-
return cricket::kVp9CodecName;
21-
case webrtc::kVideoCodecH264:
22-
return cricket::kH264CodecName;
23-
default:
24-
return "Unknown codec";
25-
}
26-
}
13+
#include "webrtc/common_types.h"
2714

2815
namespace cricket {
2916

3017
webrtc::VideoEncoder* WebRtcVideoEncoderFactory::CreateVideoEncoder(
3118
const cricket::VideoCodec& codec) {
32-
return CreateVideoEncoder(CodecTypeFromName(codec.name));
19+
return CreateVideoEncoder(webrtc::PayloadNameToCodecType(codec.name)
20+
.value_or(webrtc::kVideoCodecUnknown));
3321
}
3422

3523
const std::vector<cricket::VideoCodec>&
@@ -43,7 +31,8 @@ WebRtcVideoEncoderFactory::supported_codecs() const {
4331

4432
webrtc::VideoEncoder* WebRtcVideoEncoderFactory::CreateVideoEncoder(
4533
webrtc::VideoCodecType type) {
46-
const cricket::VideoCodec codec(NameFromCodecType(type));
34+
const cricket::VideoCodec codec(
35+
webrtc::CodecTypeToPayloadName(type).value_or("Unknown codec"));
4736
return CreateVideoEncoder(codec);
4837
}
4938

@@ -52,7 +41,9 @@ WebRtcVideoEncoderFactory::codecs() const {
5241
const std::vector<cricket::VideoCodec>& codecs = supported_codecs();
5342
for (const cricket::VideoCodec& codec : codecs) {
5443
encoder_codecs_.push_back(
55-
VideoCodec(CodecTypeFromName(codec.name), codec.name));
44+
VideoCodec(webrtc::PayloadNameToCodecType(codec.name)
45+
.value_or(webrtc::kVideoCodecUnknown),
46+
codec.name));
5647
}
5748
return encoder_codecs_;
5849
}

webrtc/media/engine/webrtcvideoengine2.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,9 @@ void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
17891789
parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
17901790
parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
17911791
if (new_encoder.external) {
1792-
webrtc::VideoCodecType type = CodecTypeFromName(codec_settings.codec.name);
1792+
webrtc::VideoCodecType type =
1793+
webrtc::PayloadNameToCodecType(codec_settings.codec.name)
1794+
.value_or(webrtc::kVideoCodecUnknown);
17931795
parameters_.config.encoder_settings.internal_source =
17941796
external_encoder_factory_->EncoderTypeHasInternalSource(type);
17951797
}
@@ -2233,7 +2235,8 @@ WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
22332235
WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
22342236
std::vector<AllocatedDecoder>* old_decoders,
22352237
const VideoCodec& codec) {
2236-
webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
2238+
webrtc::VideoCodecType type = webrtc::PayloadNameToCodecType(codec.name)
2239+
.value_or(webrtc::kVideoCodecUnknown);
22372240

22382241
for (size_t i = 0; i < old_decoders->size(); ++i) {
22392242
if ((*old_decoders)[i].type == type) {

webrtc/modules/video_coding/codecs/test/videoprocessor.cc

-18
Original file line numberDiff line numberDiff line change
@@ -403,24 +403,6 @@ const char* ExcludeFrameTypesToStr(ExcludeFrameTypes e) {
403403
}
404404
}
405405

406-
const char* VideoCodecTypeToStr(webrtc::VideoCodecType e) {
407-
switch (e) {
408-
case kVideoCodecVP8:
409-
return "VP8";
410-
case kVideoCodecI420:
411-
return "I420";
412-
case kVideoCodecRED:
413-
return "RED";
414-
case kVideoCodecULPFEC:
415-
return "ULPFEC";
416-
case kVideoCodecUnknown:
417-
return "Unknown";
418-
default:
419-
RTC_NOTREACHED();
420-
return "Unknown";
421-
}
422-
}
423-
424406
// Callbacks
425407
EncodedImageCallback::Result
426408
VideoProcessorImpl::VideoProcessorEncodeCompleteCallback::OnEncodedImage(

webrtc/modules/video_coding/codecs/test/videoprocessor.h

-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ struct TestConfig {
108108
bool verbose;
109109
};
110110

111-
// Returns a string representation of the enum value.
112-
const char* VideoCodecTypeToStr(webrtc::VideoCodecType e);
113-
114111
// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
115112
// interfaces. This is done in a sequential manner in order to be able to
116113
// measure times properly.

webrtc/modules/video_coding/codecs/tools/video_quality_measurement.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ void PrintPythonOutput(const webrtc::test::TestConfig& config,
412412
ExcludeFrameTypesToStr(config.exclude_frame_types),
413413
config.frame_length_in_bytes, config.use_single_core ? "True " : "False",
414414
config.keyframe_interval,
415-
webrtc::test::VideoCodecTypeToStr(config.codec_settings->codecType),
415+
CodecTypeToPayloadName(config.codec_settings->codecType)
416+
.value_or("Unknown"),
416417
config.codec_settings->width, config.codec_settings->height,
417418
config.codec_settings->startBitrate);
418419
printf(

webrtc/modules/video_coding/utility/ivf_file_writer.cc

+2-14
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,8 @@ bool IvfFileWriter::InitFromFirstFrame(const EncodedImage& encoded_image,
122122
if (!WriteHeader())
123123
return false;
124124

125-
std::string codec_name;
126-
switch (codec_type_) {
127-
case kVideoCodecVP8:
128-
codec_name = "VP8";
129-
break;
130-
case kVideoCodecVP9:
131-
codec_name = "VP9";
132-
break;
133-
case kVideoCodecH264:
134-
codec_name = "H264";
135-
break;
136-
default:
137-
codec_name = "Unknown";
138-
}
125+
const char* codec_name =
126+
CodecTypeToPayloadName(codec_type_).value_or("Unknown");
139127
LOG(LS_WARNING) << "Created IVF file for codec data of type " << codec_name
140128
<< " at resolution " << width_ << " x " << height_
141129
<< ", using " << (using_capture_timestamps_ ? "1" : "90")

webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.cc

+6-16
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@
1717
#include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_decoder.h"
1818
#endif
1919

20-
// TODO(kthelgason): delete this when CreateVideoDecoder takes
21-
// a cricket::VideoCodec instead of webrtc::VideoCodecType.
22-
static const char* NameFromCodecType(webrtc::VideoCodecType type) {
23-
switch (type) {
24-
case webrtc::kVideoCodecVP8:
25-
return cricket::kVp8CodecName;
26-
case webrtc::kVideoCodecVP9:
27-
return cricket::kVp9CodecName;
28-
case webrtc::kVideoCodecH264:
29-
return cricket::kH264CodecName;
30-
default:
31-
return "Unknown codec";
32-
}
33-
}
34-
3520
namespace webrtc {
3621

3722
// VideoToolboxVideoEncoderFactory
@@ -94,7 +79,12 @@ VideoToolboxVideoDecoderFactory::~VideoToolboxVideoDecoderFactory() {}
9479

9580
VideoDecoder* VideoToolboxVideoDecoderFactory::CreateVideoDecoder(
9681
VideoCodecType type) {
97-
const auto codec = cricket::VideoCodec(NameFromCodecType(type));
82+
const rtc::Optional<const char*> codec_name = CodecTypeToPayloadName(type);
83+
if (!codec_name) {
84+
LOG(LS_ERROR) << "Invalid codec type: " << type;
85+
return nullptr;
86+
}
87+
const cricket::VideoCodec codec(*codec_name);
9888
#if defined(WEBRTC_IOS)
9989
if (FindMatchingCodec(supported_codecs_, codec)) {
10090
LOG(LS_INFO) << "Creating HW decoder for " << codec.name;

0 commit comments

Comments
 (0)