forked from react-native-webrtc/react-native-webrtc
/
RTCPeerConnection.js
125 lines (119 loc) · 4.6 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
'use strict';
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var WebRTCModule = require('react-native').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) {
console.log('streameew', stream);
WebRTCModule.peerConnectionAddStream(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 = [
RCTDeviceEventEmitter.addListener('peerConnectionOnRenegotiationNeeded', ev => {
if (ev.id !== id) {
return;
}
this.onnegotiationneeded && this.onnegotiationneeded();
}),
RCTDeviceEventEmitter.addListener('peerConnectionIceConnectionChanged', ev => {
if (ev.id !== id) {
return;
}
this.iceConnectionState = ev.iceConnectionState;
var event = new RTCEvent('iceconnectionstatechange', {target: this});
this.oniceconnectionstatechange && this.oniceconnectionstatechange(event);
}),
RCTDeviceEventEmitter.addListener('peerConnectionSignalingStateChanged', ev => {
if (ev.id !== id) {
return;
}
this.signalingState = ev.signalingState;
var event = new RTCEvent('signalingstatechange', {target: this});
this.onsignalingstatechange && this.onsignalingstatechange(event);
}),
RCTDeviceEventEmitter.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);
}),
RCTDeviceEventEmitter.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);
})
];
}
}
module.exports = RTCPeerConnection;