livekit-client 2.23.0 → 2.24.0

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 (869) hide show
  1. package/README.md +48 -32
  2. package/dist/core/_virtual/_rollupPluginBabelHelpers.mjs +48 -0
  3. package/dist/core/_virtual/_rollupPluginBabelHelpers.mjs.map +1 -0
  4. package/dist/core/api/SignalClient.mjs +1212 -0
  5. package/dist/core/api/SignalClient.mjs.map +1 -0
  6. package/dist/core/api/SignalClientStateMachine.mjs +115 -0
  7. package/dist/core/api/SignalClientStateMachine.mjs.map +1 -0
  8. package/dist/core/api/WebSocketStream.mjs +146 -0
  9. package/dist/core/api/WebSocketStream.mjs.map +1 -0
  10. package/dist/core/api/utils.mjs +66 -0
  11. package/dist/core/api/utils.mjs.map +1 -0
  12. package/dist/core/connectionHelper/ConnectionCheck.mjs +89 -0
  13. package/dist/core/connectionHelper/ConnectionCheck.mjs.map +1 -0
  14. package/dist/core/connectionHelper/checks/Checker.mjs +146 -0
  15. package/dist/core/connectionHelper/checks/Checker.mjs.map +1 -0
  16. package/dist/core/connectionHelper/checks/cloudRegion.mjs +82 -0
  17. package/dist/core/connectionHelper/checks/cloudRegion.mjs.map +1 -0
  18. package/dist/core/connectionHelper/checks/connectionProtocol.mjs +111 -0
  19. package/dist/core/connectionHelper/checks/connectionProtocol.mjs.map +1 -0
  20. package/dist/core/connectionHelper/checks/publishAudio.mjs +35 -0
  21. package/dist/core/connectionHelper/checks/publishAudio.mjs.map +1 -0
  22. package/dist/core/connectionHelper/checks/publishVideo.mjs +82 -0
  23. package/dist/core/connectionHelper/checks/publishVideo.mjs.map +1 -0
  24. package/dist/core/connectionHelper/checks/reconnect.mjs +37 -0
  25. package/dist/core/connectionHelper/checks/reconnect.mjs.map +1 -0
  26. package/dist/core/connectionHelper/checks/turn.mjs +56 -0
  27. package/dist/core/connectionHelper/checks/turn.mjs.map +1 -0
  28. package/dist/core/connectionHelper/checks/webrtc.mjs +73 -0
  29. package/dist/core/connectionHelper/checks/webrtc.mjs.map +1 -0
  30. package/dist/core/connectionHelper/checks/websocket.mjs +47 -0
  31. package/dist/core/connectionHelper/checks/websocket.mjs.map +1 -0
  32. package/dist/core/core.mjs +1 -0
  33. package/dist/core/core.mjs.map +1 -0
  34. package/dist/core/e2ee/E2eeManager.mjs +603 -0
  35. package/dist/core/e2ee/E2eeManager.mjs.map +1 -0
  36. package/dist/core/e2ee/KeyProvider.mjs +95 -0
  37. package/dist/core/e2ee/KeyProvider.mjs.map +1 -0
  38. package/dist/core/e2ee/constants.mjs +19 -0
  39. package/dist/core/e2ee/constants.mjs.map +1 -0
  40. package/dist/core/e2ee/errors.mjs +15 -0
  41. package/dist/core/e2ee/errors.mjs.map +1 -0
  42. package/dist/core/e2ee/events.mjs +24 -0
  43. package/dist/core/e2ee/events.mjs.map +1 -0
  44. package/dist/core/e2ee/extension.mjs +70 -0
  45. package/dist/core/e2ee/extension.mjs.map +1 -0
  46. package/dist/core/e2ee/utils.mjs +168 -0
  47. package/dist/core/e2ee/utils.mjs.map +1 -0
  48. package/dist/core/frameMetadata/FrameMetadataManager.mjs +205 -0
  49. package/dist/core/frameMetadata/FrameMetadataManager.mjs.map +1 -0
  50. package/dist/core/frameMetadata/extension.mjs +17 -0
  51. package/dist/core/frameMetadata/extension.mjs.map +1 -0
  52. package/dist/core/frameMetadata/utils.mjs +42 -0
  53. package/dist/core/frameMetadata/utils.mjs.map +1 -0
  54. package/dist/core/logger.mjs +111 -0
  55. package/dist/core/logger.mjs.map +1 -0
  56. package/dist/core/package.json.mjs +1 -0
  57. package/dist/core/package.json.mjs.map +1 -0
  58. package/dist/core/room/BackOffStrategy.mjs +60 -0
  59. package/dist/core/room/BackOffStrategy.mjs.map +1 -0
  60. package/dist/core/room/CoreRoom.mjs +2279 -0
  61. package/dist/core/room/CoreRoom.mjs.map +1 -0
  62. package/dist/core/room/DefaultReconnectPolicy.mjs +23 -0
  63. package/dist/core/room/DefaultReconnectPolicy.mjs.map +1 -0
  64. package/dist/core/room/DeviceManager.mjs +92 -0
  65. package/dist/core/room/DeviceManager.mjs.map +1 -0
  66. package/dist/core/room/PCTransport.mjs +1110 -0
  67. package/dist/core/room/PCTransport.mjs.map +1 -0
  68. package/dist/core/room/PCTransportManager.mjs +360 -0
  69. package/dist/core/room/PCTransportManager.mjs.map +1 -0
  70. package/dist/core/room/RTCEngine.mjs +1830 -0
  71. package/dist/core/room/RTCEngine.mjs.map +1 -0
  72. package/dist/core/room/RegionUrlProvider.mjs +194 -0
  73. package/dist/core/room/RegionUrlProvider.mjs.map +1 -0
  74. package/dist/core/room/attribute-typings.mjs +24 -0
  75. package/dist/core/room/attribute-typings.mjs.map +1 -0
  76. package/dist/core/room/createRoom.mjs +19 -0
  77. package/dist/core/room/createRoom.mjs.map +1 -0
  78. package/dist/core/room/data-channel/DataChannelManager.mjs +188 -0
  79. package/dist/core/room/data-channel/DataChannelManager.mjs.map +1 -0
  80. package/dist/core/room/data-channel/FlowControlledDataChannel.mjs +185 -0
  81. package/dist/core/room/data-channel/FlowControlledDataChannel.mjs.map +1 -0
  82. package/dist/core/room/data-channel/LossyDataChannel.mjs +99 -0
  83. package/dist/core/room/data-channel/LossyDataChannel.mjs.map +1 -0
  84. package/dist/core/room/data-channel/ReliableDataChannel.mjs +150 -0
  85. package/dist/core/room/data-channel/ReliableDataChannel.mjs.map +1 -0
  86. package/dist/core/room/data-channel/types.mjs +22 -0
  87. package/dist/core/room/data-channel/types.mjs.map +1 -0
  88. package/dist/core/room/data-stream/compression.mjs +68 -0
  89. package/dist/core/room/data-stream/compression.mjs.map +1 -0
  90. package/dist/core/room/data-stream/constants.mjs +26 -0
  91. package/dist/core/room/data-stream/constants.mjs.map +1 -0
  92. package/dist/core/room/data-stream/extension.mjs +42 -0
  93. package/dist/core/room/data-stream/extension.mjs.map +1 -0
  94. package/dist/core/room/data-stream/incoming/IncomingDataStreamManager.mjs +500 -0
  95. package/dist/core/room/data-stream/incoming/IncomingDataStreamManager.mjs.map +1 -0
  96. package/dist/core/room/data-stream/incoming/StreamReader.mjs +279 -0
  97. package/dist/core/room/data-stream/incoming/StreamReader.mjs.map +1 -0
  98. package/dist/core/room/data-stream/outgoing/OutgoingDataStreamManager.mjs +543 -0
  99. package/dist/core/room/data-stream/outgoing/OutgoingDataStreamManager.mjs.map +1 -0
  100. package/dist/core/room/data-stream/outgoing/StreamWriter.mjs +21 -0
  101. package/dist/core/room/data-stream/outgoing/StreamWriter.mjs.map +1 -0
  102. package/dist/core/room/data-stream/outgoing/header-utils.mjs +53 -0
  103. package/dist/core/room/data-stream/outgoing/header-utils.mjs.map +1 -0
  104. package/dist/core/room/data-track/LocalDataTrack.mjs +146 -0
  105. package/dist/core/room/data-track/LocalDataTrack.mjs.map +1 -0
  106. package/dist/core/room/data-track/RemoteDataTrack.mjs +51 -0
  107. package/dist/core/room/data-track/RemoteDataTrack.mjs.map +1 -0
  108. package/dist/core/room/data-track/depacketizer.mjs +193 -0
  109. package/dist/core/room/data-track/depacketizer.mjs.map +1 -0
  110. package/dist/core/room/data-track/extension.mjs +154 -0
  111. package/dist/core/room/data-track/extension.mjs.map +1 -0
  112. package/dist/core/room/data-track/frame.mjs +19 -0
  113. package/dist/core/room/data-track/frame.mjs.map +1 -0
  114. package/dist/core/room/data-track/handle.mjs +50 -0
  115. package/dist/core/room/data-track/handle.mjs.map +1 -0
  116. package/dist/core/room/data-track/incoming/IncomingDataTrackManager.mjs +618 -0
  117. package/dist/core/room/data-track/incoming/IncomingDataTrackManager.mjs.map +1 -0
  118. package/dist/core/room/data-track/incoming/errors.mjs +32 -0
  119. package/dist/core/room/data-track/incoming/errors.mjs.map +1 -0
  120. package/dist/core/room/data-track/incoming/pipeline.mjs +85 -0
  121. package/dist/core/room/data-track/incoming/pipeline.mjs.map +1 -0
  122. package/dist/core/room/data-track/outgoing/OutgoingDataTrackManager.mjs +351 -0
  123. package/dist/core/room/data-track/outgoing/OutgoingDataTrackManager.mjs.map +1 -0
  124. package/dist/core/room/data-track/outgoing/errors.mjs +114 -0
  125. package/dist/core/room/data-track/outgoing/errors.mjs.map +1 -0
  126. package/dist/core/room/data-track/outgoing/pipeline.mjs +41 -0
  127. package/dist/core/room/data-track/outgoing/pipeline.mjs.map +1 -0
  128. package/dist/core/room/data-track/packet/constants.mjs +21 -0
  129. package/dist/core/room/data-track/packet/constants.mjs.map +1 -0
  130. package/dist/core/room/data-track/packet/errors.mjs +56 -0
  131. package/dist/core/room/data-track/packet/errors.mjs.map +1 -0
  132. package/dist/core/room/data-track/packet/extensions.mjs +172 -0
  133. package/dist/core/room/data-track/packet/extensions.mjs.map +1 -0
  134. package/dist/core/room/data-track/packet/index.mjs +242 -0
  135. package/dist/core/room/data-track/packet/index.mjs.map +1 -0
  136. package/dist/core/room/data-track/packet/serializable.mjs +15 -0
  137. package/dist/core/room/data-track/packet/serializable.mjs.map +1 -0
  138. package/dist/core/room/data-track/packetizer.mjs +77 -0
  139. package/dist/core/room/data-track/packetizer.mjs.map +1 -0
  140. package/dist/core/room/data-track/track-interfaces.mjs +2 -0
  141. package/dist/core/room/data-track/track-interfaces.mjs.map +1 -0
  142. package/dist/core/room/data-track/types.mjs +18 -0
  143. package/dist/core/room/data-track/types.mjs.map +1 -0
  144. package/dist/core/room/data-track/utils.mjs +149 -0
  145. package/dist/core/room/data-track/utils.mjs.map +1 -0
  146. package/dist/core/room/debounce.mjs +97 -0
  147. package/dist/core/room/debounce.mjs.map +1 -0
  148. package/dist/core/room/defaults.mjs +34 -0
  149. package/dist/core/room/defaults.mjs.map +1 -0
  150. package/dist/core/room/errors.mjs +208 -0
  151. package/dist/core/room/errors.mjs.map +1 -0
  152. package/dist/core/room/events.mjs +643 -0
  153. package/dist/core/room/events.mjs.map +1 -0
  154. package/dist/core/room/extensions.mjs +48 -0
  155. package/dist/core/room/extensions.mjs.map +1 -0
  156. package/dist/core/room/participant/LocalParticipant.mjs +1324 -0
  157. package/dist/core/room/participant/LocalParticipant.mjs.map +1 -0
  158. package/dist/core/room/participant/Participant.mjs +300 -0
  159. package/dist/core/room/participant/Participant.mjs.map +1 -0
  160. package/dist/core/room/participant/ParticipantTrackPermission.mjs +12 -0
  161. package/dist/core/room/participant/ParticipantTrackPermission.mjs.map +1 -0
  162. package/dist/core/room/participant/RemoteParticipant.mjs +287 -0
  163. package/dist/core/room/participant/RemoteParticipant.mjs.map +1 -0
  164. package/dist/core/room/participant/publishUtils.mjs +356 -0
  165. package/dist/core/room/participant/publishUtils.mjs.map +1 -0
  166. package/dist/core/room/rpc/client/RpcClientManager.mjs +216 -0
  167. package/dist/core/room/rpc/client/RpcClientManager.mjs.map +1 -0
  168. package/dist/core/room/rpc/extension.mjs +82 -0
  169. package/dist/core/room/rpc/extension.mjs.map +1 -0
  170. package/dist/core/room/rpc/server/RpcServerManager.mjs +186 -0
  171. package/dist/core/room/rpc/server/RpcServerManager.mjs.map +1 -0
  172. package/dist/core/room/rpc/utils.mjs +148 -0
  173. package/dist/core/room/rpc/utils.mjs.map +1 -0
  174. package/dist/core/room/simulated-participants/extension.mjs +105 -0
  175. package/dist/core/room/simulated-participants/extension.mjs.map +1 -0
  176. package/dist/core/room/stats.mjs +19 -0
  177. package/dist/core/room/stats.mjs.map +1 -0
  178. package/dist/core/room/statsSummary.mjs +165 -0
  179. package/dist/core/room/statsSummary.mjs.map +1 -0
  180. package/dist/core/room/timers.mjs +20 -0
  181. package/dist/core/room/timers.mjs.map +1 -0
  182. package/dist/core/room/token-source/TokenSource.mjs +230 -0
  183. package/dist/core/room/token-source/TokenSource.mjs.map +1 -0
  184. package/dist/core/room/token-source/types.mjs +18 -0
  185. package/dist/core/room/token-source/types.mjs.map +1 -0
  186. package/dist/core/room/token-source/utils.mjs +76 -0
  187. package/dist/core/room/token-source/utils.mjs.map +1 -0
  188. package/dist/core/room/track/FrameMetadataExtractor.mjs +36 -0
  189. package/dist/core/room/track/FrameMetadataExtractor.mjs.map +1 -0
  190. package/dist/core/room/track/LocalAudioTrack.mjs +327 -0
  191. package/dist/core/room/track/LocalAudioTrack.mjs.map +1 -0
  192. package/dist/core/room/track/LocalTrack.mjs +694 -0
  193. package/dist/core/room/track/LocalTrack.mjs.map +1 -0
  194. package/dist/core/room/track/LocalTrackPublication.mjs +111 -0
  195. package/dist/core/room/track/LocalTrackPublication.mjs.map +1 -0
  196. package/dist/core/room/track/LocalVideoTrack.mjs +810 -0
  197. package/dist/core/room/track/LocalVideoTrack.mjs.map +1 -0
  198. package/dist/core/room/track/RemoteAudioTrack.mjs +245 -0
  199. package/dist/core/room/track/RemoteAudioTrack.mjs.map +1 -0
  200. package/dist/core/room/track/RemoteTrack.mjs +143 -0
  201. package/dist/core/room/track/RemoteTrack.mjs.map +1 -0
  202. package/dist/core/room/track/RemoteTrackPublication.mjs +284 -0
  203. package/dist/core/room/track/RemoteTrackPublication.mjs.map +1 -0
  204. package/dist/core/room/track/RemoteVideoTrack.mjs +384 -0
  205. package/dist/core/room/track/RemoteVideoTrack.mjs.map +1 -0
  206. package/dist/core/room/track/Track.mjs +413 -0
  207. package/dist/core/room/track/Track.mjs.map +1 -0
  208. package/dist/core/room/track/TrackPublication.mjs +97 -0
  209. package/dist/core/room/track/TrackPublication.mjs.map +1 -0
  210. package/dist/core/room/track/create.mjs +142 -0
  211. package/dist/core/room/track/create.mjs.map +1 -0
  212. package/dist/core/room/track/facingMode.mjs +81 -0
  213. package/dist/core/room/track/facingMode.mjs.map +1 -0
  214. package/dist/core/room/track/options.mjs +109 -0
  215. package/dist/core/room/track/options.mjs.map +1 -0
  216. package/dist/core/room/track/record.mjs +67 -0
  217. package/dist/core/room/track/record.mjs.map +1 -0
  218. package/dist/core/room/track/utils.mjs +429 -0
  219. package/dist/core/room/track/utils.mjs.map +1 -0
  220. package/dist/core/room/utils.mjs +852 -0
  221. package/dist/core/room/utils.mjs.map +1 -0
  222. package/dist/core/room/video/create.mjs +71 -0
  223. package/dist/core/room/video/create.mjs.map +1 -0
  224. package/dist/core/room/video/extension.mjs +27 -0
  225. package/dist/core/room/video/extension.mjs.map +1 -0
  226. package/dist/core/room/video/publisher.mjs +343 -0
  227. package/dist/core/room/video/publisher.mjs.map +1 -0
  228. package/dist/core/ts4.2/api/SignalClient.d.ts +19 -0
  229. package/dist/core/ts4.2/api/SignalClientStateMachine.d.ts +84 -0
  230. package/dist/core/ts4.2/api/WebSocketStream.d.ts +33 -0
  231. package/dist/core/ts4.2/api/utils.d.ts +8 -0
  232. package/dist/core/ts4.2/connectionHelper/ConnectionCheck.d.ts +29 -0
  233. package/dist/core/ts4.2/connectionHelper/checks/Checker.d.ts +64 -0
  234. package/dist/core/ts4.2/connectionHelper/checks/cloudRegion.d.ts +17 -0
  235. package/dist/core/ts4.2/connectionHelper/checks/connectionProtocol.d.ts +19 -0
  236. package/dist/core/ts4.2/connectionHelper/checks/publishAudio.d.ts +5 -0
  237. package/dist/core/ts4.2/connectionHelper/checks/publishVideo.d.ts +6 -0
  238. package/dist/core/ts4.2/connectionHelper/checks/reconnect.d.ts +5 -0
  239. package/dist/core/ts4.2/connectionHelper/checks/turn.d.ts +5 -0
  240. package/dist/core/ts4.2/connectionHelper/checks/webrtc.d.ts +5 -0
  241. package/dist/core/ts4.2/connectionHelper/checks/websocket.d.ts +5 -0
  242. package/dist/core/ts4.2/core.d.ts +33 -0
  243. package/dist/core/ts4.2/e2ee/E2eeManager.d.ts +89 -0
  244. package/dist/core/ts4.2/e2ee/KeyProvider.d.ts +53 -0
  245. package/dist/core/ts4.2/e2ee/constants.d.ts +20 -0
  246. package/dist/core/ts4.2/e2ee/errors.d.ts +11 -0
  247. package/dist/core/ts4.2/e2ee/events.d.ts +38 -0
  248. package/dist/core/ts4.2/e2ee/extension.d.ts +19 -0
  249. package/dist/core/ts4.2/e2ee/index.d.ts +5 -0
  250. package/dist/core/ts4.2/e2ee/types.d.ts +244 -0
  251. package/dist/core/ts4.2/e2ee/utils.d.ts +36 -0
  252. package/dist/core/ts4.2/exports.d.ts +82 -0
  253. package/dist/core/ts4.2/frameMetadata/FrameMetadataManager.d.ts +52 -0
  254. package/dist/core/ts4.2/frameMetadata/extension.d.ts +12 -0
  255. package/dist/core/ts4.2/frameMetadata/frameMetadata.d.ts +32 -0
  256. package/dist/core/ts4.2/frameMetadata/types.d.ts +61 -0
  257. package/dist/core/ts4.2/frameMetadata/utils.d.ts +13 -0
  258. package/dist/core/ts4.2/logger.d.ts +59 -0
  259. package/dist/core/ts4.2/options.d.ts +171 -0
  260. package/dist/core/ts4.2/room/BackOffStrategy.d.ts +26 -0
  261. package/dist/core/ts4.2/room/CoreRoom.d.ts +363 -0
  262. package/dist/core/ts4.2/room/DefaultReconnectPolicy.d.ts +7 -0
  263. package/dist/core/ts4.2/room/DeviceManager.d.ts +11 -0
  264. package/dist/core/ts4.2/room/PCTransport.d.ts +132 -0
  265. package/dist/core/ts4.2/room/PCTransportManager.d.ts +80 -0
  266. package/dist/core/ts4.2/room/RTCEngine.d.ts +233 -0
  267. package/dist/core/ts4.2/room/ReconnectPolicy.d.ts +23 -0
  268. package/dist/core/ts4.2/room/RegionUrlProvider.d.ts +33 -0
  269. package/dist/core/ts4.2/room/attribute-typings.d.ts +34 -0
  270. package/dist/core/ts4.2/room/chat/extension.d.ts +21 -0
  271. package/dist/core/ts4.2/room/createRoom.d.ts +19 -0
  272. package/dist/core/ts4.2/room/data-channel/DataChannelManager.d.ts +67 -0
  273. package/dist/core/ts4.2/room/data-channel/FlowControlledDataChannel.d.ts +106 -0
  274. package/dist/core/ts4.2/room/data-channel/LossyDataChannel.d.ts +40 -0
  275. package/dist/core/ts4.2/room/data-channel/ReliableDataChannel.d.ts +56 -0
  276. package/dist/core/ts4.2/room/data-channel/types.d.ts +11 -0
  277. package/dist/core/ts4.2/room/data-stream/compression.d.ts +29 -0
  278. package/dist/core/ts4.2/room/data-stream/constants.d.ts +1 -0
  279. package/dist/core/ts4.2/room/data-stream/extension.d.ts +63 -0
  280. package/dist/core/ts4.2/room/data-stream/incoming/IncomingDataStreamManager.d.ts +33 -0
  281. package/dist/core/ts4.2/room/data-stream/incoming/StreamReader.d.ts +77 -0
  282. package/dist/core/ts4.2/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +67 -0
  283. package/dist/core/ts4.2/room/data-stream/outgoing/StreamWriter.d.ts +15 -0
  284. package/dist/core/ts4.2/room/data-stream/outgoing/header-utils.d.ts +17 -0
  285. package/dist/core/ts4.2/room/data-track/LocalDataTrack.d.ts +69 -0
  286. package/dist/core/ts4.2/room/data-track/RemoteDataTrack.d.ts +43 -0
  287. package/dist/core/ts4.2/room/data-track/depacketizer.d.ts +58 -0
  288. package/dist/core/ts4.2/room/data-track/extension.d.ts +30 -0
  289. package/dist/core/ts4.2/room/data-track/frame.d.ts +21 -0
  290. package/dist/core/ts4.2/room/data-track/handle.d.ts +26 -0
  291. package/dist/core/ts4.2/room/data-track/incoming/IncomingDataTrackManager.d.ts +95 -0
  292. package/dist/core/ts4.2/room/data-track/incoming/errors.d.ts +23 -0
  293. package/dist/core/ts4.2/room/data-track/incoming/pipeline.d.ts +40 -0
  294. package/dist/core/ts4.2/room/data-track/incoming/types.d.ts +19 -0
  295. package/dist/core/ts4.2/room/data-track/outgoing/OutgoingDataTrackManager.d.ts +64 -0
  296. package/dist/core/ts4.2/room/data-track/outgoing/errors.d.ts +73 -0
  297. package/dist/core/ts4.2/room/data-track/outgoing/pipeline.d.ts +22 -0
  298. package/dist/core/ts4.2/room/data-track/outgoing/types.d.ts +47 -0
  299. package/dist/core/ts4.2/room/data-track/packet/constants.d.ts +19 -0
  300. package/dist/core/ts4.2/room/data-track/packet/errors.d.ts +39 -0
  301. package/dist/core/ts4.2/room/data-track/packet/extensions.d.ts +68 -0
  302. package/dist/core/ts4.2/room/data-track/packet/index.d.ts +97 -0
  303. package/dist/core/ts4.2/room/data-track/packet/serializable.d.ts +12 -0
  304. package/dist/core/ts4.2/room/data-track/packetizer.d.ts +40 -0
  305. package/dist/core/ts4.2/room/data-track/track-interfaces.d.ts +22 -0
  306. package/dist/core/ts4.2/room/data-track/types.d.ts +21 -0
  307. package/dist/core/ts4.2/room/data-track/utils.d.ts +59 -0
  308. package/dist/core/ts4.2/room/debounce.d.ts +10 -0
  309. package/dist/core/ts4.2/room/defaults.d.ts +7 -0
  310. package/dist/core/ts4.2/room/errors.d.ts +179 -0
  311. package/dist/core/ts4.2/room/events.d.ts +534 -0
  312. package/dist/core/ts4.2/room/extensions.d.ts +206 -0
  313. package/dist/core/ts4.2/room/participant/LocalParticipant.d.ts +164 -0
  314. package/dist/core/ts4.2/room/participant/Participant.d.ts +122 -0
  315. package/dist/core/ts4.2/room/participant/ParticipantTrackPermission.d.ts +25 -0
  316. package/dist/core/ts4.2/room/participant/RemoteParticipant.d.ts +37 -0
  317. package/dist/core/ts4.2/room/participant/publishUtils.d.ts +11 -0
  318. package/dist/core/ts4.2/room/rpc/client/RpcClientManager.d.ts +1 -0
  319. package/dist/core/ts4.2/room/rpc/client/events.d.ts +7 -0
  320. package/dist/core/ts4.2/room/rpc/extension.d.ts +73 -0
  321. package/dist/core/ts4.2/room/rpc/index.d.ts +6 -0
  322. package/dist/core/ts4.2/room/rpc/server/RpcServerManager.d.ts +1 -0
  323. package/dist/core/ts4.2/room/rpc/server/events.d.ts +7 -0
  324. package/dist/core/ts4.2/room/rpc/utils.d.ts +76 -0
  325. package/dist/core/ts4.2/room/simulated-participants/extension.d.ts +24 -0
  326. package/dist/core/ts4.2/room/stats.d.ts +70 -0
  327. package/dist/core/ts4.2/room/statsSummary.d.ts +12 -0
  328. package/dist/core/ts4.2/room/timers.d.ts +11 -0
  329. package/dist/core/ts4.2/room/token-source/TokenSource.d.ts +85 -0
  330. package/dist/core/ts4.2/room/token-source/types.d.ts +69 -0
  331. package/dist/core/ts4.2/room/token-source/utils.d.ts +7 -0
  332. package/dist/core/ts4.2/room/track/FrameMetadataExtractor.d.ts +18 -0
  333. package/dist/core/ts4.2/room/track/LocalAudioTrack.d.ts +50 -0
  334. package/dist/core/ts4.2/room/track/LocalTrack.d.ts +118 -0
  335. package/dist/core/ts4.2/room/track/LocalTrackPublication.d.ts +41 -0
  336. package/dist/core/ts4.2/room/track/LocalVideoTrack.d.ts +91 -0
  337. package/dist/core/ts4.2/room/track/RemoteAudioTrack.d.ts +49 -0
  338. package/dist/core/ts4.2/room/track/RemoteTrack.d.ts +29 -0
  339. package/dist/core/ts4.2/room/track/RemoteTrackPublication.d.ts +62 -0
  340. package/dist/core/ts4.2/room/track/RemoteVideoTrack.d.ts +53 -0
  341. package/dist/core/ts4.2/room/track/Track.d.ts +129 -0
  342. package/dist/core/ts4.2/room/track/TrackPublication.d.ts +68 -0
  343. package/dist/core/ts4.2/room/track/create.d.ts +12 -0
  344. package/dist/core/ts4.2/room/track/facingMode.d.ts +40 -0
  345. package/dist/core/ts4.2/room/track/options.d.ts +396 -0
  346. package/dist/core/ts4.2/room/track/processor/types.d.ts +41 -0
  347. package/dist/core/ts4.2/room/track/record.d.ts +12 -0
  348. package/dist/core/ts4.2/room/track/types.d.ts +27 -0
  349. package/dist/core/ts4.2/room/track/utils.d.ts +20 -0
  350. package/dist/core/ts4.2/room/types.d.ts +126 -0
  351. package/dist/core/ts4.2/room/utils.d.ts +246 -0
  352. package/dist/core/ts4.2/room/video/create.d.ts +27 -0
  353. package/dist/core/ts4.2/room/video/extension.d.ts +43 -0
  354. package/dist/core/ts4.2/room/video/publisher.d.ts +23 -0
  355. package/dist/core/ts4.2/type-polyfills/non-shared-typed-arrays.d.ts +1 -0
  356. package/dist/core/ts4.2/utils/AsyncQueue.d.ts +22 -0
  357. package/dist/core/ts4.2/utils/TypedPromise.d.ts +16 -0
  358. package/dist/core/ts4.2/utils/abort-signal-polyfill.d.ts +12 -0
  359. package/dist/core/ts4.2/utils/browserParser.d.ts +9 -0
  360. package/dist/core/ts4.2/utils/camelToSnakeCase.d.ts +7 -0
  361. package/dist/core/ts4.2/utils/cloneDeep.d.ts +1 -0
  362. package/dist/core/ts4.2/utils/dataPacketBuffer.d.ts +33 -0
  363. package/dist/core/ts4.2/utils/deferrable-map.d.ts +31 -0
  364. package/dist/core/ts4.2/utils/machineInspector.d.ts +53 -0
  365. package/dist/core/ts4.2/utils/serializer.d.ts +47 -0
  366. package/dist/core/ts4.2/utils/throws.d.ts +9 -0
  367. package/dist/core/ts4.2/utils/ttlmap.d.ts +19 -0
  368. package/dist/core/ts4.2/version.d.ts +17 -0
  369. package/dist/core/types/src/api/SignalClient.d.ts +19 -0
  370. package/dist/core/types/src/api/SignalClientStateMachine.d.ts +84 -0
  371. package/dist/core/types/src/api/WebSocketStream.d.ts +33 -0
  372. package/dist/core/types/src/api/utils.d.ts +8 -0
  373. package/dist/core/types/src/connectionHelper/ConnectionCheck.d.ts +28 -0
  374. package/dist/core/types/src/connectionHelper/checks/Checker.d.ts +64 -0
  375. package/dist/core/types/src/connectionHelper/checks/cloudRegion.d.ts +16 -0
  376. package/dist/core/types/src/connectionHelper/checks/connectionProtocol.d.ts +18 -0
  377. package/dist/core/types/src/connectionHelper/checks/publishAudio.d.ts +5 -0
  378. package/dist/core/types/src/connectionHelper/checks/publishVideo.d.ts +6 -0
  379. package/dist/core/types/src/connectionHelper/checks/reconnect.d.ts +5 -0
  380. package/dist/core/types/src/connectionHelper/checks/turn.d.ts +5 -0
  381. package/dist/core/types/src/connectionHelper/checks/webrtc.d.ts +5 -0
  382. package/dist/core/types/src/connectionHelper/checks/websocket.d.ts +5 -0
  383. package/dist/core/types/src/core.d.ts +32 -0
  384. package/dist/core/types/src/e2ee/E2eeManager.d.ts +89 -0
  385. package/dist/core/types/src/e2ee/KeyProvider.d.ts +53 -0
  386. package/dist/core/types/src/e2ee/constants.d.ts +20 -0
  387. package/dist/core/types/src/e2ee/errors.d.ts +11 -0
  388. package/dist/core/types/src/e2ee/events.d.ts +38 -0
  389. package/dist/core/types/src/e2ee/extension.d.ts +18 -0
  390. package/dist/core/types/src/e2ee/index.d.ts +5 -0
  391. package/dist/core/types/src/e2ee/types.d.ts +244 -0
  392. package/dist/core/types/src/e2ee/utils.d.ts +35 -0
  393. package/dist/core/types/src/exports.d.ts +75 -0
  394. package/dist/core/types/src/frameMetadata/FrameMetadataManager.d.ts +52 -0
  395. package/dist/core/types/src/frameMetadata/extension.d.ts +10 -0
  396. package/dist/core/types/src/frameMetadata/frameMetadata.d.ts +32 -0
  397. package/dist/core/types/src/frameMetadata/types.d.ts +61 -0
  398. package/dist/core/types/src/frameMetadata/utils.d.ts +13 -0
  399. package/dist/core/types/src/logger.d.ts +59 -0
  400. package/dist/core/types/src/options.d.ts +171 -0
  401. package/dist/core/types/src/room/BackOffStrategy.d.ts +26 -0
  402. package/dist/core/types/src/room/CoreRoom.d.ts +374 -0
  403. package/dist/core/types/src/room/DefaultReconnectPolicy.d.ts +7 -0
  404. package/dist/core/types/src/room/DeviceManager.d.ts +11 -0
  405. package/dist/core/types/src/room/PCTransport.d.ts +132 -0
  406. package/dist/core/types/src/room/PCTransportManager.d.ts +80 -0
  407. package/dist/core/types/src/room/RTCEngine.d.ts +232 -0
  408. package/dist/core/types/src/room/ReconnectPolicy.d.ts +23 -0
  409. package/dist/core/types/src/room/RegionUrlProvider.d.ts +33 -0
  410. package/dist/core/types/src/room/attribute-typings.d.ts +34 -0
  411. package/dist/core/types/src/room/chat/extension.d.ts +21 -0
  412. package/dist/core/types/src/room/createRoom.d.ts +18 -0
  413. package/dist/core/types/src/room/data-channel/DataChannelManager.d.ts +68 -0
  414. package/dist/core/types/src/room/data-channel/FlowControlledDataChannel.d.ts +107 -0
  415. package/dist/core/types/src/room/data-channel/LossyDataChannel.d.ts +39 -0
  416. package/dist/core/types/src/room/data-channel/ReliableDataChannel.d.ts +55 -0
  417. package/dist/core/types/src/room/data-channel/types.d.ts +11 -0
  418. package/dist/core/types/src/room/data-stream/compression.d.ts +29 -0
  419. package/dist/core/types/src/room/data-stream/constants.d.ts +1 -0
  420. package/dist/core/types/src/room/data-stream/extension.d.ts +62 -0
  421. package/dist/core/types/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts +32 -0
  422. package/dist/core/types/src/room/data-stream/incoming/StreamReader.d.ts +77 -0
  423. package/dist/core/types/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +67 -0
  424. package/dist/core/types/src/room/data-stream/outgoing/StreamWriter.d.ts +15 -0
  425. package/dist/core/types/src/room/data-stream/outgoing/header-utils.d.ts +17 -0
  426. package/dist/core/types/src/room/data-track/LocalDataTrack.d.ts +70 -0
  427. package/dist/core/types/src/room/data-track/RemoteDataTrack.d.ts +43 -0
  428. package/dist/core/types/src/room/data-track/depacketizer.d.ts +58 -0
  429. package/dist/core/types/src/room/data-track/extension.d.ts +30 -0
  430. package/dist/core/types/src/room/data-track/frame.d.ts +21 -0
  431. package/dist/core/types/src/room/data-track/handle.d.ts +26 -0
  432. package/dist/core/types/src/room/data-track/incoming/IncomingDataTrackManager.d.ts +95 -0
  433. package/dist/core/types/src/room/data-track/incoming/errors.d.ts +23 -0
  434. package/dist/core/types/src/room/data-track/incoming/pipeline.d.ts +40 -0
  435. package/dist/core/types/src/room/data-track/incoming/types.d.ts +19 -0
  436. package/dist/core/types/src/room/data-track/outgoing/OutgoingDataTrackManager.d.ts +64 -0
  437. package/dist/core/types/src/room/data-track/outgoing/errors.d.ts +73 -0
  438. package/dist/core/types/src/room/data-track/outgoing/pipeline.d.ts +22 -0
  439. package/dist/core/types/src/room/data-track/outgoing/types.d.ts +47 -0
  440. package/dist/core/types/src/room/data-track/packet/constants.d.ts +19 -0
  441. package/dist/core/types/src/room/data-track/packet/errors.d.ts +39 -0
  442. package/dist/core/types/src/room/data-track/packet/extensions.d.ts +68 -0
  443. package/dist/core/types/src/room/data-track/packet/index.d.ts +91 -0
  444. package/dist/core/types/src/room/data-track/packet/serializable.d.ts +12 -0
  445. package/dist/core/types/src/room/data-track/packetizer.d.ts +40 -0
  446. package/dist/core/types/src/room/data-track/track-interfaces.d.ts +22 -0
  447. package/dist/core/types/src/room/data-track/types.d.ts +21 -0
  448. package/dist/core/types/src/room/data-track/utils.d.ts +59 -0
  449. package/dist/core/types/src/room/debounce.d.ts +10 -0
  450. package/dist/core/types/src/room/defaults.d.ts +7 -0
  451. package/dist/core/types/src/room/errors.d.ts +179 -0
  452. package/dist/core/types/src/room/events.d.ts +534 -0
  453. package/dist/core/types/src/room/extensions.d.ts +205 -0
  454. package/dist/core/types/src/room/participant/LocalParticipant.d.ts +164 -0
  455. package/dist/core/types/src/room/participant/Participant.d.ts +123 -0
  456. package/dist/core/types/src/room/participant/ParticipantTrackPermission.d.ts +25 -0
  457. package/dist/core/types/src/room/participant/RemoteParticipant.d.ts +37 -0
  458. package/dist/core/types/src/room/participant/publishUtils.d.ts +11 -0
  459. package/dist/core/types/src/room/rpc/client/RpcClientManager.d.ts +1 -0
  460. package/dist/core/types/src/room/rpc/client/events.d.ts +7 -0
  461. package/dist/core/types/src/room/rpc/extension.d.ts +69 -0
  462. package/dist/core/types/src/room/rpc/index.d.ts +5 -0
  463. package/dist/core/types/src/room/rpc/server/RpcServerManager.d.ts +1 -0
  464. package/dist/core/types/src/room/rpc/server/events.d.ts +7 -0
  465. package/dist/core/types/src/room/rpc/utils.d.ts +76 -0
  466. package/dist/core/types/src/room/simulated-participants/extension.d.ts +24 -0
  467. package/dist/core/types/src/room/stats.d.ts +70 -0
  468. package/dist/core/types/src/room/statsSummary.d.ts +12 -0
  469. package/dist/core/types/src/room/timers.d.ts +11 -0
  470. package/dist/core/types/src/room/token-source/TokenSource.d.ts +91 -0
  471. package/dist/core/types/src/room/token-source/types.d.ts +69 -0
  472. package/dist/core/types/src/room/token-source/utils.d.ts +7 -0
  473. package/dist/core/types/src/room/track/FrameMetadataExtractor.d.ts +18 -0
  474. package/dist/core/types/src/room/track/LocalAudioTrack.d.ts +53 -0
  475. package/dist/core/types/src/room/track/LocalTrack.d.ts +118 -0
  476. package/dist/core/types/src/room/track/LocalTrackPublication.d.ts +41 -0
  477. package/dist/core/types/src/room/track/LocalVideoTrack.d.ts +91 -0
  478. package/dist/core/types/src/room/track/RemoteAudioTrack.d.ts +49 -0
  479. package/dist/core/types/src/room/track/RemoteTrack.d.ts +29 -0
  480. package/dist/core/types/src/room/track/RemoteTrackPublication.d.ts +76 -0
  481. package/dist/core/types/src/room/track/RemoteVideoTrack.d.ts +56 -0
  482. package/dist/core/types/src/room/track/Track.d.ts +134 -0
  483. package/dist/core/types/src/room/track/TrackPublication.d.ts +74 -0
  484. package/dist/core/types/src/room/track/create.d.ts +12 -0
  485. package/dist/core/types/src/room/track/facingMode.d.ts +40 -0
  486. package/dist/core/types/src/room/track/options.d.ts +384 -0
  487. package/dist/core/types/src/room/track/processor/types.d.ts +41 -0
  488. package/dist/core/types/src/room/track/record.d.ts +12 -0
  489. package/dist/core/types/src/room/track/types.d.ts +27 -0
  490. package/dist/core/types/src/room/track/utils.d.ts +20 -0
  491. package/dist/core/types/src/room/types.d.ts +126 -0
  492. package/dist/core/types/src/room/utils.d.ts +246 -0
  493. package/dist/core/types/src/room/video/create.d.ts +27 -0
  494. package/dist/core/types/src/room/video/extension.d.ts +43 -0
  495. package/dist/core/types/src/room/video/publisher.d.ts +23 -0
  496. package/dist/core/types/src/type-polyfills/non-shared-typed-arrays.d.ts +1 -0
  497. package/dist/core/types/src/utils/AsyncQueue.d.ts +22 -0
  498. package/dist/core/types/src/utils/TypedPromise.d.ts +15 -0
  499. package/dist/core/types/src/utils/abort-signal-polyfill.d.ts +12 -0
  500. package/dist/core/types/src/utils/browserParser.d.ts +9 -0
  501. package/dist/core/types/src/utils/camelToSnakeCase.d.ts +7 -0
  502. package/dist/core/types/src/utils/cloneDeep.d.ts +1 -0
  503. package/dist/core/types/src/utils/dataPacketBuffer.d.ts +33 -0
  504. package/dist/core/types/src/utils/deferrable-map.d.ts +31 -0
  505. package/dist/core/types/src/utils/machineInspector.d.ts +53 -0
  506. package/dist/core/types/src/utils/serializer.d.ts +47 -0
  507. package/dist/core/types/src/utils/throws.d.ts +9 -0
  508. package/dist/core/types/src/utils/ttlmap.d.ts +19 -0
  509. package/dist/core/types/src/version.d.ts +17 -0
  510. package/dist/core/utils/AsyncQueue.mjs +41 -0
  511. package/dist/core/utils/AsyncQueue.mjs.map +1 -0
  512. package/dist/core/utils/TypedPromise.mjs +23 -0
  513. package/dist/core/utils/TypedPromise.mjs.map +1 -0
  514. package/dist/core/utils/abort-signal-polyfill.mjs +53 -0
  515. package/dist/core/utils/abort-signal-polyfill.mjs.map +1 -0
  516. package/dist/core/utils/browserParser.mjs +62 -0
  517. package/dist/core/utils/browserParser.mjs.map +1 -0
  518. package/dist/core/utils/cloneDeep.mjs +14 -0
  519. package/dist/core/utils/cloneDeep.mjs.map +1 -0
  520. package/dist/core/utils/dataPacketBuffer.mjs +76 -0
  521. package/dist/core/utils/dataPacketBuffer.mjs.map +1 -0
  522. package/dist/core/utils/deferrable-map.mjs +94 -0
  523. package/dist/core/utils/deferrable-map.mjs.map +1 -0
  524. package/dist/core/utils/machineInspector.mjs +19 -0
  525. package/dist/core/utils/machineInspector.mjs.map +1 -0
  526. package/dist/core/utils/serializer.mjs +47 -0
  527. package/dist/core/utils/serializer.mjs.map +1 -0
  528. package/dist/core/utils/ttlmap.mjs +97 -0
  529. package/dist/core/utils/ttlmap.mjs.map +1 -0
  530. package/dist/core/version.mjs +17 -0
  531. package/dist/core/version.mjs.map +1 -0
  532. package/dist/livekit-client.e2ee.worker.js +1 -1
  533. package/dist/livekit-client.e2ee.worker.js.map +1 -1
  534. package/dist/livekit-client.e2ee.worker.mjs +157 -131
  535. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  536. package/dist/livekit-client.esm.mjs +31497 -30818
  537. package/dist/livekit-client.esm.mjs.map +1 -1
  538. package/dist/livekit-client.fm.worker.js +1 -1
  539. package/dist/livekit-client.fm.worker.js.map +1 -1
  540. package/dist/livekit-client.fm.worker.mjs +21 -82
  541. package/dist/livekit-client.fm.worker.mjs.map +1 -1
  542. package/dist/livekit-client.umd.js +1 -1
  543. package/dist/livekit-client.umd.js.map +1 -1
  544. package/dist/src/api/SignalClient.d.ts +1 -1
  545. package/dist/src/api/SignalClient.d.ts.map +1 -1
  546. package/dist/src/api/WebSocketStream.d.ts.map +1 -1
  547. package/dist/src/connectionHelper/checks/Checker.d.ts +7 -4
  548. package/dist/src/connectionHelper/checks/Checker.d.ts.map +1 -1
  549. package/dist/src/core.d.ts +33 -0
  550. package/dist/src/core.d.ts.map +1 -0
  551. package/dist/src/e2ee/E2eeManager.d.ts +25 -5
  552. package/dist/src/e2ee/E2eeManager.d.ts.map +1 -1
  553. package/dist/src/e2ee/KeyProvider.d.ts.map +1 -1
  554. package/dist/src/e2ee/extension.d.ts +19 -0
  555. package/dist/src/e2ee/extension.d.ts.map +1 -0
  556. package/dist/src/e2ee/types.d.ts +14 -0
  557. package/dist/src/e2ee/types.d.ts.map +1 -1
  558. package/dist/src/e2ee/utils.d.ts +6 -1
  559. package/dist/src/e2ee/utils.d.ts.map +1 -1
  560. package/dist/src/e2ee/worker/FrameCryptor.d.ts +7 -1
  561. package/dist/src/e2ee/worker/FrameCryptor.d.ts.map +1 -1
  562. package/dist/src/e2ee/worker/naluUtils.d.ts +7 -0
  563. package/dist/src/e2ee/worker/naluUtils.d.ts.map +1 -1
  564. package/dist/src/e2ee/worker/sifPayload.d.ts +1 -1
  565. package/dist/src/e2ee/worker/sifPayload.d.ts.map +1 -1
  566. package/dist/src/exports.d.ts +76 -0
  567. package/dist/src/exports.d.ts.map +1 -0
  568. package/dist/src/frameMetadata/FrameMetadataManager.d.ts +5 -2
  569. package/dist/src/frameMetadata/FrameMetadataManager.d.ts.map +1 -1
  570. package/dist/src/frameMetadata/extension.d.ts +11 -0
  571. package/dist/src/frameMetadata/extension.d.ts.map +1 -0
  572. package/dist/src/frameMetadata/utils.d.ts +5 -0
  573. package/dist/src/frameMetadata/utils.d.ts.map +1 -1
  574. package/dist/src/index.d.ts +35 -63
  575. package/dist/src/index.d.ts.map +1 -1
  576. package/dist/src/logger.d.ts +1 -3
  577. package/dist/src/logger.d.ts.map +1 -1
  578. package/dist/src/options.d.ts +20 -6
  579. package/dist/src/options.d.ts.map +1 -1
  580. package/dist/src/room/CoreRoom.d.ts +392 -0
  581. package/dist/src/room/CoreRoom.d.ts.map +1 -0
  582. package/dist/src/room/PCTransport.d.ts +5 -2
  583. package/dist/src/room/PCTransport.d.ts.map +1 -1
  584. package/dist/src/room/PCTransportManager.d.ts +8 -4
  585. package/dist/src/room/PCTransportManager.d.ts.map +1 -1
  586. package/dist/src/room/RTCEngine.d.ts +23 -6
  587. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  588. package/dist/src/room/Room.d.ts +5 -403
  589. package/dist/src/room/Room.d.ts.map +1 -1
  590. package/dist/src/room/chat/extension.d.ts +22 -0
  591. package/dist/src/room/chat/extension.d.ts.map +1 -0
  592. package/dist/src/room/createRoom.d.ts +19 -0
  593. package/dist/src/room/createRoom.d.ts.map +1 -0
  594. package/dist/src/room/data-channel/DataChannelManager.d.ts +7 -1
  595. package/dist/src/room/data-channel/DataChannelManager.d.ts.map +1 -1
  596. package/dist/src/room/data-stream/constants.d.ts +9 -0
  597. package/dist/src/room/data-stream/constants.d.ts.map +1 -1
  598. package/dist/src/room/data-stream/extension.d.ts +73 -0
  599. package/dist/src/room/data-stream/extension.d.ts.map +1 -0
  600. package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts +6 -1
  601. package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts.map +1 -1
  602. package/dist/src/room/data-stream/incoming/StreamReader.d.ts.map +1 -1
  603. package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +0 -1
  604. package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts.map +1 -1
  605. package/dist/src/room/data-track/LocalDataTrack.d.ts +1 -1
  606. package/dist/src/room/data-track/depacketizer.d.ts +1 -1
  607. package/dist/src/room/data-track/depacketizer.d.ts.map +1 -1
  608. package/dist/src/room/data-track/extension.d.ts +31 -0
  609. package/dist/src/room/data-track/extension.d.ts.map +1 -0
  610. package/dist/src/room/data-track/handle.d.ts +1 -1
  611. package/dist/src/room/data-track/handle.d.ts.map +1 -1
  612. package/dist/src/room/data-track/incoming/IncomingDataTrackManager.d.ts +1 -1
  613. package/dist/src/room/data-track/incoming/IncomingDataTrackManager.d.ts.map +1 -1
  614. package/dist/src/room/data-track/incoming/pipeline.d.ts +1 -1
  615. package/dist/src/room/data-track/incoming/pipeline.d.ts.map +1 -1
  616. package/dist/src/room/data-track/outgoing/OutgoingDataTrackManager.d.ts +2 -1
  617. package/dist/src/room/data-track/outgoing/OutgoingDataTrackManager.d.ts.map +1 -1
  618. package/dist/src/room/data-track/outgoing/pipeline.d.ts +1 -1
  619. package/dist/src/room/data-track/outgoing/pipeline.d.ts.map +1 -1
  620. package/dist/src/room/data-track/packet/extensions.d.ts +1 -1
  621. package/dist/src/room/data-track/packet/extensions.d.ts.map +1 -1
  622. package/dist/src/room/data-track/packet/index.d.ts +1 -1
  623. package/dist/src/room/data-track/packet/index.d.ts.map +1 -1
  624. package/dist/src/room/data-track/packet/serializable.d.ts +1 -1
  625. package/dist/src/room/data-track/packet/serializable.d.ts.map +1 -1
  626. package/dist/src/room/data-track/packetizer.d.ts +1 -1
  627. package/dist/src/room/data-track/packetizer.d.ts.map +1 -1
  628. package/dist/src/room/data-track/track-interfaces.d.ts +1 -1
  629. package/dist/src/room/data-track/track-interfaces.d.ts.map +1 -1
  630. package/dist/src/room/data-track/utils.d.ts +1 -1
  631. package/dist/src/room/defaults.d.ts +1 -2
  632. package/dist/src/room/defaults.d.ts.map +1 -1
  633. package/dist/src/room/extensions.d.ts +208 -0
  634. package/dist/src/room/extensions.d.ts.map +1 -0
  635. package/dist/src/room/participant/LocalParticipant.d.ts +45 -120
  636. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  637. package/dist/src/room/participant/Participant.d.ts +8 -1
  638. package/dist/src/room/participant/Participant.d.ts.map +1 -1
  639. package/dist/src/room/participant/RemoteParticipant.d.ts +3 -3
  640. package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
  641. package/dist/src/room/participant/publishUtils.d.ts.map +1 -1
  642. package/dist/src/room/rpc/extension.d.ts +70 -0
  643. package/dist/src/room/rpc/extension.d.ts.map +1 -0
  644. package/dist/src/room/rpc/index.d.ts +1 -1
  645. package/dist/src/room/rpc/index.d.ts.map +1 -1
  646. package/dist/src/room/rpc/legacyExtension.d.ts +34 -0
  647. package/dist/src/room/rpc/legacyExtension.d.ts.map +1 -0
  648. package/dist/src/room/simulated-participants/extension.d.ts +25 -0
  649. package/dist/src/room/simulated-participants/extension.d.ts.map +1 -0
  650. package/dist/src/room/token-source/TokenSource.d.ts +5 -1
  651. package/dist/src/room/token-source/TokenSource.d.ts.map +1 -1
  652. package/dist/src/room/token-source/utils.d.ts.map +1 -1
  653. package/dist/src/room/track/LocalTrack.d.ts +1 -1
  654. package/dist/src/room/track/LocalTrack.d.ts.map +1 -1
  655. package/dist/src/room/track/LocalVideoTrack.d.ts.map +1 -1
  656. package/dist/src/room/track/RemoteTrackPublication.d.ts +14 -1
  657. package/dist/src/room/track/RemoteTrackPublication.d.ts.map +1 -1
  658. package/dist/src/room/track/Track.d.ts.map +1 -1
  659. package/dist/src/room/track/TrackPublication.d.ts +2 -2
  660. package/dist/src/room/track/TrackPublication.d.ts.map +1 -1
  661. package/dist/src/room/track/create.d.ts +15 -13
  662. package/dist/src/room/track/create.d.ts.map +1 -1
  663. package/dist/src/room/track/facingMode.d.ts.map +1 -1
  664. package/dist/src/room/track/options.d.ts +0 -6
  665. package/dist/src/room/track/options.d.ts.map +1 -1
  666. package/dist/src/room/track/processor/types.d.ts +2 -2
  667. package/dist/src/room/track/processor/types.d.ts.map +1 -1
  668. package/dist/src/room/track/utils.d.ts +1 -1
  669. package/dist/src/room/track/utils.d.ts.map +1 -1
  670. package/dist/src/room/utils.d.ts +2 -2
  671. package/dist/src/room/utils.d.ts.map +1 -1
  672. package/dist/src/room/video/create.d.ts +28 -0
  673. package/dist/src/room/video/create.d.ts.map +1 -0
  674. package/dist/src/room/video/extension.d.ts +48 -0
  675. package/dist/src/room/video/extension.d.ts.map +1 -0
  676. package/dist/src/room/video/publisher.d.ts +24 -0
  677. package/dist/src/room/video/publisher.d.ts.map +1 -0
  678. package/dist/src/test/MockMediaStream.d.ts +6 -0
  679. package/dist/src/test/MockMediaStream.d.ts.map +1 -0
  680. package/dist/src/utils/browserParser.d.ts +0 -3
  681. package/dist/src/utils/browserParser.d.ts.map +1 -1
  682. package/dist/src/utils/deferrable-map.d.ts +1 -1
  683. package/dist/src/utils/deferrable-map.d.ts.map +1 -1
  684. package/dist/src/utils/serializer.d.ts.map +1 -1
  685. package/dist/src/utils/throws.d.ts +10 -0
  686. package/dist/src/utils/throws.d.ts.map +1 -0
  687. package/dist/src/version.d.ts +4 -1
  688. package/dist/src/version.d.ts.map +1 -1
  689. package/dist/ts4.2/connectionHelper/checks/Checker.d.ts +7 -4
  690. package/dist/ts4.2/core.d.ts +34 -0
  691. package/dist/ts4.2/e2ee/E2eeManager.d.ts +25 -5
  692. package/dist/ts4.2/e2ee/extension.d.ts +20 -0
  693. package/dist/ts4.2/e2ee/types.d.ts +14 -0
  694. package/dist/ts4.2/e2ee/utils.d.ts +6 -1
  695. package/dist/ts4.2/e2ee/worker/FrameCryptor.d.ts +7 -1
  696. package/dist/ts4.2/e2ee/worker/naluUtils.d.ts +7 -0
  697. package/dist/ts4.2/e2ee/worker/sifPayload.d.ts +1 -1
  698. package/dist/ts4.2/exports.d.ts +83 -0
  699. package/dist/ts4.2/frameMetadata/FrameMetadataManager.d.ts +5 -2
  700. package/dist/ts4.2/frameMetadata/extension.d.ts +13 -0
  701. package/dist/ts4.2/frameMetadata/utils.d.ts +5 -0
  702. package/dist/ts4.2/index.d.ts +35 -69
  703. package/dist/ts4.2/logger.d.ts +1 -3
  704. package/dist/ts4.2/options.d.ts +20 -6
  705. package/dist/ts4.2/room/CoreRoom.d.ts +381 -0
  706. package/dist/ts4.2/room/PCTransport.d.ts +5 -2
  707. package/dist/ts4.2/room/PCTransportManager.d.ts +8 -4
  708. package/dist/ts4.2/room/RTCEngine.d.ts +23 -6
  709. package/dist/ts4.2/room/Room.d.ts +6 -392
  710. package/dist/ts4.2/room/chat/extension.d.ts +22 -0
  711. package/dist/ts4.2/room/createRoom.d.ts +20 -0
  712. package/dist/ts4.2/room/data-channel/DataChannelManager.d.ts +6 -1
  713. package/dist/ts4.2/room/data-stream/constants.d.ts +9 -0
  714. package/dist/ts4.2/room/data-stream/extension.d.ts +74 -0
  715. package/dist/ts4.2/room/data-stream/incoming/IncomingDataStreamManager.d.ts +6 -1
  716. package/dist/ts4.2/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +0 -1
  717. package/dist/ts4.2/room/data-track/LocalDataTrack.d.ts +1 -1
  718. package/dist/ts4.2/room/data-track/depacketizer.d.ts +1 -1
  719. package/dist/ts4.2/room/data-track/extension.d.ts +31 -0
  720. package/dist/ts4.2/room/data-track/handle.d.ts +1 -1
  721. package/dist/ts4.2/room/data-track/incoming/IncomingDataTrackManager.d.ts +1 -1
  722. package/dist/ts4.2/room/data-track/incoming/pipeline.d.ts +1 -1
  723. package/dist/ts4.2/room/data-track/outgoing/OutgoingDataTrackManager.d.ts +2 -1
  724. package/dist/ts4.2/room/data-track/outgoing/pipeline.d.ts +1 -1
  725. package/dist/ts4.2/room/data-track/packet/extensions.d.ts +1 -1
  726. package/dist/ts4.2/room/data-track/packet/index.d.ts +1 -1
  727. package/dist/ts4.2/room/data-track/packet/serializable.d.ts +1 -1
  728. package/dist/ts4.2/room/data-track/packetizer.d.ts +1 -1
  729. package/dist/ts4.2/room/data-track/track-interfaces.d.ts +1 -1
  730. package/dist/ts4.2/room/data-track/utils.d.ts +1 -1
  731. package/dist/ts4.2/room/defaults.d.ts +1 -2
  732. package/dist/ts4.2/room/extensions.d.ts +214 -0
  733. package/dist/ts4.2/room/participant/LocalParticipant.d.ts +44 -121
  734. package/dist/ts4.2/room/participant/Participant.d.ts +8 -1
  735. package/dist/ts4.2/room/participant/RemoteParticipant.d.ts +3 -3
  736. package/dist/ts4.2/room/rpc/extension.d.ts +74 -0
  737. package/dist/ts4.2/room/rpc/index.d.ts +1 -1
  738. package/dist/ts4.2/room/rpc/legacyExtension.d.ts +39 -0
  739. package/dist/ts4.2/room/simulated-participants/extension.d.ts +25 -0
  740. package/dist/ts4.2/room/token-source/TokenSource.d.ts +5 -1
  741. package/dist/ts4.2/room/track/LocalTrack.d.ts +1 -1
  742. package/dist/ts4.2/room/track/RemoteTrackPublication.d.ts +3 -1
  743. package/dist/ts4.2/room/track/TrackPublication.d.ts +2 -2
  744. package/dist/ts4.2/room/track/create.d.ts +15 -13
  745. package/dist/ts4.2/room/track/options.d.ts +0 -6
  746. package/dist/ts4.2/room/track/processor/types.d.ts +2 -2
  747. package/dist/ts4.2/room/track/utils.d.ts +1 -1
  748. package/dist/ts4.2/room/utils.d.ts +2 -2
  749. package/dist/ts4.2/room/video/create.d.ts +28 -0
  750. package/dist/ts4.2/room/video/extension.d.ts +48 -0
  751. package/dist/ts4.2/room/video/publisher.d.ts +24 -0
  752. package/dist/ts4.2/test/MockMediaStream.d.ts +6 -0
  753. package/dist/ts4.2/utils/browserParser.d.ts +0 -3
  754. package/dist/ts4.2/utils/deferrable-map.d.ts +1 -1
  755. package/dist/ts4.2/utils/throws.d.ts +10 -0
  756. package/dist/ts4.2/version.d.ts +4 -1
  757. package/package.json +24 -9
  758. package/src/api/SignalClient.test.ts +107 -0
  759. package/src/api/SignalClient.ts +13 -3
  760. package/src/api/WebSocketStream.test.ts +67 -0
  761. package/src/api/WebSocketStream.ts +41 -3
  762. package/src/connectionHelper/checks/Checker.ts +12 -5
  763. package/src/connectionHelper/checks/publishVideo.ts +1 -1
  764. package/src/connectionHelper/checks/reconnect.ts +1 -1
  765. package/src/core.ts +35 -0
  766. package/src/e2ee/E2eeManager.test.ts +70 -1
  767. package/src/e2ee/E2eeManager.ts +53 -32
  768. package/src/e2ee/KeyProvider.ts +2 -1
  769. package/src/e2ee/extension.ts +113 -0
  770. package/src/e2ee/subscriberBlackScreen.test.ts +9 -12
  771. package/src/e2ee/types.ts +14 -0
  772. package/src/e2ee/utils.test.ts +47 -0
  773. package/src/e2ee/utils.ts +26 -14
  774. package/src/e2ee/worker/DataCryptor.test.ts +42 -1
  775. package/src/e2ee/worker/DataCryptor.ts +2 -2
  776. package/src/e2ee/worker/FrameCryptor.race.test.ts +3 -7
  777. package/src/e2ee/worker/FrameCryptor.test.ts +68 -13
  778. package/src/e2ee/worker/FrameCryptor.ts +78 -18
  779. package/src/e2ee/worker/e2ee.worker.test.ts +151 -0
  780. package/src/e2ee/worker/e2ee.worker.ts +28 -20
  781. package/src/e2ee/worker/naluUtils.test.ts +21 -1
  782. package/src/e2ee/worker/naluUtils.ts +30 -0
  783. package/src/e2ee/worker/sifPayload.ts +1 -1
  784. package/src/exports.ts +224 -0
  785. package/src/frameMetadata/FrameMetadataManager.ts +9 -3
  786. package/src/frameMetadata/extension.ts +34 -0
  787. package/src/frameMetadata/frameMetadata.ts +1 -1
  788. package/src/frameMetadata/utils.ts +23 -0
  789. package/src/index.ts +47 -189
  790. package/src/logger.ts +1 -3
  791. package/src/options.ts +25 -6
  792. package/src/room/CoreRoom.ts +3017 -0
  793. package/src/room/DeviceManager.test.ts +55 -1
  794. package/src/room/DeviceManager.ts +1 -1
  795. package/src/room/PCTransport.test.ts +43 -0
  796. package/src/room/PCTransport.ts +22 -22
  797. package/src/room/PCTransportManager.test.ts +65 -3
  798. package/src/room/PCTransportManager.ts +28 -12
  799. package/src/room/RTCEngine.test.ts +421 -21
  800. package/src/room/RTCEngine.ts +138 -34
  801. package/src/room/RegionUrlProvider.ts +1 -1
  802. package/src/room/Room.test.ts +717 -4
  803. package/src/room/Room.ts +34 -3317
  804. package/src/room/chat/extension.ts +78 -0
  805. package/src/room/createRoom.test.ts +149 -0
  806. package/src/room/createRoom.ts +26 -0
  807. package/src/room/data-channel/DataChannelManager.test.ts +37 -0
  808. package/src/room/data-channel/DataChannelManager.ts +21 -3
  809. package/src/room/data-stream/constants.ts +10 -0
  810. package/src/room/data-stream/extension.ts +162 -0
  811. package/src/room/data-stream/incoming/IncomingDataStreamManager.test.ts +269 -3
  812. package/src/room/data-stream/incoming/IncomingDataStreamManager.ts +53 -2
  813. package/src/room/data-stream/incoming/StreamReader.ts +14 -4
  814. package/src/room/data-stream/outgoing/OutgoingDataStreamManager.ts +1 -2
  815. package/src/room/data-track/depacketizer.ts +2 -2
  816. package/src/room/data-track/extension.ts +244 -0
  817. package/src/room/data-track/handle.ts +1 -1
  818. package/src/room/data-track/incoming/IncomingDataTrackManager.ts +2 -2
  819. package/src/room/data-track/incoming/pipeline.ts +2 -2
  820. package/src/room/data-track/outgoing/OutgoingDataTrackManager.test.ts +42 -3
  821. package/src/room/data-track/outgoing/OutgoingDataTrackManager.ts +5 -3
  822. package/src/room/data-track/outgoing/pipeline.ts +1 -1
  823. package/src/room/data-track/packet/extensions.ts +1 -1
  824. package/src/room/data-track/packet/index.ts +1 -1
  825. package/src/room/data-track/packet/serializable.ts +1 -1
  826. package/src/room/data-track/packetizer.ts +1 -1
  827. package/src/room/data-track/track-interfaces.ts +4 -4
  828. package/src/room/data-track/utils.ts +1 -1
  829. package/src/room/defaults.ts +3 -16
  830. package/src/room/extensions.ts +310 -0
  831. package/src/room/participant/LocalParticipant.test.ts +23 -6
  832. package/src/room/participant/LocalParticipant.ts +92 -669
  833. package/src/room/participant/Participant.test.ts +60 -0
  834. package/src/room/participant/Participant.ts +21 -1
  835. package/src/room/participant/RemoteParticipant.ts +3 -8
  836. package/src/room/participant/publishUtils.ts +3 -3
  837. package/src/room/participant/restartOnDefaultAudioDevice.test.ts +1 -1
  838. package/src/room/rpc/extension.ts +159 -0
  839. package/src/room/rpc/index.ts +1 -10
  840. package/src/room/rpc/legacyExtension.ts +36 -0
  841. package/src/room/simulated-participants/extension.ts +189 -0
  842. package/src/room/token-source/TokenSource.test.ts +29 -0
  843. package/src/room/token-source/TokenSource.ts +8 -3
  844. package/src/room/token-source/utils.test.ts +34 -0
  845. package/src/room/token-source/utils.ts +23 -1
  846. package/src/room/track/LocalTrack.ts +18 -10
  847. package/src/room/track/LocalVideoTrack.test.ts +66 -2
  848. package/src/room/track/LocalVideoTrack.ts +5 -3
  849. package/src/room/track/RemoteTrackPublication.ts +14 -1
  850. package/src/room/track/RemoteVideoTrack.test.ts +2 -0
  851. package/src/room/track/Track.test.ts +102 -0
  852. package/src/room/track/Track.ts +8 -1
  853. package/src/room/track/TrackPublication.ts +2 -2
  854. package/src/room/track/create.ts +59 -71
  855. package/src/room/track/facingMode.ts +5 -2
  856. package/src/room/track/options.ts +26 -34
  857. package/src/room/track/processor/types.ts +2 -2
  858. package/src/room/track/utils.ts +5 -1
  859. package/src/room/utils.ts +2 -2
  860. package/src/room/video/create.ts +95 -0
  861. package/src/room/video/extension.ts +115 -0
  862. package/src/room/video/publisher.ts +472 -0
  863. package/src/test/MockMediaStream.ts +13 -0
  864. package/src/utils/browserParser.ts +0 -3
  865. package/src/utils/deferrable-map.test.ts +60 -0
  866. package/src/utils/deferrable-map.ts +7 -4
  867. package/src/utils/serializer.ts +1 -1
  868. package/src/utils/throws.ts +15 -0
  869. package/src/version.ts +6 -1
@@ -0,0 +1,3017 @@
1
+ import { Mutex } from '@livekit/mutex';
2
+ import {
3
+ ClientInfo_Capability,
4
+ ConnectionQualityUpdate,
5
+ type DataPacket,
6
+ DataPacket_Kind,
7
+ DisconnectReason,
8
+ Encryption_Type,
9
+ JoinResponse,
10
+ LeaveRequest,
11
+ LeaveRequest_Action,
12
+ MetricsBatch,
13
+ ParticipantInfo,
14
+ ParticipantInfo_State,
15
+ ParticipantPermission,
16
+ PublishDataTrackResponse,
17
+ Room as RoomModel,
18
+ ServerInfo,
19
+ SimulateScenario,
20
+ SipDTMF,
21
+ SpeakerInfo,
22
+ StreamStateUpdate,
23
+ SubscriptionError,
24
+ SubscriptionPermissionUpdate,
25
+ SubscriptionResponse,
26
+ Transcription as TranscriptionModel,
27
+ TranscriptionSegment as TranscriptionSegmentModel,
28
+ UserPacket,
29
+ } from '@livekit/protocol';
30
+ import { EventEmitter } from 'events';
31
+ import type TypedEmitter from 'typed-emitter';
32
+ import { ensureTrailingSlash } from '../api/utils';
33
+ import { isFrameMetadataSupported } from '../frameMetadata/utils';
34
+ import log, { LoggerNames, getLogger } from '../logger';
35
+ import type {
36
+ CoreRoomOptions,
37
+ InternalRoomConnectOptions,
38
+ InternalRoomOptions,
39
+ RoomConnectOptions,
40
+ } from '../options';
41
+ import type { NonSharedUint8Array } from '../type-polyfills/non-shared-typed-arrays';
42
+ import TypedPromise from '../utils/TypedPromise';
43
+ import { getBrowser } from '../utils/browserParser';
44
+ import { BackOffStrategy } from './BackOffStrategy';
45
+ import DeviceManager from './DeviceManager';
46
+ import RTCEngine, { type RegionStrategy } from './RTCEngine';
47
+ import { DEFAULT_MAX_AGE_MS, RegionUrlProvider } from './RegionUrlProvider';
48
+ import type { DataChannelKind } from './data-channel/types';
49
+ import type LocalDataTrack from './data-track/LocalDataTrack';
50
+ import type RemoteDataTrack from './data-track/RemoteDataTrack';
51
+ import type { DataTrackSid } from './data-track/types';
52
+ import {
53
+ audioDefaults,
54
+ publishDefaults,
55
+ roomConnectOptionDefaults,
56
+ roomOptionDefaults,
57
+ } from './defaults';
58
+ import {
59
+ ConnectionError,
60
+ ConnectionErrorReason,
61
+ UnexpectedConnectionState,
62
+ UnsupportedServer,
63
+ canFailOverToAnotherRegion,
64
+ } from './errors';
65
+ import { EngineEvent, ParticipantEvent, RoomEvent, TrackEvent } from './events';
66
+ import type {
67
+ CoreSlots,
68
+ DataPacketCase,
69
+ ExtensionContext,
70
+ ExtensionResult,
71
+ RoomExtension,
72
+ } from './extensions';
73
+ import { SLOT_KEYS } from './extensions';
74
+ import { LocalParticipant } from './participant/LocalParticipant';
75
+ import Participant from './participant/Participant';
76
+ import { type ConnectionQuality, ParticipantKind } from './participant/Participant';
77
+ import RemoteParticipant from './participant/RemoteParticipant';
78
+ import { summarizeStatsReport } from './statsSummary';
79
+ import CriticalTimers from './timers';
80
+ import type LocalAudioTrack from './track/LocalAudioTrack';
81
+ import type LocalTrack from './track/LocalTrack';
82
+ import LocalTrackPublication from './track/LocalTrackPublication';
83
+ import type RemoteTrack from './track/RemoteTrack';
84
+ import RemoteTrackPublication from './track/RemoteTrackPublication';
85
+ import { Track } from './track/Track';
86
+ import type { TrackPublication } from './track/TrackPublication';
87
+ import type { TrackProcessor } from './track/processor/types';
88
+ import type { AdaptiveStreamSettings } from './track/types';
89
+ import { getNewAudioContext, kindToSource, sourceToKind } from './track/utils';
90
+ import { type ChatMessage, type SimulationScenario, type TranscriptionSegment } from './types';
91
+ import {
92
+ Future,
93
+ extractTrackSid,
94
+ extractTranscriptionSegments,
95
+ getDisconnectReasonFromConnectionError,
96
+ getEmptyAudioStreamTrack,
97
+ isBrowserSupported,
98
+ isCloud,
99
+ isDeflateRawCompressionSupported,
100
+ isLocalAudioTrack,
101
+ isLocalVideoTrack,
102
+ isReactNative,
103
+ isRemotePub,
104
+ isSafariBased,
105
+ isWeb,
106
+ numberToBigInt,
107
+ releaseEmptyAudioStreamTrack,
108
+ sleep,
109
+ supportsSetSinkId,
110
+ toHttpUrl,
111
+ unpackStreamId,
112
+ unwrapConstraint,
113
+ } from './utils';
114
+
115
+ export enum ConnectionState {
116
+ Disconnected = 'disconnected',
117
+ Connecting = 'connecting',
118
+ Connected = 'connected',
119
+ Reconnecting = 'reconnecting',
120
+ SignalReconnecting = 'signalReconnecting',
121
+ }
122
+
123
+ const CONNECTION_RECONCILE_FREQUENCY_MS = 4 * 1000;
124
+ const STATS_LOG_FREQUENCY_MS = 30 * 1000;
125
+
126
+ const DUMMY_AUDIO_ELEMENT_ID = 'livekit-dummy-audio-el';
127
+
128
+ /**
129
+ * One silent audio element serves every room on the page, so it is reference counted: a room
130
+ * disconnecting must not pull it out from under the rooms still playing through it. The element
131
+ * and its track go away once the last room hands its reference back.
132
+ */
133
+ let sharedDummyAudioElement:
134
+ | { element: HTMLAudioElement; stream: MediaStream; track: MediaStreamTrack; refCount: number }
135
+ | undefined;
136
+
137
+ function createSharedDummyAudioElement() {
138
+ const element = document.createElement('audio');
139
+ element.id = DUMMY_AUDIO_ELEMENT_ID;
140
+ element.autoplay = true;
141
+ element.hidden = true;
142
+ const track = getEmptyAudioStreamTrack();
143
+ track.enabled = true;
144
+ const stream = new MediaStream([track]);
145
+ element.srcObject = stream;
146
+ document.body.append(element);
147
+ return { element, stream, track, refCount: 0 };
148
+ }
149
+
150
+ /**
151
+ * The room without optional features. Use `CoreRoom.with(...extensions)` to build a room class
152
+ * with the features an application needs, or use `Room` from the main entry for all of them.
153
+ *
154
+ * In LiveKit, a room is the logical grouping for a list of participants.
155
+ * Participants in a room can publish tracks, and subscribe to others' tracks.
156
+ *
157
+ * a Room fires [[RoomEvent | RoomEvents]].
158
+ *
159
+ * @noInheritDoc
160
+ * @experimental
161
+ */
162
+ export class CoreRoom extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>) {
163
+ state: ConnectionState = ConnectionState.Disconnected;
164
+
165
+ /**
166
+ * map of identity: [[RemoteParticipant]]
167
+ */
168
+ remoteParticipants: Map<string, RemoteParticipant>;
169
+
170
+ /**
171
+ * list of participants that are actively speaking. when this changes
172
+ * a [[RoomEvent.ActiveSpeakersChanged]] event is fired
173
+ */
174
+ activeSpeakers: Participant[] = [];
175
+
176
+ /**
177
+ * @internal
178
+ */
179
+ engine!: RTCEngine;
180
+
181
+ /** the current participant */
182
+ localParticipant: LocalParticipant;
183
+
184
+ /** options of room */
185
+ options: InternalRoomOptions;
186
+
187
+ /** reflects the sender encryption status of the local participant */
188
+ isE2EEEnabled: boolean = false;
189
+
190
+ serverInfo?: Partial<ServerInfo>;
191
+
192
+ private roomInfo?: RoomModel;
193
+
194
+ private sidToIdentity: Map<string, string>;
195
+
196
+ /** connect options of room */
197
+ private connOptions?: InternalRoomConnectOptions;
198
+
199
+ private audioEnabled = true;
200
+
201
+ private audioContext?: AudioContext;
202
+
203
+ /**
204
+ * whether `audioContext` was created by the SDK and therefore has to be closed by it.
205
+ * A context handed in through `webAudioMix` is owned by the application instead.
206
+ */
207
+ private ownsAudioContext = false;
208
+
209
+ /**
210
+ * set once the audio context has been released as part of a disconnect. Guards against
211
+ * creating a new context outside of a connection lifecycle, where nothing would close it again.
212
+ */
213
+ private audioContextReleased = false;
214
+
215
+ /**
216
+ * serialises `acquireAudioContext` against `releaseAudioContext`. Both await per-track graph
217
+ * rebuilds, so without it a reconnect landing inside a disconnect's teardown leaves the two
218
+ * racing to set the participants' context, and the loser's value is the one that sticks.
219
+ */
220
+ private audioContextMutex: Mutex = new Mutex();
221
+
222
+ /** tears down the iOS dummy audio element, set while one is in place. See `startAudio` */
223
+ private releaseDummyAudioElement?: () => void;
224
+
225
+ /** silent audio tracks this room acquired, handed back to the shared source on disconnect */
226
+ private acquiredEmptyAudioTracks = new Set<MediaStreamTrack>();
227
+
228
+ /** used for aborting pending connections to a LiveKit server */
229
+ private abortController?: AbortController;
230
+
231
+ /** future holding client initiated connection attempt */
232
+ private connectFuture?: Future<void, Error>;
233
+
234
+ private disconnectLock: Mutex;
235
+
236
+ /** the E2EE manager slot; filled by the `e2ee` extension */
237
+
238
+ private connectionReconcileInterval?: ReturnType<typeof setInterval>;
239
+
240
+ private statsLogInterval?: ReturnType<typeof setInterval>;
241
+
242
+ private regionUrlProvider?: RegionUrlProvider;
243
+
244
+ private regionUrl?: string;
245
+
246
+ private isVideoPlaybackBlocked: boolean = false;
247
+
248
+ private log = log;
249
+
250
+ private statsLog = log;
251
+
252
+ private bufferedEvents: Array<any> = [];
253
+
254
+ /**
255
+ * the room as it was before the current full reconnect. Every region the full reconnect tries
256
+ * starts from it, so the region it connects to reports every change since the outage
257
+ */
258
+ private roomInfoBeforeRestart?: RoomModel;
259
+
260
+ private isResuming: boolean = false;
261
+
262
+ private pendingTrackAddedCallbacks = new Map<Track.SID, Set<() => void>>();
263
+
264
+ /**
265
+ * map to store first point in time when a particular transcription segment was received
266
+ */
267
+ private transcriptionReceivedTimes: Map<string, number>;
268
+
269
+ private installedExtensions = new Map<symbol, ExtensionResult<any, any>>();
270
+
271
+ /** Values extensions hand to core (the `*Provider` members of an install result); shared with the local participant. */
272
+
273
+ private readonly slots: Partial<CoreSlots> = {};
274
+
275
+ private extensionDisposers: Array<() => void> = [];
276
+
277
+ private dataPacketHandlers = new Map<
278
+ DataPacketCase,
279
+ (
280
+ packet: DataPacket,
281
+ encryptionType: Encryption_Type,
282
+ participant: RemoteParticipant | undefined,
283
+ ) => void
284
+ >();
285
+
286
+ /** aborts the `devicechange` listener; also reachable from the FinalizationRegistry callback */
287
+ private cleanupController?: AbortController;
288
+
289
+ private disposed = false;
290
+
291
+ get hasE2EESetup(): boolean {
292
+ return this.slots.e2eeManager !== undefined;
293
+ }
294
+
295
+ /**
296
+ * Creates a new Room, the primary construct for a LiveKit session.
297
+ * @param options
298
+ */
299
+ constructor(options?: CoreRoomOptions) {
300
+ super();
301
+ this.setMaxListeners(100);
302
+ this.remoteParticipants = new Map();
303
+ this.sidToIdentity = new Map();
304
+ this.options = { ...roomOptionDefaults, ...options };
305
+
306
+ this.log = getLogger(this.options.loggerName ?? LoggerNames.Room, () => this.logContext);
307
+ // its own logger name, so the stats dumps can be silenced or routed
308
+ // separately from the rest of the room's logs
309
+ this.statsLog = getLogger(LoggerNames.Stats, () => this.logContext);
310
+ this.transcriptionReceivedTimes = new Map();
311
+
312
+ this.options.audioCaptureDefaults = {
313
+ ...audioDefaults,
314
+ ...options?.audioCaptureDefaults,
315
+ };
316
+ this.options.videoCaptureDefaults = {
317
+ ...options?.videoCaptureDefaults,
318
+ };
319
+ this.options.publishDefaults = {
320
+ ...publishDefaults,
321
+ ...options?.publishDefaults,
322
+ };
323
+
324
+ this.maybeCreateEngine();
325
+
326
+ this.disconnectLock = new Mutex();
327
+ this.localParticipant = new LocalParticipant('', '', this.engine, this.options, this.slots);
328
+
329
+ if (this.options.videoCaptureDefaults.deviceId) {
330
+ this.localParticipant.activeDeviceMap.set(
331
+ 'videoinput',
332
+ unwrapConstraint(this.options.videoCaptureDefaults.deviceId),
333
+ );
334
+ }
335
+ if (this.options.audioCaptureDefaults.deviceId) {
336
+ this.localParticipant.activeDeviceMap.set(
337
+ 'audioinput',
338
+ unwrapConstraint(this.options.audioCaptureDefaults.deviceId),
339
+ );
340
+ }
341
+ if (this.options.audioOutput?.deviceId) {
342
+ this.switchActiveDevice(
343
+ 'audiooutput',
344
+ unwrapConstraint(this.options.audioOutput.deviceId),
345
+ ).catch((e) => this.log.warn(`Could not set audio output: ${e.message}`));
346
+ }
347
+
348
+ if (isWeb()) {
349
+ const cleanupController = new AbortController();
350
+ this.cleanupController = cleanupController;
351
+ let onDeviceChange: () => void;
352
+
353
+ const cleanupRegistry = (this.constructor as typeof CoreRoom).cleanupRegistry;
354
+ if (cleanupRegistry) {
355
+ // Wrap the listener in a WeakRef closure so navigator.mediaDevices does not
356
+ // strongly retain the Room. When the user drops their Room ref, the
357
+ // FinalizationRegistry callback aborts the controller and removes the listener.
358
+ onDeviceChange = CoreRoom.createWeakDeviceChangeListener(new WeakRef(this));
359
+ cleanupRegistry.register(this, cleanupController);
360
+ } else {
361
+ // Legacy browsers without WeakRef/FinalizationRegistry: fall back to a
362
+ // direct listener (matches pre-#1944 behavior).
363
+ onDeviceChange = this.handleDeviceChange;
364
+ }
365
+
366
+ // in order to catch device changes prior to room connection we need to register the event in the constructor
367
+ navigator.mediaDevices?.addEventListener?.('devicechange', onDeviceChange, {
368
+ signal: cleanupController.signal,
369
+ });
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Installs resolved extensions on this room. `installExtensions()` in `extensions.ts` resolves
375
+ * dependencies and calls this; `createRoom()` and the full `Room` go through it.
376
+ * @internal
377
+ */
378
+ installExtensions(extensions: readonly RoomExtension<any, any>[]) {
379
+ const ctx: ExtensionContext = {
380
+ log: this.log,
381
+ get: (ext) => {
382
+ const result = this.installedExtensions.get(ext.key);
383
+ if (!result) {
384
+ // @throws-transformer ignore - programmer error
385
+ throw new Error(`extension '${String(ext.key)}' is not installed`);
386
+ }
387
+ return result as any;
388
+ },
389
+ slot: (slot) => this.slots[slot],
390
+ getOrCreateParticipant: (identity, info) => this.getOrCreateParticipant(identity, info),
391
+ simulateConnected: (roomInfo, localParticipantInfo) =>
392
+ this.simulateConnected(roomInfo, localParticipantInfo),
393
+ acquireEmptyAudioStreamTrack: () => this.acquireEmptyAudioStreamTrack(),
394
+ };
395
+ const results: ExtensionResult<any, any>[] = [];
396
+ for (const ext of extensions) {
397
+ if (this.installedExtensions.has(ext.key)) {
398
+ // @throws-transformer ignore - programmer error
399
+ throw new Error(`extension '${String(ext.key)}' is already installed`);
400
+ }
401
+ const result = ext.install(this, ctx);
402
+ this.installedExtensions.set(ext.key, result);
403
+ results.push(result);
404
+ for (const [kind, handler] of Object.entries(result.dataPackets ?? {})) {
405
+ if (this.dataPacketHandlers.has(kind as DataPacketCase)) {
406
+ // @throws-transformer ignore - programmer error
407
+ throw new Error(`data packet case '${kind}' is already handled by another extension`);
408
+ }
409
+ this.dataPacketHandlers.set(kind as DataPacketCase, (packet, encryptionType, participant) =>
410
+ (handler as any)(packet.value.value, packet, encryptionType, participant),
411
+ );
412
+ }
413
+ for (const key of SLOT_KEYS) {
414
+ if (result[key] !== undefined) {
415
+ (this.slots as Record<string, unknown>)[key] = result[key];
416
+ }
417
+ }
418
+ defineExtensionApi(this, CoreRoom.prototype, result.room, ext);
419
+ defineExtensionApi(
420
+ this.localParticipant,
421
+ LocalParticipant.prototype,
422
+ result.localParticipant,
423
+ ext,
424
+ );
425
+ if (result.dispose) {
426
+ this.extensionDisposers.push(result.dispose);
427
+ }
428
+ }
429
+ this.applySlotsToEngine();
430
+ // every slot of the list is filled before the first hook runs
431
+ results.forEach((result) => result.onEngineCreated?.(this.engine));
432
+ }
433
+
434
+ /** The engine keeps its own copy of the slots it reads on the hot path. */
435
+ private applySlotsToEngine() {
436
+ this.engine.e2eeManager = this.slots.e2eeManager;
437
+ this.engine.frameMetadataOptions = this.slots.frameMetadataOptions;
438
+ }
439
+
440
+ private get extensionResults() {
441
+ return Array.from(this.installedExtensions.values());
442
+ }
443
+
444
+ /**
445
+ * Releases everything the room holds: disconnects if connected, disposes the extensions and
446
+ * removes global listeners. The room cannot connect again afterwards.
447
+ */
448
+ async dispose() {
449
+ if (this.disposed) {
450
+ return;
451
+ }
452
+ this.disposed = true;
453
+ if (this.state !== ConnectionState.Disconnected) {
454
+ await this.disconnect();
455
+ }
456
+ for (const dispose of this.extensionDisposers.reverse()) {
457
+ dispose();
458
+ }
459
+ this.cleanupController?.abort();
460
+ }
461
+
462
+ private get logContext() {
463
+ return {
464
+ room: this.name,
465
+ roomID: this.roomInfo?.sid,
466
+ participant: this.localParticipant?.identity,
467
+ participantID: this.localParticipant?.sid,
468
+ };
469
+ }
470
+
471
+ /**
472
+ * if the current room has a participant with `recorder: true` in its JWT grant
473
+ **/
474
+ get isRecording(): boolean {
475
+ return this.roomInfo?.activeRecording ?? false;
476
+ }
477
+
478
+ /**
479
+ * server assigned unique room id.
480
+ * returns once a sid has been issued by the server.
481
+ */
482
+ getSid(): TypedPromise<string, UnexpectedConnectionState> {
483
+ if (this.state === ConnectionState.Disconnected) {
484
+ return TypedPromise.resolve('');
485
+ }
486
+ if (this.roomInfo && this.roomInfo.sid !== '') {
487
+ return TypedPromise.resolve(this.roomInfo.sid);
488
+ }
489
+ return new TypedPromise<string, UnexpectedConnectionState>((resolve, reject) => {
490
+ const handleRoomUpdate = (roomInfo: RoomModel) => {
491
+ if (roomInfo.sid !== '') {
492
+ this.engine.off(EngineEvent.RoomUpdate, handleRoomUpdate);
493
+ resolve(roomInfo.sid);
494
+ }
495
+ };
496
+ this.engine.on(EngineEvent.RoomUpdate, handleRoomUpdate);
497
+ this.once(RoomEvent.Disconnected, () => {
498
+ this.engine.off(EngineEvent.RoomUpdate, handleRoomUpdate);
499
+ reject(
500
+ new UnexpectedConnectionState('Room disconnected before room server id was available'),
501
+ );
502
+ });
503
+ });
504
+ }
505
+
506
+ /** user assigned name, derived from JWT token */
507
+ get name(): string {
508
+ return this.roomInfo?.name ?? '';
509
+ }
510
+
511
+ /** room metadata */
512
+ get metadata(): string | undefined {
513
+ return this.roomInfo?.metadata;
514
+ }
515
+
516
+ get numParticipants(): number {
517
+ return this.roomInfo?.numParticipants ?? 0;
518
+ }
519
+
520
+ get numPublishers(): number {
521
+ return this.roomInfo?.numPublishers ?? 0;
522
+ }
523
+
524
+ private maybeCreateEngine() {
525
+ if (this.engine && (this.engine.isNewlyCreated || !this.engine.isClosed)) {
526
+ return;
527
+ }
528
+
529
+ this.engine = new RTCEngine(this.options);
530
+ this.applySlotsToEngine();
531
+
532
+ this.engine
533
+ .on(EngineEvent.ParticipantUpdate, this.handleParticipantUpdates)
534
+ .on(EngineEvent.RoomUpdate, this.handleRoomUpdate)
535
+ .on(EngineEvent.SpeakersChanged, this.handleSpeakersChanged)
536
+ .on(EngineEvent.StreamStateChanged, this.handleStreamStateUpdate)
537
+ .on(EngineEvent.ConnectionQualityUpdate, this.handleConnectionQualityUpdate)
538
+ .on(EngineEvent.SubscriptionError, this.handleSubscriptionError)
539
+ .on(EngineEvent.SubscriptionPermissionUpdate, this.handleSubscriptionPermissionUpdate)
540
+ .on(
541
+ EngineEvent.MediaTrackAdded,
542
+ (mediaTrack: MediaStreamTrack, stream: MediaStream, receiver: RTCRtpReceiver) => {
543
+ this.onTrackAdded(mediaTrack, stream, receiver);
544
+ },
545
+ )
546
+ .on(EngineEvent.Disconnected, (reason?: DisconnectReason) => {
547
+ // the server can end the session while connect() still waits for the transports (e.g. a
548
+ // DUPLICATE_IDENTITY leave). Cancel that attempt: left running, it times out, fails over to
549
+ // another region and rejoins with the same token after the room reported Disconnected,
550
+ // and disconnect() can't stop it since the room already looks disconnected
551
+ this.abortController?.abort('room disconnected by the server during connection attempt');
552
+ this.handleDisconnect(this.options.stopLocalTrackOnUnpublish, reason);
553
+ })
554
+ .on(EngineEvent.ActiveSpeakersUpdate, this.handleActiveSpeakersUpdate)
555
+ .on(EngineEvent.DataPacketReceived, this.handleDataPacket)
556
+ .on(EngineEvent.Resuming, () => {
557
+ this.clearConnectionReconcile();
558
+ this.isResuming = true;
559
+ this.log.debug('Resuming signal connection');
560
+ // During connect(), the room stays `Connecting` and connect() reports the outcome.
561
+ if (
562
+ !this.connectFuture &&
563
+ this.setAndEmitConnectionState(ConnectionState.SignalReconnecting)
564
+ ) {
565
+ this.emit(RoomEvent.SignalReconnecting);
566
+ }
567
+ })
568
+ .on(EngineEvent.Resumed, () => {
569
+ this.registerConnectionReconcile();
570
+ this.isResuming = false;
571
+ this.log.debug('Resumed signal connection');
572
+ this.updateSubscriptions();
573
+ if (this.connectFuture) {
574
+ // connect() emits `Connected`; as on any initial join, events from before it are dropped.
575
+ this.bufferedEvents = [];
576
+ return;
577
+ }
578
+ if (this.setAndEmitConnectionState(ConnectionState.Connected)) {
579
+ this.emit(RoomEvent.Reconnected);
580
+ }
581
+ this.emitBufferedEvents();
582
+ })
583
+ .on(EngineEvent.SignalResumed, () => {
584
+ if (this.state === ConnectionState.Reconnecting || this.isResuming) {
585
+ this.sendSyncState();
586
+ }
587
+ this.emitBufferedEvents();
588
+ })
589
+ .on(EngineEvent.Restarting, this.handleRestarting)
590
+ .on(EngineEvent.SignalRestarted, this.handleSignalRestarted)
591
+ .on(EngineEvent.Restarted, () => {
592
+ // normally handleSignalRestarted flushes after the restart, but if the room already
593
+ // reports connected, anything buffered since then would otherwise wait for the next reconnect
594
+ if (this.state === ConnectionState.Connected) {
595
+ this.emitBufferedEvents();
596
+ }
597
+ })
598
+ .on(EngineEvent.Offline, () => {
599
+ // During connect(), the room stays `Connecting` and connect() reports the outcome.
600
+ if (!this.connectFuture && this.setAndEmitConnectionState(ConnectionState.Reconnecting)) {
601
+ this.emit(RoomEvent.Reconnecting);
602
+ }
603
+ })
604
+ .on(EngineEvent.DCBufferStatusChanged, (status, kind) => {
605
+ this.emit(RoomEvent.DCBufferStatusChanged, status, kind);
606
+ })
607
+ .on(EngineEvent.LocalTrackSubscribed, (subscribedSid) => {
608
+ this.handleLocalTrackSubscribed(subscribedSid);
609
+ })
610
+ .on(EngineEvent.RoomMoved, (roomMoved) => {
611
+ this.log.debug('room moved', roomMoved);
612
+
613
+ if (roomMoved.room) {
614
+ this.handleRoomUpdate(roomMoved.room);
615
+ }
616
+
617
+ this.remoteParticipants.forEach((participant, identity) => {
618
+ this.handleParticipantDisconnected(identity, participant);
619
+ });
620
+
621
+ this.emit(RoomEvent.Moved, roomMoved.room!.name);
622
+
623
+ if (roomMoved.participant) {
624
+ this.handleParticipantUpdates([roomMoved.participant, ...roomMoved.otherParticipants]);
625
+ } else {
626
+ this.handleParticipantUpdates(roomMoved.otherParticipants);
627
+ }
628
+ })
629
+ .on(EngineEvent.TokenRefreshed, (token) => {
630
+ this.regionUrlProvider?.updateToken(token);
631
+ })
632
+ .on(EngineEvent.ServerRegionsReported, (regions) => {
633
+ this.regionUrlProvider?.setServerReportedRegions({
634
+ regionSettings: regions,
635
+ updatedAtInMs: Date.now(),
636
+ maxAgeInMs: DEFAULT_MAX_AGE_MS,
637
+ });
638
+ })
639
+ .on(EngineEvent.RequestSubscribedCodecRefresh, () => {
640
+ for (const videoPub of this.localParticipant.videoTrackPublications.values()) {
641
+ if (isLocalVideoTrack(videoPub.track)) {
642
+ videoPub.track.refreshSubscribedCodecs();
643
+ }
644
+ }
645
+ });
646
+
647
+ if (this.localParticipant) {
648
+ this.localParticipant.setupEngine(this.engine);
649
+ }
650
+ if (this.slots.e2eeManager) {
651
+ this.slots.e2eeManager.setupEngine(this.engine);
652
+ }
653
+ this.extensionResults.forEach((result) => result.onEngineCreated?.(this.engine));
654
+ }
655
+
656
+ private createRegionStrategy(): RegionStrategy {
657
+ return {
658
+ getNextUrl: async (signal?: AbortSignal) =>
659
+ this.regionUrlProvider ? this.regionUrlProvider.getNextBestRegionUrl(signal) : null,
660
+ resetAttempts: () => this.regionUrlProvider?.resetAttempts(),
661
+ };
662
+ }
663
+
664
+ /**
665
+ * getLocalDevices abstracts navigator.mediaDevices.enumerateDevices.
666
+ * In particular, it requests device permissions by default if needed
667
+ * and makes sure the returned device does not consist of dummy devices
668
+ * @param kind
669
+ * @returns a list of available local devices
670
+ */
671
+ static getLocalDevices(
672
+ kind?: MediaDeviceKind,
673
+ requestPermissions: boolean = true,
674
+ ): Promise<MediaDeviceInfo[]> {
675
+ return DeviceManager.getInstance().getDevices(kind, requestPermissions);
676
+ }
677
+
678
+ static cleanupRegistry =
679
+ typeof FinalizationRegistry !== 'undefined' &&
680
+ typeof WeakRef !== 'undefined' &&
681
+ new FinalizationRegistry((cleanupController: AbortController) => {
682
+ cleanupController.abort();
683
+ });
684
+
685
+ /**
686
+ * Builds the constructor's `devicechange` listener. It lives outside the constructor so that
687
+ * its closure only captures `roomRef`, not the constructor's context (which holds the Room).
688
+ */
689
+ private static createWeakDeviceChangeListener(roomRef: WeakRef<CoreRoom>) {
690
+ return () => {
691
+ roomRef.deref()?.handleDeviceChange();
692
+ };
693
+ }
694
+
695
+ /**
696
+ * prepareConnection should be called as soon as the page is loaded, in order
697
+ * to speed up the connection attempt. This function will
698
+ * - perform DNS resolution and pre-warm the DNS cache
699
+ * - establish TLS connection and cache TLS keys
700
+ *
701
+ * With LiveKit Cloud, it will also determine the best edge data center for
702
+ * the current client to connect to if a token is provided.
703
+ */
704
+ async prepareConnection(url: string, token?: string) {
705
+ if (this.state !== ConnectionState.Disconnected) {
706
+ return;
707
+ }
708
+ this.log.debug(`prepareConnection to ${url}`);
709
+ try {
710
+ if (isCloud(new URL(url)) && token) {
711
+ this.regionUrlProvider = new RegionUrlProvider(url, token);
712
+ const regionUrl = await this.regionUrlProvider.getNextBestRegionUrl();
713
+ // we will not replace the regionUrl if an attempt had already started
714
+ // to avoid overriding regionUrl after a new connection attempt had started
715
+ if (regionUrl && this.state === ConnectionState.Disconnected) {
716
+ this.regionUrl = regionUrl;
717
+ await fetch(toHttpUrl(regionUrl), { method: 'HEAD' });
718
+ this.log.debug(`prepared connection to ${regionUrl}`);
719
+ }
720
+ } else {
721
+ await fetch(toHttpUrl(url), { method: 'HEAD' });
722
+ }
723
+ } catch (e) {
724
+ this.log.warn('could not prepare connection', { error: e });
725
+ }
726
+ }
727
+
728
+ connect = async (url: string, token: string, opts?: RoomConnectOptions): Promise<void> => {
729
+ if (this.disposed) {
730
+ throw new UnexpectedConnectionState('room has been disposed');
731
+ }
732
+ if (!isBrowserSupported()) {
733
+ if (isReactNative()) {
734
+ throw Error("WebRTC isn't detected, have you called registerGlobals?");
735
+ } else {
736
+ throw Error(
737
+ "LiveKit doesn't seem to be supported on this browser. Try to update your browser and make sure no browser extensions are disabling webRTC.",
738
+ );
739
+ }
740
+ }
741
+
742
+ // In case a disconnect called happened right before the connect call, make sure the disconnect is completed first by awaiting its lock
743
+ const unlockDisconnect = await this.disconnectLock.lock();
744
+
745
+ if (this.disposed) {
746
+ // dispose() ran while this call waited for a disconnect to finish
747
+ unlockDisconnect();
748
+ throw new UnexpectedConnectionState('room has been disposed');
749
+ }
750
+
751
+ if (this.state === ConnectionState.Connected) {
752
+ // when the state is reconnecting or connected, this function returns immediately
753
+ this.log.info(`already connected to room ${this.name}`);
754
+ unlockDisconnect();
755
+ return Promise.resolve();
756
+ }
757
+
758
+ if (this.connectFuture) {
759
+ unlockDisconnect();
760
+ return this.connectFuture.promise;
761
+ }
762
+
763
+ this.setAndEmitConnectionState(ConnectionState.Connecting);
764
+ if (this.regionUrlProvider?.getServerUrl().toString() !== ensureTrailingSlash(url)) {
765
+ this.regionUrl = undefined;
766
+ this.regionUrlProvider = undefined;
767
+ }
768
+ if (isCloud(new URL(url))) {
769
+ if (this.regionUrlProvider === undefined) {
770
+ this.regionUrlProvider = new RegionUrlProvider(url, token);
771
+ } else {
772
+ this.regionUrlProvider.updateToken(token);
773
+ }
774
+ // trigger the first fetch without waiting for a response
775
+ // if initial connection fails, this will speed up picking regional url
776
+ // on subsequent runs
777
+ this.regionUrlProvider
778
+ .fetchRegionSettings()
779
+ .then((settings) => {
780
+ this.regionUrlProvider?.setServerReportedRegions(settings);
781
+ })
782
+ .catch((e) => {
783
+ this.log.warn('could not fetch region settings', { error: e });
784
+ });
785
+ }
786
+
787
+ const connectFn = async (
788
+ resolve: () => void,
789
+ reject: (reason: any) => void,
790
+ regionUrl?: string,
791
+ ) => {
792
+ if (this.abortController) {
793
+ this.abortController.abort();
794
+ }
795
+
796
+ // explicit creation as local var needed to satisfy TS compiler when passing it to `attemptConnection` further down
797
+ const abortController = new AbortController();
798
+ this.abortController = abortController;
799
+
800
+ // at this point the intention to connect has been signalled so we can allow cancelling of the connection via disconnect() again
801
+ unlockDisconnect?.();
802
+
803
+ const failAttempt = (error: unknown, reason?: DisconnectReason) => {
804
+ // once a newer connect() replaced this attempt's controller, the room's state belongs to it
805
+ if (this.abortController === abortController) {
806
+ this.handleDisconnect(this.options.stopLocalTrackOnUnpublish, reason);
807
+ }
808
+ reject(error);
809
+ };
810
+
811
+ try {
812
+ await BackOffStrategy.getInstance().getBackOffPromise(url);
813
+ if (abortController.signal.aborted) {
814
+ throw ConnectionError.cancelled('Connection attempt aborted');
815
+ }
816
+ await this.attemptConnection(regionUrl ?? url, token, opts, abortController);
817
+ this.abortController = undefined;
818
+ resolve();
819
+ } catch (error) {
820
+ if (
821
+ this.regionUrlProvider &&
822
+ error instanceof ConnectionError &&
823
+ canFailOverToAnotherRegion(error)
824
+ ) {
825
+ let nextUrl: string | null = null;
826
+ try {
827
+ this.log.debug('Fetching next region');
828
+ nextUrl = await this.regionUrlProvider.getNextBestRegionUrl(abortController.signal);
829
+ } catch (regionFetchError) {
830
+ if (
831
+ regionFetchError instanceof ConnectionError &&
832
+ (regionFetchError.status === 401 ||
833
+ regionFetchError.reason === ConnectionErrorReason.Cancelled)
834
+ ) {
835
+ failAttempt(regionFetchError);
836
+ return;
837
+ }
838
+ }
839
+ if (
840
+ // making sure we only register failed attempts on things we actually care about
841
+ [
842
+ ConnectionErrorReason.InternalError,
843
+ ConnectionErrorReason.ServerUnreachable,
844
+ ConnectionErrorReason.Timeout,
845
+ ].includes(error.reason)
846
+ ) {
847
+ this.log.debug('Adding failed connection attempt to back off');
848
+ BackOffStrategy.getInstance().addFailedConnectionAttempt(url);
849
+ }
850
+ if (nextUrl && !abortController.signal.aborted) {
851
+ this.log.info(
852
+ `Initial connection failed with ConnectionError: ${error.message}. Retrying with another region: ${nextUrl}`,
853
+ );
854
+ this.recreateEngine(true);
855
+ await connectFn(resolve, reject, nextUrl);
856
+ } else {
857
+ failAttempt(error, getDisconnectReasonFromConnectionError(error));
858
+ }
859
+ } else {
860
+ let disconnectReason = DisconnectReason.UNKNOWN_REASON;
861
+ if (error instanceof ConnectionError) {
862
+ disconnectReason = getDisconnectReasonFromConnectionError(error);
863
+ }
864
+ failAttempt(error, disconnectReason);
865
+ }
866
+ }
867
+ };
868
+
869
+ const regionUrl = this.regionUrl;
870
+ this.regionUrl = undefined;
871
+ this.connectFuture = new Future(
872
+ (resolve, reject) => {
873
+ connectFn(resolve, reject, regionUrl);
874
+ },
875
+ () => {
876
+ this.clearConnectionFutures();
877
+ },
878
+ );
879
+
880
+ return this.connectFuture.promise;
881
+ };
882
+
883
+ private connectSignal = async (
884
+ url: string,
885
+ token: string,
886
+ engine: RTCEngine,
887
+ connectOptions: InternalRoomConnectOptions,
888
+ roomOptions: InternalRoomOptions,
889
+ abortController: AbortController,
890
+ ): Promise<JoinResponse> => {
891
+ const { joinResponse, serverInfo } = await engine.join(
892
+ url,
893
+ token,
894
+ {
895
+ autoSubscribe: connectOptions.autoSubscribe,
896
+ adaptiveStream:
897
+ typeof roomOptions.adaptiveStream === 'object' ? true : roomOptions.adaptiveStream,
898
+ disableIceLite: connectOptions.disableIceLite,
899
+ subscriberAllowPause: connectOptions.subscriberAllowPause,
900
+ clientInfoCapabilities: this.getClientInfoCapabilities(),
901
+ maxRetries: connectOptions.maxRetries,
902
+ e2eeEnabled: !!this.slots.e2eeManager,
903
+ websocketTimeout: connectOptions.websocketTimeout,
904
+ },
905
+ abortController.signal,
906
+ !roomOptions.singlePeerConnection,
907
+ );
908
+
909
+ this.serverInfo = serverInfo;
910
+
911
+ if (!serverInfo.version) {
912
+ throw new UnsupportedServer('unknown server version');
913
+ }
914
+
915
+ if (serverInfo.version === '0.15.1' && this.options.dynacast) {
916
+ this.log.debug('disabling dynacast due to server version');
917
+ // dynacast has a bug in 0.15.1, so we cannot use it then
918
+ roomOptions.dynacast = false;
919
+ }
920
+
921
+ return joinResponse;
922
+ };
923
+
924
+ private applyJoinResponse = (joinResponse: JoinResponse) => {
925
+ const pi = joinResponse.participant!;
926
+
927
+ this.localParticipant.sid = pi.sid;
928
+ this.localParticipant.identity = pi.identity;
929
+ this.localParticipant.setEnabledPublishCodecs(joinResponse.enabledPublishCodecs);
930
+
931
+ if (this.slots.e2eeManager) {
932
+ try {
933
+ this.slots.e2eeManager.setSifTrailer(joinResponse.sifTrailer as NonSharedUint8Array);
934
+ } catch (e: any) {
935
+ this.log.error(e instanceof Error ? e.message : 'Could not set SifTrailer', {
936
+ error: e,
937
+ });
938
+ }
939
+ }
940
+
941
+ // populate remote participants, these should not trigger new events
942
+ this.handleParticipantUpdates([pi, ...joinResponse.otherParticipants]);
943
+
944
+ if (joinResponse.room) {
945
+ this.handleRoomUpdate(joinResponse.room);
946
+ }
947
+ };
948
+
949
+ private attemptConnection = async (
950
+ url: string,
951
+ token: string,
952
+ opts: RoomConnectOptions | undefined,
953
+ abortController: AbortController,
954
+ ) => {
955
+ if (
956
+ this.state === ConnectionState.Reconnecting ||
957
+ this.isResuming ||
958
+ this.engine?.pendingReconnect
959
+ ) {
960
+ this.log.info('Reconnection attempt replaced by new connection attempt');
961
+ // make sure we close and recreate the existing engine in order to get rid of any potentially ongoing reconnection attempts
962
+ this.recreateEngine(true);
963
+ } else {
964
+ // create engine if previously disconnected
965
+ this.maybeCreateEngine();
966
+ }
967
+ // a concurrent disconnect() or connect() can replace or clear `this.engine` while we await below
968
+ const engine = this.engine;
969
+ if (this.regionUrlProvider?.isCloud()) {
970
+ engine.setRegionStrategy(this.createRegionStrategy());
971
+ }
972
+
973
+ this.acquireAudioContext(true).catch((error) =>
974
+ this.log.warn('Could not acquire audio context', { ...this.logContext, error }),
975
+ );
976
+
977
+ this.connOptions = { ...roomConnectOptionDefaults, ...opts } as InternalRoomConnectOptions;
978
+
979
+ if (this.connOptions.rtcConfig) {
980
+ engine.rtcConfig = this.connOptions.rtcConfig;
981
+ }
982
+ if (this.connOptions.peerConnectionTimeout) {
983
+ engine.peerConnectionTimeout = this.connOptions.peerConnectionTimeout;
984
+ }
985
+
986
+ try {
987
+ const joinResponse = await this.connectSignal(
988
+ url,
989
+ token,
990
+ engine,
991
+ this.connOptions,
992
+ this.options,
993
+ abortController,
994
+ );
995
+
996
+ this.applyJoinResponse(joinResponse);
997
+ // forward metadata changed for the local participant
998
+ this.setupLocalParticipantEvents();
999
+ this.emit(RoomEvent.SignalConnected);
1000
+ } catch (err) {
1001
+ await engine.close();
1002
+ this.recreateEngine(false, engine);
1003
+
1004
+ const resultingError = abortController.signal.aborted
1005
+ ? ConnectionError.cancelled('Signal connection aborted')
1006
+ : ConnectionError.serverUnreachable('could not establish signal connection');
1007
+
1008
+ if (err instanceof Error) {
1009
+ resultingError.message = `${resultingError.message}: ${err.message}`;
1010
+ }
1011
+ if (err instanceof ConnectionError) {
1012
+ resultingError.reason = err.reason;
1013
+ resultingError.status = err.status;
1014
+ }
1015
+ this.log.debug(`error trying to establish signal connection`, {
1016
+ error: err,
1017
+ });
1018
+ throw resultingError;
1019
+ }
1020
+
1021
+ if (abortController.signal.aborted) {
1022
+ await engine.close();
1023
+ this.recreateEngine(false, engine);
1024
+ throw ConnectionError.cancelled(`Connection attempt aborted`);
1025
+ }
1026
+
1027
+ try {
1028
+ await engine.waitForPCInitialConnection(
1029
+ this.connOptions.peerConnectionTimeout,
1030
+ abortController,
1031
+ );
1032
+ } catch (e) {
1033
+ await engine.close();
1034
+ this.recreateEngine(false, engine);
1035
+ throw e;
1036
+ }
1037
+
1038
+ // also hook unload event
1039
+ if (isWeb() && this.options.disconnectOnPageLeave) {
1040
+ // capturing both 'pagehide' and 'beforeunload' to capture broadest set of browser behaviors
1041
+ window.addEventListener('pagehide', this.onPageLeave);
1042
+ window.addEventListener('beforeunload', this.onPageLeave);
1043
+ }
1044
+ if (isWeb()) {
1045
+ window.addEventListener('freeze', this.onPageLeave);
1046
+ }
1047
+ this.setAndEmitConnectionState(ConnectionState.Connected);
1048
+ this.emit(RoomEvent.Connected);
1049
+ BackOffStrategy.getInstance().resetFailedConnectionAttempts(url);
1050
+ this.registerConnectionReconcile();
1051
+
1052
+ // Notify region provider about successful connection
1053
+ if (this.regionUrlProvider) {
1054
+ this.regionUrlProvider.notifyConnected();
1055
+ }
1056
+ };
1057
+
1058
+ /**
1059
+ * disconnects the room, emits [[RoomEvent.Disconnected]]
1060
+ */
1061
+ disconnect = async (stopTracks = true) => {
1062
+ const unlock = await this.disconnectLock.lock();
1063
+ try {
1064
+ if (this.state === ConnectionState.Disconnected) {
1065
+ this.log.debug('already disconnected');
1066
+ return;
1067
+ }
1068
+ this.log.info('disconnect from room');
1069
+ if (
1070
+ this.state === ConnectionState.Connecting ||
1071
+ this.state === ConnectionState.Reconnecting ||
1072
+ this.isResuming
1073
+ ) {
1074
+ // try aborting pending connection attempt
1075
+ const msg = 'Abort connection attempt due to user initiated disconnect';
1076
+ this.log.warn(msg);
1077
+ this.abortController?.abort(msg);
1078
+ // in case the abort controller didn't manage to cancel the connection attempt, reject the connect promise explicitly
1079
+ this.connectFuture?.reject?.(ConnectionError.cancelled('Client initiated disconnect'));
1080
+ this.connectFuture = undefined;
1081
+ }
1082
+
1083
+ // close engine (also closes client)
1084
+ if (this.engine) {
1085
+ // send leave
1086
+ if (!this.engine.client.isDisconnected) {
1087
+ await this.engine.client.sendLeave();
1088
+ }
1089
+ await this.engine.close();
1090
+ }
1091
+ this.handleDisconnect(stopTracks, DisconnectReason.CLIENT_INITIATED);
1092
+ /* @ts-ignore */
1093
+ this.engine = undefined;
1094
+ } finally {
1095
+ unlock();
1096
+ }
1097
+ };
1098
+
1099
+ /**
1100
+ * retrieves a participant by identity
1101
+ * @param identity
1102
+ * @returns
1103
+ */
1104
+ getParticipantByIdentity(identity: string): Participant | undefined {
1105
+ if (this.localParticipant.identity === identity) {
1106
+ return this.localParticipant;
1107
+ }
1108
+ return this.remoteParticipants.get(identity);
1109
+ }
1110
+
1111
+ private clearConnectionFutures() {
1112
+ this.connectFuture = undefined;
1113
+ }
1114
+
1115
+ /**
1116
+ * @internal for testing
1117
+ */
1118
+ async simulateScenario(scenario: SimulationScenario, arg?: any) {
1119
+ let postAction = async () => {};
1120
+ let req: SimulateScenario | undefined;
1121
+ switch (scenario) {
1122
+ case 'signal-reconnect':
1123
+ const reconnectDelay = typeof arg === 'number' ? arg : 0;
1124
+ // @ts-expect-error function is private
1125
+ await this.engine.client.handleOnClose('simulate disconnect', undefined, reconnectDelay);
1126
+ break;
1127
+ case 'fail-on-v1-path':
1128
+ this.engine.failNextV1Path();
1129
+ break;
1130
+ case 'speaker':
1131
+ req = new SimulateScenario({
1132
+ scenario: {
1133
+ case: 'speakerUpdate',
1134
+ value: 3,
1135
+ },
1136
+ });
1137
+ break;
1138
+ case 'node-failure':
1139
+ req = new SimulateScenario({
1140
+ scenario: {
1141
+ case: 'nodeFailure',
1142
+ value: true,
1143
+ },
1144
+ });
1145
+ break;
1146
+ case 'server-leave':
1147
+ req = new SimulateScenario({
1148
+ scenario: {
1149
+ case: 'serverLeave',
1150
+ value: true,
1151
+ },
1152
+ });
1153
+ break;
1154
+ case 'migration':
1155
+ req = new SimulateScenario({
1156
+ scenario: {
1157
+ case: 'migration',
1158
+ value: true,
1159
+ },
1160
+ });
1161
+ break;
1162
+ case 'resume-reconnect':
1163
+ this.engine.failNext();
1164
+ // @ts-expect-error function is private
1165
+ await this.engine.client.handleOnClose('simulate resume-disconnect');
1166
+ break;
1167
+ case 'disconnect-signal-on-resume':
1168
+ postAction = async () => {
1169
+ // @ts-expect-error function is private
1170
+ await this.engine.client.handleOnClose('simulate resume-disconnect');
1171
+ };
1172
+ req = new SimulateScenario({
1173
+ scenario: {
1174
+ case: 'disconnectSignalOnResume',
1175
+ value: true,
1176
+ },
1177
+ });
1178
+ break;
1179
+ case 'disconnect-signal-on-resume-no-messages':
1180
+ postAction = async () => {
1181
+ // @ts-expect-error function is private
1182
+ await this.engine.client.handleOnClose('simulate resume-disconnect');
1183
+ };
1184
+ req = new SimulateScenario({
1185
+ scenario: {
1186
+ case: 'disconnectSignalOnResumeNoMessages',
1187
+ value: true,
1188
+ },
1189
+ });
1190
+ break;
1191
+ case 'full-reconnect':
1192
+ this.engine.fullReconnectOnNext = true;
1193
+ // @ts-expect-error function is private
1194
+ await this.engine.client.handleOnClose('simulate full-reconnect');
1195
+ break;
1196
+ case 'force-tcp':
1197
+ case 'force-tls':
1198
+ req = new SimulateScenario({
1199
+ scenario: {
1200
+ case: 'switchCandidateProtocol',
1201
+ value: scenario === 'force-tls' ? 2 : 1,
1202
+ },
1203
+ });
1204
+ postAction = async () => {
1205
+ const onLeave = this.engine.client.onLeave;
1206
+ if (onLeave) {
1207
+ onLeave(
1208
+ new LeaveRequest({
1209
+ reason: DisconnectReason.CLIENT_INITIATED,
1210
+ action: LeaveRequest_Action.RECONNECT,
1211
+ }),
1212
+ );
1213
+ }
1214
+ };
1215
+ break;
1216
+ case 'subscriber-bandwidth':
1217
+ if (arg === undefined || typeof arg !== 'number') {
1218
+ throw new Error('subscriber-bandwidth requires a number as argument');
1219
+ }
1220
+ req = new SimulateScenario({
1221
+ scenario: {
1222
+ case: 'subscriberBandwidth',
1223
+ value: numberToBigInt(arg),
1224
+ },
1225
+ });
1226
+ break;
1227
+ case 'leave-full-reconnect':
1228
+ req = new SimulateScenario({
1229
+ scenario: {
1230
+ case: 'leaveRequestFullReconnect',
1231
+ value: true,
1232
+ },
1233
+ });
1234
+ default:
1235
+ }
1236
+ if (req) {
1237
+ await this.engine.client.sendSimulateScenario(req);
1238
+ await postAction();
1239
+ }
1240
+ }
1241
+
1242
+ private onPageLeave = async () => {
1243
+ this.log.info('Page leave detected, disconnecting');
1244
+ await this.disconnect();
1245
+ };
1246
+
1247
+ /**
1248
+ * iOS blocks audio element playback if
1249
+ * - user is not publishing audio themselves and
1250
+ * - no other audio source is playing
1251
+ *
1252
+ * as a workaround, we create an audio element with an empty track, so that
1253
+ * silent audio is always playing
1254
+ *
1255
+ * The element is shared with any other room on the page and handed back on disconnect, so it
1256
+ * outlives whichever room happened to create it.
1257
+ */
1258
+ private acquireDummyAudioElement(): HTMLAudioElement {
1259
+ if (!sharedDummyAudioElement) {
1260
+ sharedDummyAudioElement = createSharedDummyAudioElement();
1261
+ }
1262
+ const shared = sharedDummyAudioElement;
1263
+ if (this.releaseDummyAudioElement) {
1264
+ // this room already holds a reference, a second one would never be handed back
1265
+ return shared.element;
1266
+ }
1267
+ shared.refCount += 1;
1268
+
1269
+ // the listener is per room rather than shared, so that the room it resumes playback for is
1270
+ // still connected, and so that it goes away with the room that installed it
1271
+ const handleVisibilityChange = () => {
1272
+ // set the srcObject to null on page hide in order to prevent lock screen controls to show up for it
1273
+ shared.element.srcObject = document.hidden ? null : shared.stream;
1274
+ if (!document.hidden) {
1275
+ this.log.debug(
1276
+ 'page visible again, triggering startAudio to resume playback and update playback status',
1277
+ );
1278
+ this.startAudio().catch((error) =>
1279
+ this.log.warn('Could not restart audio on page becoming visible', {
1280
+ ...this.logContext,
1281
+ error,
1282
+ }),
1283
+ );
1284
+ }
1285
+ };
1286
+ document.addEventListener('visibilitychange', handleVisibilityChange);
1287
+
1288
+ this.releaseDummyAudioElement = () => {
1289
+ this.releaseDummyAudioElement = undefined;
1290
+ document.removeEventListener('visibilitychange', handleVisibilityChange);
1291
+ shared.refCount -= 1;
1292
+ if (shared.refCount > 0) {
1293
+ return;
1294
+ }
1295
+ shared.element.srcObject = null;
1296
+ shared.element.remove();
1297
+ sharedDummyAudioElement = undefined;
1298
+ releaseEmptyAudioStreamTrack(shared.track).catch((error) =>
1299
+ this.log.warn('Could not release the dummy audio element track', {
1300
+ ...this.logContext,
1301
+ error,
1302
+ }),
1303
+ );
1304
+ };
1305
+
1306
+ return shared.element;
1307
+ }
1308
+
1309
+ /**
1310
+ * Obtains a silent audio track from the shared source and keeps track of it, so that it can be
1311
+ * handed back on disconnect and the shared `AudioContext` behind it can be closed again.
1312
+ */
1313
+ private acquireEmptyAudioStreamTrack(): MediaStreamTrack {
1314
+ const track = getEmptyAudioStreamTrack();
1315
+ this.acquiredEmptyAudioTracks.add(track);
1316
+ return track;
1317
+ }
1318
+
1319
+ private releaseAcquiredEmptyAudioTrack(track: MediaStreamTrack) {
1320
+ if (!this.acquiredEmptyAudioTracks.delete(track)) {
1321
+ return;
1322
+ }
1323
+ releaseEmptyAudioStreamTrack(track).catch((error) =>
1324
+ this.log.warn('Could not release empty audio stream track', { ...this.logContext, error }),
1325
+ );
1326
+ }
1327
+
1328
+ private releaseAcquiredEmptyAudioTracks() {
1329
+ for (const track of this.acquiredEmptyAudioTracks) {
1330
+ this.releaseAcquiredEmptyAudioTrack(track);
1331
+ }
1332
+ }
1333
+
1334
+ /**
1335
+ * Browsers have different policies regarding audio playback. Most requiring
1336
+ * some form of user interaction (click/tap/etc).
1337
+ * In those cases, audio will be silent until a click/tap triggering one of the following
1338
+ * - `startAudio`
1339
+ * - `getUserMedia`
1340
+ */
1341
+ startAudio = async () => {
1342
+ const elements: Array<HTMLMediaElement> = [];
1343
+ const browser = getBrowser();
1344
+ if (browser && browser.os === 'iOS' && !this.audioContextReleased) {
1345
+ elements.push(this.acquireDummyAudioElement());
1346
+ }
1347
+
1348
+ this.remoteParticipants.forEach((p) => {
1349
+ p.audioTrackPublications.forEach((t) => {
1350
+ if (t.track) {
1351
+ t.track.attachedElements.forEach((e) => {
1352
+ elements.push(e);
1353
+ });
1354
+ }
1355
+ });
1356
+ });
1357
+
1358
+ try {
1359
+ await Promise.all([
1360
+ // once the audio context has been released as part of a disconnect there is nothing left
1361
+ // to unlock, and creating a new context here would leak it as no disconnect will follow
1362
+ ...(this.audioContextReleased ? [] : [this.acquireAudioContext()]),
1363
+ ...elements.map((e) => {
1364
+ // when webAudioMix is enabled, attached elements are deliberately kept muted by
1365
+ // RemoteAudioTrack.attach() and audio is routed through the web audio graph instead.
1366
+ // unmuting them here would cause double playback on platforms where element.volume
1367
+ // has no effect (e.g. iOS Safari)
1368
+ if (!this.options.webAudioMix) {
1369
+ e.muted = false;
1370
+ }
1371
+ return e.play();
1372
+ }),
1373
+ ]);
1374
+ this.handleAudioPlaybackStarted();
1375
+ } catch (err) {
1376
+ this.handleAudioPlaybackFailed(err);
1377
+ throw err;
1378
+ }
1379
+ };
1380
+
1381
+ startVideo = async () => {
1382
+ const elements: HTMLMediaElement[] = [];
1383
+ for (const p of this.remoteParticipants.values()) {
1384
+ p.videoTrackPublications.forEach((tr) => {
1385
+ tr.track?.attachedElements.forEach((el) => {
1386
+ if (!elements.includes(el)) {
1387
+ elements.push(el);
1388
+ }
1389
+ });
1390
+ });
1391
+ }
1392
+ await Promise.all(elements.map((el) => el.play()))
1393
+ .then(() => {
1394
+ this.handleVideoPlaybackStarted();
1395
+ })
1396
+ .catch((e) => {
1397
+ if (e.name === 'NotAllowedError') {
1398
+ this.handleVideoPlaybackFailed();
1399
+ } else {
1400
+ this.log.warn(
1401
+ 'Resuming video playback failed, make sure you call `startVideo` directly in a user gesture handler',
1402
+ );
1403
+ }
1404
+ });
1405
+ };
1406
+
1407
+ /**
1408
+ * Returns true if audio playback is enabled
1409
+ */
1410
+ get canPlaybackAudio(): boolean {
1411
+ return this.audioEnabled;
1412
+ }
1413
+
1414
+ /**
1415
+ * Returns true if video playback is enabled
1416
+ */
1417
+ get canPlaybackVideo(): boolean {
1418
+ return !this.isVideoPlaybackBlocked;
1419
+ }
1420
+
1421
+ getActiveDevice(kind: MediaDeviceKind): string | undefined {
1422
+ return this.localParticipant.activeDeviceMap.get(kind);
1423
+ }
1424
+
1425
+ /**
1426
+ * Switches all active devices used in this room to the given device.
1427
+ *
1428
+ * Note: setting AudioOutput is not supported on some browsers. See [setSinkId](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility)
1429
+ *
1430
+ * @param kind use `videoinput` for camera track,
1431
+ * `audioinput` for microphone track,
1432
+ * `audiooutput` to set speaker for all incoming audio tracks
1433
+ * @param deviceId
1434
+ */
1435
+ async switchActiveDevice(kind: MediaDeviceKind, deviceId: string, exact: boolean = true) {
1436
+ let success = true;
1437
+ let shouldTriggerImmediateDeviceChange = false;
1438
+ const deviceConstraint = exact ? { exact: deviceId } : deviceId;
1439
+ if (kind === 'audioinput') {
1440
+ shouldTriggerImmediateDeviceChange = this.localParticipant.audioTrackPublications.size === 0;
1441
+ const prevDeviceId =
1442
+ this.getActiveDevice(kind) ?? this.options.audioCaptureDefaults!.deviceId;
1443
+ this.options.audioCaptureDefaults!.deviceId = deviceConstraint;
1444
+ const tracks = Array.from(this.localParticipant.audioTrackPublications.values()).filter(
1445
+ (track) => track.source === Track.Source.Microphone,
1446
+ );
1447
+ try {
1448
+ success = (
1449
+ await Promise.all(tracks.map((t) => t.audioTrack?.setDeviceId(deviceConstraint)))
1450
+ ).every((val) => val === true);
1451
+ } catch (e) {
1452
+ this.options.audioCaptureDefaults!.deviceId = prevDeviceId;
1453
+ throw e;
1454
+ }
1455
+ const isMuted = tracks.some((t) => t.track?.isMuted ?? false);
1456
+ if (success && isMuted) shouldTriggerImmediateDeviceChange = true;
1457
+ } else if (kind === 'videoinput') {
1458
+ shouldTriggerImmediateDeviceChange = this.localParticipant.videoTrackPublications.size === 0;
1459
+ const prevDeviceId =
1460
+ this.getActiveDevice(kind) ?? this.options.videoCaptureDefaults!.deviceId;
1461
+ this.options.videoCaptureDefaults!.deviceId = deviceConstraint;
1462
+ const tracks = Array.from(this.localParticipant.videoTrackPublications.values()).filter(
1463
+ (track) => track.source === Track.Source.Camera,
1464
+ );
1465
+ try {
1466
+ success = (
1467
+ await Promise.all(tracks.map((t) => t.videoTrack?.setDeviceId(deviceConstraint)))
1468
+ ).every((val) => val === true);
1469
+ } catch (e) {
1470
+ this.options.videoCaptureDefaults!.deviceId = prevDeviceId;
1471
+ throw e;
1472
+ }
1473
+ const isMuted = tracks.some((t) => t.track?.isMuted ?? false);
1474
+ if (success && isMuted) shouldTriggerImmediateDeviceChange = true;
1475
+ } else if (kind === 'audiooutput') {
1476
+ shouldTriggerImmediateDeviceChange = true;
1477
+ if (
1478
+ (!supportsSetSinkId() && !this.options.webAudioMix) ||
1479
+ (this.options.webAudioMix && this.audioContext && !('setSinkId' in this.audioContext))
1480
+ ) {
1481
+ throw new Error('cannot switch audio output, the current browser does not support it');
1482
+ }
1483
+ if (this.options.webAudioMix) {
1484
+ // setting `default` for web audio output doesn't work, so we need to normalize the id before
1485
+ deviceId =
1486
+ (await DeviceManager.getInstance().normalizeDeviceId('audiooutput', deviceId)) ?? '';
1487
+ }
1488
+ this.options.audioOutput ??= {};
1489
+ const prevDeviceId = this.getActiveDevice(kind) ?? this.options.audioOutput.deviceId;
1490
+ this.options.audioOutput.deviceId = deviceId;
1491
+
1492
+ try {
1493
+ if (this.options.webAudioMix) {
1494
+ // @ts-expect-error setSinkId is not yet in the typescript type of AudioContext
1495
+ this.audioContext?.setSinkId(deviceId);
1496
+ }
1497
+
1498
+ // also set audio output on all audio elements, even if webAudioMix is enabled in order to workaround echo cancellation not working on chrome with non-default output devices
1499
+ // see https://issues.chromium.org/issues/40252911#comment7
1500
+ await Promise.all(
1501
+ Array.from(this.remoteParticipants.values()).map((p) => p.setAudioOutput({ deviceId })),
1502
+ );
1503
+ } catch (e) {
1504
+ this.options.audioOutput.deviceId = prevDeviceId;
1505
+ throw e;
1506
+ }
1507
+ }
1508
+
1509
+ if (shouldTriggerImmediateDeviceChange) {
1510
+ this.localParticipant.activeDeviceMap.set(kind, deviceId);
1511
+ this.emit(RoomEvent.ActiveDeviceChanged, kind, deviceId);
1512
+ }
1513
+
1514
+ return success;
1515
+ }
1516
+
1517
+ private setupLocalParticipantEvents() {
1518
+ this.localParticipant
1519
+ .on(ParticipantEvent.ParticipantMetadataChanged, this.onLocalParticipantMetadataChanged)
1520
+ .on(ParticipantEvent.ParticipantNameChanged, this.onLocalParticipantNameChanged)
1521
+ .on(ParticipantEvent.AttributesChanged, this.onLocalAttributesChanged)
1522
+ .on(ParticipantEvent.TrackMuted, this.onLocalTrackMuted)
1523
+ .on(ParticipantEvent.TrackUnmuted, this.onLocalTrackUnmuted)
1524
+ .on(ParticipantEvent.LocalTrackPublished, this.onLocalTrackPublished)
1525
+ .on(ParticipantEvent.LocalTrackUnpublished, this.onLocalTrackUnpublished)
1526
+ .on(ParticipantEvent.ConnectionQualityChanged, this.onLocalConnectionQualityChanged)
1527
+ .on(ParticipantEvent.MediaDevicesError, this.onMediaDevicesError)
1528
+ .on(ParticipantEvent.AudioStreamAcquired, this.startAudio)
1529
+ .on(
1530
+ ParticipantEvent.ParticipantPermissionsChanged,
1531
+ this.onLocalParticipantPermissionsChanged,
1532
+ );
1533
+ }
1534
+
1535
+ private recreateEngine(sendLeave?: boolean, oldEngine: RTCEngine | undefined = this.engine) {
1536
+ if (sendLeave && oldEngine && !oldEngine.client.isDisconnected) {
1537
+ oldEngine.client.sendLeave().finally(() => oldEngine.close());
1538
+ } else {
1539
+ oldEngine?.close();
1540
+ }
1541
+ // the room already moved on to a newer engine (or none); leave that one alone
1542
+ if (oldEngine !== this.engine) {
1543
+ return;
1544
+ }
1545
+ /* @ts-ignore */
1546
+ this.engine = undefined;
1547
+ this.isResuming = false;
1548
+
1549
+ // clear out existing remote participants, since they may have attached
1550
+ // the old engine
1551
+ this.remoteParticipants.clear();
1552
+ this.sidToIdentity.clear();
1553
+ this.bufferedEvents = [];
1554
+ this.maybeCreateEngine();
1555
+ }
1556
+
1557
+ private onTrackAdded(
1558
+ mediaTrack: MediaStreamTrack,
1559
+ stream: MediaStream,
1560
+ receiver: RTCRtpReceiver,
1561
+ ) {
1562
+ // don't fire onSubscribed when connecting
1563
+ // WebRTC fires onTrack as soon as setRemoteDescription is called on the offer
1564
+ // at that time, ICE connectivity has not been established so the track is not
1565
+ // technically subscribed.
1566
+ // We'll defer these events until when the room is connected or eventually disconnected.
1567
+ if ([ConnectionState.Connecting, ConnectionState.Reconnecting].includes(this.state)) {
1568
+ const pendingTrackSid = extractTrackSid(mediaTrack, stream);
1569
+ this.log.debug('deferring on track for later', {
1570
+ mediaTrackId: mediaTrack.id,
1571
+ mediaStreamId: stream.id,
1572
+ tracksInStream: stream.getTracks().map((track) => track.id),
1573
+ });
1574
+ const reconnectedHandler = () => {
1575
+ cleanup();
1576
+ // the server can stop sending the track before the room connects, e.g. when the
1577
+ // subscription failed. The subscription error cannot always cancel the deferral: it can
1578
+ // arrive before the `track` event, and in single peer connection mode the deferral is
1579
+ // not keyed by the track sid, as only the answer's mid mapping carries it.
1580
+ // When the server stops sending, the browser removes the track from its stream,
1581
+ // the same signal that ends a track once it is subscribed
1582
+ if (!stream.getTracks().includes(mediaTrack)) {
1583
+ this.log.debug('skipping deferred track, the server no longer sends it', {
1584
+ mediaTrackId: mediaTrack.id,
1585
+ mediaStreamId: stream.id,
1586
+ });
1587
+ return;
1588
+ }
1589
+ this.onTrackAdded(mediaTrack, stream, receiver);
1590
+ };
1591
+ const cleanup = () => {
1592
+ this.off(RoomEvent.Reconnected, reconnectedHandler);
1593
+ this.off(RoomEvent.Connected, reconnectedHandler);
1594
+ this.off(RoomEvent.Disconnected, cleanup);
1595
+ if (pendingTrackSid) {
1596
+ const pendingCallbacks = this.pendingTrackAddedCallbacks.get(pendingTrackSid);
1597
+ pendingCallbacks?.delete(cleanup);
1598
+ if (pendingCallbacks?.size === 0) {
1599
+ this.pendingTrackAddedCallbacks.delete(pendingTrackSid);
1600
+ }
1601
+ }
1602
+ };
1603
+ this.once(RoomEvent.Reconnected, reconnectedHandler);
1604
+ this.once(RoomEvent.Connected, reconnectedHandler);
1605
+ this.once(RoomEvent.Disconnected, cleanup);
1606
+ if (pendingTrackSid) {
1607
+ const pendingCallbacks =
1608
+ this.pendingTrackAddedCallbacks.get(pendingTrackSid) ?? new Set<() => void>();
1609
+ pendingCallbacks.add(cleanup);
1610
+ this.pendingTrackAddedCallbacks.set(pendingTrackSid, pendingCallbacks);
1611
+ }
1612
+ return;
1613
+ }
1614
+ if (this.state === ConnectionState.Disconnected) {
1615
+ this.log.warn('skipping incoming track after Room disconnected');
1616
+ return;
1617
+ }
1618
+ if (mediaTrack.readyState === 'ended') {
1619
+ this.log.debug('skipping incoming track as it already ended');
1620
+ return;
1621
+ }
1622
+ const parts = unpackStreamId(stream.id);
1623
+ const participantSid = parts[0];
1624
+ const streamId = parts[1];
1625
+ // firefox will get streamId (pID|trackId) instead of (pID|streamId) as it doesn't support sync tracks by stream
1626
+ // and generates its own track id instead of infer from sdp track id.
1627
+ let trackId = extractTrackSid(mediaTrack, stream) ?? mediaTrack.id;
1628
+
1629
+ if (participantSid === this.localParticipant.sid) {
1630
+ this.log.warn('tried to create RemoteParticipant for local participant');
1631
+ return;
1632
+ }
1633
+
1634
+ const participant = Array.from(this.remoteParticipants.values()).find(
1635
+ (p) => p.sid === participantSid,
1636
+ ) as RemoteParticipant | undefined;
1637
+
1638
+ if (!participant) {
1639
+ // server could require extra media sections to accelerate subscription.
1640
+ if (participantSid.startsWith('PA')) {
1641
+ this.log.error(
1642
+ `Tried to add a track for a participant, that's not present. Sid: ${participantSid}`,
1643
+ );
1644
+ }
1645
+ return;
1646
+ }
1647
+
1648
+ // in single peer connection case, the trackID is locally generated,
1649
+ // not the TR_ prefixed one generated by the server,
1650
+ // use `mid` to find the appropriate track.
1651
+ if (!trackId.startsWith('TR')) {
1652
+ const id = this.engine.getTrackIdForReceiver(receiver);
1653
+ if (!id) {
1654
+ this.log.error(
1655
+ `Tried to add a track whose 'sid' could not be found for a participant, that's not present. Sid: ${participantSid}`,
1656
+ );
1657
+ return;
1658
+ }
1659
+
1660
+ trackId = id;
1661
+ }
1662
+ if (!trackId.startsWith('TR')) {
1663
+ this.log.warn(
1664
+ `Tried to add a track whose 'sid' could not be determined for a participant, that's not present. Sid: ${participantSid}, streamId: ${streamId}, trackId: ${trackId}`,
1665
+ { remoteParticipantID: participantSid, streamId, trackId },
1666
+ );
1667
+ }
1668
+
1669
+ let adaptiveStreamSettings: AdaptiveStreamSettings | undefined;
1670
+ if (this.options.adaptiveStream) {
1671
+ if (typeof this.options.adaptiveStream === 'object') {
1672
+ adaptiveStreamSettings = this.options.adaptiveStream;
1673
+ } else {
1674
+ adaptiveStreamSettings = {};
1675
+ }
1676
+ }
1677
+
1678
+ const publication = participant.addSubscribedMediaTrack(
1679
+ mediaTrack,
1680
+ trackId,
1681
+ stream,
1682
+ receiver,
1683
+ adaptiveStreamSettings,
1684
+ );
1685
+
1686
+ if (publication?.isEncrypted && !this.slots.e2eeManager) {
1687
+ this.emit(
1688
+ RoomEvent.EncryptionError,
1689
+ new Error(
1690
+ `Encrypted ${publication.source} track received from participant ${participant.sid}, but room does not have encryption enabled!`,
1691
+ ),
1692
+ );
1693
+ }
1694
+ }
1695
+
1696
+ private cancelPendingTrackAdded(trackSid: Track.SID) {
1697
+ this.pendingTrackAddedCallbacks.get(trackSid)?.forEach((cleanup) => cleanup());
1698
+ }
1699
+
1700
+ private handleLocalTrackSubscribed(subscribedSid: string) {
1701
+ const findPublication = () =>
1702
+ this.localParticipant
1703
+ .getTrackPublications()
1704
+ .find(({ trackSid }) => trackSid === subscribedSid) as LocalTrackPublication | undefined;
1705
+
1706
+ const trackPublication = findPublication();
1707
+ if (trackPublication) {
1708
+ this.emitLocalTrackSubscribed(trackPublication);
1709
+ return;
1710
+ }
1711
+
1712
+ // the track publication may not be registered yet if the server signals
1713
+ // the subscription before publishTrack has finished adding the publication.
1714
+ // defer with a timeout until LocalTrackPublished fires for the matching trackSid
1715
+ this.log.debug('deferring LocalTrackSubscribed, publication not yet available', {
1716
+ subscribedSid,
1717
+ });
1718
+
1719
+ const TIMEOUT_MS = 10_000;
1720
+ let timer: ReturnType<typeof setTimeout>;
1721
+
1722
+ const onPublished = (pub: LocalTrackPublication) => {
1723
+ if (pub.trackSid === subscribedSid) {
1724
+ cleanup();
1725
+ this.emitLocalTrackSubscribed(pub);
1726
+ }
1727
+ };
1728
+
1729
+ const cleanup = () => {
1730
+ clearTimeout(timer);
1731
+ this.localParticipant.off(ParticipantEvent.LocalTrackPublished, onPublished);
1732
+ this.off(RoomEvent.Disconnected, cleanup);
1733
+ };
1734
+
1735
+ this.localParticipant.on(ParticipantEvent.LocalTrackPublished, onPublished);
1736
+ this.once(RoomEvent.Disconnected, cleanup);
1737
+
1738
+ timer = setTimeout(() => {
1739
+ cleanup();
1740
+ // final attempt in case the publication was added without emitting the event
1741
+ const pub = findPublication();
1742
+ if (pub) {
1743
+ this.emitLocalTrackSubscribed(pub);
1744
+ } else {
1745
+ this.log.warn(
1746
+ 'could not find local track publication for LocalTrackSubscribed event after timeout',
1747
+ { subscribedSid },
1748
+ );
1749
+ }
1750
+ }, TIMEOUT_MS);
1751
+ }
1752
+
1753
+ private emitLocalTrackSubscribed(trackPublication: LocalTrackPublication) {
1754
+ this.localParticipant.emit(ParticipantEvent.LocalTrackSubscribed, trackPublication);
1755
+ this.emitWhenConnected(RoomEvent.LocalTrackSubscribed, trackPublication, this.localParticipant);
1756
+ }
1757
+
1758
+ private handleRestarting = () => {
1759
+ this.clearConnectionReconcile();
1760
+ // in case we went from resuming to full-reconnect, make sure to reflect it on the isResuming flag
1761
+ this.isResuming = false;
1762
+
1763
+ // a region that failed after its join leaves its room state and held back events behind
1764
+ this.roomInfoBeforeRestart ??= this.roomInfo;
1765
+ this.roomInfo = this.roomInfoBeforeRestart;
1766
+ this.bufferedEvents = [];
1767
+
1768
+ // also unwind existing participants & existing subscriptions
1769
+ for (const p of this.remoteParticipants.values()) {
1770
+ this.handleParticipantDisconnected(p.identity, p);
1771
+ }
1772
+
1773
+ // During connect(), the room stays `Connecting` and connect() reports the outcome.
1774
+ if (!this.connectFuture && this.setAndEmitConnectionState(ConnectionState.Reconnecting)) {
1775
+ this.emit(RoomEvent.Reconnecting);
1776
+ }
1777
+ };
1778
+
1779
+ private handleSignalRestarted = async (
1780
+ joinResponse: JoinResponse,
1781
+ restartAttempt: AbortSignal,
1782
+ ) => {
1783
+ this.log.debug(`signal reconnected to server, region ${joinResponse.serverRegion}`, {
1784
+ region: joinResponse.serverRegion,
1785
+ });
1786
+ this.bufferedEvents = [];
1787
+ const initialConnection = this.connectFuture !== undefined;
1788
+ const restarted = this.engine.waitForRestarted(restartAttempt);
1789
+
1790
+ this.applyJoinResponse(joinResponse);
1791
+
1792
+ try {
1793
+ // unpublish & republish tracks
1794
+ await this.localParticipant.republishAllTracks(undefined, true);
1795
+ } catch (error) {
1796
+ this.log.error('error trying to re-publish tracks after reconnection', { error });
1797
+ }
1798
+
1799
+ // the attempt is aborted when a newer attempt began, on the next region or in a later
1800
+ // restart, or when the engine closed; whatever replaced it reports the outcome
1801
+ if (!(await restarted) || restartAttempt.aborted) {
1802
+ return;
1803
+ }
1804
+ this.log.debug(`fully reconnected to server`, { region: joinResponse.serverRegion });
1805
+ this.roomInfoBeforeRestart = undefined;
1806
+ if (initialConnection) {
1807
+ // connect() emits `Connected`; as on any initial join, events from before it are dropped.
1808
+ this.bufferedEvents = [];
1809
+ return;
1810
+ }
1811
+ this.setAndEmitConnectionState(ConnectionState.Connected);
1812
+ this.emit(RoomEvent.Reconnected);
1813
+ this.registerConnectionReconcile();
1814
+ this.emitBufferedEvents();
1815
+ };
1816
+
1817
+ private handleDisconnect(shouldStopTracks = true, reason?: DisconnectReason) {
1818
+ this.clearConnectionReconcile();
1819
+ this.isResuming = false;
1820
+ this.bufferedEvents = [];
1821
+ this.roomInfoBeforeRestart = undefined;
1822
+ this.transcriptionReceivedTimes.clear();
1823
+ this.extensionResults.forEach((result) => result.onDisconnect?.());
1824
+ if (this.state === ConnectionState.Disconnected) {
1825
+ return;
1826
+ }
1827
+
1828
+ this.regionUrl = undefined;
1829
+
1830
+ // Notify region provider about disconnect to potentially stop auto-refetch
1831
+ if (this.regionUrlProvider) {
1832
+ this.regionUrlProvider.notifyDisconnected();
1833
+ }
1834
+
1835
+ try {
1836
+ // audio tracks that outlive this room are about to become unreachable through the local
1837
+ // participant, collect them while we can so they can be detached from the context below
1838
+ const retainedAudioTracks = shouldStopTracks
1839
+ ? []
1840
+ : Array.from(this.localParticipant.audioTrackPublications.values())
1841
+ .map((pub) => pub.track)
1842
+ .filter(isLocalAudioTrack);
1843
+
1844
+ this.remoteParticipants.forEach((p) => {
1845
+ p.trackPublications.forEach((pub) => {
1846
+ p.unpublishTrack(pub.trackSid);
1847
+ });
1848
+ });
1849
+
1850
+ this.localParticipant.trackPublications.forEach((pub) => {
1851
+ if (pub.track) {
1852
+ this.localParticipant.unpublishTrack(pub.track, shouldStopTracks);
1853
+ }
1854
+ if (shouldStopTracks) {
1855
+ pub.track?.detach();
1856
+ pub.track?.stop();
1857
+ } else {
1858
+ pub.track?.stopMonitor();
1859
+ }
1860
+ });
1861
+
1862
+ this.localParticipant
1863
+ .off(ParticipantEvent.ParticipantMetadataChanged, this.onLocalParticipantMetadataChanged)
1864
+ .off(ParticipantEvent.ParticipantNameChanged, this.onLocalParticipantNameChanged)
1865
+ .off(ParticipantEvent.AttributesChanged, this.onLocalAttributesChanged)
1866
+ .off(ParticipantEvent.TrackMuted, this.onLocalTrackMuted)
1867
+ .off(ParticipantEvent.TrackUnmuted, this.onLocalTrackUnmuted)
1868
+ .off(ParticipantEvent.LocalTrackPublished, this.onLocalTrackPublished)
1869
+ .off(ParticipantEvent.LocalTrackUnpublished, this.onLocalTrackUnpublished)
1870
+ .off(ParticipantEvent.ConnectionQualityChanged, this.onLocalConnectionQualityChanged)
1871
+ .off(ParticipantEvent.MediaDevicesError, this.onMediaDevicesError)
1872
+ .off(ParticipantEvent.AudioStreamAcquired, this.startAudio)
1873
+ .off(
1874
+ ParticipantEvent.ParticipantPermissionsChanged,
1875
+ this.onLocalParticipantPermissionsChanged,
1876
+ );
1877
+
1878
+ this.localParticipant.trackPublications.clear();
1879
+ this.localParticipant.videoTrackPublications.clear();
1880
+ this.localParticipant.audioTrackPublications.clear();
1881
+
1882
+ this.remoteParticipants.clear();
1883
+ this.sidToIdentity.clear();
1884
+ this.activeSpeakers = [];
1885
+ this.releaseDummyAudioElement?.();
1886
+ this.releaseAcquiredEmptyAudioTracks();
1887
+ // teardown is asynchronous (processors need to release their web audio nodes), but
1888
+ // `handleDisconnect` is not, so let it finish in the background
1889
+ this.releaseAudioContext(retainedAudioTracks).catch((error) =>
1890
+ this.log.warn('Could not release audio context', { ...this.logContext, error }),
1891
+ );
1892
+ if (isWeb()) {
1893
+ window.removeEventListener('beforeunload', this.onPageLeave);
1894
+ window.removeEventListener('pagehide', this.onPageLeave);
1895
+ window.removeEventListener('freeze', this.onPageLeave);
1896
+ navigator.mediaDevices?.removeEventListener?.('devicechange', this.handleDeviceChange);
1897
+ }
1898
+ } finally {
1899
+ this.setAndEmitConnectionState(ConnectionState.Disconnected);
1900
+ this.emit(RoomEvent.Disconnected, reason);
1901
+ }
1902
+ }
1903
+
1904
+ private handleParticipantUpdates = (participantInfos: ParticipantInfo[]) => {
1905
+ // handle changes to participant state, and send events
1906
+ const appliedInfos: ParticipantInfo[] = [];
1907
+ for (const info of participantInfos) {
1908
+ if (info.identity === this.localParticipant.identity) {
1909
+ this.localParticipant.updateInfo(info);
1910
+ appliedInfos.push(info);
1911
+ continue;
1912
+ }
1913
+
1914
+ // LiveKit server doesn't send identity info prior to version 1.5.2 in disconnect updates
1915
+ // so we try to map an empty identity to an already known sID manually
1916
+ if (info.identity === '') {
1917
+ info.identity = this.sidToIdentity.get(info.sid) ?? '';
1918
+ }
1919
+
1920
+ let remoteParticipant = this.remoteParticipants.get(info.identity);
1921
+
1922
+ // a late update for an older session of this identity must not change the current session
1923
+ if (remoteParticipant?.isOlderSession(info)) {
1924
+ this.log.debug('ignoring update from an older participant session', {
1925
+ remoteIdentity: info.identity,
1926
+ staleSid: info.sid,
1927
+ currentSid: remoteParticipant.sid,
1928
+ });
1929
+ continue;
1930
+ }
1931
+
1932
+ // when it's disconnected, send updates
1933
+ if (info.state === ParticipantInfo_State.DISCONNECTED) {
1934
+ this.handleParticipantDisconnected(
1935
+ info.identity,
1936
+ remoteParticipant,
1937
+ info.disconnectReason === DisconnectReason.UNKNOWN_REASON
1938
+ ? undefined
1939
+ : info.disconnectReason,
1940
+ );
1941
+ } else {
1942
+ // create participant if doesn't exist
1943
+ this.getOrCreateParticipant(info.identity, info);
1944
+ }
1945
+ appliedInfos.push(info);
1946
+ }
1947
+
1948
+ this.extensionResults.forEach((result) => result.onParticipantUpdates?.(appliedInfos));
1949
+ };
1950
+
1951
+ private handleParticipantDisconnected(
1952
+ identity: string,
1953
+ participant?: RemoteParticipant,
1954
+ disconnectReason?: DisconnectReason,
1955
+ ) {
1956
+ // remove and send event
1957
+ this.remoteParticipants.delete(identity);
1958
+ if (!participant) {
1959
+ return;
1960
+ }
1961
+
1962
+ participant.trackPublications.forEach((publication) => {
1963
+ participant.unpublishTrack(publication.trackSid, true);
1964
+ });
1965
+ this.emit(RoomEvent.ParticipantDisconnected, participant, disconnectReason);
1966
+ participant.setDisconnected();
1967
+ }
1968
+
1969
+ // updates are sent only when there's a change to speaker ordering
1970
+ private handleActiveSpeakersUpdate = (speakers: SpeakerInfo[]) => {
1971
+ const activeSpeakers: Participant[] = [];
1972
+ const seenSids: any = {};
1973
+ speakers.forEach((speaker) => {
1974
+ seenSids[speaker.sid] = true;
1975
+ if (speaker.sid === this.localParticipant.sid) {
1976
+ this.localParticipant.audioLevel = speaker.level;
1977
+ this.localParticipant.setIsSpeaking(true);
1978
+ activeSpeakers.push(this.localParticipant);
1979
+ } else {
1980
+ const p = this.getRemoteParticipantBySid(speaker.sid);
1981
+ if (p) {
1982
+ p.audioLevel = speaker.level;
1983
+ p.setIsSpeaking(true);
1984
+ activeSpeakers.push(p);
1985
+ }
1986
+ }
1987
+ });
1988
+
1989
+ if (!seenSids[this.localParticipant.sid]) {
1990
+ this.localParticipant.audioLevel = 0;
1991
+ this.localParticipant.setIsSpeaking(false);
1992
+ }
1993
+ this.remoteParticipants.forEach((p) => {
1994
+ if (!seenSids[p.sid]) {
1995
+ p.audioLevel = 0;
1996
+ p.setIsSpeaking(false);
1997
+ }
1998
+ });
1999
+
2000
+ this.activeSpeakers = activeSpeakers;
2001
+ this.emitWhenConnected(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
2002
+ };
2003
+
2004
+ // process list of changed speakers
2005
+ private handleSpeakersChanged = (speakerUpdates: SpeakerInfo[]) => {
2006
+ const lastSpeakers = new Map<string, Participant>();
2007
+ this.activeSpeakers.forEach((p) => {
2008
+ const remoteParticipant = this.remoteParticipants.get(p.identity);
2009
+ if (remoteParticipant && remoteParticipant.sid !== p.sid) {
2010
+ return;
2011
+ }
2012
+ lastSpeakers.set(p.sid, p);
2013
+ });
2014
+ speakerUpdates.forEach((speaker) => {
2015
+ let p: Participant | undefined = this.getRemoteParticipantBySid(speaker.sid);
2016
+ if (speaker.sid === this.localParticipant.sid) {
2017
+ p = this.localParticipant;
2018
+ }
2019
+ if (!p) {
2020
+ return;
2021
+ }
2022
+ p.audioLevel = speaker.level;
2023
+ p.setIsSpeaking(speaker.active);
2024
+
2025
+ if (speaker.active) {
2026
+ lastSpeakers.set(speaker.sid, p);
2027
+ } else {
2028
+ lastSpeakers.delete(speaker.sid);
2029
+ }
2030
+ });
2031
+ const activeSpeakers = Array.from(lastSpeakers.values());
2032
+ activeSpeakers.sort((a, b) => b.audioLevel - a.audioLevel);
2033
+ this.activeSpeakers = activeSpeakers;
2034
+ this.emitWhenConnected(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
2035
+ };
2036
+
2037
+ private handleStreamStateUpdate = (streamStateUpdate: StreamStateUpdate) => {
2038
+ streamStateUpdate.streamStates.forEach((streamState) => {
2039
+ const participant = this.getRemoteParticipantBySid(streamState.participantSid);
2040
+ if (!participant) {
2041
+ return;
2042
+ }
2043
+ const pub = participant.getTrackPublicationBySid(streamState.trackSid);
2044
+ if (!pub || !pub.track) {
2045
+ return;
2046
+ }
2047
+ const newStreamState = Track.streamStateFromProto(streamState.state);
2048
+ const prevStreamState = pub.track.streamState;
2049
+ pub.track.setStreamState(newStreamState);
2050
+ if (newStreamState !== prevStreamState) {
2051
+ participant.emit(ParticipantEvent.TrackStreamStateChanged, pub, pub.track.streamState);
2052
+ this.emitWhenConnected(
2053
+ RoomEvent.TrackStreamStateChanged,
2054
+ pub,
2055
+ pub.track.streamState,
2056
+ participant,
2057
+ );
2058
+ }
2059
+ });
2060
+ };
2061
+
2062
+ private handleSubscriptionPermissionUpdate = (update: SubscriptionPermissionUpdate) => {
2063
+ const participant = this.getRemoteParticipantBySid(update.participantSid);
2064
+ if (!participant) {
2065
+ return;
2066
+ }
2067
+ const pub = participant.getTrackPublicationBySid(update.trackSid);
2068
+ if (!pub) {
2069
+ return;
2070
+ }
2071
+
2072
+ pub.setAllowed(update.allowed);
2073
+ };
2074
+
2075
+ private handleSubscriptionError = (update: SubscriptionResponse) => {
2076
+ this.cancelPendingTrackAdded(update.trackSid);
2077
+
2078
+ const participant = Array.from(this.remoteParticipants.values()).find((p) =>
2079
+ p.trackPublications.has(update.trackSid),
2080
+ );
2081
+ if (!participant) {
2082
+ return;
2083
+ }
2084
+ const pub = participant.getTrackPublicationBySid(update.trackSid);
2085
+ if (!pub) {
2086
+ return;
2087
+ }
2088
+
2089
+ pub.setSubscriptionError(update.err);
2090
+ };
2091
+
2092
+ private handleDataPacket = (packet: DataPacket, encryptionType: Encryption_Type) => {
2093
+ // find the participant
2094
+ const participant = this.remoteParticipants.get(packet.participantIdentity);
2095
+ if (packet.value.case === 'user') {
2096
+ this.handleUserPacket(
2097
+ participant,
2098
+ packet.value.value,
2099
+ packet.kind,
2100
+ encryptionType,
2101
+ packet.participantIdentity,
2102
+ );
2103
+ } else if (packet.value.case === 'transcription') {
2104
+ this.handleTranscription(participant, packet.value.value);
2105
+ } else if (packet.value.case === 'sipDtmf') {
2106
+ this.handleSipDtmf(participant, packet.value.value);
2107
+ } else if (packet.value.case === 'metrics') {
2108
+ this.handleMetrics(packet.value.value, participant);
2109
+ } else if (packet.value.case) {
2110
+ // every other case belongs to an extension (data streams, rpc, ...)
2111
+ this.dataPacketHandlers.get(packet.value.case)?.(packet, encryptionType, participant);
2112
+ }
2113
+ };
2114
+
2115
+ private handleUserPacket = (
2116
+ participant: RemoteParticipant | undefined,
2117
+ userPacket: UserPacket,
2118
+ kind: DataPacket_Kind,
2119
+ encryptionType: Encryption_Type,
2120
+ participantIdentity: string,
2121
+ ) => {
2122
+ this.emit(
2123
+ RoomEvent.DataReceived,
2124
+ userPacket.payload as NonSharedUint8Array,
2125
+ participant,
2126
+ kind,
2127
+ userPacket.topic,
2128
+ encryptionType,
2129
+ participantIdentity,
2130
+ );
2131
+
2132
+ // also emit on the participant
2133
+ participant?.emit(
2134
+ ParticipantEvent.DataReceived,
2135
+ userPacket.payload as NonSharedUint8Array,
2136
+ kind,
2137
+ encryptionType,
2138
+ );
2139
+ };
2140
+
2141
+ private handleSipDtmf = (participant: RemoteParticipant | undefined, dtmf: SipDTMF) => {
2142
+ this.emit(RoomEvent.SipDTMFReceived, dtmf, participant);
2143
+
2144
+ // also emit on the participant
2145
+ participant?.emit(ParticipantEvent.SipDTMFReceived, dtmf);
2146
+ };
2147
+
2148
+ private handleTranscription = (
2149
+ _remoteParticipant: RemoteParticipant | undefined,
2150
+ transcription: TranscriptionModel,
2151
+ ) => {
2152
+ // find the participant
2153
+ const participant =
2154
+ transcription.transcribedParticipantIdentity === this.localParticipant.identity
2155
+ ? this.localParticipant
2156
+ : this.getParticipantByIdentity(transcription.transcribedParticipantIdentity);
2157
+ const publication = participant?.trackPublications.get(transcription.trackId);
2158
+
2159
+ const segments = extractTranscriptionSegments(transcription, this.transcriptionReceivedTimes);
2160
+
2161
+ publication?.emit(TrackEvent.TranscriptionReceived, segments);
2162
+ participant?.emit(ParticipantEvent.TranscriptionReceived, segments, publication);
2163
+ this.emit(RoomEvent.TranscriptionReceived, segments, participant, publication);
2164
+ };
2165
+
2166
+ private handleMetrics = (metrics: MetricsBatch, participant?: Participant) => {
2167
+ this.emit(RoomEvent.MetricsReceived, metrics, participant);
2168
+ };
2169
+
2170
+ bufferedSegments: Map<string, TranscriptionSegmentModel> = new Map();
2171
+
2172
+ private handleAudioPlaybackStarted = () => {
2173
+ if (this.canPlaybackAudio) {
2174
+ return;
2175
+ }
2176
+ this.audioEnabled = true;
2177
+ this.emit(RoomEvent.AudioPlaybackStatusChanged, true);
2178
+ };
2179
+
2180
+ private handleAudioPlaybackFailed = (e: any) => {
2181
+ this.log.warn('could not playback audio', { error: e });
2182
+ if (!this.canPlaybackAudio) {
2183
+ return;
2184
+ }
2185
+ this.audioEnabled = false;
2186
+ this.emit(RoomEvent.AudioPlaybackStatusChanged, false);
2187
+ };
2188
+
2189
+ private handleVideoPlaybackStarted = () => {
2190
+ if (this.isVideoPlaybackBlocked) {
2191
+ this.isVideoPlaybackBlocked = false;
2192
+ this.emit(RoomEvent.VideoPlaybackStatusChanged, true);
2193
+ }
2194
+ };
2195
+
2196
+ private handleVideoPlaybackFailed = () => {
2197
+ if (!this.isVideoPlaybackBlocked) {
2198
+ this.isVideoPlaybackBlocked = true;
2199
+ this.emit(RoomEvent.VideoPlaybackStatusChanged, false);
2200
+ }
2201
+ };
2202
+
2203
+ /**
2204
+ * attempt to select the default devices if the previously selected devices are no longer available after a device change event
2205
+ */
2206
+ private async selectDefaultDevices() {
2207
+ const previousDevices = DeviceManager.getInstance().previousDevices;
2208
+ // check for available devices, but don't request permissions in order to avoid prompts for kinds that haven't been used before
2209
+ const availableDevices = await DeviceManager.getInstance().getDevices(undefined, false);
2210
+ const browser = getBrowser();
2211
+ if (browser?.name === 'Chrome' && browser.os !== 'iOS') {
2212
+ for (let availableDevice of availableDevices) {
2213
+ const previousDevice = previousDevices.find(
2214
+ (info) => info.deviceId === availableDevice.deviceId,
2215
+ );
2216
+ if (
2217
+ previousDevice &&
2218
+ previousDevice.label !== '' &&
2219
+ previousDevice.kind === availableDevice.kind &&
2220
+ previousDevice.label !== availableDevice.label
2221
+ ) {
2222
+ // label has changed on device the same deviceId, indicating that the default device has changed on the OS level
2223
+ if (this.getActiveDevice(availableDevice.kind) === 'default') {
2224
+ // emit an active device change event only if the selected output device is actually on `default`
2225
+ this.emit(
2226
+ RoomEvent.ActiveDeviceChanged,
2227
+ availableDevice.kind,
2228
+ availableDevice.deviceId,
2229
+ );
2230
+ }
2231
+ }
2232
+ }
2233
+ }
2234
+
2235
+ const kinds: MediaDeviceKind[] = ['audiooutput', 'audioinput', 'videoinput'];
2236
+ for (let kind of kinds) {
2237
+ const targetSource = kindToSource(kind);
2238
+ const targetPublication = this.localParticipant.getTrackPublication(targetSource);
2239
+ if (targetPublication && targetPublication.track?.isUserProvided) {
2240
+ // if the track is user provided, we don't want to switch devices on behalf of the user
2241
+ continue;
2242
+ }
2243
+ const devicesOfKind = availableDevices.filter((d) => d.kind === kind);
2244
+ const activeDevice = this.getActiveDevice(kind);
2245
+
2246
+ if (activeDevice === previousDevices.filter((info) => info.kind === kind)[0]?.deviceId) {
2247
+ // in Safari the first device is always the default, so we assume a user on the default device would like to switch to the default once it changes
2248
+ // FF doesn't emit an event when the default device changes, so we perform the same best effort and switch to the new device once connected and if it's the first in the array
2249
+ if (devicesOfKind.length > 0 && devicesOfKind[0]?.deviceId !== activeDevice) {
2250
+ await this.switchActiveDevice(kind, devicesOfKind[0].deviceId);
2251
+ continue;
2252
+ }
2253
+ }
2254
+
2255
+ if ((kind === 'audioinput' && !isSafariBased()) || kind === 'videoinput') {
2256
+ // airpods on Safari need special handling for audioinput as the track doesn't end as soon as you take them out
2257
+ continue;
2258
+ }
2259
+ // switch to first available device if previously active device is not available any more
2260
+ if (
2261
+ devicesOfKind.length > 0 &&
2262
+ !devicesOfKind.find((deviceInfo) => deviceInfo.deviceId === this.getActiveDevice(kind)) &&
2263
+ // avoid switching audio output on safari without explicit user action as it leads to slowed down audio playback
2264
+ (kind !== 'audiooutput' || !isSafariBased())
2265
+ ) {
2266
+ await this.switchActiveDevice(kind, devicesOfKind[0].deviceId);
2267
+ }
2268
+ }
2269
+ }
2270
+
2271
+ private handleDeviceChange = async () => {
2272
+ if (getBrowser()?.os !== 'iOS') {
2273
+ // default devices are non deterministic on iOS, so we don't attempt to select them here
2274
+ await this.selectDefaultDevices();
2275
+ }
2276
+ this.emit(RoomEvent.MediaDevicesChanged);
2277
+ };
2278
+
2279
+ private handleRoomUpdate = (room: RoomModel) => {
2280
+ const oldRoom = this.roomInfo;
2281
+ this.roomInfo = room;
2282
+ if (oldRoom && oldRoom.metadata !== room.metadata) {
2283
+ this.emitWhenConnected(RoomEvent.RoomMetadataChanged, room.metadata);
2284
+ }
2285
+ if (oldRoom?.activeRecording !== room.activeRecording) {
2286
+ this.emitWhenConnected(RoomEvent.RecordingStatusChanged, room.activeRecording);
2287
+ }
2288
+ };
2289
+
2290
+ private handleConnectionQualityUpdate = (update: ConnectionQualityUpdate) => {
2291
+ update.updates.forEach((info) => {
2292
+ if (info.participantSid === this.localParticipant.sid) {
2293
+ this.localParticipant.setConnectionQuality(info.quality);
2294
+ return;
2295
+ }
2296
+ const participant = this.getRemoteParticipantBySid(info.participantSid);
2297
+ if (participant) {
2298
+ participant.setConnectionQuality(info.quality);
2299
+ }
2300
+ });
2301
+ };
2302
+
2303
+ /**
2304
+ * @param forConnect whether this acquisition belongs to a connect, which is the only caller
2305
+ * allowed to put a released room back on an audio context.
2306
+ */
2307
+ private async acquireAudioContext(forConnect = false) {
2308
+ const unlock = await this.audioContextMutex.lock();
2309
+ try {
2310
+ if (this.audioContextReleased && !forConnect) {
2311
+ // a disconnect set the flag and took the lock while this call was queued behind it.
2312
+ // Creating a context now would leave one open on a room nothing tears down again
2313
+ return;
2314
+ }
2315
+ this.audioContextReleased = false;
2316
+ const providedAudioContext =
2317
+ typeof this.options.webAudioMix !== 'boolean'
2318
+ ? this.options.webAudioMix.audioContext
2319
+ : undefined;
2320
+ if (providedAudioContext) {
2321
+ // override audio context with custom audio context if supplied by user
2322
+ await this.setRoomAudioContext(providedAudioContext, false);
2323
+ } else if (!this.audioContext || this.audioContext.state === 'closed') {
2324
+ // by using an AudioContext, it reduces lag on audio elements
2325
+ // https://stackoverflow.com/questions/9811429/html5-audio-tag-on-safari-has-a-delay/54119854#54119854
2326
+ await this.setRoomAudioContext(getNewAudioContext() ?? undefined, true);
2327
+ }
2328
+
2329
+ if (this.audioContext && this.audioContext.state === 'suspended') {
2330
+ // for iOS a newly created AudioContext is always in `suspended` state.
2331
+ // we try our best to resume the context here, if that doesn't work, we just continue with regular processing
2332
+ try {
2333
+ await Promise.race([this.audioContext.resume(), sleep(200)]);
2334
+ } catch (e: any) {
2335
+ this.log.warn('Could not resume audio context', { error: e });
2336
+ }
2337
+ }
2338
+
2339
+ const newContextIsRunning = this.audioContext?.state === 'running';
2340
+ if (newContextIsRunning !== this.canPlaybackAudio) {
2341
+ this.audioEnabled = newContextIsRunning;
2342
+ this.emit(RoomEvent.AudioPlaybackStatusChanged, newContextIsRunning);
2343
+ }
2344
+ } finally {
2345
+ unlock();
2346
+ }
2347
+ }
2348
+
2349
+ /**
2350
+ * Points the room and its participants at `audioContext`, rebuilding the web audio graph of
2351
+ * every audio track in it. Closes the previous context if the SDK `owned` it.
2352
+ */
2353
+ private async setRoomAudioContext(audioContext: AudioContext | undefined, owned: boolean) {
2354
+ const ownsAudioContext = !!audioContext && owned;
2355
+ if (this.audioContext === audioContext) {
2356
+ this.ownsAudioContext = ownsAudioContext;
2357
+ return;
2358
+ }
2359
+ const contextToClose = this.ownsAudioContext ? this.audioContext : undefined;
2360
+ this.audioContext = audioContext;
2361
+ this.ownsAudioContext = ownsAudioContext;
2362
+
2363
+ try {
2364
+ // every audio track rebuilds its nodes in the new context, which has to happen before the
2365
+ // previous one is closed so that processors can move off it while it is still alive
2366
+ if (this.options.webAudioMix) {
2367
+ await Promise.all(
2368
+ Array.from(this.remoteParticipants.values()).map((participant) =>
2369
+ participant.setAudioContext(audioContext),
2370
+ ),
2371
+ );
2372
+ }
2373
+ await this.localParticipant.setAudioContext(audioContext);
2374
+ } catch (error) {
2375
+ // the context is being abandoned either way, so a track that failed to move off it must
2376
+ // not stop us closing it — that would leak the context this whole path exists to release
2377
+ this.log.warn('Could not move audio tracks to the new audio context', {
2378
+ ...this.logContext,
2379
+ error,
2380
+ });
2381
+ } finally {
2382
+ if (contextToClose && contextToClose.state !== 'closed') {
2383
+ try {
2384
+ await contextToClose.close();
2385
+ } catch (error) {
2386
+ this.log.warn('Could not close audio context', { ...this.logContext, error });
2387
+ }
2388
+ }
2389
+ }
2390
+ }
2391
+
2392
+ /**
2393
+ * Detaches the web audio graph the SDK built from the current audio context and closes that
2394
+ * context if the SDK created it. A context provided through `webAudioMix` is left untouched.
2395
+ */
2396
+ private async releaseAudioContext(retainedAudioTracks: LocalAudioTrack[] = []) {
2397
+ // both reads happen before the first await, so they describe the room as it was when the
2398
+ // disconnect started rather than after a reconnect has had a chance to run
2399
+ this.audioContextReleased = true;
2400
+ const contextToRelease = this.audioContext;
2401
+ const unlock = await this.audioContextMutex.lock();
2402
+ try {
2403
+ if (!this.audioContextReleased || this.audioContext !== contextToRelease) {
2404
+ // a reconnect acquired a new context while we were waiting, closing it would leave the
2405
+ // reconnected room without one
2406
+ return;
2407
+ }
2408
+ if (!this.ownsAudioContext) {
2409
+ // a provided context stays open, so tracks that outlive the room stay functional on it.
2410
+ // The tracks this room detached have released their nodes during track teardown already.
2411
+ // read under the lock: an acquire queued behind an in-flight release flips this flag
2412
+ return;
2413
+ }
2414
+ try {
2415
+ // tracks that outlive the room aren't reachable through the local participant anymore,
2416
+ // but still hold web audio nodes in the context we're about to close
2417
+ await Promise.all(retainedAudioTracks.map((track) => track.setAudioContext(undefined)));
2418
+ } catch (error) {
2419
+ this.log.warn('Could not detach retained audio tracks from the audio context', {
2420
+ ...this.logContext,
2421
+ error,
2422
+ });
2423
+ }
2424
+ // the participant maps have already been cleared by `handleDisconnect`, so this reaches no
2425
+ // tracks of its own — `retainedAudioTracks` above is what detaches them
2426
+ await this.setRoomAudioContext(undefined, false);
2427
+ } finally {
2428
+ unlock();
2429
+ }
2430
+ }
2431
+
2432
+ private createParticipant(identity: string, info?: ParticipantInfo): RemoteParticipant {
2433
+ let participant: RemoteParticipant;
2434
+ if (info) {
2435
+ participant = RemoteParticipant.fromParticipantInfo(this.engine.client, info, {
2436
+ loggerContextCb: () => this.logContext,
2437
+ loggerName: this.options.loggerName,
2438
+ });
2439
+ this.extensionResults.forEach((result) => result.onParticipantCreated?.(participant, info));
2440
+ } else {
2441
+ participant = new RemoteParticipant(
2442
+ this.engine.client,
2443
+ '',
2444
+ identity,
2445
+ undefined,
2446
+ undefined,
2447
+ undefined,
2448
+ {
2449
+ loggerContextCb: () => this.logContext,
2450
+ loggerName: this.options.loggerName,
2451
+ },
2452
+ );
2453
+ }
2454
+ if (this.options.webAudioMix) {
2455
+ participant
2456
+ .setAudioContext(this.audioContext)
2457
+ .catch((error) =>
2458
+ this.log.warn('Could not set audio context', { ...this.logContext, error }),
2459
+ );
2460
+ }
2461
+ if (this.options.audioOutput?.deviceId) {
2462
+ participant
2463
+ .setAudioOutput(this.options.audioOutput)
2464
+ .catch((e) => this.log.warn(`Could not set audio output: ${e.message}`));
2465
+ }
2466
+ return participant;
2467
+ }
2468
+
2469
+ private getOrCreateParticipant(identity: string, info: ParticipantInfo): RemoteParticipant {
2470
+ if (this.remoteParticipants.has(identity)) {
2471
+ const existingParticipant = this.remoteParticipants.get(identity)!;
2472
+ if (info) {
2473
+ const wasUpdated = existingParticipant.updateInfo(info);
2474
+ if (wasUpdated) {
2475
+ this.sidToIdentity.set(info.sid, info.identity);
2476
+ }
2477
+ }
2478
+ return existingParticipant;
2479
+ }
2480
+ const participant = this.createParticipant(identity, info);
2481
+ this.remoteParticipants.set(identity, participant);
2482
+
2483
+ this.sidToIdentity.set(info.sid, info.identity);
2484
+ // if we have valid info and the participant wasn't in the map before, we can assume the participant is new
2485
+ // firing here to make sure that `ParticipantConnected` fires before the initial track events
2486
+ this.emitWhenConnected(RoomEvent.ParticipantConnected, participant);
2487
+
2488
+ // also forward events
2489
+ // trackPublished is only fired for tracks added after both local participant
2490
+ // and remote participant joined the room
2491
+ participant
2492
+ .on(ParticipantEvent.TrackPublished, (trackPublication: RemoteTrackPublication) => {
2493
+ this.emitWhenConnected(RoomEvent.TrackPublished, trackPublication, participant);
2494
+ })
2495
+ .on(
2496
+ ParticipantEvent.TrackSubscribed,
2497
+ (track: RemoteTrack, publication: RemoteTrackPublication) => {
2498
+ // monitor playback status
2499
+ if (track.kind === Track.Kind.Audio) {
2500
+ track.on(TrackEvent.AudioPlaybackStarted, this.handleAudioPlaybackStarted);
2501
+ track.on(TrackEvent.AudioPlaybackFailed, this.handleAudioPlaybackFailed);
2502
+ } else if (track.kind === Track.Kind.Video) {
2503
+ track.on(TrackEvent.VideoPlaybackFailed, this.handleVideoPlaybackFailed);
2504
+ track.on(TrackEvent.VideoPlaybackStarted, this.handleVideoPlaybackStarted);
2505
+ }
2506
+ this.emitWhenConnected(RoomEvent.TrackSubscribed, track, publication, participant);
2507
+ },
2508
+ )
2509
+ .on(ParticipantEvent.TrackUnpublished, (publication: RemoteTrackPublication) => {
2510
+ this.cancelPendingTrackAdded(publication.trackSid);
2511
+ this.emit(RoomEvent.TrackUnpublished, publication, participant);
2512
+ })
2513
+ .on(
2514
+ ParticipantEvent.TrackUnsubscribed,
2515
+ (track: RemoteTrack, publication: RemoteTrackPublication) => {
2516
+ this.emit(RoomEvent.TrackUnsubscribed, track, publication, participant);
2517
+ },
2518
+ )
2519
+ .on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
2520
+ this.emitWhenConnected(RoomEvent.TrackMuted, pub, participant);
2521
+ })
2522
+ .on(ParticipantEvent.TrackUnmuted, (pub: TrackPublication) => {
2523
+ this.emitWhenConnected(RoomEvent.TrackUnmuted, pub, participant);
2524
+ })
2525
+ .on(ParticipantEvent.ParticipantMetadataChanged, (metadata: string | undefined) => {
2526
+ this.emitWhenConnected(RoomEvent.ParticipantMetadataChanged, metadata, participant);
2527
+ })
2528
+ .on(ParticipantEvent.ParticipantNameChanged, (name) => {
2529
+ this.emitWhenConnected(RoomEvent.ParticipantNameChanged, name, participant);
2530
+ })
2531
+ .on(ParticipantEvent.AttributesChanged, (changedAttributes: Record<string, string>) => {
2532
+ this.emitWhenConnected(
2533
+ RoomEvent.ParticipantAttributesChanged,
2534
+ changedAttributes,
2535
+ participant,
2536
+ );
2537
+ })
2538
+ .on(ParticipantEvent.ConnectionQualityChanged, (quality: ConnectionQuality) => {
2539
+ this.emitWhenConnected(RoomEvent.ConnectionQualityChanged, quality, participant);
2540
+ })
2541
+ .on(
2542
+ ParticipantEvent.ParticipantPermissionsChanged,
2543
+ (prevPermissions?: ParticipantPermission) => {
2544
+ this.emitWhenConnected(
2545
+ RoomEvent.ParticipantPermissionsChanged,
2546
+ prevPermissions,
2547
+ participant,
2548
+ );
2549
+ },
2550
+ )
2551
+ .on(ParticipantEvent.TrackSubscriptionStatusChanged, (pub, status) => {
2552
+ this.emitWhenConnected(RoomEvent.TrackSubscriptionStatusChanged, pub, status, participant);
2553
+ })
2554
+ .on(ParticipantEvent.TrackSubscriptionFailed, (trackSid, error) => {
2555
+ this.emit(RoomEvent.TrackSubscriptionFailed, trackSid, participant, error);
2556
+ })
2557
+ .on(ParticipantEvent.TrackSubscriptionPermissionChanged, (pub, status) => {
2558
+ this.emitWhenConnected(
2559
+ RoomEvent.TrackSubscriptionPermissionChanged,
2560
+ pub,
2561
+ status,
2562
+ participant,
2563
+ );
2564
+ })
2565
+ .on(ParticipantEvent.Active, () => {
2566
+ this.emitWhenConnected(RoomEvent.ParticipantActive, participant);
2567
+ if (participant.kind === ParticipantKind.AGENT) {
2568
+ this.localParticipant.setActiveAgent(participant);
2569
+ }
2570
+ });
2571
+
2572
+ // update info at the end after callbacks have been set up
2573
+ if (info) {
2574
+ participant.updateInfo(info);
2575
+ }
2576
+ return participant;
2577
+ }
2578
+
2579
+ private sendSyncState() {
2580
+ const remoteTracks = Array.from(this.remoteParticipants.values()).reduce((acc, participant) => {
2581
+ acc.push(...(participant.getTrackPublications() as RemoteTrackPublication[])); // FIXME would be nice to have this return RemoteTrackPublications directly instead of the type cast
2582
+ return acc;
2583
+ }, [] as RemoteTrackPublication[]);
2584
+ const localTracks = this.localParticipant.getTrackPublications() as LocalTrackPublication[]; // FIXME would be nice to have this return LocalTrackPublications directly instead of the type cast
2585
+ const publishDataTracks = ([] as PublishDataTrackResponse[]).concat(
2586
+ ...this.extensionResults.map((result) => result.syncState?.() ?? []),
2587
+ );
2588
+ this.engine.sendSyncState(remoteTracks, localTracks, publishDataTracks);
2589
+ }
2590
+
2591
+ /**
2592
+ * After resuming, we'll need to notify the server of the current
2593
+ * subscription settings.
2594
+ */
2595
+ private updateSubscriptions() {
2596
+ for (const p of this.remoteParticipants.values()) {
2597
+ for (const pub of p.videoTrackPublications.values()) {
2598
+ if (pub.isSubscribed && isRemotePub(pub)) {
2599
+ pub.emitTrackUpdate();
2600
+ }
2601
+ }
2602
+ }
2603
+ }
2604
+
2605
+ private getRemoteParticipantBySid(sid: string): RemoteParticipant | undefined {
2606
+ const identity = this.sidToIdentity.get(sid);
2607
+ if (identity) {
2608
+ const participant = this.remoteParticipants.get(identity);
2609
+ // an older session's sid must not resolve to the current session of the same identity
2610
+ if (participant?.sid === sid) {
2611
+ return participant;
2612
+ }
2613
+ }
2614
+ }
2615
+
2616
+ /** The client capabilities this SDK advertises to other participants in its `ClientInfo`. */
2617
+ private getClientInfoCapabilities(): Array<ClientInfo_Capability> {
2618
+ const capabilities: Array<ClientInfo_Capability> = [];
2619
+ if (isFrameMetadataSupported(this.slots.frameMetadataOptions) || !!this.slots.e2eeManager) {
2620
+ capabilities.push(ClientInfo_Capability.CAP_PACKET_TRAILER);
2621
+ }
2622
+ // Advertise deflate-raw decompression support so peers know they can send us compressed data
2623
+ // streams (gated separately from clientProtocol — see the data streams v2 spec).
2624
+ if (isDeflateRawCompressionSupported()) {
2625
+ capabilities.push(ClientInfo_Capability.CAP_COMPRESSION_DEFLATE_RAW);
2626
+ }
2627
+ return capabilities;
2628
+ }
2629
+
2630
+ private setStatsLogging(enabled: boolean) {
2631
+ if (enabled) {
2632
+ if (!this.statsLogInterval) {
2633
+ this.statsLogInterval = CriticalTimers.setInterval(() => {
2634
+ // logWebRTCStats handles its own errors, nothing to await here
2635
+ this.logWebRTCStats();
2636
+ }, STATS_LOG_FREQUENCY_MS);
2637
+ }
2638
+ } else if (this.statsLogInterval) {
2639
+ CriticalTimers.clearInterval(this.statsLogInterval);
2640
+ this.statsLogInterval = undefined;
2641
+ }
2642
+ }
2643
+
2644
+ /**
2645
+ * Dumps stats of both peer connections.
2646
+ */
2647
+ private logWebRTCStats = async () => {
2648
+ const pcManager = this.engine?.pcManager;
2649
+ if (!pcManager) {
2650
+ return;
2651
+ }
2652
+ try {
2653
+ const [publisher, subscriber] = await Promise.all([
2654
+ pcManager.publisher.getStats(),
2655
+ pcManager.subscriber?.getStats(),
2656
+ ]);
2657
+ const publisherStats = publisher && summarizeStatsReport(publisher);
2658
+ const subscriberStats = subscriber && summarizeStatsReport(subscriber);
2659
+ this.statsLog.info(`webrtc stats`, {
2660
+ publisher: publisherStats?.connection,
2661
+ subscriber: subscriberStats?.connection,
2662
+ inbound: [...(publisherStats?.inbound ?? []), ...(subscriberStats?.inbound ?? [])],
2663
+ outbound: publisherStats?.outbound,
2664
+ });
2665
+ } catch (error) {
2666
+ this.statsLog.debug('could not collect webrtc stats', { error });
2667
+ }
2668
+ };
2669
+
2670
+ private registerConnectionReconcile() {
2671
+ this.clearConnectionReconcile();
2672
+ let consecutiveFailures = 0;
2673
+ this.connectionReconcileInterval = CriticalTimers.setInterval(() => {
2674
+ if (
2675
+ // ensure we didn't tear it down
2676
+ !this.engine ||
2677
+ // engine detected close, but Room missed it
2678
+ this.engine.isClosed ||
2679
+ // transports failed without notifying engine
2680
+ !this.engine.verifyTransport()
2681
+ ) {
2682
+ consecutiveFailures++;
2683
+ this.log.warn('detected connection state mismatch', {
2684
+ numFailures: consecutiveFailures,
2685
+ engine: this.engine
2686
+ ? {
2687
+ closed: this.engine.isClosed,
2688
+ transportsConnectedOrConnecting: this.engine.verifyTransport(),
2689
+ }
2690
+ : undefined,
2691
+ });
2692
+ if (consecutiveFailures >= 3) {
2693
+ this.clearConnectionReconcile();
2694
+ if (this.engine && !this.engine.isClosed) {
2695
+ // The transport silently died while we still looked connected. Try a full reconnect
2696
+ // (keeps the room alive; the engine falls back to Disconnected if it ultimately fails).
2697
+ this.log.warn('detected connection state mismatch, attempting full reconnect');
2698
+ this.engine.reconnect();
2699
+ } else {
2700
+ // No usable engine to reconnect with; tear down.
2701
+ this.recreateEngine();
2702
+ this.handleDisconnect(
2703
+ this.options.stopLocalTrackOnUnpublish,
2704
+ DisconnectReason.STATE_MISMATCH,
2705
+ );
2706
+ }
2707
+ }
2708
+ } else {
2709
+ consecutiveFailures = 0;
2710
+ }
2711
+ }, CONNECTION_RECONCILE_FREQUENCY_MS);
2712
+ }
2713
+
2714
+ private clearConnectionReconcile() {
2715
+ if (this.connectionReconcileInterval) {
2716
+ CriticalTimers.clearInterval(this.connectionReconcileInterval);
2717
+ }
2718
+ }
2719
+
2720
+ private setAndEmitConnectionState(state: ConnectionState): boolean {
2721
+ if (state === this.state) {
2722
+ // unchanged
2723
+ return false;
2724
+ }
2725
+ this.log.info(`connection state changed: ${this.state} -> ${state}`);
2726
+ this.state = state;
2727
+ this.setStatsLogging(state === ConnectionState.Connected);
2728
+
2729
+ this.emit(RoomEvent.ConnectionStateChanged, this.state);
2730
+
2731
+ return true;
2732
+ }
2733
+
2734
+ private emitBufferedEvents() {
2735
+ this.bufferedEvents.forEach(([ev, args]) => {
2736
+ this.emit(ev, ...args);
2737
+ });
2738
+ this.bufferedEvents = [];
2739
+ }
2740
+
2741
+ private emitWhenConnected<E extends keyof RoomEventCallbacks>(
2742
+ event: E,
2743
+ ...args: Parameters<RoomEventCallbacks[E]>
2744
+ ): boolean {
2745
+ if (
2746
+ this.state === ConnectionState.Reconnecting ||
2747
+ this.isResuming ||
2748
+ !this.engine ||
2749
+ this.engine.pendingReconnect
2750
+ ) {
2751
+ // in case the room is reconnecting, buffer the events by firing them later after emitting RoomEvent.Reconnected
2752
+ this.bufferedEvents.push([event, args]);
2753
+ } else if (this.state === ConnectionState.Connected) {
2754
+ return this.emit(event, ...args);
2755
+ }
2756
+ return false;
2757
+ }
2758
+
2759
+ private onLocalParticipantMetadataChanged = (metadata: string | undefined) => {
2760
+ this.emit(RoomEvent.ParticipantMetadataChanged, metadata, this.localParticipant);
2761
+ };
2762
+
2763
+ private onLocalParticipantNameChanged = (name: string) => {
2764
+ this.emit(RoomEvent.ParticipantNameChanged, name, this.localParticipant);
2765
+ };
2766
+
2767
+ private onLocalAttributesChanged = (changedAttributes: Record<string, string>) => {
2768
+ this.emit(RoomEvent.ParticipantAttributesChanged, changedAttributes, this.localParticipant);
2769
+ };
2770
+
2771
+ private onLocalTrackMuted = (pub: TrackPublication) => {
2772
+ this.emit(RoomEvent.TrackMuted, pub, this.localParticipant);
2773
+ };
2774
+
2775
+ private onLocalTrackUnmuted = (pub: TrackPublication) => {
2776
+ this.emit(RoomEvent.TrackUnmuted, pub, this.localParticipant);
2777
+ };
2778
+
2779
+ private onTrackProcessorUpdate = (processor?: TrackProcessor<Track.Kind, any>) => {
2780
+ processor?.onPublish?.(this);
2781
+ };
2782
+
2783
+ private onLocalTrackPublished = async (pub: LocalTrackPublication) => {
2784
+ pub.track?.on(TrackEvent.TrackProcessorUpdate, this.onTrackProcessorUpdate);
2785
+ pub.track?.on(TrackEvent.Restarted, this.onLocalTrackRestarted);
2786
+ pub.track?.getProcessor()?.onPublish?.(this);
2787
+
2788
+ this.emit(RoomEvent.LocalTrackPublished, pub, this.localParticipant);
2789
+
2790
+ if (isLocalAudioTrack(pub.track)) {
2791
+ const trackIsSilent = await pub.track.checkForSilence();
2792
+ if (trackIsSilent) {
2793
+ this.emit(RoomEvent.LocalAudioSilenceDetected, pub);
2794
+ }
2795
+ }
2796
+ const deviceId = await pub.track?.getDeviceId(false);
2797
+ const deviceKind = sourceToKind(pub.source);
2798
+ if (
2799
+ deviceKind &&
2800
+ deviceId &&
2801
+ deviceId !== this.localParticipant.activeDeviceMap.get(deviceKind)
2802
+ ) {
2803
+ this.localParticipant.activeDeviceMap.set(deviceKind, deviceId);
2804
+ this.emit(RoomEvent.ActiveDeviceChanged, deviceKind, deviceId);
2805
+ }
2806
+ };
2807
+
2808
+ private onLocalTrackUnpublished = (pub: LocalTrackPublication) => {
2809
+ pub.track?.off(TrackEvent.TrackProcessorUpdate, this.onTrackProcessorUpdate);
2810
+ pub.track?.off(TrackEvent.Restarted, this.onLocalTrackRestarted);
2811
+ this.emit(RoomEvent.LocalTrackUnpublished, pub, this.localParticipant);
2812
+ };
2813
+
2814
+ private onLocalTrackRestarted = async (track: LocalTrack) => {
2815
+ const deviceId = await track.getDeviceId(false);
2816
+ const deviceKind = sourceToKind(track.source);
2817
+ if (
2818
+ deviceKind &&
2819
+ deviceId &&
2820
+ deviceId !== this.localParticipant.activeDeviceMap.get(deviceKind)
2821
+ ) {
2822
+ this.log.debug(`local track restarted, setting ${deviceKind} ${deviceId} active`);
2823
+ this.localParticipant.activeDeviceMap.set(deviceKind, deviceId);
2824
+ this.emit(RoomEvent.ActiveDeviceChanged, deviceKind, deviceId);
2825
+ }
2826
+ };
2827
+
2828
+ private onLocalConnectionQualityChanged = (quality: ConnectionQuality) => {
2829
+ this.emit(RoomEvent.ConnectionQualityChanged, quality, this.localParticipant);
2830
+ };
2831
+
2832
+ private onMediaDevicesError = (e: Error, kind?: MediaDeviceKind) => {
2833
+ this.emit(RoomEvent.MediaDevicesError, e, kind);
2834
+ };
2835
+
2836
+ private onLocalParticipantPermissionsChanged = (prevPermissions?: ParticipantPermission) => {
2837
+ this.emit(RoomEvent.ParticipantPermissionsChanged, prevPermissions, this.localParticipant);
2838
+ };
2839
+
2840
+ /** Puts the room into the connected state without a server (`ExtensionContext.simulateConnected`). */
2841
+ private simulateConnected(roomInfo: RoomModel, localParticipantInfo: ParticipantInfo) {
2842
+ this.handleDisconnect();
2843
+ this.roomInfo = roomInfo;
2844
+ this.localParticipant.updateInfo(localParticipantInfo);
2845
+ this.setupLocalParticipantEvents();
2846
+ this.emit(RoomEvent.SignalConnected);
2847
+ this.emit(RoomEvent.Connected);
2848
+ this.setAndEmitConnectionState(ConnectionState.Connected);
2849
+ }
2850
+
2851
+ // /** @internal */
2852
+ emit<E extends keyof RoomEventCallbacks>(
2853
+ event: E,
2854
+ ...args: Parameters<RoomEventCallbacks[E]>
2855
+ ): boolean {
2856
+ // active speaker updates are too spammy
2857
+ if (event !== RoomEvent.ActiveSpeakersChanged && event !== RoomEvent.TranscriptionReceived) {
2858
+ // only extract logContext from arguments in order to avoid logging the whole object tree
2859
+ const minimizedArgs = mapArgs(args).filter((arg: unknown) => arg !== undefined);
2860
+ if (event === RoomEvent.TrackSubscribed || event === RoomEvent.TrackUnsubscribed) {
2861
+ this.log.trace(`subscribe trace: ${event}`, { event, args: minimizedArgs });
2862
+ }
2863
+ this.log.debug(`room event ${event}`, { event, args: minimizedArgs });
2864
+ }
2865
+ return super.emit(event, ...args);
2866
+ }
2867
+ }
2868
+
2869
+ function mapArgs(args: unknown[]): any {
2870
+ return args.map((arg: unknown) => {
2871
+ if (!arg) {
2872
+ return;
2873
+ }
2874
+ if (Array.isArray(arg)) {
2875
+ return mapArgs(arg);
2876
+ }
2877
+ if (typeof arg === 'object') {
2878
+ return 'logContext' in arg ? arg.logContext : undefined;
2879
+ }
2880
+ return arg;
2881
+ });
2882
+ }
2883
+
2884
+ /** Copies an extension's API onto `target`. Refuses to redefine an existing member. */
2885
+ /**
2886
+ * Copies an extension's API onto `target`. A name that core (`base`) or an earlier extension
2887
+ * already defines is a collision. A name a subclass of `base` defines is an override: the
2888
+ * subclass method stays and the extension's is not installed, so `Room` subclasses that
2889
+ * override a moved method keep working (there is no `super` to call, the override replaces it).
2890
+ */
2891
+ function defineExtensionApi(
2892
+ target: object,
2893
+ base: object,
2894
+ api: object | undefined,
2895
+ ext: RoomExtension<any, any>,
2896
+ ) {
2897
+ if (!api) {
2898
+ return;
2899
+ }
2900
+ const descriptors = Object.getOwnPropertyDescriptors(api);
2901
+ for (const name of Object.keys(descriptors)) {
2902
+ if (Object.getOwnPropertyDescriptor(target, name) || name in base) {
2903
+ // @throws-transformer ignore - programmer error
2904
+ throw new Error(`extension '${String(ext.key)}' redefines '${name}'`);
2905
+ }
2906
+ if (name in target) {
2907
+ delete descriptors[name];
2908
+ }
2909
+ }
2910
+ // defineProperties (not Object.assign) keeps getters intact
2911
+ Object.defineProperties(target, descriptors);
2912
+ }
2913
+
2914
+ export default CoreRoom;
2915
+
2916
+ export type RoomEventCallbacks = {
2917
+ connected: () => void;
2918
+ reconnecting: () => void;
2919
+ signalReconnecting: () => void;
2920
+ reconnected: () => void;
2921
+ disconnected: (reason?: DisconnectReason) => void;
2922
+ connectionStateChanged: (state: ConnectionState) => void;
2923
+ moved: (name: string) => void;
2924
+ mediaDevicesChanged: () => void;
2925
+ participantConnected: (participant: RemoteParticipant) => void;
2926
+ participantDisconnected: (
2927
+ participant: RemoteParticipant,
2928
+ disconnectReason?: DisconnectReason,
2929
+ ) => void;
2930
+ trackPublished: (publication: RemoteTrackPublication, participant: RemoteParticipant) => void;
2931
+ trackSubscribed: (
2932
+ track: RemoteTrack,
2933
+ publication: RemoteTrackPublication,
2934
+ participant: RemoteParticipant,
2935
+ ) => void;
2936
+ trackSubscriptionFailed: (
2937
+ trackSid: string,
2938
+ participant: RemoteParticipant,
2939
+ reason?: SubscriptionError,
2940
+ ) => void;
2941
+ trackUnpublished: (publication: RemoteTrackPublication, participant: RemoteParticipant) => void;
2942
+ trackUnsubscribed: (
2943
+ track: RemoteTrack,
2944
+ publication: RemoteTrackPublication,
2945
+ participant: RemoteParticipant,
2946
+ ) => void;
2947
+ trackMuted: (publication: TrackPublication, participant: Participant) => void;
2948
+ trackUnmuted: (publication: TrackPublication, participant: Participant) => void;
2949
+ localTrackPublished: (publication: LocalTrackPublication, participant: LocalParticipant) => void;
2950
+ localTrackUnpublished: (
2951
+ publication: LocalTrackPublication,
2952
+ participant: LocalParticipant,
2953
+ ) => void;
2954
+ localAudioSilenceDetected: (publication: LocalTrackPublication) => void;
2955
+ participantMetadataChanged: (
2956
+ metadata: string | undefined,
2957
+ participant: RemoteParticipant | LocalParticipant,
2958
+ ) => void;
2959
+ participantNameChanged: (name: string, participant: RemoteParticipant | LocalParticipant) => void;
2960
+ participantPermissionsChanged: (
2961
+ prevPermissions: ParticipantPermission | undefined,
2962
+ participant: RemoteParticipant | LocalParticipant,
2963
+ ) => void;
2964
+ participantAttributesChanged: (
2965
+ changedAttributes: Record<string, string>,
2966
+ participant: RemoteParticipant | LocalParticipant,
2967
+ ) => void;
2968
+ activeSpeakersChanged: (speakers: Array<Participant>) => void;
2969
+ roomMetadataChanged: (metadata: string) => void;
2970
+ dataReceived: (
2971
+ payload: NonSharedUint8Array,
2972
+ participant?: RemoteParticipant,
2973
+ kind?: DataPacket_Kind,
2974
+ topic?: string,
2975
+ encryptionType?: Encryption_Type,
2976
+ participantIdentity?: string,
2977
+ ) => void;
2978
+ sipDTMFReceived: (dtmf: SipDTMF, participant?: RemoteParticipant) => void;
2979
+ transcriptionReceived: (
2980
+ transcription: TranscriptionSegment[],
2981
+ participant?: Participant,
2982
+ publication?: TrackPublication,
2983
+ ) => void;
2984
+ connectionQualityChanged: (quality: ConnectionQuality, participant: Participant) => void;
2985
+ mediaDevicesError: (error: Error, kind?: MediaDeviceKind) => void;
2986
+ trackStreamStateChanged: (
2987
+ publication: RemoteTrackPublication,
2988
+ streamState: Track.StreamState,
2989
+ participant: RemoteParticipant,
2990
+ ) => void;
2991
+ trackSubscriptionPermissionChanged: (
2992
+ publication: RemoteTrackPublication,
2993
+ status: TrackPublication.PermissionStatus,
2994
+ participant: RemoteParticipant,
2995
+ ) => void;
2996
+ trackSubscriptionStatusChanged: (
2997
+ publication: RemoteTrackPublication,
2998
+ status: TrackPublication.SubscriptionStatus,
2999
+ participant: RemoteParticipant,
3000
+ ) => void;
3001
+ audioPlaybackChanged: (playing: boolean) => void;
3002
+ videoPlaybackChanged: (playing: boolean) => void;
3003
+ signalConnected: () => void;
3004
+ recordingStatusChanged: (recording: boolean) => void;
3005
+ participantEncryptionStatusChanged: (encrypted: boolean, participant?: Participant) => void;
3006
+ encryptionError: (error: Error, participant?: Participant) => void;
3007
+ dcBufferStatusChanged: (isLow: boolean, kind: DataChannelKind) => void;
3008
+ activeDeviceChanged: (kind: MediaDeviceKind, deviceId: string) => void;
3009
+ chatMessage: (message: ChatMessage, participant?: RemoteParticipant | LocalParticipant) => void;
3010
+ localTrackSubscribed: (publication: LocalTrackPublication, participant: LocalParticipant) => void;
3011
+ metricsReceived: (metrics: MetricsBatch, participant?: Participant) => void;
3012
+ participantActive: (participant: Participant) => void;
3013
+ dataTrackPublished: (track: RemoteDataTrack) => void;
3014
+ dataTrackUnpublished: (sid: DataTrackSid) => void;
3015
+ localDataTrackPublished: (track: LocalDataTrack) => void;
3016
+ localDataTrackUnpublished: (sid: DataTrackSid) => void;
3017
+ };