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-Original-Commit-Position: refs/heads/master@{#19485}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: 1cdddc96fa2f81219bb363f540af8dfb822afbb3
diff --git a/common_types.cc b/common_types.cc
index a76175f..82307ce 100644
--- a/common_types.cc
+++ b/common_types.cc
@@ -132,43 +132,50 @@
   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 =
diff --git a/common_types.h b/common_types.h
index dc62b63..ecfa15d 100644
--- a/common_types.h
+++ b/common_types.h
@@ -537,6 +537,10 @@
 };
 
 // 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);
 
diff --git a/media/engine/internalencoderfactory.cc b/media/engine/internalencoderfactory.cc
index a8d4f2d..004db79 100644
--- a/media/engine/internalencoderfactory.cc
+++ b/media/engine/internalencoderfactory.cc
@@ -71,8 +71,7 @@
 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);
diff --git a/media/engine/videoencodersoftwarefallbackwrapper.cc b/media/engine/videoencodersoftwarefallbackwrapper.cc
index 5405978..8dbf5d1 100644
--- a/media/engine/videoencodersoftwarefallbackwrapper.cc
+++ b/media/engine/videoencodersoftwarefallbackwrapper.cc
@@ -27,8 +27,7 @@
   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) {
diff --git a/media/engine/webrtcvideoengine.cc b/media/engine/webrtcvideoengine.cc
index bf128c8..8a23ae8 100644
--- a/media/engine/webrtcvideoengine.cc
+++ b/media/engine/webrtcvideoengine.cc
@@ -1730,8 +1730,7 @@
   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::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) {
diff --git a/modules/video_coding/codecs/test/videoprocessor.cc b/modules/video_coding/codecs/test/videoprocessor.cc
index 52f8d7c..5ebe836 100644
--- a/modules/video_coding/codecs/test/videoprocessor.cc
+++ b/modules/video_coding/codecs/test/videoprocessor.cc
@@ -49,7 +49,7 @@
 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 @@
     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);
diff --git a/modules/video_coding/codecs/test/videoprocessor_integrationtest.h b/modules/video_coding/codecs/test/videoprocessor_integrationtest.h
index 8bcc53d..e47df37 100644
--- a/modules/video_coding/codecs/test/videoprocessor_integrationtest.h
+++ b/modules/video_coding/codecs/test/videoprocessor_integrationtest.h
@@ -199,8 +199,7 @@
 
     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 =
diff --git a/modules/video_coding/codecs/tools/video_quality_measurement.cc b/modules/video_coding/codecs/tools/video_quality_measurement.cc
index 90ddcfe..48929b9 100644
--- a/modules/video_coding/codecs/tools/video_quality_measurement.cc
+++ b/modules/video_coding/codecs/tools/video_quality_measurement.cc
@@ -411,8 +411,7 @@
       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(
diff --git a/modules/video_coding/packet.cc b/modules/video_coding/packet.cc
index 22ef7ae..7bc4d94 100644
--- a/modules/video_coding/packet.cc
+++ b/modules/video_coding/packet.cc
@@ -134,6 +134,8 @@
       codec = kVideoCodecH264;
       return;
     case kRtpVideoGeneric:
+      codec = kVideoCodecGeneric;
+      return;
     case kRtpVideoNone:
       codec = kVideoCodecUnknown;
       return;
diff --git a/modules/video_coding/utility/ivf_file_writer.cc b/modules/video_coding/utility/ivf_file_writer.cc
index ce3919d..78e956a 100644
--- a/modules/video_coding/utility/ivf_file_writer.cc
+++ b/modules/video_coding/utility/ivf_file_writer.cc
@@ -123,7 +123,7 @@
     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")
diff --git a/modules/video_coding/video_codec_initializer.cc b/modules/video_coding/video_codec_initializer.cc
index df8f136..d274bf1 100644
--- a/modules/video_coding/video_codec_initializer.cc
+++ b/modules/video_coding/video_codec_initializer.cc
@@ -104,8 +104,7 @@
 
   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:
diff --git a/sdk/android/src/jni/androidmediaencoder_jni.cc b/sdk/android/src/jni/androidmediaencoder_jni.cc
index 78fd075..0aa5a51 100644
--- a/sdk/android/src/jni/androidmediaencoder_jni.cc
+++ b/sdk/android/src/jni/androidmediaencoder_jni.cc
@@ -553,8 +553,7 @@
 }
 
 VideoCodecType MediaCodecVideoEncoder::GetCodecType() const {
-  return webrtc::PayloadNameToCodecType(codec_.name)
-      .value_or(webrtc::kVideoCodecUnknown);
+  return webrtc::PayloadStringToCodecType(codec_.name);
 }
 
 int32_t MediaCodecVideoEncoder::InitEncodeInternal(int width,
diff --git a/sdk/android/src/jni/videodecoderfactorywrapper.cc b/sdk/android/src/jni/videodecoderfactorywrapper.cc
index 678dc3d..4e825ad 100644
--- a/sdk/android/src/jni/videodecoderfactorywrapper.cc
+++ b/sdk/android/src/jni/videodecoderfactorywrapper.cc
@@ -30,10 +30,8 @@
     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;
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoEncoderSettings.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoEncoderSettings.mm
index af82e0c..0fb8e46 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoEncoderSettings.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoEncoderSettings.mm
@@ -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;
diff --git a/sdk/objc/Framework/Classes/VideoToolbox/objc_video_decoder_factory.mm b/sdk/objc/Framework/Classes/VideoToolbox/objc_video_decoder_factory.mm
index 5a1a51f..9d4f2b8 100644
--- a/sdk/objc/Framework/Classes/VideoToolbox/objc_video_decoder_factory.mm
+++ b/sdk/objc/Framework/Classes/VideoToolbox/objc_video_decoder_factory.mm
@@ -104,13 +104,9 @@
 }
 
 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];
diff --git a/video/send_statistics_proxy.cc b/video/send_statistics_proxy.cc
index a6f421a..3b97343 100644
--- a/video/send_statistics_proxy.cc
+++ b/video/send_statistics_proxy.cc
@@ -50,12 +50,8 @@
 
 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:
diff --git a/video/video_receive_stream.cc b/video/video_receive_stream.cc
index 56264d9..ae14634 100644
--- a/video/video_receive_stream.cc
+++ b/video/video_receive_stream.cc
@@ -47,8 +47,7 @@
 
   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();
diff --git a/video/video_send_stream.cc b/video/video_send_stream.cc
index 28ef003..ce1eb2f 100644
--- a/video/video_send_stream.cc
+++ b/video/video_send_stream.cc
@@ -148,15 +148,10 @@
 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;
 }
 
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index 7e25df0..52dcbf0 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -387,8 +387,7 @@
       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()