OLD | NEW |
| (Empty) |
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/RTCPeerConnectionFactory+Private.h" | |
19 #import "webrtc/api/objc/RTCVideoTrack+Private.h" | |
20 #import "webrtc/base/objc/NSString+StdString.h" | |
21 | |
22 @implementation RTCMediaStream { | |
23 NSMutableArray *_audioTracks; | |
24 NSMutableArray *_videoTracks; | |
25 rtc::scoped_refptr<webrtc::MediaStreamInterface> _nativeMediaStream; | |
26 } | |
27 | |
28 - (instancetype)initWithFactory:(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 initWithNativeMediaStream:stream]; | |
36 } | |
37 | |
38 - (NSArray<RTCAudioTrack *> *)audioTracks { | |
39 return [_audioTracks copy]; | |
40 } | |
41 | |
42 - (NSArray<RTCVideoTrack *> *)videoTracks { | |
43 return [_videoTracks copy]; | |
44 } | |
45 | |
46 - (NSString *)streamId { | |
47 return [NSString stringForStdString:_nativeMediaStream->label()]; | |
48 } | |
49 | |
50 - (void)addAudioTrack:(RTCAudioTrack *)audioTrack { | |
51 if (_nativeMediaStream->AddTrack(audioTrack.nativeAudioTrack)) { | |
52 [_audioTracks addObject:audioTrack]; | |
53 } | |
54 } | |
55 | |
56 - (void)addVideoTrack:(RTCVideoTrack *)videoTrack { | |
57 if (_nativeMediaStream->AddTrack(videoTrack.nativeVideoTrack)) { | |
58 [_videoTracks addObject:videoTrack]; | |
59 } | |
60 } | |
61 | |
62 - (void)removeAudioTrack:(RTCAudioTrack *)audioTrack { | |
63 NSUInteger index = [_audioTracks indexOfObjectIdenticalTo:audioTrack]; | |
64 NSAssert(index != NSNotFound, | |
65 @"|removeAudioTrack| called on unexpected RTCAudioTrack"); | |
66 if (index != NSNotFound && | |
67 _nativeMediaStream->RemoveTrack(audioTrack.nativeAudioTrack)) { | |
68 [_audioTracks removeObjectAtIndex:index]; | |
69 } | |
70 } | |
71 | |
72 - (void)removeVideoTrack:(RTCVideoTrack *)videoTrack { | |
73 NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:videoTrack]; | |
74 NSAssert(index != NSNotFound, | |
75 @"|removeVideoTrack| called on unexpected RTCVideoTrack"); | |
76 if (index != NSNotFound && | |
77 _nativeMediaStream->RemoveTrack(videoTrack.nativeVideoTrack)) { | |
78 [_videoTracks removeObjectAtIndex:index]; | |
79 } | |
80 } | |
81 | |
82 - (NSString *)description { | |
83 return [NSString stringWithFormat:@"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)initWithNativeMediaStream: | |
96 (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream { | |
97 NSParameterAssert(nativeMediaStream); | |
98 if (self = [super init]) { | |
99 webrtc::AudioTrackVector audioTracks = nativeMediaStream->GetAudioTracks(); | |
100 webrtc::VideoTrackVector videoTracks = nativeMediaStream->GetVideoTracks(); | |
101 | |
102 _audioTracks = [NSMutableArray arrayWithCapacity:audioTracks.size()]; | |
103 _videoTracks = [NSMutableArray arrayWithCapacity:videoTracks.size()]; | |
104 _nativeMediaStream = nativeMediaStream; | |
105 | |
106 for (auto &track : audioTracks) { | |
107 RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeAudio; | |
108 RTCAudioTrack *audioTrack = | |
109 [[RTCAudioTrack alloc] initWithNativeTrack:track type:type]; | |
110 [_audioTracks addObject:audioTrack]; | |
111 } | |
112 | |
113 for (auto &track : videoTracks) { | |
114 RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeVideo; | |
115 RTCVideoTrack *videoTrack = | |
116 [[RTCVideoTrack alloc] initWithNativeTrack:track type:type]; | |
117 [_videoTracks addObject:videoTrack]; | |
118 } | |
119 } | |
120 return self; | |
121 } | |
122 | |
123 @end | |
OLD | NEW |