react-native-webrtc 1.100.0 → 106.0.0-beta.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.
Files changed (167) hide show
  1. package/ISSUE_TEMPLATE.md +17 -13
  2. package/README.md +46 -129
  3. package/android/src/main/java/com/oney/WebRTCModule/PeerConnectionObserver.java +160 -165
  4. package/android/src/main/java/com/oney/WebRTCModule/SerializeUtils.java +279 -0
  5. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +361 -56
  6. package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +5 -1
  7. package/ios/RCTWebRTC/RCTConvert+WebRTC.m +2 -6
  8. package/ios/RCTWebRTC/ScreenCaptureController.m +4 -0
  9. package/ios/RCTWebRTC/ScreenCapturer.m +7 -1
  10. package/ios/RCTWebRTC/SerializeUtils.h +25 -0
  11. package/ios/RCTWebRTC/SerializeUtils.m +219 -0
  12. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +286 -128
  13. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +216 -0
  14. package/ios/RCTWebRTC/WebRTCModule.h +15 -4
  15. package/ios/RCTWebRTC/WebRTCModule.m +26 -6
  16. package/lib/commonjs/EventEmitter.js +24 -0
  17. package/lib/commonjs/EventEmitter.js.map +1 -1
  18. package/lib/commonjs/Logger.js +65 -0
  19. package/lib/commonjs/Logger.js.map +1 -0
  20. package/lib/commonjs/MediaStream.js +1 -1
  21. package/lib/commonjs/MediaStream.js.map +1 -1
  22. package/lib/commonjs/MediaStreamTrack.js +4 -2
  23. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  24. package/lib/commonjs/Permissions.js +1 -1
  25. package/lib/commonjs/Permissions.js.map +1 -1
  26. package/lib/commonjs/RTCDataChannel.js.map +1 -1
  27. package/lib/commonjs/RTCErrorEvent.js +33 -0
  28. package/lib/commonjs/RTCErrorEvent.js.map +1 -0
  29. package/lib/commonjs/RTCPeerConnection.js +354 -103
  30. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  31. package/lib/commonjs/RTCRtcpParameters.js +24 -0
  32. package/lib/commonjs/RTCRtcpParameters.js.map +1 -0
  33. package/lib/commonjs/RTCRtpCapabilities.js +79 -0
  34. package/lib/commonjs/RTCRtpCapabilities.js.map +1 -0
  35. package/lib/commonjs/RTCRtpCodecCapability.js +25 -0
  36. package/lib/commonjs/RTCRtpCodecCapability.js.map +1 -0
  37. package/lib/commonjs/RTCRtpCodecParameters.js +33 -0
  38. package/lib/commonjs/RTCRtpCodecParameters.js.map +1 -0
  39. package/lib/commonjs/RTCRtpEncodingParameters.js +81 -0
  40. package/lib/commonjs/RTCRtpEncodingParameters.js.map +1 -0
  41. package/lib/commonjs/RTCRtpHeaderExtension.js +27 -0
  42. package/lib/commonjs/RTCRtpHeaderExtension.js.map +1 -0
  43. package/lib/commonjs/RTCRtpParameters.js +26 -0
  44. package/lib/commonjs/RTCRtpParameters.js.map +1 -0
  45. package/lib/commonjs/RTCRtpReceiveParameters.js +20 -0
  46. package/lib/commonjs/RTCRtpReceiveParameters.js.map +1 -0
  47. package/lib/commonjs/RTCRtpReceiver.js +51 -0
  48. package/lib/commonjs/RTCRtpReceiver.js.map +1 -0
  49. package/lib/commonjs/RTCRtpSendParameters.js +60 -0
  50. package/lib/commonjs/RTCRtpSendParameters.js.map +1 -0
  51. package/lib/commonjs/RTCRtpSender.js +85 -0
  52. package/lib/commonjs/RTCRtpSender.js.map +1 -0
  53. package/lib/commonjs/RTCRtpTransceiver.js +141 -0
  54. package/lib/commonjs/RTCRtpTransceiver.js.map +1 -0
  55. package/lib/commonjs/RTCSessionDescription.js.map +1 -1
  56. package/lib/commonjs/RTCTrackEvent.js +32 -0
  57. package/lib/commonjs/RTCTrackEvent.js.map +1 -0
  58. package/lib/commonjs/RTCUtil.js +20 -37
  59. package/lib/commonjs/RTCUtil.js.map +1 -1
  60. package/lib/commonjs/getDisplayMedia.js.map +1 -1
  61. package/lib/commonjs/getUserMedia.js.map +1 -1
  62. package/lib/commonjs/index.js +127 -3
  63. package/lib/commonjs/index.js.map +1 -1
  64. package/lib/module/EventEmitter.js +21 -0
  65. package/lib/module/EventEmitter.js.map +1 -1
  66. package/lib/module/Logger.js +53 -0
  67. package/lib/module/Logger.js.map +1 -0
  68. package/lib/module/MediaStream.js +1 -1
  69. package/lib/module/MediaStream.js.map +1 -1
  70. package/lib/module/MediaStreamTrack.js +4 -2
  71. package/lib/module/MediaStreamTrack.js.map +1 -1
  72. package/lib/module/Permissions.js +1 -1
  73. package/lib/module/Permissions.js.map +1 -1
  74. package/lib/module/RTCDataChannel.js.map +1 -1
  75. package/lib/module/RTCErrorEvent.js +22 -0
  76. package/lib/module/RTCErrorEvent.js.map +1 -0
  77. package/lib/module/RTCPeerConnection.js +348 -103
  78. package/lib/module/RTCPeerConnection.js.map +1 -1
  79. package/lib/module/RTCRtcpParameters.js +15 -0
  80. package/lib/module/RTCRtcpParameters.js.map +1 -0
  81. package/lib/module/RTCRtpCapabilities.js +63 -0
  82. package/lib/module/RTCRtpCapabilities.js.map +1 -0
  83. package/lib/module/RTCRtpCodecCapability.js +16 -0
  84. package/lib/module/RTCRtpCodecCapability.js.map +1 -0
  85. package/lib/module/RTCRtpCodecParameters.js +24 -0
  86. package/lib/module/RTCRtpCodecParameters.js.map +1 -0
  87. package/lib/module/RTCRtpEncodingParameters.js +72 -0
  88. package/lib/module/RTCRtpEncodingParameters.js.map +1 -0
  89. package/lib/module/RTCRtpHeaderExtension.js +18 -0
  90. package/lib/module/RTCRtpHeaderExtension.js.map +1 -0
  91. package/lib/module/RTCRtpParameters.js +17 -0
  92. package/lib/module/RTCRtpParameters.js.map +1 -0
  93. package/lib/module/RTCRtpReceiveParameters.js +8 -0
  94. package/lib/module/RTCRtpReceiveParameters.js.map +1 -0
  95. package/lib/module/RTCRtpReceiver.js +39 -0
  96. package/lib/module/RTCRtpReceiver.js.map +1 -0
  97. package/lib/module/RTCRtpSendParameters.js +49 -0
  98. package/lib/module/RTCRtpSendParameters.js.map +1 -0
  99. package/lib/module/RTCRtpSender.js +70 -0
  100. package/lib/module/RTCRtpSender.js.map +1 -0
  101. package/lib/module/RTCRtpTransceiver.js +125 -0
  102. package/lib/module/RTCRtpTransceiver.js.map +1 -0
  103. package/lib/module/RTCSessionDescription.js.map +1 -1
  104. package/lib/module/RTCTrackEvent.js +23 -0
  105. package/lib/module/RTCTrackEvent.js.map +1 -0
  106. package/lib/module/RTCUtil.js +18 -36
  107. package/lib/module/RTCUtil.js.map +1 -1
  108. package/lib/module/getDisplayMedia.js.map +1 -1
  109. package/lib/module/getUserMedia.js.map +1 -1
  110. package/lib/module/index.js +35 -4
  111. package/lib/module/index.js.map +1 -1
  112. package/lib/typescript/EventEmitter.d.ts +3 -0
  113. package/lib/typescript/Logger.d.ts +13 -0
  114. package/lib/typescript/MediaDevices.d.ts +2 -2
  115. package/lib/typescript/MediaStream.d.ts +6 -6
  116. package/lib/typescript/MediaStreamTrack.d.ts +10 -9
  117. package/lib/typescript/RTCDataChannel.d.ts +4 -2
  118. package/lib/typescript/RTCErrorEvent.d.ts +12 -0
  119. package/lib/typescript/RTCPeerConnection.d.ts +42 -11
  120. package/lib/typescript/RTCRtcpParameters.d.ts +8 -0
  121. package/lib/typescript/RTCRtpCapabilities.d.ts +19 -0
  122. package/lib/typescript/RTCRtpCodecCapability.d.ts +7 -0
  123. package/lib/typescript/RTCRtpCodecParameters.d.ts +14 -0
  124. package/lib/typescript/RTCRtpEncodingParameters.d.ts +21 -0
  125. package/lib/typescript/RTCRtpHeaderExtension.d.ts +10 -0
  126. package/lib/typescript/RTCRtpParameters.d.ts +14 -0
  127. package/lib/typescript/RTCRtpReceiveParameters.d.ts +4 -0
  128. package/lib/typescript/RTCRtpReceiver.d.ts +15 -0
  129. package/lib/typescript/RTCRtpSendParameters.d.ts +17 -0
  130. package/lib/typescript/RTCRtpSender.d.ts +21 -0
  131. package/lib/typescript/RTCRtpTransceiver.d.ts +35 -0
  132. package/lib/typescript/RTCTrackEvent.d.ts +15 -0
  133. package/lib/typescript/RTCUtil.d.ts +5 -9
  134. package/lib/typescript/getDisplayMedia.d.ts +2 -1
  135. package/lib/typescript/getUserMedia.d.ts +2 -1
  136. package/lib/typescript/index.d.ts +16 -3
  137. package/package.json +6 -4
  138. package/react-native-webrtc.podspec +1 -1
  139. package/src/EventEmitter.ts +21 -1
  140. package/src/Logger.ts +49 -0
  141. package/src/MediaStream.ts +11 -11
  142. package/src/MediaStreamTrack.ts +14 -13
  143. package/src/Permissions.ts +1 -1
  144. package/src/RTCDataChannel.ts +11 -8
  145. package/src/RTCErrorEvent.ts +21 -0
  146. package/src/RTCPeerConnection.ts +400 -158
  147. package/src/RTCRtcpParameters.ts +14 -0
  148. package/src/RTCRtpCapabilities.ts +54 -0
  149. package/src/RTCRtpCodecCapability.ts +13 -0
  150. package/src/RTCRtpCodecParameters.ts +23 -0
  151. package/src/RTCRtpEncodingParameters.ts +67 -0
  152. package/src/RTCRtpHeaderExtension.ts +18 -0
  153. package/src/RTCRtpParameters.ts +21 -0
  154. package/src/RTCRtpReceiveParameters.ts +7 -0
  155. package/src/RTCRtpReceiver.ts +35 -0
  156. package/src/RTCRtpSendParameters.ts +51 -0
  157. package/src/RTCRtpSender.ts +72 -0
  158. package/src/RTCRtpTransceiver.ts +125 -0
  159. package/src/RTCSessionDescription.ts +3 -3
  160. package/src/RTCTrackEvent.ts +21 -0
  161. package/src/RTCUtil.ts +20 -40
  162. package/src/getDisplayMedia.ts +1 -1
  163. package/src/getUserMedia.ts +2 -2
  164. package/src/index.ts +51 -6
  165. package/tools/build-webrtc.py +32 -33
  166. package/tools/downloadBitcode.sh +2 -12
  167. package/tools/downloadWebRTC.js +21 -12
@@ -2,21 +2,28 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
2
2
 
3
3
  import { defineCustomEventTarget } from 'event-target-shim';
4
4
  import { NativeModules } from 'react-native';
5
+ import Logger from './Logger';
5
6
  import MediaStream from './MediaStream';
6
- import MediaStreamEvent from './MediaStreamEvent';
7
- import MediaStreamTrackEvent from './MediaStreamTrackEvent';
7
+ import MediaStreamTrack from './MediaStreamTrack';
8
+ import RTCRtpTransceiver from './RTCRtpTransceiver';
8
9
  import RTCDataChannel from './RTCDataChannel';
9
10
  import RTCDataChannelEvent from './RTCDataChannelEvent';
11
+ import RTCTrackEvent from './RTCTrackEvent';
10
12
  import RTCSessionDescription from './RTCSessionDescription';
11
13
  import RTCIceCandidate from './RTCIceCandidate';
12
14
  import RTCIceCandidateEvent from './RTCIceCandidateEvent';
15
+ import RTCErrorEvent from './RTCErrorEvent';
13
16
  import RTCEvent from './RTCEvent';
14
17
  import * as RTCUtil from './RTCUtil';
15
- import EventEmitter from './EventEmitter';
18
+ import { addListener, removeListener } from './EventEmitter';
19
+ import RTCRtpReceiver from './RTCRtpReceiver';
20
+ import RTCRtpSender from './RTCRtpSender';
21
+ import RTCRtpSendParameters from './RTCRtpSendParameters';
22
+ const log = new Logger('pc');
16
23
  const {
17
24
  WebRTCModule
18
25
  } = NativeModules;
19
- const PEER_CONNECTION_EVENTS = ['connectionstatechange', 'icecandidate', 'icecandidateerror', 'iceconnectionstatechange', 'icegatheringstatechange', 'negotiationneeded', 'signalingstatechange', 'datachannel', 'addstream', 'removestream'];
26
+ const PEER_CONNECTION_EVENTS = ['connectionstatechange', 'icecandidate', 'icecandidateerror', 'iceconnectionstatechange', 'icegatheringstatechange', 'negotiationneeded', 'signalingstatechange', 'datachannel', 'track', 'error'];
20
27
  let nextPeerConnectionId = 0;
21
28
  export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_CONNECTION_EVENTS) {
22
29
  constructor(configuration) {
@@ -36,61 +43,50 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
36
43
 
37
44
  _defineProperty(this, "_peerConnectionId", void 0);
38
45
 
39
- _defineProperty(this, "_localStreams", []);
46
+ _defineProperty(this, "_subscriptions", []);
40
47
 
41
- _defineProperty(this, "_remoteStreams", []);
48
+ _defineProperty(this, "_transceivers", []);
42
49
 
43
- _defineProperty(this, "_subscriptions", []);
50
+ _defineProperty(this, "_remoteStreams", new Map());
44
51
 
45
52
  this._peerConnectionId = nextPeerConnectionId++;
46
53
  WebRTCModule.peerConnectionInit(configuration, this._peerConnectionId);
47
54
 
48
55
  this._registerEvents();
49
- }
50
56
 
51
- addStream(stream) {
52
- const index = this._localStreams.indexOf(stream);
53
-
54
- if (index !== -1) {
55
- return;
56
- }
57
-
58
- WebRTCModule.peerConnectionAddStream(stream._reactTag, this._peerConnectionId);
59
-
60
- this._localStreams.push(stream);
61
- }
62
-
63
- removeStream(stream) {
64
- const index = this._localStreams.indexOf(stream);
65
-
66
- if (index === -1) {
67
- return;
68
- }
69
-
70
- this._localStreams.splice(index, 1);
71
-
72
- WebRTCModule.peerConnectionRemoveStream(stream._reactTag, this._peerConnectionId);
57
+ log.debug(`${this._peerConnectionId} ctor`);
73
58
  }
74
59
 
75
60
  createOffer(options) {
61
+ log.debug(`${this._peerConnectionId} createOffer`);
76
62
  return new Promise((resolve, reject) => {
77
- WebRTCModule.peerConnectionCreateOffer(this._peerConnectionId, RTCUtil.normalizeOfferAnswerOptions(options), (successful, data) => {
63
+ WebRTCModule.peerConnectionCreateOffer(this._peerConnectionId, RTCUtil.normalizeOfferOptions(options), (successful, data) => {
78
64
  if (successful) {
79
- resolve(data);
65
+ log.debug(`${this._peerConnectionId} createOffer OK`);
66
+
67
+ this._updateTransceivers(data.transceiversInfo);
68
+
69
+ resolve(data.sdpInfo);
80
70
  } else {
81
- reject(data); // TODO: convert to NavigatorUserMediaError
71
+ log.debug(`${this._peerConnectionId} createOffer ERROR`);
72
+ reject(data);
82
73
  }
83
74
  });
84
75
  });
85
76
  }
86
77
 
87
78
  createAnswer() {
88
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
79
+ log.debug(`${this._peerConnectionId} createAnswer`);
89
80
  return new Promise((resolve, reject) => {
90
- WebRTCModule.peerConnectionCreateAnswer(this._peerConnectionId, RTCUtil.normalizeOfferAnswerOptions(options), (successful, data) => {
81
+ WebRTCModule.peerConnectionCreateAnswer(this._peerConnectionId, {}, (successful, data) => {
91
82
  if (successful) {
92
- resolve(data);
83
+ log.debug(`${this._peerConnectionId} createAnswer OK`);
84
+
85
+ this._updateTransceivers(data.transceiversInfo);
86
+
87
+ resolve(data.sdpInfo);
93
88
  } else {
89
+ log.debug(`${this._peerConnectionId} createAnswer ERROR`);
94
90
  reject(data);
95
91
  }
96
92
  });
@@ -102,16 +98,29 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
102
98
  }
103
99
 
104
100
  async setLocalDescription(sessionDescription) {
101
+ log.debug(`${this._peerConnectionId} setLocalDescription`);
105
102
  const desc = sessionDescription ? sessionDescription.toJSON ? sessionDescription.toJSON() : sessionDescription : null;
106
- const newSdp = await WebRTCModule.peerConnectionSetLocalDescription(this._peerConnectionId, desc);
107
- this.localDescription = new RTCSessionDescription(newSdp);
103
+ const {
104
+ sdpInfo,
105
+ transceiversInfo
106
+ } = await WebRTCModule.peerConnectionSetLocalDescription(this._peerConnectionId, desc);
107
+ this.localDescription = new RTCSessionDescription(sdpInfo);
108
+
109
+ this._updateTransceivers(transceiversInfo);
110
+
111
+ log.debug(`${this._peerConnectionId} setLocalDescription OK`);
108
112
  }
109
113
 
110
114
  setRemoteDescription(sessionDescription) {
115
+ log.debug(`${this._peerConnectionId} setRemoteDescription`);
111
116
  return new Promise((resolve, reject) => {
112
117
  WebRTCModule.peerConnectionSetRemoteDescription(sessionDescription.toJSON ? sessionDescription.toJSON() : sessionDescription, this._peerConnectionId, (successful, data) => {
113
118
  if (successful) {
114
- this.remoteDescription = new RTCSessionDescription(data);
119
+ log.debug(`${this._peerConnectionId} setRemoteDescription OK`);
120
+ this.remoteDescription = new RTCSessionDescription(data.sdpInfo);
121
+
122
+ this._updateTransceivers(data.transceiversInfo);
123
+
115
124
  resolve();
116
125
  } else {
117
126
  reject(data);
@@ -121,6 +130,8 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
121
130
  }
122
131
 
123
132
  async addIceCandidate(candidate) {
133
+ log.debug(`${this._peerConnectionId} addIceCandidate`);
134
+
124
135
  if (!candidate || !candidate.candidate) {
125
136
  // XXX end-of cantidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
126
137
  return;
@@ -129,8 +140,123 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
129
140
  const newSdp = await WebRTCModule.peerConnectionAddICECandidate(this._peerConnectionId, candidate.toJSON ? candidate.toJSON() : candidate);
130
141
  this.remoteDescription = new RTCSessionDescription(newSdp);
131
142
  }
143
+ /**
144
+ * @brief Adds a new track to the {@link RTCPeerConnection},
145
+ * and indicates that it is contained in the specified {@link MediaStream} array.
146
+ * This method has to be synchronous as the W3C API expects a track to be return
147
+ * @param {MediaStreamTrack} track The track to be added
148
+ * @param {MediaStream[]} streams An array of streams the track needs to be added to
149
+ * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack
150
+ */
151
+
152
+
153
+ addTrack(track) {
154
+ let streams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
155
+ log.debug(`${this._peerConnectionId} addTrack`);
156
+ if (this.connectionState === 'closed') throw new Error("Peer Connection is closed");
157
+ if (this._trackExists(track)) throw new Error("Track already exists in a sender");
158
+ const streamIds = streams.map(s => s.id);
159
+ const result = WebRTCModule.peerConnectionAddTrack(this._peerConnectionId, track.id, {
160
+ streamIds
161
+ });
162
+ if (result == null) throw new Error("Could not add sender");
163
+ const {
164
+ transceiverOrder,
165
+ transceiver,
166
+ sender
167
+ } = result; // According to the W3C docs, the sender could have been reused, and
168
+ // so we check if that is the case, and update accordingly.
169
+
170
+ const [existingSender] = this.getSenders().filter(s => s.id === sender.id);
171
+
172
+ if (existingSender) {
173
+ // Update sender
174
+ existingSender._track = track; // Update the corresponding transceiver as well
175
+
176
+ const [existingTransceiver] = this.getTransceivers().filter(t => t.sender.id === existingSender.id);
177
+ existingTransceiver._direction = transceiver.direction;
178
+ existingTransceiver._currentDirection = transceiver.currentDirection;
179
+ return existingSender;
180
+ } // This is a new transceiver, should create a transceiver for it and add it
181
+
182
+
183
+ const newSender = new RTCRtpSender({ ...transceiver.sender,
184
+ track
185
+ });
186
+ const newTransceiver = new RTCRtpTransceiver({ ...transceiver,
187
+ sender: newSender,
188
+ receiver: new RTCRtpReceiver(transceiver.receiver)
189
+ });
190
+
191
+ this._insertTransceiverSorted(transceiverOrder, newTransceiver);
192
+
193
+ return newSender;
194
+ }
195
+
196
+ addTransceiver(source, init) {
197
+ log.debug(`${this._peerConnectionId} addTransceiver`);
198
+ let src = {};
199
+
200
+ if (source === 'audio') {
201
+ src = {
202
+ type: 'audio'
203
+ };
204
+ } else if (source === 'video') {
205
+ src = {
206
+ type: 'video'
207
+ };
208
+ } else {
209
+ src = {
210
+ trackId: source.id
211
+ };
212
+ } // Extract the stream ids
213
+
214
+
215
+ if (init && init.streams) {
216
+ init.streamIds = init.streams.map(stream => {
217
+ return stream.id;
218
+ });
219
+ }
220
+
221
+ const result = WebRTCModule.peerConnectionAddTransceiver(this._peerConnectionId, { ...src,
222
+ init: { ...init
223
+ }
224
+ });
225
+
226
+ if (result == null) {
227
+ console.log("Error adding transceiver");
228
+ throw new Error("Transceiver could not be added");
229
+ }
230
+
231
+ const sender = new RTCRtpSender(result.transceiver.sender);
232
+
233
+ if (source instanceof MediaStreamTrack) {
234
+ sender._track = source;
235
+ }
236
+
237
+ const receiver = new RTCRtpReceiver(result.transceiver.receiver);
238
+ const transceiver = new RTCRtpTransceiver({ ...result.transceiver,
239
+ sender,
240
+ receiver
241
+ });
242
+
243
+ this._insertTransceiverSorted(result.transceiverOrder, transceiver);
244
+
245
+ return transceiver;
246
+ }
247
+
248
+ removeTrack(sender) {
249
+ log.debug(`${this._peerConnectionId} removeTrack`);
250
+ if (this._peerConnectionId !== sender._peerConnectionId) throw new Error("Sender does not belong to this peer connection");
251
+ if (this.connectionState === 'closed') throw new Error("Peer Connection is closed");
252
+ const existingSender = this.getSenders().find(s => s === sender);
253
+ if (!existingSender) throw new Error("Sender does not exist");
254
+ if (existingSender.track === null) return;
255
+ WebRTCModule.peerConnectionRemoveTrack(this._peerConnectionId, sender.id);
256
+ }
132
257
 
133
258
  getStats() {
259
+ log.debug(`${this._peerConnectionId} getStats`);
134
260
  return WebRTCModule.peerConnectionGetStats(this._peerConnectionId).then(data => {
135
261
  /* On both Android and iOS it is faster to construct a single
136
262
  JSON string representing the Map of StatsReports and have it
@@ -146,15 +272,29 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
146
272
  });
147
273
  }
148
274
 
149
- getLocalStreams() {
150
- return this._localStreams.slice();
275
+ getTransceivers() {
276
+ return this._transceivers.map(e => e.transceiver);
277
+ }
278
+
279
+ getSenders() {
280
+ return this._transceivers.map(e => e.transceiver.sender);
151
281
  }
152
282
 
153
- getRemoteStreams() {
154
- return this._remoteStreams.slice();
283
+ getReceivers() {
284
+ return this._transceivers.map(e => e.transceiver.receiver);
155
285
  }
156
286
 
157
287
  close() {
288
+ log.debug(`${this._peerConnectionId} close`); // According to the W3C spec: https://w3c.github.io/webrtc-pc/#rtcpeerconnection-interface
289
+ // transceivers have to be stopped
290
+
291
+ this._transceivers.forEach(_ref => {
292
+ let {
293
+ transceiver
294
+ } = _ref;
295
+ transceiver.stop();
296
+ });
297
+
158
298
  WebRTCModule.peerConnectionClose(this._peerConnectionId);
159
299
  }
160
300
 
@@ -162,47 +302,46 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
162
302
  WebRTCModule.peerConnectionRestartIce(this._peerConnectionId);
163
303
  }
164
304
 
165
- _unregisterEvents() {
166
- this._subscriptions.forEach(e => e.remove());
167
-
168
- this._subscriptions = [];
169
- }
170
-
171
305
  _registerEvents() {
172
- this._subscriptions = [EventEmitter.addListener('peerConnectionOnRenegotiationNeeded', ev => {
306
+ addListener(this, 'peerConnectionOnRenegotiationNeeded', ev => {
173
307
  if (ev.id !== this._peerConnectionId) {
174
308
  return;
175
309
  } // @ts-ignore
176
310
 
177
311
 
178
312
  this.dispatchEvent(new RTCEvent('negotiationneeded'));
179
- }), EventEmitter.addListener('peerConnectionIceConnectionChanged', ev => {
313
+ });
314
+ addListener(this, 'peerConnectionIceConnectionChanged', ev => {
180
315
  if (ev.id !== this._peerConnectionId) {
181
316
  return;
182
317
  }
183
318
 
184
- this.iceConnectionState = ev.iceConnectionState; // @ts-ignore
185
-
186
- this.dispatchEvent(new RTCEvent('iceconnectionstatechange'));
319
+ this.iceConnectionState = ev.iceConnectionState;
187
320
 
188
321
  if (ev.iceConnectionState === 'closed') {
189
322
  // This PeerConnection is done, clean up event handlers.
190
- this._unregisterEvents();
191
- }
192
- }), EventEmitter.addListener('peerConnectionStateChanged', ev => {
323
+ removeListener(this);
324
+ } // @ts-ignore
325
+
326
+
327
+ this.dispatchEvent(new RTCEvent('iceconnectionstatechange'));
328
+ });
329
+ addListener(this, 'peerConnectionStateChanged', ev => {
193
330
  if (ev.id !== this._peerConnectionId) {
194
331
  return;
195
332
  }
196
333
 
197
- this.connectionState = ev.connectionState; // @ts-ignore
198
-
199
- this.dispatchEvent(new RTCEvent('connectionstatechange'));
334
+ this.connectionState = ev.connectionState;
200
335
 
201
336
  if (ev.connectionState === 'closed') {
202
337
  // This PeerConnection is done, clean up event handlers.
203
- this._unregisterEvents();
204
- }
205
- }), EventEmitter.addListener('peerConnectionSignalingStateChanged', ev => {
338
+ removeListener(this);
339
+ } // @ts-ignore
340
+
341
+
342
+ this.dispatchEvent(new RTCEvent('connectionstatechange'));
343
+ });
344
+ addListener(this, 'peerConnectionSignalingStateChanged', ev => {
206
345
  if (ev.id !== this._peerConnectionId) {
207
346
  return;
208
347
  }
@@ -210,64 +349,110 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
210
349
  this.signalingState = ev.signalingState; // @ts-ignore
211
350
 
212
351
  this.dispatchEvent(new RTCEvent('signalingstatechange'));
213
- }), EventEmitter.addListener('peerConnectionAddedStream', ev => {
352
+ });
353
+ addListener(this, 'peerConnectionOnTrack', ev => {
214
354
  if (ev.id !== this._peerConnectionId) {
215
355
  return;
216
356
  }
217
357
 
218
- const stream = new MediaStream(ev);
358
+ log.debug(`${this._peerConnectionId} ontrack`);
359
+ const track = new MediaStreamTrack(ev.receiver.track);
360
+ let transceiver; // Make sure transceivers are stored in timestamp order. Also, we have to make
361
+ // sure we do not add a transceiver if it exists.
362
+ // sure we do not add a transceiver if it exists.
363
+ // sure we do not add a transceiver if it exists.
364
+
365
+ let [{
366
+ transceiver: oldTransceiver
367
+ } = {
368
+ transceiver: null
369
+ }] = this._transceivers.filter(_ref2 => {
370
+ let {
371
+ transceiver
372
+ } = _ref2;
373
+ return transceiver.id === ev.transceiver.id;
374
+ });
219
375
 
220
- this._remoteStreams.push(stream);
376
+ if (!oldTransceiver) {
377
+ // Creating objects out of the event data.
378
+ const receiver = new RTCRtpReceiver({ ...ev.receiver,
379
+ track
380
+ });
381
+ transceiver = new RTCRtpTransceiver({ ...ev.transceiver,
382
+ receiver
383
+ });
384
+
385
+ this._insertTransceiverSorted(ev.transceiverOrder, transceiver);
386
+ } else {
387
+ transceiver = oldTransceiver;
388
+ transceiver._receiver._track = track;
389
+ transceiver._mid = ev.transceiver.mid;
390
+ transceiver._currentDirection = ev.transceiver.currentDirection;
391
+ transceiver._direction = ev.transceiver.direction;
392
+ } // Get the stream object from the event. Create if necessary.
393
+
394
+
395
+ const streams = ev.streams.map(streamInfo => {
396
+ // Here we are making sure that we don't create stream objects that already exist
397
+ // So that event listeners do get the same object if it has been created before.
398
+ if (!this._remoteStreams.has(streamInfo.streamId)) {
399
+ const stream = new MediaStream({
400
+ streamId: streamInfo.streamId,
401
+ streamReactTag: streamInfo.streamReactTag,
402
+ tracks: []
403
+ });
404
+
405
+ this._remoteStreams.set(streamInfo.streamId, stream);
406
+ }
221
407
 
222
- this.remoteDescription = new RTCSessionDescription(ev.sdp); // @ts-ignore
408
+ const stream = this._remoteStreams.get(streamInfo.streamId);
223
409
 
224
- this.dispatchEvent(new MediaStreamEvent('addstream', {
225
- stream
226
- }));
227
- }), EventEmitter.addListener('peerConnectionRemovedStream', ev => {
228
- if (ev.id !== this._peerConnectionId) {
410
+ stream === null || stream === void 0 ? void 0 : stream._tracks.push(track);
411
+ return stream;
412
+ });
413
+ const eventData = {
414
+ streams,
415
+ transceiver,
416
+ track,
417
+ receiver: transceiver.receiver
418
+ }; // @ts-ignore
419
+
420
+ this.dispatchEvent(new RTCTrackEvent('track', eventData));
421
+ });
422
+ addListener(this, 'peerConnectionOnRemoveTrack', ev => {
423
+ if (ev.peerConnectionId !== this._peerConnectionId) {
229
424
  return;
230
425
  }
231
426
 
232
- const stream = this._remoteStreams.find(s => s._reactTag === ev.streamId);
427
+ log.debug(`${this._peerConnectionId} onremovetrack`); // Based on the W3C specs https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-removetrack,
428
+ // we need to remove the track from any media streams
429
+ // that were previously passed to the `track` event.
233
430
 
234
- if (stream) {
235
- const index = this._remoteStreams.indexOf(stream);
431
+ for (const stream of this._remoteStreams.values()) {
432
+ const trackIdx = stream._tracks.findIndex(t => t.id === ev.trackId);
236
433
 
237
- if (index !== -1) {
238
- this._remoteStreams.splice(index, 1);
434
+ if (trackIdx !== -1) {
435
+ stream._tracks.splice(trackIdx, 1);
239
436
  }
240
437
  }
241
-
242
- this.remoteDescription = new RTCSessionDescription(ev.sdp); // @ts-ignore
243
-
244
- this.dispatchEvent(new MediaStreamEvent('removestream', {
245
- stream
246
- }));
247
- }), EventEmitter.addListener('mediaStreamTrackMuteChanged', ev => {
438
+ });
439
+ addListener(this, 'peerConnectionOnRemoveTrackSuccessful', ev => {
248
440
  if (ev.peerConnectionId !== this._peerConnectionId) {
249
441
  return;
250
442
  }
251
443
 
252
- let track;
253
-
254
- for (const stream of this._remoteStreams) {
255
- const t = stream._tracks.find(track => track.id === ev.trackId);
444
+ const [existingSender] = this.getSenders().filter(s => s.id === ev.senderId);
445
+ const oldTrack = existingSender._track;
256
446
 
257
- if (t) {
258
- track = t;
259
- break;
260
- }
447
+ if (oldTrack) {
448
+ oldTrack._muted = true;
261
449
  }
262
450
 
263
- if (track) {
264
- track._muted = ev.muted;
265
- const eventName = ev.muted ? 'mute' : 'unmute';
266
- track.dispatchEvent(new MediaStreamTrackEvent(eventName, {
267
- track
268
- }));
269
- }
270
- }), EventEmitter.addListener('peerConnectionGotICECandidate', ev => {
451
+ existingSender._track = null;
452
+ const [existingTransceiver] = this.getTransceivers().filter(t => t.sender.id === existingSender.id);
453
+ existingTransceiver._direction = existingTransceiver.direction === 'sendrecv' ? 'recvonly' : 'inactive';
454
+ });
455
+ addListener(this, 'peerConnectionGotICECandidate', ev => {
271
456
  if (ev.id !== this._peerConnectionId) {
272
457
  return;
273
458
  }
@@ -278,7 +463,8 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
278
463
  this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', {
279
464
  candidate
280
465
  }));
281
- }), EventEmitter.addListener('peerConnectionIceGatheringChanged', ev => {
466
+ });
467
+ addListener(this, 'peerConnectionIceGatheringChanged', ev => {
282
468
  if (ev.id !== this._peerConnectionId) {
283
469
  return;
284
470
  }
@@ -295,7 +481,8 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
295
481
 
296
482
 
297
483
  this.dispatchEvent(new RTCEvent('icegatheringstatechange'));
298
- }), EventEmitter.addListener('peerConnectionDidOpenDataChannel', ev => {
484
+ });
485
+ addListener(this, 'peerConnectionDidOpenDataChannel', ev => {
299
486
  if (ev.id !== this._peerConnectionId) {
300
487
  return;
301
488
  }
@@ -305,7 +492,20 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
305
492
  this.dispatchEvent(new RTCDataChannelEvent('datachannel', {
306
493
  channel
307
494
  }));
308
- })];
495
+ }); // Since the current underlying architecture performs certain actions
496
+ // Asynchronously when the outer web API expects synchronous behaviour
497
+ // This is the only way to report error on operations for those who wish
498
+ // to handle them.
499
+
500
+ addListener(this, 'peerConnectionOnError', ev => {
501
+ if (ev.info.peerConnectionId !== this._peerConnectionId) {
502
+ return;
503
+ }
504
+
505
+ log.error(`onerror: ${JSON.stringify(ev)}`); // @ts-ignore
506
+
507
+ this.dispatchEvent(new RTCErrorEvent('error', ev.func, ev.message));
508
+ });
309
509
  }
310
510
  /**
311
511
  * Creates a new RTCDataChannel object with the given label. The
@@ -337,6 +537,51 @@ export default class RTCPeerConnection extends defineCustomEventTarget(...PEER_C
337
537
 
338
538
  return new RTCDataChannel(channelInfo);
339
539
  }
540
+ /**
541
+ * Check whether a media stream track exists already in a sender.
542
+ * See https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack for more information
543
+ */
544
+
545
+
546
+ _trackExists(track) {
547
+ const [sender] = this.getSenders().filter(sender => {
548
+ var _sender$track;
549
+
550
+ return ((_sender$track = sender.track) === null || _sender$track === void 0 ? void 0 : _sender$track.id) === track.id;
551
+ });
552
+ return sender ? true : false;
553
+ }
554
+ /**
555
+ * updates transceivers after offer/answer updates if necessary
556
+ */
557
+
558
+
559
+ _updateTransceivers(transceiverUpdates) {
560
+ for (var update of transceiverUpdates) {
561
+ const [transceiver] = this.getTransceivers().filter(t => t.id === update.transceiverId);
562
+ if (!transceiver) continue;
563
+ transceiver._currentDirection = update.currentDirection;
564
+ transceiver._mid = update.mid;
565
+ transceiver._sender._rtpParameters = new RTCRtpSendParameters(update.senderRtpParameters);
566
+ }
567
+ }
568
+ /**
569
+ * Inserts transceiver into the transceiver array in the order they are created (timestamp).
570
+ * @param order an index that refers to when it it was created relatively.
571
+ * @param transceiver the transceiver object to be inserted.
572
+ */
573
+
574
+
575
+ _insertTransceiverSorted(order, transceiver) {
576
+ this._transceivers.push({
577
+ order,
578
+ transceiver
579
+ });
580
+
581
+ this._transceivers.sort((a, b) => {
582
+ return a.order - b.order;
583
+ });
584
+ }
340
585
 
341
586
  }
342
587
  //# sourceMappingURL=RTCPeerConnection.js.map