Tweaks for new Objective-C API.

BUG=

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

Cr-Commit-Position: refs/heads/master@{#11872}
diff --git a/webrtc/api/objc/RTCAVFoundationVideoSource.h b/webrtc/api/objc/RTCAVFoundationVideoSource.h
index 1fa19c5..e340444 100644
--- a/webrtc/api/objc/RTCAVFoundationVideoSource.h
+++ b/webrtc/api/objc/RTCAVFoundationVideoSource.h
@@ -26,7 +26,7 @@
 @interface RTCAVFoundationVideoSource : RTCVideoSource
 
 - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
-                    constraints:(RTCMediaConstraints *)constraints;
+                    constraints:(nullable RTCMediaConstraints *)constraints;
 
 /** Switches the camera being used (either front or back). */
 @property(nonatomic, assign) BOOL useBackCamera;
diff --git a/webrtc/api/objc/RTCConfiguration.mm b/webrtc/api/objc/RTCConfiguration.mm
index 2c39118..2d44d01 100644
--- a/webrtc/api/objc/RTCConfiguration.mm
+++ b/webrtc/api/objc/RTCConfiguration.mm
@@ -70,7 +70,7 @@
   webrtc::PeerConnectionInterface::RTCConfiguration nativeConfig;
 
   for (RTCIceServer *iceServer in _iceServers) {
-    nativeConfig.servers.push_back(iceServer.iceServer);
+    nativeConfig.servers.push_back(iceServer.nativeServer);
   }
   nativeConfig.type =
       [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
diff --git a/webrtc/api/objc/RTCDataChannel.h b/webrtc/api/objc/RTCDataChannel.h
index 61d41ba..8ab4b34 100644
--- a/webrtc/api/objc/RTCDataChannel.h
+++ b/webrtc/api/objc/RTCDataChannel.h
@@ -93,7 +93,7 @@
 @property(nonatomic, readonly) BOOL isNegotiated;
 
 /** The identifier for this data channel. */
-@property(nonatomic, readonly) int id;
+@property(nonatomic, readonly) int channelId;
 
 /** The state of the data channel. */
 @property(nonatomic, readonly) RTCDataChannelState readyState;
diff --git a/webrtc/api/objc/RTCDataChannel.mm b/webrtc/api/objc/RTCDataChannel.mm
index a1f484a..5778cb5 100644
--- a/webrtc/api/objc/RTCDataChannel.mm
+++ b/webrtc/api/objc/RTCDataChannel.mm
@@ -123,7 +123,7 @@
   return _nativDataChannel->negotiated();
 }
 
-- (int)id {
+- (int)channelId {
   return _nativDataChannel->id();
 }
 
@@ -161,7 +161,7 @@
 
 - (NSString *)description {
   return [NSString stringWithFormat:@"RTCDataChannel:\n%ld\n%@\n%@",
-                                    (long)self.id,
+                                    (long)self.channelId,
                                     self.label,
                                     [[self class]
                                         stringForState:self.readyState]];
diff --git a/webrtc/api/objc/RTCDataChannelConfiguration.h b/webrtc/api/objc/RTCDataChannelConfiguration.h
index c343eb5..ef0562e 100644
--- a/webrtc/api/objc/RTCDataChannelConfiguration.h
+++ b/webrtc/api/objc/RTCDataChannelConfiguration.h
@@ -15,24 +15,24 @@
 @interface RTCDataChannelConfiguration : NSObject
 
 /** Set to YES if ordered delivery is required. */
-@property(nonatomic) BOOL isOrdered;
+@property(nonatomic, assign) BOOL isOrdered;
 
 /**
  * Max period in milliseconds in which retransmissions will be sent. After this
  * time, no more retransmissions will be sent. -1 if unset.
  */
-@property(nonatomic) int maxPacketLifeTime;
+@property(nonatomic, assign) int maxPacketLifeTime;
 
 /** The max number of retransmissions. -1 if unset. */
-@property(nonatomic) int maxRetransmits;
+@property(nonatomic, assign) int maxRetransmits;
 
 /** Set to YES if the channel has been externally negotiated and we do not send
  * an in-band signalling in the form of an "open" message.
  */
-@property(nonatomic) BOOL isNegotiated;
+@property(nonatomic, assign) BOOL isNegotiated;
 
 /** The stream id, or SID, for SCTP data channels. -1 if unset. */
-@property(nonatomic) int streamId;
+@property(nonatomic, assign) int streamId;
 
 /** Set by the application and opaque to the WebRTC implementation. */
 @property(nonatomic) NSString *protocol;
diff --git a/webrtc/api/objc/RTCIceServer+Private.h b/webrtc/api/objc/RTCIceServer+Private.h
index 556936d..8098bee 100644
--- a/webrtc/api/objc/RTCIceServer+Private.h
+++ b/webrtc/api/objc/RTCIceServer+Private.h
@@ -21,7 +21,7 @@
  * This is needed to pass to the underlying C++ APIs.
  */
 @property(nonatomic, readonly)
-    webrtc::PeerConnectionInterface::IceServer iceServer;
+    webrtc::PeerConnectionInterface::IceServer nativeServer;
 
 /** Initialize an RTCIceServer from a native IceServer. */
 - (instancetype)initWithNativeServer:
diff --git a/webrtc/api/objc/RTCIceServer.h b/webrtc/api/objc/RTCIceServer.h
index 94e3497..8ca179f 100644
--- a/webrtc/api/objc/RTCIceServer.h
+++ b/webrtc/api/objc/RTCIceServer.h
@@ -16,14 +16,14 @@
 @interface RTCIceServer : NSObject
 
 /** URI(s) for this server represented as NSStrings. */
-@property(nonatomic, copy, readonly, nonnull) NSArray *urlStrings;
-// @property(nonatomic, copy, readonly) NSArray<NSString *> *urlStrings;
+@property(nonatomic, readonly, nonnull) NSArray *urlStrings;
+// @property(nonatomic, readonly) NSArray<NSString *> *urlStrings;
 
 /** Username to use if this RTCIceServer object is a TURN server. */
-@property(nonatomic, copy, readonly, nullable) NSString *username;
+@property(nonatomic, readonly, nullable) NSString *username;
 
 /** Credential to use if this RTCIceServer object is a TURN server. */
-@property(nonatomic, copy, readonly, nullable) NSString *credential;
+@property(nonatomic, readonly, nullable) NSString *credential;
 
 - (nonnull instancetype)init NS_UNAVAILABLE;
 
diff --git a/webrtc/api/objc/RTCIceServer.mm b/webrtc/api/objc/RTCIceServer.mm
index bdac700..9fc2154 100644
--- a/webrtc/api/objc/RTCIceServer.mm
+++ b/webrtc/api/objc/RTCIceServer.mm
@@ -50,7 +50,7 @@
 
 #pragma mark - Private
 
-- (webrtc::PeerConnectionInterface::IceServer)iceServer {
+- (webrtc::PeerConnectionInterface::IceServer)nativeServer {
   __block webrtc::PeerConnectionInterface::IceServer iceServer;
 
   iceServer.username = [NSString stdStringForString:_username];
diff --git a/webrtc/api/objc/RTCMediaStream.h b/webrtc/api/objc/RTCMediaStream.h
index b314360..7df1ea8 100644
--- a/webrtc/api/objc/RTCMediaStream.h
+++ b/webrtc/api/objc/RTCMediaStream.h
@@ -14,6 +14,7 @@
 // TODO(hjon): Update nullability types. See http://crbug/webrtc/5592
 
 @class RTCAudioTrack;
+@class RTCPeerConnectionFactory;
 @class RTCVideoTrack;
 
 @interface RTCMediaStream : NSObject
@@ -31,6 +32,10 @@
 
 - (instancetype)init NS_UNAVAILABLE;
 
+/** Initialize an RTCMediaStream with an id. */
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                       streamId:(NSString *)streamId;
+
 /** Adds the given audio track to this media stream. */
 - (void)addAudioTrack:(RTCAudioTrack *)audioTrack;
 
diff --git a/webrtc/api/objc/RTCMediaStream.mm b/webrtc/api/objc/RTCMediaStream.mm
index dcdef9d..3c3e4d9 100644
--- a/webrtc/api/objc/RTCMediaStream.mm
+++ b/webrtc/api/objc/RTCMediaStream.mm
@@ -15,6 +15,7 @@
 #import "webrtc/api/objc/RTCAudioTrack+Private.h"
 #import "webrtc/api/objc/RTCMediaStream+Private.h"
 #import "webrtc/api/objc/RTCMediaStreamTrack+Private.h"
+#import "webrtc/api/objc/RTCPeerConnectionFactory+Private.h"
 #import "webrtc/api/objc/RTCVideoTrack+Private.h"
 #import "webrtc/base/objc/NSString+StdString.h"
 
@@ -26,6 +27,16 @@
   rtc::scoped_refptr<webrtc::MediaStreamInterface> _nativeMediaStream;
 }
 
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                       streamId:(NSString *)streamId {
+  NSParameterAssert(factory);
+  NSParameterAssert(streamId.length);
+  std::string nativeId = [NSString stdStringForString:streamId];
+  rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
+      factory.nativeFactory->CreateLocalMediaStream(nativeId);
+  return [self initWithNativeMediaStream:stream];
+}
+
 - (NSArray *)audioTracks {
 // - (NSArray<RTCAudioTrack *> *)audioTracks {
   return [_audioTracks copy];
diff --git a/webrtc/api/objc/RTCMediaStreamTrack.h b/webrtc/api/objc/RTCMediaStreamTrack.h
index beb48d3..7883843 100644
--- a/webrtc/api/objc/RTCMediaStreamTrack.h
+++ b/webrtc/api/objc/RTCMediaStreamTrack.h
@@ -35,7 +35,7 @@
 @property(nonatomic, readonly) NSString *trackId;
 
 /** The enabled state of the track. */
-@property(nonatomic) BOOL isEnabled;
+@property(nonatomic, assign) BOOL isEnabled;
 
 /** The state of the track. */
 @property(nonatomic, readonly) RTCMediaStreamTrackState readyState;
diff --git a/webrtc/api/objc/RTCPeerConnection.h b/webrtc/api/objc/RTCPeerConnection.h
index 86668a5..72645b5 100644
--- a/webrtc/api/objc/RTCPeerConnection.h
+++ b/webrtc/api/objc/RTCPeerConnection.h
@@ -181,7 +181,7 @@
  *  statistics are gathered for all tracks.
  */
 - (void)statsForTrack:
-    (nonnull RTCMediaStreamTrack *)mediaStreamTrack
+    (nullable RTCMediaStreamTrack *)mediaStreamTrack
      statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel
     completionHandler:
     (nullable void (^)(NSArray * _Nonnull stats))completionHandler;
diff --git a/webrtc/api/objc/RTCPeerConnection.mm b/webrtc/api/objc/RTCPeerConnection.mm
index f75d663..35ac07a 100644
--- a/webrtc/api/objc/RTCPeerConnection.mm
+++ b/webrtc/api/objc/RTCPeerConnection.mm
@@ -199,11 +199,11 @@
     _observer.reset(new webrtc::PeerConnectionDelegateAdapter(self));
     webrtc::PeerConnectionInterface::RTCConfiguration config =
         configuration.nativeConfiguration;
-    webrtc::MediaConstraints *nativeConstraints =
-        constraints.nativeConstraints.get();
+    rtc::scoped_ptr<webrtc::MediaConstraints> nativeConstraints =
+        constraints.nativeConstraints;
     _peerConnection =
         factory.nativeFactory->CreatePeerConnection(config,
-                                                    nativeConstraints,
+                                                    nativeConstraints.get(),
                                                     nullptr,
                                                     nullptr,
                                                     _observer.get());
@@ -259,7 +259,7 @@
 }
 
 - (void)addStream:(RTCMediaStream *)stream {
-  if (_peerConnection->AddStream(stream.nativeMediaStream)) {
+  if (!_peerConnection->AddStream(stream.nativeMediaStream)) {
     RTCLogError(@"Failed to add stream: %@", stream);
     return;
   }
diff --git a/webrtc/api/objc/RTCSessionDescription+Private.h b/webrtc/api/objc/RTCSessionDescription+Private.h
index 2a9601d..9de8f0e 100644
--- a/webrtc/api/objc/RTCSessionDescription+Private.h
+++ b/webrtc/api/objc/RTCSessionDescription+Private.h
@@ -32,9 +32,9 @@
 - (instancetype)initWithNativeDescription:
     (const webrtc::SessionDescriptionInterface *)nativeDescription;
 
-+ (std::string)stringForType:(RTCSdpType)type;
++ (std::string)stdStringForType:(RTCSdpType)type;
 
-+ (RTCSdpType)typeForString:(const std::string &)string;
++ (RTCSdpType)typeForStdString:(const std::string &)string;
 
 @end
 
diff --git a/webrtc/api/objc/RTCSessionDescription.h b/webrtc/api/objc/RTCSessionDescription.h
index 5f00b1c..2635633 100644
--- a/webrtc/api/objc/RTCSessionDescription.h
+++ b/webrtc/api/objc/RTCSessionDescription.h
@@ -36,6 +36,10 @@
 - (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp
     NS_DESIGNATED_INITIALIZER;
 
++ (NSString *)stringForType:(RTCSdpType)type;
+
++ (RTCSdpType)typeForString:(NSString *)string;
+
 @end
 
 NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCSessionDescription.mm b/webrtc/api/objc/RTCSessionDescription.mm
index e401fbc..94c1a3f 100644
--- a/webrtc/api/objc/RTCSessionDescription.mm
+++ b/webrtc/api/objc/RTCSessionDescription.mm
@@ -21,6 +21,16 @@
 @synthesize type = _type;
 @synthesize sdp = _sdp;
 
++ (NSString *)stringForType:(RTCSdpType)type {
+  std::string string = [[self class] stdStringForType:type];
+  return [NSString stringForStdString:string];
+}
+
++ (RTCSdpType)typeForString:(NSString *)string {
+  std::string typeString = string.stdString;
+  return [[self class] typeForStdString:typeString];
+}
+
 - (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp {
   NSParameterAssert(sdp.length);
   if (self = [super init]) {
@@ -31,8 +41,8 @@
 }
 
 - (NSString *)description {
-  return [NSString stringWithFormat:@"RTCSessionDescription:\n%s\n%@",
-                                    [[self class] stringForType:_type].c_str(),
+  return [NSString stringWithFormat:@"RTCSessionDescription:\n%@\n%@",
+                                    [[self class] stringForType:_type],
                                     _sdp];
 }
 
@@ -42,7 +52,7 @@
   webrtc::SdpParseError error;
 
   webrtc::SessionDescriptionInterface *description =
-      webrtc::CreateSessionDescription([[self class] stringForType:_type],
+      webrtc::CreateSessionDescription([[self class] stdStringForType:_type],
                                        _sdp.stdString,
                                        &error);
 
@@ -60,13 +70,13 @@
   NSParameterAssert(nativeDescription);
   std::string sdp;
   nativeDescription->ToString(&sdp);
-  RTCSdpType type = [[self class] typeForString:nativeDescription->type()];
+  RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
 
   return [self initWithType:type
                         sdp:[NSString stringForStdString:sdp]];
 }
 
-+ (std::string)stringForType:(RTCSdpType)type {
++ (std::string)stdStringForType:(RTCSdpType)type {
   switch (type) {
     case RTCSdpTypeOffer:
       return webrtc::SessionDescriptionInterface::kOffer;
@@ -77,7 +87,7 @@
   }
 }
 
-+ (RTCSdpType)typeForString:(const std::string &)string {
++ (RTCSdpType)typeForStdString:(const std::string &)string {
   if (string == webrtc::SessionDescriptionInterface::kOffer) {
     return RTCSdpTypeOffer;
   } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) {
diff --git a/webrtc/api/objc/RTCVideoRenderer.h b/webrtc/api/objc/RTCVideoRenderer.h
index a974562..2fe4efb 100644
--- a/webrtc/api/objc/RTCVideoRenderer.h
+++ b/webrtc/api/objc/RTCVideoRenderer.h
@@ -23,7 +23,7 @@
 - (void)setSize:(CGSize)size;
 
 /** The frame to be displayed. */
-- (void)renderFrame:(RTCVideoFrame *)frame;
+- (void)renderFrame:(nullable RTCVideoFrame *)frame;
 
 @end
 
diff --git a/webrtc/api/objc/RTCVideoTrack.mm b/webrtc/api/objc/RTCVideoTrack.mm
index 7422eff..8a9e3d7 100644
--- a/webrtc/api/objc/RTCVideoTrack.mm
+++ b/webrtc/api/objc/RTCVideoTrack.mm
@@ -33,21 +33,19 @@
   rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
       factory.nativeFactory->CreateVideoTrack(nativeId,
                                               source.nativeVideoSource);
-  return [self initWithNativeTrack:track type:RTCMediaStreamTrackTypeVideo];
+  if ([self initWithNativeTrack:track type:RTCMediaStreamTrackTypeVideo]) {
+    _source = source;
+  }
+  return self;
 }
 
-- (instancetype)initWithNativeMediaTrack:
+- (instancetype)initWithNativeTrack:
     (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeMediaTrack
-                                    type:(RTCMediaStreamTrackType)type {
+                               type:(RTCMediaStreamTrackType)type {
   NSParameterAssert(nativeMediaTrack);
   NSParameterAssert(type == RTCMediaStreamTrackTypeVideo);
   if (self = [super initWithNativeTrack:nativeMediaTrack type:type]) {
     _adapters = [NSMutableArray array];
-    rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
-        self.nativeVideoTrack->GetSource();
-    if (source) {
-      _source = [[RTCVideoSource alloc] initWithNativeVideoSource:source.get()];
-    }
   }
   return self;
 }
@@ -58,6 +56,17 @@
   }
 }
 
+- (RTCVideoSource *)source {
+  if (!_source) {
+    rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
+        self.nativeVideoTrack->GetSource();
+    if (source) {
+      _source = [[RTCVideoSource alloc] initWithNativeVideoSource:source.get()];
+    }
+  }
+  return _source;
+}
+
 - (void)addRenderer:(id<RTCVideoRenderer>)renderer {
   // Make sure we don't have this renderer yet.
   for (RTCVideoRendererAdapter *adapter in _adapters) {
@@ -74,7 +83,6 @@
 }
 
 - (void)removeRenderer:(id<RTCVideoRenderer>)renderer {
-  RTCVideoRendererAdapter *adapter;
   __block NSUInteger indexToRemove = NSNotFound;
   [_adapters enumerateObjectsUsingBlock:^(RTCVideoRendererAdapter *adapter,
                                           NSUInteger idx,
@@ -87,7 +95,9 @@
   if (indexToRemove == NSNotFound) {
     return;
   }
-  self.nativeVideoTrack->RemoveRenderer(adapter.nativeVideoRenderer);
+  RTCVideoRendererAdapter *adapterToRemove =
+      [_adapters objectAtIndex:indexToRemove];
+  self.nativeVideoTrack->RemoveRenderer(adapterToRemove.nativeVideoRenderer);
   [_adapters removeObjectAtIndex:indexToRemove];
 }