From 10165ab8e72f1aa947d25222f4c1895ad7b1792f Mon Sep 17 00:00:00 2001 From: magjed Date: Tue, 22 Nov 2016 10:16:57 -0800 Subject: [PATCH] Unify VideoCodecType to/from string functionality BUG=None Review-Url: https://codereview.webrtc.org/2509273002 Cr-Commit-Position: refs/heads/master@{#15200} --- .../android/jni/androidmediaencoder_jni.cc | 10 +++-- webrtc/common_types.cc | 40 +++++++++++-------- webrtc/common_types.h | 2 +- webrtc/media/base/codec.cc | 11 ----- webrtc/media/base/codec.h | 1 - webrtc/media/engine/internalencoderfactory.cc | 5 ++- .../media/engine/webrtcvideoencoderfactory.cc | 25 ++++-------- webrtc/media/engine/webrtcvideoengine2.cc | 7 +++- .../codecs/test/videoprocessor.cc | 18 --------- .../video_coding/codecs/test/videoprocessor.h | 3 -- .../codecs/tools/video_quality_measurement.cc | 3 +- .../video_coding/utility/ivf_file_writer.cc | 16 +------- .../Classes/videotoolboxvideocodecfactory.cc | 22 +++------- 13 files changed, 58 insertions(+), 105 deletions(-) diff --git a/webrtc/api/android/jni/androidmediaencoder_jni.cc b/webrtc/api/android/jni/androidmediaencoder_jni.cc index d0a293173e..680ee4e694 100644 --- a/webrtc/api/android/jni/androidmediaencoder_jni.cc +++ b/webrtc/api/android/jni/androidmediaencoder_jni.cc @@ -393,7 +393,8 @@ int32_t MediaCodecVideoEncoder::InitEncode( return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; } // Factory should guard against other codecs being used with us. - const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name); + const VideoCodecType codec_type = webrtc::PayloadNameToCodecType(codec_.name) + .value_or(webrtc::kVideoCodecUnknown); RTC_CHECK(codec_settings->codecType == codec_type) << "Unsupported codec " << codec_settings->codecType << " for " << codec_type; @@ -552,7 +553,8 @@ int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( JNIEnv* jni = AttachCurrentThreadIfNeeded(); ScopedLocalRefFrame local_ref_frame(jni); - const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name); + const VideoCodecType codec_type = webrtc::PayloadNameToCodecType(codec_.name) + .value_or(webrtc::kVideoCodecUnknown); ALOGD << "InitEncodeOnCodecThread Type: " << (int)codec_type << ", " << width << " x " << height << ". Bitrate: " << kbps << " kbps. Fps: " << fps; if (kbps == 0) { @@ -1067,7 +1069,9 @@ bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { } // Callback - return encoded frame. - const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name); + const VideoCodecType codec_type = + webrtc::PayloadNameToCodecType(codec_.name) + .value_or(webrtc::kVideoCodecUnknown); webrtc::EncodedImageCallback::Result callback_result( webrtc::EncodedImageCallback::Result::OK); if (callback_) { diff --git a/webrtc/common_types.cc b/webrtc/common_types.cc index 89f3eab8cb..2cbad4711d 100644 --- a/webrtc/common_types.cc +++ b/webrtc/common_types.cc @@ -8,12 +8,14 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "webrtc/base/checks.h" #include "webrtc/common_types.h" #include #include +#include "webrtc/base/checks.h" +#include "webrtc/base/stringutils.h" + namespace webrtc { StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {} @@ -101,41 +103,45 @@ static const char* kPayloadNameRED = "RED"; static const char* kPayloadNameULPFEC = "ULPFEC"; static const char* kPayloadNameGeneric = "Generic"; -rtc::Optional CodecTypeToPayloadName(VideoCodecType type) { +static bool CodecNamesEq(const char* name1, const char* name2) { + return _stricmp(name1, name2) == 0; +} + +rtc::Optional CodecTypeToPayloadName(VideoCodecType type) { switch (type) { case kVideoCodecVP8: - return rtc::Optional(kPayloadNameVp8); + return rtc::Optional(kPayloadNameVp8); case kVideoCodecVP9: - return rtc::Optional(kPayloadNameVp9); + return rtc::Optional(kPayloadNameVp9); case kVideoCodecH264: - return rtc::Optional(kPayloadNameH264); + return rtc::Optional(kPayloadNameH264); case kVideoCodecI420: - return rtc::Optional(kPayloadNameI420); + return rtc::Optional(kPayloadNameI420); case kVideoCodecRED: - return rtc::Optional(kPayloadNameRED); + return rtc::Optional(kPayloadNameRED); case kVideoCodecULPFEC: - return rtc::Optional(kPayloadNameULPFEC); + return rtc::Optional(kPayloadNameULPFEC); case kVideoCodecGeneric: - return rtc::Optional(kPayloadNameGeneric); + return rtc::Optional(kPayloadNameGeneric); default: - return rtc::Optional(); + return rtc::Optional(); } } rtc::Optional PayloadNameToCodecType(const std::string& name) { - if (name == kPayloadNameVp8) + if (CodecNamesEq(name.c_str(), kPayloadNameVp8)) return rtc::Optional(kVideoCodecVP8); - if (name == kPayloadNameVp9) + if (CodecNamesEq(name.c_str(), kPayloadNameVp9)) return rtc::Optional(kVideoCodecVP9); - if (name == kPayloadNameH264) + if (CodecNamesEq(name.c_str(), kPayloadNameH264)) return rtc::Optional(kVideoCodecH264); - if (name == kPayloadNameI420) + if (CodecNamesEq(name.c_str(), kPayloadNameI420)) return rtc::Optional(kVideoCodecI420); - if (name == kPayloadNameRED) + if (CodecNamesEq(name.c_str(), kPayloadNameRED)) return rtc::Optional(kVideoCodecRED); - if (name == kPayloadNameULPFEC) + if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC)) return rtc::Optional(kVideoCodecULPFEC); - if (name == kPayloadNameGeneric) + if (CodecNamesEq(name.c_str(), kPayloadNameGeneric)) return rtc::Optional(kVideoCodecGeneric); return rtc::Optional(); } diff --git a/webrtc/common_types.h b/webrtc/common_types.h index 303c596943..4128f13ffa 100644 --- a/webrtc/common_types.h +++ b/webrtc/common_types.h @@ -573,7 +573,7 @@ enum VideoCodecType { }; // Translates from name of codec to codec type and vice versa. -rtc::Optional CodecTypeToPayloadName(VideoCodecType type); +rtc::Optional CodecTypeToPayloadName(VideoCodecType type); rtc::Optional PayloadNameToCodecType(const std::string& name); union VideoCodecUnion { diff --git a/webrtc/media/base/codec.cc b/webrtc/media/base/codec.cc index 0320e58c72..93cd4f3daf 100644 --- a/webrtc/media/base/codec.cc +++ b/webrtc/media/base/codec.cc @@ -324,17 +324,6 @@ bool CodecNamesEq(const char* name1, const char* name2) { return _stricmp(name1, name2) == 0; } -webrtc::VideoCodecType CodecTypeFromName(const std::string& name) { - if (CodecNamesEq(name.c_str(), kVp8CodecName)) { - return webrtc::kVideoCodecVP8; - } else if (CodecNamesEq(name.c_str(), kVp9CodecName)) { - return webrtc::kVideoCodecVP9; - } else if (CodecNamesEq(name.c_str(), kH264CodecName)) { - return webrtc::kVideoCodecH264; - } - return webrtc::kVideoCodecUnknown; -} - const VideoCodec* FindMatchingCodec( const std::vector& supported_codecs, const VideoCodec& codec) { diff --git a/webrtc/media/base/codec.h b/webrtc/media/base/codec.h index ca439a6e2f..85b4327bb5 100644 --- a/webrtc/media/base/codec.h +++ b/webrtc/media/base/codec.h @@ -212,7 +212,6 @@ const Codec* FindCodecById(const std::vector& codecs, int payload_type) { bool CodecNamesEq(const std::string& name1, const std::string& name2); bool CodecNamesEq(const char* name1, const char* name2); -webrtc::VideoCodecType CodecTypeFromName(const std::string& name); bool HasNack(const Codec& codec); bool HasRemb(const Codec& codec); bool HasTransportCc(const Codec& codec); diff --git a/webrtc/media/engine/internalencoderfactory.cc b/webrtc/media/engine/internalencoderfactory.cc index 28e336deb0..db30b668d2 100644 --- a/webrtc/media/engine/internalencoderfactory.cc +++ b/webrtc/media/engine/internalencoderfactory.cc @@ -64,7 +64,10 @@ InternalEncoderFactory::~InternalEncoderFactory() {} // WebRtcVideoEncoderFactory implementation. webrtc::VideoEncoder* InternalEncoderFactory::CreateVideoEncoder( const cricket::VideoCodec& codec) { - switch (CodecTypeFromName(codec.name)) { + const webrtc::VideoCodecType codec_type = + webrtc::PayloadNameToCodecType(codec.name) + .value_or(webrtc::kVideoCodecUnknown); + switch (codec_type) { case webrtc::kVideoCodecH264: return webrtc::H264Encoder::Create(); case webrtc::kVideoCodecVP8: diff --git a/webrtc/media/engine/webrtcvideoencoderfactory.cc b/webrtc/media/engine/webrtcvideoencoderfactory.cc index ccc32d894b..c7e5ac47fd 100644 --- a/webrtc/media/engine/webrtcvideoencoderfactory.cc +++ b/webrtc/media/engine/webrtcvideoencoderfactory.cc @@ -10,26 +10,14 @@ #include "webrtc/media/engine/webrtcvideoencoderfactory.h" -#include "webrtc/media/base/mediaconstants.h" - -static const char* NameFromCodecType(webrtc::VideoCodecType type) { - switch (type) { - case webrtc::kVideoCodecVP8: - return cricket::kVp8CodecName; - case webrtc::kVideoCodecVP9: - return cricket::kVp9CodecName; - case webrtc::kVideoCodecH264: - return cricket::kH264CodecName; - default: - return "Unknown codec"; - } -} +#include "webrtc/common_types.h" namespace cricket { webrtc::VideoEncoder* WebRtcVideoEncoderFactory::CreateVideoEncoder( const cricket::VideoCodec& codec) { - return CreateVideoEncoder(CodecTypeFromName(codec.name)); + return CreateVideoEncoder(webrtc::PayloadNameToCodecType(codec.name) + .value_or(webrtc::kVideoCodecUnknown)); } const std::vector& @@ -43,7 +31,8 @@ WebRtcVideoEncoderFactory::supported_codecs() const { webrtc::VideoEncoder* WebRtcVideoEncoderFactory::CreateVideoEncoder( webrtc::VideoCodecType type) { - const cricket::VideoCodec codec(NameFromCodecType(type)); + const cricket::VideoCodec codec( + webrtc::CodecTypeToPayloadName(type).value_or("Unknown codec")); return CreateVideoEncoder(codec); } @@ -52,7 +41,9 @@ WebRtcVideoEncoderFactory::codecs() const { const std::vector& codecs = supported_codecs(); for (const cricket::VideoCodec& codec : codecs) { encoder_codecs_.push_back( - VideoCodec(CodecTypeFromName(codec.name), codec.name)); + VideoCodec(webrtc::PayloadNameToCodecType(codec.name) + .value_or(webrtc::kVideoCodecUnknown), + codec.name)); } return encoder_codecs_; } diff --git a/webrtc/media/engine/webrtcvideoengine2.cc b/webrtc/media/engine/webrtcvideoengine2.cc index 8f1d1f1ffc..596e7091f7 100644 --- a/webrtc/media/engine/webrtcvideoengine2.cc +++ b/webrtc/media/engine/webrtcvideoengine2.cc @@ -1789,7 +1789,9 @@ void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; if (new_encoder.external) { - webrtc::VideoCodecType type = CodecTypeFromName(codec_settings.codec.name); + webrtc::VideoCodecType type = + webrtc::PayloadNameToCodecType(codec_settings.codec.name) + .value_or(webrtc::kVideoCodecUnknown); parameters_.config.encoder_settings.internal_source = external_encoder_factory_->EncoderTypeHasInternalSource(type); } @@ -2233,7 +2235,8 @@ WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( std::vector* old_decoders, const VideoCodec& codec) { - webrtc::VideoCodecType type = CodecTypeFromName(codec.name); + webrtc::VideoCodecType type = webrtc::PayloadNameToCodecType(codec.name) + .value_or(webrtc::kVideoCodecUnknown); for (size_t i = 0; i < old_decoders->size(); ++i) { if ((*old_decoders)[i].type == type) { diff --git a/webrtc/modules/video_coding/codecs/test/videoprocessor.cc b/webrtc/modules/video_coding/codecs/test/videoprocessor.cc index cb7ab75af3..aaf29ebb9e 100644 --- a/webrtc/modules/video_coding/codecs/test/videoprocessor.cc +++ b/webrtc/modules/video_coding/codecs/test/videoprocessor.cc @@ -403,24 +403,6 @@ const char* ExcludeFrameTypesToStr(ExcludeFrameTypes e) { } } -const char* VideoCodecTypeToStr(webrtc::VideoCodecType e) { - switch (e) { - case kVideoCodecVP8: - return "VP8"; - case kVideoCodecI420: - return "I420"; - case kVideoCodecRED: - return "RED"; - case kVideoCodecULPFEC: - return "ULPFEC"; - case kVideoCodecUnknown: - return "Unknown"; - default: - RTC_NOTREACHED(); - return "Unknown"; - } -} - // Callbacks EncodedImageCallback::Result VideoProcessorImpl::VideoProcessorEncodeCompleteCallback::OnEncodedImage( diff --git a/webrtc/modules/video_coding/codecs/test/videoprocessor.h b/webrtc/modules/video_coding/codecs/test/videoprocessor.h index dba204f169..3142946509 100644 --- a/webrtc/modules/video_coding/codecs/test/videoprocessor.h +++ b/webrtc/modules/video_coding/codecs/test/videoprocessor.h @@ -108,9 +108,6 @@ struct TestConfig { bool verbose; }; -// Returns a string representation of the enum value. -const char* VideoCodecTypeToStr(webrtc::VideoCodecType e); - // Handles encoding/decoding of video using the VideoEncoder/VideoDecoder // interfaces. This is done in a sequential manner in order to be able to // measure times properly. diff --git a/webrtc/modules/video_coding/codecs/tools/video_quality_measurement.cc b/webrtc/modules/video_coding/codecs/tools/video_quality_measurement.cc index 18c48dbfcd..1eb47db8c2 100644 --- a/webrtc/modules/video_coding/codecs/tools/video_quality_measurement.cc +++ b/webrtc/modules/video_coding/codecs/tools/video_quality_measurement.cc @@ -412,7 +412,8 @@ void PrintPythonOutput(const webrtc::test::TestConfig& config, ExcludeFrameTypesToStr(config.exclude_frame_types), config.frame_length_in_bytes, config.use_single_core ? "True " : "False", config.keyframe_interval, - webrtc::test::VideoCodecTypeToStr(config.codec_settings->codecType), + CodecTypeToPayloadName(config.codec_settings->codecType) + .value_or("Unknown"), config.codec_settings->width, config.codec_settings->height, config.codec_settings->startBitrate); printf( diff --git a/webrtc/modules/video_coding/utility/ivf_file_writer.cc b/webrtc/modules/video_coding/utility/ivf_file_writer.cc index 4198dfea49..d01d0231e8 100644 --- a/webrtc/modules/video_coding/utility/ivf_file_writer.cc +++ b/webrtc/modules/video_coding/utility/ivf_file_writer.cc @@ -122,20 +122,8 @@ bool IvfFileWriter::InitFromFirstFrame(const EncodedImage& encoded_image, if (!WriteHeader()) return false; - std::string codec_name; - switch (codec_type_) { - case kVideoCodecVP8: - codec_name = "VP8"; - break; - case kVideoCodecVP9: - codec_name = "VP9"; - break; - case kVideoCodecH264: - codec_name = "H264"; - break; - default: - codec_name = "Unknown"; - } + const char* codec_name = + CodecTypeToPayloadName(codec_type_).value_or("Unknown"); LOG(LS_WARNING) << "Created IVF file for codec data of type " << codec_name << " at resolution " << width_ << " x " << height_ << ", using " << (using_capture_timestamps_ ? "1" : "90") diff --git a/webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.cc b/webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.cc index 4bf7a4d559..8acc1f6e68 100644 --- a/webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.cc +++ b/webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.cc @@ -17,21 +17,6 @@ #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_decoder.h" #endif -// TODO(kthelgason): delete this when CreateVideoDecoder takes -// a cricket::VideoCodec instead of webrtc::VideoCodecType. -static const char* NameFromCodecType(webrtc::VideoCodecType type) { - switch (type) { - case webrtc::kVideoCodecVP8: - return cricket::kVp8CodecName; - case webrtc::kVideoCodecVP9: - return cricket::kVp9CodecName; - case webrtc::kVideoCodecH264: - return cricket::kH264CodecName; - default: - return "Unknown codec"; - } -} - namespace webrtc { // VideoToolboxVideoEncoderFactory @@ -94,7 +79,12 @@ VideoToolboxVideoDecoderFactory::~VideoToolboxVideoDecoderFactory() {} VideoDecoder* VideoToolboxVideoDecoderFactory::CreateVideoDecoder( VideoCodecType type) { - const auto codec = cricket::VideoCodec(NameFromCodecType(type)); + const rtc::Optional codec_name = CodecTypeToPayloadName(type); + if (!codec_name) { + LOG(LS_ERROR) << "Invalid codec type: " << type; + return nullptr; + } + const cricket::VideoCodec codec(*codec_name); #if defined(WEBRTC_IOS) if (FindMatchingCodec(supported_codecs_, codec)) { LOG(LS_INFO) << "Creating HW decoder for " << codec.name;