Make CodecType conversion functions non-optional.

We can't handle no value here anyway and end up setting a default
at each call site. The defaults aren't even the same in each place.

BUG=None

Review-Url: https://codereview.webrtc.org/2998293002
Cr-Commit-Position: refs/heads/master@{#19485}
This commit is contained in:
kthelgason 2017-08-24 03:52:48 -07:00 committed by Commit Bot
parent 821e21402d
commit 1cdddc96fa
19 changed files with 56 additions and 71 deletions

View File

@ -132,43 +132,50 @@ static bool CodecNamesEq(const char* name1, const char* name2) {
return _stricmp(name1, name2) == 0;
}
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type) {
const char* CodecTypeToPayloadString(VideoCodecType type) {
switch (type) {
case kVideoCodecVP8:
return rtc::Optional<const char*>(kPayloadNameVp8);
return kPayloadNameVp8;
case kVideoCodecVP9:
return rtc::Optional<const char*>(kPayloadNameVp9);
return kPayloadNameVp9;
case kVideoCodecH264:
return rtc::Optional<const char*>(kPayloadNameH264);
return kPayloadNameH264;
case kVideoCodecI420:
return rtc::Optional<const char*>(kPayloadNameI420);
return kPayloadNameI420;
case kVideoCodecRED:
return rtc::Optional<const char*>(kPayloadNameRED);
return kPayloadNameRED;
case kVideoCodecULPFEC:
return rtc::Optional<const char*>(kPayloadNameULPFEC);
case kVideoCodecGeneric:
return rtc::Optional<const char*>(kPayloadNameGeneric);
return kPayloadNameULPFEC;
default:
return rtc::Optional<const char*>();
// Unrecognized codecs default to generic.
return kPayloadNameGeneric;
}
}
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
VideoCodecType PayloadStringToCodecType(const std::string& name) {
if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
return rtc::Optional<VideoCodecType>(kVideoCodecVP8);
return kVideoCodecVP8;
if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
return rtc::Optional<VideoCodecType>(kVideoCodecVP9);
return kVideoCodecVP9;
if (CodecNamesEq(name.c_str(), kPayloadNameH264))
return rtc::Optional<VideoCodecType>(kVideoCodecH264);
return kVideoCodecH264;
if (CodecNamesEq(name.c_str(), kPayloadNameI420))
return rtc::Optional<VideoCodecType>(kVideoCodecI420);
return kVideoCodecI420;
if (CodecNamesEq(name.c_str(), kPayloadNameRED))
return rtc::Optional<VideoCodecType>(kVideoCodecRED);
return kVideoCodecRED;
if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
return rtc::Optional<VideoCodecType>(kVideoCodecULPFEC);
if (CodecNamesEq(name.c_str(), kPayloadNameGeneric))
return rtc::Optional<VideoCodecType>(kVideoCodecGeneric);
return rtc::Optional<VideoCodecType>();
return kVideoCodecULPFEC;
return kVideoCodecGeneric;
}
// TODO(kthelgason): Remove these methods once upstream projects
// have been updated.
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type) {
return rtc::Optional<const char*>(CodecTypeToPayloadString(type));
}
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
return rtc::Optional<VideoCodecType>(PayloadStringToCodecType(name));
}
const uint32_t BitrateAllocation::kMaxBitrateBps =

View File

@ -537,6 +537,10 @@ enum VideoCodecType {
};
// Translates from name of codec to codec type and vice versa.
const char* CodecTypeToPayloadString(VideoCodecType type);
VideoCodecType PayloadStringToCodecType(const std::string& name);
// TODO(kthelgason): Remove these methods once upstream projects
// have been updated.
rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type);
rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name);

View File

@ -71,8 +71,7 @@ InternalEncoderFactory::~InternalEncoderFactory() {}
webrtc::VideoEncoder* InternalEncoderFactory::CreateVideoEncoder(
const cricket::VideoCodec& codec) {
const webrtc::VideoCodecType codec_type =
webrtc::PayloadNameToCodecType(codec.name)
.value_or(webrtc::kVideoCodecUnknown);
webrtc::PayloadStringToCodecType(codec.name);
switch (codec_type) {
case webrtc::kVideoCodecH264:
return webrtc::H264Encoder::Create(codec);

View File

@ -27,8 +27,7 @@ bool EnableForcedFallback(const cricket::VideoCodec& codec) {
if (!webrtc::field_trial::IsEnabled(kVp8ForceFallbackEncoderFieldTrial))
return false;
return (PayloadNameToCodecType(codec.name).value_or(kVideoCodecUnknown) ==
kVideoCodecVP8);
return (PayloadStringToCodecType(codec.name) == kVideoCodecVP8);
}
bool IsForcedFallbackPossible(const VideoCodec& codec_settings) {

View File

@ -1730,8 +1730,7 @@ void WebRtcVideoChannel::WebRtcVideoSendStream::SetCodec(
parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
if (new_encoder.external) {
webrtc::VideoCodecType type =
webrtc::PayloadNameToCodecType(codec_settings.codec.name)
.value_or(webrtc::kVideoCodecUnknown);
webrtc::PayloadStringToCodecType(codec_settings.codec.name);
parameters_.config.encoder_settings.internal_source =
external_encoder_factory_->EncoderTypeHasInternalSource(type);
} else {
@ -2175,8 +2174,7 @@ WebRtcVideoChannel::WebRtcVideoReceiveStream::AllocatedDecoder
WebRtcVideoChannel::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
std::vector<AllocatedDecoder>* old_decoders,
const VideoCodec& codec) {
webrtc::VideoCodecType type = webrtc::PayloadNameToCodecType(codec.name)
.value_or(webrtc::kVideoCodecUnknown);
webrtc::VideoCodecType type = webrtc::PayloadStringToCodecType(codec.name);
for (size_t i = 0; i < old_decoders->size(); ++i) {
if ((*old_decoders)[i].type == type) {

View File

@ -49,7 +49,7 @@ std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
void PrintCodecSettings(const VideoCodec& codec_settings) {
printf(" Codec settings:\n");
printf(" Codec type : %s\n",
CodecTypeToPayloadName(codec_settings.codecType).value_or("Unknown"));
CodecTypeToPayloadString(codec_settings.codecType));
printf(" Start bitrate : %d kbps\n", codec_settings.startBitrate);
printf(" Max bitrate : %d kbps\n", codec_settings.maxBitrate);
printf(" Min bitrate : %d kbps\n", codec_settings.minBitrate);
@ -189,8 +189,7 @@ void VideoProcessor::Init() {
printf(" Decoder implementation name: %s\n", decoder_name);
if (strcmp(encoder_name, decoder_name) == 0) {
printf(" Codec implementation name : %s_%s\n",
CodecTypeToPayloadName(config_.codec_settings.codecType)
.value_or("Unknown"),
CodecTypeToPayloadString(config_.codec_settings.codecType),
encoder_->ImplementationName());
}
PrintCodecSettings(config_.codec_settings);

View File

@ -199,8 +199,7 @@ class VideoProcessorIntegrationTest : public testing::Test {
if (visualization_params) {
const std::string codec_name =
CodecTypeToPayloadName(config_.codec_settings.codecType)
.value_or("unknown");
CodecTypeToPayloadString(config_.codec_settings.codecType);
const std::string implementation_type = config_.hw_codec ? "hw" : "sw";
// clang-format off
const std::string output_filename_base =

View File

@ -411,8 +411,7 @@ 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,
CodecTypeToPayloadName(config.codec_settings.codecType)
.value_or("Unknown"),
CodecTypeToPayloadString(config.codec_settings.codecType),
config.codec_settings.width, config.codec_settings.height,
config.codec_settings.startBitrate);
printf(

View File

@ -134,6 +134,8 @@ void VCMPacket::CopyCodecSpecifics(const RTPVideoHeader& videoHeader) {
codec = kVideoCodecH264;
return;
case kRtpVideoGeneric:
codec = kVideoCodecGeneric;
return;
case kRtpVideoNone:
codec = kVideoCodecUnknown;
return;

View File

@ -123,7 +123,7 @@ bool IvfFileWriter::InitFromFirstFrame(const EncodedImage& encoded_image,
return false;
const char* codec_name =
CodecTypeToPayloadName(codec_type_).value_or("Unknown");
CodecTypeToPayloadString(codec_type_);
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

@ -104,8 +104,7 @@ VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
VideoCodec video_codec;
memset(&video_codec, 0, sizeof(video_codec));
video_codec.codecType = PayloadNameToCodecType(payload_name)
.value_or(VideoCodecType::kVideoCodecGeneric);
video_codec.codecType = PayloadStringToCodecType(payload_name);
switch (config.content_type) {
case VideoEncoderConfig::ContentType::kRealtimeVideo:

View File

@ -553,8 +553,7 @@ int32_t MediaCodecVideoEncoder::ProcessHWErrorOnEncode() {
}
VideoCodecType MediaCodecVideoEncoder::GetCodecType() const {
return webrtc::PayloadNameToCodecType(codec_.name)
.value_or(webrtc::kVideoCodecUnknown);
return webrtc::PayloadStringToCodecType(codec_.name);
}
int32_t MediaCodecVideoEncoder::InitEncodeInternal(int width,

View File

@ -30,10 +30,8 @@ webrtc::VideoDecoder* VideoDecoderFactoryWrapper::CreateVideoDecoder(
webrtc::VideoCodecType type) {
JNIEnv* jni = AttachCurrentThreadIfNeeded();
ScopedLocalRefFrame local_ref_frame(jni);
rtc::Optional<const char*> type_payload =
webrtc::CodecTypeToPayloadName(type);
RTC_DCHECK(type_payload);
jstring name = jni->NewStringUTF(*type_payload);
const char* type_payload = webrtc::CodecTypeToPayloadString(type);
jstring name = jni->NewStringUTF(type_payload);
jobject decoder =
jni->CallObjectMethod(*decoder_factory_, create_decoder_method_, name);
return decoder != nullptr ? new VideoDecoderWrapper(jni, decoder) : nullptr;

View File

@ -30,10 +30,8 @@
- (instancetype)initWithNativeVideoCodec:(const webrtc::VideoCodec *)videoCodec {
if (self = [super init]) {
if (videoCodec) {
rtc::Optional<const char *> codecName = CodecTypeToPayloadName(videoCodec->codecType);
if (codecName) {
_name = [NSString stringWithUTF8String:codecName.value()];
}
const char *codecName = CodecTypeToPayloadString(videoCodec->codecType);
_name = [NSString stringWithUTF8String:codecName];
_width = videoCodec->width;
_height = videoCodec->height;

View File

@ -104,13 +104,9 @@ id<RTCVideoDecoderFactory> ObjCVideoDecoderFactory::wrapped_decoder_factory() co
}
VideoDecoder *ObjCVideoDecoderFactory::CreateVideoDecoder(VideoCodecType type) {
const rtc::Optional<const char *> codec_name = CodecTypeToPayloadName(type);
if (!codec_name) {
LOG(LS_ERROR) << "Invalid codec type: " << type;
return nullptr;
}
const char *codec_name = CodecTypeToPayloadString(type);
NSString *codecName = [NSString stringWithUTF8String:codec_name.value()];
NSString *codecName = [NSString stringWithUTF8String:codec_name];
for (RTCVideoCodecInfo *codecInfo in decoder_factory_.supportedCodecs) {
if ([codecName isEqualToString:codecInfo.name]) {
id<RTCVideoDecoder> decoder = [decoder_factory_ createDecoder:codecInfo];

View File

@ -50,12 +50,8 @@ const char* GetUmaPrefix(VideoEncoderConfig::ContentType content_type) {
HistogramCodecType PayloadNameToHistogramCodecType(
const std::string& payload_name) {
rtc::Optional<VideoCodecType> codecType =
PayloadNameToCodecType(payload_name);
if (!codecType) {
return kVideoUnknown;
}
switch (*codecType) {
VideoCodecType codecType = PayloadStringToCodecType(payload_name);
switch (codecType) {
case kVideoCodecVP8:
return kVideoVp8;
case kVideoCodecVP9:

View File

@ -47,8 +47,7 @@ VideoCodec CreateDecoderVideoCodec(const VideoReceiveStream::Decoder& decoder) {
codec.plType = decoder.payload_type;
strncpy(codec.plName, decoder.payload_name.c_str(), sizeof(codec.plName));
codec.codecType =
PayloadNameToCodecType(decoder.payload_name).value_or(kVideoCodecGeneric);
codec.codecType = PayloadStringToCodecType(decoder.payload_name);
if (codec.codecType == kVideoCodecVP8) {
*(codec.VP8()) = VideoEncoder::GetDefaultVp8Settings();

View File

@ -148,15 +148,10 @@ std::unique_ptr<FlexfecSender> MaybeCreateFlexfecSender(
namespace {
bool PayloadTypeSupportsSkippingFecPackets(const std::string& payload_name) {
rtc::Optional<VideoCodecType> codecType =
PayloadNameToCodecType(payload_name);
if (codecType &&
(*codecType == kVideoCodecVP8 || *codecType == kVideoCodecVP9)) {
const VideoCodecType codecType = PayloadStringToCodecType(payload_name);
if (codecType == kVideoCodecVP8 || codecType == kVideoCodecVP9) {
return true;
}
RTC_DCHECK((codecType && *codecType == kVideoCodecH264) ||
payload_name == "FAKE")
<< "unknown payload_name " << payload_name;
return false;
}

View File

@ -387,8 +387,7 @@ VideoStreamEncoder::VideoStreamEncoder(uint32_t number_of_cores,
source_proxy_(new VideoSourceProxy(this)),
sink_(nullptr),
settings_(settings),
codec_type_(PayloadNameToCodecType(settings.payload_name)
.value_or(VideoCodecType::kVideoCodecUnknown)),
codec_type_(PayloadStringToCodecType(settings.payload_name)),
video_sender_(Clock::GetRealTimeClock(), this, this),
overuse_detector_(
overuse_detector.get()