forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
RTCRtpTransceiver.js
100 lines (85 loc) · 2.59 KB
/
RTCRtpTransceiver.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
import {NativeModules} from 'react-native';
import RTCRtpSender from './RTCRtpSender';
import RTCRtpReceiver from './RTCRtpReceiver';
import MediaStreamTrack from './MediaStreamTrack';
const {WebRTCModule} = NativeModules;
export default class RTCRtpTransceiver {
_peerConnectionId: number;
_sender: RTCRtpSender;
_receiver: RTCRtpReceiver
_id: string;
_mid: string | null;
_direction: string;
_currentDirection: string;
_stopped: boolean;
_mergeState: Function;
constructor(pcId, state, mergeState) {
this._peerConnectionId = pcId;
this._id = state.id;
this._mid = state.mid ? state.mid : null;
this._direction = state.direction;
this._currentDirection = state.currentDirection;
this._stopped = state.isStopped;
this._mergeState = mergeState;
this._sender = new RTCRtpSender(this, mergeState);
this._receiver = new RTCRtpReceiver(state.receiver.id, new MediaStreamTrack(state.receiver.track));
}
get id() {
return this._id;
}
get mid() {
return this._mid;
}
get isStopped() {
return this._stopped;
}
get direction() {
return this._direction;
}
set direction(val) {
if (this._stopped) {
throw Error('Transceiver Stopped');
}
this._direction = val;
WebRTCModule.peerConnectionTransceiverSetDirection(this._peerConnectionId, this.id, val, (successful, data) => {
if (successful) {
this._mergeState(data.state);
} else {
console.warn("Unable to set direction: " + data);
}
});
}
get currentDirection() {
return this._currentDirection;
}
get sender() {
return this._sender;
}
get receiver() {
return this._receiver;
}
stop() {
if (this._stopped) {
return;
}
this._stopped = true;
return new Promise((resolve, reject) => {
WebRTCModule.peerConnectionTransceiverStop(this._peerConnectionId, this.id, (successful, data) => {
if (successful) {
this._mergeState(data.state);
resolve();
} else {
reject(new Error(data));
}
});
});
}
_updateState(state) {
this._mid = state.mid ? state.mid : null;
this._direction = state.direction;
this._currentDirection = state.currentDirection;
if (state.isStopped) {
this._stopped = true;
}
}
}