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+Private.h"
12
13#include <vector>
14
15#import "RTCAudioTrack+Private.h"
16#import "RTCMediaStreamTrack+Private.h"
17#import "RTCPeerConnectionFactory+Private.h"
18#import "RTCVideoTrack+Private.h"
19#import "helpers/NSString+StdString.h"
20
21@implementation RTC_OBJC_TYPE (RTCMediaStream) {
22  RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory;
23  NSMutableArray *_audioTracks;
24  NSMutableArray *_videoTracks;
25  rtc::scoped_refptr<webrtc::MediaStreamInterface> _nativeMediaStream;
26}
27
28- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
29                       streamId:(NSString *)streamId {
30  NSParameterAssert(factory);
31  NSParameterAssert(streamId.length);
32  std::string nativeId = [NSString stdStringForString:streamId];
33  rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
34      factory.nativeFactory->CreateLocalMediaStream(nativeId);
35  return [self initWithFactory:factory nativeMediaStream:stream];
36}
37
38- (NSArray<RTC_OBJC_TYPE(RTCAudioTrack) *> *)audioTracks {
39  return [_audioTracks copy];
40}
41
42- (NSArray<RTC_OBJC_TYPE(RTCVideoTrack) *> *)videoTracks {
43  return [_videoTracks copy];
44}
45
46- (NSString *)streamId {
47  return [NSString stringForStdString:_nativeMediaStream->id()];
48}
49
50- (void)addAudioTrack:(RTC_OBJC_TYPE(RTCAudioTrack) *)audioTrack {
51  if (_nativeMediaStream->AddTrack(audioTrack.nativeAudioTrack)) {
52    [_audioTracks addObject:audioTrack];
53  }
54}
55
56- (void)addVideoTrack:(RTC_OBJC_TYPE(RTCVideoTrack) *)videoTrack {
57  if (_nativeMediaStream->AddTrack(videoTrack.nativeVideoTrack)) {
58    [_videoTracks addObject:videoTrack];
59  }
60}
61
62- (void)removeAudioTrack:(RTC_OBJC_TYPE(RTCAudioTrack) *)audioTrack {
63  NSUInteger index = [_audioTracks indexOfObjectIdenticalTo:audioTrack];
64  NSAssert(index != NSNotFound,
65           @"|removeAudioTrack| called on unexpected RTC_OBJC_TYPE(RTCAudioTrack)");
66  if (index != NSNotFound &&
67      _nativeMediaStream->RemoveTrack(audioTrack.nativeAudioTrack)) {
68    [_audioTracks removeObjectAtIndex:index];
69  }
70}
71
72- (void)removeVideoTrack:(RTC_OBJC_TYPE(RTCVideoTrack) *)videoTrack {
73  NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:videoTrack];
74  NSAssert(index != NSNotFound,
75           @"|removeVideoTrack| called on unexpected RTC_OBJC_TYPE(RTCVideoTrack)");
76  if (index != NSNotFound &&
77      _nativeMediaStream->RemoveTrack(videoTrack.nativeVideoTrack)) {
78    [_videoTracks removeObjectAtIndex:index];
79  }
80}
81
82- (NSString *)description {
83  return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCMediaStream):\n%@\nA=%lu\nV=%lu",
84                                    self.streamId,
85                                    (unsigned long)self.audioTracks.count,
86                                    (unsigned long)self.videoTracks.count];
87}
88
89#pragma mark - Private
90
91- (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream {
92  return _nativeMediaStream;
93}
94
95- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
96              nativeMediaStream:
97                  (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream {
98  NSParameterAssert(nativeMediaStream);
99  if (self = [super init]) {
100    _factory = factory;
101
102    webrtc::AudioTrackVector audioTracks = nativeMediaStream->GetAudioTracks();
103    webrtc::VideoTrackVector videoTracks = nativeMediaStream->GetVideoTracks();
104
105    _audioTracks = [NSMutableArray arrayWithCapacity:audioTracks.size()];
106    _videoTracks = [NSMutableArray arrayWithCapacity:videoTracks.size()];
107    _nativeMediaStream = nativeMediaStream;
108
109    for (auto &track : audioTracks) {
110      RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeAudio;
111      RTC_OBJC_TYPE(RTCAudioTrack) *audioTrack =
112          [[RTC_OBJC_TYPE(RTCAudioTrack) alloc] initWithFactory:_factory
113                                                    nativeTrack:track
114                                                           type:type];
115      [_audioTracks addObject:audioTrack];
116    }
117
118    for (auto &track : videoTracks) {
119      RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeVideo;
120      RTC_OBJC_TYPE(RTCVideoTrack) *videoTrack =
121          [[RTC_OBJC_TYPE(RTCVideoTrack) alloc] initWithFactory:_factory
122                                                    nativeTrack:track
123                                                           type:type];
124      [_videoTracks addObject:videoTrack];
125    }
126  }
127  return self;
128}
129
130@end
131