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