Update API for Objective-C RTCMediaStream.

BUG=
R=tkchin@webrtc.org

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

Patch from Jon Hjelle <hjon@andyet.net>.

Cr-Commit-Position: refs/heads/master@{#11351}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
index 9144984..1c333e4 100644
--- a/webrtc/api/BUILD.gn
+++ b/webrtc/api/BUILD.gn
@@ -29,15 +29,18 @@
     ]
     sources = [
       # Add these when there's a BUILD.gn for peer connection APIs
-      #"objc/RTCAudioTrack+Private.h",
-      #"objc/RTCAudioTrack.h",
-      #"objc/RTCAudioTrack.mm",s
       #"objc/RTCAVFoundationVideoSource+Private.h",
       #"objc/RTCAVFoundationVideoSource.h",
       #"objc/RTCAVFoundationVideoSource.mm",
+      #"objc/RTCAudioTrack+Private.h",
+      #"objc/RTCAudioTrack.h",
+      #"objc/RTCAudioTrack.mm",
       #"objc/RTCIceCandidate+Private.h",
       #"objc/RTCIceCandidate.h",
       #"objc/RTCIceCandidate.mm",
+      #"objc/RTCMediaStream+Private.h",
+      #"objc/RTCMediaStream.h",
+      #"objc/RTCMediaStream.mm",
       #"objc/RTCMediaStreamTrack+Private.h",
       #"objc/RTCMediaStreamTrack.h",
       #"objc/RTCMediaStreamTrack.mm",
diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp
index 5d40cd0..5f848d7 100644
--- a/webrtc/api/api.gyp
+++ b/webrtc/api/api.gyp
@@ -19,12 +19,12 @@
             '../../talk/libjingle.gyp:libjingle_peerconnection',
           ],
           'sources': [
-            'objc/RTCAudioTrack+Private.h',
-            'objc/RTCAudioTrack.h',
-            'objc/RTCAudioTrack.mm',
             'objc/RTCAVFoundationVideoSource+Private.h',
             'objc/RTCAVFoundationVideoSource.h',
             'objc/RTCAVFoundationVideoSource.mm',
+            'objc/RTCAudioTrack+Private.h',
+            'objc/RTCAudioTrack.h',
+            'objc/RTCAudioTrack.mm',
             'objc/RTCIceCandidate+Private.h',
             'objc/RTCIceCandidate.h',
             'objc/RTCIceCandidate.mm',
@@ -34,6 +34,9 @@
             'objc/RTCMediaConstraints+Private.h',
             'objc/RTCMediaConstraints.h',
             'objc/RTCMediaConstraints.mm',
+            'objc/RTCMediaStream+Private.h',
+            'objc/RTCMediaStream.h',
+            'objc/RTCMediaStream.mm',
             'objc/RTCMediaStreamTrack+Private.h',
             'objc/RTCMediaStreamTrack.h',
             'objc/RTCMediaStreamTrack.mm',
diff --git a/webrtc/api/objc/RTCMediaStream+Private.h b/webrtc/api/objc/RTCMediaStream+Private.h
new file mode 100644
index 0000000..2c2662b
--- /dev/null
+++ b/webrtc/api/objc/RTCMediaStream+Private.h
@@ -0,0 +1,32 @@
+/*
+ *  Copyright 2015 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 "RTCMediaStream.h"
+
+#include "talk/app/webrtc/mediastreaminterface.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCMediaStream ()
+
+/**
+ * MediaStreamInterface representation of this RTCMediaStream object. This is
+ * needed to pass to the underlying C++ APIs.
+ */
+@property(nonatomic, readonly)
+    rtc::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream;
+
+/** Initialize an RTCMediaStream from a native MediaStreamInterface. */
+- (instancetype)initWithNativeMediaStream:
+    (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCMediaStream.h b/webrtc/api/objc/RTCMediaStream.h
new file mode 100644
index 0000000..c63f9ed
--- /dev/null
+++ b/webrtc/api/objc/RTCMediaStream.h
@@ -0,0 +1,45 @@
+/*
+ *  Copyright 2015 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
+
+@class RTCAudioTrack;
+@class RTCVideoTrack;
+
+@interface RTCMediaStream : NSObject
+
+/** The audio tracks in this stream. */
+@property(nonatomic, strong, readonly) NSArray<RTCAudioTrack *> *audioTracks;
+
+/** The video tracks in this stream. */
+@property(nonatomic, strong, readonly) NSArray<RTCVideoTrack *> *videoTracks;
+
+/** An identifier for this media stream. */
+@property(nonatomic, readonly) NSString *streamId;
+
+- (instancetype)init NS_UNAVAILABLE;
+
+/** Adds the given audio track to this media stream. */
+- (void)addAudioTrack:(RTCAudioTrack *)audioTrack;
+
+/** Adds the given video track to this media stream. */
+- (void)addVideoTrack:(RTCVideoTrack *)videoTrack;
+
+/** Removes the given audio track to this media stream. */
+- (void)removeAudioTrack:(RTCAudioTrack *)audioTrack;
+
+/** Removes the given video track to this media stream. */
+- (void)removeVideoTrack:(RTCVideoTrack *)videoTrack;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCMediaStream.mm b/webrtc/api/objc/RTCMediaStream.mm
new file mode 100644
index 0000000..2c24d78
--- /dev/null
+++ b/webrtc/api/objc/RTCMediaStream.mm
@@ -0,0 +1,112 @@
+/*
+ *  Copyright 2015 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 "RTCMediaStream.h"
+
+#include <vector>
+
+#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/RTCVideoTrack+Private.h"
+#import "webrtc/base/objc/NSString+StdString.h"
+
+@implementation RTCMediaStream {
+  NSMutableArray *_audioTracks;
+  NSMutableArray *_videoTracks;
+  rtc::scoped_refptr<webrtc::MediaStreamInterface> _nativeMediaStream;
+}
+
+- (NSArray<RTCAudioTrack *> *)audioTracks {
+  return [_audioTracks copy];
+}
+
+- (NSArray<RTCVideoTrack *> *)videoTracks {
+  return [_videoTracks copy];
+}
+
+- (NSString *)streamId {
+  return [NSString stringForStdString:_nativeMediaStream->label()];
+}
+
+- (void)addAudioTrack:(RTCAudioTrack *)audioTrack {
+  if (_nativeMediaStream->AddTrack(audioTrack.nativeAudioTrack)) {
+    [_audioTracks addObject:audioTrack];
+  }
+}
+
+- (void)addVideoTrack:(RTCVideoTrack *)videoTrack {
+  if (_nativeMediaStream->AddTrack(videoTrack.nativeVideoTrack)) {
+    [_videoTracks addObject:videoTrack];
+  }
+}
+
+- (void)removeAudioTrack:(RTCAudioTrack *)audioTrack {
+  NSUInteger index = [_audioTracks indexOfObjectIdenticalTo:audioTrack];
+  NSAssert(index != NSNotFound,
+           @"|removeAudioTrack| called on unexpected RTCAudioTrack");
+  if (index != NSNotFound &&
+      _nativeMediaStream->RemoveTrack(audioTrack.nativeAudioTrack)) {
+    [_audioTracks removeObjectAtIndex:index];
+  }
+}
+
+- (void)removeVideoTrack:(RTCVideoTrack *)videoTrack {
+  NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:videoTrack];
+  NSAssert(index != NSNotFound,
+           @"|removeVideoTrack| called on unexpected RTCVideoTrack");
+  if (index != NSNotFound &&
+      _nativeMediaStream->RemoveTrack(videoTrack.nativeVideoTrack)) {
+    [_videoTracks removeObjectAtIndex:index];
+  }
+}
+
+- (NSString *)description {
+  return [NSString stringWithFormat:@"RTCMediaStream:\n%@\nA=%lu\nV=%lu",
+                                    self.streamId,
+                                    (unsigned long)self.audioTracks.count,
+                                    (unsigned long)self.videoTracks.count];
+}
+
+#pragma mark - Private
+
+- (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream {
+  return _nativeMediaStream;
+}
+
+- (instancetype)initWithNativeMediaStream:
+    (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream {
+  NSParameterAssert(nativeMediaStream);
+  if (self = [super init]) {
+    webrtc::AudioTrackVector audioTracks = nativeMediaStream->GetAudioTracks();
+    webrtc::VideoTrackVector videoTracks = nativeMediaStream->GetVideoTracks();
+
+    _audioTracks = [NSMutableArray arrayWithCapacity:audioTracks.size()];
+    _videoTracks = [NSMutableArray arrayWithCapacity:videoTracks.size()];
+    _nativeMediaStream = nativeMediaStream;
+
+    for (auto &track : audioTracks) {
+      RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeAudio;
+      RTCAudioTrack *audioTrack =
+          [[RTCAudioTrack alloc] initWithNativeTrack:track type:type];
+      [_audioTracks addObject:audioTrack];
+    }
+
+    for (auto &track : videoTracks) {
+      RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeVideo;
+      RTCVideoTrack *videoTrack =
+          [[RTCVideoTrack alloc] initWithNativeTrack:track type:type];
+      [_videoTracks addObject:videoTrack];
+    }
+  }
+  return self;
+}
+
+@end