react-native-webrtc 1.92.2 → 1.94.0

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 (44) hide show
  1. package/.prettierignore +9 -0
  2. package/.prettierrc.json +10 -0
  3. package/android/libs/.placeholder +0 -0
  4. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +0 -5
  5. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +199 -255
  6. package/apple/.placeholder +0 -0
  7. package/ios/RCTWebRTC/VideoCaptureController.m +5 -3
  8. package/metro.config.js +3 -4
  9. package/metro.config.macos.js +7 -8
  10. package/package.json +20 -12
  11. package/react-native.config.js +5 -6
  12. package/src/EventEmitter.js +1 -1
  13. package/src/MediaDevices.js +6 -12
  14. package/src/MediaStream.js +120 -131
  15. package/src/MediaStreamError.js +8 -10
  16. package/src/MediaStreamErrorEvent.js +6 -7
  17. package/src/MediaStreamEvent.js +6 -7
  18. package/src/MediaStreamTrack.js +89 -103
  19. package/src/MediaStreamTrackEvent.js +6 -7
  20. package/src/MessageEvent.js +7 -8
  21. package/src/Permissions.js +26 -23
  22. package/src/RTCDataChannel.js +127 -145
  23. package/src/RTCDataChannelEvent.js +6 -7
  24. package/src/RTCEvent.js +5 -6
  25. package/src/RTCIceCandidate.js +15 -16
  26. package/src/RTCIceCandidateEvent.js +8 -9
  27. package/src/RTCPeerConnection.js +295 -325
  28. package/src/RTCSessionDescription.js +15 -16
  29. package/src/RTCUtil.js +56 -57
  30. package/src/RTCView.js +59 -69
  31. package/src/ScreenCapturePickerView.js +2 -5
  32. package/src/getDisplayMedia.js +7 -6
  33. package/src/getUserMedia.js +85 -85
  34. package/src/index.js +28 -34
  35. package/tools/build-webrtc.py +61 -49
  36. package/tools/downloadBitcode.sh +4 -16
  37. package/tools/downloadWebRTC.js +100 -0
  38. package/android/libs/libjingle_peerconnection.so.jar +0 -0
  39. package/android/libs/libwebrtc.jar +0 -0
  40. package/apple/WebRTC.xcframework.tgz +0 -0
  41. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  42. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  43. package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcuserdata/saghul.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  44. package/ios/RCTWebRTC.xcodeproj/xcuserdata/saghul.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
@@ -1,118 +1,104 @@
1
- 'use strict';
2
1
 
3
- import {NativeModules} from 'react-native';
4
- import EventTarget from 'event-target-shim';
5
- import MediaStreamErrorEvent from './MediaStreamErrorEvent';
6
- import type MediaStreamError from './MediaStreamError';
2
+ import { NativeModules } from 'react-native';
3
+ import { defineCustomEventTarget } from 'event-target-shim';
4
+
7
5
  import { deepClone } from './RTCUtil';
8
6
 
9
- const {WebRTCModule} = NativeModules;
10
-
11
- const MEDIA_STREAM_TRACK_EVENTS = [
12
- 'ended',
13
- 'mute',
14
- 'unmute',
15
- // see: https://www.w3.org/TR/mediacapture-streams/#constrainable-interface
16
- 'overconstrained',
17
- ];
18
-
19
- type MediaStreamTrackState = "live" | "ended";
20
-
21
- class MediaStreamTrack extends EventTarget(MEDIA_STREAM_TRACK_EVENTS) {
22
- _constraints: Object;
23
- _enabled: boolean;
24
- _settings: Object;
25
-
26
- id: string;
27
- kind: string;
28
- label: string;
29
- muted: boolean;
30
- // readyState in java: INITIALIZING, LIVE, ENDED, FAILED
31
- readyState: MediaStreamTrackState;
32
- remote: boolean;
33
-
34
- onended: ?Function;
35
- onmute: ?Function;
36
- onunmute: ?Function;
37
- overconstrained: ?Function;
38
-
39
- constructor(info) {
40
- super();
41
-
42
- this._constraints = info.constraints || {};
43
- this._enabled = info.enabled;
44
- this._settings = info.settings || {};
45
-
46
- this.id = info.id;
47
- this.kind = info.kind;
48
- this.label = info.label;
49
- this.muted = false;
50
- this.remote = info.remote;
51
-
52
- const _readyState = info.readyState.toLowerCase();
53
- this.readyState = (_readyState === "initializing"
54
- || _readyState === "live") ? "live" : "ended";
55
- }
56
-
57
- get enabled(): boolean {
58
- return this._enabled;
59
- }
60
-
61
- set enabled(enabled: boolean): void {
62
- if (enabled === this._enabled) {
63
- return;
7
+ const { WebRTCModule } = NativeModules;
8
+
9
+ const MEDIA_STREAM_TRACK_EVENTS = ['ended', 'mute', 'unmute'];
10
+
11
+ type MediaStreamTrackState = 'live' | 'ended';
12
+
13
+ class MediaStreamTrack extends defineCustomEventTarget(...MEDIA_STREAM_TRACK_EVENTS) {
14
+ _constraints: Object;
15
+ _enabled: boolean;
16
+ _settings: Object;
17
+
18
+ id: string;
19
+ kind: string;
20
+ label: string;
21
+ muted: boolean;
22
+ // readyState in java: INITIALIZING, LIVE, ENDED, FAILED
23
+ readyState: MediaStreamTrackState;
24
+ remote: boolean;
25
+
26
+ constructor(info) {
27
+ super();
28
+
29
+ this._constraints = info.constraints || {};
30
+ this._enabled = info.enabled;
31
+ this._settings = info.settings || {};
32
+
33
+ this.id = info.id;
34
+ this.kind = info.kind;
35
+ this.label = info.label;
36
+ this.muted = false;
37
+ this.remote = info.remote;
38
+
39
+ const _readyState = info.readyState.toLowerCase();
40
+ this.readyState = _readyState === 'initializing' || _readyState === 'live' ? 'live' : 'ended';
64
41
  }
65
- WebRTCModule.mediaStreamTrackSetEnabled(this.id, !this._enabled);
66
- this._enabled = !this._enabled;
67
- this.muted = !this._enabled;
68
- }
69
-
70
- stop() {
71
- WebRTCModule.mediaStreamTrackSetEnabled(this.id, false);
72
- this.readyState = 'ended';
73
- // TODO: save some stopped flag?
74
- }
75
-
76
- /**
77
- * Private / custom API for switching the cameras on the fly, without the
78
- * need for adding / removing tracks or doing any SDP renegotiation.
79
- *
80
- * This is how the reference application (AppRTCMobile) implements camera
81
- * switching.
82
- */
83
- _switchCamera() {
84
- if (this.remote) {
85
- throw new Error('Not implemented for remote tracks');
42
+
43
+ get enabled(): boolean {
44
+ return this._enabled;
86
45
  }
87
- if (this.kind !== 'video') {
88
- throw new Error('Only implemented for video tracks');
46
+
47
+ set enabled(enabled: boolean): void {
48
+ if (enabled === this._enabled) {
49
+ return;
50
+ }
51
+ WebRTCModule.mediaStreamTrackSetEnabled(this.id, !this._enabled);
52
+ this._enabled = !this._enabled;
53
+ this.muted = !this._enabled;
89
54
  }
90
- WebRTCModule.mediaStreamTrackSwitchCamera(this.id);
91
- }
92
55
 
93
- applyConstraints() {
94
- throw new Error('Not implemented.');
95
- }
56
+ stop() {
57
+ WebRTCModule.mediaStreamTrackSetEnabled(this.id, false);
58
+ this.readyState = 'ended';
59
+ // TODO: save some stopped flag?
60
+ }
61
+
62
+ /**
63
+ * Private / custom API for switching the cameras on the fly, without the
64
+ * need for adding / removing tracks or doing any SDP renegotiation.
65
+ *
66
+ * This is how the reference application (AppRTCMobile) implements camera
67
+ * switching.
68
+ */
69
+ _switchCamera() {
70
+ if (this.remote) {
71
+ throw new Error('Not implemented for remote tracks');
72
+ }
73
+ if (this.kind !== 'video') {
74
+ throw new Error('Only implemented for video tracks');
75
+ }
76
+ WebRTCModule.mediaStreamTrackSwitchCamera(this.id);
77
+ }
78
+
79
+ applyConstraints() {
80
+ throw new Error('Not implemented.');
81
+ }
96
82
 
97
- clone() {
98
- throw new Error('Not implemented.');
99
- }
83
+ clone() {
84
+ throw new Error('Not implemented.');
85
+ }
100
86
 
101
- getCapabilities() {
102
- throw new Error('Not implemented.');
103
- }
87
+ getCapabilities() {
88
+ throw new Error('Not implemented.');
89
+ }
104
90
 
105
- getConstraints() {
106
- return deepClone(this._constraints);
107
- }
91
+ getConstraints() {
92
+ return deepClone(this._constraints);
93
+ }
108
94
 
109
- getSettings() {
110
- return deepClone(this._settings);
111
- }
95
+ getSettings() {
96
+ return deepClone(this._settings);
97
+ }
112
98
 
113
- release() {
114
- WebRTCModule.mediaStreamTrackRelease(this.id);
115
- }
99
+ release() {
100
+ WebRTCModule.mediaStreamTrackRelease(this.id);
101
+ }
116
102
  }
117
103
 
118
104
  export default MediaStreamTrack;
@@ -1,12 +1,11 @@
1
- 'use strict';
2
1
 
3
2
  import type MediaStreamTrack from './MediaStreamTrack';
4
3
 
5
4
  export default class MediaStreamTrackEvent {
6
- type: string;
7
- track: MediaStreamTrack;
8
- constructor(type, eventInitDict) {
9
- this.type = type.toString();
10
- Object.assign(this, eventInitDict);
11
- }
5
+ type: string;
6
+ track: MediaStreamTrack;
7
+ constructor(type, eventInitDict) {
8
+ this.type = type.toString();
9
+ Object.assign(this, eventInitDict);
10
+ }
12
11
  }
@@ -1,11 +1,10 @@
1
- 'use strict';
2
1
 
3
2
  export default class MessageEvent {
4
- type: string;
5
- data: string | ArrayBuffer | Blob;
6
- origin: string;
7
- constructor(type, eventInitDict) {
8
- this.type = type.toString();
9
- Object.assign(this, eventInitDict);
10
- }
3
+ type: string;
4
+ data: string | ArrayBuffer | Blob;
5
+ origin: string;
6
+ constructor(type, eventInitDict) {
7
+ this.type = type.toString();
8
+ Object.assign(this, eventInitDict);
9
+ }
11
10
  }
@@ -1,4 +1,3 @@
1
- 'use strict';
2
1
 
3
2
  import { NativeModules, PermissionsAndroid, Platform } from 'react-native';
4
3
 
@@ -8,8 +7,8 @@ const { WebRTCModule } = NativeModules;
8
7
  * Type declaration for a permissions descriptor.
9
8
  */
10
9
  type PermissionDescriptor = {
11
- name: string;
12
- }
10
+ name: string
11
+ };
13
12
 
14
13
  /**
15
14
  * Class implementing a subset of W3C's Permissions API as defined by:
@@ -30,7 +29,7 @@ class Permissions {
30
29
  * This implementation only supports requesting these permissions, a subset
31
30
  * of: https://www.w3.org/TR/permissions/#permission-registry
32
31
  */
33
- VALID_PERMISSIONS = [ 'camera', 'microphone' ];
32
+ VALID_PERMISSIONS = ['camera', 'microphone'];
34
33
 
35
34
  _lastReq = Promise.resolve();
36
35
 
@@ -39,7 +38,7 @@ class Permissions {
39
38
  * can be requested at a time (unless the multi-permission API is used,
40
39
  * but we are not using that for symmetry with the W3C API for querying)
41
40
  * so we'll queue them up.
42
- *
41
+ *
43
42
  * @param {string} perm - The requested permission from
44
43
  * {@link PermissionsAndroid.PERMISSIONS}
45
44
  * https://facebook.github.io/react-native/docs/permissionsandroid#permissions-that-require-prompting-the-user
@@ -48,7 +47,8 @@ class Permissions {
48
47
  return new Promise((resolve, reject) => {
49
48
  PermissionsAndroid.request(perm).then(
50
49
  granted => resolve(granted === true || granted === PermissionsAndroid.RESULTS.GRANTED),
51
- () => resolve(false));
50
+ () => resolve(false)
51
+ );
52
52
  });
53
53
  }
54
54
 
@@ -56,14 +56,16 @@ class Permissions {
56
56
  * Validates the given permission descriptor.
57
57
  */
58
58
  _validatePermissionDescriptior(permissionDesc) {
59
- if (typeof permissionDesc !== "object") {
60
- throw new TypeError("Argument 1 of Permissions.query is not an object.");
59
+ if (typeof permissionDesc !== 'object') {
60
+ throw new TypeError('Argument 1 of Permissions.query is not an object.');
61
61
  }
62
- if (typeof permissionDesc.name === "undefined") {
62
+ if (typeof permissionDesc.name === 'undefined') {
63
63
  throw new TypeError("Missing required 'name' member of PermissionDescriptor.");
64
64
  }
65
65
  if (this.VALID_PERMISSIONS.indexOf(permissionDesc.name) === -1) {
66
- throw new TypeError("'name' member of PermissionDescriptor is not a valid value for enumeration PermissionName.");
66
+ throw new TypeError(
67
+ "'name' member of PermissionDescriptor is not a valid value for enumeration PermissionName."
68
+ );
67
69
  }
68
70
  }
69
71
 
@@ -78,18 +80,20 @@ class Permissions {
78
80
  return Promise.reject(e);
79
81
  }
80
82
  if (Platform.OS === 'android') {
81
- const perm = permissionDesc.name === 'camera'
82
- ? PermissionsAndroid.PERMISSIONS.CAMERA
83
- : PermissionsAndroid.PERMISSIONS.RECORD_AUDIO;
83
+ const perm =
84
+ permissionDesc.name === 'camera'
85
+ ? PermissionsAndroid.PERMISSIONS.CAMERA
86
+ : PermissionsAndroid.PERMISSIONS.RECORD_AUDIO;
84
87
  return new Promise((resolve, reject) => {
85
88
  PermissionsAndroid.check(perm).then(
86
89
  granted => resolve(granted ? this.RESULT.GRANTED : this.RESULT.PROMPT),
87
- () => resolve(this.RESULT.PROMPT));
90
+ () => resolve(this.RESULT.PROMPT)
91
+ );
88
92
  });
89
93
  } else if (Platform.OS === 'ios' || Platform.OS === 'macos') {
90
94
  return WebRTCModule.checkPermission(permissionDesc.name);
91
95
  } else {
92
- return Promise.reject(new TypeError("Unsupported platform."));
96
+ return Promise.reject(new TypeError('Unsupported platform.'));
93
97
  }
94
98
  }
95
99
 
@@ -104,18 +108,17 @@ class Permissions {
104
108
  return Promise.reject(e);
105
109
  }
106
110
  if (Platform.OS === 'android') {
107
- const perm = permissionDesc.name === 'camera'
108
- ? PermissionsAndroid.PERMISSIONS.CAMERA
109
- : PermissionsAndroid.PERMISSIONS.RECORD_AUDIO;
110
- const requestPermission
111
- = () => this._requestPermissionAndroid(perm);
112
- this._lastReq
113
- = this._lastReq.then(requestPermission, requestPermission);
111
+ const perm =
112
+ permissionDesc.name === 'camera'
113
+ ? PermissionsAndroid.PERMISSIONS.CAMERA
114
+ : PermissionsAndroid.PERMISSIONS.RECORD_AUDIO;
115
+ const requestPermission = () => this._requestPermissionAndroid(perm);
116
+ this._lastReq = this._lastReq.then(requestPermission, requestPermission);
114
117
  return this._lastReq;
115
118
  } else if (Platform.OS === 'ios' || Platform.OS === 'macos') {
116
119
  return WebRTCModule.requestPermission(permissionDesc.name);
117
120
  } else {
118
- return Promise.reject(new TypeError("Unsupported platform."));
121
+ return Promise.reject(new TypeError('Unsupported platform.'));
119
122
  }
120
123
  }
121
124
  }
@@ -1,161 +1,143 @@
1
- 'use strict';
2
1
 
3
2
  import { NativeModules } from 'react-native';
4
3
  import base64 from 'base64-js';
5
- import EventTarget from 'event-target-shim';
4
+ import { defineCustomEventTarget } from 'event-target-shim';
6
5
  import MessageEvent from './MessageEvent';
7
6
  import RTCDataChannelEvent from './RTCDataChannelEvent';
8
7
  import EventEmitter from './EventEmitter';
9
8
 
10
- const {WebRTCModule} = NativeModules;
11
-
12
- type RTCDataChannelState =
13
- 'connecting' |
14
- 'open' |
15
- 'closing' |
16
- 'closed';
17
-
18
- const DATA_CHANNEL_EVENTS = [
19
- 'open',
20
- 'message',
21
- 'bufferedamountlow',
22
- 'closing',
23
- 'close',
24
- 'error',
25
- ];
26
-
27
- export default class RTCDataChannel extends EventTarget(DATA_CHANNEL_EVENTS) {
28
- _peerConnectionId: number;
29
- _reactTag: string;
30
-
31
- _id: number;
32
- _label: string;
33
- _maxPacketLifeTime: ?number;
34
- _maxRetransmits: ?number;
35
- _negotiated: boolean;
36
- _ordered: boolean;
37
- _protocol: string;
38
- _readyState: RTCDataChannelState;
39
-
40
- binaryType: 'arraybuffer' = 'arraybuffer'; // we only support 'arraybuffer'
41
- bufferedAmount: number = 0;
42
- bufferedAmountLowThreshold: number = 0;
43
-
44
- onopen: ?Function;
45
- onmessage: ?Function;
46
- onbufferedamountlow: ?Function;
47
- onerror: ?Function;
48
- onclose: ?Function;
49
-
50
- constructor(info) {
51
- super();
52
-
53
- this._peerConnectionId = info.peerConnectionId;
54
- this._reactTag = info.reactTag;
55
-
56
- this._label = info.label;
57
- this._id = info.id === -1 ? null : info.id; // null until negotiated.
58
- this._ordered = Boolean(info.ordered);
59
- this._maxPacketLifeTime = info.maxPacketLifeTime;
60
- this._maxRetransmits = info.maxRetransmits;
61
- this._protocol = info.protocol || '';
62
- this._negotiated = Boolean(info.negotiated);
63
- this._readyState = info.readyState;
64
-
65
- this._registerEvents();
66
- }
67
-
68
- get label(): string {
69
- return this._label;
70
- }
71
-
72
- get id(): number {
73
- return this._id;
74
- }
75
-
76
- get ordered(): boolean {
77
- return this._ordered;
78
- }
79
-
80
- get maxPacketLifeTime(): number {
81
- return this._maxPacketLifeTime;
82
- }
83
-
84
- get maxRetransmits(): number {
85
- return this._maxRetransmits;
86
- }
87
-
88
- get protocol(): string {
89
- return this._protocol;
90
- }
91
-
92
- get negotiated(): boolean {
93
- return this._negotiated;
94
- }
95
-
96
- get readyState(): string {
97
- return this._readyState;
98
- }
99
-
100
- send(data: string | ArrayBuffer | ArrayBufferView) {
101
- if (typeof data === 'string') {
102
- WebRTCModule.dataChannelSend(this._peerConnectionId, this._reactTag, data, 'text');
103
- return;
9
+ const { WebRTCModule } = NativeModules;
10
+
11
+ type RTCDataChannelState = 'connecting' | 'open' | 'closing' | 'closed';
12
+
13
+ const DATA_CHANNEL_EVENTS = ['open', 'message', 'bufferedamountlow', 'closing', 'close', 'error'];
14
+
15
+ export default class RTCDataChannel extends defineCustomEventTarget(...DATA_CHANNEL_EVENTS) {
16
+ _peerConnectionId: number;
17
+ _reactTag: string;
18
+
19
+ _id: number;
20
+ _label: string;
21
+ _maxPacketLifeTime: ?number;
22
+ _maxRetransmits: ?number;
23
+ _negotiated: boolean;
24
+ _ordered: boolean;
25
+ _protocol: string;
26
+ _readyState: RTCDataChannelState;
27
+
28
+ binaryType: 'arraybuffer' = 'arraybuffer'; // we only support 'arraybuffer'
29
+ bufferedAmount: number = 0;
30
+ bufferedAmountLowThreshold: number = 0;
31
+
32
+ constructor(info) {
33
+ super();
34
+
35
+ this._peerConnectionId = info.peerConnectionId;
36
+ this._reactTag = info.reactTag;
37
+
38
+ this._label = info.label;
39
+ this._id = info.id === -1 ? null : info.id; // null until negotiated.
40
+ this._ordered = Boolean(info.ordered);
41
+ this._maxPacketLifeTime = info.maxPacketLifeTime;
42
+ this._maxRetransmits = info.maxRetransmits;
43
+ this._protocol = info.protocol || '';
44
+ this._negotiated = Boolean(info.negotiated);
45
+ this._readyState = info.readyState;
46
+
47
+ this._registerEvents();
104
48
  }
105
49
 
106
- // Safely convert the buffer object to an Uint8Array for base64-encoding
107
- if (ArrayBuffer.isView(data)) {
108
- data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
109
- } else if (data instanceof ArrayBuffer) {
110
- data = new Uint8Array(data);
111
- } else {
112
- throw new TypeError('Data must be either string, ArrayBuffer, or ArrayBufferView');
50
+ get label(): string {
51
+ return this._label;
113
52
  }
114
- WebRTCModule.dataChannelSend(this._peerConnectionId, this._reactTag, base64.fromByteArray(data), 'binary');
115
- }
116
53
 
117
- close() {
118
- if (this._readyState === 'closing' || this._readyState === 'closed') {
119
- return;
54
+ get id(): number {
55
+ return this._id;
120
56
  }
121
- WebRTCModule.dataChannelClose(this._peerConnectionId, this._reactTag);
122
- }
123
-
124
- _unregisterEvents() {
125
- this._subscriptions.forEach(e => e.remove());
126
- this._subscriptions = [];
127
- }
128
-
129
- _registerEvents() {
130
- this._subscriptions = [
131
- EventEmitter.addListener('dataChannelStateChanged', ev => {
132
- if (ev.reactTag !== this._reactTag) {
133
- return;
134
- }
135
- this._readyState = ev.state;
136
- if (this._id === null && ev.id !== -1) {
137
- this._id = ev.id;
138
- }
139
- if (this._readyState === 'open') {
140
- this.dispatchEvent(new RTCDataChannelEvent('open', {channel: this}));
141
- } else if (this._readyState === 'closing') {
142
- this.dispatchEvent(new RTCDataChannelEvent('closing', {channel: this}));
143
- } else if (this._readyState === 'closed') {
144
- this.dispatchEvent(new RTCDataChannelEvent('close', {channel: this}));
145
- this._unregisterEvents();
146
- WebRTCModule.dataChannelDispose(this._peerConnectionId, this._reactTag);
57
+
58
+ get ordered(): boolean {
59
+ return this._ordered;
60
+ }
61
+
62
+ get maxPacketLifeTime(): number {
63
+ return this._maxPacketLifeTime;
64
+ }
65
+
66
+ get maxRetransmits(): number {
67
+ return this._maxRetransmits;
68
+ }
69
+
70
+ get protocol(): string {
71
+ return this._protocol;
72
+ }
73
+
74
+ get negotiated(): boolean {
75
+ return this._negotiated;
76
+ }
77
+
78
+ get readyState(): string {
79
+ return this._readyState;
80
+ }
81
+
82
+ send(data: string | ArrayBuffer | ArrayBufferView) {
83
+ if (typeof data === 'string') {
84
+ WebRTCModule.dataChannelSend(this._peerConnectionId, this._reactTag, data, 'text');
85
+ return;
147
86
  }
148
- }),
149
- EventEmitter.addListener('dataChannelReceiveMessage', ev => {
150
- if (ev.reactTag !== this._reactTag) {
151
- return;
87
+
88
+ // Safely convert the buffer object to an Uint8Array for base64-encoding
89
+ if (ArrayBuffer.isView(data)) {
90
+ data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
91
+ } else if (data instanceof ArrayBuffer) {
92
+ data = new Uint8Array(data);
93
+ } else {
94
+ throw new TypeError('Data must be either string, ArrayBuffer, or ArrayBufferView');
152
95
  }
153
- let data = ev.data;
154
- if (ev.type === 'binary') {
155
- data = base64.toByteArray(ev.data).buffer;
96
+ WebRTCModule.dataChannelSend(this._peerConnectionId, this._reactTag, base64.fromByteArray(data), 'binary');
97
+ }
98
+
99
+ close() {
100
+ if (this._readyState === 'closing' || this._readyState === 'closed') {
101
+ return;
156
102
  }
157
- this.dispatchEvent(new MessageEvent('message', {data}));
158
- }),
159
- ];
160
- }
103
+ WebRTCModule.dataChannelClose(this._peerConnectionId, this._reactTag);
104
+ }
105
+
106
+ _unregisterEvents() {
107
+ this._subscriptions.forEach(e => e.remove());
108
+ this._subscriptions = [];
109
+ }
110
+
111
+ _registerEvents() {
112
+ this._subscriptions = [
113
+ EventEmitter.addListener('dataChannelStateChanged', ev => {
114
+ if (ev.reactTag !== this._reactTag) {
115
+ return;
116
+ }
117
+ this._readyState = ev.state;
118
+ if (this._id === null && ev.id !== -1) {
119
+ this._id = ev.id;
120
+ }
121
+ if (this._readyState === 'open') {
122
+ this.dispatchEvent(new RTCDataChannelEvent('open', { channel: this }));
123
+ } else if (this._readyState === 'closing') {
124
+ this.dispatchEvent(new RTCDataChannelEvent('closing', { channel: this }));
125
+ } else if (this._readyState === 'closed') {
126
+ this.dispatchEvent(new RTCDataChannelEvent('close', { channel: this }));
127
+ this._unregisterEvents();
128
+ WebRTCModule.dataChannelDispose(this._peerConnectionId, this._reactTag);
129
+ }
130
+ }),
131
+ EventEmitter.addListener('dataChannelReceiveMessage', ev => {
132
+ if (ev.reactTag !== this._reactTag) {
133
+ return;
134
+ }
135
+ let data = ev.data;
136
+ if (ev.type === 'binary') {
137
+ data = base64.toByteArray(ev.data).buffer;
138
+ }
139
+ this.dispatchEvent(new MessageEvent('message', { data }));
140
+ })
141
+ ];
142
+ }
161
143
  }
@@ -1,12 +1,11 @@
1
- 'use strict';
2
1
 
3
2
  import type RTCDataChannel from './RTCDataChannel';
4
3
 
5
4
  export default class RTCDataChannelEvent {
6
- type: string;
7
- channel: RTCDataChannel;
8
- constructor(type, eventInitDict) {
9
- this.type = type.toString();
10
- Object.assign(this, eventInitDict);
11
- }
5
+ type: string;
6
+ channel: RTCDataChannel;
7
+ constructor(type, eventInitDict) {
8
+ this.type = type.toString();
9
+ Object.assign(this, eventInitDict);
10
+ }
12
11
  }