Jon Hjelle | 2bf9a5f | 2016-01-21 16:14:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #import "RTCMediaStream.h" |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | #import "webrtc/api/objc/RTCAudioTrack+Private.h" |
| 16 | #import "webrtc/api/objc/RTCMediaStream+Private.h" |
| 17 | #import "webrtc/api/objc/RTCMediaStreamTrack+Private.h" |
| 18 | #import "webrtc/api/objc/RTCVideoTrack+Private.h" |
| 19 | #import "webrtc/base/objc/NSString+StdString.h" |
| 20 | |
hjon | 6b03995 | 2016-02-25 12:32:58 -0800 | [diff] [blame^] | 21 | // TODO(hjon): Update nullability types. See http://crbug/webrtc/5592 |
| 22 | |
Jon Hjelle | 2bf9a5f | 2016-01-21 16:14:11 -0800 | [diff] [blame] | 23 | @implementation RTCMediaStream { |
| 24 | NSMutableArray *_audioTracks; |
| 25 | NSMutableArray *_videoTracks; |
| 26 | rtc::scoped_refptr<webrtc::MediaStreamInterface> _nativeMediaStream; |
| 27 | } |
| 28 | |
hjon | 6b03995 | 2016-02-25 12:32:58 -0800 | [diff] [blame^] | 29 | - (NSArray *)audioTracks { |
| 30 | // - (NSArray<RTCAudioTrack *> *)audioTracks { |
Jon Hjelle | 2bf9a5f | 2016-01-21 16:14:11 -0800 | [diff] [blame] | 31 | return [_audioTracks copy]; |
| 32 | } |
| 33 | |
hjon | 6b03995 | 2016-02-25 12:32:58 -0800 | [diff] [blame^] | 34 | - (NSArray *)videoTracks { |
| 35 | // - (NSArray<RTCVideoTrack *> *)videoTracks { |
Jon Hjelle | 2bf9a5f | 2016-01-21 16:14:11 -0800 | [diff] [blame] | 36 | return [_videoTracks copy]; |
| 37 | } |
| 38 | |
| 39 | - (NSString *)streamId { |
| 40 | return [NSString stringForStdString:_nativeMediaStream->label()]; |
| 41 | } |
| 42 | |
| 43 | - (void)addAudioTrack:(RTCAudioTrack *)audioTrack { |
| 44 | if (_nativeMediaStream->AddTrack(audioTrack.nativeAudioTrack)) { |
| 45 | [_audioTracks addObject:audioTrack]; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | - (void)addVideoTrack:(RTCVideoTrack *)videoTrack { |
| 50 | if (_nativeMediaStream->AddTrack(videoTrack.nativeVideoTrack)) { |
| 51 | [_videoTracks addObject:videoTrack]; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | - (void)removeAudioTrack:(RTCAudioTrack *)audioTrack { |
| 56 | NSUInteger index = [_audioTracks indexOfObjectIdenticalTo:audioTrack]; |
| 57 | NSAssert(index != NSNotFound, |
| 58 | @"|removeAudioTrack| called on unexpected RTCAudioTrack"); |
| 59 | if (index != NSNotFound && |
| 60 | _nativeMediaStream->RemoveTrack(audioTrack.nativeAudioTrack)) { |
| 61 | [_audioTracks removeObjectAtIndex:index]; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | - (void)removeVideoTrack:(RTCVideoTrack *)videoTrack { |
| 66 | NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:videoTrack]; |
| 67 | NSAssert(index != NSNotFound, |
| 68 | @"|removeVideoTrack| called on unexpected RTCVideoTrack"); |
| 69 | if (index != NSNotFound && |
| 70 | _nativeMediaStream->RemoveTrack(videoTrack.nativeVideoTrack)) { |
| 71 | [_videoTracks removeObjectAtIndex:index]; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | - (NSString *)description { |
| 76 | return [NSString stringWithFormat:@"RTCMediaStream:\n%@\nA=%lu\nV=%lu", |
| 77 | self.streamId, |
| 78 | (unsigned long)self.audioTracks.count, |
| 79 | (unsigned long)self.videoTracks.count]; |
| 80 | } |
| 81 | |
| 82 | #pragma mark - Private |
| 83 | |
| 84 | - (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream { |
| 85 | return _nativeMediaStream; |
| 86 | } |
| 87 | |
| 88 | - (instancetype)initWithNativeMediaStream: |
| 89 | (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream { |
| 90 | NSParameterAssert(nativeMediaStream); |
| 91 | if (self = [super init]) { |
| 92 | webrtc::AudioTrackVector audioTracks = nativeMediaStream->GetAudioTracks(); |
| 93 | webrtc::VideoTrackVector videoTracks = nativeMediaStream->GetVideoTracks(); |
| 94 | |
| 95 | _audioTracks = [NSMutableArray arrayWithCapacity:audioTracks.size()]; |
| 96 | _videoTracks = [NSMutableArray arrayWithCapacity:videoTracks.size()]; |
| 97 | _nativeMediaStream = nativeMediaStream; |
| 98 | |
| 99 | for (auto &track : audioTracks) { |
| 100 | RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeAudio; |
| 101 | RTCAudioTrack *audioTrack = |
| 102 | [[RTCAudioTrack alloc] initWithNativeTrack:track type:type]; |
| 103 | [_audioTracks addObject:audioTrack]; |
| 104 | } |
| 105 | |
| 106 | for (auto &track : videoTracks) { |
| 107 | RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeVideo; |
| 108 | RTCVideoTrack *videoTrack = |
| 109 | [[RTCVideoTrack alloc] initWithNativeTrack:track type:type]; |
| 110 | [_videoTracks addObject:videoTrack]; |
| 111 | } |
| 112 | } |
| 113 | return self; |
| 114 | } |
| 115 | |
| 116 | @end |