@sbhjt-gr/react-native-webrtc 124.0.3 → 124.0.5

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 (361) hide show
  1. package/.clang-format +11 -11
  2. package/.eslintignore +6 -6
  3. package/.nvmrc +1 -1
  4. package/ISSUE_TEMPLATE.md +40 -40
  5. package/LICENSE +22 -22
  6. package/README.md +103 -103
  7. package/android/build.gradle +37 -37
  8. package/android/consumer-rules.pro +3 -3
  9. package/android/src/main/AndroidManifest.xml +11 -11
  10. package/android/src/main/java/com/oney/WebRTCModule/AbstractVideoCaptureController.java +113 -113
  11. package/android/src/main/java/com/oney/WebRTCModule/CameraCaptureController.java +338 -338
  12. package/android/src/main/java/com/oney/WebRTCModule/CameraEventsHandler.java +49 -49
  13. package/android/src/main/java/com/oney/WebRTCModule/DataChannelWrapper.java +99 -99
  14. package/android/src/main/java/com/oney/WebRTCModule/DataPacketCryptorManager.java +62 -62
  15. package/android/src/main/java/com/oney/WebRTCModule/DisplayUtils.java +16 -16
  16. package/android/src/main/java/com/oney/WebRTCModule/EglUtils.java +66 -66
  17. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +539 -539
  18. package/android/src/main/java/com/oney/WebRTCModule/LibraryLoader.java +21 -21
  19. package/android/src/main/java/com/oney/WebRTCModule/MediaProjectionNotification.java +70 -70
  20. package/android/src/main/java/com/oney/WebRTCModule/MediaProjectionService.java +82 -82
  21. package/android/src/main/java/com/oney/WebRTCModule/PeerConnectionObserver.java +588 -588
  22. package/android/src/main/java/com/oney/WebRTCModule/RTCCryptoManager.java +493 -493
  23. package/android/src/main/java/com/oney/WebRTCModule/RTCVideoViewManager.java +98 -98
  24. package/android/src/main/java/com/oney/WebRTCModule/ReactBridgeUtil.java +35 -35
  25. package/android/src/main/java/com/oney/WebRTCModule/ScreenCaptureController.java +94 -94
  26. package/android/src/main/java/com/oney/WebRTCModule/SerializeUtils.java +342 -342
  27. package/android/src/main/java/com/oney/WebRTCModule/StringUtils.java +100 -100
  28. package/android/src/main/java/com/oney/WebRTCModule/ThreadUtils.java +41 -41
  29. package/android/src/main/java/com/oney/WebRTCModule/TrackCapturerEventsEmitter.java +34 -34
  30. package/android/src/main/java/com/oney/WebRTCModule/VideoTrackAdapter.java +137 -137
  31. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +1649 -1643
  32. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModuleOptions.java +33 -33
  33. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModulePackage.java +21 -21
  34. package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +583 -583
  35. package/android/src/main/java/com/oney/WebRTCModule/palabra/PalabraClient.java +529 -501
  36. package/android/src/main/java/com/oney/WebRTCModule/palabra/PalabraConfig.java +17 -17
  37. package/android/src/main/java/com/oney/WebRTCModule/palabra/PalabraListener.java +7 -7
  38. package/android/src/main/java/com/oney/WebRTCModule/videoEffects/ProcessorProvider.java +38 -38
  39. package/android/src/main/java/com/oney/WebRTCModule/videoEffects/VideoEffectProcessor.java +59 -59
  40. package/android/src/main/java/com/oney/WebRTCModule/videoEffects/VideoFrameProcessor.java +19 -19
  41. package/android/src/main/java/com/oney/WebRTCModule/videoEffects/VideoFrameProcessorFactoryInterface.java +12 -12
  42. package/android/src/main/java/com/oney/WebRTCModule/webrtcutils/H264AndSoftwareVideoDecoderFactory.java +73 -73
  43. package/android/src/main/java/com/oney/WebRTCModule/webrtcutils/H264AndSoftwareVideoEncoderFactory.java +73 -73
  44. package/android/src/main/java/com/oney/WebRTCModule/webrtcutils/SoftwareVideoDecoderFactoryProxy.java +36 -36
  45. package/android/src/main/java/com/oney/WebRTCModule/webrtcutils/SoftwareVideoEncoderFactoryProxy.java +36 -36
  46. package/android/src/main/java/org/webrtc/Camera1Helper.java +54 -54
  47. package/android/src/main/java/org/webrtc/Camera2Helper.java +52 -52
  48. package/android/src/main/res/values/strings.xml +5 -5
  49. package/android/src/main/res/values/styles.xml +8 -8
  50. package/ios/RCTWebRTC/CaptureController.h +18 -18
  51. package/ios/RCTWebRTC/CaptureController.m +28 -28
  52. package/ios/RCTWebRTC/CapturerEventsDelegate.h +12 -12
  53. package/ios/RCTWebRTC/DataChannelWrapper.h +27 -27
  54. package/ios/RCTWebRTC/DataChannelWrapper.m +42 -42
  55. package/ios/RCTWebRTC/I420Converter.h +22 -22
  56. package/ios/RCTWebRTC/I420Converter.m +164 -164
  57. package/ios/RCTWebRTC/PIPController.h +24 -24
  58. package/ios/RCTWebRTC/PIPController.m +234 -234
  59. package/ios/RCTWebRTC/PalabraAudioSink.h +13 -13
  60. package/ios/RCTWebRTC/PalabraAudioSink.m +18 -18
  61. package/ios/RCTWebRTC/PalabraClient.h +42 -36
  62. package/ios/RCTWebRTC/PalabraClient.m +680 -644
  63. package/ios/RCTWebRTC/RCTConvert+WebRTC.h +16 -16
  64. package/ios/RCTWebRTC/RCTConvert+WebRTC.m +206 -206
  65. package/ios/RCTWebRTC/RTCMediaStreamTrack+React.h +10 -10
  66. package/ios/RCTWebRTC/RTCMediaStreamTrack+React.m +16 -16
  67. package/ios/RCTWebRTC/RTCVideoViewManager.h +29 -29
  68. package/ios/RCTWebRTC/RTCVideoViewManager.m +411 -411
  69. package/ios/RCTWebRTC/SampleBufferVideoCallView.h +12 -12
  70. package/ios/RCTWebRTC/SampleBufferVideoCallView.m +178 -178
  71. package/ios/RCTWebRTC/ScreenCaptureController.h +20 -20
  72. package/ios/RCTWebRTC/ScreenCaptureController.m +82 -82
  73. package/ios/RCTWebRTC/ScreenCapturePickerViewManager.h +7 -7
  74. package/ios/RCTWebRTC/ScreenCapturePickerViewManager.m +59 -59
  75. package/ios/RCTWebRTC/ScreenCapturer.h +19 -19
  76. package/ios/RCTWebRTC/ScreenCapturer.m +263 -263
  77. package/ios/RCTWebRTC/SerializeUtils.h +28 -28
  78. package/ios/RCTWebRTC/SerializeUtils.m +314 -314
  79. package/ios/RCTWebRTC/SocketConnection.h +13 -13
  80. package/ios/RCTWebRTC/SocketConnection.m +137 -137
  81. package/ios/RCTWebRTC/TrackCapturerEventsEmitter.h +14 -14
  82. package/ios/RCTWebRTC/TrackCapturerEventsEmitter.m +36 -36
  83. package/ios/RCTWebRTC/VideoCaptureController.h +21 -21
  84. package/ios/RCTWebRTC/VideoCaptureController.m +328 -328
  85. package/ios/RCTWebRTC/WLVAudioDevice.h +12 -12
  86. package/ios/RCTWebRTC/WLVAudioDevice.m +137 -137
  87. package/ios/RCTWebRTC/WebRTCModule+Palabra.h +4 -4
  88. package/ios/RCTWebRTC/WebRTCModule+Palabra.m +92 -83
  89. package/ios/RCTWebRTC/WebRTCModule+Permissions.m +75 -75
  90. package/ios/RCTWebRTC/WebRTCModule+RTCAudioSession.m +20 -20
  91. package/ios/RCTWebRTC/WebRTCModule+RTCDataChannel.h +14 -14
  92. package/ios/RCTWebRTC/WebRTCModule+RTCDataChannel.m +165 -165
  93. package/ios/RCTWebRTC/WebRTCModule+RTCFrameCryptor.m +611 -611
  94. package/ios/RCTWebRTC/WebRTCModule+RTCMediaStream.h +13 -13
  95. package/ios/RCTWebRTC/WebRTCModule+RTCMediaStream.m +728 -728
  96. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.h +24 -24
  97. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +1004 -1004
  98. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +267 -267
  99. package/ios/RCTWebRTC/WebRTCModule+VideoTrackAdapter.h +12 -12
  100. package/ios/RCTWebRTC/WebRTCModule+VideoTrackAdapter.m +166 -166
  101. package/ios/RCTWebRTC/WebRTCModule.h +58 -58
  102. package/ios/RCTWebRTC/WebRTCModule.m +169 -169
  103. package/ios/RCTWebRTC/WebRTCModuleOptions.h +24 -24
  104. package/ios/RCTWebRTC/WebRTCModuleOptions.m +31 -31
  105. package/ios/RCTWebRTC/videoEffects/ProcessorProvider.h +9 -9
  106. package/ios/RCTWebRTC/videoEffects/ProcessorProvider.m +23 -23
  107. package/ios/RCTWebRTC/videoEffects/VideoEffectProcessor.h +13 -13
  108. package/ios/RCTWebRTC/videoEffects/VideoEffectProcessor.m +23 -23
  109. package/ios/RCTWebRTC/videoEffects/VideoFrameProcessor.h +8 -8
  110. package/ios/RCTWebRTC.xcodeproj/project.pbxproj +404 -404
  111. package/ios/RCTWebRTC.xcworkspace/contents.xcworkspacedata +10 -10
  112. package/lib/commonjs/Constraints.js.map +1 -1
  113. package/lib/commonjs/EventEmitter.js.map +1 -1
  114. package/lib/commonjs/Logger.js.map +1 -1
  115. package/lib/commonjs/MediaDevices.js +17 -17
  116. package/lib/commonjs/MediaDevices.js.map +1 -1
  117. package/lib/commonjs/MediaStream.js +19 -19
  118. package/lib/commonjs/MediaStream.js.map +1 -1
  119. package/lib/commonjs/MediaStreamError.js.map +1 -1
  120. package/lib/commonjs/MediaStreamErrorEvent.js.map +1 -1
  121. package/lib/commonjs/MediaStreamTrack.js +28 -28
  122. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  123. package/lib/commonjs/MediaStreamTrackEvent.js +6 -6
  124. package/lib/commonjs/MediaStreamTrackEvent.js.map +1 -1
  125. package/lib/commonjs/MessageEvent.js +7 -7
  126. package/lib/commonjs/MessageEvent.js.map +1 -1
  127. package/lib/commonjs/Permissions.js +28 -28
  128. package/lib/commonjs/Permissions.js.map +1 -1
  129. package/lib/commonjs/RTCAudioSession.js +4 -4
  130. package/lib/commonjs/RTCAudioSession.js.map +1 -1
  131. package/lib/commonjs/RTCDataChannel.js +2 -2
  132. package/lib/commonjs/RTCDataChannel.js.map +1 -1
  133. package/lib/commonjs/RTCDataChannelEvent.js +6 -6
  134. package/lib/commonjs/RTCDataChannelEvent.js.map +1 -1
  135. package/lib/commonjs/RTCDataPacketCryptor.js.map +1 -1
  136. package/lib/commonjs/RTCDataPacketCryptorFactory.js.map +1 -1
  137. package/lib/commonjs/RTCErrorEvent.js +3 -3
  138. package/lib/commonjs/RTCErrorEvent.js.map +1 -1
  139. package/lib/commonjs/RTCFrameCryptor.js +8 -8
  140. package/lib/commonjs/RTCFrameCryptor.js.map +1 -1
  141. package/lib/commonjs/RTCFrameCryptorFactory.js.map +1 -1
  142. package/lib/commonjs/RTCIceCandidate.js.map +1 -1
  143. package/lib/commonjs/RTCIceCandidateEvent.js +7 -7
  144. package/lib/commonjs/RTCIceCandidateEvent.js.map +1 -1
  145. package/lib/commonjs/RTCKeyProvider.js.map +1 -1
  146. package/lib/commonjs/RTCPIPView.js +2 -2
  147. package/lib/commonjs/RTCPIPView.js.map +1 -1
  148. package/lib/commonjs/RTCPIPView.web.js.map +1 -1
  149. package/lib/commonjs/RTCPeerConnection.js +36 -36
  150. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  151. package/lib/commonjs/RTCRtcpParameters.js.map +1 -1
  152. package/lib/commonjs/RTCRtpCapabilities.js +2 -2
  153. package/lib/commonjs/RTCRtpCapabilities.js.map +1 -1
  154. package/lib/commonjs/RTCRtpCodecCapability.js.map +1 -1
  155. package/lib/commonjs/RTCRtpCodecParameters.js.map +1 -1
  156. package/lib/commonjs/RTCRtpEncodingParameters.js.map +1 -1
  157. package/lib/commonjs/RTCRtpHeaderExtension.js.map +1 -1
  158. package/lib/commonjs/RTCRtpParameters.js.map +1 -1
  159. package/lib/commonjs/RTCRtpReceiveParameters.js.map +1 -1
  160. package/lib/commonjs/RTCRtpReceiver.js +7 -7
  161. package/lib/commonjs/RTCRtpReceiver.js.map +1 -1
  162. package/lib/commonjs/RTCRtpSendParameters.js +3 -3
  163. package/lib/commonjs/RTCRtpSendParameters.js.map +1 -1
  164. package/lib/commonjs/RTCRtpSender.js +7 -7
  165. package/lib/commonjs/RTCRtpSender.js.map +1 -1
  166. package/lib/commonjs/RTCRtpTransceiver.js.map +1 -1
  167. package/lib/commonjs/RTCSessionDescription.js.map +1 -1
  168. package/lib/commonjs/RTCTrackEvent.js +6 -6
  169. package/lib/commonjs/RTCTrackEvent.js.map +1 -1
  170. package/lib/commonjs/RTCUtil.js +28 -28
  171. package/lib/commonjs/RTCUtil.js.map +1 -1
  172. package/lib/commonjs/RTCView.js +5 -5
  173. package/lib/commonjs/RTCView.js.map +1 -1
  174. package/lib/commonjs/RTCView.web.js.map +1 -1
  175. package/lib/commonjs/ScreenCapturePickerView.js.map +1 -1
  176. package/lib/commonjs/ScreenCapturePickerView.web.js.map +1 -1
  177. package/lib/commonjs/getDisplayMedia.js.map +1 -1
  178. package/lib/commonjs/getUserMedia.js.map +1 -1
  179. package/lib/commonjs/index.js.map +1 -1
  180. package/lib/commonjs/index.web.js.map +1 -1
  181. package/lib/commonjs/webStream.js.map +1 -1
  182. package/lib/module/Constraints.js.map +1 -1
  183. package/lib/module/EventEmitter.js.map +1 -1
  184. package/lib/module/Logger.js.map +1 -1
  185. package/lib/module/MediaDevices.js +17 -17
  186. package/lib/module/MediaDevices.js.map +1 -1
  187. package/lib/module/MediaStream.js +19 -19
  188. package/lib/module/MediaStream.js.map +1 -1
  189. package/lib/module/MediaStreamError.js.map +1 -1
  190. package/lib/module/MediaStreamErrorEvent.js.map +1 -1
  191. package/lib/module/MediaStreamTrack.js +28 -28
  192. package/lib/module/MediaStreamTrack.js.map +1 -1
  193. package/lib/module/MediaStreamTrackEvent.js +6 -6
  194. package/lib/module/MediaStreamTrackEvent.js.map +1 -1
  195. package/lib/module/MessageEvent.js +7 -7
  196. package/lib/module/MessageEvent.js.map +1 -1
  197. package/lib/module/Permissions.js +28 -28
  198. package/lib/module/Permissions.js.map +1 -1
  199. package/lib/module/RTCAudioSession.js +4 -4
  200. package/lib/module/RTCAudioSession.js.map +1 -1
  201. package/lib/module/RTCDataChannel.js +2 -2
  202. package/lib/module/RTCDataChannel.js.map +1 -1
  203. package/lib/module/RTCDataChannelEvent.js +6 -6
  204. package/lib/module/RTCDataChannelEvent.js.map +1 -1
  205. package/lib/module/RTCDataPacketCryptor.js.map +1 -1
  206. package/lib/module/RTCDataPacketCryptorFactory.js.map +1 -1
  207. package/lib/module/RTCErrorEvent.js +3 -3
  208. package/lib/module/RTCErrorEvent.js.map +1 -1
  209. package/lib/module/RTCFrameCryptor.js +8 -8
  210. package/lib/module/RTCFrameCryptor.js.map +1 -1
  211. package/lib/module/RTCFrameCryptorFactory.js.map +1 -1
  212. package/lib/module/RTCIceCandidate.js.map +1 -1
  213. package/lib/module/RTCIceCandidateEvent.js +7 -7
  214. package/lib/module/RTCIceCandidateEvent.js.map +1 -1
  215. package/lib/module/RTCKeyProvider.js.map +1 -1
  216. package/lib/module/RTCPIPView.js +2 -2
  217. package/lib/module/RTCPIPView.js.map +1 -1
  218. package/lib/module/RTCPIPView.web.js.map +1 -1
  219. package/lib/module/RTCPeerConnection.js +36 -36
  220. package/lib/module/RTCPeerConnection.js.map +1 -1
  221. package/lib/module/RTCRtcpParameters.js.map +1 -1
  222. package/lib/module/RTCRtpCapabilities.js +2 -2
  223. package/lib/module/RTCRtpCapabilities.js.map +1 -1
  224. package/lib/module/RTCRtpCodecCapability.js.map +1 -1
  225. package/lib/module/RTCRtpCodecParameters.js.map +1 -1
  226. package/lib/module/RTCRtpEncodingParameters.js.map +1 -1
  227. package/lib/module/RTCRtpHeaderExtension.js.map +1 -1
  228. package/lib/module/RTCRtpParameters.js.map +1 -1
  229. package/lib/module/RTCRtpReceiveParameters.js.map +1 -1
  230. package/lib/module/RTCRtpReceiver.js +7 -7
  231. package/lib/module/RTCRtpReceiver.js.map +1 -1
  232. package/lib/module/RTCRtpSendParameters.js +3 -3
  233. package/lib/module/RTCRtpSendParameters.js.map +1 -1
  234. package/lib/module/RTCRtpSender.js +7 -7
  235. package/lib/module/RTCRtpSender.js.map +1 -1
  236. package/lib/module/RTCRtpTransceiver.js.map +1 -1
  237. package/lib/module/RTCSessionDescription.js.map +1 -1
  238. package/lib/module/RTCTrackEvent.js +6 -6
  239. package/lib/module/RTCTrackEvent.js.map +1 -1
  240. package/lib/module/RTCUtil.js +28 -28
  241. package/lib/module/RTCUtil.js.map +1 -1
  242. package/lib/module/RTCView.js +5 -5
  243. package/lib/module/RTCView.js.map +1 -1
  244. package/lib/module/RTCView.web.js.map +1 -1
  245. package/lib/module/ScreenCapturePickerView.js.map +1 -1
  246. package/lib/module/ScreenCapturePickerView.web.js.map +1 -1
  247. package/lib/module/getDisplayMedia.js.map +1 -1
  248. package/lib/module/getUserMedia.js.map +1 -1
  249. package/lib/module/index.js.map +1 -1
  250. package/lib/module/index.web.js.map +1 -1
  251. package/lib/module/webStream.js.map +1 -1
  252. package/lib/typescript/Constraints.d.ts +19 -19
  253. package/lib/typescript/EventEmitter.d.ts +6 -6
  254. package/lib/typescript/Logger.d.ts +13 -13
  255. package/lib/typescript/MediaDevices.d.ts +30 -30
  256. package/lib/typescript/MediaStream.d.ts +48 -48
  257. package/lib/typescript/MediaStreamError.d.ts +6 -6
  258. package/lib/typescript/MediaStreamErrorEvent.d.ts +6 -6
  259. package/lib/typescript/MediaStreamTrack.d.ts +101 -101
  260. package/lib/typescript/MediaStreamTrackEvent.d.ts +19 -19
  261. package/lib/typescript/MessageEvent.d.ts +20 -20
  262. package/lib/typescript/Permissions.d.ts +55 -55
  263. package/lib/typescript/RTCAudioSession.d.ts +10 -10
  264. package/lib/typescript/RTCDataChannel.d.ts +43 -43
  265. package/lib/typescript/RTCDataChannelEvent.d.ts +19 -19
  266. package/lib/typescript/RTCDataPacketCryptor.d.ts +12 -12
  267. package/lib/typescript/RTCDataPacketCryptorFactory.d.ts +6 -6
  268. package/lib/typescript/RTCErrorEvent.d.ts +12 -12
  269. package/lib/typescript/RTCFrameCryptor.d.ts +47 -47
  270. package/lib/typescript/RTCFrameCryptorFactory.d.ts +21 -21
  271. package/lib/typescript/RTCIceCandidate.d.ts +17 -17
  272. package/lib/typescript/RTCIceCandidateEvent.d.ts +20 -20
  273. package/lib/typescript/RTCKeyProvider.d.ts +21 -21
  274. package/lib/typescript/RTCPIPView.d.ts +15 -15
  275. package/lib/typescript/RTCPIPView.web.d.ts +13 -13
  276. package/lib/typescript/RTCPeerConnection.d.ts +117 -117
  277. package/lib/typescript/RTCRtcpParameters.d.ts +10 -10
  278. package/lib/typescript/RTCRtpCapabilities.d.ts +9 -9
  279. package/lib/typescript/RTCRtpCodecCapability.d.ts +7 -7
  280. package/lib/typescript/RTCRtpCodecParameters.d.ts +16 -16
  281. package/lib/typescript/RTCRtpEncodingParameters.d.ts +23 -23
  282. package/lib/typescript/RTCRtpHeaderExtension.d.ts +12 -12
  283. package/lib/typescript/RTCRtpParameters.d.ts +19 -19
  284. package/lib/typescript/RTCRtpReceiveParameters.d.ts +4 -4
  285. package/lib/typescript/RTCRtpReceiver.d.ts +21 -21
  286. package/lib/typescript/RTCRtpSendParameters.d.ts +20 -20
  287. package/lib/typescript/RTCRtpSender.d.ts +22 -22
  288. package/lib/typescript/RTCRtpTransceiver.d.ts +31 -31
  289. package/lib/typescript/RTCSessionDescription.d.ts +12 -12
  290. package/lib/typescript/RTCTrackEvent.d.ts +29 -29
  291. package/lib/typescript/RTCUtil.d.ts +37 -37
  292. package/lib/typescript/RTCView.d.ts +117 -117
  293. package/lib/typescript/RTCView.web.d.ts +25 -25
  294. package/lib/typescript/ScreenCapturePickerView.d.ts +2 -2
  295. package/lib/typescript/ScreenCapturePickerView.web.d.ts +1 -1
  296. package/lib/typescript/getDisplayMedia.d.ts +2 -2
  297. package/lib/typescript/getUserMedia.d.ts +7 -7
  298. package/lib/typescript/index.d.ts +22 -22
  299. package/lib/typescript/index.web.d.ts +101 -101
  300. package/lib/typescript/webStream.d.ts +3 -3
  301. package/livekit-react-native-webrtc.podspec +29 -29
  302. package/macos/RCTWebRTC.xcodeproj/project.pbxproj +324 -324
  303. package/macos/RCTWebRTC.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -7
  304. package/macos/RCTWebRTC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -8
  305. package/metro.config.js +7 -7
  306. package/metro.config.macos.js +14 -14
  307. package/package.json +66 -66
  308. package/react-native.config.js +11 -11
  309. package/src/.eslintrc.cjs +67 -67
  310. package/src/Constraints.ts +20 -20
  311. package/src/EventEmitter.ts +65 -65
  312. package/src/Logger.ts +49 -49
  313. package/src/MediaDevices.ts +53 -53
  314. package/src/MediaStream.ts +161 -161
  315. package/src/MediaStreamError.ts +12 -12
  316. package/src/MediaStreamErrorEvent.ts +11 -11
  317. package/src/MediaStreamTrack.ts +282 -282
  318. package/src/MediaStreamTrackEvent.ts +25 -25
  319. package/src/MessageEvent.ts +26 -26
  320. package/src/Permissions.ts +133 -133
  321. package/src/RTCAudioSession.ts +25 -25
  322. package/src/RTCDataChannel.ts +190 -190
  323. package/src/RTCDataChannelEvent.ts +28 -28
  324. package/src/RTCDataPacketCryptor.ts +90 -90
  325. package/src/RTCDataPacketCryptorFactory.ts +24 -24
  326. package/src/RTCErrorEvent.ts +20 -20
  327. package/src/RTCFrameCryptor.ts +162 -162
  328. package/src/RTCFrameCryptorFactory.ts +101 -101
  329. package/src/RTCIceCandidate.ts +29 -29
  330. package/src/RTCIceCandidateEvent.ts +26 -26
  331. package/src/RTCKeyProvider.ts +117 -117
  332. package/src/RTCPIPView.tsx +46 -46
  333. package/src/RTCPIPView.web.tsx +18 -18
  334. package/src/RTCPeerConnection.ts +935 -935
  335. package/src/RTCRtcpParameters.ts +23 -23
  336. package/src/RTCRtpCapabilities.ts +16 -16
  337. package/src/RTCRtpCodecCapability.ts +12 -12
  338. package/src/RTCRtpCodecParameters.ts +44 -44
  339. package/src/RTCRtpEncodingParameters.ts +90 -90
  340. package/src/RTCRtpHeaderExtension.ts +27 -27
  341. package/src/RTCRtpParameters.ts +37 -37
  342. package/src/RTCRtpReceiveParameters.ts +7 -7
  343. package/src/RTCRtpReceiver.ts +60 -60
  344. package/src/RTCRtpSendParameters.ts +63 -63
  345. package/src/RTCRtpSender.ts +78 -78
  346. package/src/RTCRtpTransceiver.ts +107 -107
  347. package/src/RTCSessionDescription.ts +30 -30
  348. package/src/RTCTrackEvent.ts +42 -42
  349. package/src/RTCUtil.ts +211 -211
  350. package/src/RTCView.ts +122 -122
  351. package/src/RTCView.web.tsx +80 -80
  352. package/src/ScreenCapturePickerView.ts +4 -4
  353. package/src/ScreenCapturePickerView.web.tsx +3 -3
  354. package/src/getDisplayMedia.ts +30 -30
  355. package/src/getUserMedia.ts +136 -136
  356. package/src/index.ts +107 -107
  357. package/src/index.web.ts +191 -191
  358. package/src/webStream.ts +31 -31
  359. package/tools/format.sh +6 -6
  360. package/tools/release.sh +45 -45
  361. package/tsconfig.json +17 -17
@@ -1,935 +1,935 @@
1
- import { EventTarget, Event, defineEventAttribute } from 'event-target-shim/index';
2
- import { NativeModules } from 'react-native';
3
-
4
- import { addListener, removeListener } from './EventEmitter';
5
- import Logger from './Logger';
6
- import MediaStream from './MediaStream';
7
- import MediaStreamTrack from './MediaStreamTrack';
8
- import MediaStreamTrackEvent from './MediaStreamTrackEvent';
9
- import RTCDataChannel from './RTCDataChannel';
10
- import RTCDataChannelEvent from './RTCDataChannelEvent';
11
- import RTCIceCandidate from './RTCIceCandidate';
12
- import RTCIceCandidateEvent from './RTCIceCandidateEvent';
13
- import RTCRtpReceiveParameters from './RTCRtpReceiveParameters';
14
- import RTCRtpReceiver from './RTCRtpReceiver';
15
- import RTCRtpSendParameters from './RTCRtpSendParameters';
16
- import RTCRtpSender from './RTCRtpSender';
17
- import RTCRtpTransceiver from './RTCRtpTransceiver';
18
- import RTCSessionDescription, { RTCSessionDescriptionInit } from './RTCSessionDescription';
19
- import RTCTrackEvent from './RTCTrackEvent';
20
- import * as RTCUtil from './RTCUtil';
21
- import { RTCOfferOptions } from './RTCUtil';
22
-
23
- const log = new Logger('pc');
24
- const { WebRTCModule } = NativeModules;
25
-
26
- type RTCSignalingState =
27
- | 'stable'
28
- | 'have-local-offer'
29
- | 'have-remote-offer'
30
- | 'have-local-pranswer'
31
- | 'have-remote-pranswer'
32
- | 'closed';
33
-
34
- type RTCIceGatheringState = 'new' | 'gathering' | 'complete';
35
-
36
- type RTCPeerConnectionState = 'new' | 'connecting' | 'connected' | 'disconnected' | 'failed' | 'closed';
37
-
38
- type RTCIceConnectionState = 'new' | 'checking' | 'connected' | 'completed' | 'failed' | 'disconnected' | 'closed';
39
-
40
- type RTCDataChannelInit = {
41
- ordered?: boolean,
42
- maxPacketLifeTime?: number,
43
- maxRetransmits?: number,
44
- protocol?: string,
45
- negotiated?: boolean,
46
- id?: number
47
- };
48
-
49
- type RTCIceServer = {
50
- credential?: string,
51
- url?: string, // Deprecated.
52
- urls?: string | string[],
53
- username?: string
54
- };
55
-
56
- type RTCConfiguration = {
57
- bundlePolicy?: 'balanced' | 'max-compat' | 'max-bundle',
58
- iceCandidatePoolSize?: number,
59
- iceServers?: RTCIceServer[],
60
- iceTransportPolicy?: 'all' | 'relay',
61
- rtcpMuxPolicy?: 'negotiate' | 'require'
62
- };
63
-
64
- type RTCPeerConnectionEventMap = {
65
- connectionstatechange: Event<'connectionstatechange'>
66
- icecandidate: RTCIceCandidateEvent<'icecandidate'>
67
- icecandidateerror: RTCIceCandidateEvent<'icecandidateerror'>
68
- iceconnectionstatechange: Event<'iceconnectionstatechange'>
69
- icegatheringstatechange: Event<'icegatheringstatechange'>
70
- negotiationneeded: Event<'negotiationneeded'>
71
- signalingstatechange: Event<'signalingstatechange'>
72
- datachannel: RTCDataChannelEvent<'datachannel'>
73
- track: RTCTrackEvent<'track'>
74
- error: Event<'error'>
75
- }
76
-
77
- let nextPeerConnectionId = 0;
78
-
79
- export default class RTCPeerConnection extends EventTarget<RTCPeerConnectionEventMap> {
80
- localDescription: RTCSessionDescription | null = null;
81
- remoteDescription: RTCSessionDescription | null = null;
82
- signalingState: RTCSignalingState = 'stable';
83
- iceGatheringState: RTCIceGatheringState = 'new';
84
- connectionState: RTCPeerConnectionState = 'new';
85
- iceConnectionState: RTCIceConnectionState = 'new';
86
-
87
- _pcId: number;
88
- _transceivers: { order: number, transceiver: RTCRtpTransceiver }[];
89
- _remoteStreams: Map<string, MediaStream>;
90
- _pendingTrackEvents: any[];
91
-
92
- constructor(configuration?: RTCConfiguration) {
93
- super();
94
-
95
- this._pcId = nextPeerConnectionId++;
96
-
97
- // Sanitize ICE servers.
98
- if (configuration) {
99
- const servers = configuration?.iceServers ?? [];
100
-
101
- for (const server of servers) {
102
- let urls = server.url || server.urls;
103
-
104
- delete server.url;
105
- delete server.urls;
106
-
107
- if (!urls) {
108
- continue;
109
- }
110
-
111
- if (!Array.isArray(urls)) {
112
- urls = [ urls ];
113
- }
114
-
115
- // Native WebRTC does case sensitive parsing.
116
- server.urls = urls.map(url => url.toLowerCase());
117
- }
118
-
119
- // Filter out bogus servers.
120
- configuration.iceServers = servers.filter(s => s.urls);
121
- }
122
-
123
- if (!WebRTCModule.peerConnectionInit(configuration, this._pcId)) {
124
- throw new Error('Failed to initialize PeerConnection, check the native logs!');
125
- }
126
-
127
- this._transceivers = [];
128
- this._remoteStreams = new Map();
129
- this._pendingTrackEvents = [];
130
-
131
- this._registerEvents();
132
-
133
- log.debug(`${this._pcId} ctor`);
134
- }
135
-
136
- async createOffer(options?:RTCOfferOptions) {
137
- const _t0 = Date.now();
138
- console.log(`[RNWebRTC] pc:${this._pcId} create_offer_start`, {
139
- signalingState: this.signalingState,
140
- connectionState: this.connectionState,
141
- iceConnectionState: this.iceConnectionState,
142
- });
143
- log.debug(`${this._pcId} createOffer`);
144
-
145
- const {
146
- sdpInfo,
147
- newTransceivers,
148
- transceiversInfo
149
- } = await WebRTCModule.peerConnectionCreateOffer(this._pcId, RTCUtil.normalizeOfferOptions(options));
150
-
151
- console.log(`[RNWebRTC] pc:${this._pcId} create_offer_done`, {
152
- durationMs: Date.now() - _t0,
153
- sdpType: sdpInfo?.type,
154
- sdpLen: sdpInfo?.sdp?.length ?? 0,
155
- });
156
- log.debug(`${this._pcId} createOffer OK`);
157
-
158
- newTransceivers?.forEach(t => {
159
- const { transceiverOrder, transceiver } = t;
160
- const newSender = new RTCRtpSender({ ...transceiver.sender, track: null });
161
- const remoteTrack
162
- = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
163
- const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
164
- const newTransceiver = new RTCRtpTransceiver({
165
- ...transceiver,
166
- sender: newSender,
167
- receiver: newReceiver,
168
- });
169
-
170
- this._insertTransceiverSorted(transceiverOrder, newTransceiver);
171
- });
172
-
173
- this._updateTransceivers(transceiversInfo);
174
-
175
- return sdpInfo;
176
- }
177
-
178
- async createAnswer() {
179
- const _t0 = Date.now();
180
- console.log(`[RNWebRTC] pc:${this._pcId} create_answer_start`, {
181
- signalingState: this.signalingState,
182
- connectionState: this.connectionState,
183
- });
184
- log.debug(`${this._pcId} createAnswer`);
185
-
186
- const {
187
- sdpInfo,
188
- transceiversInfo
189
- } = await WebRTCModule.peerConnectionCreateAnswer(this._pcId, {});
190
-
191
- console.log(`[RNWebRTC] pc:${this._pcId} create_answer_done`, {
192
- durationMs: Date.now() - _t0,
193
- sdpType: sdpInfo?.type,
194
- sdpLen: sdpInfo?.sdp?.length ?? 0,
195
- });
196
-
197
- this._updateTransceivers(transceiversInfo);
198
-
199
- return sdpInfo;
200
- }
201
-
202
- setConfiguration(configuration): void {
203
- WebRTCModule.peerConnectionSetConfiguration(configuration, this._pcId);
204
- }
205
-
206
- async setLocalDescription(sessionDescription?: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
207
- const _t0 = Date.now();
208
- console.log(`[RNWebRTC] pc:${this._pcId} set_local_desc_start`, {
209
- type: sessionDescription?.type,
210
- sdpLen: sessionDescription?.sdp?.length ?? 0,
211
- signalingState: this.signalingState,
212
- });
213
- log.debug(`${this._pcId} setLocalDescription`);
214
-
215
- let desc;
216
-
217
- if (sessionDescription) {
218
- desc = {
219
- type: sessionDescription.type,
220
- sdp: sessionDescription.sdp ?? ''
221
- };
222
-
223
- if (!RTCUtil.isSdpTypeValid(desc.type)) {
224
- throw new Error(`Invalid session description: invalid type: ${desc.type}`);
225
- }
226
- } else {
227
- desc = null;
228
- }
229
-
230
- const {
231
- sdpInfo,
232
- transceiversInfo
233
- } = await WebRTCModule.peerConnectionSetLocalDescription(this._pcId, desc);
234
-
235
- if (sdpInfo.type && sdpInfo.sdp) {
236
- this.localDescription = new RTCSessionDescription(sdpInfo);
237
- } else {
238
- this.localDescription = null;
239
- }
240
-
241
- this._updateTransceivers(transceiversInfo, /* removeStopped */ desc?.type === 'answer');
242
-
243
- console.log(`[RNWebRTC] pc:${this._pcId} set_local_desc_done`, {
244
- durationMs: Date.now() - _t0,
245
- signalingState: this.signalingState,
246
- });
247
- log.debug(`${this._pcId} setLocalDescription OK`);
248
- }
249
-
250
- async setRemoteDescription(sessionDescription: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
251
- const _t0 = Date.now();
252
- console.log(`[RNWebRTC] pc:${this._pcId} set_remote_desc_start`, {
253
- type: sessionDescription?.type,
254
- sdpLen: sessionDescription?.sdp?.length ?? 0,
255
- signalingState: this.signalingState,
256
- connectionState: this.connectionState,
257
- });
258
- log.debug(`${this._pcId} setRemoteDescription`);
259
-
260
- if (!sessionDescription) {
261
- return Promise.reject(new Error('No session description provided'));
262
- }
263
-
264
- const desc = {
265
- type: sessionDescription.type,
266
- sdp: sessionDescription.sdp ?? ''
267
- };
268
-
269
- if (!RTCUtil.isSdpTypeValid(desc.type ?? '')) {
270
- throw new Error(`Invalid session description: invalid type: ${desc.type}`);
271
- }
272
-
273
- const {
274
- sdpInfo,
275
- newTransceivers,
276
- transceiversInfo
277
- } = await WebRTCModule.peerConnectionSetRemoteDescription(this._pcId, desc);
278
-
279
- if (sdpInfo.type && sdpInfo.sdp) {
280
- this.remoteDescription = new RTCSessionDescription(sdpInfo);
281
- } else {
282
- this.remoteDescription = null;
283
- }
284
-
285
- newTransceivers?.forEach(t => {
286
- const { transceiverOrder, transceiver } = t;
287
- const newSender = new RTCRtpSender({ ...transceiver.sender, track: null });
288
- const remoteTrack
289
- = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
290
- const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
291
- const newTransceiver = new RTCRtpTransceiver({
292
- ...transceiver,
293
- sender: newSender,
294
- receiver: newReceiver,
295
- });
296
-
297
- this._insertTransceiverSorted(transceiverOrder, newTransceiver);
298
- });
299
-
300
- this._updateTransceivers(transceiversInfo, /* removeStopped */ desc.type === 'answer');
301
-
302
- // Fire track events. They must fire before sRD resolves.
303
- const pendingTrackEvents = this._pendingTrackEvents;
304
-
305
- this._pendingTrackEvents = [];
306
-
307
- for (const ev of pendingTrackEvents) {
308
- const [ transceiver ] = this
309
- .getTransceivers()
310
- .filter(t => t.receiver.id === ev.receiver.id);
311
-
312
- // We need to fire this event for an existing track sometimes, like
313
- // when the transceiver direction (on the sending side) switches from
314
- // sendrecv to recvonly and then back.
315
-
316
- // @ts-ignore
317
- const track: MediaStreamTrack = transceiver.receiver.track;
318
-
319
- transceiver._mid = ev.transceiver.mid;
320
- transceiver._currentDirection = ev.transceiver.currentDirection;
321
- transceiver._direction = ev.transceiver.direction;
322
-
323
- // Get the stream object from the event. Create if necessary.
324
- const streams: MediaStream[] = ev.streams.map(streamInfo => {
325
- // Here we are making sure that we don't create stream objects that already exist
326
- // So that event listeners do get the same object if it has been created before.
327
- if (!this._remoteStreams.has(streamInfo.streamId)) {
328
- const stream = new MediaStream({
329
- streamId: streamInfo.streamId,
330
- streamReactTag: streamInfo.streamReactTag,
331
- tracks: []
332
- });
333
-
334
- this._remoteStreams.set(streamInfo.streamId, stream);
335
- }
336
-
337
- const stream = this._remoteStreams.get(streamInfo.streamId);
338
-
339
- if (!stream?._tracks.includes(track)) {
340
- stream?._tracks.push(track);
341
- }
342
-
343
- return stream;
344
- });
345
-
346
- const eventData = {
347
- streams,
348
- transceiver,
349
- track,
350
- receiver: transceiver.receiver
351
- };
352
-
353
-
354
- this.dispatchEvent(new RTCTrackEvent('track', eventData));
355
-
356
- streams.forEach(stream => {
357
- stream.dispatchEvent(new MediaStreamTrackEvent('addtrack', { track }));
358
- });
359
-
360
- // Dispatch an unmute event for the track.
361
- track._setMutedInternal(false);
362
- }
363
-
364
- console.log(`[RNWebRTC] pc:${this._pcId} set_remote_desc_done`, {
365
- durationMs: Date.now() - _t0,
366
- signalingState: this.signalingState,
367
- pendingTrackEventsDispatched: this._pendingTrackEvents.length,
368
- });
369
- log.debug(`${this._pcId} setRemoteDescription OK`);
370
- }
371
-
372
- async addIceCandidate(candidate): Promise<void> {
373
- console.log(`[RNWebRTC] pc:${this._pcId} add_ice_candidate`, {
374
- type: candidate?.type,
375
- protocol: candidate?.protocol,
376
- sdpMid: candidate?.sdpMid,
377
- sdpMLineIndex: candidate?.sdpMLineIndex,
378
- empty: !candidate?.candidate,
379
- signalingState: this.signalingState,
380
- connectionState: this.connectionState,
381
- iceConnectionState: this.iceConnectionState,
382
- });
383
- log.debug(`${this._pcId} addIceCandidate`);
384
-
385
- if (!candidate || !candidate.candidate) {
386
- // XXX end-of candidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
387
- return;
388
- }
389
-
390
- if ((candidate.sdpMLineIndex === null ||
391
- candidate.sdpMLineIndex === undefined) &&
392
- (candidate.sdpMid === null ||
393
- candidate.sdpMid === undefined)
394
- ) {
395
- throw new TypeError('`sdpMLineIndex` and `sdpMid` must not be both null or undefined');
396
- }
397
-
398
- const newSdp = await WebRTCModule.peerConnectionAddICECandidate(
399
- this._pcId,
400
- RTCUtil.deepClone(candidate)
401
- );
402
-
403
- this.remoteDescription = new RTCSessionDescription(newSdp);
404
- }
405
-
406
- /**
407
- * @brief Adds a new track to the {@link RTCPeerConnection},
408
- * and indicates that it is contained in the specified {@link MediaStream}s.
409
- * This method has to be synchronous as the W3C API expects a track to be returned
410
- * @param {MediaStreamTrack} track The track to be added
411
- * @param {...MediaStream} streams One or more {@link MediaStream}s the track needs to be added to
412
- * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack
413
- */
414
- addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
415
- log.debug(`${this._pcId} addTrack`);
416
-
417
- if (this.connectionState === 'closed') {
418
- throw new Error('Peer Connection is closed');
419
- }
420
-
421
- if (this._trackExists(track)) {
422
- throw new Error('Track already exists in a sender');
423
- }
424
-
425
- const streamIds = streams.map(s => s.id);
426
- const result = WebRTCModule.peerConnectionAddTrack(this._pcId, track.id, { streamIds });
427
-
428
- if (result === null) {
429
- throw new Error('Could not add sender');
430
- }
431
-
432
- const { transceiverOrder, transceiver, sender } = result;
433
-
434
- // According to the W3C docs, the sender could have been reused, and
435
- // so we check if that is the case, and update accordingly.
436
- const [ existingSender ] = this
437
- .getSenders()
438
- .filter(s => s.id === sender.id);
439
-
440
- if (existingSender) {
441
- // Update sender
442
- existingSender._track = track;
443
-
444
- // Update the corresponding transceiver as well
445
- const [ existingTransceiver ] = this
446
- .getTransceivers()
447
- .filter(t => t.sender.id === existingSender.id);
448
-
449
- existingTransceiver._direction = transceiver.direction;
450
- existingTransceiver._currentDirection = transceiver.currentDirection;
451
-
452
- return existingSender;
453
- }
454
-
455
- // This is a new transceiver, should create a transceiver for it and add it
456
- const newSender = new RTCRtpSender({ ...transceiver.sender, track });
457
- const remoteTrack = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
458
- const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
459
- const newTransceiver = new RTCRtpTransceiver({
460
- ...transceiver,
461
- sender: newSender,
462
- receiver: newReceiver,
463
- });
464
-
465
- this._insertTransceiverSorted(transceiverOrder, newTransceiver);
466
-
467
- return newSender;
468
- }
469
-
470
- addTransceiver(source: 'audio' | 'video' | MediaStreamTrack, init): RTCRtpTransceiver {
471
- log.debug(`${this._pcId} addTransceiver`);
472
-
473
- let src = {};
474
-
475
- if (source === 'audio') {
476
- src = { type: 'audio' };
477
- } else if (source === 'video') {
478
- src = { type: 'video' };
479
- } else {
480
- src = { trackId: source.id };
481
- }
482
-
483
- // Extract the stream ids
484
- if (init && init.streams) {
485
- init.streamIds = init.streams.map(stream => stream.id);
486
- }
487
-
488
- const result = WebRTCModule.peerConnectionAddTransceiver(this._pcId, { ...src, init: { ...init } });
489
-
490
- if (result === null) {
491
- throw new Error('Transceiver could not be added');
492
- }
493
-
494
- const t = result.transceiver;
495
- let track: MediaStreamTrack | null = null;
496
-
497
- if (typeof source === 'string') {
498
- if (t.sender.track) {
499
- track = new MediaStreamTrack(t.sender.track);
500
- }
501
- } else {
502
- // 'source' is a MediaStreamTrack
503
- track = source;
504
- }
505
-
506
- const sender = new RTCRtpSender({ ...t.sender, track });
507
- const remoteTrack = t.receiver.track ? new MediaStreamTrack(t.receiver.track) : null;
508
- const receiver = new RTCRtpReceiver({ ...t.receiver, track: remoteTrack });
509
- const transceiver = new RTCRtpTransceiver({
510
- ...result.transceiver,
511
- sender,
512
- receiver
513
- });
514
-
515
- this._insertTransceiverSorted(result.transceiverOrder, transceiver);
516
-
517
- return transceiver;
518
- }
519
-
520
- removeTrack(sender: RTCRtpSender) {
521
- log.debug(`${this._pcId} removeTrack`);
522
-
523
- if (this._pcId !== sender._peerConnectionId) {
524
- throw new Error('Sender does not belong to this peer connection');
525
- }
526
-
527
- if (this.connectionState === 'closed') {
528
- throw new Error('Peer Connection is closed');
529
- }
530
-
531
- const existingSender = this
532
- .getSenders()
533
- .find(s => s === sender);
534
-
535
- if (!existingSender) {
536
- throw new Error('Sender does not exist');
537
- }
538
-
539
- if (existingSender.track === null) {
540
- return;
541
- }
542
-
543
- // Blocking!
544
- WebRTCModule.peerConnectionRemoveTrack(this._pcId, sender.id);
545
-
546
- existingSender._track = null;
547
-
548
- const [ existingTransceiver ] = this
549
- .getTransceivers()
550
- .filter(t => t.sender.id === existingSender.id);
551
-
552
- existingTransceiver._direction = existingTransceiver.direction === 'sendrecv' ? 'recvonly' : 'inactive';
553
- }
554
-
555
- async getStats(selector?: MediaStreamTrack) {
556
- log.debug(`${this._pcId} getStats`);
557
-
558
- if (!selector) {
559
- const data = await WebRTCModule.peerConnectionGetStats(this._pcId);
560
-
561
- /**
562
- * On both Android and iOS it is faster to construct a single
563
- * JSON string representing the Map of StatsReports and have it
564
- * pass through the React Native bridge rather than the Map of
565
- * StatsReports. While the implementations do try to be faster in
566
- * general, the stress is on being faster to pass through the React
567
- * Native bridge which is a bottleneck that tends to be visible in
568
- * the UI when there is congestion involving UI-related passing.
569
- */
570
- return new Map(JSON.parse(data));
571
- } else {
572
- const senders = this.getSenders().filter(s => s.track === selector);
573
- const receivers = this.getReceivers().filter(r => r.track === selector);
574
- const matches = senders.length + receivers.length;
575
-
576
- if (matches === 0) {
577
- throw new Error('Invalid selector: could not find matching sender / receiver');
578
- } else if (matches > 1) {
579
- throw new Error('Invalid selector: multiple matching senders / receivers');
580
- } else {
581
- const sr = senders[0] || receivers[0];
582
-
583
- return sr.getStats();
584
- }
585
- }
586
- }
587
-
588
- getTransceivers(): RTCRtpTransceiver[] {
589
- return this._transceivers.map(e => e.transceiver);
590
- }
591
-
592
- getSenders(): RTCRtpSender[] {
593
- return this._transceivers.filter(e => !e.transceiver.stopped).map(e => e.transceiver.sender);
594
- }
595
-
596
- getReceivers(): RTCRtpReceiver[] {
597
- return this._transceivers.filter(e => !e.transceiver.stopped).map(e => e.transceiver.receiver);
598
- }
599
-
600
- close(): void {
601
- log.debug(`${this._pcId} close`);
602
-
603
- if (this.connectionState === 'closed') {
604
- return;
605
- }
606
-
607
- WebRTCModule.peerConnectionClose(this._pcId);
608
-
609
- // Mark transceivers as stopped.
610
- this._transceivers.forEach(({ transceiver })=> {
611
- transceiver._setStopped();
612
- });
613
- }
614
-
615
- restartIce(): void {
616
- WebRTCModule.peerConnectionRestartIce(this._pcId);
617
- }
618
-
619
- _registerEvents(): void {
620
- addListener(this, 'peerConnectionOnRenegotiationNeeded', (ev: any) => {
621
- if (ev.pcId !== this._pcId) {
622
- return;
623
- }
624
-
625
- console.log(`[RNWebRTC] pc:${this._pcId} negotiation_needed`, {
626
- signalingState: this.signalingState,
627
- connectionState: this.connectionState,
628
- iceConnectionState: this.iceConnectionState,
629
- });
630
-
631
- this.dispatchEvent(new Event('negotiationneeded'));
632
- });
633
-
634
- addListener(this, 'peerConnectionIceConnectionChanged', (ev: any) => {
635
- if (ev.pcId !== this._pcId) {
636
- return;
637
- }
638
-
639
- const prev = this.iceConnectionState;
640
- this.iceConnectionState = ev.iceConnectionState;
641
- console.log(`[RNWebRTC] pc:${this._pcId} ice_connection_changed`, {
642
- prev,
643
- current: ev.iceConnectionState,
644
- connectionState: this.connectionState,
645
- signalingState: this.signalingState,
646
- });
647
-
648
- this.dispatchEvent(new Event('iceconnectionstatechange'));
649
- });
650
-
651
- addListener(this, 'peerConnectionStateChanged', (ev: any) => {
652
- if (ev.pcId !== this._pcId) {
653
- return;
654
- }
655
-
656
- const prev = this.connectionState;
657
- this.connectionState = ev.connectionState;
658
- console.log(`[RNWebRTC] pc:${this._pcId} connection_state_changed`, {
659
- prev,
660
- current: ev.connectionState,
661
- iceConnectionState: this.iceConnectionState,
662
- iceGatheringState: this.iceGatheringState,
663
- signalingState: this.signalingState,
664
- });
665
-
666
- this.dispatchEvent(new Event('connectionstatechange'));
667
-
668
- if (ev.connectionState === 'closed') {
669
- console.log(`[RNWebRTC] pc:${this._pcId} disposing`);
670
- // This PeerConnection is done, clean up.
671
- removeListener(this);
672
-
673
- WebRTCModule.peerConnectionDispose(this._pcId);
674
- }
675
- });
676
-
677
- addListener(this, 'peerConnectionSignalingStateChanged', (ev: any) => {
678
- if (ev.pcId !== this._pcId) {
679
- return;
680
- }
681
-
682
- const prev = this.signalingState;
683
- this.signalingState = ev.signalingState;
684
- console.log(`[RNWebRTC] pc:${this._pcId} signaling_state_changed`, {
685
- prev,
686
- current: ev.signalingState,
687
- connectionState: this.connectionState,
688
- });
689
-
690
- this.dispatchEvent(new Event('signalingstatechange'));
691
- });
692
-
693
- // Consider moving away from this event: https://github.com/WebKit/WebKit/pull/3953
694
- addListener(this, 'peerConnectionOnTrack', (ev: any) => {
695
- if (ev.pcId !== this._pcId) {
696
- return;
697
- }
698
-
699
- console.log(`[RNWebRTC] pc:${this._pcId} track_event_queued`, {
700
- receiverId: ev.receiver?.id,
701
- trackKind: ev.receiver?.track?.kind,
702
- streamCount: ev.streams?.length ?? 0,
703
- pendingCount: this._pendingTrackEvents.length + 1,
704
- });
705
-
706
- log.debug(`${this._pcId} ontrack`);
707
-
708
- // NOTE: We need to make sure the track event fires right before sRD completes,
709
- // so we queue them up here and dispatch the events when sRD fires, but before completing it.
710
- // In the future we should probably implement out own logic and drop this event altogether.
711
- this._pendingTrackEvents.push(ev);
712
- });
713
-
714
- addListener(this, 'peerConnectionOnRemoveTrack', (ev: any) => {
715
- if (ev.pcId !== this._pcId) {
716
- return;
717
- }
718
-
719
- console.log(`[RNWebRTC] pc:${this._pcId} remove_track`, { receiverId: ev.receiverId });
720
- log.debug(`${this._pcId} onremovetrack ${ev.receiverId}`);
721
-
722
- const receiver = this.getReceivers().find(r => r.id === ev.receiverId);
723
- const track = receiver?.track;
724
-
725
- if (receiver && track) {
726
- // As per the spec:
727
- // - Remove the track from any media streams that were previously passed to the `track` event.
728
- // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-removetrack,
729
- // - Mark the track as muted:
730
- // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
731
- for (const stream of this._remoteStreams.values()) {
732
- if (stream._tracks.includes(track)) {
733
- const trackIdx = stream._tracks.indexOf(track);
734
-
735
- log.debug(`${this._pcId} removetrack ${track.id}`);
736
-
737
- stream._tracks.splice(trackIdx, 1);
738
-
739
- stream.dispatchEvent(new MediaStreamTrackEvent('removetrack', { track }));
740
-
741
- // Dispatch a mute event for the track.
742
- track._setMutedInternal(true);
743
- }
744
- }
745
- }
746
- });
747
-
748
- addListener(this, 'peerConnectionGotICECandidate', (ev: any) => {
749
- if (ev.pcId !== this._pcId) {
750
- return;
751
- }
752
-
753
- console.log(`[RNWebRTC] pc:${this._pcId} local_ice_candidate`, {
754
- type: ev.candidate?.type,
755
- protocol: ev.candidate?.protocol,
756
- sdpMid: ev.candidate?.sdpMid,
757
- iceGatheringState: this.iceGatheringState,
758
- });
759
-
760
- const sdpInfo = ev.sdp;
761
-
762
- // Can happen when doing a rollback.
763
- if (sdpInfo.type && sdpInfo.sdp) {
764
- this.localDescription = new RTCSessionDescription(sdpInfo);
765
- } else {
766
- this.localDescription = null;
767
- }
768
-
769
- const candidate = new RTCIceCandidate(ev.candidate);
770
-
771
- this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', { candidate }));
772
- });
773
-
774
- addListener(this, 'peerConnectionIceGatheringChanged', (ev: any) => {
775
- if (ev.pcId !== this._pcId) {
776
- return;
777
- }
778
-
779
- const prev = this.iceGatheringState;
780
- this.iceGatheringState = ev.iceGatheringState;
781
- console.log(`[RNWebRTC] pc:${this._pcId} ice_gathering_state_changed`, {
782
- prev,
783
- current: ev.iceGatheringState,
784
- connectionState: this.connectionState,
785
- });
786
-
787
- if (this.iceGatheringState === 'complete') {
788
- const sdpInfo = ev.sdp;
789
-
790
- // Can happen when doing a rollback.
791
- if (sdpInfo.type && sdpInfo.sdp) {
792
- this.localDescription = new RTCSessionDescription(sdpInfo);
793
- } else {
794
- this.localDescription = null;
795
- }
796
-
797
- this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', { candidate: null }));
798
- }
799
-
800
- this.dispatchEvent(new Event('icegatheringstatechange'));
801
- });
802
-
803
- addListener(this, 'peerConnectionDidOpenDataChannel', (ev: any) => {
804
- if (ev.pcId !== this._pcId) {
805
- return;
806
- }
807
-
808
- const channel = new RTCDataChannel(ev.dataChannel);
809
-
810
- this.dispatchEvent(new RTCDataChannelEvent('datachannel', { channel }));
811
-
812
- // Send 'open' event. Native doesn't update the state since it's already
813
- // set at this point.
814
- channel.dispatchEvent(new RTCDataChannelEvent('open', { channel }));
815
- });
816
-
817
- addListener(this, 'mediaStreamTrackMuteChanged', (ev: any) => {
818
- if (ev.pcId !== this._pcId) {
819
- return;
820
- }
821
-
822
- const [
823
- track
824
- ] = this.getReceivers().map(r => r.track).filter(t => t?.id === ev.trackId);
825
-
826
- if (track) {
827
- track._setMutedInternal(ev.muted);
828
- }
829
- });
830
- }
831
-
832
- /**
833
- * Creates a new RTCDataChannel object with the given label. The
834
- * RTCDataChannelInit dictionary can be used to configure properties of the
835
- * underlying channel such as data reliability.
836
- *
837
- * @param {string} label - the value with which the label attribute of the new
838
- * instance is to be initialized
839
- * @param {RTCDataChannelInit} dataChannelDict - an optional dictionary of
840
- * values with which to initialize corresponding attributes of the new
841
- * instance such as id
842
- */
843
- createDataChannel(label: string, dataChannelDict?: RTCDataChannelInit): RTCDataChannel {
844
- if (arguments.length === 0) {
845
- throw new TypeError('1 argument required, but 0 present');
846
- }
847
-
848
- if (dataChannelDict && 'id' in dataChannelDict) {
849
- const id = dataChannelDict.id;
850
-
851
- if (typeof id !== 'number') {
852
- throw new TypeError('DataChannel id must be a number: ' + id);
853
- }
854
- }
855
-
856
- const channelInfo = WebRTCModule.createDataChannel(this._pcId, String(label), dataChannelDict);
857
-
858
- if (channelInfo === null) {
859
- throw new TypeError('Failed to create new DataChannel');
860
- }
861
-
862
- return new RTCDataChannel(channelInfo);
863
- }
864
-
865
- /**
866
- * Check whether a media stream track exists already in a sender.
867
- * See https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack for more information
868
- */
869
- _trackExists(track: MediaStreamTrack): boolean {
870
- const [ sender ] = this
871
- .getSenders()
872
- .filter(
873
- sender => sender.track?.id === track.id
874
- );
875
-
876
- return sender? true : false;
877
- }
878
-
879
- /**
880
- * Updates transceivers after offer/answer updates if necessary.
881
- */
882
- _updateTransceivers(transceiverUpdates, removeStopped = false) {
883
- for (const update of transceiverUpdates) {
884
- const [ transceiver ] = this
885
- .getTransceivers()
886
- .filter(t => t.sender.id === update.transceiverId);
887
-
888
- if (!transceiver) {
889
- continue;
890
- }
891
-
892
- if (update.currentDirection) {
893
- transceiver._currentDirection = update.currentDirection;
894
- }
895
-
896
- transceiver._mid = update.mid;
897
- transceiver._stopped = Boolean(update.isStopped);
898
- transceiver._sender._rtpParameters = new RTCRtpSendParameters(update.senderRtpParameters);
899
- transceiver._receiver._rtpParameters = new RTCRtpReceiveParameters(update.receiverRtpParameters);
900
- }
901
-
902
- if (removeStopped) {
903
- const stopped = this.getTransceivers().filter(t => t.stopped);
904
- const newTransceivers = this._transceivers.filter(t => !stopped.includes(t.transceiver));
905
-
906
- this._transceivers = newTransceivers;
907
- }
908
- }
909
-
910
- /**
911
- * Inserts transceiver into the transceiver array in the order they are created (timestamp).
912
- * @param order an index that refers to when it it was created relatively.
913
- * @param transceiver the transceiver object to be inserted.
914
- */
915
- _insertTransceiverSorted(order: number, transceiver: RTCRtpTransceiver) {
916
- this._transceivers.push({ order, transceiver });
917
- this._transceivers.sort((a, b) => a.order - b.order);
918
- }
919
- }
920
-
921
- /**
922
- * Define the `onxxx` event handlers.
923
- */
924
- const proto = RTCPeerConnection.prototype;
925
-
926
- defineEventAttribute(proto, 'connectionstatechange');
927
- defineEventAttribute(proto, 'icecandidate');
928
- defineEventAttribute(proto, 'icecandidateerror');
929
- defineEventAttribute(proto, 'iceconnectionstatechange');
930
- defineEventAttribute(proto, 'icegatheringstatechange');
931
- defineEventAttribute(proto, 'negotiationneeded');
932
- defineEventAttribute(proto, 'signalingstatechange');
933
- defineEventAttribute(proto, 'datachannel');
934
- defineEventAttribute(proto, 'track');
935
- defineEventAttribute(proto, 'error');
1
+ import { EventTarget, Event, defineEventAttribute } from 'event-target-shim/index';
2
+ import { NativeModules } from 'react-native';
3
+
4
+ import { addListener, removeListener } from './EventEmitter';
5
+ import Logger from './Logger';
6
+ import MediaStream from './MediaStream';
7
+ import MediaStreamTrack from './MediaStreamTrack';
8
+ import MediaStreamTrackEvent from './MediaStreamTrackEvent';
9
+ import RTCDataChannel from './RTCDataChannel';
10
+ import RTCDataChannelEvent from './RTCDataChannelEvent';
11
+ import RTCIceCandidate from './RTCIceCandidate';
12
+ import RTCIceCandidateEvent from './RTCIceCandidateEvent';
13
+ import RTCRtpReceiveParameters from './RTCRtpReceiveParameters';
14
+ import RTCRtpReceiver from './RTCRtpReceiver';
15
+ import RTCRtpSendParameters from './RTCRtpSendParameters';
16
+ import RTCRtpSender from './RTCRtpSender';
17
+ import RTCRtpTransceiver from './RTCRtpTransceiver';
18
+ import RTCSessionDescription, { RTCSessionDescriptionInit } from './RTCSessionDescription';
19
+ import RTCTrackEvent from './RTCTrackEvent';
20
+ import * as RTCUtil from './RTCUtil';
21
+ import { RTCOfferOptions } from './RTCUtil';
22
+
23
+ const log = new Logger('pc');
24
+ const { WebRTCModule } = NativeModules;
25
+
26
+ type RTCSignalingState =
27
+ | 'stable'
28
+ | 'have-local-offer'
29
+ | 'have-remote-offer'
30
+ | 'have-local-pranswer'
31
+ | 'have-remote-pranswer'
32
+ | 'closed';
33
+
34
+ type RTCIceGatheringState = 'new' | 'gathering' | 'complete';
35
+
36
+ type RTCPeerConnectionState = 'new' | 'connecting' | 'connected' | 'disconnected' | 'failed' | 'closed';
37
+
38
+ type RTCIceConnectionState = 'new' | 'checking' | 'connected' | 'completed' | 'failed' | 'disconnected' | 'closed';
39
+
40
+ type RTCDataChannelInit = {
41
+ ordered?: boolean,
42
+ maxPacketLifeTime?: number,
43
+ maxRetransmits?: number,
44
+ protocol?: string,
45
+ negotiated?: boolean,
46
+ id?: number
47
+ };
48
+
49
+ type RTCIceServer = {
50
+ credential?: string,
51
+ url?: string, // Deprecated.
52
+ urls?: string | string[],
53
+ username?: string
54
+ };
55
+
56
+ type RTCConfiguration = {
57
+ bundlePolicy?: 'balanced' | 'max-compat' | 'max-bundle',
58
+ iceCandidatePoolSize?: number,
59
+ iceServers?: RTCIceServer[],
60
+ iceTransportPolicy?: 'all' | 'relay',
61
+ rtcpMuxPolicy?: 'negotiate' | 'require'
62
+ };
63
+
64
+ type RTCPeerConnectionEventMap = {
65
+ connectionstatechange: Event<'connectionstatechange'>
66
+ icecandidate: RTCIceCandidateEvent<'icecandidate'>
67
+ icecandidateerror: RTCIceCandidateEvent<'icecandidateerror'>
68
+ iceconnectionstatechange: Event<'iceconnectionstatechange'>
69
+ icegatheringstatechange: Event<'icegatheringstatechange'>
70
+ negotiationneeded: Event<'negotiationneeded'>
71
+ signalingstatechange: Event<'signalingstatechange'>
72
+ datachannel: RTCDataChannelEvent<'datachannel'>
73
+ track: RTCTrackEvent<'track'>
74
+ error: Event<'error'>
75
+ }
76
+
77
+ let nextPeerConnectionId = 0;
78
+
79
+ export default class RTCPeerConnection extends EventTarget<RTCPeerConnectionEventMap> {
80
+ localDescription: RTCSessionDescription | null = null;
81
+ remoteDescription: RTCSessionDescription | null = null;
82
+ signalingState: RTCSignalingState = 'stable';
83
+ iceGatheringState: RTCIceGatheringState = 'new';
84
+ connectionState: RTCPeerConnectionState = 'new';
85
+ iceConnectionState: RTCIceConnectionState = 'new';
86
+
87
+ _pcId: number;
88
+ _transceivers: { order: number, transceiver: RTCRtpTransceiver }[];
89
+ _remoteStreams: Map<string, MediaStream>;
90
+ _pendingTrackEvents: any[];
91
+
92
+ constructor(configuration?: RTCConfiguration) {
93
+ super();
94
+
95
+ this._pcId = nextPeerConnectionId++;
96
+
97
+ // Sanitize ICE servers.
98
+ if (configuration) {
99
+ const servers = configuration?.iceServers ?? [];
100
+
101
+ for (const server of servers) {
102
+ let urls = server.url || server.urls;
103
+
104
+ delete server.url;
105
+ delete server.urls;
106
+
107
+ if (!urls) {
108
+ continue;
109
+ }
110
+
111
+ if (!Array.isArray(urls)) {
112
+ urls = [ urls ];
113
+ }
114
+
115
+ // Native WebRTC does case sensitive parsing.
116
+ server.urls = urls.map(url => url.toLowerCase());
117
+ }
118
+
119
+ // Filter out bogus servers.
120
+ configuration.iceServers = servers.filter(s => s.urls);
121
+ }
122
+
123
+ if (!WebRTCModule.peerConnectionInit(configuration, this._pcId)) {
124
+ throw new Error('Failed to initialize PeerConnection, check the native logs!');
125
+ }
126
+
127
+ this._transceivers = [];
128
+ this._remoteStreams = new Map();
129
+ this._pendingTrackEvents = [];
130
+
131
+ this._registerEvents();
132
+
133
+ log.debug(`${this._pcId} ctor`);
134
+ }
135
+
136
+ async createOffer(options?:RTCOfferOptions) {
137
+ const _t0 = Date.now();
138
+ console.log(`[RNWebRTC] pc:${this._pcId} create_offer_start`, {
139
+ signalingState: this.signalingState,
140
+ connectionState: this.connectionState,
141
+ iceConnectionState: this.iceConnectionState,
142
+ });
143
+ log.debug(`${this._pcId} createOffer`);
144
+
145
+ const {
146
+ sdpInfo,
147
+ newTransceivers,
148
+ transceiversInfo
149
+ } = await WebRTCModule.peerConnectionCreateOffer(this._pcId, RTCUtil.normalizeOfferOptions(options));
150
+
151
+ console.log(`[RNWebRTC] pc:${this._pcId} create_offer_done`, {
152
+ durationMs: Date.now() - _t0,
153
+ sdpType: sdpInfo?.type,
154
+ sdpLen: sdpInfo?.sdp?.length ?? 0,
155
+ });
156
+ log.debug(`${this._pcId} createOffer OK`);
157
+
158
+ newTransceivers?.forEach(t => {
159
+ const { transceiverOrder, transceiver } = t;
160
+ const newSender = new RTCRtpSender({ ...transceiver.sender, track: null });
161
+ const remoteTrack
162
+ = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
163
+ const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
164
+ const newTransceiver = new RTCRtpTransceiver({
165
+ ...transceiver,
166
+ sender: newSender,
167
+ receiver: newReceiver,
168
+ });
169
+
170
+ this._insertTransceiverSorted(transceiverOrder, newTransceiver);
171
+ });
172
+
173
+ this._updateTransceivers(transceiversInfo);
174
+
175
+ return sdpInfo;
176
+ }
177
+
178
+ async createAnswer() {
179
+ const _t0 = Date.now();
180
+ console.log(`[RNWebRTC] pc:${this._pcId} create_answer_start`, {
181
+ signalingState: this.signalingState,
182
+ connectionState: this.connectionState,
183
+ });
184
+ log.debug(`${this._pcId} createAnswer`);
185
+
186
+ const {
187
+ sdpInfo,
188
+ transceiversInfo
189
+ } = await WebRTCModule.peerConnectionCreateAnswer(this._pcId, {});
190
+
191
+ console.log(`[RNWebRTC] pc:${this._pcId} create_answer_done`, {
192
+ durationMs: Date.now() - _t0,
193
+ sdpType: sdpInfo?.type,
194
+ sdpLen: sdpInfo?.sdp?.length ?? 0,
195
+ });
196
+
197
+ this._updateTransceivers(transceiversInfo);
198
+
199
+ return sdpInfo;
200
+ }
201
+
202
+ setConfiguration(configuration): void {
203
+ WebRTCModule.peerConnectionSetConfiguration(configuration, this._pcId);
204
+ }
205
+
206
+ async setLocalDescription(sessionDescription?: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
207
+ const _t0 = Date.now();
208
+ console.log(`[RNWebRTC] pc:${this._pcId} set_local_desc_start`, {
209
+ type: sessionDescription?.type,
210
+ sdpLen: sessionDescription?.sdp?.length ?? 0,
211
+ signalingState: this.signalingState,
212
+ });
213
+ log.debug(`${this._pcId} setLocalDescription`);
214
+
215
+ let desc;
216
+
217
+ if (sessionDescription) {
218
+ desc = {
219
+ type: sessionDescription.type,
220
+ sdp: sessionDescription.sdp ?? ''
221
+ };
222
+
223
+ if (!RTCUtil.isSdpTypeValid(desc.type)) {
224
+ throw new Error(`Invalid session description: invalid type: ${desc.type}`);
225
+ }
226
+ } else {
227
+ desc = null;
228
+ }
229
+
230
+ const {
231
+ sdpInfo,
232
+ transceiversInfo
233
+ } = await WebRTCModule.peerConnectionSetLocalDescription(this._pcId, desc);
234
+
235
+ if (sdpInfo.type && sdpInfo.sdp) {
236
+ this.localDescription = new RTCSessionDescription(sdpInfo);
237
+ } else {
238
+ this.localDescription = null;
239
+ }
240
+
241
+ this._updateTransceivers(transceiversInfo, /* removeStopped */ desc?.type === 'answer');
242
+
243
+ console.log(`[RNWebRTC] pc:${this._pcId} set_local_desc_done`, {
244
+ durationMs: Date.now() - _t0,
245
+ signalingState: this.signalingState,
246
+ });
247
+ log.debug(`${this._pcId} setLocalDescription OK`);
248
+ }
249
+
250
+ async setRemoteDescription(sessionDescription: RTCSessionDescription | RTCSessionDescriptionInit): Promise<void> {
251
+ const _t0 = Date.now();
252
+ console.log(`[RNWebRTC] pc:${this._pcId} set_remote_desc_start`, {
253
+ type: sessionDescription?.type,
254
+ sdpLen: sessionDescription?.sdp?.length ?? 0,
255
+ signalingState: this.signalingState,
256
+ connectionState: this.connectionState,
257
+ });
258
+ log.debug(`${this._pcId} setRemoteDescription`);
259
+
260
+ if (!sessionDescription) {
261
+ return Promise.reject(new Error('No session description provided'));
262
+ }
263
+
264
+ const desc = {
265
+ type: sessionDescription.type,
266
+ sdp: sessionDescription.sdp ?? ''
267
+ };
268
+
269
+ if (!RTCUtil.isSdpTypeValid(desc.type ?? '')) {
270
+ throw new Error(`Invalid session description: invalid type: ${desc.type}`);
271
+ }
272
+
273
+ const {
274
+ sdpInfo,
275
+ newTransceivers,
276
+ transceiversInfo
277
+ } = await WebRTCModule.peerConnectionSetRemoteDescription(this._pcId, desc);
278
+
279
+ if (sdpInfo.type && sdpInfo.sdp) {
280
+ this.remoteDescription = new RTCSessionDescription(sdpInfo);
281
+ } else {
282
+ this.remoteDescription = null;
283
+ }
284
+
285
+ newTransceivers?.forEach(t => {
286
+ const { transceiverOrder, transceiver } = t;
287
+ const newSender = new RTCRtpSender({ ...transceiver.sender, track: null });
288
+ const remoteTrack
289
+ = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
290
+ const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
291
+ const newTransceiver = new RTCRtpTransceiver({
292
+ ...transceiver,
293
+ sender: newSender,
294
+ receiver: newReceiver,
295
+ });
296
+
297
+ this._insertTransceiverSorted(transceiverOrder, newTransceiver);
298
+ });
299
+
300
+ this._updateTransceivers(transceiversInfo, /* removeStopped */ desc.type === 'answer');
301
+
302
+ // Fire track events. They must fire before sRD resolves.
303
+ const pendingTrackEvents = this._pendingTrackEvents;
304
+
305
+ this._pendingTrackEvents = [];
306
+
307
+ for (const ev of pendingTrackEvents) {
308
+ const [ transceiver ] = this
309
+ .getTransceivers()
310
+ .filter(t => t.receiver.id === ev.receiver.id);
311
+
312
+ // We need to fire this event for an existing track sometimes, like
313
+ // when the transceiver direction (on the sending side) switches from
314
+ // sendrecv to recvonly and then back.
315
+
316
+ // @ts-ignore
317
+ const track: MediaStreamTrack = transceiver.receiver.track;
318
+
319
+ transceiver._mid = ev.transceiver.mid;
320
+ transceiver._currentDirection = ev.transceiver.currentDirection;
321
+ transceiver._direction = ev.transceiver.direction;
322
+
323
+ // Get the stream object from the event. Create if necessary.
324
+ const streams: MediaStream[] = ev.streams.map(streamInfo => {
325
+ // Here we are making sure that we don't create stream objects that already exist
326
+ // So that event listeners do get the same object if it has been created before.
327
+ if (!this._remoteStreams.has(streamInfo.streamId)) {
328
+ const stream = new MediaStream({
329
+ streamId: streamInfo.streamId,
330
+ streamReactTag: streamInfo.streamReactTag,
331
+ tracks: []
332
+ });
333
+
334
+ this._remoteStreams.set(streamInfo.streamId, stream);
335
+ }
336
+
337
+ const stream = this._remoteStreams.get(streamInfo.streamId);
338
+
339
+ if (!stream?._tracks.includes(track)) {
340
+ stream?._tracks.push(track);
341
+ }
342
+
343
+ return stream;
344
+ });
345
+
346
+ const eventData = {
347
+ streams,
348
+ transceiver,
349
+ track,
350
+ receiver: transceiver.receiver
351
+ };
352
+
353
+
354
+ this.dispatchEvent(new RTCTrackEvent('track', eventData));
355
+
356
+ streams.forEach(stream => {
357
+ stream.dispatchEvent(new MediaStreamTrackEvent('addtrack', { track }));
358
+ });
359
+
360
+ // Dispatch an unmute event for the track.
361
+ track._setMutedInternal(false);
362
+ }
363
+
364
+ console.log(`[RNWebRTC] pc:${this._pcId} set_remote_desc_done`, {
365
+ durationMs: Date.now() - _t0,
366
+ signalingState: this.signalingState,
367
+ pendingTrackEventsDispatched: this._pendingTrackEvents.length,
368
+ });
369
+ log.debug(`${this._pcId} setRemoteDescription OK`);
370
+ }
371
+
372
+ async addIceCandidate(candidate): Promise<void> {
373
+ console.log(`[RNWebRTC] pc:${this._pcId} add_ice_candidate`, {
374
+ type: candidate?.type,
375
+ protocol: candidate?.protocol,
376
+ sdpMid: candidate?.sdpMid,
377
+ sdpMLineIndex: candidate?.sdpMLineIndex,
378
+ empty: !candidate?.candidate,
379
+ signalingState: this.signalingState,
380
+ connectionState: this.connectionState,
381
+ iceConnectionState: this.iceConnectionState,
382
+ });
383
+ log.debug(`${this._pcId} addIceCandidate`);
384
+
385
+ if (!candidate || !candidate.candidate) {
386
+ // XXX end-of candidates is not implemented: https://bugs.chromium.org/p/webrtc/issues/detail?id=9218
387
+ return;
388
+ }
389
+
390
+ if ((candidate.sdpMLineIndex === null ||
391
+ candidate.sdpMLineIndex === undefined) &&
392
+ (candidate.sdpMid === null ||
393
+ candidate.sdpMid === undefined)
394
+ ) {
395
+ throw new TypeError('`sdpMLineIndex` and `sdpMid` must not be both null or undefined');
396
+ }
397
+
398
+ const newSdp = await WebRTCModule.peerConnectionAddICECandidate(
399
+ this._pcId,
400
+ RTCUtil.deepClone(candidate)
401
+ );
402
+
403
+ this.remoteDescription = new RTCSessionDescription(newSdp);
404
+ }
405
+
406
+ /**
407
+ * @brief Adds a new track to the {@link RTCPeerConnection},
408
+ * and indicates that it is contained in the specified {@link MediaStream}s.
409
+ * This method has to be synchronous as the W3C API expects a track to be returned
410
+ * @param {MediaStreamTrack} track The track to be added
411
+ * @param {...MediaStream} streams One or more {@link MediaStream}s the track needs to be added to
412
+ * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack
413
+ */
414
+ addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
415
+ log.debug(`${this._pcId} addTrack`);
416
+
417
+ if (this.connectionState === 'closed') {
418
+ throw new Error('Peer Connection is closed');
419
+ }
420
+
421
+ if (this._trackExists(track)) {
422
+ throw new Error('Track already exists in a sender');
423
+ }
424
+
425
+ const streamIds = streams.map(s => s.id);
426
+ const result = WebRTCModule.peerConnectionAddTrack(this._pcId, track.id, { streamIds });
427
+
428
+ if (result === null) {
429
+ throw new Error('Could not add sender');
430
+ }
431
+
432
+ const { transceiverOrder, transceiver, sender } = result;
433
+
434
+ // According to the W3C docs, the sender could have been reused, and
435
+ // so we check if that is the case, and update accordingly.
436
+ const [ existingSender ] = this
437
+ .getSenders()
438
+ .filter(s => s.id === sender.id);
439
+
440
+ if (existingSender) {
441
+ // Update sender
442
+ existingSender._track = track;
443
+
444
+ // Update the corresponding transceiver as well
445
+ const [ existingTransceiver ] = this
446
+ .getTransceivers()
447
+ .filter(t => t.sender.id === existingSender.id);
448
+
449
+ existingTransceiver._direction = transceiver.direction;
450
+ existingTransceiver._currentDirection = transceiver.currentDirection;
451
+
452
+ return existingSender;
453
+ }
454
+
455
+ // This is a new transceiver, should create a transceiver for it and add it
456
+ const newSender = new RTCRtpSender({ ...transceiver.sender, track });
457
+ const remoteTrack = transceiver.receiver.track ? new MediaStreamTrack(transceiver.receiver.track) : null;
458
+ const newReceiver = new RTCRtpReceiver({ ...transceiver.receiver, track: remoteTrack });
459
+ const newTransceiver = new RTCRtpTransceiver({
460
+ ...transceiver,
461
+ sender: newSender,
462
+ receiver: newReceiver,
463
+ });
464
+
465
+ this._insertTransceiverSorted(transceiverOrder, newTransceiver);
466
+
467
+ return newSender;
468
+ }
469
+
470
+ addTransceiver(source: 'audio' | 'video' | MediaStreamTrack, init): RTCRtpTransceiver {
471
+ log.debug(`${this._pcId} addTransceiver`);
472
+
473
+ let src = {};
474
+
475
+ if (source === 'audio') {
476
+ src = { type: 'audio' };
477
+ } else if (source === 'video') {
478
+ src = { type: 'video' };
479
+ } else {
480
+ src = { trackId: source.id };
481
+ }
482
+
483
+ // Extract the stream ids
484
+ if (init && init.streams) {
485
+ init.streamIds = init.streams.map(stream => stream.id);
486
+ }
487
+
488
+ const result = WebRTCModule.peerConnectionAddTransceiver(this._pcId, { ...src, init: { ...init } });
489
+
490
+ if (result === null) {
491
+ throw new Error('Transceiver could not be added');
492
+ }
493
+
494
+ const t = result.transceiver;
495
+ let track: MediaStreamTrack | null = null;
496
+
497
+ if (typeof source === 'string') {
498
+ if (t.sender.track) {
499
+ track = new MediaStreamTrack(t.sender.track);
500
+ }
501
+ } else {
502
+ // 'source' is a MediaStreamTrack
503
+ track = source;
504
+ }
505
+
506
+ const sender = new RTCRtpSender({ ...t.sender, track });
507
+ const remoteTrack = t.receiver.track ? new MediaStreamTrack(t.receiver.track) : null;
508
+ const receiver = new RTCRtpReceiver({ ...t.receiver, track: remoteTrack });
509
+ const transceiver = new RTCRtpTransceiver({
510
+ ...result.transceiver,
511
+ sender,
512
+ receiver
513
+ });
514
+
515
+ this._insertTransceiverSorted(result.transceiverOrder, transceiver);
516
+
517
+ return transceiver;
518
+ }
519
+
520
+ removeTrack(sender: RTCRtpSender) {
521
+ log.debug(`${this._pcId} removeTrack`);
522
+
523
+ if (this._pcId !== sender._peerConnectionId) {
524
+ throw new Error('Sender does not belong to this peer connection');
525
+ }
526
+
527
+ if (this.connectionState === 'closed') {
528
+ throw new Error('Peer Connection is closed');
529
+ }
530
+
531
+ const existingSender = this
532
+ .getSenders()
533
+ .find(s => s === sender);
534
+
535
+ if (!existingSender) {
536
+ throw new Error('Sender does not exist');
537
+ }
538
+
539
+ if (existingSender.track === null) {
540
+ return;
541
+ }
542
+
543
+ // Blocking!
544
+ WebRTCModule.peerConnectionRemoveTrack(this._pcId, sender.id);
545
+
546
+ existingSender._track = null;
547
+
548
+ const [ existingTransceiver ] = this
549
+ .getTransceivers()
550
+ .filter(t => t.sender.id === existingSender.id);
551
+
552
+ existingTransceiver._direction = existingTransceiver.direction === 'sendrecv' ? 'recvonly' : 'inactive';
553
+ }
554
+
555
+ async getStats(selector?: MediaStreamTrack) {
556
+ log.debug(`${this._pcId} getStats`);
557
+
558
+ if (!selector) {
559
+ const data = await WebRTCModule.peerConnectionGetStats(this._pcId);
560
+
561
+ /**
562
+ * On both Android and iOS it is faster to construct a single
563
+ * JSON string representing the Map of StatsReports and have it
564
+ * pass through the React Native bridge rather than the Map of
565
+ * StatsReports. While the implementations do try to be faster in
566
+ * general, the stress is on being faster to pass through the React
567
+ * Native bridge which is a bottleneck that tends to be visible in
568
+ * the UI when there is congestion involving UI-related passing.
569
+ */
570
+ return new Map(JSON.parse(data));
571
+ } else {
572
+ const senders = this.getSenders().filter(s => s.track === selector);
573
+ const receivers = this.getReceivers().filter(r => r.track === selector);
574
+ const matches = senders.length + receivers.length;
575
+
576
+ if (matches === 0) {
577
+ throw new Error('Invalid selector: could not find matching sender / receiver');
578
+ } else if (matches > 1) {
579
+ throw new Error('Invalid selector: multiple matching senders / receivers');
580
+ } else {
581
+ const sr = senders[0] || receivers[0];
582
+
583
+ return sr.getStats();
584
+ }
585
+ }
586
+ }
587
+
588
+ getTransceivers(): RTCRtpTransceiver[] {
589
+ return this._transceivers.map(e => e.transceiver);
590
+ }
591
+
592
+ getSenders(): RTCRtpSender[] {
593
+ return this._transceivers.filter(e => !e.transceiver.stopped).map(e => e.transceiver.sender);
594
+ }
595
+
596
+ getReceivers(): RTCRtpReceiver[] {
597
+ return this._transceivers.filter(e => !e.transceiver.stopped).map(e => e.transceiver.receiver);
598
+ }
599
+
600
+ close(): void {
601
+ log.debug(`${this._pcId} close`);
602
+
603
+ if (this.connectionState === 'closed') {
604
+ return;
605
+ }
606
+
607
+ WebRTCModule.peerConnectionClose(this._pcId);
608
+
609
+ // Mark transceivers as stopped.
610
+ this._transceivers.forEach(({ transceiver })=> {
611
+ transceiver._setStopped();
612
+ });
613
+ }
614
+
615
+ restartIce(): void {
616
+ WebRTCModule.peerConnectionRestartIce(this._pcId);
617
+ }
618
+
619
+ _registerEvents(): void {
620
+ addListener(this, 'peerConnectionOnRenegotiationNeeded', (ev: any) => {
621
+ if (ev.pcId !== this._pcId) {
622
+ return;
623
+ }
624
+
625
+ console.log(`[RNWebRTC] pc:${this._pcId} negotiation_needed`, {
626
+ signalingState: this.signalingState,
627
+ connectionState: this.connectionState,
628
+ iceConnectionState: this.iceConnectionState,
629
+ });
630
+
631
+ this.dispatchEvent(new Event('negotiationneeded'));
632
+ });
633
+
634
+ addListener(this, 'peerConnectionIceConnectionChanged', (ev: any) => {
635
+ if (ev.pcId !== this._pcId) {
636
+ return;
637
+ }
638
+
639
+ const prev = this.iceConnectionState;
640
+ this.iceConnectionState = ev.iceConnectionState;
641
+ console.log(`[RNWebRTC] pc:${this._pcId} ice_connection_changed`, {
642
+ prev,
643
+ current: ev.iceConnectionState,
644
+ connectionState: this.connectionState,
645
+ signalingState: this.signalingState,
646
+ });
647
+
648
+ this.dispatchEvent(new Event('iceconnectionstatechange'));
649
+ });
650
+
651
+ addListener(this, 'peerConnectionStateChanged', (ev: any) => {
652
+ if (ev.pcId !== this._pcId) {
653
+ return;
654
+ }
655
+
656
+ const prev = this.connectionState;
657
+ this.connectionState = ev.connectionState;
658
+ console.log(`[RNWebRTC] pc:${this._pcId} connection_state_changed`, {
659
+ prev,
660
+ current: ev.connectionState,
661
+ iceConnectionState: this.iceConnectionState,
662
+ iceGatheringState: this.iceGatheringState,
663
+ signalingState: this.signalingState,
664
+ });
665
+
666
+ this.dispatchEvent(new Event('connectionstatechange'));
667
+
668
+ if (ev.connectionState === 'closed') {
669
+ console.log(`[RNWebRTC] pc:${this._pcId} disposing`);
670
+ // This PeerConnection is done, clean up.
671
+ removeListener(this);
672
+
673
+ WebRTCModule.peerConnectionDispose(this._pcId);
674
+ }
675
+ });
676
+
677
+ addListener(this, 'peerConnectionSignalingStateChanged', (ev: any) => {
678
+ if (ev.pcId !== this._pcId) {
679
+ return;
680
+ }
681
+
682
+ const prev = this.signalingState;
683
+ this.signalingState = ev.signalingState;
684
+ console.log(`[RNWebRTC] pc:${this._pcId} signaling_state_changed`, {
685
+ prev,
686
+ current: ev.signalingState,
687
+ connectionState: this.connectionState,
688
+ });
689
+
690
+ this.dispatchEvent(new Event('signalingstatechange'));
691
+ });
692
+
693
+ // Consider moving away from this event: https://github.com/WebKit/WebKit/pull/3953
694
+ addListener(this, 'peerConnectionOnTrack', (ev: any) => {
695
+ if (ev.pcId !== this._pcId) {
696
+ return;
697
+ }
698
+
699
+ console.log(`[RNWebRTC] pc:${this._pcId} track_event_queued`, {
700
+ receiverId: ev.receiver?.id,
701
+ trackKind: ev.receiver?.track?.kind,
702
+ streamCount: ev.streams?.length ?? 0,
703
+ pendingCount: this._pendingTrackEvents.length + 1,
704
+ });
705
+
706
+ log.debug(`${this._pcId} ontrack`);
707
+
708
+ // NOTE: We need to make sure the track event fires right before sRD completes,
709
+ // so we queue them up here and dispatch the events when sRD fires, but before completing it.
710
+ // In the future we should probably implement out own logic and drop this event altogether.
711
+ this._pendingTrackEvents.push(ev);
712
+ });
713
+
714
+ addListener(this, 'peerConnectionOnRemoveTrack', (ev: any) => {
715
+ if (ev.pcId !== this._pcId) {
716
+ return;
717
+ }
718
+
719
+ console.log(`[RNWebRTC] pc:${this._pcId} remove_track`, { receiverId: ev.receiverId });
720
+ log.debug(`${this._pcId} onremovetrack ${ev.receiverId}`);
721
+
722
+ const receiver = this.getReceivers().find(r => r.id === ev.receiverId);
723
+ const track = receiver?.track;
724
+
725
+ if (receiver && track) {
726
+ // As per the spec:
727
+ // - Remove the track from any media streams that were previously passed to the `track` event.
728
+ // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-removetrack,
729
+ // - Mark the track as muted:
730
+ // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
731
+ for (const stream of this._remoteStreams.values()) {
732
+ if (stream._tracks.includes(track)) {
733
+ const trackIdx = stream._tracks.indexOf(track);
734
+
735
+ log.debug(`${this._pcId} removetrack ${track.id}`);
736
+
737
+ stream._tracks.splice(trackIdx, 1);
738
+
739
+ stream.dispatchEvent(new MediaStreamTrackEvent('removetrack', { track }));
740
+
741
+ // Dispatch a mute event for the track.
742
+ track._setMutedInternal(true);
743
+ }
744
+ }
745
+ }
746
+ });
747
+
748
+ addListener(this, 'peerConnectionGotICECandidate', (ev: any) => {
749
+ if (ev.pcId !== this._pcId) {
750
+ return;
751
+ }
752
+
753
+ console.log(`[RNWebRTC] pc:${this._pcId} local_ice_candidate`, {
754
+ type: ev.candidate?.type,
755
+ protocol: ev.candidate?.protocol,
756
+ sdpMid: ev.candidate?.sdpMid,
757
+ iceGatheringState: this.iceGatheringState,
758
+ });
759
+
760
+ const sdpInfo = ev.sdp;
761
+
762
+ // Can happen when doing a rollback.
763
+ if (sdpInfo.type && sdpInfo.sdp) {
764
+ this.localDescription = new RTCSessionDescription(sdpInfo);
765
+ } else {
766
+ this.localDescription = null;
767
+ }
768
+
769
+ const candidate = new RTCIceCandidate(ev.candidate);
770
+
771
+ this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', { candidate }));
772
+ });
773
+
774
+ addListener(this, 'peerConnectionIceGatheringChanged', (ev: any) => {
775
+ if (ev.pcId !== this._pcId) {
776
+ return;
777
+ }
778
+
779
+ const prev = this.iceGatheringState;
780
+ this.iceGatheringState = ev.iceGatheringState;
781
+ console.log(`[RNWebRTC] pc:${this._pcId} ice_gathering_state_changed`, {
782
+ prev,
783
+ current: ev.iceGatheringState,
784
+ connectionState: this.connectionState,
785
+ });
786
+
787
+ if (this.iceGatheringState === 'complete') {
788
+ const sdpInfo = ev.sdp;
789
+
790
+ // Can happen when doing a rollback.
791
+ if (sdpInfo.type && sdpInfo.sdp) {
792
+ this.localDescription = new RTCSessionDescription(sdpInfo);
793
+ } else {
794
+ this.localDescription = null;
795
+ }
796
+
797
+ this.dispatchEvent(new RTCIceCandidateEvent('icecandidate', { candidate: null }));
798
+ }
799
+
800
+ this.dispatchEvent(new Event('icegatheringstatechange'));
801
+ });
802
+
803
+ addListener(this, 'peerConnectionDidOpenDataChannel', (ev: any) => {
804
+ if (ev.pcId !== this._pcId) {
805
+ return;
806
+ }
807
+
808
+ const channel = new RTCDataChannel(ev.dataChannel);
809
+
810
+ this.dispatchEvent(new RTCDataChannelEvent('datachannel', { channel }));
811
+
812
+ // Send 'open' event. Native doesn't update the state since it's already
813
+ // set at this point.
814
+ channel.dispatchEvent(new RTCDataChannelEvent('open', { channel }));
815
+ });
816
+
817
+ addListener(this, 'mediaStreamTrackMuteChanged', (ev: any) => {
818
+ if (ev.pcId !== this._pcId) {
819
+ return;
820
+ }
821
+
822
+ const [
823
+ track
824
+ ] = this.getReceivers().map(r => r.track).filter(t => t?.id === ev.trackId);
825
+
826
+ if (track) {
827
+ track._setMutedInternal(ev.muted);
828
+ }
829
+ });
830
+ }
831
+
832
+ /**
833
+ * Creates a new RTCDataChannel object with the given label. The
834
+ * RTCDataChannelInit dictionary can be used to configure properties of the
835
+ * underlying channel such as data reliability.
836
+ *
837
+ * @param {string} label - the value with which the label attribute of the new
838
+ * instance is to be initialized
839
+ * @param {RTCDataChannelInit} dataChannelDict - an optional dictionary of
840
+ * values with which to initialize corresponding attributes of the new
841
+ * instance such as id
842
+ */
843
+ createDataChannel(label: string, dataChannelDict?: RTCDataChannelInit): RTCDataChannel {
844
+ if (arguments.length === 0) {
845
+ throw new TypeError('1 argument required, but 0 present');
846
+ }
847
+
848
+ if (dataChannelDict && 'id' in dataChannelDict) {
849
+ const id = dataChannelDict.id;
850
+
851
+ if (typeof id !== 'number') {
852
+ throw new TypeError('DataChannel id must be a number: ' + id);
853
+ }
854
+ }
855
+
856
+ const channelInfo = WebRTCModule.createDataChannel(this._pcId, String(label), dataChannelDict);
857
+
858
+ if (channelInfo === null) {
859
+ throw new TypeError('Failed to create new DataChannel');
860
+ }
861
+
862
+ return new RTCDataChannel(channelInfo);
863
+ }
864
+
865
+ /**
866
+ * Check whether a media stream track exists already in a sender.
867
+ * See https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtrack for more information
868
+ */
869
+ _trackExists(track: MediaStreamTrack): boolean {
870
+ const [ sender ] = this
871
+ .getSenders()
872
+ .filter(
873
+ sender => sender.track?.id === track.id
874
+ );
875
+
876
+ return sender? true : false;
877
+ }
878
+
879
+ /**
880
+ * Updates transceivers after offer/answer updates if necessary.
881
+ */
882
+ _updateTransceivers(transceiverUpdates, removeStopped = false) {
883
+ for (const update of transceiverUpdates) {
884
+ const [ transceiver ] = this
885
+ .getTransceivers()
886
+ .filter(t => t.sender.id === update.transceiverId);
887
+
888
+ if (!transceiver) {
889
+ continue;
890
+ }
891
+
892
+ if (update.currentDirection) {
893
+ transceiver._currentDirection = update.currentDirection;
894
+ }
895
+
896
+ transceiver._mid = update.mid;
897
+ transceiver._stopped = Boolean(update.isStopped);
898
+ transceiver._sender._rtpParameters = new RTCRtpSendParameters(update.senderRtpParameters);
899
+ transceiver._receiver._rtpParameters = new RTCRtpReceiveParameters(update.receiverRtpParameters);
900
+ }
901
+
902
+ if (removeStopped) {
903
+ const stopped = this.getTransceivers().filter(t => t.stopped);
904
+ const newTransceivers = this._transceivers.filter(t => !stopped.includes(t.transceiver));
905
+
906
+ this._transceivers = newTransceivers;
907
+ }
908
+ }
909
+
910
+ /**
911
+ * Inserts transceiver into the transceiver array in the order they are created (timestamp).
912
+ * @param order an index that refers to when it it was created relatively.
913
+ * @param transceiver the transceiver object to be inserted.
914
+ */
915
+ _insertTransceiverSorted(order: number, transceiver: RTCRtpTransceiver) {
916
+ this._transceivers.push({ order, transceiver });
917
+ this._transceivers.sort((a, b) => a.order - b.order);
918
+ }
919
+ }
920
+
921
+ /**
922
+ * Define the `onxxx` event handlers.
923
+ */
924
+ const proto = RTCPeerConnection.prototype;
925
+
926
+ defineEventAttribute(proto, 'connectionstatechange');
927
+ defineEventAttribute(proto, 'icecandidate');
928
+ defineEventAttribute(proto, 'icecandidateerror');
929
+ defineEventAttribute(proto, 'iceconnectionstatechange');
930
+ defineEventAttribute(proto, 'icegatheringstatechange');
931
+ defineEventAttribute(proto, 'negotiationneeded');
932
+ defineEventAttribute(proto, 'signalingstatechange');
933
+ defineEventAttribute(proto, 'datachannel');
934
+ defineEventAttribute(proto, 'track');
935
+ defineEventAttribute(proto, 'error');