Unify VideoCodecType to/from string functionality

BUG=None

Review-Url: https://codereview.webrtc.org/2509273002
Cr-Commit-Position: refs/heads/master@{#15200}
This commit is contained in:
magjed
2016-11-22 10:16:57 -08:00
committed by Commit bot
parent 2d60e53ad5
commit 10165ab8e7
13 changed files with 58 additions and 105 deletions

View File

@ -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_) {

View File

@ -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 <limits>
#include <string.h>
#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<std::string> CodecTypeToPayloadName(VideoCodecType type) {
static bool CodecNamesEq(const char* name1, const char* name2) {
return _stricmp(name1, name2) == 0;
}
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type) {
switch (type) {
case kVideoCodecVP8:
return rtc::Optional<std::string>(kPayloadNameVp8);
return rtc::Optional<const char*>(kPayloadNameVp8);
case kVideoCodecVP9:
return rtc::Optional<std::string>(kPayloadNameVp9);
return rtc::Optional<const char*>(kPayloadNameVp9);
case kVideoCodecH264:
return rtc::Optional<std::string>(kPayloadNameH264);
return rtc::Optional<const char*>(kPayloadNameH264);
case kVideoCodecI420:
return rtc::Optional<std::string>(kPayloadNameI420);
return rtc::Optional<const char*>(kPayloadNameI420);
case kVideoCodecRED:
return rtc::Optional<std::string>(kPayloadNameRED);
return rtc::Optional<const char*>(kPayloadNameRED);
case kVideoCodecULPFEC:
return rtc::Optional<std::string>(kPayloadNameULPFEC);
return rtc::Optional<const char*>(kPayloadNameULPFEC);
case kVideoCodecGeneric:
return rtc::Optional<std::string>(kPayloadNameGeneric);
return rtc::Optional<const char*>(kPayloadNameGeneric);
default:
return rtc::Optional<std::string>();
return rtc::Optional<const char*>();
}
}
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
if (name == kPayloadNameVp8)
if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
return rtc::Optional<VideoCodecType>(kVideoCodecVP8);
if (name == kPayloadNameVp9)
if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
return rtc::Optional<VideoCodecType>(kVideoCodecVP9);
if (name == kPayloadNameH264)
if (CodecNamesEq(name.c_str(), kPayloadNameH264))
return rtc::Optional<VideoCodecType>(kVideoCodecH264);
if (name == kPayloadNameI420)
if (CodecNamesEq(name.c_str(), kPayloadNameI420))
return rtc::Optional<VideoCodecType>(kVideoCodecI420);
if (name == kPayloadNameRED)
if (CodecNamesEq(name.c_str(), kPayloadNameRED))
return rtc::Optional<VideoCodecType>(kVideoCodecRED);
if (name == kPayloadNameULPFEC)
if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
return rtc::Optional<VideoCodecType>(kVideoCodecULPFEC);
if (name == kPayloadNameGeneric)
if (CodecNamesEq(name.c_str(), kPayloadNameGeneric))
return rtc::Optional<VideoCodecType>(kVideoCodecGeneric);
return rtc::Optional<VideoCodecType>();
}

View File

@ -573,7 +573,7 @@ enum VideoCodecType {
};
// Translates from name of codec to codec type and vice versa.
rtc::Optional<std::string> CodecTypeToPayloadName(VideoCodecType type);
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type);
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name);
union VideoCodecUnion {

View File

@ -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<VideoCodec>& supported_codecs,
const VideoCodec& codec) {

View File

@ -212,7 +212,6 @@ const Codec* FindCodecById(const std::vector<Codec>& 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);

View File

@ -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:

View File

@ -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<cricket::VideoCodec>&
@ -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<cricket::VideoCodec>& 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_;
}

View File

@ -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<AllocatedDecoder>* 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) {

View File

@ -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(

View File

@ -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.

View File

@ -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(

View File

@ -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")

View File

@ -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<const char*> 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;