react-native-webrtc 1.92.0 → 1.94.1
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.
- package/.prettierignore +9 -0
- package/.prettierrc.json +10 -0
- package/README.md +2 -1
- package/android/libs/.placeholder +0 -0
- package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +0 -5
- package/android/src/main/java/com/oney/WebRTCModule/LibraryLoader.java +21 -0
- package/android/src/main/java/com/oney/WebRTCModule/StringUtils.java +24 -0
- package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +222 -275
- package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +9 -84
- package/apple/.placeholder +0 -0
- package/ios/RCTWebRTC/VideoCaptureController.h +2 -2
- package/ios/RCTWebRTC/VideoCaptureController.m +131 -48
- package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +39 -20
- package/metro.config.js +3 -4
- package/metro.config.macos.js +7 -8
- package/package.json +21 -12
- package/react-native.config.js +5 -6
- package/{EventEmitter.js → src/EventEmitter.js} +1 -1
- package/{MediaDevices.js → src/MediaDevices.js} +6 -12
- package/src/MediaStream.js +128 -0
- package/src/MediaStreamError.js +12 -0
- package/src/MediaStreamErrorEvent.js +11 -0
- package/src/MediaStreamEvent.js +11 -0
- package/src/MediaStreamTrack.js +104 -0
- package/src/MediaStreamTrackEvent.js +11 -0
- package/src/MessageEvent.js +10 -0
- package/{Permissions.js → src/Permissions.js} +26 -23
- package/src/RTCDataChannel.js +143 -0
- package/src/RTCDataChannelEvent.js +11 -0
- package/src/RTCEvent.js +8 -0
- package/src/RTCIceCandidate.js +20 -0
- package/src/RTCIceCandidateEvent.js +15 -0
- package/src/RTCPeerConnection.js +349 -0
- package/src/RTCSessionDescription.js +22 -0
- package/{RTCUtil.js → src/RTCUtil.js} +56 -57
- package/src/RTCView.js +60 -0
- package/src/ScreenCapturePickerView.js +4 -0
- package/{getDisplayMedia.js → src/getDisplayMedia.js} +8 -11
- package/src/getUserMedia.js +98 -0
- package/src/index.js +44 -0
- package/tools/build-webrtc.py +61 -49
- package/tools/downloadBitcode.sh +4 -16
- package/tools/downloadWebRTC.js +100 -0
- package/MediaStream.js +0 -138
- package/MediaStreamError.js +0 -14
- package/MediaStreamErrorEvent.js +0 -12
- package/MediaStreamEvent.js +0 -12
- package/MediaStreamTrack.js +0 -118
- package/MediaStreamTrackEvent.js +0 -12
- package/MessageEvent.js +0 -11
- package/RTCDataChannel.js +0 -161
- package/RTCDataChannelEvent.js +0 -12
- package/RTCEvent.js +0 -9
- package/RTCIceCandidate.js +0 -21
- package/RTCIceCandidateEvent.js +0 -16
- package/RTCPeerConnection.js +0 -379
- package/RTCSessionDescription.js +0 -23
- package/RTCView.js +0 -70
- package/ScreenCapturePickerView.js +0 -7
- package/android/libs/libjingle_peerconnection.so.jar +0 -0
- package/android/libs/libwebrtc.jar +0 -0
- package/apple/WebRTC.xcframework.tgz +0 -0
- package/getUserMedia.js +0 -98
- package/index.js +0 -47
- package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/ios/RCTWebRTC.xcodeproj/project.xcworkspace/xcuserdata/saghul.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/RCTWebRTC.xcodeproj/xcuserdata/saghul.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
|
|
2
|
+
import { NativeModules } from 'react-native';
|
|
3
|
+
import { defineCustomEventTarget } from 'event-target-shim';
|
|
4
|
+
import uuid from 'uuid';
|
|
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 = uuid.v4();
|
|
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,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,104 @@
|
|
|
1
|
+
|
|
2
|
+
import { NativeModules } from 'react-native';
|
|
3
|
+
import { defineCustomEventTarget } from 'event-target-shim';
|
|
4
|
+
|
|
5
|
+
import { deepClone } from './RTCUtil';
|
|
6
|
+
|
|
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';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get enabled(): boolean {
|
|
44
|
+
return this._enabled;
|
|
45
|
+
}
|
|
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;
|
|
54
|
+
}
|
|
55
|
+
|
|
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
|
+
}
|
|
82
|
+
|
|
83
|
+
clone() {
|
|
84
|
+
throw new Error('Not implemented.');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
getCapabilities() {
|
|
88
|
+
throw new Error('Not implemented.');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getConstraints() {
|
|
92
|
+
return deepClone(this._constraints);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
getSettings() {
|
|
96
|
+
return deepClone(this._settings);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
release() {
|
|
100
|
+
WebRTCModule.mediaStreamTrackRelease(this.id);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export default MediaStreamTrack;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
import type MediaStreamTrack from './MediaStreamTrack';
|
|
3
|
+
|
|
4
|
+
export default class MediaStreamTrackEvent {
|
|
5
|
+
type: string;
|
|
6
|
+
track: MediaStreamTrack;
|
|
7
|
+
constructor(type, eventInitDict) {
|
|
8
|
+
this.type = type.toString();
|
|
9
|
+
Object.assign(this, eventInitDict);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -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 = [
|
|
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 !==
|
|
60
|
-
throw new TypeError(
|
|
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 ===
|
|
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(
|
|
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 =
|
|
82
|
-
|
|
83
|
-
|
|
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(
|
|
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 =
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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(
|
|
121
|
+
return Promise.reject(new TypeError('Unsupported platform.'));
|
|
119
122
|
}
|
|
120
123
|
}
|
|
121
124
|
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
|
|
2
|
+
import { NativeModules } from 'react-native';
|
|
3
|
+
import base64 from 'base64-js';
|
|
4
|
+
import { defineCustomEventTarget } from 'event-target-shim';
|
|
5
|
+
import MessageEvent from './MessageEvent';
|
|
6
|
+
import RTCDataChannelEvent from './RTCDataChannelEvent';
|
|
7
|
+
import EventEmitter from './EventEmitter';
|
|
8
|
+
|
|
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();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get label(): string {
|
|
51
|
+
return this._label;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get id(): number {
|
|
55
|
+
return this._id;
|
|
56
|
+
}
|
|
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;
|
|
86
|
+
}
|
|
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');
|
|
95
|
+
}
|
|
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;
|
|
102
|
+
}
|
|
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
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
import type RTCDataChannel from './RTCDataChannel';
|
|
3
|
+
|
|
4
|
+
export default class RTCDataChannelEvent {
|
|
5
|
+
type: string;
|
|
6
|
+
channel: RTCDataChannel;
|
|
7
|
+
constructor(type, eventInitDict) {
|
|
8
|
+
this.type = type.toString();
|
|
9
|
+
Object.assign(this, eventInitDict);
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/RTCEvent.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
export default class RTCIceCandidate {
|
|
3
|
+
candidate: string;
|
|
4
|
+
sdpMLineIndex: number;
|
|
5
|
+
sdpMid: string;
|
|
6
|
+
|
|
7
|
+
constructor(info) {
|
|
8
|
+
this.candidate = info.candidate;
|
|
9
|
+
this.sdpMLineIndex = info.sdpMLineIndex;
|
|
10
|
+
this.sdpMid = info.sdpMid;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
toJSON() {
|
|
14
|
+
return {
|
|
15
|
+
candidate: this.candidate,
|
|
16
|
+
sdpMLineIndex: this.sdpMLineIndex,
|
|
17
|
+
sdpMid: this.sdpMid
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
import type RTCIceCandidate from './RTCIceCandidate';
|
|
3
|
+
|
|
4
|
+
export default class RTCIceCandidateEvent {
|
|
5
|
+
type: string;
|
|
6
|
+
candidate: RTCIceCandidate;
|
|
7
|
+
constructor(type, eventInitDict) {
|
|
8
|
+
this.type = type.toString();
|
|
9
|
+
|
|
10
|
+
this.candidate = null;
|
|
11
|
+
if (eventInitDict && eventInitDict.candidate) {
|
|
12
|
+
this.candidate = eventInitDict.candidate;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|