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