forked from cordova-rtc/cordova-plugin-iosrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libwebrtc-objc-iosrtc.patch
284 lines (270 loc) · 9.29 KB
/
libwebrtc-objc-iosrtc.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
--- a/talk/app/webrtc/objc/RTCMediaStream+Internal.h
+++ b/talk/app/webrtc/objc/RTCMediaStream+Internal.h
@@ -37,4 +37,12 @@
- (id)initWithMediaStream:
(rtc::scoped_refptr<webrtc::MediaStreamInterface>)mediaStream;
+- (void)update;
+- (BOOL)hasNativeAudioTrack:(RTCAudioTrack *)objcTrack;
+- (BOOL)hasNativeVideoTrack:(RTCVideoTrack *)objcTrack;
+- (BOOL)hasObjcAudioTrack:
+ (rtc::scoped_refptr<webrtc::AudioTrackInterface>)nativeTrack;
+- (BOOL)hasObjcVideoTrack:
+ (rtc::scoped_refptr<webrtc::VideoTrackInterface>)nativeTrack;
+
@end
--- a/talk/app/webrtc/objc/RTCMediaStream.mm
+++ b/talk/app/webrtc/objc/RTCMediaStream.mm
@@ -31,16 +31,31 @@
#import "RTCMediaStream+Internal.h"
-#import "RTCAudioTrack+Internal.h"
#import "RTCMediaStreamTrack+Internal.h"
+#import "RTCAudioTrack+Internal.h"
#import "RTCVideoTrack+Internal.h"
#include "webrtc/api/mediastreaminterface.h"
+namespace webrtc {
+ class RTCMediaStreamObserver : public ObserverInterface {
+ public:
+ RTCMediaStreamObserver(RTCMediaStream* stream) { _stream = stream; }
+
+ void OnChanged() override {
+ [_stream update];
+ }
+
+ private:
+ __weak RTCMediaStream* _stream;
+ };
+}
+
@implementation RTCMediaStream {
NSMutableArray* _audioTracks;
NSMutableArray* _videoTracks;
rtc::scoped_refptr<webrtc::MediaStreamInterface> _mediaStream;
+ std::unique_ptr<webrtc::RTCMediaStreamObserver> _observer;
}
- (NSString*)description {
@@ -64,7 +79,6 @@ - (NSString*)label {
- (BOOL)addAudioTrack:(RTCAudioTrack*)track {
if (self.mediaStream->AddTrack(track.audioTrack)) {
- [_audioTracks addObject:track];
return YES;
}
return NO;
@@ -72,7 +86,6 @@ - (BOOL)addAudioTrack:(RTCAudioTrack*)track {
- (BOOL)addVideoTrack:(RTCVideoTrack*)track {
if (self.mediaStream->AddTrack(track.nativeVideoTrack)) {
- [_videoTracks addObject:track];
return YES;
}
return NO;
@@ -83,7 +96,6 @@ - (BOOL)removeAudioTrack:(RTCAudioTrack*)track {
NSAssert(index != NSNotFound,
@"|removeAudioTrack| called on unexpected RTCAudioTrack");
if (index != NSNotFound && self.mediaStream->RemoveTrack(track.audioTrack)) {
- [_audioTracks removeObjectAtIndex:index];
return YES;
}
return NO;
@@ -93,9 +105,7 @@ - (BOOL)removeVideoTrack:(RTCVideoTrack*)track {
NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:track];
NSAssert(index != NSNotFound,
@"|removeAudioTrack| called on unexpected RTCVideoTrack");
- if (index != NSNotFound &&
- self.mediaStream->RemoveTrack(track.nativeVideoTrack)) {
- [_videoTracks removeObjectAtIndex:index];
+ if (index != NSNotFound && self.mediaStream->RemoveTrack(track.nativeVideoTrack)) {
return YES;
}
return NO;
@@ -119,6 +129,8 @@ - (id)initWithMediaStream:
_audioTracks = [NSMutableArray arrayWithCapacity:audio_tracks.size()];
_videoTracks = [NSMutableArray arrayWithCapacity:video_tracks.size()];
_mediaStream = mediaStream;
+ _observer.reset(new webrtc::RTCMediaStreamObserver(self));
+ _mediaStream->RegisterObserver(_observer.get());
for (size_t i = 0; i < audio_tracks.size(); ++i) {
rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
@@ -139,8 +151,157 @@ - (id)initWithMediaStream:
return self;
}
+- (void)dealloc {
+ _mediaStream->UnregisterObserver(_observer.get());
+}
+
- (rtc::scoped_refptr<webrtc::MediaStreamInterface>)mediaStream {
return _mediaStream;
}
+- (void)update {
+ size_t i;
+ webrtc::AudioTrackVector native_audio_tracks = _mediaStream->GetAudioTracks();
+ webrtc::VideoTrackVector native_video_tracks = _mediaStream->GetVideoTracks();
+ std::vector<size_t> removedAudioTrackIndexes;
+ std::vector<size_t> removedVideoTrackIndexes;
+
+ // Detect audio tracks removal.
+ for (i = 0; i < [_audioTracks count]; i++) {
+ RTCAudioTrack* objcTrack = [_audioTracks objectAtIndex:i];
+ NSNumber* index;
+
+ if (![self hasNativeAudioTrack:objcTrack]) {
+ removedAudioTrackIndexes.push_back(i);
+ }
+ }
+
+ // Detect video tracks removal.
+ for (i = 0; i < [_videoTracks count]; i++) {
+ RTCVideoTrack* objcTrack = [_videoTracks objectAtIndex:i];
+ NSNumber* index;
+
+ if (![self hasNativeVideoTrack:objcTrack]) {
+ removedVideoTrackIndexes.push_back(i);
+ }
+ }
+
+ // Remove old audio tracks and notify the delegate.
+ for (std::vector<size_t>::iterator it = removedAudioTrackIndexes.begin();
+ it != removedAudioTrackIndexes.end(); ++it) {
+ RTCAudioTrack* objcTrack = [_audioTracks objectAtIndex:*it];
+
+ // Remove the track from the ObjC container.
+ [_audioTracks removeObjectAtIndex:*it];
+
+ // Notify the delegate.
+ [_delegate OnRemoveAudioTrack:self track:objcTrack];
+ }
+
+ // Remove old video tracks and notify the delegate.
+ for (std::vector<size_t>::iterator it = removedVideoTrackIndexes.begin();
+ it != removedVideoTrackIndexes.end(); ++it) {
+ RTCVideoTrack* objcTrack = [_videoTracks objectAtIndex:*it];
+
+ // Remove the track from the ObjC container.
+ [_videoTracks removeObjectAtIndex:*it];
+
+ // Notify the delegate.
+ [_delegate OnRemoveVideoTrack:self track:objcTrack];
+ }
+
+ // Detect audio tracks addition and notify the delegate.
+ for (i = 0; i < native_audio_tracks.size(); ++i) {
+ rtc::scoped_refptr<webrtc::AudioTrackInterface> nativeTrack =
+ native_audio_tracks[i];
+
+ if (![self hasObjcAudioTrack:nativeTrack]) {
+ // Create the RTCAudioTrack instance and add it to the ObjC container.
+ RTCAudioTrack* objcTrack =
+ [[RTCAudioTrack alloc] initWithMediaTrack:nativeTrack];
+ [_audioTracks addObject:objcTrack];
+
+ // Notify the delegate.
+ [_delegate OnAddAudioTrack:self track:objcTrack];
+ }
+ }
+
+ // Detect video tracks addition and notify the delegate.
+ for (i = 0; i < native_video_tracks.size(); ++i) {
+ rtc::scoped_refptr<webrtc::VideoTrackInterface> nativeTrack =
+ native_video_tracks[i];
+
+ if (![self hasObjcVideoTrack:nativeTrack]) {
+ // Create the RTCVideoTrack instance and add it to the ObjC container.
+ RTCVideoTrack* objcTrack =
+ [[RTCVideoTrack alloc] initWithMediaTrack:nativeTrack];
+ [_videoTracks addObject:objcTrack];
+
+ // Notify the delegate.
+ [_delegate OnAddVideoTrack:self track:objcTrack];
+ }
+ }
+
+ unsigned long num_native_audios = native_audio_tracks.size();
+ unsigned long num_native_videos = native_video_tracks.size();
+ unsigned long num_objc_audios = (unsigned long)[_audioTracks count];
+ unsigned long num_objc_videos = (unsigned long)[_videoTracks count];
+
+ NSAssert(num_native_audios == num_objc_audios,
+ @"ObjC audio tracks does not match number of native audio tracks");
+
+ NSAssert(num_native_videos == num_objc_videos,
+ @"ObjC video tracks does not match number of native video tracks");
+}
+
+- (BOOL)hasNativeAudioTrack:(RTCAudioTrack *)objcTrack {
+ webrtc::AudioTrackVector audio_tracks = _mediaStream->GetAudioTracks();
+
+ for (size_t i = 0; i < audio_tracks.size(); ++i) {
+ rtc::scoped_refptr<webrtc::AudioTrackInterface> track = audio_tracks[i];
+
+ if (track->id().compare(objcTrack.audioTrack->id()) == 0) {
+ return YES;
+ }
+ }
+ return NO;
+}
+
+- (BOOL)hasNativeVideoTrack:(RTCVideoTrack *)objcTrack {
+ webrtc::VideoTrackVector video_tracks = _mediaStream->GetVideoTracks();
+
+ for (size_t i = 0; i < video_tracks.size(); ++i) {
+ rtc::scoped_refptr<webrtc::VideoTrackInterface> track = video_tracks[i];
+
+ if (track->id().compare(objcTrack.nativeVideoTrack->id()) == 0) {
+ return YES;
+ }
+ }
+ return NO;
+}
+
+- (BOOL)hasObjcAudioTrack:
+ (rtc::scoped_refptr<webrtc::AudioTrackInterface>)nativeTrack {
+ for (size_t i = 0; i < [_audioTracks count]; i++) {
+ RTCAudioTrack* track = [_audioTracks objectAtIndex:i];
+
+ if (track.audioTrack->id().compare(nativeTrack->id()) == 0) {
+ return YES;
+ }
+ }
+ return NO;
+}
+
+- (BOOL)hasObjcVideoTrack:
+ (rtc::scoped_refptr<webrtc::VideoTrackInterface>)nativeTrack {
+ for (size_t i = 0; i < [_videoTracks count]; i++) {
+ RTCVideoTrack* track = [_videoTracks objectAtIndex:i];
+
+ if (track.nativeVideoTrack->id().compare(nativeTrack->id()) == 0) {
+ return YES;
+ }
+ }
+ return NO;
+}
+
@end
--- a/talk/app/webrtc/objc/public/RTCMediaStream.h
+++ b/talk/app/webrtc/objc/public/RTCMediaStream.h
@@ -29,6 +29,21 @@
@class RTCAudioTrack;
@class RTCVideoTrack;
+@class RTCMediaStream;
+@class RTCMediaStreamTrack;
+
+@protocol RTCMediaStreamDelegate<NSObject>
+
+- (void)OnAddAudioTrack:
+ (RTCMediaStream*)mediaStream track:(RTCMediaStreamTrack*)track;
+- (void)OnAddVideoTrack:
+ (RTCMediaStream*)mediaStream track:(RTCMediaStreamTrack*)track;
+- (void)OnRemoveAudioTrack:
+ (RTCMediaStream*)mediaStream track:(RTCMediaStreamTrack*)track;
+- (void)OnRemoveVideoTrack:
+ (RTCMediaStream*)mediaStream track:(RTCMediaStreamTrack*)track;
+
+@end
// RTCMediaStream is an ObjectiveC wrapper for MediaStreamInterface.
@interface RTCMediaStream : NSObject
@@ -36,6 +51,7 @@
@property(nonatomic, strong, readonly) NSArray *audioTracks;
@property(nonatomic, strong, readonly) NSArray *videoTracks;
@property(nonatomic, strong, readonly) NSString *label;
+@property(nonatomic, weak) id<RTCMediaStreamDelegate> delegate;
- (BOOL)addAudioTrack:(RTCAudioTrack *)track;
- (BOOL)addVideoTrack:(RTCVideoTrack *)track;