@dtelecom/livekit-client 1.12.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 (279) hide show
  1. package/LICENSE +175 -0
  2. package/README.md +315 -0
  3. package/dist/@dtelecom/livekit-client.e2ee.worker.js +2 -0
  4. package/dist/@dtelecom/livekit-client.e2ee.worker.js.map +1 -0
  5. package/dist/@dtelecom/livekit-client.e2ee.worker.mjs +1633 -0
  6. package/dist/@dtelecom/livekit-client.e2ee.worker.mjs.map +1 -0
  7. package/dist/@dtelecom/livekit-client.esm.mjs +22545 -0
  8. package/dist/@dtelecom/livekit-client.esm.mjs.map +1 -0
  9. package/dist/@dtelecom/livekit-client.umd.js +2 -0
  10. package/dist/@dtelecom/livekit-client.umd.js.map +1 -0
  11. package/dist/@dtelecom/src/api/SignalClient.d.ts +102 -0
  12. package/dist/@dtelecom/src/api/SignalClient.d.ts.map +1 -0
  13. package/dist/@dtelecom/src/connectionHelper/ConnectionCheck.d.ts +26 -0
  14. package/dist/@dtelecom/src/connectionHelper/ConnectionCheck.d.ts.map +1 -0
  15. package/dist/@dtelecom/src/connectionHelper/checks/Checker.d.ts +59 -0
  16. package/dist/@dtelecom/src/connectionHelper/checks/Checker.d.ts.map +1 -0
  17. package/dist/@dtelecom/src/connectionHelper/checks/publishAudio.d.ts +6 -0
  18. package/dist/@dtelecom/src/connectionHelper/checks/publishAudio.d.ts.map +1 -0
  19. package/dist/@dtelecom/src/connectionHelper/checks/publishVideo.d.ts +6 -0
  20. package/dist/@dtelecom/src/connectionHelper/checks/publishVideo.d.ts.map +1 -0
  21. package/dist/@dtelecom/src/connectionHelper/checks/reconnect.d.ts +6 -0
  22. package/dist/@dtelecom/src/connectionHelper/checks/reconnect.d.ts.map +1 -0
  23. package/dist/@dtelecom/src/connectionHelper/checks/turn.d.ts +6 -0
  24. package/dist/@dtelecom/src/connectionHelper/checks/turn.d.ts.map +1 -0
  25. package/dist/@dtelecom/src/connectionHelper/checks/webrtc.d.ts +6 -0
  26. package/dist/@dtelecom/src/connectionHelper/checks/webrtc.d.ts.map +1 -0
  27. package/dist/@dtelecom/src/connectionHelper/checks/websocket.d.ts +6 -0
  28. package/dist/@dtelecom/src/connectionHelper/checks/websocket.d.ts.map +1 -0
  29. package/dist/@dtelecom/src/e2ee/E2eeManager.d.ts +47 -0
  30. package/dist/@dtelecom/src/e2ee/E2eeManager.d.ts.map +1 -0
  31. package/dist/@dtelecom/src/e2ee/KeyProvider.d.ts +44 -0
  32. package/dist/@dtelecom/src/e2ee/KeyProvider.d.ts.map +1 -0
  33. package/dist/@dtelecom/src/e2ee/constants.d.ts +15 -0
  34. package/dist/@dtelecom/src/e2ee/constants.d.ts.map +1 -0
  35. package/dist/@dtelecom/src/e2ee/errors.d.ts +11 -0
  36. package/dist/@dtelecom/src/e2ee/errors.d.ts.map +1 -0
  37. package/dist/@dtelecom/src/e2ee/index.d.ts +4 -0
  38. package/dist/@dtelecom/src/e2ee/index.d.ts.map +1 -0
  39. package/dist/@dtelecom/src/e2ee/types.d.ts +130 -0
  40. package/dist/@dtelecom/src/e2ee/types.d.ts.map +1 -0
  41. package/dist/@dtelecom/src/e2ee/utils.d.ts +24 -0
  42. package/dist/@dtelecom/src/e2ee/utils.d.ts.map +1 -0
  43. package/dist/@dtelecom/src/e2ee/worker/FrameCryptor.d.ts +176 -0
  44. package/dist/@dtelecom/src/e2ee/worker/FrameCryptor.d.ts.map +1 -0
  45. package/dist/@dtelecom/src/e2ee/worker/ParticipantKeyHandler.d.ts +65 -0
  46. package/dist/@dtelecom/src/e2ee/worker/ParticipantKeyHandler.d.ts.map +1 -0
  47. package/dist/@dtelecom/src/e2ee/worker/e2ee.worker.d.ts +2 -0
  48. package/dist/@dtelecom/src/e2ee/worker/e2ee.worker.d.ts.map +1 -0
  49. package/dist/@dtelecom/src/index.d.ts +38 -0
  50. package/dist/@dtelecom/src/index.d.ts.map +1 -0
  51. package/dist/@dtelecom/src/logger.d.ts +29 -0
  52. package/dist/@dtelecom/src/logger.d.ts.map +1 -0
  53. package/dist/@dtelecom/src/options.d.ts +104 -0
  54. package/dist/@dtelecom/src/options.d.ts.map +1 -0
  55. package/dist/@dtelecom/src/proto/livekit_models_pb.d.ts +1264 -0
  56. package/dist/@dtelecom/src/proto/livekit_models_pb.d.ts.map +1 -0
  57. package/dist/@dtelecom/src/proto/livekit_rtc_pb.d.ts +1373 -0
  58. package/dist/@dtelecom/src/proto/livekit_rtc_pb.d.ts.map +1 -0
  59. package/dist/@dtelecom/src/room/DefaultReconnectPolicy.d.ts +8 -0
  60. package/dist/@dtelecom/src/room/DefaultReconnectPolicy.d.ts.map +1 -0
  61. package/dist/@dtelecom/src/room/DeviceManager.d.ts +9 -0
  62. package/dist/@dtelecom/src/room/DeviceManager.d.ts.map +1 -0
  63. package/dist/@dtelecom/src/room/PCTransport.d.ts +41 -0
  64. package/dist/@dtelecom/src/room/PCTransport.d.ts.map +1 -0
  65. package/dist/@dtelecom/src/room/RTCEngine.d.ts +143 -0
  66. package/dist/@dtelecom/src/room/RTCEngine.d.ts.map +1 -0
  67. package/dist/@dtelecom/src/room/ReconnectPolicy.d.ts +24 -0
  68. package/dist/@dtelecom/src/room/ReconnectPolicy.d.ts.map +1 -0
  69. package/dist/@dtelecom/src/room/RegionUrlProvider.d.ts +17 -0
  70. package/dist/@dtelecom/src/room/RegionUrlProvider.d.ts.map +1 -0
  71. package/dist/@dtelecom/src/room/Room.d.ts +248 -0
  72. package/dist/@dtelecom/src/room/Room.d.ts.map +1 -0
  73. package/dist/@dtelecom/src/room/defaults.d.ts +8 -0
  74. package/dist/@dtelecom/src/room/defaults.d.ts.map +1 -0
  75. package/dist/@dtelecom/src/room/errors.d.ts +43 -0
  76. package/dist/@dtelecom/src/room/errors.d.ts.map +1 -0
  77. package/dist/@dtelecom/src/room/events.d.ts +477 -0
  78. package/dist/@dtelecom/src/room/events.d.ts.map +1 -0
  79. package/dist/@dtelecom/src/room/participant/LocalParticipant.d.ts +177 -0
  80. package/dist/@dtelecom/src/room/participant/LocalParticipant.d.ts.map +1 -0
  81. package/dist/@dtelecom/src/room/participant/Participant.d.ts +95 -0
  82. package/dist/@dtelecom/src/room/participant/Participant.d.ts.map +1 -0
  83. package/dist/@dtelecom/src/room/participant/ParticipantTrackPermission.d.ts +26 -0
  84. package/dist/@dtelecom/src/room/participant/ParticipantTrackPermission.d.ts.map +1 -0
  85. package/dist/@dtelecom/src/room/participant/RemoteParticipant.d.ts +55 -0
  86. package/dist/@dtelecom/src/room/participant/RemoteParticipant.d.ts.map +1 -0
  87. package/dist/@dtelecom/src/room/participant/publishUtils.d.ts +28 -0
  88. package/dist/@dtelecom/src/room/participant/publishUtils.d.ts.map +1 -0
  89. package/dist/@dtelecom/src/room/stats.d.ts +67 -0
  90. package/dist/@dtelecom/src/room/stats.d.ts.map +1 -0
  91. package/dist/@dtelecom/src/room/timers.d.ts +13 -0
  92. package/dist/@dtelecom/src/room/timers.d.ts.map +1 -0
  93. package/dist/@dtelecom/src/room/track/LocalAudioTrack.d.ts +25 -0
  94. package/dist/@dtelecom/src/room/track/LocalAudioTrack.d.ts.map +1 -0
  95. package/dist/@dtelecom/src/room/track/LocalTrack.d.ts +78 -0
  96. package/dist/@dtelecom/src/room/track/LocalTrack.d.ts.map +1 -0
  97. package/dist/@dtelecom/src/room/track/LocalTrackPublication.d.ts +38 -0
  98. package/dist/@dtelecom/src/room/track/LocalTrackPublication.d.ts.map +1 -0
  99. package/dist/@dtelecom/src/room/track/LocalVideoTrack.d.ts +54 -0
  100. package/dist/@dtelecom/src/room/track/LocalVideoTrack.d.ts.map +1 -0
  101. package/dist/@dtelecom/src/room/track/RemoteAudioTrack.d.ts +53 -0
  102. package/dist/@dtelecom/src/room/track/RemoteAudioTrack.d.ts.map +1 -0
  103. package/dist/@dtelecom/src/room/track/RemoteTrack.d.ts +15 -0
  104. package/dist/@dtelecom/src/room/track/RemoteTrack.d.ts.map +1 -0
  105. package/dist/@dtelecom/src/room/track/RemoteTrackPublication.d.ts +64 -0
  106. package/dist/@dtelecom/src/room/track/RemoteTrackPublication.d.ts.map +1 -0
  107. package/dist/@dtelecom/src/room/track/RemoteVideoTrack.d.ts +56 -0
  108. package/dist/@dtelecom/src/room/track/RemoteVideoTrack.d.ts.map +1 -0
  109. package/dist/@dtelecom/src/room/track/Track.d.ts +125 -0
  110. package/dist/@dtelecom/src/room/track/Track.d.ts.map +1 -0
  111. package/dist/@dtelecom/src/room/track/TrackPublication.d.ts +72 -0
  112. package/dist/@dtelecom/src/room/track/TrackPublication.d.ts.map +1 -0
  113. package/dist/@dtelecom/src/room/track/create.d.ts +24 -0
  114. package/dist/@dtelecom/src/room/track/create.d.ts.map +1 -0
  115. package/dist/@dtelecom/src/room/track/facingMode.d.ts +41 -0
  116. package/dist/@dtelecom/src/room/track/facingMode.d.ts.map +1 -0
  117. package/dist/@dtelecom/src/room/track/options.d.ts +272 -0
  118. package/dist/@dtelecom/src/room/track/options.d.ts.map +1 -0
  119. package/dist/@dtelecom/src/room/track/processor/types.d.ts +19 -0
  120. package/dist/@dtelecom/src/room/track/processor/types.d.ts.map +1 -0
  121. package/dist/@dtelecom/src/room/track/types.d.ts +24 -0
  122. package/dist/@dtelecom/src/room/track/types.d.ts.map +1 -0
  123. package/dist/@dtelecom/src/room/track/utils.d.ts +27 -0
  124. package/dist/@dtelecom/src/room/track/utils.d.ts.map +1 -0
  125. package/dist/@dtelecom/src/room/types.d.ts +26 -0
  126. package/dist/@dtelecom/src/room/types.d.ts.map +1 -0
  127. package/dist/@dtelecom/src/room/utils.d.ts +92 -0
  128. package/dist/@dtelecom/src/room/utils.d.ts.map +1 -0
  129. package/dist/@dtelecom/src/test/MockMediaStreamTrack.d.ts +26 -0
  130. package/dist/@dtelecom/src/test/MockMediaStreamTrack.d.ts.map +1 -0
  131. package/dist/@dtelecom/src/test/mocks.d.ts +12 -0
  132. package/dist/@dtelecom/src/test/mocks.d.ts.map +1 -0
  133. package/dist/@dtelecom/src/utils/AsyncQueue.d.ts +23 -0
  134. package/dist/@dtelecom/src/utils/AsyncQueue.d.ts.map +1 -0
  135. package/dist/@dtelecom/src/utils/browserParser.d.ts +12 -0
  136. package/dist/@dtelecom/src/utils/browserParser.d.ts.map +1 -0
  137. package/dist/@dtelecom/src/version.d.ts +3 -0
  138. package/dist/@dtelecom/src/version.d.ts.map +1 -0
  139. package/dist/ts4.2/@dtelecom/src/api/SignalClient.d.ts +105 -0
  140. package/dist/ts4.2/@dtelecom/src/connectionHelper/ConnectionCheck.d.ts +26 -0
  141. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/Checker.d.ts +59 -0
  142. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/publishAudio.d.ts +6 -0
  143. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/publishVideo.d.ts +6 -0
  144. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/reconnect.d.ts +6 -0
  145. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/turn.d.ts +6 -0
  146. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/webrtc.d.ts +6 -0
  147. package/dist/ts4.2/@dtelecom/src/connectionHelper/checks/websocket.d.ts +6 -0
  148. package/dist/ts4.2/@dtelecom/src/e2ee/E2eeManager.d.ts +47 -0
  149. package/dist/ts4.2/@dtelecom/src/e2ee/KeyProvider.d.ts +44 -0
  150. package/dist/ts4.2/@dtelecom/src/e2ee/constants.d.ts +15 -0
  151. package/dist/ts4.2/@dtelecom/src/e2ee/errors.d.ts +11 -0
  152. package/dist/ts4.2/@dtelecom/src/e2ee/index.d.ts +4 -0
  153. package/dist/ts4.2/@dtelecom/src/e2ee/types.d.ts +130 -0
  154. package/dist/ts4.2/@dtelecom/src/e2ee/utils.d.ts +24 -0
  155. package/dist/ts4.2/@dtelecom/src/e2ee/worker/FrameCryptor.d.ts +176 -0
  156. package/dist/ts4.2/@dtelecom/src/e2ee/worker/ParticipantKeyHandler.d.ts +65 -0
  157. package/dist/ts4.2/@dtelecom/src/e2ee/worker/e2ee.worker.d.ts +2 -0
  158. package/dist/ts4.2/@dtelecom/src/index.d.ts +38 -0
  159. package/dist/ts4.2/@dtelecom/src/logger.d.ts +29 -0
  160. package/dist/ts4.2/@dtelecom/src/options.d.ts +104 -0
  161. package/dist/ts4.2/@dtelecom/src/proto/livekit_models_pb.d.ts +1264 -0
  162. package/dist/ts4.2/@dtelecom/src/proto/livekit_rtc_pb.d.ts +1373 -0
  163. package/dist/ts4.2/@dtelecom/src/room/DefaultReconnectPolicy.d.ts +8 -0
  164. package/dist/ts4.2/@dtelecom/src/room/DeviceManager.d.ts +9 -0
  165. package/dist/ts4.2/@dtelecom/src/room/PCTransport.d.ts +43 -0
  166. package/dist/ts4.2/@dtelecom/src/room/RTCEngine.d.ts +143 -0
  167. package/dist/ts4.2/@dtelecom/src/room/ReconnectPolicy.d.ts +24 -0
  168. package/dist/ts4.2/@dtelecom/src/room/RegionUrlProvider.d.ts +17 -0
  169. package/dist/ts4.2/@dtelecom/src/room/Room.d.ts +248 -0
  170. package/dist/ts4.2/@dtelecom/src/room/defaults.d.ts +8 -0
  171. package/dist/ts4.2/@dtelecom/src/room/errors.d.ts +43 -0
  172. package/dist/ts4.2/@dtelecom/src/room/events.d.ts +477 -0
  173. package/dist/ts4.2/@dtelecom/src/room/participant/LocalParticipant.d.ts +177 -0
  174. package/dist/ts4.2/@dtelecom/src/room/participant/Participant.d.ts +95 -0
  175. package/dist/ts4.2/@dtelecom/src/room/participant/ParticipantTrackPermission.d.ts +26 -0
  176. package/dist/ts4.2/@dtelecom/src/room/participant/RemoteParticipant.d.ts +55 -0
  177. package/dist/ts4.2/@dtelecom/src/room/participant/publishUtils.d.ts +28 -0
  178. package/dist/ts4.2/@dtelecom/src/room/stats.d.ts +67 -0
  179. package/dist/ts4.2/@dtelecom/src/room/timers.d.ts +13 -0
  180. package/dist/ts4.2/@dtelecom/src/room/track/LocalAudioTrack.d.ts +25 -0
  181. package/dist/ts4.2/@dtelecom/src/room/track/LocalTrack.d.ts +78 -0
  182. package/dist/ts4.2/@dtelecom/src/room/track/LocalTrackPublication.d.ts +38 -0
  183. package/dist/ts4.2/@dtelecom/src/room/track/LocalVideoTrack.d.ts +54 -0
  184. package/dist/ts4.2/@dtelecom/src/room/track/RemoteAudioTrack.d.ts +53 -0
  185. package/dist/ts4.2/@dtelecom/src/room/track/RemoteTrack.d.ts +15 -0
  186. package/dist/ts4.2/@dtelecom/src/room/track/RemoteTrackPublication.d.ts +64 -0
  187. package/dist/ts4.2/@dtelecom/src/room/track/RemoteVideoTrack.d.ts +56 -0
  188. package/dist/ts4.2/@dtelecom/src/room/track/Track.d.ts +125 -0
  189. package/dist/ts4.2/@dtelecom/src/room/track/TrackPublication.d.ts +72 -0
  190. package/dist/ts4.2/@dtelecom/src/room/track/create.d.ts +24 -0
  191. package/dist/ts4.2/@dtelecom/src/room/track/facingMode.d.ts +41 -0
  192. package/dist/ts4.2/@dtelecom/src/room/track/options.d.ts +280 -0
  193. package/dist/ts4.2/@dtelecom/src/room/track/processor/types.d.ts +19 -0
  194. package/dist/ts4.2/@dtelecom/src/room/track/types.d.ts +24 -0
  195. package/dist/ts4.2/@dtelecom/src/room/track/utils.d.ts +27 -0
  196. package/dist/ts4.2/@dtelecom/src/room/types.d.ts +26 -0
  197. package/dist/ts4.2/@dtelecom/src/room/utils.d.ts +92 -0
  198. package/dist/ts4.2/@dtelecom/src/test/MockMediaStreamTrack.d.ts +26 -0
  199. package/dist/ts4.2/@dtelecom/src/test/mocks.d.ts +12 -0
  200. package/dist/ts4.2/@dtelecom/src/utils/AsyncQueue.d.ts +23 -0
  201. package/dist/ts4.2/@dtelecom/src/utils/browserParser.d.ts +12 -0
  202. package/dist/ts4.2/@dtelecom/src/version.d.ts +3 -0
  203. package/package.json +102 -0
  204. package/src/api/SignalClient.ts +790 -0
  205. package/src/connectionHelper/ConnectionCheck.ts +90 -0
  206. package/src/connectionHelper/checks/Checker.ts +163 -0
  207. package/src/connectionHelper/checks/publishAudio.ts +33 -0
  208. package/src/connectionHelper/checks/publishVideo.ts +33 -0
  209. package/src/connectionHelper/checks/reconnect.ts +45 -0
  210. package/src/connectionHelper/checks/turn.ts +54 -0
  211. package/src/connectionHelper/checks/webrtc.ts +82 -0
  212. package/src/connectionHelper/checks/websocket.ts +27 -0
  213. package/src/e2ee/E2eeManager.ts +375 -0
  214. package/src/e2ee/KeyProvider.ts +78 -0
  215. package/src/e2ee/constants.ts +44 -0
  216. package/src/e2ee/errors.ts +16 -0
  217. package/src/e2ee/index.ts +3 -0
  218. package/src/e2ee/types.ts +161 -0
  219. package/src/e2ee/utils.ts +127 -0
  220. package/src/e2ee/worker/FrameCryptor.test.ts +19 -0
  221. package/src/e2ee/worker/FrameCryptor.ts +612 -0
  222. package/src/e2ee/worker/ParticipantKeyHandler.ts +175 -0
  223. package/src/e2ee/worker/e2ee.worker.ts +223 -0
  224. package/src/e2ee/worker/tsconfig.json +6 -0
  225. package/src/index.ts +85 -0
  226. package/src/logger.ts +64 -0
  227. package/src/options.ts +127 -0
  228. package/src/proto/livekit_models_pb.ts +2096 -0
  229. package/src/proto/livekit_rtc_pb.ts +2332 -0
  230. package/src/room/DefaultReconnectPolicy.ts +35 -0
  231. package/src/room/DeviceManager.ts +88 -0
  232. package/src/room/PCTransport.ts +452 -0
  233. package/src/room/RTCEngine.ts +1421 -0
  234. package/src/room/ReconnectPolicy.ts +27 -0
  235. package/src/room/RegionUrlProvider.ts +82 -0
  236. package/src/room/Room.test.ts +30 -0
  237. package/src/room/Room.ts +1907 -0
  238. package/src/room/defaults.ts +49 -0
  239. package/src/room/errors.ts +90 -0
  240. package/src/room/events.ts +535 -0
  241. package/src/room/participant/LocalParticipant.ts +1350 -0
  242. package/src/room/participant/Participant.ts +299 -0
  243. package/src/room/participant/ParticipantTrackPermission.ts +42 -0
  244. package/src/room/participant/RemoteParticipant.ts +359 -0
  245. package/src/room/participant/publishUtils.test.ts +189 -0
  246. package/src/room/participant/publishUtils.ts +399 -0
  247. package/src/room/stats.ts +136 -0
  248. package/src/room/timers.ts +16 -0
  249. package/src/room/track/LocalAudioTrack.ts +171 -0
  250. package/src/room/track/LocalTrack.ts +430 -0
  251. package/src/room/track/LocalTrackPublication.ts +87 -0
  252. package/src/room/track/LocalVideoTrack.test.ts +73 -0
  253. package/src/room/track/LocalVideoTrack.ts +513 -0
  254. package/src/room/track/RemoteAudioTrack.ts +248 -0
  255. package/src/room/track/RemoteTrack.ts +62 -0
  256. package/src/room/track/RemoteTrackPublication.ts +311 -0
  257. package/src/room/track/RemoteVideoTrack.test.ts +152 -0
  258. package/src/room/track/RemoteVideoTrack.ts +410 -0
  259. package/src/room/track/Track.ts +452 -0
  260. package/src/room/track/TrackPublication.ts +158 -0
  261. package/src/room/track/create.ts +143 -0
  262. package/src/room/track/facingMode.test.ts +31 -0
  263. package/src/room/track/facingMode.ts +103 -0
  264. package/src/room/track/options.ts +362 -0
  265. package/src/room/track/processor/types.ts +20 -0
  266. package/src/room/track/types.ts +25 -0
  267. package/src/room/track/utils.test.ts +111 -0
  268. package/src/room/track/utils.ts +183 -0
  269. package/src/room/types.ts +39 -0
  270. package/src/room/utils.test.ts +16 -0
  271. package/src/room/utils.ts +501 -0
  272. package/src/room/worker.d.ts +4 -0
  273. package/src/test/MockMediaStreamTrack.ts +84 -0
  274. package/src/test/mocks.ts +19 -0
  275. package/src/utils/AsyncQueue.test.ts +100 -0
  276. package/src/utils/AsyncQueue.ts +57 -0
  277. package/src/utils/browserParser.test.ts +88 -0
  278. package/src/utils/browserParser.ts +74 -0
  279. package/src/version.ts +4 -0
@@ -0,0 +1,1907 @@
1
+ import { protoInt64 } from '@bufbuild/protobuf';
2
+ import { EventEmitter } from 'events';
3
+ import type TypedEmitter from 'typed-emitter';
4
+ import 'webrtc-adapter';
5
+ import { toProtoSessionDescription } from '../api/SignalClient';
6
+ import { EncryptionEvent } from '../e2ee';
7
+ import { E2EEManager } from '../e2ee/E2eeManager';
8
+ import log from '../logger';
9
+ import type {
10
+ InternalRoomConnectOptions,
11
+ InternalRoomOptions,
12
+ RoomConnectOptions,
13
+ RoomOptions,
14
+ } from '../options';
15
+ import {
16
+ DataPacket_Kind,
17
+ DisconnectReason,
18
+ ParticipantInfo,
19
+ ParticipantInfo_State,
20
+ ParticipantPermission,
21
+ Room as RoomModel,
22
+ ServerInfo,
23
+ SpeakerInfo,
24
+ SubscriptionError,
25
+ TrackInfo,
26
+ TrackSource,
27
+ TrackType,
28
+ UserPacket,
29
+ } from '../proto/livekit_models_pb';
30
+ import {
31
+ ConnectionQualityUpdate,
32
+ JoinResponse,
33
+ LeaveRequest,
34
+ SimulateScenario,
35
+ StreamStateUpdate,
36
+ SubscriptionPermissionUpdate,
37
+ SubscriptionResponse,
38
+ SyncState,
39
+ UpdateSubscription,
40
+ } from '../proto/livekit_rtc_pb';
41
+ import { getBrowser } from '../utils/browserParser';
42
+ import DeviceManager from './DeviceManager';
43
+ import RTCEngine from './RTCEngine';
44
+ import { RegionUrlProvider } from './RegionUrlProvider';
45
+ import {
46
+ audioDefaults,
47
+ publishDefaults,
48
+ roomConnectOptionDefaults,
49
+ roomOptionDefaults,
50
+ videoDefaults,
51
+ } from './defaults';
52
+ import { ConnectionError, ConnectionErrorReason, UnsupportedServer } from './errors';
53
+ import { EngineEvent, ParticipantEvent, RoomEvent, TrackEvent } from './events';
54
+ import LocalParticipant from './participant/LocalParticipant';
55
+ import type Participant from './participant/Participant';
56
+ import type { ConnectionQuality } from './participant/Participant';
57
+ import RemoteParticipant from './participant/RemoteParticipant';
58
+ import CriticalTimers from './timers';
59
+ import LocalAudioTrack from './track/LocalAudioTrack';
60
+ import LocalTrackPublication from './track/LocalTrackPublication';
61
+ import LocalVideoTrack from './track/LocalVideoTrack';
62
+ import type RemoteTrack from './track/RemoteTrack';
63
+ import RemoteTrackPublication from './track/RemoteTrackPublication';
64
+ import { Track } from './track/Track';
65
+ import type { TrackPublication } from './track/TrackPublication';
66
+ import type { AdaptiveStreamSettings } from './track/types';
67
+ import { getNewAudioContext, sourceToKind } from './track/utils';
68
+ import type { SimulationOptions, SimulationScenario } from './types';
69
+ import {
70
+ Future,
71
+ Mutex,
72
+ createDummyVideoStreamTrack,
73
+ getEmptyAudioStreamTrack,
74
+ isCloud,
75
+ isWeb,
76
+ supportsSetSinkId,
77
+ toHttpUrl,
78
+ unpackStreamId,
79
+ unwrapConstraint,
80
+ } from './utils';
81
+
82
+ export enum ConnectionState {
83
+ Disconnected = 'disconnected',
84
+ Connecting = 'connecting',
85
+ Connected = 'connected',
86
+ Reconnecting = 'reconnecting',
87
+ }
88
+
89
+ const connectionReconcileFrequency = 2 * 1000;
90
+
91
+ /** @deprecated RoomState has been renamed to [[ConnectionState]] */
92
+ export const RoomState = ConnectionState;
93
+
94
+ /**
95
+ * In LiveKit, a room is the logical grouping for a list of participants.
96
+ * Participants in a room can publish tracks, and subscribe to others' tracks.
97
+ *
98
+ * a Room fires [[RoomEvent | RoomEvents]].
99
+ *
100
+ * @noInheritDoc
101
+ */
102
+ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>) {
103
+ state: ConnectionState = ConnectionState.Disconnected;
104
+
105
+ /** map of sid: [[RemoteParticipant]] */
106
+ participants: Map<string, RemoteParticipant>;
107
+
108
+ /**
109
+ * list of participants that are actively speaking. when this changes
110
+ * a [[RoomEvent.ActiveSpeakersChanged]] event is fired
111
+ */
112
+ activeSpeakers: Participant[] = [];
113
+
114
+ /** @internal */
115
+ engine!: RTCEngine;
116
+
117
+ /** the current participant */
118
+ localParticipant: LocalParticipant;
119
+
120
+ /** options of room */
121
+ options: InternalRoomOptions;
122
+
123
+ /** reflects the sender encryption status of the local participant */
124
+ isE2EEEnabled: boolean = false;
125
+
126
+ private roomInfo?: RoomModel;
127
+
128
+ private identityToSid: Map<string, string>;
129
+
130
+ /** connect options of room */
131
+ private connOptions?: InternalRoomConnectOptions;
132
+
133
+ private audioEnabled = true;
134
+
135
+ private audioContext?: AudioContext;
136
+
137
+ /** used for aborting pending connections to a LiveKit server */
138
+ private abortController?: AbortController;
139
+
140
+ /** future holding client initiated connection attempt */
141
+ private connectFuture?: Future<void>;
142
+
143
+ private disconnectLock: Mutex;
144
+
145
+ private e2eeManager: E2EEManager | undefined;
146
+
147
+ private cachedParticipantSids: Array<string>;
148
+
149
+ private connectionReconcileInterval?: ReturnType<typeof setInterval>;
150
+
151
+ private regionUrlProvider?: RegionUrlProvider;
152
+
153
+ private regionUrl?: string;
154
+
155
+ /**
156
+ * Creates a new Room, the primary construct for a LiveKit session.
157
+ * @param options
158
+ */
159
+ constructor(options?: RoomOptions) {
160
+ super();
161
+ this.setMaxListeners(100);
162
+ this.participants = new Map();
163
+ this.cachedParticipantSids = [];
164
+ this.identityToSid = new Map();
165
+ this.options = { ...roomOptionDefaults, ...options };
166
+
167
+ this.options.audioCaptureDefaults = {
168
+ ...audioDefaults,
169
+ ...options?.audioCaptureDefaults,
170
+ };
171
+ this.options.videoCaptureDefaults = {
172
+ ...videoDefaults,
173
+ ...options?.videoCaptureDefaults,
174
+ };
175
+ this.options.publishDefaults = {
176
+ ...publishDefaults,
177
+ ...options?.publishDefaults,
178
+ };
179
+
180
+ this.maybeCreateEngine();
181
+
182
+ this.disconnectLock = new Mutex();
183
+
184
+ this.localParticipant = new LocalParticipant('', '', this.engine, this.options);
185
+
186
+ if (this.options.videoCaptureDefaults.deviceId) {
187
+ this.localParticipant.activeDeviceMap.set(
188
+ 'videoinput',
189
+ unwrapConstraint(this.options.videoCaptureDefaults.deviceId),
190
+ );
191
+ }
192
+ if (this.options.audioCaptureDefaults.deviceId) {
193
+ this.localParticipant.activeDeviceMap.set(
194
+ 'audioinput',
195
+ unwrapConstraint(this.options.audioCaptureDefaults.deviceId),
196
+ );
197
+ }
198
+ if (this.options.audioOutput?.deviceId) {
199
+ this.switchActiveDevice('audiooutput', unwrapConstraint(this.options.audioOutput.deviceId));
200
+ }
201
+
202
+ if (this.options.e2ee) {
203
+ this.setupE2EE();
204
+ }
205
+ }
206
+
207
+ /**
208
+ * @experimental
209
+ */
210
+ async setE2EEEnabled(enabled: boolean) {
211
+ if (this.e2eeManager) {
212
+ await Promise.all([
213
+ this.localParticipant.setE2EEEnabled(enabled),
214
+ this.e2eeManager.setParticipantCryptorEnabled(enabled),
215
+ ]);
216
+ } else {
217
+ throw Error('e2ee not configured, please set e2ee settings within the room options');
218
+ }
219
+ }
220
+
221
+ private setupE2EE() {
222
+ if (this.options.e2ee) {
223
+ this.e2eeManager = new E2EEManager(this.options.e2ee);
224
+ this.e2eeManager.on(
225
+ EncryptionEvent.ParticipantEncryptionStatusChanged,
226
+ (enabled, participant) => {
227
+ if (participant instanceof LocalParticipant) {
228
+ this.isE2EEEnabled = enabled;
229
+ }
230
+ this.emit(RoomEvent.ParticipantEncryptionStatusChanged, enabled, participant);
231
+ },
232
+ );
233
+ this.e2eeManager.on(EncryptionEvent.Error, (error) =>
234
+ this.emit(RoomEvent.EncryptionError, error),
235
+ );
236
+ this.e2eeManager?.setup(this);
237
+ }
238
+ }
239
+
240
+ /**
241
+ * if the current room has a participant with `recorder: true` in its JWT grant
242
+ **/
243
+ get isRecording(): boolean {
244
+ return this.roomInfo?.activeRecording ?? false;
245
+ }
246
+
247
+ /** server assigned unique room id */
248
+ get sid(): string {
249
+ return this.roomInfo?.sid ?? '';
250
+ }
251
+
252
+ /** user assigned name, derived from JWT token */
253
+ get name(): string {
254
+ return this.roomInfo?.name ?? '';
255
+ }
256
+
257
+ /** room metadata */
258
+ get metadata(): string | undefined {
259
+ return this.roomInfo?.metadata;
260
+ }
261
+
262
+ get numParticipants(): number {
263
+ return this.roomInfo?.numParticipants ?? 0;
264
+ }
265
+
266
+ get numPublishers(): number {
267
+ return this.roomInfo?.numPublishers ?? 0;
268
+ }
269
+
270
+ private maybeCreateEngine() {
271
+ if (this.engine && !this.engine.isClosed) {
272
+ return;
273
+ }
274
+
275
+ this.engine = new RTCEngine(this.options);
276
+
277
+ this.engine
278
+ .on(EngineEvent.ParticipantUpdate, this.handleParticipantUpdates)
279
+ .on(EngineEvent.RoomUpdate, this.handleRoomUpdate)
280
+ .on(EngineEvent.SpeakersChanged, this.handleSpeakersChanged)
281
+ .on(EngineEvent.StreamStateChanged, this.handleStreamStateUpdate)
282
+ .on(EngineEvent.ConnectionQualityUpdate, this.handleConnectionQualityUpdate)
283
+ .on(EngineEvent.SubscriptionError, this.handleSubscriptionError)
284
+ .on(EngineEvent.SubscriptionPermissionUpdate, this.handleSubscriptionPermissionUpdate)
285
+ .on(
286
+ EngineEvent.MediaTrackAdded,
287
+ (mediaTrack: MediaStreamTrack, stream: MediaStream, receiver?: RTCRtpReceiver) => {
288
+ this.onTrackAdded(mediaTrack, stream, receiver);
289
+ },
290
+ )
291
+ .on(EngineEvent.Disconnected, (reason?: DisconnectReason) => {
292
+ this.handleDisconnect(this.options.stopLocalTrackOnUnpublish, reason);
293
+ })
294
+ .on(EngineEvent.ActiveSpeakersUpdate, this.handleActiveSpeakersUpdate)
295
+ .on(EngineEvent.DataPacketReceived, this.handleDataPacket)
296
+ .on(EngineEvent.Resuming, () => {
297
+ this.clearConnectionReconcile();
298
+ if (this.setAndEmitConnectionState(ConnectionState.Reconnecting)) {
299
+ this.emit(RoomEvent.Reconnecting);
300
+ }
301
+ this.cachedParticipantSids = Array.from(this.participants.keys());
302
+ })
303
+ .on(EngineEvent.Resumed, () => {
304
+ this.setAndEmitConnectionState(ConnectionState.Connected);
305
+ this.emit(RoomEvent.Reconnected);
306
+ this.registerConnectionReconcile();
307
+ this.updateSubscriptions();
308
+
309
+ // once reconnected, figure out if any participants connected during reconnect and emit events for it
310
+ const diffParticipants = Array.from(this.participants.values()).filter(
311
+ (p) => !this.cachedParticipantSids.includes(p.sid),
312
+ );
313
+ diffParticipants.forEach((p) => this.emit(RoomEvent.ParticipantConnected, p));
314
+ this.cachedParticipantSids = [];
315
+ })
316
+ .on(EngineEvent.SignalResumed, () => {
317
+ if (this.state === ConnectionState.Reconnecting) {
318
+ this.sendSyncState();
319
+ }
320
+ })
321
+ .on(EngineEvent.Restarting, this.handleRestarting)
322
+ .on(EngineEvent.SignalRestarted, this.handleSignalRestarted)
323
+ .on(EngineEvent.DCBufferStatusChanged, (status, kind) => {
324
+ this.emit(RoomEvent.DCBufferStatusChanged, status, kind);
325
+ });
326
+
327
+ if (this.localParticipant) {
328
+ this.localParticipant.setupEngine(this.engine);
329
+ }
330
+ if (this.e2eeManager) {
331
+ this.e2eeManager.setupEngine(this.engine);
332
+ }
333
+ }
334
+
335
+ /**
336
+ * getLocalDevices abstracts navigator.mediaDevices.enumerateDevices.
337
+ * In particular, it handles Chrome's unique behavior of creating `default`
338
+ * devices. When encountered, it'll be removed from the list of devices.
339
+ * The actual default device will be placed at top.
340
+ * @param kind
341
+ * @returns a list of available local devices
342
+ */
343
+ static getLocalDevices(
344
+ kind?: MediaDeviceKind,
345
+ requestPermissions: boolean = true,
346
+ ): Promise<MediaDeviceInfo[]> {
347
+ return DeviceManager.getInstance().getDevices(kind, requestPermissions);
348
+ }
349
+
350
+ /**
351
+ * prepareConnection should be called as soon as the page is loaded, in order
352
+ * to speed up the connection attempt. This function will
353
+ * - perform DNS resolution and pre-warm the DNS cache
354
+ * - establish TLS connection and cache TLS keys
355
+ *
356
+ * With LiveKit Cloud, it will also determine the best edge data center for
357
+ * the current client to connect to if a token is provided.
358
+ */
359
+ async prepareConnection(url: string, token?: string) {
360
+ if (this.state !== ConnectionState.Disconnected) {
361
+ return;
362
+ }
363
+ log.debug(`prepareConnection to ${url}`);
364
+ try {
365
+ if (isCloud(new URL(url)) && token) {
366
+ this.regionUrlProvider = new RegionUrlProvider(url, token);
367
+ const regionUrl = await this.regionUrlProvider.getNextBestRegionUrl();
368
+ // we will not replace the regionUrl if an attempt had already started
369
+ // to avoid overriding regionUrl after a new connection attempt had started
370
+ if (regionUrl && this.state === ConnectionState.Disconnected) {
371
+ this.regionUrl = regionUrl;
372
+ await fetch(toHttpUrl(regionUrl), { method: 'HEAD' });
373
+ log.debug(`prepared connection to ${regionUrl}`);
374
+ }
375
+ } else {
376
+ await fetch(toHttpUrl(url), { method: 'HEAD' });
377
+ }
378
+ } catch (e) {
379
+ log.warn('could not prepare connection', { error: e });
380
+ }
381
+ }
382
+
383
+ connect = async (url: string, token: string, opts?: RoomConnectOptions): Promise<void> => {
384
+ // In case a disconnect called happened right before the connect call, make sure the disconnect is completed first by awaiting its lock
385
+ const unlockDisconnect = await this.disconnectLock.lock();
386
+
387
+ if (this.state === ConnectionState.Connected) {
388
+ // when the state is reconnecting or connected, this function returns immediately
389
+ log.info(`already connected to room ${this.name}`);
390
+ unlockDisconnect();
391
+ return Promise.resolve();
392
+ }
393
+
394
+ if (this.connectFuture) {
395
+ unlockDisconnect();
396
+ return this.connectFuture.promise;
397
+ }
398
+
399
+ this.setAndEmitConnectionState(ConnectionState.Connecting);
400
+ if (this.regionUrlProvider?.getServerUrl().toString() !== url) {
401
+ this.regionUrl = undefined;
402
+ this.regionUrlProvider = undefined;
403
+ }
404
+ if (isCloud(new URL(url))) {
405
+ if (this.regionUrlProvider === undefined) {
406
+ this.regionUrlProvider = new RegionUrlProvider(url, token);
407
+ } else {
408
+ this.regionUrlProvider.updateToken(token);
409
+ }
410
+ // trigger the first fetch without waiting for a response
411
+ // if initial connection fails, this will speed up picking regional url
412
+ // on subsequent runs
413
+ this.regionUrlProvider.fetchRegionSettings().catch((e) => {
414
+ log.warn('could not fetch region settings', { error: e });
415
+ });
416
+ }
417
+
418
+ const connectFn = async (
419
+ resolve: () => void,
420
+ reject: (reason: any) => void,
421
+ regionUrl?: string,
422
+ ) => {
423
+ if (this.abortController) {
424
+ this.abortController.abort();
425
+ }
426
+
427
+ this.abortController = new AbortController();
428
+
429
+ // at this point the intention to connect has been signalled so we can allow cancelling of the connection via disconnect() again
430
+ unlockDisconnect?.();
431
+
432
+ try {
433
+ await this.attemptConnection(regionUrl ?? url, token, opts, this.abortController);
434
+ this.abortController = undefined;
435
+ resolve();
436
+ } catch (e) {
437
+ if (
438
+ this.regionUrlProvider &&
439
+ e instanceof ConnectionError &&
440
+ e.reason !== ConnectionErrorReason.Cancelled &&
441
+ e.reason !== ConnectionErrorReason.NotAllowed
442
+ ) {
443
+ let nextUrl: string | null = null;
444
+ try {
445
+ nextUrl = await this.regionUrlProvider.getNextBestRegionUrl(
446
+ this.abortController?.signal,
447
+ );
448
+ } catch (error) {
449
+ if (
450
+ error instanceof ConnectionError &&
451
+ (error.status === 401 || error.reason === ConnectionErrorReason.Cancelled)
452
+ ) {
453
+ reject(error);
454
+ return;
455
+ }
456
+ }
457
+ if (nextUrl) {
458
+ log.info('initial connection failed, retrying with another region', {
459
+ nextUrl,
460
+ });
461
+ await connectFn(resolve, reject, nextUrl);
462
+ } else {
463
+ reject(e);
464
+ }
465
+ } else {
466
+ reject(e);
467
+ }
468
+ }
469
+ };
470
+
471
+ const regionUrl = this.regionUrl;
472
+ this.regionUrl = undefined;
473
+ this.connectFuture = new Future(
474
+ (resolve, reject) => {
475
+ connectFn(resolve, reject, regionUrl);
476
+ },
477
+ () => {
478
+ this.clearConnectionFutures();
479
+ },
480
+ );
481
+
482
+ return this.connectFuture.promise;
483
+ };
484
+
485
+ private connectSignal = async (
486
+ url: string,
487
+ token: string,
488
+ engine: RTCEngine,
489
+ connectOptions: InternalRoomConnectOptions,
490
+ roomOptions: InternalRoomOptions,
491
+ abortController: AbortController,
492
+ ): Promise<JoinResponse> => {
493
+ const joinResponse = await engine.join(
494
+ url,
495
+ token,
496
+ {
497
+ autoSubscribe: connectOptions.autoSubscribe,
498
+ publishOnly: connectOptions.publishOnly,
499
+ adaptiveStream:
500
+ typeof roomOptions.adaptiveStream === 'object' ? true : roomOptions.adaptiveStream,
501
+ maxRetries: connectOptions.maxRetries,
502
+ e2eeEnabled: !!this.e2eeManager,
503
+ },
504
+ abortController.signal,
505
+ );
506
+
507
+ let serverInfo: Partial<ServerInfo> | undefined = joinResponse.serverInfo;
508
+ if (!serverInfo) {
509
+ serverInfo = { version: joinResponse.serverVersion, region: joinResponse.serverRegion };
510
+ }
511
+
512
+ log.debug(
513
+ `connected to Livekit Server ${Object.entries(serverInfo)
514
+ .map(([key, value]) => `${key}: ${value}`)
515
+ .join(', ')}`,
516
+ );
517
+
518
+ if (!joinResponse.serverVersion) {
519
+ throw new UnsupportedServer('unknown server version');
520
+ }
521
+
522
+ if (joinResponse.serverVersion === '0.15.1' && this.options.dynacast) {
523
+ log.debug('disabling dynacast due to server version');
524
+ // dynacast has a bug in 0.15.1, so we cannot use it then
525
+ roomOptions.dynacast = false;
526
+ }
527
+
528
+ return joinResponse;
529
+ };
530
+
531
+ private applyJoinResponse = (joinResponse: JoinResponse) => {
532
+ const pi = joinResponse.participant!;
533
+
534
+ this.localParticipant.sid = pi.sid;
535
+ this.localParticipant.identity = pi.identity;
536
+
537
+ // populate remote participants, these should not trigger new events
538
+ this.handleParticipantUpdates([pi, ...joinResponse.otherParticipants]);
539
+
540
+ if (joinResponse.room) {
541
+ this.handleRoomUpdate(joinResponse.room);
542
+ }
543
+ };
544
+
545
+ private attemptConnection = async (
546
+ url: string,
547
+ token: string,
548
+ opts: RoomConnectOptions | undefined,
549
+ abortController: AbortController,
550
+ ) => {
551
+ if (this.state === ConnectionState.Reconnecting) {
552
+ log.info('Reconnection attempt replaced by new connection attempt');
553
+ // make sure we close and recreate the existing engine in order to get rid of any potentially ongoing reconnection attempts
554
+ this.recreateEngine();
555
+ } else {
556
+ // create engine if previously disconnected
557
+ this.maybeCreateEngine();
558
+ }
559
+ if (this.regionUrlProvider?.isCloud()) {
560
+ this.engine.setRegionUrlProvider(this.regionUrlProvider);
561
+ }
562
+
563
+ this.acquireAudioContext();
564
+
565
+ this.connOptions = { ...roomConnectOptionDefaults, ...opts } as InternalRoomConnectOptions;
566
+
567
+ if (this.connOptions.rtcConfig) {
568
+ this.engine.rtcConfig = this.connOptions.rtcConfig;
569
+ }
570
+ if (this.connOptions.peerConnectionTimeout) {
571
+ this.engine.peerConnectionTimeout = this.connOptions.peerConnectionTimeout;
572
+ }
573
+
574
+ try {
575
+ const joinResponse = await this.connectSignal(
576
+ url,
577
+ token,
578
+ this.engine,
579
+ this.connOptions,
580
+ this.options,
581
+ abortController,
582
+ );
583
+
584
+ this.applyJoinResponse(joinResponse);
585
+ // forward metadata changed for the local participant
586
+ this.setupLocalParticipantEvents();
587
+ this.emit(RoomEvent.SignalConnected);
588
+ } catch (err) {
589
+ this.recreateEngine();
590
+ this.handleDisconnect(this.options.stopLocalTrackOnUnpublish);
591
+ const resultingError = new ConnectionError(`could not establish signal connection`);
592
+ if (err instanceof Error) {
593
+ resultingError.message = `${resultingError.message}: ${err.message}`;
594
+ }
595
+ if (err instanceof ConnectionError) {
596
+ resultingError.reason = err.reason;
597
+ resultingError.status = err.status;
598
+ }
599
+ log.debug(`error trying to establish signal connection`, { error: err });
600
+ throw resultingError;
601
+ }
602
+
603
+ if (abortController.signal.aborted) {
604
+ this.recreateEngine();
605
+ this.handleDisconnect(this.options.stopLocalTrackOnUnpublish);
606
+ throw new ConnectionError(`Connection attempt aborted`);
607
+ }
608
+
609
+ try {
610
+ await this.engine.waitForPCInitialConnection(
611
+ this.connOptions.peerConnectionTimeout,
612
+ abortController,
613
+ );
614
+ } catch (e) {
615
+ this.recreateEngine();
616
+ this.handleDisconnect(this.options.stopLocalTrackOnUnpublish);
617
+ throw e;
618
+ }
619
+
620
+ // also hook unload event
621
+ if (isWeb() && this.options.disconnectOnPageLeave) {
622
+ // capturing both 'pagehide' and 'beforeunload' to capture broadest set of browser behaviors
623
+ window.addEventListener('pagehide', this.onPageLeave);
624
+ window.addEventListener('beforeunload', this.onPageLeave);
625
+ }
626
+ if (isWeb()) {
627
+ document.addEventListener('freeze', this.onPageLeave);
628
+ navigator.mediaDevices?.addEventListener('devicechange', this.handleDeviceChange);
629
+ }
630
+ this.setAndEmitConnectionState(ConnectionState.Connected);
631
+ this.emit(RoomEvent.Connected);
632
+ this.registerConnectionReconcile();
633
+ };
634
+
635
+ /**
636
+ * disconnects the room, emits [[RoomEvent.Disconnected]]
637
+ */
638
+ disconnect = async (stopTracks = true) => {
639
+ const unlock = await this.disconnectLock.lock();
640
+ try {
641
+ if (this.state === ConnectionState.Disconnected) {
642
+ log.debug('already disconnected');
643
+ return;
644
+ }
645
+ log.info('disconnect from room', { identity: this.localParticipant.identity });
646
+ if (
647
+ this.state === ConnectionState.Connecting ||
648
+ this.state === ConnectionState.Reconnecting
649
+ ) {
650
+ // try aborting pending connection attempt
651
+ log.warn('abort connection attempt');
652
+ this.abortController?.abort();
653
+ // in case the abort controller didn't manage to cancel the connection attempt, reject the connect promise explicitly
654
+ this.connectFuture?.reject?.(new ConnectionError('Client initiated disconnect'));
655
+ this.connectFuture = undefined;
656
+ }
657
+ // send leave
658
+ if (this.engine?.client.isConnected) {
659
+ await this.engine.client.sendLeave();
660
+ }
661
+ // close engine (also closes client)
662
+ if (this.engine) {
663
+ await this.engine.close();
664
+ }
665
+ this.handleDisconnect(stopTracks, DisconnectReason.CLIENT_INITIATED);
666
+ /* @ts-ignore */
667
+ this.engine = undefined;
668
+ } finally {
669
+ unlock();
670
+ }
671
+ };
672
+
673
+ /**
674
+ * retrieves a participant by identity
675
+ * @param identity
676
+ * @returns
677
+ */
678
+ getParticipantByIdentity(identity: string): Participant | undefined {
679
+ if (this.localParticipant.identity === identity) {
680
+ return this.localParticipant;
681
+ }
682
+ const sid = this.identityToSid.get(identity);
683
+ if (sid) {
684
+ return this.participants.get(sid);
685
+ }
686
+ }
687
+
688
+ private clearConnectionFutures() {
689
+ this.connectFuture = undefined;
690
+ }
691
+
692
+ /**
693
+ * @internal for testing
694
+ */
695
+ async simulateScenario(scenario: SimulationScenario) {
696
+ let postAction = () => {};
697
+ let req: SimulateScenario | undefined;
698
+ switch (scenario) {
699
+ case 'signal-reconnect':
700
+ // @ts-expect-error function is private
701
+ await this.engine.client.handleOnClose('simulate disconnect');
702
+ break;
703
+ case 'speaker':
704
+ req = new SimulateScenario({
705
+ scenario: {
706
+ case: 'speakerUpdate',
707
+ value: 3,
708
+ },
709
+ });
710
+ break;
711
+ case 'node-failure':
712
+ req = new SimulateScenario({
713
+ scenario: {
714
+ case: 'nodeFailure',
715
+ value: true,
716
+ },
717
+ });
718
+ break;
719
+ case 'server-leave':
720
+ req = new SimulateScenario({
721
+ scenario: {
722
+ case: 'serverLeave',
723
+ value: true,
724
+ },
725
+ });
726
+ break;
727
+ case 'migration':
728
+ req = new SimulateScenario({
729
+ scenario: {
730
+ case: 'migration',
731
+ value: true,
732
+ },
733
+ });
734
+ break;
735
+ case 'resume-reconnect':
736
+ this.engine.failNext();
737
+ // @ts-expect-error function is private
738
+ await this.engine.client.handleOnClose('simulate resume-disconnect');
739
+ break;
740
+ case 'full-reconnect':
741
+ this.engine.fullReconnectOnNext = true;
742
+ // @ts-expect-error function is private
743
+ await this.engine.client.handleOnClose('simulate full-reconnect');
744
+ break;
745
+ case 'force-tcp':
746
+ case 'force-tls':
747
+ req = new SimulateScenario({
748
+ scenario: {
749
+ case: 'switchCandidateProtocol',
750
+ value: scenario === 'force-tls' ? 2 : 1,
751
+ },
752
+ });
753
+ postAction = async () => {
754
+ const onLeave = this.engine.client.onLeave;
755
+ if (onLeave) {
756
+ onLeave(
757
+ new LeaveRequest({
758
+ reason: DisconnectReason.CLIENT_INITIATED,
759
+ canReconnect: true,
760
+ }),
761
+ );
762
+ }
763
+ };
764
+ break;
765
+ default:
766
+ }
767
+ if (req) {
768
+ this.engine.client.sendSimulateScenario(req);
769
+ postAction();
770
+ }
771
+ }
772
+
773
+ private onPageLeave = async () => {
774
+ await this.disconnect();
775
+ };
776
+
777
+ /**
778
+ * Browsers have different policies regarding audio playback. Most requiring
779
+ * some form of user interaction (click/tap/etc).
780
+ * In those cases, audio will be silent until a click/tap triggering one of the following
781
+ * - `startAudio`
782
+ * - `getUserMedia`
783
+ */
784
+ async startAudio() {
785
+ await this.acquireAudioContext();
786
+ const elements: Array<HTMLMediaElement> = [];
787
+ const browser = getBrowser();
788
+ if (browser && browser.os === 'iOS') {
789
+ /**
790
+ * iOS blocks audio element playback if
791
+ * - user is not publishing audio themselves and
792
+ * - no other audio source is playing
793
+ *
794
+ * as a workaround, we create an audio element with an empty track, so that
795
+ * silent audio is always playing
796
+ */
797
+ const audioId = 'livekit-dummy-audio-el';
798
+ let dummyAudioEl = document.getElementById(audioId) as HTMLAudioElement | null;
799
+ if (!dummyAudioEl) {
800
+ dummyAudioEl = document.createElement('audio');
801
+ dummyAudioEl.id = audioId;
802
+ dummyAudioEl.autoplay = true;
803
+ dummyAudioEl.hidden = true;
804
+ const track = getEmptyAudioStreamTrack();
805
+ track.enabled = true;
806
+ const stream = new MediaStream([track]);
807
+ dummyAudioEl.srcObject = stream;
808
+ document.addEventListener('visibilitychange', () => {
809
+ if (!dummyAudioEl) {
810
+ return;
811
+ }
812
+ // set the srcObject to null on page hide in order to prevent lock screen controls to show up for it
813
+ dummyAudioEl.srcObject = document.hidden ? null : stream;
814
+ });
815
+ document.body.append(dummyAudioEl);
816
+ this.once(RoomEvent.Disconnected, () => {
817
+ dummyAudioEl?.remove();
818
+ });
819
+ }
820
+ elements.push(dummyAudioEl);
821
+ }
822
+
823
+ this.participants.forEach((p) => {
824
+ p.audioTracks.forEach((t) => {
825
+ if (t.track) {
826
+ t.track.attachedElements.forEach((e) => {
827
+ elements.push(e);
828
+ });
829
+ }
830
+ });
831
+ });
832
+
833
+ try {
834
+ await Promise.all(
835
+ elements.map((e) => {
836
+ e.muted = false;
837
+ return e.play();
838
+ }),
839
+ );
840
+ this.handleAudioPlaybackStarted();
841
+ } catch (err) {
842
+ this.handleAudioPlaybackFailed(err);
843
+ throw err;
844
+ }
845
+ }
846
+
847
+ /**
848
+ * Returns true if audio playback is enabled
849
+ */
850
+ get canPlaybackAudio(): boolean {
851
+ return this.audioEnabled;
852
+ }
853
+
854
+ /**
855
+ * Returns the active audio output device used in this room.
856
+ * @return the previously successfully set audio output device ID or an empty string if the default device is used.
857
+ * @deprecated use `getActiveDevice('audiooutput')` instead
858
+ */
859
+ getActiveAudioOutputDevice(): string {
860
+ return this.options.audioOutput?.deviceId ?? '';
861
+ }
862
+
863
+ getActiveDevice(kind: MediaDeviceKind): string | undefined {
864
+ return this.localParticipant.activeDeviceMap.get(kind);
865
+ }
866
+
867
+ /**
868
+ * Switches all active devices used in this room to the given device.
869
+ *
870
+ * Note: setting AudioOutput is not supported on some browsers. See [setSinkId](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility)
871
+ *
872
+ * @param kind use `videoinput` for camera track,
873
+ * `audioinput` for microphone track,
874
+ * `audiooutput` to set speaker for all incoming audio tracks
875
+ * @param deviceId
876
+ */
877
+ async switchActiveDevice(kind: MediaDeviceKind, deviceId: string, exact: boolean = false) {
878
+ let deviceHasChanged = false;
879
+ let success = true;
880
+ const deviceConstraint = exact ? { exact: deviceId } : deviceId;
881
+ if (kind === 'audioinput') {
882
+ const prevDeviceId = this.options.audioCaptureDefaults!.deviceId;
883
+ this.options.audioCaptureDefaults!.deviceId = deviceConstraint;
884
+ deviceHasChanged = prevDeviceId !== deviceConstraint;
885
+ const tracks = Array.from(this.localParticipant.audioTracks.values()).filter(
886
+ (track) => track.source === Track.Source.Microphone,
887
+ );
888
+ try {
889
+ success = (
890
+ await Promise.all(tracks.map((t) => t.audioTrack?.setDeviceId(deviceConstraint)))
891
+ ).every((val) => val === true);
892
+ } catch (e) {
893
+ this.options.audioCaptureDefaults!.deviceId = prevDeviceId;
894
+ throw e;
895
+ }
896
+ } else if (kind === 'videoinput') {
897
+ const prevDeviceId = this.options.videoCaptureDefaults!.deviceId;
898
+ this.options.videoCaptureDefaults!.deviceId = deviceConstraint;
899
+ deviceHasChanged = prevDeviceId !== deviceConstraint;
900
+ const tracks = Array.from(this.localParticipant.videoTracks.values()).filter(
901
+ (track) => track.source === Track.Source.Camera,
902
+ );
903
+ try {
904
+ success = (
905
+ await Promise.all(tracks.map((t) => t.videoTrack?.setDeviceId(deviceConstraint)))
906
+ ).every((val) => val === true);
907
+ } catch (e) {
908
+ this.options.videoCaptureDefaults!.deviceId = prevDeviceId;
909
+ throw e;
910
+ }
911
+ } else if (kind === 'audiooutput') {
912
+ if (
913
+ (!supportsSetSinkId() && !this.options.expWebAudioMix) ||
914
+ (this.options.expWebAudioMix && this.audioContext && !('setSinkId' in this.audioContext))
915
+ ) {
916
+ throw new Error('cannot switch audio output, setSinkId not supported');
917
+ }
918
+ this.options.audioOutput ??= {};
919
+ const prevDeviceId = this.options.audioOutput.deviceId;
920
+ this.options.audioOutput.deviceId = deviceId;
921
+ deviceHasChanged = prevDeviceId !== deviceConstraint;
922
+
923
+ try {
924
+ if (this.options.expWebAudioMix) {
925
+ // @ts-expect-error setSinkId is not yet in the typescript type of AudioContext
926
+ this.audioContext?.setSinkId(deviceId);
927
+ } else {
928
+ await Promise.all(
929
+ Array.from(this.participants.values()).map((p) => p.setAudioOutput({ deviceId })),
930
+ );
931
+ }
932
+ } catch (e) {
933
+ this.options.audioOutput.deviceId = prevDeviceId;
934
+ throw e;
935
+ }
936
+ }
937
+ if (deviceHasChanged && success) {
938
+ this.localParticipant.activeDeviceMap.set(kind, deviceId);
939
+ this.emit(RoomEvent.ActiveDeviceChanged, kind, deviceId);
940
+ }
941
+
942
+ return success;
943
+ }
944
+
945
+ private setupLocalParticipantEvents() {
946
+ this.localParticipant
947
+ .on(ParticipantEvent.ParticipantMetadataChanged, this.onLocalParticipantMetadataChanged)
948
+ .on(ParticipantEvent.ParticipantNameChanged, this.onLocalParticipantNameChanged)
949
+ .on(ParticipantEvent.TrackMuted, this.onLocalTrackMuted)
950
+ .on(ParticipantEvent.TrackUnmuted, this.onLocalTrackUnmuted)
951
+ .on(ParticipantEvent.LocalTrackPublished, this.onLocalTrackPublished)
952
+ .on(ParticipantEvent.LocalTrackUnpublished, this.onLocalTrackUnpublished)
953
+ .on(ParticipantEvent.ConnectionQualityChanged, this.onLocalConnectionQualityChanged)
954
+ .on(ParticipantEvent.MediaDevicesError, this.onMediaDevicesError)
955
+ .on(
956
+ ParticipantEvent.ParticipantPermissionsChanged,
957
+ this.onLocalParticipantPermissionsChanged,
958
+ );
959
+ }
960
+
961
+ private recreateEngine() {
962
+ this.engine?.close();
963
+ /* @ts-ignore */
964
+ this.engine = undefined;
965
+
966
+ // clear out existing remote participants, since they may have attached
967
+ // the old engine
968
+ this.participants.clear();
969
+ this.maybeCreateEngine();
970
+ }
971
+
972
+ private onTrackAdded(
973
+ mediaTrack: MediaStreamTrack,
974
+ stream: MediaStream,
975
+ receiver?: RTCRtpReceiver,
976
+ ) {
977
+ // don't fire onSubscribed when connecting
978
+ // WebRTC fires onTrack as soon as setRemoteDescription is called on the offer
979
+ // at that time, ICE connectivity has not been established so the track is not
980
+ // technically subscribed.
981
+ // We'll defer these events until when the room is connected or eventually disconnected.
982
+ if (this.state === ConnectionState.Connecting || this.state === ConnectionState.Reconnecting) {
983
+ const reconnectedHandler = () => {
984
+ this.onTrackAdded(mediaTrack, stream, receiver);
985
+ cleanup();
986
+ };
987
+ const cleanup = () => {
988
+ this.off(RoomEvent.Reconnected, reconnectedHandler);
989
+ this.off(RoomEvent.Connected, reconnectedHandler);
990
+ this.off(RoomEvent.Disconnected, cleanup);
991
+ };
992
+ this.once(RoomEvent.Reconnected, reconnectedHandler);
993
+ this.once(RoomEvent.Connected, reconnectedHandler);
994
+ this.once(RoomEvent.Disconnected, cleanup);
995
+ return;
996
+ }
997
+ if (this.state === ConnectionState.Disconnected) {
998
+ log.warn('skipping incoming track after Room disconnected');
999
+ return;
1000
+ }
1001
+ const parts = unpackStreamId(stream.id);
1002
+ const participantId = parts[0];
1003
+ let trackId = parts[1];
1004
+ if (!trackId || trackId === '') trackId = mediaTrack.id;
1005
+
1006
+ if (participantId === this.localParticipant.sid) {
1007
+ log.warn('tried to create RemoteParticipant for local participant');
1008
+ return;
1009
+ }
1010
+ const participant = this.getOrCreateParticipant(participantId);
1011
+
1012
+ let adaptiveStreamSettings: AdaptiveStreamSettings | undefined;
1013
+ if (this.options.adaptiveStream) {
1014
+ if (typeof this.options.adaptiveStream === 'object') {
1015
+ adaptiveStreamSettings = this.options.adaptiveStream;
1016
+ } else {
1017
+ adaptiveStreamSettings = {};
1018
+ }
1019
+ }
1020
+ participant.addSubscribedMediaTrack(
1021
+ mediaTrack,
1022
+ trackId,
1023
+ stream,
1024
+ receiver,
1025
+ adaptiveStreamSettings,
1026
+ );
1027
+ }
1028
+
1029
+ private handleRestarting = () => {
1030
+ this.clearConnectionReconcile();
1031
+ // also unwind existing participants & existing subscriptions
1032
+ for (const p of this.participants.values()) {
1033
+ this.handleParticipantDisconnected(p.sid, p);
1034
+ }
1035
+
1036
+ if (this.setAndEmitConnectionState(ConnectionState.Reconnecting)) {
1037
+ this.emit(RoomEvent.Reconnecting);
1038
+ }
1039
+ };
1040
+
1041
+ private handleSignalRestarted = async (joinResponse: JoinResponse) => {
1042
+ log.debug(`signal reconnected to server`, {
1043
+ region: joinResponse.serverRegion,
1044
+ });
1045
+
1046
+ this.cachedParticipantSids = [];
1047
+ this.applyJoinResponse(joinResponse);
1048
+
1049
+ try {
1050
+ // unpublish & republish tracks
1051
+ const localPubs: LocalTrackPublication[] = [];
1052
+ this.localParticipant.tracks.forEach((pub) => {
1053
+ if (pub.track) {
1054
+ localPubs.push(pub);
1055
+ }
1056
+ });
1057
+
1058
+ await Promise.all(
1059
+ localPubs.map(async (pub) => {
1060
+ const track = pub.track!;
1061
+ this.localParticipant.unpublishTrack(track, false);
1062
+ if (!track.isMuted) {
1063
+ if (
1064
+ (track instanceof LocalAudioTrack || track instanceof LocalVideoTrack) &&
1065
+ track.source !== Track.Source.ScreenShare &&
1066
+ track.source !== Track.Source.ScreenShareAudio &&
1067
+ !track.isUserProvided
1068
+ ) {
1069
+ // we need to restart the track before publishing, often a full reconnect
1070
+ // is necessary because computer had gone to sleep.
1071
+ log.debug('restarting existing track', {
1072
+ track: pub.trackSid,
1073
+ });
1074
+ await track.restartTrack();
1075
+ }
1076
+ log.debug('publishing new track', {
1077
+ track: pub.trackSid,
1078
+ });
1079
+ await this.localParticipant.publishTrack(track, pub.options);
1080
+ }
1081
+ }),
1082
+ );
1083
+ } catch (error) {
1084
+ log.error('error trying to re-publish tracks after reconnection', { error });
1085
+ }
1086
+
1087
+ try {
1088
+ await this.engine.waitForRestarted();
1089
+ log.debug(`fully reconnected to server`, {
1090
+ region: joinResponse.serverRegion,
1091
+ });
1092
+ } catch {
1093
+ // reconnection failed, handleDisconnect is being invoked already, just return here
1094
+ return;
1095
+ }
1096
+ this.setAndEmitConnectionState(ConnectionState.Connected);
1097
+ this.emit(RoomEvent.Reconnected);
1098
+ this.registerConnectionReconcile();
1099
+
1100
+ // emit participant connected events after connection has been re-established
1101
+ this.participants.forEach((participant) => {
1102
+ this.emit(RoomEvent.ParticipantConnected, participant);
1103
+ });
1104
+ };
1105
+
1106
+ private handleDisconnect(shouldStopTracks = true, reason?: DisconnectReason) {
1107
+ this.clearConnectionReconcile();
1108
+ if (this.state === ConnectionState.Disconnected) {
1109
+ return;
1110
+ }
1111
+
1112
+ this.regionUrl = undefined;
1113
+
1114
+ try {
1115
+ this.participants.forEach((p) => {
1116
+ p.tracks.forEach((pub) => {
1117
+ p.unpublishTrack(pub.trackSid);
1118
+ });
1119
+ });
1120
+
1121
+ this.localParticipant.tracks.forEach((pub) => {
1122
+ if (pub.track) {
1123
+ this.localParticipant.unpublishTrack(pub.track, shouldStopTracks);
1124
+ }
1125
+ if (shouldStopTracks) {
1126
+ pub.track?.detach();
1127
+ pub.track?.stop();
1128
+ }
1129
+ });
1130
+
1131
+ this.localParticipant
1132
+ .off(ParticipantEvent.ParticipantMetadataChanged, this.onLocalParticipantMetadataChanged)
1133
+ .off(ParticipantEvent.ParticipantNameChanged, this.onLocalParticipantNameChanged)
1134
+ .off(ParticipantEvent.TrackMuted, this.onLocalTrackMuted)
1135
+ .off(ParticipantEvent.TrackUnmuted, this.onLocalTrackUnmuted)
1136
+ .off(ParticipantEvent.LocalTrackPublished, this.onLocalTrackPublished)
1137
+ .off(ParticipantEvent.LocalTrackUnpublished, this.onLocalTrackUnpublished)
1138
+ .off(ParticipantEvent.ConnectionQualityChanged, this.onLocalConnectionQualityChanged)
1139
+ .off(ParticipantEvent.MediaDevicesError, this.onMediaDevicesError)
1140
+ .off(
1141
+ ParticipantEvent.ParticipantPermissionsChanged,
1142
+ this.onLocalParticipantPermissionsChanged,
1143
+ );
1144
+
1145
+ this.localParticipant.tracks.clear();
1146
+ this.localParticipant.videoTracks.clear();
1147
+ this.localParticipant.audioTracks.clear();
1148
+
1149
+ this.participants.clear();
1150
+ this.activeSpeakers = [];
1151
+ if (this.audioContext && typeof this.options.expWebAudioMix === 'boolean') {
1152
+ this.audioContext.close();
1153
+ this.audioContext = undefined;
1154
+ }
1155
+ if (isWeb()) {
1156
+ window.removeEventListener('beforeunload', this.onPageLeave);
1157
+ window.removeEventListener('pagehide', this.onPageLeave);
1158
+ window.removeEventListener('freeze', this.onPageLeave);
1159
+ navigator.mediaDevices?.removeEventListener('devicechange', this.handleDeviceChange);
1160
+ }
1161
+ } finally {
1162
+ this.setAndEmitConnectionState(ConnectionState.Disconnected);
1163
+ this.emit(RoomEvent.Disconnected, reason);
1164
+ }
1165
+ }
1166
+
1167
+ private handleParticipantUpdates = (participantInfos: ParticipantInfo[]) => {
1168
+ // handle changes to participant state, and send events
1169
+ participantInfos.forEach((info) => {
1170
+ if (info.identity === this.localParticipant.identity) {
1171
+ this.localParticipant.updateInfo(info);
1172
+ return;
1173
+ }
1174
+
1175
+ // ensure identity <=> sid mapping
1176
+ const sid = this.identityToSid.get(info.identity);
1177
+ if (sid && sid !== info.sid) {
1178
+ // sid had changed, need to remove previous participant
1179
+ this.handleParticipantDisconnected(sid, this.participants.get(sid));
1180
+ }
1181
+
1182
+ let remoteParticipant = this.participants.get(info.sid);
1183
+ const isNewParticipant = !remoteParticipant;
1184
+
1185
+ // when it's disconnected, send updates
1186
+ if (info.state === ParticipantInfo_State.DISCONNECTED) {
1187
+ this.handleParticipantDisconnected(info.sid, remoteParticipant);
1188
+ } else {
1189
+ // create participant if doesn't exist
1190
+ remoteParticipant = this.getOrCreateParticipant(info.sid, info);
1191
+ if (!isNewParticipant) {
1192
+ // just update, no events
1193
+ remoteParticipant.updateInfo(info);
1194
+ }
1195
+ }
1196
+ });
1197
+ };
1198
+
1199
+ private handleParticipantDisconnected(sid: string, participant?: RemoteParticipant) {
1200
+ // remove and send event
1201
+ this.participants.delete(sid);
1202
+ if (!participant) {
1203
+ return;
1204
+ }
1205
+
1206
+ this.identityToSid.delete(participant.identity);
1207
+ participant.tracks.forEach((publication) => {
1208
+ participant.unpublishTrack(publication.trackSid, true);
1209
+ });
1210
+ this.emit(RoomEvent.ParticipantDisconnected, participant);
1211
+ }
1212
+
1213
+ // updates are sent only when there's a change to speaker ordering
1214
+ private handleActiveSpeakersUpdate = (speakers: SpeakerInfo[]) => {
1215
+ const activeSpeakers: Participant[] = [];
1216
+ const seenSids: any = {};
1217
+ speakers.forEach((speaker) => {
1218
+ seenSids[speaker.sid] = true;
1219
+ if (speaker.sid === this.localParticipant.sid) {
1220
+ this.localParticipant.audioLevel = speaker.level;
1221
+ this.localParticipant.setIsSpeaking(true);
1222
+ activeSpeakers.push(this.localParticipant);
1223
+ } else {
1224
+ const p = this.participants.get(speaker.sid);
1225
+ if (p) {
1226
+ p.audioLevel = speaker.level;
1227
+ p.setIsSpeaking(true);
1228
+ activeSpeakers.push(p);
1229
+ }
1230
+ }
1231
+ });
1232
+
1233
+ if (!seenSids[this.localParticipant.sid]) {
1234
+ this.localParticipant.audioLevel = 0;
1235
+ this.localParticipant.setIsSpeaking(false);
1236
+ }
1237
+ this.participants.forEach((p) => {
1238
+ if (!seenSids[p.sid]) {
1239
+ p.audioLevel = 0;
1240
+ p.setIsSpeaking(false);
1241
+ }
1242
+ });
1243
+
1244
+ this.activeSpeakers = activeSpeakers;
1245
+ this.emitWhenConnected(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
1246
+ };
1247
+
1248
+ // process list of changed speakers
1249
+ private handleSpeakersChanged = (speakerUpdates: SpeakerInfo[]) => {
1250
+ const lastSpeakers = new Map<string, Participant>();
1251
+ this.activeSpeakers.forEach((p) => {
1252
+ lastSpeakers.set(p.sid, p);
1253
+ });
1254
+ speakerUpdates.forEach((speaker) => {
1255
+ let p: Participant | undefined = this.participants.get(speaker.sid);
1256
+ if (speaker.sid === this.localParticipant.sid) {
1257
+ p = this.localParticipant;
1258
+ }
1259
+ if (!p) {
1260
+ return;
1261
+ }
1262
+ p.audioLevel = speaker.level;
1263
+ p.setIsSpeaking(speaker.active);
1264
+
1265
+ if (speaker.active) {
1266
+ lastSpeakers.set(speaker.sid, p);
1267
+ } else {
1268
+ lastSpeakers.delete(speaker.sid);
1269
+ }
1270
+ });
1271
+ const activeSpeakers = Array.from(lastSpeakers.values());
1272
+ activeSpeakers.sort((a, b) => b.audioLevel - a.audioLevel);
1273
+ this.activeSpeakers = activeSpeakers;
1274
+ this.emitWhenConnected(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
1275
+ };
1276
+
1277
+ private handleStreamStateUpdate = (streamStateUpdate: StreamStateUpdate) => {
1278
+ streamStateUpdate.streamStates.forEach((streamState) => {
1279
+ const participant = this.participants.get(streamState.participantSid);
1280
+ if (!participant) {
1281
+ return;
1282
+ }
1283
+ const pub = participant.getTrackPublication(streamState.trackSid);
1284
+ if (!pub || !pub.track) {
1285
+ return;
1286
+ }
1287
+ pub.track.streamState = Track.streamStateFromProto(streamState.state);
1288
+ participant.emit(ParticipantEvent.TrackStreamStateChanged, pub, pub.track.streamState);
1289
+ this.emitWhenConnected(
1290
+ RoomEvent.TrackStreamStateChanged,
1291
+ pub,
1292
+ pub.track.streamState,
1293
+ participant,
1294
+ );
1295
+ });
1296
+ };
1297
+
1298
+ private handleSubscriptionPermissionUpdate = (update: SubscriptionPermissionUpdate) => {
1299
+ const participant = this.participants.get(update.participantSid);
1300
+ if (!participant) {
1301
+ return;
1302
+ }
1303
+ const pub = participant.getTrackPublication(update.trackSid);
1304
+ if (!pub) {
1305
+ return;
1306
+ }
1307
+
1308
+ pub.setAllowed(update.allowed);
1309
+ };
1310
+
1311
+ private handleSubscriptionError = (update: SubscriptionResponse) => {
1312
+ const participant = Array.from(this.participants.values()).find((p) =>
1313
+ p.tracks.has(update.trackSid),
1314
+ );
1315
+ if (!participant) {
1316
+ return;
1317
+ }
1318
+ const pub = participant.getTrackPublication(update.trackSid);
1319
+ if (!pub) {
1320
+ return;
1321
+ }
1322
+
1323
+ pub.setSubscriptionError(update.err);
1324
+ };
1325
+
1326
+ private handleDataPacket = (userPacket: UserPacket, kind: DataPacket_Kind) => {
1327
+ // find the participant
1328
+ const participant = this.participants.get(userPacket.participantSid);
1329
+
1330
+ this.emit(RoomEvent.DataReceived, userPacket.payload, participant, kind, userPacket.topic);
1331
+
1332
+ // also emit on the participant
1333
+ participant?.emit(ParticipantEvent.DataReceived, userPacket.payload, kind);
1334
+ };
1335
+
1336
+ private handleAudioPlaybackStarted = () => {
1337
+ if (this.canPlaybackAudio) {
1338
+ return;
1339
+ }
1340
+ this.audioEnabled = true;
1341
+ this.emit(RoomEvent.AudioPlaybackStatusChanged, true);
1342
+ };
1343
+
1344
+ private handleAudioPlaybackFailed = (e: any) => {
1345
+ log.warn('could not playback audio', e);
1346
+ if (!this.canPlaybackAudio) {
1347
+ return;
1348
+ }
1349
+ this.audioEnabled = false;
1350
+ this.emit(RoomEvent.AudioPlaybackStatusChanged, false);
1351
+ };
1352
+
1353
+ private handleDeviceChange = async () => {
1354
+ this.emit(RoomEvent.MediaDevicesChanged);
1355
+ };
1356
+
1357
+ private handleRoomUpdate = (room: RoomModel) => {
1358
+ const oldRoom = this.roomInfo;
1359
+ this.roomInfo = room;
1360
+ if (oldRoom && oldRoom.metadata !== room.metadata) {
1361
+ this.emitWhenConnected(RoomEvent.RoomMetadataChanged, room.metadata);
1362
+ }
1363
+ if (oldRoom?.activeRecording !== room.activeRecording) {
1364
+ this.emitWhenConnected(RoomEvent.RecordingStatusChanged, room.activeRecording);
1365
+ }
1366
+ };
1367
+
1368
+ private handleConnectionQualityUpdate = (update: ConnectionQualityUpdate) => {
1369
+ update.updates.forEach((info) => {
1370
+ if (info.participantSid === this.localParticipant.sid) {
1371
+ this.localParticipant.setConnectionQuality(info.quality);
1372
+ return;
1373
+ }
1374
+ const participant = this.participants.get(info.participantSid);
1375
+ if (participant) {
1376
+ participant.setConnectionQuality(info.quality);
1377
+ }
1378
+ });
1379
+ };
1380
+
1381
+ private async acquireAudioContext() {
1382
+ if (
1383
+ typeof this.options.expWebAudioMix !== 'boolean' &&
1384
+ this.options.expWebAudioMix.audioContext
1385
+ ) {
1386
+ // override audio context with custom audio context if supplied by user
1387
+ this.audioContext = this.options.expWebAudioMix.audioContext;
1388
+ } else if (!this.audioContext || this.audioContext.state === 'closed') {
1389
+ // by using an AudioContext, it reduces lag on audio elements
1390
+ // https://stackoverflow.com/questions/9811429/html5-audio-tag-on-safari-has-a-delay/54119854#54119854
1391
+ this.audioContext = getNewAudioContext() ?? undefined;
1392
+ }
1393
+
1394
+ if (this.audioContext && this.audioContext.state === 'suspended') {
1395
+ // for iOS a newly created AudioContext is always in `suspended` state.
1396
+ // we try our best to resume the context here, if that doesn't work, we just continue with regular processing
1397
+ try {
1398
+ await this.audioContext.resume();
1399
+ } catch (e: any) {
1400
+ log.warn(e);
1401
+ }
1402
+ }
1403
+
1404
+ if (this.options.expWebAudioMix) {
1405
+ this.participants.forEach((participant) => participant.setAudioContext(this.audioContext));
1406
+ }
1407
+
1408
+ const newContextIsRunning = this.audioContext?.state === 'running';
1409
+ if (newContextIsRunning !== this.canPlaybackAudio) {
1410
+ this.audioEnabled = newContextIsRunning;
1411
+ this.emit(RoomEvent.AudioPlaybackStatusChanged, newContextIsRunning);
1412
+ }
1413
+ }
1414
+
1415
+ private createParticipant(id: string, info?: ParticipantInfo): RemoteParticipant {
1416
+ let participant: RemoteParticipant;
1417
+ if (info) {
1418
+ participant = RemoteParticipant.fromParticipantInfo(this.engine.client, info);
1419
+ } else {
1420
+ participant = new RemoteParticipant(this.engine.client, id, '', undefined, undefined);
1421
+ }
1422
+ if (this.options.expWebAudioMix) {
1423
+ participant.setAudioContext(this.audioContext);
1424
+ }
1425
+ return participant;
1426
+ }
1427
+
1428
+ private getOrCreateParticipant(id: string, info?: ParticipantInfo): RemoteParticipant {
1429
+ if (this.participants.has(id)) {
1430
+ return this.participants.get(id) as RemoteParticipant;
1431
+ }
1432
+ // it's possible for the RTC track to arrive before signaling data
1433
+ // when this happens, we'll create the participant and make the track work
1434
+ const participant = this.createParticipant(id, info);
1435
+ this.participants.set(id, participant);
1436
+ if (info) {
1437
+ this.identityToSid.set(info.identity, info.sid);
1438
+ // if we have valid info and the participant wasn't in the map before, we can assume the participant is new
1439
+ // firing here to make sure that `ParticipantConnected` fires before the initial track events
1440
+ this.emitWhenConnected(RoomEvent.ParticipantConnected, participant);
1441
+ }
1442
+
1443
+ // also forward events
1444
+ // trackPublished is only fired for tracks added after both local participant
1445
+ // and remote participant joined the room
1446
+ participant
1447
+ .on(ParticipantEvent.TrackPublished, (trackPublication: RemoteTrackPublication) => {
1448
+ this.emitWhenConnected(RoomEvent.TrackPublished, trackPublication, participant);
1449
+ })
1450
+ .on(
1451
+ ParticipantEvent.TrackSubscribed,
1452
+ (track: RemoteTrack, publication: RemoteTrackPublication) => {
1453
+ // monitor playback status
1454
+ if (track.kind === Track.Kind.Audio) {
1455
+ track.on(TrackEvent.AudioPlaybackStarted, this.handleAudioPlaybackStarted);
1456
+ track.on(TrackEvent.AudioPlaybackFailed, this.handleAudioPlaybackFailed);
1457
+ }
1458
+ this.emit(RoomEvent.TrackSubscribed, track, publication, participant);
1459
+ },
1460
+ )
1461
+ .on(ParticipantEvent.TrackUnpublished, (publication: RemoteTrackPublication) => {
1462
+ this.emit(RoomEvent.TrackUnpublished, publication, participant);
1463
+ })
1464
+ .on(
1465
+ ParticipantEvent.TrackUnsubscribed,
1466
+ (track: RemoteTrack, publication: RemoteTrackPublication) => {
1467
+ this.emit(RoomEvent.TrackUnsubscribed, track, publication, participant);
1468
+ },
1469
+ )
1470
+ .on(ParticipantEvent.TrackSubscriptionFailed, (sid: string) => {
1471
+ this.emit(RoomEvent.TrackSubscriptionFailed, sid, participant);
1472
+ })
1473
+ .on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
1474
+ this.emitWhenConnected(RoomEvent.TrackMuted, pub, participant);
1475
+ })
1476
+ .on(ParticipantEvent.TrackUnmuted, (pub: TrackPublication) => {
1477
+ this.emitWhenConnected(RoomEvent.TrackUnmuted, pub, participant);
1478
+ })
1479
+ .on(ParticipantEvent.ParticipantMetadataChanged, (metadata: string | undefined) => {
1480
+ this.emitWhenConnected(RoomEvent.ParticipantMetadataChanged, metadata, participant);
1481
+ })
1482
+ .on(ParticipantEvent.ParticipantNameChanged, (name) => {
1483
+ this.emitWhenConnected(RoomEvent.ParticipantNameChanged, name, participant);
1484
+ })
1485
+ .on(ParticipantEvent.ConnectionQualityChanged, (quality: ConnectionQuality) => {
1486
+ this.emitWhenConnected(RoomEvent.ConnectionQualityChanged, quality, participant);
1487
+ })
1488
+ .on(
1489
+ ParticipantEvent.ParticipantPermissionsChanged,
1490
+ (prevPermissions?: ParticipantPermission) => {
1491
+ this.emitWhenConnected(
1492
+ RoomEvent.ParticipantPermissionsChanged,
1493
+ prevPermissions,
1494
+ participant,
1495
+ );
1496
+ },
1497
+ )
1498
+ .on(ParticipantEvent.TrackSubscriptionStatusChanged, (pub, status) => {
1499
+ this.emitWhenConnected(RoomEvent.TrackSubscriptionStatusChanged, pub, status, participant);
1500
+ })
1501
+ .on(ParticipantEvent.TrackSubscriptionFailed, (trackSid, error) => {
1502
+ this.emit(RoomEvent.TrackSubscriptionFailed, trackSid, participant, error);
1503
+ })
1504
+ .on(ParticipantEvent.TrackSubscriptionPermissionChanged, (pub, status) => {
1505
+ this.emitWhenConnected(
1506
+ RoomEvent.TrackSubscriptionPermissionChanged,
1507
+ pub,
1508
+ status,
1509
+ participant,
1510
+ );
1511
+ });
1512
+
1513
+ // update info at the end after callbacks have been set up
1514
+ if (info) {
1515
+ participant.updateInfo(info);
1516
+ }
1517
+ return participant;
1518
+ }
1519
+
1520
+ private sendSyncState() {
1521
+ if (
1522
+ this.engine.subscriber === undefined ||
1523
+ this.engine.subscriber.pc.localDescription === null
1524
+ ) {
1525
+ return;
1526
+ }
1527
+ const previousAnswer = this.engine.subscriber.pc.localDescription;
1528
+ const previousOffer = this.engine.subscriber.pc.remoteDescription;
1529
+
1530
+ /* 1. autosubscribe on, so subscribed tracks = all tracks - unsub tracks,
1531
+ in this case, we send unsub tracks, so server add all tracks to this
1532
+ subscribe pc and unsub special tracks from it.
1533
+ 2. autosubscribe off, we send subscribed tracks.
1534
+ */
1535
+ const autoSubscribe = this.connOptions?.autoSubscribe ?? true;
1536
+ const trackSids = new Array<string>();
1537
+ this.participants.forEach((participant) => {
1538
+ participant.tracks.forEach((track) => {
1539
+ if (track.isDesired !== autoSubscribe) {
1540
+ trackSids.push(track.trackSid);
1541
+ }
1542
+ });
1543
+ });
1544
+
1545
+ this.engine.client.sendSyncState(
1546
+ new SyncState({
1547
+ answer: toProtoSessionDescription({
1548
+ sdp: previousAnswer.sdp,
1549
+ type: previousAnswer.type,
1550
+ }),
1551
+ offer: previousOffer
1552
+ ? toProtoSessionDescription({
1553
+ sdp: previousOffer.sdp,
1554
+ type: previousOffer.type,
1555
+ })
1556
+ : undefined,
1557
+ subscription: new UpdateSubscription({
1558
+ trackSids,
1559
+ subscribe: !autoSubscribe,
1560
+ participantTracks: [],
1561
+ }),
1562
+ publishTracks: this.localParticipant.publishedTracksInfo(),
1563
+ dataChannels: this.localParticipant.dataChannelsInfo(),
1564
+ }),
1565
+ );
1566
+ }
1567
+
1568
+ /**
1569
+ * After resuming, we'll need to notify the server of the current
1570
+ * subscription settings.
1571
+ */
1572
+ private updateSubscriptions() {
1573
+ for (const p of this.participants.values()) {
1574
+ for (const pub of p.videoTracks.values()) {
1575
+ if (pub.isSubscribed && pub instanceof RemoteTrackPublication) {
1576
+ pub.emitTrackUpdate();
1577
+ }
1578
+ }
1579
+ }
1580
+ }
1581
+
1582
+ private registerConnectionReconcile() {
1583
+ this.clearConnectionReconcile();
1584
+ let consecutiveFailures = 0;
1585
+ this.connectionReconcileInterval = CriticalTimers.setInterval(() => {
1586
+ if (
1587
+ // ensure we didn't tear it down
1588
+ !this.engine ||
1589
+ // engine detected close, but Room missed it
1590
+ this.engine.isClosed ||
1591
+ // transports failed without notifying engine
1592
+ !this.engine.verifyTransport()
1593
+ ) {
1594
+ consecutiveFailures++;
1595
+ log.warn('detected connection state mismatch', { numFailures: consecutiveFailures });
1596
+ if (consecutiveFailures >= 3) {
1597
+ this.recreateEngine();
1598
+ this.handleDisconnect(
1599
+ this.options.stopLocalTrackOnUnpublish,
1600
+ DisconnectReason.STATE_MISMATCH,
1601
+ );
1602
+ }
1603
+ } else {
1604
+ consecutiveFailures = 0;
1605
+ }
1606
+ }, connectionReconcileFrequency);
1607
+ }
1608
+
1609
+ private clearConnectionReconcile() {
1610
+ if (this.connectionReconcileInterval) {
1611
+ CriticalTimers.clearInterval(this.connectionReconcileInterval);
1612
+ }
1613
+ }
1614
+
1615
+ private setAndEmitConnectionState(state: ConnectionState): boolean {
1616
+ if (state === this.state) {
1617
+ // unchanged
1618
+ return false;
1619
+ }
1620
+ this.state = state;
1621
+ this.emit(RoomEvent.ConnectionStateChanged, this.state);
1622
+ return true;
1623
+ }
1624
+
1625
+ private emitWhenConnected<E extends keyof RoomEventCallbacks>(
1626
+ event: E,
1627
+ ...args: Parameters<RoomEventCallbacks[E]>
1628
+ ): boolean {
1629
+ if (this.state === ConnectionState.Connected) {
1630
+ return this.emit(event, ...args);
1631
+ }
1632
+ return false;
1633
+ }
1634
+
1635
+ private onLocalParticipantMetadataChanged = (metadata: string | undefined) => {
1636
+ this.emit(RoomEvent.ParticipantMetadataChanged, metadata, this.localParticipant);
1637
+ };
1638
+
1639
+ private onLocalParticipantNameChanged = (name: string) => {
1640
+ this.emit(RoomEvent.ParticipantNameChanged, name, this.localParticipant);
1641
+ };
1642
+
1643
+ private onLocalTrackMuted = (pub: TrackPublication) => {
1644
+ this.emit(RoomEvent.TrackMuted, pub, this.localParticipant);
1645
+ };
1646
+
1647
+ private onLocalTrackUnmuted = (pub: TrackPublication) => {
1648
+ this.emit(RoomEvent.TrackUnmuted, pub, this.localParticipant);
1649
+ };
1650
+
1651
+ private onLocalTrackPublished = async (pub: LocalTrackPublication) => {
1652
+ this.emit(RoomEvent.LocalTrackPublished, pub, this.localParticipant);
1653
+ if (pub.track instanceof LocalAudioTrack) {
1654
+ const trackIsSilent = await pub.track.checkForSilence();
1655
+ if (trackIsSilent) {
1656
+ this.emit(RoomEvent.LocalAudioSilenceDetected, pub);
1657
+ }
1658
+ }
1659
+ const deviceId = await pub.track?.getDeviceId();
1660
+ const deviceKind = sourceToKind(pub.source);
1661
+ if (
1662
+ deviceKind &&
1663
+ deviceId &&
1664
+ deviceId !== this.localParticipant.activeDeviceMap.get(deviceKind)
1665
+ ) {
1666
+ this.localParticipant.activeDeviceMap.set(deviceKind, deviceId);
1667
+ this.emit(RoomEvent.ActiveDeviceChanged, deviceKind, deviceId);
1668
+ }
1669
+ };
1670
+
1671
+ private onLocalTrackUnpublished = (pub: LocalTrackPublication) => {
1672
+ this.emit(RoomEvent.LocalTrackUnpublished, pub, this.localParticipant);
1673
+ };
1674
+
1675
+ private onLocalConnectionQualityChanged = (quality: ConnectionQuality) => {
1676
+ this.emit(RoomEvent.ConnectionQualityChanged, quality, this.localParticipant);
1677
+ };
1678
+
1679
+ private onMediaDevicesError = (e: Error) => {
1680
+ this.emit(RoomEvent.MediaDevicesError, e);
1681
+ };
1682
+
1683
+ private onLocalParticipantPermissionsChanged = (prevPermissions?: ParticipantPermission) => {
1684
+ this.emit(RoomEvent.ParticipantPermissionsChanged, prevPermissions, this.localParticipant);
1685
+ };
1686
+
1687
+ /**
1688
+ * Allows to populate a room with simulated participants.
1689
+ * No actual connection to a server will be established, all state is
1690
+ * @experimental
1691
+ */
1692
+ async simulateParticipants(options: SimulationOptions) {
1693
+ const publishOptions = {
1694
+ audio: true,
1695
+ video: true,
1696
+ useRealTracks: false,
1697
+ ...options.publish,
1698
+ };
1699
+ const participantOptions = {
1700
+ count: 9,
1701
+ audio: false,
1702
+ video: true,
1703
+ aspectRatios: [1.66, 1.7, 1.3],
1704
+ ...options.participants,
1705
+ };
1706
+ this.handleDisconnect();
1707
+ this.roomInfo = new RoomModel({
1708
+ sid: 'RM_SIMULATED',
1709
+ name: 'simulated-room',
1710
+ emptyTimeout: 0,
1711
+ maxParticipants: 0,
1712
+ creationTime: protoInt64.parse(new Date().getTime()),
1713
+ metadata: '',
1714
+ numParticipants: 1,
1715
+ numPublishers: 1,
1716
+ turnPassword: '',
1717
+ enabledCodecs: [],
1718
+ activeRecording: false,
1719
+ });
1720
+
1721
+ this.localParticipant.updateInfo(
1722
+ new ParticipantInfo({
1723
+ identity: 'simulated-local',
1724
+ name: 'local-name',
1725
+ }),
1726
+ );
1727
+ this.setupLocalParticipantEvents();
1728
+ this.emit(RoomEvent.SignalConnected);
1729
+ this.emit(RoomEvent.Connected);
1730
+ this.setAndEmitConnectionState(ConnectionState.Connected);
1731
+ if (publishOptions.video) {
1732
+ const camPub = new LocalTrackPublication(
1733
+ Track.Kind.Video,
1734
+ new TrackInfo({
1735
+ source: TrackSource.CAMERA,
1736
+ sid: Math.floor(Math.random() * 10_000).toString(),
1737
+ type: TrackType.AUDIO,
1738
+ name: 'video-dummy',
1739
+ }),
1740
+ new LocalVideoTrack(
1741
+ publishOptions.useRealTracks
1742
+ ? (
1743
+ await window.navigator.mediaDevices.getUserMedia({ video: true })
1744
+ ).getVideoTracks()[0]
1745
+ : createDummyVideoStreamTrack(
1746
+ 160 * participantOptions.aspectRatios[0] ?? 1,
1747
+ 160,
1748
+ true,
1749
+ true,
1750
+ ),
1751
+ ),
1752
+ );
1753
+ // @ts-ignore
1754
+ this.localParticipant.addTrackPublication(camPub);
1755
+ this.localParticipant.emit(ParticipantEvent.LocalTrackPublished, camPub);
1756
+ }
1757
+ if (publishOptions.audio) {
1758
+ const audioPub = new LocalTrackPublication(
1759
+ Track.Kind.Audio,
1760
+ new TrackInfo({
1761
+ source: TrackSource.MICROPHONE,
1762
+ sid: Math.floor(Math.random() * 10_000).toString(),
1763
+ type: TrackType.AUDIO,
1764
+ }),
1765
+ new LocalAudioTrack(
1766
+ publishOptions.useRealTracks
1767
+ ? (await navigator.mediaDevices.getUserMedia({ audio: true })).getAudioTracks()[0]
1768
+ : getEmptyAudioStreamTrack(),
1769
+ ),
1770
+ );
1771
+ // @ts-ignore
1772
+ this.localParticipant.addTrackPublication(audioPub);
1773
+ this.localParticipant.emit(ParticipantEvent.LocalTrackPublished, audioPub);
1774
+ }
1775
+
1776
+ for (let i = 0; i < participantOptions.count - 1; i += 1) {
1777
+ let info: ParticipantInfo = new ParticipantInfo({
1778
+ sid: Math.floor(Math.random() * 10_000).toString(),
1779
+ identity: `simulated-${i}`,
1780
+ state: ParticipantInfo_State.ACTIVE,
1781
+ tracks: [],
1782
+ joinedAt: protoInt64.parse(Date.now()),
1783
+ });
1784
+ const p = this.getOrCreateParticipant(info.identity, info);
1785
+ if (participantOptions.video) {
1786
+ const dummyVideo = createDummyVideoStreamTrack(
1787
+ 160 * participantOptions.aspectRatios[i % participantOptions.aspectRatios.length] ?? 1,
1788
+ 160,
1789
+ false,
1790
+ true,
1791
+ );
1792
+ const videoTrack = new TrackInfo({
1793
+ source: TrackSource.CAMERA,
1794
+ sid: Math.floor(Math.random() * 10_000).toString(),
1795
+ type: TrackType.AUDIO,
1796
+ });
1797
+ p.addSubscribedMediaTrack(dummyVideo, videoTrack.sid, new MediaStream([dummyVideo]));
1798
+ info.tracks = [...info.tracks, videoTrack];
1799
+ }
1800
+ if (participantOptions.audio) {
1801
+ const dummyTrack = getEmptyAudioStreamTrack();
1802
+ const audioTrack = new TrackInfo({
1803
+ source: TrackSource.MICROPHONE,
1804
+ sid: Math.floor(Math.random() * 10_000).toString(),
1805
+ type: TrackType.AUDIO,
1806
+ });
1807
+ p.addSubscribedMediaTrack(dummyTrack, audioTrack.sid, new MediaStream([dummyTrack]));
1808
+ info.tracks = [...info.tracks, audioTrack];
1809
+ }
1810
+
1811
+ p.updateInfo(info);
1812
+ }
1813
+ }
1814
+
1815
+ // /** @internal */
1816
+ emit<E extends keyof RoomEventCallbacks>(
1817
+ event: E,
1818
+ ...args: Parameters<RoomEventCallbacks[E]>
1819
+ ): boolean {
1820
+ // active speaker updates are too spammy
1821
+ if (event !== RoomEvent.ActiveSpeakersChanged) {
1822
+ log.debug(`room event ${event}`, { event, args });
1823
+ }
1824
+ return super.emit(event, ...args);
1825
+ }
1826
+ }
1827
+
1828
+ export default Room;
1829
+
1830
+ export type RoomEventCallbacks = {
1831
+ connected: () => void;
1832
+ reconnecting: () => void;
1833
+ reconnected: () => void;
1834
+ disconnected: (reason?: DisconnectReason) => void;
1835
+ /** @deprecated stateChanged has been renamed to connectionStateChanged */
1836
+ stateChanged: (state: ConnectionState) => void;
1837
+ connectionStateChanged: (state: ConnectionState) => void;
1838
+ mediaDevicesChanged: () => void;
1839
+ participantConnected: (participant: RemoteParticipant) => void;
1840
+ participantDisconnected: (participant: RemoteParticipant) => void;
1841
+ trackPublished: (publication: RemoteTrackPublication, participant: RemoteParticipant) => void;
1842
+ trackSubscribed: (
1843
+ track: RemoteTrack,
1844
+ publication: RemoteTrackPublication,
1845
+ participant: RemoteParticipant,
1846
+ ) => void;
1847
+ trackSubscriptionFailed: (
1848
+ trackSid: string,
1849
+ participant: RemoteParticipant,
1850
+ reason?: SubscriptionError,
1851
+ ) => void;
1852
+ trackUnpublished: (publication: RemoteTrackPublication, participant: RemoteParticipant) => void;
1853
+ trackUnsubscribed: (
1854
+ track: RemoteTrack,
1855
+ publication: RemoteTrackPublication,
1856
+ participant: RemoteParticipant,
1857
+ ) => void;
1858
+ trackMuted: (publication: TrackPublication, participant: Participant) => void;
1859
+ trackUnmuted: (publication: TrackPublication, participant: Participant) => void;
1860
+ localTrackPublished: (publication: LocalTrackPublication, participant: LocalParticipant) => void;
1861
+ localTrackUnpublished: (
1862
+ publication: LocalTrackPublication,
1863
+ participant: LocalParticipant,
1864
+ ) => void;
1865
+ localAudioSilenceDetected: (publication: LocalTrackPublication) => void;
1866
+ participantMetadataChanged: (
1867
+ metadata: string | undefined,
1868
+ participant: RemoteParticipant | LocalParticipant,
1869
+ ) => void;
1870
+ participantNameChanged: (name: string, participant: RemoteParticipant | LocalParticipant) => void;
1871
+ participantPermissionsChanged: (
1872
+ prevPermissions: ParticipantPermission | undefined,
1873
+ participant: RemoteParticipant | LocalParticipant,
1874
+ ) => void;
1875
+ activeSpeakersChanged: (speakers: Array<Participant>) => void;
1876
+ roomMetadataChanged: (metadata: string) => void;
1877
+ dataReceived: (
1878
+ payload: Uint8Array,
1879
+ participant?: RemoteParticipant,
1880
+ kind?: DataPacket_Kind,
1881
+ topic?: string,
1882
+ ) => void;
1883
+ connectionQualityChanged: (quality: ConnectionQuality, participant: Participant) => void;
1884
+ mediaDevicesError: (error: Error) => void;
1885
+ trackStreamStateChanged: (
1886
+ publication: RemoteTrackPublication,
1887
+ streamState: Track.StreamState,
1888
+ participant: RemoteParticipant,
1889
+ ) => void;
1890
+ trackSubscriptionPermissionChanged: (
1891
+ publication: RemoteTrackPublication,
1892
+ status: TrackPublication.PermissionStatus,
1893
+ participant: RemoteParticipant,
1894
+ ) => void;
1895
+ trackSubscriptionStatusChanged: (
1896
+ publication: RemoteTrackPublication,
1897
+ status: TrackPublication.SubscriptionStatus,
1898
+ participant: RemoteParticipant,
1899
+ ) => void;
1900
+ audioPlaybackChanged: (playing: boolean) => void;
1901
+ signalConnected: () => void;
1902
+ recordingStatusChanged: (recording: boolean) => void;
1903
+ participantEncryptionStatusChanged: (encrypted: boolean, participant?: Participant) => void;
1904
+ encryptionError: (error: Error) => void;
1905
+ dcBufferStatusChanged: (isLow: boolean, kind: DataPacket_Kind) => void;
1906
+ activeDeviceChanged: (kind: MediaDeviceKind, deviceId: string) => void;
1907
+ };