forked from react-native-webrtc/react-native-webrtc
/
RTCPeerConnection.js
140 lines (133 loc) · 5.02 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
'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 RTCPeerConnectionBase = require('./RTCPeerConnectionBase');
var PeerConnectionId = 0;
class RTCPeerConnection extends RTCPeerConnectionBase {
_peerConnectionId: number;
_subs: any;
constructorImpl(configuration) {
this._peerConnectionId = PeerConnectionId++;
WebRTCModule.peerConnectionInit(configuration, this._peerConnectionId);
this._registerEvents(this._peerConnectionId);
}
addStreamImpl(stream) {
WebRTCModule.peerConnectionAddStream(stream._streamId, this._peerConnectionId);
}
removeStreamImpl(stream) {
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();
}
});
}
closeImpl() {
WebRTCModule.peerConnectionClose(this._peerConnectionId);
}
_registerEvents(id: number): void {
this._subs = [
DeviceEventEmitter.addListener('peerConnectionOnRenegotiationNeeded', ev => {
if (ev.id !== id) {
return;
}
this.onnegotiationneeded && this.onnegotiationneeded();
}),
DeviceEventEmitter.addListener('peerConnectionIceConnectionChanged', ev => {
if (ev.id !== id) {
return;
}
this.iceConnectionState = ev.iceConnectionState;
var event = new RTCEvent('iceconnectionstatechange', {target: this});
this.oniceconnectionstatechange && this.oniceconnectionstatechange(event);
}),
DeviceEventEmitter.addListener('peerConnectionSignalingStateChanged', ev => {
if (ev.id !== id) {
return;
}
this.signalingState = ev.signalingState;
var event = new RTCEvent('signalingstatechange', {target: this});
this.onsignalingstatechange && this.onsignalingstatechange(event);
}),
DeviceEventEmitter.addListener('peerConnectionAddedStream', ev => {
if (ev.id !== id) {
return;
}
var stream = new RTCMediaStream(ev.streamId);
var event = new MediaStreamEvent('addstream', {target: this, stream: stream});
this.onaddstream && this.onaddstream(event);
}),
DeviceEventEmitter.addListener('peerConnectionGotICECandidate', ev => {
if (ev.id !== id) {
return;
}
var candidate = new RTCIceCandidate(ev.candidate);
var event = new RTCIceCandidateEvent('icecandidate', {target: this, candidate: candidate});
this.onicecandidate && this.onicecandidate(event);
}),
DeviceEventEmitter.addListener('peerConnectionIceGatheringChanged', ev => {
if (ev.id !== id) {
return;
}
this.iceGatheringState = ev.iceGatheringState;
var event = new RTCEvent('icegatheringstatechange', {target: this});
this.onicegatheringstatechange && this.onicegatheringstatechange(event);
})
];
}
}
module.exports = RTCPeerConnection;