react-native-webrtc 1.92.1 → 1.94.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/.prettierignore +9 -0
  2. package/.prettierrc.json +10 -0
  3. package/README.md +2 -2
  4. package/android/libs/.placeholder +0 -0
  5. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +21 -8
  6. package/android/src/main/java/com/oney/WebRTCModule/StringUtils.java +24 -0
  7. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +199 -255
  8. package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +10 -2
  9. package/apple/.placeholder +0 -0
  10. package/ios/RCTWebRTC/VideoCaptureController.h +2 -2
  11. package/ios/RCTWebRTC/VideoCaptureController.m +131 -48
  12. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +39 -20
  13. package/metro.config.js +3 -4
  14. package/metro.config.macos.js +7 -8
  15. package/package.json +20 -12
  16. package/react-native.config.js +5 -6
  17. package/{EventEmitter.js → src/EventEmitter.js} +1 -1
  18. package/{MediaDevices.js → src/MediaDevices.js} +6 -12
  19. package/src/MediaStream.js +128 -0
  20. package/src/MediaStreamError.js +12 -0
  21. package/src/MediaStreamErrorEvent.js +11 -0
  22. package/src/MediaStreamEvent.js +11 -0
  23. package/src/MediaStreamTrack.js +104 -0
  24. package/src/MediaStreamTrackEvent.js +11 -0
  25. package/src/MessageEvent.js +10 -0
  26. package/{Permissions.js → src/Permissions.js} +26 -23
  27. package/src/RTCDataChannel.js +143 -0
  28. package/src/RTCDataChannelEvent.js +11 -0
  29. package/src/RTCEvent.js +8 -0
  30. package/src/RTCIceCandidate.js +20 -0
  31. package/src/RTCIceCandidateEvent.js +15 -0
  32. package/src/RTCPeerConnection.js +349 -0
  33. package/src/RTCSessionDescription.js +22 -0
  34. package/{RTCUtil.js → src/RTCUtil.js} +77 -57
  35. package/src/RTCView.js +60 -0
  36. package/src/ScreenCapturePickerView.js +4 -0
  37. package/{getDisplayMedia.js → src/getDisplayMedia.js} +7 -6
  38. package/src/getUserMedia.js +98 -0
  39. package/src/index.js +44 -0
  40. package/tools/build-webrtc.py +61 -49
  41. package/tools/downloadBitcode.sh +4 -16
  42. package/tools/downloadWebRTC.js +100 -0
  43. package/MediaStream.js +0 -138
  44. package/MediaStreamError.js +0 -14
  45. package/MediaStreamErrorEvent.js +0 -12
  46. package/MediaStreamEvent.js +0 -12
  47. package/MediaStreamTrack.js +0 -118
  48. package/MediaStreamTrackEvent.js +0 -12
  49. package/MessageEvent.js +0 -11
  50. package/RTCDataChannel.js +0 -161
  51. package/RTCDataChannelEvent.js +0 -12
  52. package/RTCEvent.js +0 -9
  53. package/RTCIceCandidate.js +0 -21
  54. package/RTCIceCandidateEvent.js +0 -16
  55. package/RTCPeerConnection.js +0 -379
  56. package/RTCSessionDescription.js +0 -23
  57. package/RTCView.js +0 -70
  58. package/ScreenCapturePickerView.js +0 -7
  59. package/android/libs/libjingle_peerconnection.so.jar +0 -0
  60. package/android/libs/libwebrtc.jar +0 -0
  61. package/apple/WebRTC.xcframework.tgz +0 -0
  62. package/getUserMedia.js +0 -98
  63. package/index.js +0 -50
  64. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  65. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  66. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcuserdata/saghul.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  67. package/ios/RCTWebRTC.xcodeproj/xcuserdata/saghul.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
@@ -4,32 +4,36 @@
4
4
  #import <React/RCTLog.h>
5
5
 
6
6
 
7
- @implementation VideoCaptureController {
8
- RTCCameraVideoCapturer *_capturer;
9
- NSString *_deviceId;
10
- BOOL _running;
11
- BOOL _usingFrontCamera;
12
- int _width;
13
- int _height;
14
- int _fps;
15
- }
7
+ @interface VideoCaptureController ()
8
+
9
+ @property (nonatomic, strong) RTCCameraVideoCapturer *capturer;
10
+ @property (nonatomic, strong) AVCaptureDeviceFormat *selectedFormat;
11
+ @property (nonatomic, strong) AVCaptureDevice *device;
12
+ @property (nonatomic, copy) NSString *deviceId;
13
+ @property (nonatomic, assign) BOOL running;
14
+ @property (nonatomic, assign) BOOL usingFrontCamera;
15
+ @property (nonatomic, assign) int width;
16
+ @property (nonatomic, assign) int height;
17
+ @property (nonatomic, assign) int frameRate;
18
+
19
+ @end
16
20
 
17
- @dynamic frameRate;
21
+ @implementation VideoCaptureController
18
22
 
19
- -(instancetype)initWithCapturer:(RTCCameraVideoCapturer *)capturer
20
- andConstraints:(NSDictionary *)constraints {
23
+ - (instancetype)initWithCapturer:(RTCCameraVideoCapturer *)capturer
24
+ andConstraints:(NSDictionary *)constraints {
21
25
  self = [super init];
22
26
  if (self) {
23
- _capturer = capturer;
24
- _running = NO;
27
+ self.capturer = capturer;
28
+ self.running = NO;
25
29
 
26
30
  // Default to the front camera.
27
- _usingFrontCamera = YES;
31
+ self.usingFrontCamera = YES;
28
32
 
29
- _deviceId = constraints[@"deviceId"];
30
- _width = [constraints[@"width"] intValue];
31
- _height = [constraints[@"height"] intValue];
32
- _fps = [constraints[@"frameRate"] intValue];
33
+ self.deviceId = constraints[@"deviceId"];
34
+ self.width = [constraints[@"width"] intValue];
35
+ self.height = [constraints[@"height"] intValue];
36
+ self.frameRate = [constraints[@"frameRate"] intValue];
33
37
 
34
38
  id facingMode = constraints[@"facingMode"];
35
39
 
@@ -45,55 +49,59 @@
45
49
  position = AVCaptureDevicePositionFront;
46
50
  }
47
51
 
48
- _usingFrontCamera = position == AVCaptureDevicePositionFront;
52
+ self.usingFrontCamera = position == AVCaptureDevicePositionFront;
49
53
  }
50
54
  }
51
55
 
52
56
  return self;
53
57
  }
54
58
 
55
- -(void)startCapture {
56
- AVCaptureDevice *device;
57
- if (_deviceId) {
58
- device = [AVCaptureDevice deviceWithUniqueID:_deviceId];
59
+ - (void)dealloc {
60
+ self.device = NULL;
61
+ }
62
+
63
+ - (void)startCapture {
64
+ if (self.deviceId) {
65
+ self.device = [AVCaptureDevice deviceWithUniqueID:self.deviceId];
59
66
  }
60
- if (!device) {
67
+ if (!self.device) {
61
68
  AVCaptureDevicePosition position
62
- = _usingFrontCamera
69
+ = self.usingFrontCamera
63
70
  ? AVCaptureDevicePositionFront
64
71
  : AVCaptureDevicePositionBack;
65
- device = [self findDeviceForPosition:position];
72
+ self.device = [self findDeviceForPosition:position];
66
73
  }
67
74
 
68
- if (!device) {
75
+ if (!self.device) {
69
76
  RCTLogWarn(@"[VideoCaptureController] No capture devices found!");
70
77
 
71
78
  return;
72
79
  }
73
-
80
+
74
81
  AVCaptureDeviceFormat *format
75
- = [self selectFormatForDevice:device
76
- withTargetWidth:_width
77
- withTargetHeight:_height];
78
- if (format == nil) {
79
- RCTLogWarn(@"[VideoCaptureController] No valid formats for device %@", device);
82
+ = [self selectFormatForDevice:self.device
83
+ withTargetWidth:self.width
84
+ withTargetHeight:self.height];
85
+ if (!format) {
86
+ RCTLogWarn(@"[VideoCaptureController] No valid formats for device %@", self.device);
80
87
 
81
88
  return;
82
89
  }
83
-
84
- _selectedFormat = format;
90
+
91
+ self.selectedFormat = format;
85
92
 
86
93
  RCTLog(@"[VideoCaptureController] Capture will start");
87
94
 
88
95
  // Starting the capture happens on another thread. Wait for it.
89
96
  dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
90
-
91
- [_capturer startCaptureWithDevice:device format:format fps:_fps completionHandler:^(NSError *err) {
97
+
98
+ __weak VideoCaptureController *weakSelf = self;
99
+ [self.capturer startCaptureWithDevice:self.device format:format fps:self.frameRate completionHandler:^(NSError *err) {
92
100
  if (err) {
93
101
  RCTLogError(@"[VideoCaptureController] Error starting capture: %@", err);
94
102
  } else {
95
103
  RCTLog(@"[VideoCaptureController] Capture started");
96
- self->_running = YES;
104
+ weakSelf.running = YES;
97
105
  }
98
106
  dispatch_semaphore_signal(semaphore);
99
107
  }];
@@ -101,36 +109,81 @@
101
109
  dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
102
110
  }
103
111
 
104
- -(void)stopCapture {
105
- if (!_running)
112
+ - (void)stopCapture {
113
+ if (!self.running)
106
114
  return;
107
115
 
108
116
  RCTLog(@"[VideoCaptureController] Capture will stop");
109
117
  // Stopping the capture happens on another thread. Wait for it.
110
118
  dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
111
119
 
112
- [_capturer stopCaptureWithCompletionHandler:^{
120
+ __weak VideoCaptureController *weakSelf = self;
121
+ [self.capturer stopCaptureWithCompletionHandler:^{
113
122
  RCTLog(@"[VideoCaptureController] Capture stopped");
114
- self->_running = NO;
123
+ weakSelf.running = NO;
124
+ weakSelf.device = nil;
125
+
115
126
  dispatch_semaphore_signal(semaphore);
116
127
  }];
117
128
 
118
129
  dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
119
130
  }
120
131
 
121
- -(void)switchCamera {
122
- _usingFrontCamera = !_usingFrontCamera;
123
- _deviceId = NULL;
132
+ - (void)switchCamera {
133
+ self.usingFrontCamera = !self.usingFrontCamera;
134
+ self.deviceId = nil;
135
+ self.device = nil;
124
136
 
125
137
  [self startCapture];
126
138
  }
127
139
 
128
- -(int)frameRate {
129
- return _fps;
140
+ #pragma mark NSKeyValueObserving
141
+
142
+ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
143
+ if (@available(iOS 11.1, *)) {
144
+ if ([object isKindOfClass:[AVCaptureDevice class]] && [keyPath isEqualToString:@"systemPressureState"]) {
145
+ AVCaptureDevice *device = (AVCaptureDevice *)object;
146
+ AVCaptureSystemPressureLevel pressureLevel = ((AVCaptureSystemPressureState *)change[NSKeyValueChangeNewKey]).level;
147
+ if (pressureLevel == AVCaptureSystemPressureLevelSerious || pressureLevel == AVCaptureSystemPressureLevelCritical) {
148
+ RCTLogWarn(@"[VideoCaptureController] Reached elevated system pressure level: %@. Throttling frame rate.", pressureLevel);
149
+ [self throttleFrameRateForDevice:device];
150
+ }
151
+ else if (pressureLevel == AVCaptureSystemPressureLevelNominal) {
152
+ RCTLogWarn(@"[VideoCaptureController] Restored normal system pressure level. Resetting frame rate to default.");
153
+ [self resetFrameRateForDevice:device];
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ - (void)registerSystemPressureStateObserverForDevice:(AVCaptureDevice *)device {
160
+ if (@available(iOS 11.1, *)) {
161
+ [device addObserver:self
162
+ forKeyPath:@"systemPressureState"
163
+ options:NSKeyValueObservingOptionNew
164
+ context:nil];
165
+ }
166
+ }
167
+
168
+ - (void)removeObserverForDevice:(AVCaptureDevice *)device {
169
+ if (@available(iOS 11.1, *)) {
170
+ [device removeObserver:self forKeyPath:@"systemPressureState"];
171
+ }
130
172
  }
131
173
 
132
174
  #pragma mark Private
133
175
 
176
+ - (void)setDevice:(AVCaptureDevice *)device {
177
+ if (_device) {
178
+ [self removeObserverForDevice:_device];
179
+ }
180
+ if (device) {
181
+ [self registerSystemPressureStateObserverForDevice:device];
182
+ }
183
+
184
+ _device = device;
185
+ }
186
+
134
187
  - (AVCaptureDevice *)findDeviceForPosition:(AVCaptureDevicePosition)position {
135
188
  NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDevices];
136
189
  for (AVCaptureDevice *device in captureDevices) {
@@ -165,4 +218,34 @@
165
218
  return selectedFormat;
166
219
  }
167
220
 
221
+ - (void)throttleFrameRateForDevice:(AVCaptureDevice *)device {
222
+ NSError *error = nil;
223
+
224
+ [device lockForConfiguration:&error];
225
+ if (error) {
226
+ RCTLog(@"[VideoCaptureController] Could not lock device for configuration: %@", error);
227
+ return;
228
+ }
229
+
230
+ device.activeVideoMinFrameDuration = CMTimeMake(1, 20);
231
+ device.activeVideoMaxFrameDuration = CMTimeMake(1, 15);
232
+
233
+ [device unlockForConfiguration];
234
+ }
235
+
236
+ - (void)resetFrameRateForDevice:(AVCaptureDevice *)device {
237
+ NSError *error = nil;
238
+
239
+ [device lockForConfiguration:&error];
240
+ if (error) {
241
+ RCTLog(@"[VideoCaptureController] Could not lock device for configuration: %@", error);
242
+ return;
243
+ }
244
+
245
+ device.activeVideoMinFrameDuration = kCMTimeInvalid;
246
+ device.activeVideoMaxFrameDuration = kCMTimeInvalid;
247
+
248
+ [device unlockForConfiguration];
249
+ }
250
+
168
251
  @end
@@ -402,34 +402,53 @@ RCT_EXPORT_METHOD(peerConnectionRestartIce:(nonnull NSNumber *)objectID)
402
402
  [s appendString:key];
403
403
  [s appendString:@"\":"];
404
404
  NSObject *statisticsValue = [statistics.values objectForKey:key];
405
- if ([statisticsValue isKindOfClass:[NSArray class]]) {
406
- [s appendString:@"["];
407
- BOOL firstValue = YES;
408
- for (NSObject *value in (NSArray *)statisticsValue) {
409
- if(firstValue) {
405
+ [self appendValue:statisticsValue toString:s];
406
+ }
407
+
408
+ [s appendString:@"}]"];
409
+ }
410
+
411
+ [s appendString:@"]"];
412
+
413
+ return s;
414
+ }
415
+
416
+ - (void)appendValue:(NSObject *)statisticsValue toString:(NSMutableString *)s {
417
+ if ([statisticsValue isKindOfClass:[NSArray class]]) {
418
+ [s appendString:@"["];
419
+ BOOL firstValue = YES;
420
+ for (NSObject *element in (NSArray *)statisticsValue) {
421
+ if(firstValue) {
410
422
  firstValue = NO;
411
- } else {
423
+ } else {
412
424
  [s appendString:@","];
413
- }
414
-
415
- [s appendString:@"\""];
416
- [s appendString:[NSString stringWithFormat:@"%@", value]];
417
- [s appendString:@"\""];
418
425
  }
419
- [s appendString:@"]"];
420
- } else {
426
+
427
+ [s appendString:@"\""];
428
+ [s appendString:[NSString stringWithFormat:@"%@", element]];
429
+ [s appendString:@"\""];
430
+ }
431
+
432
+ [s appendString:@"]"];
433
+ } else if ([statisticsValue isKindOfClass:[NSDictionary class]]) {
434
+ NSError *error;
435
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:statisticsValue
436
+ options:0
437
+ error:&error];
438
+
439
+ if (!jsonData) {
421
440
  [s appendString:@"\""];
422
441
  [s appendString:[NSString stringWithFormat:@"%@", statisticsValue]];
423
442
  [s appendString:@"\""];
443
+ } else {
444
+ NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
445
+ [s appendString:jsonString];
424
446
  }
447
+ } else {
448
+ [s appendString:@"\""];
449
+ [s appendString:[NSString stringWithFormat:@"%@", statisticsValue]];
450
+ [s appendString:@"\""];
425
451
  }
426
-
427
- [s appendString:@"}]"];
428
- }
429
-
430
- [s appendString:@"]"];
431
-
432
- return s;
433
452
  }
434
453
 
435
454
  - (NSString *)stringForPeerConnectionState:(RTCPeerConnectionState)state {
package/metro.config.js CHANGED
@@ -1,8 +1,7 @@
1
-
2
1
  const blacklist = require('metro-config/src/defaults/blacklist');
3
2
 
4
3
  module.exports = {
5
- resolver: {
6
- blacklistRE: blacklist([/node_modules\/react-native-macos\/.*/])
7
- },
4
+ resolver: {
5
+ blacklistRE: blacklist([/node_modules\/react-native-macos\/.*/])
6
+ }
8
7
  };
@@ -1,15 +1,14 @@
1
-
2
1
  const path = require('path');
3
2
  const blacklist = require('metro-config/src/defaults/blacklist');
4
3
 
5
4
  const rnmPath = path.resolve(__dirname, 'node_modules/react-native-macos');
6
5
 
7
6
  module.exports = {
8
- resolver: {
9
- extraNodeModules: {
10
- 'react-native': rnmPath,
11
- },
12
- platforms: ['macos', 'ios', 'android'],
13
- blacklistRE: blacklist([/node_modules\/react-native\/.*/])
14
- },
7
+ resolver: {
8
+ extraNodeModules: {
9
+ 'react-native': rnmPath
10
+ },
11
+ platforms: ['macos', 'ios', 'android'],
12
+ blacklistRE: blacklist([/node_modules\/react-native\/.*/])
13
+ }
15
14
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-webrtc",
3
- "version": "1.92.1",
3
+ "version": "1.94.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/react-native-webrtc/react-native-webrtc.git"
@@ -16,25 +16,33 @@
16
16
  "android",
17
17
  "webrtc"
18
18
  ],
19
+ "main": "src/index.js",
19
20
  "dependencies": {
20
- "base64-js": "^1.1.2",
21
- "cross-os": "^1.3.0",
22
- "event-target-shim": "^1.0.5",
23
- "prop-types": "^15.5.10",
24
- "uuid": "^3.3.2"
21
+ "base64-js": "1.5.1",
22
+ "event-target-shim": "6.0.2",
23
+ "tar": "6.1.11"
25
24
  },
26
25
  "peerDependencies": {
27
26
  "react-native": ">=0.60.0"
28
27
  },
29
28
  "scripts": {
30
- "postinstall": "cross-os darwin-extract"
31
- },
32
- "cross-os": {
33
- "darwin-extract": {
34
- "darwin": "tar -zxf apple/WebRTC.xcframework.tgz -C apple"
35
- }
29
+ "postinstall": "node tools/downloadWebRTC.js",
30
+ "prepare": "husky install"
36
31
  },
37
32
  "bugs": {
38
33
  "url": "https://github.com/react-native-webrtc/react-native-webrtc/issues"
34
+ },
35
+ "devDependencies": {
36
+ "husky": "7.0.2",
37
+ "lint-staged": "11.2.3",
38
+ "prettier": "2.4.1"
39
+ },
40
+ "lint-staged": {
41
+ "**/*": "prettier --write --ignore-unknown"
42
+ },
43
+ "webrtc-builds": {
44
+ "android": "https://github.com/jitsi/webrtc/releases/download/v94.0.0/android-webrtc.tgz",
45
+ "ios": "https://github.com/jitsi/webrtc/releases/download/v94.0.0/WebRTC.xcframework.tgz",
46
+ "ios-bitcode": "https://github.com/jitsi/webrtc/releases/download/v94.0.0/WebRTC.xcframework-bitcode.tgz"
39
47
  }
40
48
  }
@@ -1,12 +1,11 @@
1
-
2
1
  'use strict';
3
2
 
4
3
  const macSwitch = '--use-react-native-macos';
5
4
 
6
5
  if (process.argv.includes(macSwitch)) {
7
- process.argv = process.argv.filter(arg => arg !== macSwitch);
8
- process.argv.push('--config=metro.config.macos.js');
9
- module.exports = {
10
- reactNativePath: 'node_modules/react-native-macos',
11
- };
6
+ process.argv = process.argv.filter(arg => arg !== macSwitch);
7
+ process.argv.push('--config=metro.config.macos.js');
8
+ module.exports = {
9
+ reactNativePath: 'node_modules/react-native-macos'
10
+ };
12
11
  }
@@ -1,4 +1,4 @@
1
- import {NativeModules, NativeEventEmitter} from 'react-native';
1
+ import { NativeModules, NativeEventEmitter } from 'react-native';
2
2
 
3
3
  const { WebRTCModule } = NativeModules;
4
4
 
@@ -1,21 +1,15 @@
1
- 'use strict';
2
1
 
3
- import {NativeModules} from 'react-native';
4
- import EventTarget from 'event-target-shim';
2
+ import { NativeModules } from 'react-native';
3
+ import { defineCustomEventTarget } from 'event-target-shim';
5
4
 
6
5
  import getDisplayMedia from './getDisplayMedia';
7
6
  import getUserMedia from './getUserMedia';
8
7
 
9
- const {WebRTCModule} = NativeModules;
8
+ const { WebRTCModule } = NativeModules;
10
9
 
11
- const MEDIA_DEVICES_EVENTS = [
12
- 'devicechange'
13
- ];
14
-
15
- class MediaDevices extends EventTarget(MEDIA_DEVICES_EVENTS) {
16
- // TODO: implement.
17
- ondevicechange: ?Function;
10
+ const MEDIA_DEVICES_EVENTS = ['devicechange'];
18
11
 
12
+ class MediaDevices extends defineCustomEventTarget(...MEDIA_DEVICES_EVENTS) {
19
13
  /**
20
14
  * W3C "Media Capture and Streams" compatible {@code enumerateDevices}
21
15
  * implementation.
@@ -40,7 +34,7 @@ class MediaDevices extends EventTarget(MEDIA_DEVICES_EVENTS) {
40
34
  * implementation.
41
35
  * See: https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-enumeratedevices
42
36
  *
43
- * @param {*} constraints
37
+ * @param {*} constraints
44
38
  * @returns {Promise}
45
39
  */
46
40
  getUserMedia(constraints) {
@@ -0,0 +1,128 @@
1
+
2
+ import { NativeModules } from 'react-native';
3
+ import { defineCustomEventTarget } from 'event-target-shim';
4
+ import { uniqueID } from './RTCUtil';
5
+
6
+ import MediaStreamTrack from './MediaStreamTrack';
7
+
8
+ const { WebRTCModule } = NativeModules;
9
+
10
+ const MEDIA_STREAM_EVENTS = ['active', 'inactive', 'addtrack', 'removetrack'];
11
+
12
+ export default class MediaStream extends defineCustomEventTarget(...MEDIA_STREAM_EVENTS) {
13
+ id: string;
14
+ active: boolean = true;
15
+
16
+ _tracks: Array<MediaStreamTrack> = [];
17
+
18
+ /**
19
+ * The identifier of this MediaStream unique within the associated
20
+ * WebRTCModule instance. As the id of a remote MediaStream instance is unique
21
+ * only within the associated RTCPeerConnection, it is not sufficiently unique
22
+ * to identify this MediaStream across multiple RTCPeerConnections and to
23
+ * unambiguously differentiate it from a local MediaStream instance not added
24
+ * to an RTCPeerConnection.
25
+ */
26
+ _reactTag: string;
27
+
28
+ /**
29
+ * A MediaStream can be constructed in several ways, depending on the paramters
30
+ * that are passed here.
31
+ *
32
+ * - undefined: just a new stream, with no tracks.
33
+ * - MediaStream instance: a new stream, with a copy of the tracks of the passed stream.
34
+ * - Array of MediaStreamTrack: a new stream with a copy of the tracks in the array.
35
+ * - object: a new stream instance, represented by the passed info object, this is always
36
+ * done internally, when the stream is first created in native and the JS wrapper is
37
+ * built afterwards.
38
+ */
39
+ constructor(arg) {
40
+ super();
41
+
42
+ // Assigm a UUID to start with. It may get overridden for remote streams.
43
+ this.id = uniqueID();
44
+ // Local MediaStreams are created by WebRTCModule to have their id and
45
+ // reactTag equal because WebRTCModule follows the respective standard's
46
+ // recommendation for id generation i.e. uses UUID which is unique enough
47
+ // for the purposes of reactTag.
48
+ this._reactTag = this.id;
49
+
50
+ if (typeof arg === 'undefined') {
51
+ WebRTCModule.mediaStreamCreate(this.id);
52
+ } else if (arg instanceof MediaStream) {
53
+ WebRTCModule.mediaStreamCreate(this.id);
54
+ for (const track of arg.getTracks()) {
55
+ this.addTrack(track);
56
+ }
57
+ } else if (Array.isArray(arg)) {
58
+ WebRTCModule.mediaStreamCreate(this.id);
59
+ for (const track of arg) {
60
+ this.addTrack(track);
61
+ }
62
+ } else if (typeof arg === 'object' && arg.streamId && arg.streamReactTag && arg.tracks) {
63
+ this.id = arg.streamId;
64
+ this._reactTag = arg.streamReactTag;
65
+ for (const trackInfo of arg.tracks) {
66
+ // We are not using addTrack here because the track is already part of the
67
+ // stream, so there is no need to add it on the native side.
68
+ this._tracks.push(new MediaStreamTrack(trackInfo));
69
+ }
70
+ } else {
71
+ throw new TypeError(`invalid type: ${typeof arg}`);
72
+ }
73
+ }
74
+
75
+ addTrack(track: MediaStreamTrack) {
76
+ const index = this._tracks.indexOf(track);
77
+ if (index !== -1) {
78
+ return;
79
+ }
80
+ this._tracks.push(track);
81
+ WebRTCModule.mediaStreamAddTrack(this._reactTag, track.id);
82
+ }
83
+
84
+ removeTrack(track: MediaStreamTrack) {
85
+ const index = this._tracks.indexOf(track);
86
+ if (index === -1) {
87
+ return;
88
+ }
89
+ this._tracks.splice(index, 1);
90
+ WebRTCModule.mediaStreamRemoveTrack(this._reactTag, track.id);
91
+ }
92
+
93
+ getTracks(): Array<MediaStreamTrack> {
94
+ return this._tracks.slice();
95
+ }
96
+
97
+ getTrackById(trackId): ?MediaStreamTrack {
98
+ return this._tracks.find(track => track.id === trackId);
99
+ }
100
+
101
+ getAudioTracks(): Array<MediaStreamTrack> {
102
+ return this._tracks.filter(track => track.kind === 'audio');
103
+ }
104
+
105
+ getVideoTracks(): Array<MediaStreamTrack> {
106
+ return this._tracks.filter(track => track.kind === 'video');
107
+ }
108
+
109
+ clone() {
110
+ throw new Error('Not implemented.');
111
+ }
112
+
113
+ toURL() {
114
+ return this._reactTag;
115
+ }
116
+
117
+ release(releaseTracks = true) {
118
+ const tracks = [...this._tracks];
119
+ for (const track of tracks) {
120
+ this.removeTrack(track);
121
+ if (releaseTracks) {
122
+ track.release();
123
+ }
124
+ }
125
+
126
+ WebRTCModule.mediaStreamRelease(this._reactTag);
127
+ }
128
+ }
@@ -0,0 +1,12 @@
1
+
2
+ export default class MediaStreamError {
3
+ name: string;
4
+ message: ?string;
5
+ constraintName: ?string;
6
+
7
+ constructor(error) {
8
+ this.name = error.name;
9
+ this.message = error.message;
10
+ this.constraintName = error.constraintName;
11
+ }
12
+ }
@@ -0,0 +1,11 @@
1
+
2
+ import type MediaStreamError from './MediaStreamError';
3
+
4
+ export default class MediaStreamErrorEvent {
5
+ type: string;
6
+ error: ?MediaStreamError;
7
+ constructor(type, eventInitDict) {
8
+ this.type = type.toString();
9
+ Object.assign(this, eventInitDict);
10
+ }
11
+ }
@@ -0,0 +1,11 @@
1
+
2
+ import type MediaStream from './MediaStream';
3
+
4
+ export default class MediaStreamEvent {
5
+ type: string;
6
+ stream: MediaStream;
7
+ constructor(type, eventInitDict) {
8
+ this.type = type.toString();
9
+ Object.assign(this, eventInitDict);
10
+ }
11
+ }