forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTCPeerConnection.js
179 lines (172 loc) · 6.06 KB
/
RTCPeerConnection.js
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
'use strict';
var React = require('react-native');
var {
DeviceEventEmitter,
NativeModules,
} = React;
var WebRTCModule = NativeModules.WebRTCModule;
var RTCSessionDescription = require('./RTCSessionDescription');
var RTCIceCandidate = require('./RTCIceCandidate');
var RTCMediaStream = require('./RTCMediaStream');
var RTCIceCandidateEvent = require('./RTCIceCandidateEvent');
var MediaStreamEvent = require('./MediaStreamEvent');
var RTCEvent = require('./RTCEvent');
var MediaStreamTrack = require('./MediaStreamTrack');
var RTCPeerConnectionBase = require('./RTCPeerConnectionBase');
var PeerConnectionId = 0;
class RTCPeerConnection extends RTCPeerConnectionBase {
_peerConnectionId: number;
_subs: any;
_localStreams: array;
_remoteStreams: array;
constructorImpl(configuration) {
this._peerConnectionId = PeerConnectionId++;
this._localStreams = [];
this._remoteStreams = [];
WebRTCModule.peerConnectionInit(configuration, this._peerConnectionId);
this._registerEvents(this._peerConnectionId);
}
addStreamImpl(stream) {
WebRTCModule.peerConnectionAddStream(stream._streamId, this._peerConnectionId);
this._localStreams.push(stream);
}
removeStreamImpl(stream) {
var index = this._localStreams.indexOf(stream);
if (index > -1) {
this._localStreams.splice(index, 1);
}
WebRTCModule.peerConnectionRemoveStream(stream._streamId, this._peerConnectionId);
}
createOfferImpl(success: ?Function, failure: ?Function, constraints) {
WebRTCModule.peerConnectionCreateOffer(this._peerConnectionId, (successful, data) => {
if (successful) {
var sessionDescription = new RTCSessionDescription(data);
success(sessionDescription);
} else {
failure(data); // TODO: convert to NavigatorUserMediaError
}
});
}
createAnswerImpl(success: ?Function, failure: ?Function, constraints) {
WebRTCModule.peerConnectionCreateAnswer(this._peerConnectionId, (successful, data) => {
if (successful) {
var sessionDescription = new RTCSessionDescription(data);
success(sessionDescription);
} else {
failure(data);
}
});
}
setLocalDescriptionImpl(sessionDescription: RTCSessionDescription, success: ?Function, failure: ?Function, constraints) {
WebRTCModule.peerConnectionSetLocalDescription(sessionDescription.toJSON(), this._peerConnectionId, (successful, data) => {
if (successful) {
this.localDescription = sessionDescription;
success();
} else {
failure(data);
}
});
}
setRemoteDescriptionImpl(sessionDescription: RTCSessionDescription, success: ?Function, failure: ?Function) {
WebRTCModule.peerConnectionSetRemoteDescription(sessionDescription.toJSON(), this._peerConnectionId, (successful, data) => {
if (successful) {
this.remoteDescription = sessionDescription;
success();
} else {
failure(data);
}
});
}
addIceCandidateImpl(candidate, success, failure) { // TODO: success, failure
WebRTCModule.peerConnectionAddICECandidate(candidate.toJSON(), this._peerConnectionId, (successful) => {
if (successful) {
success && success();
} else {
failure && failure();
}
});
}
getLocalStreamsImpl() {
return this._localStreams;
}
getRemoteStreamsImpl() {
return this._remoteStreams;
}
getStatsImpl(track, success, failure) {
if (WebRTCModule.peerConnectionGetStats) {
WebRTCModule.peerConnectionGetStats(track ? track.id : -1, this._peerConnectionId, stats => {
success && success(stats);
});
} else {
console.warn('RTCPeerConnection getStats doesn\'t support');
}
}
closeImpl() {
WebRTCModule.peerConnectionClose(this._peerConnectionId);
}
_registerEvents(id: number): void {
this._subs = [
DeviceEventEmitter.addListener('peerConnectionOnRenegotiationNeeded', ev => {
if (ev.id !== id) {
return;
}
this.dispatchEvent(new RTCEvent('negotiationneeded'));
}),
DeviceEventEmitter.addListener('peerConnectionIceConnectionChanged', ev => {
if (ev.id !== id) {
return;
}
this.iceConnectionState = ev.iceConnectionState;
this.dispatchEvent(new RTCEvent('iceconnectionstatechange'));
}),
DeviceEventEmitter.addListener('peerConnectionSignalingStateChanged', ev => {
if (ev.id !== id) {
return;
}
this.signalingState = ev.signalingState;
this.dispatchEvent(new RTCEvent('signalingstatechange'));
}),
DeviceEventEmitter.addListener('peerConnectionAddedStream', ev => {
if (ev.id !== id) {
return;
}
var stream = new RTCMediaStream(ev.streamId);
var tracks = ev.tracks;
for (var i = 0; i < tracks.length; i++) {
stream.addTrack(new MediaStreamTrack(tracks[i]));
}
this._remoteStreams.push(stream);
this.dispatchEvent(new MediaStreamEvent('addstream', {stream}));
}),
DeviceEventEmitter.addListener('peerConnectionRemovedStream', ev => {
if (ev.id !== id) {
return;
}
var stream = this._remoteStreams.find(s => s._streamId === ev.streamId);
if (stream) {
var index = this._remoteStreams.indexOf(stream);
if (index > -1) {
this._remoteStreams.splice(index, 1);
}
}
this.dispatchEvent(new MediaStreamEvent('removestream', {stream}));
}),
DeviceEventEmitter.addListener('peerConnectionGotICECandidate', ev => {
if (ev.id !== id) {
return;
}
var candidate = new RTCIceCandidate(ev.candidate);
var event = new RTCIceCandidateEvent('icecandidate', {candidate});
this.dispatchEvent(event);
}),
DeviceEventEmitter.addListener('peerConnectionIceGatheringChanged', ev => {
if (ev.id !== id) {
return;
}
this.iceGatheringState = ev.iceGatheringState;
this.dispatchEvent(new RTCEvent('icegatheringstatechange'));
})
];
}
}
module.exports = RTCPeerConnection;