Adding codecs to the RtpParameters returned by an RtpSender.

Contains every field except for sdpFmtpLine.
Setting a reordered list of codecs is not yet supported.

R=glaznev@webrtc.org, pthatcher@webrtc.org, skvlad@webrtc.org, tkchin@webrtc.org

Review URL: https://codereview.webrtc.org/1885473004 .

Cr-Commit-Position: refs/heads/master@{#12453}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
index 9bc4c27..6503953 100644
--- a/webrtc/api/BUILD.gn
+++ b/webrtc/api/BUILD.gn
@@ -61,6 +61,9 @@
       #"objc/RTCPeerConnectionFactory+Private.h",
       #"objc/RTCPeerConnectionFactory.h",
       #"objc/RTCPeerConnectionFactory.mm",
+      #"objc/RTCRtpCodecParameters+Private.h",
+      #"objc/RTCRtpCodecParameters.h",
+      #"objc/RTCRtpCodecParameters.mm",
       #"objc/RTCRtpEncodingParameters+Private.h",
       #"objc/RTCRtpEncodingParameters.h",
       #"objc/RTCRtpEncodingParameters.mm",
diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp
index cfa8d9b..3d47053 100644
--- a/webrtc/api/api.gyp
+++ b/webrtc/api/api.gyp
@@ -172,6 +172,9 @@
             'objc/RTCPeerConnectionFactory+Private.h',
             'objc/RTCPeerConnectionFactory.h',
             'objc/RTCPeerConnectionFactory.mm',
+            'objc/RTCRtpCodecParameters+Private.h',
+            'objc/RTCRtpCodecParameters.h',
+            'objc/RTCRtpCodecParameters.mm',
             'objc/RTCRtpEncodingParameters+Private.h',
             'objc/RTCRtpEncodingParameters.h',
             'objc/RTCRtpEncodingParameters.mm',
diff --git a/webrtc/api/java/jni/peerconnection_jni.cc b/webrtc/api/java/jni/peerconnection_jni.cc
index cbdcbe0..fdd2d44 100644
--- a/webrtc/api/java/jni/peerconnection_jni.cc
+++ b/webrtc/api/java/jni/peerconnection_jni.cc
@@ -2058,6 +2058,29 @@
   return true;
 }
 
+static bool JavaCodecToJsepRtpCodecParameters(
+    JNIEnv* jni,
+    jobject j_codecs,
+    std::vector<webrtc::RtpCodecParameters>* codecs) {
+  jclass codec_class = jni->FindClass("org/webrtc/RtpParameters$Codec");
+  jfieldID payload_type_id = GetFieldID(jni, codec_class, "payloadType", "I");
+  jfieldID mime_type_id =
+      GetFieldID(jni, codec_class, "mimeType", "Ljava/lang/String;");
+  jfieldID clock_rate_id = GetFieldID(jni, codec_class, "clockRate", "I");
+  jfieldID channels_id = GetFieldID(jni, codec_class, "channels", "I");
+
+  for (jobject j_codec : Iterable(jni, j_codecs)) {
+    webrtc::RtpCodecParameters codec;
+    codec.payload_type = GetIntField(jni, j_codec, payload_type_id);
+    codec.mime_type =
+        JavaToStdString(jni, GetStringField(jni, j_codec, mime_type_id));
+    codec.clock_rate = GetIntField(jni, j_codec, clock_rate_id);
+    codec.channels = GetIntField(jni, j_codec, channels_id);
+    codecs->push_back(codec);
+  }
+  return true;
+}
+
 JOW(jboolean, RtpSender_nativeSetParameters)
 (JNIEnv* jni, jclass, jlong j_rtp_sender_pointer, jobject j_parameters) {
   if (IsNull(jni, j_parameters)) {
@@ -2067,11 +2090,15 @@
   jclass encoding_class = jni->FindClass("org/webrtc/RtpParameters$Encoding");
   jfieldID encodings_id =
       GetFieldID(jni, parameters_class, "encodings", "Ljava/util/LinkedList;");
+  jfieldID codecs_id =
+      GetFieldID(jni, parameters_class, "codecs", "Ljava/util/LinkedList;");
 
   jobject j_encodings = GetObjectField(jni, j_parameters, encodings_id);
+  jobject j_codecs = GetObjectField(jni, j_parameters, codecs_id);
   webrtc::RtpParameters parameters;
   JavaEncodingToJsepRtpEncodingParameters(jni, j_encodings,
                                           &parameters.encodings);
+  JavaCodecToJsepRtpCodecParameters(jni, j_codecs, &parameters.codecs);
   return reinterpret_cast<RtpSenderInterface*>(j_rtp_sender_pointer)
       ->SetParameters(parameters);
 }
@@ -2093,8 +2120,8 @@
   jfieldID encodings_id =
       GetFieldID(jni, parameters_class, "encodings", "Ljava/util/LinkedList;");
   jobject j_encodings = GetObjectField(jni, j_parameters, encodings_id);
-  jmethodID add = GetMethodID(jni, GetObjectClass(jni, j_encodings), "add",
-                              "(Ljava/lang/Object;)Z");
+  jmethodID encodings_add = GetMethodID(jni, GetObjectClass(jni, j_encodings),
+                                        "add", "(Ljava/lang/Object;)Z");
   jfieldID active_id =
       GetFieldID(jni, encoding_class, "active", "Z");
   jfieldID bitrate_id =
@@ -2116,10 +2143,42 @@
       jni->SetObjectField(j_encoding_parameters, bitrate_id, j_bitrate_value);
       CHECK_EXCEPTION(jni) << "error during SetObjectField";
     }
-    jboolean added =
-        jni->CallBooleanMethod(j_encodings, add, j_encoding_parameters);
+    jboolean added = jni->CallBooleanMethod(j_encodings, encodings_add,
+                                            j_encoding_parameters);
     CHECK_EXCEPTION(jni) << "error during CallBooleanMethod";
+    RTC_CHECK(added);
   }
+
+  jclass codec_class = jni->FindClass("org/webrtc/RtpParameters$Codec");
+  jmethodID codec_ctor = GetMethodID(jni, codec_class, "<init>", "()V");
+  jfieldID codecs_id =
+      GetFieldID(jni, parameters_class, "codecs", "Ljava/util/LinkedList;");
+  jobject j_codecs = GetObjectField(jni, j_parameters, codecs_id);
+  jmethodID codecs_add = GetMethodID(jni, GetObjectClass(jni, j_codecs),
+                                     "add", "(Ljava/lang/Object;)Z");
+  jfieldID payload_type_id = GetFieldID(jni, codec_class, "payloadType", "I");
+  jfieldID mime_type_id =
+      GetFieldID(jni, codec_class, "mimeType", "Ljava/lang/String;");
+  jfieldID clock_rate_id = GetFieldID(jni, codec_class, "clockRate", "I");
+  jfieldID channels_id = GetFieldID(jni, codec_class, "channels", "I");
+
+  for (const webrtc::RtpCodecParameters& codec : parameters.codecs) {
+    jobject j_codec = jni->NewObject(codec_class, codec_ctor);
+    CHECK_EXCEPTION(jni) << "error during NewObject";
+    jni->SetIntField(j_codec, payload_type_id, codec.payload_type);
+    CHECK_EXCEPTION(jni) << "error during SetIntField";
+    jni->SetObjectField(j_codec, mime_type_id,
+                        JavaStringFromStdString(jni, codec.mime_type));
+    CHECK_EXCEPTION(jni) << "error during SetObjectField";
+    jni->SetIntField(j_codec, clock_rate_id, codec.clock_rate);
+    CHECK_EXCEPTION(jni) << "error during SetIntField";
+    jni->SetIntField(j_codec, channels_id, codec.channels);
+    CHECK_EXCEPTION(jni) << "error during SetIntField";
+    jboolean added = jni->CallBooleanMethod(j_codecs, codecs_add, j_codec);
+    CHECK_EXCEPTION(jni) << "error during CallBooleanMethod";
+    RTC_CHECK(added);
+  }
+
   return j_parameters;
 }
 
diff --git a/webrtc/api/java/src/org/webrtc/RtpParameters.java b/webrtc/api/java/src/org/webrtc/RtpParameters.java
index d689517..4aa10fb 100644
--- a/webrtc/api/java/src/org/webrtc/RtpParameters.java
+++ b/webrtc/api/java/src/org/webrtc/RtpParameters.java
@@ -24,9 +24,18 @@
     public Integer maxBitrateBps;
   }
 
+  public static class Codec {
+    int payloadType;
+    String mimeType;
+    int clockRate;
+    int channels = 1;
+  }
+
   public final LinkedList<Encoding> encodings;
+  public final LinkedList<Codec> codecs;
 
   public RtpParameters() {
     encodings = new LinkedList<Encoding>();
+    codecs = new LinkedList<Codec>();
   }
 }
diff --git a/webrtc/api/objc/RTCRtpCodecParameters+Private.h b/webrtc/api/objc/RTCRtpCodecParameters+Private.h
new file mode 100644
index 0000000..aabbf7d
--- /dev/null
+++ b/webrtc/api/objc/RTCRtpCodecParameters+Private.h
@@ -0,0 +1,30 @@
+/*
+ *  Copyright 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "webrtc/api/objc/RTCRtpCodecParameters.h"
+
+#include "webrtc/api/rtpparameters.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCRtpCodecParameters ()
+
+/** Returns the equivalent native RtpCodecParameters structure. */
+@property(nonatomic, readonly) webrtc::RtpCodecParameters nativeParameters;
+
+/** Initialize the object with a native RtpCodecParameters structure. */
+- (instancetype)initWithNativeParameters:
+    (const webrtc::RtpCodecParameters &)nativeParameters;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCRtpCodecParameters.h b/webrtc/api/objc/RTCRtpCodecParameters.h
new file mode 100644
index 0000000..ec0c647
--- /dev/null
+++ b/webrtc/api/objc/RTCRtpCodecParameters.h
@@ -0,0 +1,55 @@
+/*
+ *  Copyright 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+extern const NSString * const kRtxCodecMimeType;
+extern const NSString * const kRedCodecMimeType;
+extern const NSString * const kUlpfecCodecMimeType;
+extern const NSString * const kOpusCodecMimeType;
+extern const NSString * const kIsacCodecMimeType;
+extern const NSString * const kL16CodecMimeType;
+extern const NSString * const kG722CodecMimeType;
+extern const NSString * const kIlbcCodecMimeType;
+extern const NSString * const kPcmuCodecMimeType;
+extern const NSString * const kPcmaCodecMimeType;
+extern const NSString * const kDtmfCodecMimeType;
+extern const NSString * const kComfortNoiseCodecMimeType;
+extern const NSString * const kVp8CodecMimeType;
+extern const NSString * const kVp9CodecMimeType;
+extern const NSString * const kH264CodecMimeType;
+
+/** Defined in http://w3c.github.io/webrtc-pc/#idl-def-RTCRtpCodecParameters */
+@interface RTCRtpCodecParameters : NSObject
+
+/** The RTP payload type. */
+@property(nonatomic, assign) int payloadType;
+
+/**
+ * The codec MIME type. Valid types are listed in:
+ * http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-2
+ *
+ * Several supported types are represented by the constants above.
+ */
+@property(nonatomic, nonnull) NSString *mimeType;
+
+/** The codec clock rate expressed in Hertz. */
+@property(nonatomic, assign) int clockRate;
+
+/** The number of channels (mono=1, stereo=2). */
+@property(nonatomic, assign) int channels;
+
+- (instancetype)init NS_DESIGNATED_INITIALIZER;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCRtpCodecParameters.mm b/webrtc/api/objc/RTCRtpCodecParameters.mm
new file mode 100644
index 0000000..8bc2204
--- /dev/null
+++ b/webrtc/api/objc/RTCRtpCodecParameters.mm
@@ -0,0 +1,63 @@
+/*
+ *  Copyright 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "RTCRtpCodecParameters+Private.h"
+
+#import "webrtc/base/objc/NSString+StdString.h"
+#import "webrtc/media/base/mediaconstants.h"
+
+const NSString * const kRtxCodecMimeType = @(cricket::kRtxCodecName);
+const NSString * const kRedCodecMimeType = @(cricket::kRedCodecName);
+const NSString * const kUlpfecCodecMimeType = @(cricket::kUlpfecCodecName);
+const NSString * const kOpusCodecMimeType = @(cricket::kOpusCodecName);
+const NSString * const kIsacCodecMimeType = @(cricket::kIsacCodecName);
+const NSString * const kL16CodecMimeType  = @(cricket::kL16CodecName);
+const NSString * const kG722CodecMimeType = @(cricket::kG722CodecName);
+const NSString * const kIlbcCodecMimeType = @(cricket::kIlbcCodecName);
+const NSString * const kPcmuCodecMimeType = @(cricket::kPcmuCodecName);
+const NSString * const kPcmaCodecMimeType = @(cricket::kPcmaCodecName);
+const NSString * const kDtmfCodecMimeType = @(cricket::kDtmfCodecName);
+const NSString * const kComfortNoiseCodecMimeType = @(cricket::kComfortNoiseCodecName);
+const NSString * const kVp8CodecMimeType = @(cricket::kVp8CodecName);
+const NSString * const kVp9CodecMimeType = @(cricket::kVp9CodecName);
+const NSString * const kH264CodecMimeType = @(cricket::kH264CodecName);
+
+@implementation RTCRtpCodecParameters
+
+@synthesize payloadType = _payloadType;
+@synthesize mimeType = _mimeType;
+@synthesize clockRate = _clockRate;
+@synthesize channels = _channels;
+
+- (instancetype)init {
+  return [super init];
+}
+
+- (instancetype)initWithNativeParameters:
+    (const webrtc::RtpCodecParameters &)nativeParameters {
+  if (self = [self init]) {
+    _payloadType = nativeParameters.payload_type;
+    _mimeType = [NSString stringForStdString:nativeParameters.mime_type];
+    _clockRate = nativeParameters.clock_rate;
+    _channels = nativeParameters.channels;
+  }
+  return self;
+}
+
+- (webrtc::RtpCodecParameters)nativeParameters {
+  webrtc::RtpCodecParameters parameters;
+  parameters.payload_type = _payloadType;
+  parameters.mime_type = [NSString stdStringForString:_mimeType];
+  parameters.clock_rate = _clockRate;
+  parameters.channels = _channels;
+  return parameters;
+}
+
+@end
diff --git a/webrtc/api/objc/RTCRtpParameters.h b/webrtc/api/objc/RTCRtpParameters.h
index 91d8108..7b66f37 100644
--- a/webrtc/api/objc/RTCRtpParameters.h
+++ b/webrtc/api/objc/RTCRtpParameters.h
@@ -10,6 +10,7 @@
 
 #import <Foundation/Foundation.h>
 
+#import "webrtc/api/objc/RTCRtpCodecParameters.h"
 #import "webrtc/api/objc/RTCRtpEncodingParameters.h"
 #import "webrtc/base/objc/RTCMacros.h"
 
@@ -21,6 +22,9 @@
 /** The currently active encodings in the order of preference. */
 @property(nonatomic, copy) NSArray<RTCRtpEncodingParameters *> *encodings;
 
+/** The negotiated set of send codecs in order of preference. */
+@property(nonatomic, copy) NSArray<RTCRtpCodecParameters *> *codecs;
+
 - (instancetype)init NS_DESIGNATED_INITIALIZER;
 
 @end
diff --git a/webrtc/api/objc/RTCRtpParameters.mm b/webrtc/api/objc/RTCRtpParameters.mm
index e8c4a44..5e79106 100644
--- a/webrtc/api/objc/RTCRtpParameters.mm
+++ b/webrtc/api/objc/RTCRtpParameters.mm
@@ -9,11 +9,14 @@
  */
 
 #import "RTCRtpParameters+Private.h"
+
+#import "RTCRtpCodecParameters+Private.h"
 #import "RTCRtpEncodingParameters+Private.h"
 
 @implementation RTCRtpParameters
 
 @synthesize encodings = _encodings;
+@synthesize codecs = _codecs;
 
 - (instancetype)init {
   return [super init];
@@ -28,6 +31,13 @@
                                initWithNativeParameters:encoding]];
     }
     _encodings = encodings;
+
+    NSMutableArray *codecs = [[NSMutableArray alloc] init];
+    for (const auto &codec : nativeParameters.codecs) {
+      [codecs addObject:[[RTCRtpCodecParameters alloc]
+                            initWithNativeParameters:codec]];
+    }
+    _codecs = codecs;
   }
   return self;
 }
@@ -37,6 +47,9 @@
   for (RTCRtpEncodingParameters *encoding in _encodings) {
     parameters.encodings.push_back(encoding.nativeParameters);
   }
+  for (RTCRtpCodecParameters *codec in _codecs) {
+    parameters.codecs.push_back(codec.nativeParameters);
+  }
   return parameters;
 }
 
diff --git a/webrtc/api/rtpparameters.h b/webrtc/api/rtpparameters.h
index 2c29d98..729f841 100644
--- a/webrtc/api/rtpparameters.h
+++ b/webrtc/api/rtpparameters.h
@@ -11,6 +11,7 @@
 #ifndef WEBRTC_API_RTPPARAMETERS_H_
 #define WEBRTC_API_RTPPARAMETERS_H_
 
+#include <string>
 #include <vector>
 
 namespace webrtc {
@@ -20,10 +21,32 @@
 struct RtpEncodingParameters {
   bool active = true;
   int max_bitrate_bps = -1;
+
+  bool operator==(const RtpEncodingParameters& o) const {
+    return active == o.active && max_bitrate_bps == o.max_bitrate_bps;
+  }
+};
+
+struct RtpCodecParameters {
+  int payload_type;
+  std::string mime_type;
+  int clock_rate;
+  int channels = 1;
+  // TODO(deadbeef): Add sdpFmtpLine field.
+
+  bool operator==(const RtpCodecParameters& o) const {
+    return payload_type == o.payload_type && mime_type == o.mime_type &&
+           clock_rate == o.clock_rate && channels == o.channels;
+  }
 };
 
 struct RtpParameters {
   std::vector<RtpEncodingParameters> encodings;
+  std::vector<RtpCodecParameters> codecs;
+
+  bool operator==(const RtpParameters& o) const {
+    return encodings == o.encodings && codecs == o.codecs;
+  }
 };
 
 }  // namespace webrtc