From 2bf9a5f11b60025a588fa58d883a191577d0e350 Mon Sep 17 00:00:00 2001 From: Jon Hjelle Date: Thu, 21 Jan 2016 16:14:11 -0800 Subject: [PATCH] Update API for Objective-C RTCMediaStream. BUG= R=tkchin@webrtc.org Review URL: https://codereview.webrtc.org/1558733002 . Patch from Jon Hjelle . Cr-Commit-Position: refs/heads/master@{#11351} --- webrtc/api/BUILD.gn | 9 +- webrtc/api/api.gyp | 9 +- webrtc/api/objc/RTCMediaStream+Private.h | 32 +++++++ webrtc/api/objc/RTCMediaStream.h | 45 +++++++++ webrtc/api/objc/RTCMediaStream.mm | 112 +++++++++++++++++++++++ 5 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 webrtc/api/objc/RTCMediaStream+Private.h create mode 100644 webrtc/api/objc/RTCMediaStream.h create mode 100644 webrtc/api/objc/RTCMediaStream.mm diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn index 9144984257..1c333e4c77 100644 --- a/webrtc/api/BUILD.gn +++ b/webrtc/api/BUILD.gn @@ -29,15 +29,18 @@ if (is_ios) { ] 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 5d40cd0f39..5f848d7663 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 0000000000..2c2662b6fa --- /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 nativeMediaStream; + +/** Initialize an RTCMediaStream from a native MediaStreamInterface. */ +- (instancetype)initWithNativeMediaStream: + (rtc::scoped_refptr)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 0000000000..c63f9ed443 --- /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 + +NS_ASSUME_NONNULL_BEGIN + +@class RTCAudioTrack; +@class RTCVideoTrack; + +@interface RTCMediaStream : NSObject + +/** The audio tracks in this stream. */ +@property(nonatomic, strong, readonly) NSArray *audioTracks; + +/** The video tracks in this stream. */ +@property(nonatomic, strong, readonly) NSArray *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 0000000000..2c24d78759 --- /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 + +#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 _nativeMediaStream; +} + +- (NSArray *)audioTracks { + return [_audioTracks copy]; +} + +- (NSArray *)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)nativeMediaStream { + return _nativeMediaStream; +} + +- (instancetype)initWithNativeMediaStream: + (rtc::scoped_refptr)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