electron-cast 0.3.1 → 0.5.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (758) hide show
  1. package/CHANGELOG.md +340 -0
  2. package/CONTRIBUTING.md +305 -0
  3. package/README.md +215 -60
  4. package/SECURITY.md +324 -0
  5. package/dist/cjs/client/auth/build-time.d.ts +7 -0
  6. package/dist/cjs/client/auth/build-time.js +11 -0
  7. package/dist/cjs/client/auth/build-time.js.map +1 -0
  8. package/dist/cjs/client/auth/cast-auth.d.ts +46 -0
  9. package/dist/cjs/client/auth/cast-auth.js +145 -0
  10. package/dist/cjs/client/auth/cast-auth.js.map +1 -0
  11. package/dist/cjs/client/auth/cast-cert-validator.d.ts +34 -0
  12. package/dist/cjs/client/auth/cast-cert-validator.js +89 -0
  13. package/dist/cjs/client/auth/cast-cert-validator.js.map +1 -0
  14. package/dist/cjs/client/auth/crl.d.ts +45 -0
  15. package/dist/cjs/client/auth/crl.js +227 -0
  16. package/dist/cjs/client/auth/crl.js.map +1 -0
  17. package/dist/cjs/client/auth/der.d.ts +62 -0
  18. package/dist/cjs/client/auth/der.js +208 -0
  19. package/dist/cjs/client/auth/der.js.map +1 -0
  20. package/dist/cjs/client/auth/names.d.ts +52 -0
  21. package/dist/cjs/client/auth/names.js +447 -0
  22. package/dist/cjs/client/auth/names.js.map +1 -0
  23. package/dist/cjs/client/auth/path-validator.d.ts +29 -0
  24. package/dist/cjs/client/auth/path-validator.js +421 -0
  25. package/dist/cjs/client/auth/path-validator.js.map +1 -0
  26. package/dist/cjs/client/auth/trust-data.d.ts +24 -0
  27. package/dist/cjs/client/auth/trust-data.js +97 -0
  28. package/dist/cjs/client/auth/trust-data.js.map +1 -0
  29. package/dist/cjs/client/auth/x509.d.ts +109 -0
  30. package/dist/cjs/client/auth/x509.js +442 -0
  31. package/dist/cjs/client/auth/x509.js.map +1 -0
  32. package/dist/cjs/client/cast-client.d.ts +111 -0
  33. package/dist/cjs/client/cast-client.js +505 -0
  34. package/dist/cjs/client/cast-client.js.map +1 -0
  35. package/dist/cjs/client/errors.d.ts +28 -0
  36. package/dist/cjs/client/errors.js +50 -0
  37. package/dist/cjs/client/errors.js.map +1 -0
  38. package/dist/cjs/client/index.d.ts +6 -0
  39. package/dist/cjs/client/index.js +21 -0
  40. package/dist/cjs/client/index.js.map +1 -0
  41. package/dist/cjs/client/namespaces.d.ts +9 -0
  42. package/dist/cjs/client/namespaces.js +13 -0
  43. package/dist/cjs/client/namespaces.js.map +1 -0
  44. package/dist/cjs/client/types.d.ts +27 -0
  45. package/dist/cjs/client/types.js +3 -0
  46. package/dist/cjs/client/types.js.map +1 -0
  47. package/dist/cjs/dial/dial-registry.d.ts +104 -0
  48. package/dist/cjs/dial/dial-registry.js +301 -0
  49. package/dist/cjs/dial/dial-registry.js.map +1 -0
  50. package/dist/cjs/dial/dial-sink-service.d.ts +108 -0
  51. package/dist/cjs/dial/dial-sink-service.js +314 -0
  52. package/dist/cjs/dial/dial-sink-service.js.map +1 -0
  53. package/dist/cjs/dial/http.d.ts +40 -0
  54. package/dist/cjs/dial/http.js +172 -0
  55. package/dist/cjs/dial/http.js.map +1 -0
  56. package/dist/cjs/dial/index.d.ts +63 -0
  57. package/dist/cjs/dial/index.js +134 -0
  58. package/dist/cjs/dial/index.js.map +1 -0
  59. package/dist/cjs/dial/origins.d.ts +4 -0
  60. package/dist/cjs/dial/origins.js +36 -0
  61. package/dist/cjs/dial/origins.js.map +1 -0
  62. package/dist/cjs/dial/parse.d.ts +101 -0
  63. package/dist/cjs/dial/parse.js +296 -0
  64. package/dist/cjs/dial/parse.js.map +1 -0
  65. package/dist/cjs/dial/route-provider.d.ts +96 -0
  66. package/dist/cjs/dial/route-provider.js +376 -0
  67. package/dist/cjs/dial/route-provider.js.map +1 -0
  68. package/dist/cjs/dial/ssdp-socket.d.ts +34 -0
  69. package/dist/cjs/dial/ssdp-socket.js +72 -0
  70. package/dist/cjs/dial/ssdp-socket.js.map +1 -0
  71. package/dist/cjs/dial/xml.d.ts +13 -0
  72. package/dist/cjs/dial/xml.js +208 -0
  73. package/dist/cjs/dial/xml.js.map +1 -0
  74. package/dist/cjs/discovery/cast-discovery.d.ts +105 -0
  75. package/dist/cjs/discovery/cast-discovery.js +323 -0
  76. package/dist/cjs/discovery/cast-discovery.js.map +1 -0
  77. package/dist/cjs/discovery/device-cache.d.ts +54 -0
  78. package/dist/cjs/discovery/device-cache.js +240 -0
  79. package/dist/cjs/discovery/device-cache.js.map +1 -0
  80. package/dist/cjs/discovery/dns-packet.d.ts +70 -0
  81. package/dist/cjs/discovery/dns-packet.js +251 -0
  82. package/dist/cjs/discovery/dns-packet.js.map +1 -0
  83. package/dist/cjs/discovery/index.d.ts +5 -0
  84. package/dist/cjs/discovery/index.js +22 -0
  85. package/dist/cjs/discovery/index.js.map +1 -0
  86. package/dist/cjs/discovery/ip-policy.d.ts +20 -0
  87. package/dist/cjs/discovery/ip-policy.js +109 -0
  88. package/dist/cjs/discovery/ip-policy.js.map +1 -0
  89. package/dist/cjs/discovery/udp-socket.d.ts +19 -0
  90. package/dist/cjs/discovery/udp-socket.js +166 -0
  91. package/dist/cjs/discovery/udp-socket.js.map +1 -0
  92. package/dist/cjs/electron/access-code.d.ts +221 -0
  93. package/dist/cjs/electron/access-code.js +586 -0
  94. package/dist/cjs/electron/access-code.js.map +1 -0
  95. package/dist/cjs/electron/app-discovery.d.ts +82 -0
  96. package/dist/cjs/electron/app-discovery.js +219 -0
  97. package/dist/cjs/electron/app-discovery.js.map +1 -0
  98. package/dist/cjs/electron/default-presentation.d.ts +38 -0
  99. package/dist/cjs/electron/default-presentation.js +88 -0
  100. package/dist/cjs/electron/default-presentation.js.map +1 -0
  101. package/dist/cjs/electron/main.d.ts +173 -0
  102. package/dist/cjs/electron/main.js +990 -0
  103. package/dist/cjs/electron/main.js.map +1 -0
  104. package/dist/cjs/electron/manager-api.d.ts +314 -0
  105. package/dist/cjs/electron/manager-api.js +402 -0
  106. package/dist/cjs/electron/manager-api.js.map +1 -0
  107. package/dist/cjs/electron/media-controller.d.ts +79 -0
  108. package/dist/cjs/electron/media-controller.js +251 -0
  109. package/dist/cjs/electron/media-controller.js.map +1 -0
  110. package/dist/cjs/electron/network-monitor.d.ts +61 -0
  111. package/dist/cjs/electron/network-monitor.js +124 -0
  112. package/dist/cjs/electron/network-monitor.js.map +1 -0
  113. package/dist/cjs/electron/preload.d.ts +39 -0
  114. package/dist/cjs/electron/preload.js +96 -0
  115. package/dist/cjs/electron/preload.js.map +1 -0
  116. package/dist/cjs/electron/presentation-envelope.d.ts +60 -0
  117. package/dist/cjs/electron/presentation-envelope.js +318 -0
  118. package/dist/cjs/electron/presentation-envelope.js.map +1 -0
  119. package/dist/cjs/electron/presentation-handler.d.ts +135 -0
  120. package/dist/cjs/electron/presentation-handler.js +643 -0
  121. package/dist/cjs/electron/presentation-handler.js.map +1 -0
  122. package/dist/cjs/electron/presentation-source.d.ts +76 -0
  123. package/dist/cjs/electron/presentation-source.js +132 -0
  124. package/dist/cjs/electron/presentation-source.js.map +1 -0
  125. package/dist/cjs/electron/session-service.d.ts +348 -0
  126. package/dist/cjs/electron/session-service.js +1273 -0
  127. package/dist/cjs/electron/session-service.js.map +1 -0
  128. package/dist/cjs/electron/session-tracker.d.ts +82 -0
  129. package/dist/cjs/electron/session-tracker.js +225 -0
  130. package/dist/cjs/electron/session-tracker.js.map +1 -0
  131. package/dist/cjs/electron/sink-service.d.ts +246 -0
  132. package/dist/cjs/electron/sink-service.js +606 -0
  133. package/dist/cjs/electron/sink-service.js.map +1 -0
  134. package/dist/cjs/electron/wired-display/controller.d.ts +76 -0
  135. package/dist/cjs/electron/wired-display/controller.js +198 -0
  136. package/dist/cjs/electron/wired-display/controller.js.map +1 -0
  137. package/dist/cjs/electron/wired-display/displays.d.ts +44 -0
  138. package/dist/cjs/electron/wired-display/displays.js +98 -0
  139. package/dist/cjs/electron/wired-display/displays.js.map +1 -0
  140. package/dist/cjs/electron/wired-display/electron-windows.d.ts +111 -0
  141. package/dist/cjs/electron/wired-display/electron-windows.js +306 -0
  142. package/dist/cjs/electron/wired-display/electron-windows.js.map +1 -0
  143. package/dist/cjs/electron/wired-display/index.d.ts +90 -0
  144. package/dist/cjs/electron/wired-display/index.js +115 -0
  145. package/dist/cjs/electron/wired-display/index.js.map +1 -0
  146. package/dist/cjs/electron/wired-display/local-presentation.d.ts +98 -0
  147. package/dist/cjs/electron/wired-display/local-presentation.js +234 -0
  148. package/dist/cjs/electron/wired-display/local-presentation.js.map +1 -0
  149. package/dist/cjs/electron/wired-display/provider.d.ts +112 -0
  150. package/dist/cjs/electron/wired-display/provider.js +202 -0
  151. package/dist/cjs/electron/wired-display/provider.js.map +1 -0
  152. package/dist/cjs/index.d.ts +9 -0
  153. package/dist/cjs/index.js +27 -0
  154. package/dist/cjs/index.js.map +1 -0
  155. package/dist/cjs/mirroring/backend.d.ts +76 -0
  156. package/dist/cjs/mirroring/backend.js +21 -0
  157. package/dist/cjs/mirroring/backend.js.map +1 -0
  158. package/dist/cjs/mirroring/electron/agent-protocol.d.ts +158 -0
  159. package/dist/cjs/mirroring/electron/agent-protocol.js +11 -0
  160. package/dist/cjs/mirroring/electron/agent-protocol.js.map +1 -0
  161. package/dist/cjs/mirroring/electron/agent-source.d.ts +20 -0
  162. package/dist/cjs/mirroring/electron/agent-source.js +621 -0
  163. package/dist/cjs/mirroring/electron/agent-source.js.map +1 -0
  164. package/dist/cjs/mirroring/electron/engine-process.d.ts +87 -0
  165. package/dist/cjs/mirroring/electron/engine-process.js +81 -0
  166. package/dist/cjs/mirroring/electron/engine-process.js.map +1 -0
  167. package/dist/cjs/mirroring/electron/host.d.ts +89 -0
  168. package/dist/cjs/mirroring/electron/host.js +321 -0
  169. package/dist/cjs/mirroring/electron/host.js.map +1 -0
  170. package/dist/cjs/mirroring/electron/port-backend.d.ts +107 -0
  171. package/dist/cjs/mirroring/electron/port-backend.js +446 -0
  172. package/dist/cjs/mirroring/electron/port-backend.js.map +1 -0
  173. package/dist/cjs/mirroring/encoding-support.d.ts +66 -0
  174. package/dist/cjs/mirroring/encoding-support.js +89 -0
  175. package/dist/cjs/mirroring/encoding-support.js.map +1 -0
  176. package/dist/cjs/mirroring/engine.d.ts +16 -0
  177. package/dist/cjs/mirroring/engine.js +78 -0
  178. package/dist/cjs/mirroring/engine.js.map +1 -0
  179. package/dist/cjs/mirroring/mirror-settings.d.ts +101 -0
  180. package/dist/cjs/mirroring/mirror-settings.js +140 -0
  181. package/dist/cjs/mirroring/mirror-settings.js.map +1 -0
  182. package/dist/cjs/mirroring/mirroring-activity.d.ts +207 -0
  183. package/dist/cjs/mirroring/mirroring-activity.js +367 -0
  184. package/dist/cjs/mirroring/mirroring-activity.js.map +1 -0
  185. package/dist/cjs/mirroring/mirroring-manager.d.ts +134 -0
  186. package/dist/cjs/mirroring/mirroring-manager.js +286 -0
  187. package/dist/cjs/mirroring/mirroring-manager.js.map +1 -0
  188. package/dist/cjs/mirroring/protocol/answer.d.ts +87 -0
  189. package/dist/cjs/mirroring/protocol/answer.js +233 -0
  190. package/dist/cjs/mirroring/protocol/answer.js.map +1 -0
  191. package/dist/cjs/mirroring/protocol/constants.d.ts +64 -0
  192. package/dist/cjs/mirroring/protocol/constants.js +70 -0
  193. package/dist/cjs/mirroring/protocol/constants.js.map +1 -0
  194. package/dist/cjs/mirroring/protocol/errors.d.ts +25 -0
  195. package/dist/cjs/mirroring/protocol/errors.js +34 -0
  196. package/dist/cjs/mirroring/protocol/errors.js.map +1 -0
  197. package/dist/cjs/mirroring/protocol/frame-id.d.ts +9 -0
  198. package/dist/cjs/mirroring/protocol/frame-id.js +24 -0
  199. package/dist/cjs/mirroring/protocol/frame-id.js.map +1 -0
  200. package/dist/cjs/mirroring/protocol/messenger.d.ts +42 -0
  201. package/dist/cjs/mirroring/protocol/messenger.js +122 -0
  202. package/dist/cjs/mirroring/protocol/messenger.js.map +1 -0
  203. package/dist/cjs/mirroring/protocol/offer.d.ts +109 -0
  204. package/dist/cjs/mirroring/protocol/offer.js +192 -0
  205. package/dist/cjs/mirroring/protocol/offer.js.map +1 -0
  206. package/dist/cjs/mirroring/protocol/recommendations.d.ts +28 -0
  207. package/dist/cjs/mirroring/protocol/recommendations.js +103 -0
  208. package/dist/cjs/mirroring/protocol/recommendations.js.map +1 -0
  209. package/dist/cjs/mirroring/routing.d.ts +60 -0
  210. package/dist/cjs/mirroring/routing.js +147 -0
  211. package/dist/cjs/mirroring/routing.js.map +1 -0
  212. package/dist/cjs/mirroring/rtcp/ntp.d.ts +27 -0
  213. package/dist/cjs/mirroring/rtcp/ntp.js +62 -0
  214. package/dist/cjs/mirroring/rtcp/ntp.js.map +1 -0
  215. package/dist/cjs/mirroring/rtcp/parser.d.ts +50 -0
  216. package/dist/cjs/mirroring/rtcp/parser.js +270 -0
  217. package/dist/cjs/mirroring/rtcp/parser.js.map +1 -0
  218. package/dist/cjs/mirroring/rtcp/sender-report.d.ts +32 -0
  219. package/dist/cjs/mirroring/rtcp/sender-report.js +56 -0
  220. package/dist/cjs/mirroring/rtcp/sender-report.js.map +1 -0
  221. package/dist/cjs/mirroring/rtcp/wire.d.ts +54 -0
  222. package/dist/cjs/mirroring/rtcp/wire.js +73 -0
  223. package/dist/cjs/mirroring/rtcp/wire.js.map +1 -0
  224. package/dist/cjs/mirroring/rtp/frame-crypto.d.ts +11 -0
  225. package/dist/cjs/mirroring/rtp/frame-crypto.js +39 -0
  226. package/dist/cjs/mirroring/rtp/frame-crypto.js.map +1 -0
  227. package/dist/cjs/mirroring/rtp/frame.d.ts +23 -0
  228. package/dist/cjs/mirroring/rtp/frame.js +3 -0
  229. package/dist/cjs/mirroring/rtp/frame.js.map +1 -0
  230. package/dist/cjs/mirroring/rtp/packetizer.d.ts +27 -0
  231. package/dist/cjs/mirroring/rtp/packetizer.js +92 -0
  232. package/dist/cjs/mirroring/rtp/packetizer.js.map +1 -0
  233. package/dist/cjs/mirroring/sender/audio-sender.d.ts +23 -0
  234. package/dist/cjs/mirroring/sender/audio-sender.js +52 -0
  235. package/dist/cjs/mirroring/sender/audio-sender.js.map +1 -0
  236. package/dist/cjs/mirroring/sender/bandwidth-estimator.d.ts +26 -0
  237. package/dist/cjs/mirroring/sender/bandwidth-estimator.js +107 -0
  238. package/dist/cjs/mirroring/sender/bandwidth-estimator.js.map +1 -0
  239. package/dist/cjs/mirroring/sender/capture-size-oracle.d.ts +121 -0
  240. package/dist/cjs/mirroring/sender/capture-size-oracle.js +440 -0
  241. package/dist/cjs/mirroring/sender/capture-size-oracle.js.map +1 -0
  242. package/dist/cjs/mirroring/sender/frame-sender.d.ts +103 -0
  243. package/dist/cjs/mirroring/sender/frame-sender.js +217 -0
  244. package/dist/cjs/mirroring/sender/frame-sender.js.map +1 -0
  245. package/dist/cjs/mirroring/sender/packet-router.d.ts +74 -0
  246. package/dist/cjs/mirroring/sender/packet-router.js +196 -0
  247. package/dist/cjs/mirroring/sender/packet-router.js.map +1 -0
  248. package/dist/cjs/mirroring/sender/sender-session.d.ts +85 -0
  249. package/dist/cjs/mirroring/sender/sender-session.js +240 -0
  250. package/dist/cjs/mirroring/sender/sender-session.js.map +1 -0
  251. package/dist/cjs/mirroring/sender/sender.d.ts +120 -0
  252. package/dist/cjs/mirroring/sender/sender.js +457 -0
  253. package/dist/cjs/mirroring/sender/sender.js.map +1 -0
  254. package/dist/cjs/mirroring/sender/video-encoder-metrics.d.ts +29 -0
  255. package/dist/cjs/mirroring/sender/video-encoder-metrics.js +64 -0
  256. package/dist/cjs/mirroring/sender/video-encoder-metrics.js.map +1 -0
  257. package/dist/cjs/mirroring/sender/video-sender.d.ts +95 -0
  258. package/dist/cjs/mirroring/sender/video-sender.js +183 -0
  259. package/dist/cjs/mirroring/sender/video-sender.js.map +1 -0
  260. package/dist/cjs/mirroring/session-host.d.ts +185 -0
  261. package/dist/cjs/mirroring/session-host.js +630 -0
  262. package/dist/cjs/mirroring/session-host.js.map +1 -0
  263. package/dist/cjs/mirroring/stats/clock-offset-estimator.d.ts +11 -0
  264. package/dist/cjs/mirroring/stats/clock-offset-estimator.js +123 -0
  265. package/dist/cjs/mirroring/stats/clock-offset-estimator.js.map +1 -0
  266. package/dist/cjs/mirroring/stats/openscreen-stats-client.d.ts +12 -0
  267. package/dist/cjs/mirroring/stats/openscreen-stats-client.js +21 -0
  268. package/dist/cjs/mirroring/stats/openscreen-stats-client.js.map +1 -0
  269. package/dist/cjs/mirroring/stats/sorted-map.d.ts +23 -0
  270. package/dist/cjs/mirroring/stats/sorted-map.js +70 -0
  271. package/dist/cjs/mirroring/stats/sorted-map.js.map +1 -0
  272. package/dist/cjs/mirroring/stats/statistics-analyzer.d.ts +50 -0
  273. package/dist/cjs/mirroring/stats/statistics-analyzer.js +339 -0
  274. package/dist/cjs/mirroring/stats/statistics-analyzer.js.map +1 -0
  275. package/dist/cjs/mirroring/stats/statistics-collector.d.ts +29 -0
  276. package/dist/cjs/mirroring/stats/statistics-collector.js +53 -0
  277. package/dist/cjs/mirroring/stats/statistics-collector.js.map +1 -0
  278. package/dist/cjs/mirroring/stats/statistics-common.d.ts +61 -0
  279. package/dist/cjs/mirroring/stats/statistics-common.js +45 -0
  280. package/dist/cjs/mirroring/stats/statistics-common.js.map +1 -0
  281. package/dist/cjs/mirroring/stats/statistics-dispatcher.d.ts +28 -0
  282. package/dist/cjs/mirroring/stats/statistics-dispatcher.js +104 -0
  283. package/dist/cjs/mirroring/stats/statistics-dispatcher.js.map +1 -0
  284. package/dist/cjs/mirroring/stats/statistics.d.ts +84 -0
  285. package/dist/cjs/mirroring/stats/statistics.js +147 -0
  286. package/dist/cjs/mirroring/stats/statistics.js.map +1 -0
  287. package/dist/cjs/mirroring/transport/udp-environment.d.ts +40 -0
  288. package/dist/cjs/mirroring/transport/udp-environment.js +97 -0
  289. package/dist/cjs/mirroring/transport/udp-environment.js.map +1 -0
  290. package/dist/cjs/package.json +1 -0
  291. package/dist/cjs/polyfill/bridge-types.d.ts +97 -0
  292. package/dist/cjs/polyfill/bridge-types.js +3 -0
  293. package/dist/cjs/polyfill/bridge-types.js.map +1 -0
  294. package/dist/cjs/polyfill/install.d.ts +12 -0
  295. package/dist/cjs/polyfill/install.js +1569 -0
  296. package/dist/cjs/polyfill/install.js.map +1 -0
  297. package/dist/cjs/polyfill/presentation-receiver.d.ts +12 -0
  298. package/dist/cjs/polyfill/presentation-receiver.js +369 -0
  299. package/dist/cjs/polyfill/presentation-receiver.js.map +1 -0
  300. package/dist/cjs/polyfill/presentation-standin.d.ts +13 -0
  301. package/dist/cjs/polyfill/presentation-standin.js +421 -0
  302. package/dist/cjs/polyfill/presentation-standin.js.map +1 -0
  303. package/dist/cjs/protocol/cast-message.d.ts +20 -0
  304. package/dist/cjs/protocol/cast-message.js +171 -0
  305. package/dist/cjs/protocol/cast-message.js.map +1 -0
  306. package/dist/cjs/protocol/device-auth.d.ts +61 -0
  307. package/dist/cjs/protocol/device-auth.js +113 -0
  308. package/dist/cjs/protocol/device-auth.js.map +1 -0
  309. package/dist/cjs/protocol/errors.d.ts +4 -0
  310. package/dist/cjs/protocol/errors.js +12 -0
  311. package/dist/cjs/protocol/errors.js.map +1 -0
  312. package/dist/cjs/protocol/framing.d.ts +18 -0
  313. package/dist/cjs/protocol/framing.js +100 -0
  314. package/dist/cjs/protocol/framing.js.map +1 -0
  315. package/dist/cjs/protocol/index.d.ts +4 -0
  316. package/dist/cjs/protocol/index.js +21 -0
  317. package/dist/cjs/protocol/index.js.map +1 -0
  318. package/dist/cjs/protocol/proto-wire.d.ts +11 -0
  319. package/dist/cjs/protocol/proto-wire.js +64 -0
  320. package/dist/cjs/protocol/proto-wire.js.map +1 -0
  321. package/dist/cli.d.ts +2 -0
  322. package/dist/cli.js +56 -0
  323. package/dist/cli.js.map +1 -0
  324. package/dist/client/auth/build-time.d.ts +7 -0
  325. package/dist/client/auth/build-time.js +8 -0
  326. package/dist/client/auth/build-time.js.map +1 -0
  327. package/dist/client/auth/cast-auth.d.ts +46 -0
  328. package/dist/client/auth/cast-auth.js +141 -0
  329. package/dist/client/auth/cast-auth.js.map +1 -0
  330. package/dist/client/auth/cast-cert-validator.d.ts +34 -0
  331. package/dist/client/auth/cast-cert-validator.js +86 -0
  332. package/dist/client/auth/cast-cert-validator.js.map +1 -0
  333. package/dist/client/auth/crl.d.ts +45 -0
  334. package/dist/client/auth/crl.js +221 -0
  335. package/dist/client/auth/crl.js.map +1 -0
  336. package/dist/client/auth/der.d.ts +62 -0
  337. package/dist/client/auth/der.js +196 -0
  338. package/dist/client/auth/der.js.map +1 -0
  339. package/dist/client/auth/names.d.ts +52 -0
  340. package/dist/client/auth/names.js +438 -0
  341. package/dist/client/auth/names.js.map +1 -0
  342. package/dist/client/auth/path-validator.d.ts +29 -0
  343. package/dist/client/auth/path-validator.js +417 -0
  344. package/dist/client/auth/path-validator.js.map +1 -0
  345. package/dist/client/auth/trust-data.d.ts +24 -0
  346. package/dist/client/auth/trust-data.js +94 -0
  347. package/dist/client/auth/trust-data.js.map +1 -0
  348. package/dist/client/auth/x509.d.ts +109 -0
  349. package/dist/client/auth/x509.js +430 -0
  350. package/dist/client/auth/x509.js.map +1 -0
  351. package/dist/client/cast-client.d.ts +20 -1
  352. package/dist/client/cast-client.js +74 -3
  353. package/dist/client/cast-client.js.map +1 -1
  354. package/dist/client/errors.d.ts +11 -0
  355. package/dist/client/errors.js +13 -0
  356. package/dist/client/errors.js.map +1 -1
  357. package/dist/client/index.d.ts +2 -0
  358. package/dist/dial/dial-registry.d.ts +104 -0
  359. package/dist/dial/dial-registry.js +297 -0
  360. package/dist/dial/dial-registry.js.map +1 -0
  361. package/dist/dial/dial-sink-service.d.ts +108 -0
  362. package/dist/dial/dial-sink-service.js +310 -0
  363. package/dist/dial/dial-sink-service.js.map +1 -0
  364. package/dist/dial/http.d.ts +40 -0
  365. package/dist/dial/http.js +165 -0
  366. package/dist/dial/http.js.map +1 -0
  367. package/dist/dial/index.d.ts +63 -0
  368. package/dist/dial/index.js +115 -0
  369. package/dist/dial/index.js.map +1 -0
  370. package/dist/dial/origins.d.ts +4 -0
  371. package/dist/dial/origins.js +32 -0
  372. package/dist/dial/origins.js.map +1 -0
  373. package/dist/dial/parse.d.ts +101 -0
  374. package/dist/dial/parse.js +281 -0
  375. package/dist/dial/parse.js.map +1 -0
  376. package/dist/dial/route-provider.d.ts +96 -0
  377. package/dist/dial/route-provider.js +369 -0
  378. package/dist/dial/route-provider.js.map +1 -0
  379. package/dist/dial/ssdp-socket.d.ts +34 -0
  380. package/dist/dial/ssdp-socket.js +65 -0
  381. package/dist/dial/ssdp-socket.js.map +1 -0
  382. package/dist/dial/xml.d.ts +13 -0
  383. package/dist/dial/xml.js +204 -0
  384. package/dist/dial/xml.js.map +1 -0
  385. package/dist/dial-test/cli-main.d.ts +17 -0
  386. package/dist/dial-test/cli-main.js +130 -0
  387. package/dist/dial-test/cli-main.js.map +1 -0
  388. package/dist/dial-test/report.d.ts +55 -0
  389. package/dist/dial-test/report.js +77 -0
  390. package/dist/dial-test/report.js.map +1 -0
  391. package/dist/dial-test/runner.d.ts +42 -0
  392. package/dist/dial-test/runner.js +566 -0
  393. package/dist/dial-test/runner.js.map +1 -0
  394. package/dist/dial-test/scrub.d.ts +14 -0
  395. package/dist/dial-test/scrub.js +73 -0
  396. package/dist/dial-test/scrub.js.map +1 -0
  397. package/dist/discovery/cast-discovery.d.ts +23 -2
  398. package/dist/discovery/cast-discovery.js +94 -14
  399. package/dist/discovery/cast-discovery.js.map +1 -1
  400. package/dist/discovery/device-cache.d.ts +15 -1
  401. package/dist/discovery/device-cache.js +31 -7
  402. package/dist/discovery/device-cache.js.map +1 -1
  403. package/dist/discovery/index.d.ts +1 -0
  404. package/dist/discovery/index.js +1 -0
  405. package/dist/discovery/index.js.map +1 -1
  406. package/dist/discovery/ip-policy.d.ts +20 -0
  407. package/dist/discovery/ip-policy.js +103 -0
  408. package/dist/discovery/ip-policy.js.map +1 -0
  409. package/dist/discovery/udp-socket.d.ts +8 -1
  410. package/dist/discovery/udp-socket.js +59 -5
  411. package/dist/discovery/udp-socket.js.map +1 -1
  412. package/dist/electron/access-code.d.ts +221 -0
  413. package/dist/electron/access-code.js +577 -0
  414. package/dist/electron/access-code.js.map +1 -0
  415. package/dist/electron/app-discovery.d.ts +82 -0
  416. package/dist/electron/app-discovery.js +215 -0
  417. package/dist/electron/app-discovery.js.map +1 -0
  418. package/dist/electron/default-presentation.d.ts +38 -0
  419. package/dist/electron/default-presentation.js +83 -0
  420. package/dist/electron/default-presentation.js.map +1 -0
  421. package/dist/electron/main.d.ts +122 -13
  422. package/dist/electron/main.js +776 -63
  423. package/dist/electron/main.js.map +1 -1
  424. package/dist/electron/manager-api.d.ts +314 -0
  425. package/dist/electron/manager-api.js +390 -0
  426. package/dist/electron/manager-api.js.map +1 -0
  427. package/dist/electron/media-controller.d.ts +79 -0
  428. package/dist/electron/media-controller.js +248 -0
  429. package/dist/electron/media-controller.js.map +1 -0
  430. package/dist/electron/network-monitor.d.ts +61 -0
  431. package/dist/electron/network-monitor.js +117 -0
  432. package/dist/electron/network-monitor.js.map +1 -0
  433. package/dist/electron/preload.d.ts +5 -0
  434. package/dist/electron/preload.js +9 -1
  435. package/dist/electron/preload.js.map +1 -1
  436. package/dist/electron/presentation-envelope.js +7 -1
  437. package/dist/electron/presentation-envelope.js.map +1 -1
  438. package/dist/electron/presentation-handler.d.ts +39 -4
  439. package/dist/electron/presentation-handler.js +208 -57
  440. package/dist/electron/presentation-handler.js.map +1 -1
  441. package/dist/electron/presentation-source.d.ts +76 -0
  442. package/dist/electron/presentation-source.js +125 -0
  443. package/dist/electron/presentation-source.js.map +1 -0
  444. package/dist/electron/session-service.d.ts +225 -10
  445. package/dist/electron/session-service.js +735 -151
  446. package/dist/electron/session-service.js.map +1 -1
  447. package/dist/electron/session-tracker.d.ts +82 -0
  448. package/dist/electron/session-tracker.js +219 -0
  449. package/dist/electron/session-tracker.js.map +1 -0
  450. package/dist/electron/sink-service.d.ts +246 -0
  451. package/dist/electron/sink-service.js +602 -0
  452. package/dist/electron/sink-service.js.map +1 -0
  453. package/dist/electron/wired-display/controller.d.ts +76 -0
  454. package/dist/electron/wired-display/controller.js +194 -0
  455. package/dist/electron/wired-display/controller.js.map +1 -0
  456. package/dist/electron/wired-display/displays.d.ts +44 -0
  457. package/dist/electron/wired-display/displays.js +91 -0
  458. package/dist/electron/wired-display/displays.js.map +1 -0
  459. package/dist/electron/wired-display/electron-windows.d.ts +111 -0
  460. package/dist/electron/wired-display/electron-windows.js +301 -0
  461. package/dist/electron/wired-display/electron-windows.js.map +1 -0
  462. package/dist/electron/wired-display/index.d.ts +90 -0
  463. package/dist/electron/wired-display/index.js +99 -0
  464. package/dist/electron/wired-display/index.js.map +1 -0
  465. package/dist/electron/wired-display/local-presentation.d.ts +98 -0
  466. package/dist/electron/wired-display/local-presentation.js +228 -0
  467. package/dist/electron/wired-display/local-presentation.js.map +1 -0
  468. package/dist/electron/wired-display/provider.d.ts +112 -0
  469. package/dist/electron/wired-display/provider.js +197 -0
  470. package/dist/electron/wired-display/provider.js.map +1 -0
  471. package/dist/index.d.ts +1 -0
  472. package/dist/index.js +1 -0
  473. package/dist/index.js.map +1 -1
  474. package/dist/mirroring/backend.d.ts +76 -0
  475. package/dist/mirroring/backend.js +18 -0
  476. package/dist/mirroring/backend.js.map +1 -0
  477. package/dist/mirroring/electron/agent-protocol.d.ts +158 -0
  478. package/dist/mirroring/electron/agent-protocol.js +10 -0
  479. package/dist/mirroring/electron/agent-protocol.js.map +1 -0
  480. package/dist/mirroring/electron/agent-source.d.ts +20 -0
  481. package/dist/mirroring/electron/agent-source.js +618 -0
  482. package/dist/mirroring/electron/agent-source.js.map +1 -0
  483. package/dist/mirroring/electron/engine-process.d.ts +87 -0
  484. package/dist/mirroring/electron/engine-process.js +78 -0
  485. package/dist/mirroring/electron/engine-process.js.map +1 -0
  486. package/dist/mirroring/electron/host.d.ts +89 -0
  487. package/dist/mirroring/electron/host.js +318 -0
  488. package/dist/mirroring/electron/host.js.map +1 -0
  489. package/dist/mirroring/electron/port-backend.d.ts +107 -0
  490. package/dist/mirroring/electron/port-backend.js +441 -0
  491. package/dist/mirroring/electron/port-backend.js.map +1 -0
  492. package/dist/mirroring/encoding-support.d.ts +66 -0
  493. package/dist/mirroring/encoding-support.js +81 -0
  494. package/dist/mirroring/encoding-support.js.map +1 -0
  495. package/dist/mirroring/engine.d.ts +16 -0
  496. package/dist/mirroring/engine.js +75 -0
  497. package/dist/mirroring/engine.js.map +1 -0
  498. package/dist/mirroring/mirror-settings.d.ts +101 -0
  499. package/dist/mirroring/mirror-settings.js +134 -0
  500. package/dist/mirroring/mirror-settings.js.map +1 -0
  501. package/dist/mirroring/mirroring-activity.d.ts +207 -0
  502. package/dist/mirroring/mirroring-activity.js +362 -0
  503. package/dist/mirroring/mirroring-activity.js.map +1 -0
  504. package/dist/mirroring/mirroring-manager.d.ts +134 -0
  505. package/dist/mirroring/mirroring-manager.js +281 -0
  506. package/dist/mirroring/mirroring-manager.js.map +1 -0
  507. package/dist/mirroring/protocol/answer.d.ts +87 -0
  508. package/dist/mirroring/protocol/answer.js +226 -0
  509. package/dist/mirroring/protocol/answer.js.map +1 -0
  510. package/dist/mirroring/protocol/constants.d.ts +64 -0
  511. package/dist/mirroring/protocol/constants.js +66 -0
  512. package/dist/mirroring/protocol/constants.js.map +1 -0
  513. package/dist/mirroring/protocol/errors.d.ts +25 -0
  514. package/dist/mirroring/protocol/errors.js +29 -0
  515. package/dist/mirroring/protocol/errors.js.map +1 -0
  516. package/dist/mirroring/protocol/frame-id.d.ts +9 -0
  517. package/dist/mirroring/protocol/frame-id.js +20 -0
  518. package/dist/mirroring/protocol/frame-id.js.map +1 -0
  519. package/dist/mirroring/protocol/messenger.d.ts +42 -0
  520. package/dist/mirroring/protocol/messenger.js +118 -0
  521. package/dist/mirroring/protocol/messenger.js.map +1 -0
  522. package/dist/mirroring/protocol/offer.d.ts +109 -0
  523. package/dist/mirroring/protocol/offer.js +176 -0
  524. package/dist/mirroring/protocol/offer.js.map +1 -0
  525. package/dist/mirroring/protocol/recommendations.d.ts +28 -0
  526. package/dist/mirroring/protocol/recommendations.js +99 -0
  527. package/dist/mirroring/protocol/recommendations.js.map +1 -0
  528. package/dist/mirroring/routing.d.ts +60 -0
  529. package/dist/mirroring/routing.js +130 -0
  530. package/dist/mirroring/routing.js.map +1 -0
  531. package/dist/mirroring/rtcp/ntp.d.ts +27 -0
  532. package/dist/mirroring/rtcp/ntp.js +57 -0
  533. package/dist/mirroring/rtcp/ntp.js.map +1 -0
  534. package/dist/mirroring/rtcp/parser.d.ts +50 -0
  535. package/dist/mirroring/rtcp/parser.js +265 -0
  536. package/dist/mirroring/rtcp/parser.js.map +1 -0
  537. package/dist/mirroring/rtcp/sender-report.d.ts +32 -0
  538. package/dist/mirroring/rtcp/sender-report.js +51 -0
  539. package/dist/mirroring/rtcp/sender-report.js.map +1 -0
  540. package/dist/mirroring/rtcp/wire.d.ts +54 -0
  541. package/dist/mirroring/rtcp/wire.js +68 -0
  542. package/dist/mirroring/rtcp/wire.js.map +1 -0
  543. package/dist/mirroring/rtp/frame-crypto.d.ts +11 -0
  544. package/dist/mirroring/rtp/frame-crypto.js +35 -0
  545. package/dist/mirroring/rtp/frame-crypto.js.map +1 -0
  546. package/dist/mirroring/rtp/frame.d.ts +23 -0
  547. package/dist/mirroring/rtp/frame.js +2 -0
  548. package/dist/mirroring/rtp/frame.js.map +1 -0
  549. package/dist/mirroring/rtp/packetizer.d.ts +27 -0
  550. package/dist/mirroring/rtp/packetizer.js +88 -0
  551. package/dist/mirroring/rtp/packetizer.js.map +1 -0
  552. package/dist/mirroring/sender/audio-sender.d.ts +23 -0
  553. package/dist/mirroring/sender/audio-sender.js +54 -0
  554. package/dist/mirroring/sender/audio-sender.js.map +1 -0
  555. package/dist/mirroring/sender/bandwidth-estimator.d.ts +26 -0
  556. package/dist/mirroring/sender/bandwidth-estimator.js +103 -0
  557. package/dist/mirroring/sender/bandwidth-estimator.js.map +1 -0
  558. package/dist/mirroring/sender/capture-size-oracle.d.ts +121 -0
  559. package/dist/mirroring/sender/capture-size-oracle.js +433 -0
  560. package/dist/mirroring/sender/capture-size-oracle.js.map +1 -0
  561. package/dist/mirroring/sender/frame-sender.d.ts +103 -0
  562. package/dist/mirroring/sender/frame-sender.js +212 -0
  563. package/dist/mirroring/sender/frame-sender.js.map +1 -0
  564. package/dist/mirroring/sender/packet-router.d.ts +74 -0
  565. package/dist/mirroring/sender/packet-router.js +191 -0
  566. package/dist/mirroring/sender/packet-router.js.map +1 -0
  567. package/dist/mirroring/sender/sender-session.d.ts +85 -0
  568. package/dist/mirroring/sender/sender-session.js +235 -0
  569. package/dist/mirroring/sender/sender-session.js.map +1 -0
  570. package/dist/mirroring/sender/sender.d.ts +120 -0
  571. package/dist/mirroring/sender/sender.js +452 -0
  572. package/dist/mirroring/sender/sender.js.map +1 -0
  573. package/dist/mirroring/sender/video-encoder-metrics.d.ts +29 -0
  574. package/dist/mirroring/sender/video-encoder-metrics.js +58 -0
  575. package/dist/mirroring/sender/video-encoder-metrics.js.map +1 -0
  576. package/dist/mirroring/sender/video-sender.d.ts +95 -0
  577. package/dist/mirroring/sender/video-sender.js +190 -0
  578. package/dist/mirroring/sender/video-sender.js.map +1 -0
  579. package/dist/mirroring/session-host.d.ts +185 -0
  580. package/dist/mirroring/session-host.js +625 -0
  581. package/dist/mirroring/session-host.js.map +1 -0
  582. package/dist/mirroring/stats/clock-offset-estimator.d.ts +11 -0
  583. package/dist/mirroring/stats/clock-offset-estimator.js +120 -0
  584. package/dist/mirroring/stats/clock-offset-estimator.js.map +1 -0
  585. package/dist/mirroring/stats/openscreen-stats-client.d.ts +12 -0
  586. package/dist/mirroring/stats/openscreen-stats-client.js +17 -0
  587. package/dist/mirroring/stats/openscreen-stats-client.js.map +1 -0
  588. package/dist/mirroring/stats/sorted-map.d.ts +23 -0
  589. package/dist/mirroring/stats/sorted-map.js +66 -0
  590. package/dist/mirroring/stats/sorted-map.js.map +1 -0
  591. package/dist/mirroring/stats/statistics-analyzer.d.ts +50 -0
  592. package/dist/mirroring/stats/statistics-analyzer.js +335 -0
  593. package/dist/mirroring/stats/statistics-analyzer.js.map +1 -0
  594. package/dist/mirroring/stats/statistics-collector.d.ts +29 -0
  595. package/dist/mirroring/stats/statistics-collector.js +49 -0
  596. package/dist/mirroring/stats/statistics-collector.js.map +1 -0
  597. package/dist/mirroring/stats/statistics-common.d.ts +61 -0
  598. package/dist/mirroring/stats/statistics-common.js +39 -0
  599. package/dist/mirroring/stats/statistics-common.js.map +1 -0
  600. package/dist/mirroring/stats/statistics-dispatcher.d.ts +28 -0
  601. package/dist/mirroring/stats/statistics-dispatcher.js +100 -0
  602. package/dist/mirroring/stats/statistics-dispatcher.js.map +1 -0
  603. package/dist/mirroring/stats/statistics.d.ts +84 -0
  604. package/dist/mirroring/stats/statistics.js +141 -0
  605. package/dist/mirroring/stats/statistics.js.map +1 -0
  606. package/dist/mirroring/transport/udp-environment.d.ts +40 -0
  607. package/dist/mirroring/transport/udp-environment.js +93 -0
  608. package/dist/mirroring/transport/udp-environment.js.map +1 -0
  609. package/dist/polyfill/bridge-types.d.ts +11 -0
  610. package/dist/polyfill/presentation-receiver.d.ts +12 -0
  611. package/dist/polyfill/presentation-receiver.js +366 -0
  612. package/dist/polyfill/presentation-receiver.js.map +1 -0
  613. package/dist/polyfill/presentation-standin.js +160 -17
  614. package/dist/polyfill/presentation-standin.js.map +1 -1
  615. package/dist/protocol/device-auth.d.ts +61 -0
  616. package/dist/protocol/device-auth.js +106 -0
  617. package/dist/protocol/device-auth.js.map +1 -0
  618. package/dist/protocol/framing.d.ts +1 -1
  619. package/dist/protocol/framing.js +1 -1
  620. package/dist/protocol/framing.js.map +1 -1
  621. package/dist/protocol/index.d.ts +1 -0
  622. package/dist/protocol/index.js +1 -0
  623. package/dist/protocol/index.js.map +1 -1
  624. package/dist/protocol/proto-wire.d.ts +11 -0
  625. package/dist/protocol/proto-wire.js +60 -0
  626. package/dist/protocol/proto-wire.js.map +1 -0
  627. package/docs/API.md +724 -0
  628. package/docs/ARCHITECTURE.md +242 -0
  629. package/docs/CHROMIUM-MAP.md +176 -0
  630. package/docs/DESIGN.md +1189 -0
  631. package/docs/FAQ.md +123 -0
  632. package/docs/GETTING-STARTED.md +248 -0
  633. package/docs/MAIN-API.md +524 -0
  634. package/docs/TROUBLESHOOTING.md +283 -0
  635. package/docs/guides/audio-and-groups.md +75 -0
  636. package/docs/guides/custom-receivers.md +109 -0
  637. package/docs/guides/device-authentication.md +145 -0
  638. package/docs/guides/device-picker.md +176 -0
  639. package/docs/guides/index.md +17 -0
  640. package/docs/guides/low-level-client.md +103 -0
  641. package/docs/guides/restart-rejoin.md +172 -0
  642. package/docs/guides/testing.md +162 -0
  643. package/docs/guides/windows-and-iframes.md +116 -0
  644. package/docs/readme/badges/dependencies.svg +1 -0
  645. package/docs/readme/badges/electron.svg +1 -0
  646. package/docs/readme/badges/license.svg +1 -0
  647. package/docs/readme/badges/node.svg +1 -0
  648. package/docs/readme/board.jpg +0 -0
  649. package/docs/readme/hero-dark.jpg +0 -0
  650. package/docs/readme/hero-light.jpg +0 -0
  651. package/docs/readme/nosignal.jpg +0 -0
  652. package/docs/readme/parity.jpg +0 -0
  653. package/docs/readme/trace.svg +22 -0
  654. package/package.json +67 -16
  655. package/src/cli.ts +57 -0
  656. package/src/client/auth/build-time.ts +7 -0
  657. package/src/client/auth/cast-auth.ts +186 -0
  658. package/src/client/auth/cast-cert-validator.ts +133 -0
  659. package/src/client/auth/crl.ts +228 -0
  660. package/src/client/auth/der.ts +193 -0
  661. package/src/client/auth/names.ts +411 -0
  662. package/src/client/auth/path-validator.ts +449 -0
  663. package/src/client/auth/trust-data.ts +95 -0
  664. package/src/client/auth/x509.ts +488 -0
  665. package/src/client/cast-client.ts +85 -3
  666. package/src/client/errors.ts +29 -0
  667. package/src/client/index.ts +2 -0
  668. package/src/dial/dial-registry.ts +316 -0
  669. package/src/dial/dial-sink-service.ts +343 -0
  670. package/src/dial/http.ts +200 -0
  671. package/src/dial/index.ts +135 -0
  672. package/src/dial/origins.ts +31 -0
  673. package/src/dial/parse.ts +319 -0
  674. package/src/dial/route-provider.ts +429 -0
  675. package/src/dial/ssdp-socket.ts +79 -0
  676. package/src/dial/xml.ts +180 -0
  677. package/src/dial-test/cli-main.ts +146 -0
  678. package/src/dial-test/report.ts +109 -0
  679. package/src/dial-test/runner.ts +615 -0
  680. package/src/dial-test/scrub.ts +74 -0
  681. package/src/discovery/cast-discovery.ts +106 -19
  682. package/src/discovery/device-cache.ts +43 -8
  683. package/src/discovery/index.ts +1 -0
  684. package/src/discovery/ip-policy.ts +97 -0
  685. package/src/discovery/udp-socket.ts +66 -4
  686. package/src/electron/access-code.ts +681 -0
  687. package/src/electron/app-discovery.ts +244 -0
  688. package/src/electron/default-presentation.ts +104 -0
  689. package/src/electron/main.ts +866 -72
  690. package/src/electron/manager-api.ts +664 -0
  691. package/src/electron/media-controller.ts +313 -0
  692. package/src/electron/network-monitor.ts +140 -0
  693. package/src/electron/preload.ts +16 -2
  694. package/src/electron/presentation-envelope.ts +7 -1
  695. package/src/electron/presentation-handler.ts +211 -58
  696. package/src/electron/presentation-source.ts +155 -0
  697. package/src/electron/session-service.ts +808 -149
  698. package/src/electron/session-tracker.ts +248 -0
  699. package/src/electron/sink-service.ts +726 -0
  700. package/src/electron/wired-display/controller.ts +212 -0
  701. package/src/electron/wired-display/displays.ts +107 -0
  702. package/src/electron/wired-display/electron-windows.ts +386 -0
  703. package/src/electron/wired-display/index.ts +181 -0
  704. package/src/electron/wired-display/local-presentation.ts +286 -0
  705. package/src/electron/wired-display/provider.ts +276 -0
  706. package/src/index.ts +1 -0
  707. package/src/mirroring/backend.ts +103 -0
  708. package/src/mirroring/electron/agent-protocol.ts +84 -0
  709. package/src/mirroring/electron/agent-source.ts +619 -0
  710. package/src/mirroring/electron/engine-process.ts +131 -0
  711. package/src/mirroring/electron/host.ts +361 -0
  712. package/src/mirroring/electron/port-backend.ts +466 -0
  713. package/src/mirroring/encoding-support.ts +146 -0
  714. package/src/mirroring/engine.ts +91 -0
  715. package/src/mirroring/mirror-settings.ts +174 -0
  716. package/src/mirroring/mirroring-activity.ts +466 -0
  717. package/src/mirroring/mirroring-manager.ts +342 -0
  718. package/src/mirroring/protocol/answer.ts +285 -0
  719. package/src/mirroring/protocol/constants.ts +75 -0
  720. package/src/mirroring/protocol/errors.ts +36 -0
  721. package/src/mirroring/protocol/frame-id.ts +20 -0
  722. package/src/mirroring/protocol/messenger.ts +128 -0
  723. package/src/mirroring/protocol/offer.ts +275 -0
  724. package/src/mirroring/protocol/recommendations.ts +135 -0
  725. package/src/mirroring/routing.ts +160 -0
  726. package/src/mirroring/rtcp/ntp.ts +61 -0
  727. package/src/mirroring/rtcp/parser.ts +292 -0
  728. package/src/mirroring/rtcp/sender-report.ts +88 -0
  729. package/src/mirroring/rtcp/wire.ts +76 -0
  730. package/src/mirroring/rtp/frame-crypto.ts +38 -0
  731. package/src/mirroring/rtp/frame.ts +25 -0
  732. package/src/mirroring/rtp/packetizer.ts +95 -0
  733. package/src/mirroring/sender/audio-sender.ts +59 -0
  734. package/src/mirroring/sender/bandwidth-estimator.ts +111 -0
  735. package/src/mirroring/sender/capture-size-oracle.ts +476 -0
  736. package/src/mirroring/sender/frame-sender.ts +293 -0
  737. package/src/mirroring/sender/packet-router.ts +228 -0
  738. package/src/mirroring/sender/sender-session.ts +306 -0
  739. package/src/mirroring/sender/sender.ts +497 -0
  740. package/src/mirroring/sender/video-encoder-metrics.ts +64 -0
  741. package/src/mirroring/sender/video-sender.ts +243 -0
  742. package/src/mirroring/session-host.ts +725 -0
  743. package/src/mirroring/stats/clock-offset-estimator.ts +139 -0
  744. package/src/mirroring/stats/openscreen-stats-client.ts +19 -0
  745. package/src/mirroring/stats/sorted-map.ts +68 -0
  746. package/src/mirroring/stats/statistics-analyzer.ts +388 -0
  747. package/src/mirroring/stats/statistics-collector.ts +69 -0
  748. package/src/mirroring/stats/statistics-common.ts +112 -0
  749. package/src/mirroring/stats/statistics-dispatcher.ts +133 -0
  750. package/src/mirroring/stats/statistics.ts +170 -0
  751. package/src/mirroring/transport/udp-environment.ts +100 -0
  752. package/src/polyfill/bridge-types.ts +11 -0
  753. package/src/polyfill/presentation-receiver.ts +350 -0
  754. package/src/polyfill/presentation-standin.ts +150 -17
  755. package/src/protocol/device-auth.ts +126 -0
  756. package/src/protocol/framing.ts +1 -1
  757. package/src/protocol/index.ts +1 -0
  758. package/src/protocol/proto-wire.ts +55 -0
@@ -1,7 +1,19 @@
1
1
  import type { BridgeEvent, BridgeResult, SessionSnapshot, VolumeSnapshot } from "../polyfill/bridge-types.ts";
2
+ import type { WiredDisplaySink } from "./wired-display/displays.ts";
2
3
  import type { CastDevice } from "../discovery/index.ts";
3
4
  import type { ReceiverApplication, ReceiverStatus, ReceiverVolume } from "../client/index.ts";
4
5
  import { CastTimeoutError } from "../client/index.ts";
6
+ import {
7
+ ObservedMap, combineSources, createPickerContext, createSinksObservers, type DevicePickerContext, type MediaRoute,
8
+ } from "./manager-api.ts";
9
+ import { CastSinkService, type ChannelParams, type CastSinkOptions } from "./sink-service.ts";
10
+ import type { NetworkMonitor } from "./network-monitor.ts";
11
+ import { CastSessionTracker, type TrackedSession } from "./session-tracker.ts";
12
+ import { CastAppDiscovery, type CastAppSource } from "./app-discovery.ts";
13
+ import { EventEmitter } from "node:events";
14
+ import {
15
+ AccessCodeSinks, type AccessCodeDevice, type AccessCodeOptions, type AccessCodeRememberedSink, type AddSinkResult,
16
+ } from "./access-code.ts";
5
17
 
6
18
  /** Capability bit names in bit order: 1 video_out, 2 video_in, 4 audio_out, 8 audio_in, 32 multizone_group. */
7
19
  export function capabilityNames(bits: number): string[] {
@@ -14,6 +26,8 @@ export function capabilityNames(bits: number): string[] {
14
26
  return names;
15
27
  }
16
28
 
29
+ export { APP_AVAILABILITY_REFRESH_MS } from "./app-discovery.ts";
30
+
17
31
  /** A source of known Cast devices and their change events. */
18
32
  export interface DeviceSource {
19
33
  devices(): CastDevice[];
@@ -23,7 +37,14 @@ export interface DeviceSource {
23
37
 
24
38
  /** The subset of {@link CastClient} used by the session service. */
25
39
  export interface CastClientLike {
40
+ /**
41
+ * The id this client puts in the source of its messages (`CastClient` has it). Mirroring needs it, to address the
42
+ * receiver's replies; a client without it cannot mirror.
43
+ */
44
+ readonly senderId?: string;
26
45
  readonly connected: boolean;
46
+ /** What device authentication found out, once connected (`CastClient.deviceAuth`); absent when the device was not verified. */
47
+ readonly deviceAuth?: { readonly policy: string } | undefined;
27
48
  connect(): Promise<void>;
28
49
  close(): void;
29
50
  send(namespace: string, destinationId: string, payload: string): void;
@@ -31,6 +52,8 @@ export interface CastClientLike {
31
52
  stop(sessionId: string): Promise<ReceiverStatus>;
32
53
  setVolume(volume: { level: number } | { muted: boolean }): Promise<ReceiverStatus>;
33
54
  getStatus(): Promise<ReceiverStatus>;
55
+ /** `GET_APP_AVAILABILITY`; a client without it is taken to have every app. */
56
+ getAppAvailability?(appIds: string[]): Promise<Record<string, boolean>>;
34
57
  connectTransport(transportId: string): void;
35
58
  closeTransport(transportId: string): void;
36
59
  on(event: "message", listener: (msg: ClientMessage) => void): unknown;
@@ -58,6 +81,17 @@ export interface CastRequestInfo {
58
81
  senderId: number;
59
82
  /** The sender's current session for this app, if it has one; the key is absent when it does not. */
60
83
  session?: { sessionId: string; deviceId: string };
84
+ /**
85
+ * Set when the page's request could also be shown on a display (its URLs mix a Cast app URL and a standard presentation
86
+ * source) and displays exist: Chrome's dialog lists both kinds of sink. Answer `{ display: <sink id> }` to pick one.
87
+ */
88
+ displays?: readonly WiredDisplaySink[];
89
+ }
90
+
91
+ /** A display chosen in a picker that also lists displays (see {@link CastRequestInfo.displays}). */
92
+ export interface DisplayChoice {
93
+ /** The id of one of `request.displays`. */
94
+ display: string;
61
95
  }
62
96
 
63
97
  /** The picker's answer: a device to cast to, `null` (cancel) or `"stop"` (stop the sender's current session). */
@@ -66,14 +100,65 @@ export type DeviceChoice = CastDevice | null | "stop";
66
100
  /** Dependencies and callbacks of {@link CastSessionService}. */
67
101
  export interface CastSessionServiceOptions {
68
102
  devices: DeviceSource;
69
- createClient(device: CastDevice): CastClientLike;
103
+ /**
104
+ * Make a client for a device. `params` carries the connect and liveness timeouts Chrome would use for this attempt
105
+ * (10 s; longer after failures only when `sinks.dynamicTimeoutDeltaMs` is set).
106
+ */
107
+ createClient(device: CastDevice, params: ChannelParams): CastClientLike;
70
108
  /**
71
109
  * Ask the user which device to cast to. Resolves a device, `null` (cancel) or
72
110
  * `"stop"` (stop `request.session`, the sender's current session, if it has one).
73
111
  */
74
- selectDevice(devices: CastDevice[], request: CastRequestInfo): Promise<DeviceChoice>;
112
+ selectDevice(devices: CastDevice[], request: CastRequestInfo, picker: DevicePickerContext): Promise<DeviceChoice>;
75
113
  allowOrigin?(origin: string): boolean;
76
114
  emit(senderId: number, event: BridgeEvent): void;
115
+ /** How channels to the devices are opened and retried; the defaults are Chrome's (see {@link CastSinkOptions}). */
116
+ sinks?: CastSinkOptions;
117
+ /**
118
+ * DIAL sinks for the picker (see `requestSink`) and for a page's availability. `source` announces their changes (as
119
+ * `deviceChanged`); `available` lists the sinks that have the app for a page at `origin`; `monitor` makes the sink
120
+ * service find out, until the returned function is called.
121
+ */
122
+ dial?: {
123
+ source: DeviceSource;
124
+ available(appName: string, origin: string): CastDevice[];
125
+ monitor(appName: string, origin: string): (() => void) | null;
126
+ };
127
+ /** Adding a device by address, as Chrome's access code casting adds one; off unless `enabled` (see {@link AccessCodeOptions}). */
128
+ accessCode?: AccessCodeOptions;
129
+ /** Network changes, which the sinks and the devices added by address follow (Chrome: `DiscoveryNetworkMonitor`). */
130
+ networkMonitor?: NetworkMonitor;
131
+ /**
132
+ * True for a session another local activity of this app holds (a mirroring route, or a device it is launching one on),
133
+ * so it is not reported as a non-local route (Chrome: `CastActivityManager` adds a non-local activity only for a
134
+ * session no local activity has).
135
+ */
136
+ ownsSession?(sessionId: string, sinkId: string): boolean;
137
+ /**
138
+ * A session was launched for `senderId` (`CastActivityManager::DoLaunchSession`, where a route from the frame is
139
+ * recorded in `routes_by_frame_`). Not called for a join, which Chrome does not record there.
140
+ */
141
+ onSessionLaunched?(senderId: number): void;
142
+ }
143
+
144
+ /** How long a request that finds no DIAL sink yet waits for the devices' app info (the dialog of Chrome waits for as long as it is open). */
145
+ const DIAL_PROBE_WAIT_MS = 5000;
146
+
147
+ /** Resolves when `done()` is true after a device change, or after `ms`. */
148
+ function waitForDevices(source: DeviceSource, ms: number, done: () => boolean): Promise<void> {
149
+ return new Promise<void>((resolve) => {
150
+ if (done()) return resolve();
151
+ const finish = (): void => {
152
+ clearTimeout(timer);
153
+ source.off("deviceChanged", onChange);
154
+ resolve();
155
+ };
156
+ const onChange = (): void => {
157
+ if (done()) finish();
158
+ };
159
+ const timer = setTimeout(finish, ms);
160
+ source.on("deviceChanged", onChange);
161
+ });
77
162
  }
78
163
 
79
164
  interface SenderRecord {
@@ -89,6 +174,8 @@ interface SessionRecord {
89
174
  origin: string;
90
175
  appId: string;
91
176
  members: Set<number>;
177
+ /** The sender that launched the session (Chrome's `routes_by_frame_` key); unset for a session found running. */
178
+ launchedBy?: number;
92
179
  /**
93
180
  * Senders whose page navigated away (a reload, say) while a member. Like Chrome's Media Router, the session outlives
94
181
  * the page: it stays open on the device so the sender's next page can rejoin it. Parked senders get no events.
@@ -100,13 +187,11 @@ interface SessionRecord {
100
187
  stoppingBy?: number;
101
188
  }
102
189
 
103
- interface ClientEntry {
104
- client: CastClientLike;
105
- device: CastDevice;
106
- /** Connect promise, created once; every openSession awaits it. */
107
- ready: Promise<void>;
108
- /** Number of in-flight openSession calls; cleanupIdle never closes while > 0. */
109
- opening: number;
190
+ interface PendingRequest {
191
+ discard: { value: boolean };
192
+ cancelled: { value: boolean };
193
+ /** The live picker context while the picker is open. */
194
+ picker?: { close(): void };
110
195
  }
111
196
 
112
197
  const CAPABILITY_BITS: Record<string, number> = {
@@ -118,6 +203,8 @@ const CAPABILITY_BITS: Record<string, number> = {
118
203
  };
119
204
 
120
205
  const DEFAULT_TIMEOUT_MS = 60000;
206
+ /** The caller id of a stop that comes from the main process, not from a page. */
207
+ const MAIN_CALLER = -1;
121
208
 
122
209
  /** A request that must be reported as a specific chrome.cast error code. */
123
210
  class MappedError extends Error {
@@ -158,13 +245,42 @@ function parseInitialize(params: unknown): { appId: string; capabilities: string
158
245
  return { appId, capabilities, autoJoinPolicy };
159
246
  }
160
247
 
161
- function parseRequestSession(params: unknown): { appId: string; capabilities: string[]; timeoutMs: number } | null {
248
+ function parseRequestSession(params: unknown): { appId: string; capabilities: string[]; timeoutMs: number; appAvailability: boolean } | null {
162
249
  if (!isRecord(params)) return null;
163
250
  const { appId, capabilities, timeoutMs } = params;
164
251
  if (typeof appId !== "string" || !isStringArray(capabilities)) return null;
165
252
  const timeout = timeoutMs === undefined ? DEFAULT_TIMEOUT_MS : timeoutMs;
166
253
  if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) return null;
167
- return { appId, capabilities, timeoutMs: timeout };
254
+ return { appId, capabilities, timeoutMs: timeout, appAvailability: params.appAvailability === true };
255
+ }
256
+
257
+ /** `requestSession`'s parameters as a picker request with no DIAL app. */
258
+ function parseRequestSessionParams(params: unknown): RequestParams | null {
259
+ const p = parseRequestSession(params);
260
+ return p === null ? null : { ...p, dialApp: null };
261
+ }
262
+
263
+ /** What a picker request asks for: a Cast app, a DIAL app (`requestSink` only), or both. */
264
+ interface RequestParams {
265
+ appId: string | null;
266
+ dialApp: string | null;
267
+ capabilities: string[];
268
+ timeoutMs: number;
269
+ /** Offer only the sinks that said they have the Cast app (a page's presentation request). */
270
+ appAvailability: boolean;
271
+ }
272
+
273
+ /** `requestSink` parameters: like `requestSession`'s, with the Cast app id optional and a DIAL app name. */
274
+ function parseRequestSink(params: unknown): RequestParams | null {
275
+ if (!isRecord(params)) return null;
276
+ const { appId, dialApp, capabilities, timeoutMs } = params;
277
+ if (appId !== null && typeof appId !== "string") return null;
278
+ if (dialApp !== null && typeof dialApp !== "string") return null;
279
+ if (appId === null && dialApp === null) return null;
280
+ if (!isStringArray(capabilities)) return null;
281
+ const timeout = timeoutMs === undefined ? DEFAULT_TIMEOUT_MS : timeoutMs;
282
+ if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) return null;
283
+ return { appId, dialApp, capabilities, timeoutMs: timeout, appAvailability: params.appAvailability === true };
168
284
  }
169
285
 
170
286
  function parseSessionId(params: unknown): { sessionId: string } | null {
@@ -234,17 +350,124 @@ function sameValue(a: unknown, b: unknown): boolean {
234
350
  export class CastSessionService {
235
351
  private readonly options: CastSessionServiceOptions;
236
352
  private readonly senders = new Map<number, SenderRecord>();
237
- private readonly sessions = new Map<string, SessionRecord>();
238
- private readonly clients = new Map<string, ClientEntry>();
239
- private readonly pendingRequests = new Map<number, { discard: { value: boolean }; cancelled: { value: boolean } }>();
353
+ private readonly sessions = new ObservedMap<string, SessionRecord>((kind, rec) => this.notifySession(kind, rec));
354
+ private readonly sessionListeners = new Set<() => void>();
355
+ private readonly sinks;
356
+ /** The devices with an open channel; the only devices the service offers or casts to. */
357
+ private readonly sinkService: CastSinkService;
358
+ private readonly tracker = new CastSessionTracker();
359
+ private readonly accessCode: AccessCodeSinks;
360
+ private lastRoutesKey = "";
361
+ /** Devices a page's launch is in flight on, with how many: the session it starts is the page's, not a non-local one. */
362
+ private readonly launching = new Map<string, number>();
363
+ private readonly pendingRequests = new Map<number, PendingRequest>();
240
364
  private readonly deviceListener = (): void => this.onDevicesChanged();
365
+ /** Which sinks have which apps, asked over their open channels (Chrome: CastAppDiscoveryServiceImpl). */
366
+ private readonly appDiscovery: CastAppDiscovery;
367
+ /** The availability observations that follow the DIAL sinks (see `watchDialSinks`). */
368
+ private readonly dialWatchers = new Set<() => void>();
369
+ private readonly onDialSinks = (): void => {
370
+ for (const watcher of [...this.dialWatchers]) watcher();
371
+ };
241
372
  private disposed = false;
373
+ private readonly unobserveNetwork: () => void;
242
374
 
243
375
  constructor(options: CastSessionServiceOptions) {
244
376
  this.options = options;
245
- options.devices.on("deviceUp", this.deviceListener);
246
- options.devices.on("deviceDown", this.deviceListener);
247
- options.devices.on("deviceChanged", this.deviceListener);
377
+ this.sinkService = new CastSinkService({
378
+ ...options.sinks,
379
+ source: options.devices,
380
+ ...(options.dial !== undefined ? { dial: options.dial.source } : {}),
381
+ createClient: options.createClient,
382
+ isClientInUse: (client) => [...this.sessions.values()].some((rec) => rec.client === client),
383
+ });
384
+ this.sinks = createSinksObservers(this.sinkService);
385
+ this.appDiscovery = new CastAppDiscovery(this.sinkService);
386
+ this.sinkService.on("deviceUp", this.deviceListener);
387
+ this.sinkService.on("deviceDown", this.deviceListener);
388
+ this.sinkService.on("deviceChanged", this.deviceListener);
389
+ this.sinkService.on("channelOpened", ({ device, client }) => {
390
+ // The tracker goes first so that, when a status ends a session, it no longer lists it by the time this one reports.
391
+ this.tracker.attach(device.id, client);
392
+ this.bindClient(client, device);
393
+ });
394
+ // MediaSinkServiceBase::AddOrUpdateSink tells CastAppDiscoveryServiceImpl at once, not after the 3 s report
395
+ this.sinkService.on("channelOpened", ({ device }) => this.appDiscovery.onSinkAddedOrUpdated(device));
396
+ this.sinkService.on("sinkRemoved", (device) => {
397
+ this.tracker.removeSink(device.id);
398
+ this.appDiscovery.onSinkRemoved(device);
399
+ });
400
+ this.tracker.on("sessionAddedOrUpdated", () => this.onTrackerChanged());
401
+ this.tracker.on("sessionRemoved", () => this.onTrackerChanged());
402
+ this.accessCode = new AccessCodeSinks(
403
+ this.sinkService,
404
+ {
405
+ current: () => this.mediaRoutes().map((r) => ({ id: r.id, sinkId: r.sinkId, isLocal: r.isLocal })),
406
+ observe: (listener) => this.observeSessions(listener),
407
+ },
408
+ options.accessCode,
409
+ );
410
+ // The two observers of Chrome's DiscoveryNetworkMonitor; the sink service asks for the current id first (`Start`).
411
+ const monitor = options.networkMonitor;
412
+ if (monitor !== undefined) this.sinkService.onNetworksChanged(monitor.networkId());
413
+ this.unobserveNetwork = monitor?.observe((networkId) => {
414
+ this.sinkService.onNetworksChanged(networkId);
415
+ this.accessCode.onNetworksChanged(networkId);
416
+ }) ?? (() => {});
417
+ }
418
+
419
+ /** The devices that can be cast to: those with an open, authenticated channel (as Chrome lists sinks). */
420
+ devices(): CastDevice[] {
421
+ return this.sinkService.devices();
422
+ }
423
+
424
+ /** The devices as a {@link DeviceSource}: what `devices()` lists, with the events of that list changing. */
425
+ deviceSource(): DeviceSource {
426
+ return this.sinkService;
427
+ }
428
+
429
+ /**
430
+ * Open channels to the discovered devices that have none now, without Chrome's random delay, and ask the sinks again
431
+ * about the apps being observed, by the refresh rules (`CastMediaRouteProvider::DiscoverSinksNow` ->
432
+ * `CastAppDiscoveryServiceImpl::Refresh`).
433
+ */
434
+ discoverSinksNow(): void {
435
+ this.sinkService.discoverSinksNow();
436
+ this.appDiscovery.refresh();
437
+ }
438
+
439
+ /**
440
+ * The client of the device's open channel, which every activity on the device shares (Chrome: one `CastSocket` per
441
+ * sink, used by `CastMessageHandler` for every session); undefined when the device is not a sink now.
442
+ */
443
+ channelClient(deviceId: string): CastClientLike | undefined {
444
+ return this.sinkService.clientOf(deviceId);
445
+ }
446
+
447
+ /** Resolves when no channel is being opened and none is scheduled to be; for tests. */
448
+ settled(options?: { retries?: boolean }): Promise<void> {
449
+ // no extra wait when nothing is being read: callers of settled() depend on how soon it resolves
450
+ const reading = this.accessCode.ready();
451
+ return reading === undefined ? this.sinkService.settled(options) : reading.then(() => this.sinkService.settled(options));
452
+ }
453
+
454
+ /**
455
+ * Add a device whose address the app knows, as Chrome adds the one an access code resolved to: a channel is opened and
456
+ * the device is listed like any other, remembered for `accessCode.deviceDuration`, then removed. Rejects with
457
+ * `AccessCodeCastDisabledError` unless access code casting is enabled.
458
+ */
459
+ addSinkByAddress(device: AccessCodeDevice): Promise<AddSinkResult> {
460
+ return this.accessCode.addSinkByAddress(device);
461
+ }
462
+
463
+ /** Change `enabled` and `deviceDuration` while running, as Chrome applies its policies. */
464
+ configureAccessCode(settings: { enabled?: boolean; deviceDuration?: number }): Promise<void> {
465
+ return this.accessCode.configure(settings);
466
+ }
467
+
468
+ /** The devices added by address that are remembered now. */
469
+ rememberedSinks(): AccessCodeRememberedSink[] {
470
+ return this.accessCode.remembered();
248
471
  }
249
472
 
250
473
  /** Handle one bridge call; never throws or rejects. */
@@ -259,8 +482,19 @@ export class CastSessionService {
259
482
  return this.handleInitialize(sender, params);
260
483
  case "requestSession":
261
484
  return await this.handleRequestSession(sender, params);
485
+ case "requestSessionOnDevice":
486
+ // main only: the app's own UI chose the device (see the manager's startCasting)
487
+ return await this.handleRequestSession(sender, params, { presetDeviceId: isRecord(params) && typeof params.deviceId === "string" ? params.deviceId : "" });
262
488
  case "joinSession":
263
- return this.handleJoinSession(sender, params);
489
+ return this.handleJoinSession(sender, params, true);
490
+ case "requestSink":
491
+ // main only: the presentation handler's request with a `cast-dial:` source
492
+ return await this.handleRequestSession(sender, params, {
493
+ sink: true,
494
+ ...(isRecord(params) && typeof params.deviceId === "string" ? { presetDeviceId: params.deviceId } : {}),
495
+ });
496
+ case "joinRunningSession":
497
+ return await this.handleJoinRunningSession(sender, params);
264
498
  case "sendMessage":
265
499
  return this.handleSendMessage(sender, params);
266
500
  case "stopSession":
@@ -299,12 +533,16 @@ export class CastSessionService {
299
533
  for (const rec of this.sessions.values()) {
300
534
  if (!rec.members.delete(senderId)) continue;
301
535
  rec.parked.add(senderId);
536
+ this.notifySession("updated", rec);
302
537
  }
303
538
  }
304
539
 
305
540
  private forgetSender(senderId: number): void {
306
541
  const pending = this.pendingRequests.get(senderId);
307
- if (pending !== undefined) pending.cancelled.value = true;
542
+ if (pending !== undefined) {
543
+ pending.cancelled.value = true;
544
+ pending.picker?.close();
545
+ }
308
546
  this.pendingRequests.delete(senderId);
309
547
  this.senders.delete(senderId);
310
548
  }
@@ -313,22 +551,159 @@ export class CastSessionService {
313
551
  async dispose(): Promise<void> {
314
552
  if (this.disposed) return;
315
553
  this.disposed = true;
316
- const d = this.options.devices;
317
- d.off("deviceUp", this.deviceListener);
318
- d.off("deviceDown", this.deviceListener);
319
- d.off("deviceChanged", this.deviceListener);
320
- const entries = [...this.clients.values()];
321
- this.clients.clear();
322
- for (const entry of entries) entry.client.close();
554
+ this.unobserveNetwork();
555
+ this.sinks.dispose();
556
+ this.accessCode.dispose();
557
+ for (const pending of this.pendingRequests.values()) pending.picker?.close();
323
558
  this.sessions.clear();
559
+ this.tracker.dispose();
560
+ this.appDiscovery.dispose();
561
+ this.sinkService.dispose();
324
562
  this.senders.clear();
325
563
  this.pendingRequests.clear();
326
564
  }
327
565
 
566
+ /**
567
+ * What the device said about the app (`GET_APP_AVAILABILITY`), or undefined when it has to be asked: never asked, no
568
+ * answer last time, or "unavailable" longer than `APP_AVAILABILITY_REFRESH_MS` ago. This is
569
+ * CastAppDiscoveryServiceImpl::ShouldRefreshAppAvailability: kAvailable is kept, kUnavailable is asked again after
570
+ * kRefreshThreshold, kUnknown always.
571
+ */
572
+ cachedAppAvailability(device: CastDevice, appId: string): boolean | undefined {
573
+ return this.appDiscovery.cached(device.id, appId);
574
+ }
575
+
576
+ /**
577
+ * Ask the device whether it has the app (`GET_APP_AVAILABILITY`) unless a still-valid answer is cached. An error or
578
+ * no answer is not cached (Chrome's kUnknown): this resolves false, and the next call asks again. Callers that ask on
579
+ * every change (sinks observers) must pace themselves; see createSinksObservers.
580
+ */
581
+ async checkAppAvailability(device: CastDevice, appId: string): Promise<boolean> {
582
+ // Asked over the device's kept-open channel (Chrome: the sink's CastSocket); no channel now is kUnknown. An answer
583
+ // about a channel that was replaced while it was asked is dropped, so it is unknown too.
584
+ await this.appDiscovery.request(device.id, appId);
585
+ return this.appDiscovery.availability(device.id, appId).result === "available";
586
+ }
587
+
588
+ /**
589
+ * A page's `PresentationRequest` availability (`getAvailability()`), as Chrome serves it: a
590
+ * `PresentationMediaSinksObserver` on the source, fed by `CastAppDiscoveryServiceImpl::StartObservingMediaSinks`, so
591
+ * a sink counts once it has said over its open channel that it has the app (`GET_APP_AVAILABILITY`). With `dialApp`
592
+ * (a `cast-dial:` source of the same request) the DIAL sinks that have that app for the page's origin count too, as
593
+ * Blink ORs the sources of a request; the DIAL sink service is asked to find out while the observation lasts.
594
+ * `available` is the first answer: at once from the answers that stand, else once the Cast sinks asked have answered
595
+ * (a sink that says yes answers it at once). `onchange` is called after that only when the answer changes, as
596
+ * `PresentationMediaSinksObserver::OnSinksReceived` skips a repeated one. `close()` stops the observation
597
+ * (`StopObservingMediaSinks`). A disallowed origin is never available.
598
+ */
599
+ observeAppAvailability(
600
+ sender: SenderInfo,
601
+ source: CastAppSource & { dialApp?: string },
602
+ onchange: (available: boolean) => void,
603
+ ): { available: Promise<boolean>; close(): void } {
604
+ const allow = this.options.allowOrigin;
605
+ if (this.disposed || (allow !== undefined && !allow(sender.origin))) return { available: Promise.resolve(false), close: () => {} };
606
+ const dialApp = source.dialApp;
607
+ const dial = dialApp !== undefined ? this.options.dial : undefined;
608
+ const castSource: CastAppSource = { appIds: source.appIds, capabilities: source.capabilities };
609
+ const hasCast = castSource.appIds.length > 0;
610
+ const anyAvailable = (): boolean =>
611
+ (hasCast && this.appDiscovery.availableSinks(castSource).length > 0)
612
+ || (dial !== undefined && dialApp !== undefined && dial.available(dialApp, sender.origin).length > 0);
613
+ let last: boolean | undefined;
614
+ let closed = false;
615
+ let first!: (value: boolean) => void;
616
+ const available = new Promise<boolean>((resolve) => (first = resolve));
617
+ const report = (value: boolean): void => {
618
+ if (closed || value === last) return;
619
+ const answered = last !== undefined;
620
+ last = value;
621
+ if (answered) onchange(value);
622
+ else first(value);
623
+ };
624
+ const stopCast = hasCast ? this.appDiscovery.startObserving(castSource, () => report(anyAvailable())) : () => {};
625
+ let stopDial = (): void => {};
626
+ if (dial !== undefined && dialApp !== undefined) {
627
+ const release = dial.monitor(dialApp, sender.origin) ?? (() => undefined);
628
+ // before the first answer only a yes is final; after it every change counts
629
+ const onDial = (): void => {
630
+ const value = anyAvailable();
631
+ if (last !== undefined || value) report(value);
632
+ };
633
+ const off = this.watchDialSinks(dial.source, onDial);
634
+ stopDial = () => {
635
+ off();
636
+ release();
637
+ };
638
+ if (anyAvailable()) report(true);
639
+ }
640
+ if (last === undefined) {
641
+ const ready = hasCast ? this.appDiscovery.settled(castSource) : Promise.resolve();
642
+ void ready.then(() => {
643
+ if (last === undefined) report(anyAvailable());
644
+ });
645
+ }
646
+ return {
647
+ available,
648
+ close: () => {
649
+ if (closed) return;
650
+ closed = true;
651
+ stopCast();
652
+ stopDial();
653
+ if (last === undefined) first(false);
654
+ },
655
+ };
656
+ }
657
+
658
+ /**
659
+ * One listener on the DIAL sinks for every availability observation: a page may hold many, and a listener each would
660
+ * trip EventEmitter's limit of ten.
661
+ */
662
+ private watchDialSinks(source: DeviceSource, callback: () => void): () => void {
663
+ if (this.dialWatchers.size === 0) source.on("deviceChanged", this.onDialSinks);
664
+ this.dialWatchers.add(callback);
665
+ return () => {
666
+ if (!this.dialWatchers.delete(callback)) return;
667
+ if (this.dialWatchers.size === 0) source.off("deviceChanged", this.onDialSinks);
668
+ };
669
+ }
670
+
671
+ /**
672
+ * The sinks a request for an app is offered, as Chrome's dialog and `StartPresentation` offer the sinks of the
673
+ * source: those that have the app. Observes the source until `close()`, so the picker sees the list change; the
674
+ * first list is waited for when no answer stands yet.
675
+ */
676
+ private async appSinks(source: CastAppSource): Promise<{
677
+ devices(): CastDevice[];
678
+ source: DeviceSource;
679
+ observers: ReturnType<typeof createSinksObservers>;
680
+ close(): void;
681
+ }> {
682
+ const events = new EventEmitter();
683
+ const devices = (): CastDevice[] => this.appDiscovery.availableSinks(source);
684
+ const stop = this.appDiscovery.startObserving(source, () => events.emit("deviceChanged"));
685
+ const sinks: DeviceSource = {
686
+ devices,
687
+ on: (e, l) => events.on(e, l),
688
+ off: (e, l) => events.off(e, l),
689
+ };
690
+ const observers = createSinksObservers(sinks);
691
+ if (devices().length === 0) await this.appDiscovery.settled(source);
692
+ return {
693
+ devices,
694
+ source: sinks,
695
+ observers,
696
+ close: () => {
697
+ observers.dispose();
698
+ stop();
699
+ },
700
+ };
701
+ }
702
+
328
703
  private handleInitialize(sender: SenderInfo, params: unknown): BridgeResult {
329
704
  const p = parseInitialize(params);
330
705
  if (p === null) return bad("invalid_parameter");
331
- const available = this.options.devices.devices().some((d) => deviceMatches(d, p.capabilities));
706
+ const available = this.sinkService.devices().some((d) => deviceMatches(d, p.capabilities));
332
707
  this.senders.set(sender.id, { appId: p.appId, capabilities: p.capabilities, lastAvailable: available });
333
708
  const joined: SessionSnapshot[] = [];
334
709
  for (const rec of this.sessions.values()) {
@@ -338,39 +713,93 @@ export class CastSessionService {
338
713
  if (p.autoJoinPolicy === "origin_scoped" || (p.autoJoinPolicy === "tab_and_origin_scoped" && sameTab)) {
339
714
  rec.parked.delete(sender.id);
340
715
  rec.members.add(sender.id);
716
+ this.notifySession("updated", rec);
341
717
  joined.push(this.snapshotOf(rec));
342
718
  }
343
719
  }
344
720
  return good({ available, sessions: joined });
345
721
  }
346
722
 
347
- private async handleRequestSession(sender: SenderInfo, params: unknown): Promise<BridgeResult> {
348
- const p = parseRequestSession(params);
723
+ /**
724
+ * `requestSession`; with `presetDeviceId` (`requestSessionOnDevice`) the picker is skipped: that device is the choice
725
+ * (and must still be eligible). With `sink` (`requestSink`) the request may name a DIAL app as well as, or instead
726
+ * of, a Cast app, and the picker is offered the DIAL sinks that have it too; a DIAL choice is returned as
727
+ * `{ dialSink }` and nothing is launched here (the presentation handler creates the DIAL route).
728
+ */
729
+ private async handleRequestSession(sender: SenderInfo, params: unknown, opts: { presetDeviceId?: string; sink?: boolean } = {}): Promise<BridgeResult> {
730
+ const presetDeviceId = opts.presetDeviceId;
731
+ const p: RequestParams | null = opts.sink === true ? parseRequestSink(params) : parseRequestSessionParams(params);
349
732
  if (p === null) return bad("invalid_parameter");
733
+ const appId = p.appId;
734
+ const dialApp = p.dialApp;
735
+ const capabilities = p.capabilities;
736
+ const dial = dialApp !== null ? this.options.dial : undefined;
737
+ const dialAvailable = (): CastDevice[] => (dial !== undefined && dialApp !== null ? dial.available(dialApp, sender.origin) : []);
350
738
  if (this.pendingRequests.has(sender.id)) return bad("invalid_parameter");
351
739
  const discard = { value: false };
352
740
  const cancelled = { value: false };
353
- this.pendingRequests.set(sender.id, { discard, cancelled });
741
+ const pending: PendingRequest = { discard, cancelled };
742
+ this.pendingRequests.set(sender.id, pending);
743
+ // Chrome's dialog starts observing the sinks of the source when it opens (StartObservingMediaSinks), and the
744
+ // observer then waits for the answers; so the DIAL monitor starts before the first look, and a request with no sink
745
+ // yet waits a while for the devices' app info instead of failing on a page that never watched availability.
746
+ const stopMonitoring = dial !== undefined && dialApp !== null ? dial.monitor(dialApp, sender.origin) ?? undefined : undefined;
747
+ let appSinks: Awaited<ReturnType<CastSessionService["appSinks"]>> | undefined;
748
+ let combined: ReturnType<typeof createSinksObservers> | undefined;
354
749
  try {
355
- const eligible = this.options.devices.devices().filter((d) => deviceMatches(d, p.capabilities));
356
- if (eligible.length === 0) return bad("receiver_unavailable");
750
+ // A page's presentation request is offered the sinks that have the app; chrome.cast's requestSession is unchanged
751
+ if (p.appAvailability && appId !== null) appSinks = await this.appSinks({ appIds: [appId], capabilities });
752
+ if (cancelled.value || this.disposed) return bad("cancel");
753
+ const castEligible = (): CastDevice[] =>
754
+ appId === null ? [] : (appSinks?.devices() ?? this.sinkService.devices()).filter((d) => deviceMatches(d, capabilities));
755
+ let eligible = [...castEligible(), ...dialAvailable()];
756
+ if (eligible.length === 0 && dial !== undefined && stopMonitoring !== undefined) {
757
+ await waitForDevices(dial.source, Math.min(p.timeoutMs, DIAL_PROBE_WAIT_MS), () => cancelled.value || discard.value || dialAvailable().length > 0);
758
+ if (cancelled.value || this.disposed) return bad("cancel");
759
+ eligible = [...castEligible(), ...dialAvailable()];
760
+ }
761
+ if (eligible.length === 0) {
762
+ // Say why the devices found are not offered, when they failed to open (Chrome shows no sink and logs the error)
763
+ const why = this.sinkService.openErrors();
764
+ return bad("receiver_unavailable", why.length > 0 ? `no device could be reached: ${why.join("; ")}` : undefined);
765
+ }
357
766
  const request: CastRequestInfo = {
358
- appId: p.appId,
359
- capabilities: p.capabilities,
767
+ appId: appId ?? "",
768
+ capabilities,
360
769
  origin: sender.origin,
361
770
  senderId: sender.id,
362
771
  };
363
- const current = this.currentSessionFor(sender.id, p.appId);
772
+ const current = appId === null ? undefined : this.currentSessionFor(sender.id, appId);
364
773
  if (current !== undefined) {
365
774
  request.session = { sessionId: current.app.sessionId, deviceId: current.device.id };
366
775
  }
367
776
  let choice: DeviceChoice;
368
- try {
369
- choice = await this.options.selectDevice(eligible, request);
370
- } catch (err) {
371
- return bad("session_error", describe(err));
777
+ if (presetDeviceId !== undefined) {
778
+ choice = eligible.find((d) => d.id === presetDeviceId) ?? null;
779
+ if (choice === null) return bad("receiver_unavailable");
780
+ } else {
781
+ // The picker gets live device updates until it answers; closing aborts its signal. With a DIAL app it watches
782
+ // the DIAL sinks too.
783
+ const castObservers = appSinks?.observers ?? this.sinks;
784
+ if (dial !== undefined) combined = createSinksObservers(combineSources(appSinks?.source ?? this.sinkService, dial.source));
785
+ const pick = createPickerContext(combined ?? castObservers, (d) =>
786
+ d.dial !== undefined ? dialAvailable().some((x) => x.id === d.id) : appId !== null && deviceMatches(d, capabilities));
787
+ pending.picker = pick;
788
+ try {
789
+ choice = await this.options.selectDevice(eligible, request, pick.context);
790
+ } catch (err) {
791
+ return bad("session_error", describe(err));
792
+ } finally {
793
+ pick.close();
794
+ }
372
795
  }
373
796
  if (choice === null) return bad("cancel");
797
+ if (choice !== "stop" && choice.dial !== undefined) {
798
+ // Only a device offered for this request can be chosen.
799
+ if (!eligible.some((d) => d.id === choice.id)) return bad("receiver_unavailable");
800
+ return good({ dialSink: choice });
801
+ }
802
+ if (appId === null) return bad("invalid_parameter");
374
803
  if (choice === "stop") {
375
804
  const rec = request.session !== undefined ? this.sessions.get(request.session.sessionId) : undefined;
376
805
  if (rec === undefined) return bad("cancel");
@@ -379,7 +808,9 @@ export class CastSessionService {
379
808
  const stopped = await this.stopSessionRecord(rec, sender.id, true);
380
809
  return stopped.ok ? bad("cancel") : stopped;
381
810
  }
382
- if (request.session !== undefined && choice.id === request.session.deviceId) {
811
+ // Chrome's CastActivityManager::LaunchSession launches anew even on a sink that has a route (the receiver's status
812
+ // update then removes the old one), so only the page's own picker reuses the sender's session
813
+ if (presetDeviceId === undefined && request.session !== undefined && choice.id === request.session.deviceId) {
383
814
  const rec = this.sessions.get(request.session.sessionId);
384
815
  if (rec !== undefined) return good(this.snapshotOf(rec));
385
816
  }
@@ -392,7 +823,7 @@ export class CastSessionService {
392
823
  }, p.timeoutMs);
393
824
  });
394
825
  try {
395
- const snapshot = await Promise.race([this.openSession(choice, p.appId, sender, discard, cancelled), bound]);
826
+ const snapshot = await Promise.race([this.openSession(choice, appId, sender, discard, cancelled), bound]);
396
827
  return good(snapshot);
397
828
  } catch (err) {
398
829
  if (err instanceof Discarded) return bad("timeout", "timed out");
@@ -402,17 +833,26 @@ export class CastSessionService {
402
833
  if (timer !== undefined) clearTimeout(timer);
403
834
  }
404
835
  } finally {
836
+ combined?.dispose();
837
+ appSinks?.close();
838
+ stopMonitoring?.();
405
839
  this.pendingRequests.delete(sender.id);
406
840
  }
407
841
  }
408
842
 
409
- private handleJoinSession(sender: SenderInfo, params: unknown): BridgeResult {
843
+ /**
844
+ * `checkOrigin` is joinSession's own rule (mode "chromecast", which is frozen and has no
845
+ * Chromium counterpart). Joining by id, as Chrome's Presentation API path does, has none: AppActivity::CanJoinSession checks only
846
+ * the app and that the client is not connected yet.
847
+ */
848
+ private handleJoinSession(sender: SenderInfo, params: unknown, checkOrigin: boolean): BridgeResult {
410
849
  const p = parseSessionId(params);
411
850
  if (p === null) return bad("invalid_parameter");
412
851
  const rec = this.sessions.get(p.sessionId);
413
- if (rec === undefined || rec.origin !== sender.origin) return bad("session_error");
852
+ if (rec === undefined || (checkOrigin && rec.origin !== sender.origin)) return bad("session_error");
414
853
  rec.parked.delete(sender.id);
415
854
  rec.members.add(sender.id);
855
+ this.notifySession("updated", rec);
416
856
  return good(this.snapshotOf(rec));
417
857
  }
418
858
 
@@ -446,7 +886,7 @@ export class CastSessionService {
446
886
  private async stopSessionRecord(rec: SessionRecord, caller: number, tellCaller: boolean): Promise<BridgeResult> {
447
887
  rec.stoppingBy = caller;
448
888
  try {
449
- await rec.client.stop(rec.app.sessionId);
889
+ this.tracker.applyStatus(rec.device.id, rec.client, await rec.client.stop(rec.app.sessionId));
450
890
  } catch (err) {
451
891
  rec.stoppingBy = undefined;
452
892
  return bad("session_error", describe(err));
@@ -462,7 +902,6 @@ export class CastSessionService {
462
902
  } else if (tellCaller) {
463
903
  this.emitTo(caller, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
464
904
  }
465
- this.cleanupIdle(rec.device.id, rec.client);
466
905
  return good(undefined);
467
906
  }
468
907
 
@@ -489,7 +928,7 @@ export class CastSessionService {
489
928
  const rec = this.sessions.get(p.sessionId);
490
929
  if (rec === undefined || !rec.members.has(sender.id)) return bad("session_error");
491
930
  try {
492
- await rec.client.setVolume({ level: p.level });
931
+ this.tracker.applyStatus(rec.device.id, rec.client, await rec.client.setVolume({ level: p.level }));
493
932
  } catch (err) {
494
933
  return bad("session_error", describe(err));
495
934
  }
@@ -502,14 +941,53 @@ export class CastSessionService {
502
941
  const rec = this.sessions.get(p.sessionId);
503
942
  if (rec === undefined || !rec.members.has(sender.id)) return bad("session_error");
504
943
  try {
505
- await rec.client.setVolume({ muted: p.muted });
944
+ this.tracker.applyStatus(rec.device.id, rec.client, await rec.client.setVolume({ muted: p.muted }));
506
945
  } catch (err) {
507
946
  return bad("session_error", describe(err));
508
947
  }
509
948
  return good(undefined);
510
949
  }
511
950
 
512
- /** Connect (or reuse) the device's client, launch the app and register the session. */
951
+ /**
952
+ * Join a session by its id, as Chrome's Media Router does for `reconnect("cast-session_<id>")`. A session this
953
+ * service already holds is joined without an origin check, like one found on a device. Otherwise the id is looked up
954
+ * in the session tracker, which knows what every device with an open channel runs: Chrome's CastActivityManager adds
955
+ * a non-local activity for each session its CastSessionTracker learns (AddNonLocalActivity) and
956
+ * FindActivityForSessionJoin finds it by id alone; auto-join skips it and no origin is checked. `appId`, when given,
957
+ * must be the session's app (AppActivity::CanJoinSession). Joining makes the route local, as in Chrome.
958
+ */
959
+ private async handleJoinRunningSession(sender: SenderInfo, params: unknown): Promise<BridgeResult> {
960
+ const p = parseSessionId(params);
961
+ if (p === null) return bad("invalid_parameter");
962
+ const appId = isRecord(params) && typeof params.appId === "string" ? params.appId : undefined;
963
+ const known = this.sessions.get(p.sessionId);
964
+ if (known !== undefined) {
965
+ if (appId !== undefined && known.appId !== appId) return bad("session_error");
966
+ return this.handleJoinSession(sender, params, false);
967
+ }
968
+ const tracked = this.tracker.sessionById(p.sessionId);
969
+ if (tracked === undefined || this.disposed || (appId !== undefined && tracked.appId !== appId)) return bad("session_error");
970
+ const client = this.sinkService.clientOf(tracked.sinkId);
971
+ const device = this.sinkService.sinkOf(tracked.sinkId);
972
+ if (client === undefined || device === undefined) return bad("session_error");
973
+ client.connectTransport(tracked.app.transportId);
974
+ const rec: SessionRecord = {
975
+ device,
976
+ client,
977
+ app: tracked.app,
978
+ origin: sender.origin,
979
+ appId: tracked.appId,
980
+ members: new Set([sender.id]),
981
+ parked: new Set(),
982
+ lastVolume: tracked.volume,
983
+ lastSnapshot: null,
984
+ };
985
+ rec.lastSnapshot = this.snapshotOf(rec);
986
+ this.sessions.set(tracked.sessionId, rec);
987
+ return good(rec.lastSnapshot);
988
+ }
989
+
990
+ /** Launch the app on the device's open channel and register the session. */
513
991
  private async openSession(
514
992
  device: CastDevice,
515
993
  appId: string,
@@ -517,88 +995,88 @@ export class CastSessionService {
517
995
  discard: { value: boolean },
518
996
  cancelled: { value: boolean },
519
997
  ): Promise<SessionSnapshot> {
520
- const existing = this.clients.get(device.id);
521
- let entry = existing;
522
- if (entry === undefined) {
523
- const client = this.options.createClient(device);
524
- this.bindClient(client, device);
525
- entry = {
526
- client,
527
- device,
528
- ready: client.connect().catch((err: unknown) => {
529
- // Remove the entry and close the client once so the next request starts fresh.
530
- this.dropClient(device.id, client);
531
- throw err;
532
- }),
533
- opening: 0,
534
- };
535
- this.clients.set(device.id, entry);
536
- }
537
- const client = entry.client;
538
- entry.opening += 1;
539
- let registered = false;
998
+ // A DIAL sink has no Cast port: it is launched by the presentation handler's DIAL route, never as a Cast session.
999
+ if (device.dial !== undefined) throw new MappedError("session_error", "A DIAL device cannot host a Cast session");
1000
+ // Casting needs a channel that is open now; a device that is not a sink (or whose channel dropped) is unavailable.
1001
+ const client = this.sinkService.clientOf(device.id);
1002
+ if (client === undefined) throw new MappedError("receiver_unavailable", "no open channel to the device");
1003
+ if (discard.value) throw new Discarded();
1004
+ if (cancelled.value) throw new MappedError("cancel", "cancelled");
1005
+ this.launching.set(device.id, (this.launching.get(device.id) ?? 0) + 1);
540
1006
  try {
541
- try {
542
- await entry.ready;
543
- } catch (err) {
544
- if (err instanceof CastTimeoutError) throw new MappedError("timeout", err.message);
545
- throw new MappedError("receiver_unavailable", describe(err));
546
- }
547
- if (discard.value) throw new Discarded();
548
- if (cancelled.value) throw new MappedError("cancel", "cancelled");
549
- let app: ReceiverApplication;
550
- try {
551
- app = await client.launch(appId);
552
- } catch (err) {
553
- if (err instanceof CastTimeoutError) throw new MappedError("timeout", err.message);
554
- throw new MappedError("session_error", describe(err));
555
- }
556
- if (discard.value) {
557
- client.closeTransport(app.transportId);
558
- throw new Discarded();
559
- }
560
- if (cancelled.value) {
561
- client.closeTransport(app.transportId);
562
- throw new MappedError("cancel", "cancelled");
563
- }
564
- client.connectTransport(app.transportId);
565
- let status: ReceiverStatus;
566
- try {
567
- status = await client.getStatus();
568
- } catch (err) {
569
- client.closeTransport(app.transportId);
570
- throw new MappedError("session_error", describe(err));
571
- }
572
- if (discard.value) {
573
- client.closeTransport(app.transportId);
574
- throw new Discarded();
575
- }
576
- if (cancelled.value) {
577
- client.closeTransport(app.transportId);
578
- throw new MappedError("cancel", "cancelled");
579
- }
580
- const rec: SessionRecord = {
581
- device,
582
- client,
583
- app,
584
- origin: sender.origin,
585
- appId,
586
- members: new Set([sender.id]),
587
- parked: new Set(),
588
- lastVolume: status.volume,
589
- lastSnapshot: null,
590
- };
591
- const snapshot = this.snapshotOf(rec);
592
- rec.lastSnapshot = snapshot;
593
- this.sessions.set(app.sessionId, rec);
594
- registered = true;
595
- return snapshot;
1007
+ return await this.launchOn(device, client, appId, sender, discard, cancelled);
596
1008
  } finally {
597
- entry.opening -= 1;
598
- if (!registered) this.cleanupIdle(device.id, client);
1009
+ const left = (this.launching.get(device.id) ?? 1) - 1;
1010
+ if (left > 0) this.launching.set(device.id, left);
1011
+ else this.launching.delete(device.id);
1012
+ // A launch that ended without a session leaves the device's own session listed again
1013
+ if (left === 0) this.onTrackerChanged();
599
1014
  }
600
1015
  }
601
1016
 
1017
+ private async launchOn(
1018
+ device: CastDevice,
1019
+ client: CastClientLike,
1020
+ appId: string,
1021
+ sender: SenderInfo,
1022
+ discard: { value: boolean },
1023
+ cancelled: { value: boolean },
1024
+ ): Promise<SessionSnapshot> {
1025
+ let app: ReceiverApplication;
1026
+ try {
1027
+ app = await client.launch(appId);
1028
+ } catch (err) {
1029
+ if (err instanceof CastTimeoutError) throw new MappedError("timeout", err.message);
1030
+ throw new MappedError("session_error", describe(err));
1031
+ }
1032
+ if (discard.value) {
1033
+ client.closeTransport(app.transportId);
1034
+ throw new Discarded();
1035
+ }
1036
+ if (cancelled.value) {
1037
+ client.closeTransport(app.transportId);
1038
+ throw new MappedError("cancel", "cancelled");
1039
+ }
1040
+ client.connectTransport(app.transportId);
1041
+ let status: ReceiverStatus;
1042
+ try {
1043
+ status = await client.getStatus();
1044
+ this.tracker.applyStatus(device.id, client, status);
1045
+ } catch (err) {
1046
+ client.closeTransport(app.transportId);
1047
+ throw new MappedError("session_error", describe(err));
1048
+ }
1049
+ if (discard.value) {
1050
+ client.closeTransport(app.transportId);
1051
+ throw new Discarded();
1052
+ }
1053
+ if (cancelled.value) {
1054
+ client.closeTransport(app.transportId);
1055
+ throw new MappedError("cancel", "cancelled");
1056
+ }
1057
+ const rec: SessionRecord = {
1058
+ device,
1059
+ client,
1060
+ app,
1061
+ origin: sender.origin,
1062
+ appId,
1063
+ launchedBy: sender.id,
1064
+ members: new Set([sender.id]),
1065
+ parked: new Set(),
1066
+ lastVolume: status.volume,
1067
+ lastSnapshot: null,
1068
+ };
1069
+ const snapshot = this.snapshotOf(rec);
1070
+ rec.lastSnapshot = snapshot;
1071
+ this.sessions.set(app.sessionId, rec);
1072
+ try {
1073
+ this.options.onSessionLaunched?.(sender.id);
1074
+ } catch (err) {
1075
+ console.error("electron-cast: onSessionLaunched failed", err);
1076
+ }
1077
+ return snapshot;
1078
+ }
1079
+
602
1080
  /** Remove a sender from a session; when it was the last one, tear the session down. */
603
1081
  private leave(rec: SessionRecord, senderId: number): void {
604
1082
  rec.members.delete(senderId);
@@ -606,16 +1084,17 @@ export class CastSessionService {
606
1084
  if (rec.members.size === 0 && rec.parked.size === 0) {
607
1085
  rec.client.closeTransport(rec.app.transportId);
608
1086
  this.sessions.delete(rec.app.sessionId);
609
- this.cleanupIdle(rec.device.id, rec.client);
1087
+ } else {
1088
+ this.notifySession("updated", rec);
610
1089
  }
611
1090
  }
612
1091
 
613
- /** Subscribe to a freshly created client's events. */
614
- private bindClient(client: CastClientLike, device: CastDevice): void {
1092
+ /** Subscribe to a freshly opened channel's events. */
1093
+ private bindClient(client: CastClientLike, _device: CastDevice): void {
615
1094
  client.on("message", (msg) => this.onClientMessage(client, msg));
616
- client.on("receiverStatus", (status) => this.onClientStatus(client, device, status));
1095
+ client.on("receiverStatus", (status) => this.onClientStatus(client, status));
617
1096
  client.on("transportClosed", (transportId) => this.onTransportClosed(client, transportId));
618
- client.on("close", () => this.onClientClose(client, device.id));
1097
+ client.on("close", () => this.onClientClose(client));
619
1098
  }
620
1099
 
621
1100
  private onClientMessage(client: CastClientLike, msg: ClientMessage): void {
@@ -633,7 +1112,7 @@ export class CastSessionService {
633
1112
  }
634
1113
  }
635
1114
 
636
- private onClientStatus(client: CastClientLike, device: CastDevice, status: ReceiverStatus): void {
1115
+ private onClientStatus(client: CastClientLike, status: ReceiverStatus): void {
637
1116
  for (const rec of [...this.sessions.values()]) {
638
1117
  if (rec.client !== client) continue;
639
1118
  const app = status.applications.find((a) => a.sessionId === rec.app.sessionId);
@@ -650,12 +1129,12 @@ export class CastSessionService {
650
1129
  const snapshot = this.snapshotOf(rec);
651
1130
  if (!sameValue(snapshot, rec.lastSnapshot)) {
652
1131
  rec.lastSnapshot = snapshot;
1132
+ this.notifySession("updated", rec);
653
1133
  for (const member of rec.members) {
654
1134
  this.emitTo(member, { type: "sessionUpdated", session: snapshot });
655
1135
  }
656
1136
  }
657
1137
  }
658
- this.cleanupIdle(device.id, client);
659
1138
  }
660
1139
 
661
1140
  private onTransportClosed(client: CastClientLike, transportId: string): void {
@@ -668,9 +1147,7 @@ export class CastSessionService {
668
1147
  }
669
1148
  }
670
1149
 
671
- private onClientClose(client: CastClientLike, deviceId: string): void {
672
- const entry = this.clients.get(deviceId);
673
- if (entry !== undefined && entry.client === client) this.clients.delete(deviceId);
1150
+ private onClientClose(client: CastClientLike): void {
674
1151
  for (const rec of [...this.sessions.values()]) {
675
1152
  if (rec.client !== client) continue;
676
1153
  this.sessions.delete(rec.app.sessionId);
@@ -683,7 +1160,7 @@ export class CastSessionService {
683
1160
  /** Re-check availability for every initialized sender; emit only on change. */
684
1161
  private onDevicesChanged(): void {
685
1162
  if (this.disposed) return;
686
- const devices = this.options.devices.devices();
1163
+ const devices = this.sinkService.devices();
687
1164
  for (const [id, rec] of this.senders) {
688
1165
  const available = devices.some((d) => deviceMatches(d, rec.capabilities));
689
1166
  if (available !== rec.lastAvailable) {
@@ -693,22 +1170,204 @@ export class CastSessionService {
693
1170
  }
694
1171
  }
695
1172
 
696
- /** Close the client when no session uses it any more and no openSession is in flight. */
697
- private cleanupIdle(deviceId: string, client: CastClientLike): void {
698
- const entry = this.clients.get(deviceId);
699
- if (entry !== undefined && entry.client === client && entry.opening > 0) return;
700
- for (const rec of this.sessions.values()) {
701
- if (rec.client === client) return;
1173
+ // Main-process API (see manager-api.ts). Control goes through the paths the pages use, so pages see the same events.
1174
+
1175
+ /** The sessions `senderId` launched and that still run (`routes_by_frame_` lookup by frame). */
1176
+ sessionsLaunchedBy(senderId: number): string[] {
1177
+ return [...this.sessions.values()].filter((rec) => rec.launchedBy === senderId).map((rec) => rec.app.sessionId);
1178
+ }
1179
+
1180
+ /**
1181
+ * Every live session, parked ones included, then the sessions the devices run that this service does not hold
1182
+ * (started elsewhere, or let go of here), as Chrome's non-local routes.
1183
+ */
1184
+ mediaRoutes(): MediaRoute[] {
1185
+ const local = [...this.sessions.values()].map((rec) => this.routeOf(rec));
1186
+ return [...local, ...this.nonLocalSessions().map((s) => this.nonLocalRouteOf(s))];
1187
+ }
1188
+
1189
+ /** The tracked sessions no page of this service holds (Chrome: activities with `is_local` false). */
1190
+ private nonLocalSessions(): TrackedSession[] {
1191
+ return this.tracker.sessions().filter((s) => !this.sessions.has(s.sessionId) && !this.launching.has(s.sinkId)
1192
+ && this.options.ownsSession?.(s.sessionId, s.sinkId) !== true);
1193
+ }
1194
+
1195
+ /** Be told when a session starts, changes (status, volume, pages) or ends; returns the unsubscribe function. */
1196
+ observeSessions(listener: () => void): () => void {
1197
+ this.sessionListeners.add(listener);
1198
+ return () => void this.sessionListeners.delete(listener);
1199
+ }
1200
+
1201
+ /**
1202
+ * Stop a session on the device for every page, as if a member had called stopSession. A session this service does not
1203
+ * hold is stopped on its device as it is (Chrome's TerminateSession works on non-local activities too).
1204
+ */
1205
+ async terminateSession(sessionId: string): Promise<BridgeResult> {
1206
+ const rec = this.sessions.get(sessionId);
1207
+ // No page is the caller, so every member is told sessionEnded.
1208
+ if (rec !== undefined) return this.stopSessionRecord(rec, MAIN_CALLER, false);
1209
+ const tracked = this.nonLocalSessions().find((s) => s.sessionId === sessionId);
1210
+ if (tracked === undefined) return bad("session_error", "unknown session");
1211
+ // With no open channel the stop cannot be sent: Chrome's `CastMessageHandler::StopSession` finds no socket and
1212
+ // reports a failure, which `HandleStopSessionResponse` turns into this message while the route stays.
1213
+ const client = this.sinkService.clientOf(tracked.sinkId);
1214
+ if (client === undefined) return bad("session_error", "StopSession response is not OK. Failed to terminate route.");
1215
+ try {
1216
+ this.tracker.applyStatus(tracked.sinkId, client, await client.stop(sessionId));
1217
+ } catch (err) {
1218
+ return bad("session_error", describe(err));
1219
+ }
1220
+ return good(undefined);
1221
+ }
1222
+
1223
+ async setSessionVolume(sessionId: string, volume: { level: number } | { muted: boolean }): Promise<BridgeResult> {
1224
+ const rec = this.sessions.get(sessionId);
1225
+ const tracked = rec === undefined ? this.nonLocalSessions().find((s) => s.sessionId === sessionId) : undefined;
1226
+ const client = rec?.client ?? (tracked === undefined ? undefined : this.sinkService.clientOf(tracked.sinkId));
1227
+ const sinkId = rec?.device.id ?? tracked?.sinkId;
1228
+ if (client === undefined || sinkId === undefined) return bad("session_error", "unknown session");
1229
+ try {
1230
+ this.tracker.applyStatus(sinkId, client, await client.setVolume(volume));
1231
+ } catch (err) {
1232
+ return bad("session_error", describe(err));
1233
+ }
1234
+ return good(undefined);
1235
+ }
1236
+
1237
+ /**
1238
+ * Every receiver status that describes a session, changed or not, with its volume (the tracker's
1239
+ * `OnSessionAddedOrUpdated`); for the media controller. Returns the unsubscribe function.
1240
+ */
1241
+ observeSessionStatus(listener: (sessionId: string, volume: ReceiverVolume) => void): () => void {
1242
+ const on = (s: TrackedSession): void => listener(s.sessionId, s.volume);
1243
+ this.tracker.on("sessionStatus", on);
1244
+ return () => void this.tracker.off("sessionStatus", on);
1245
+ }
1246
+
1247
+ /**
1248
+ * Every MEDIA_STATUS on a device's channel, credited to the session the device runs and merged with the media saved
1249
+ * before (`CastSessionTracker::HandleMediaStatusMessage`); for the media controller. Returns the unsubscribe function.
1250
+ */
1251
+ observeMediaStatus(listener: (sessionId: string, message: Record<string, unknown>) => void): () => void {
1252
+ const on = (s: TrackedSession, message: Record<string, unknown>): void => listener(s.sessionId, message);
1253
+ this.tracker.on("mediaStatus", on);
1254
+ return () => void this.tracker.off("mediaStatus", on);
1255
+ }
1256
+
1257
+ /**
1258
+ * Send a message to a session's receiver app from main, with no page involved, over the device's channel. A session
1259
+ * this service does not hold is reached too, as Chrome's `CastMessageHandler::SendMediaRequest` reaches a non-local
1260
+ * activity's session: a virtual connection to its transport is opened first (`SendCastMessageToSocket` -> `DoEnsureConnection`).
1261
+ */
1262
+ sendSessionMessage(sessionId: string, namespace: string, message: string): BridgeResult {
1263
+ const rec = this.sessions.get(sessionId);
1264
+ let client = rec?.client;
1265
+ let transportId = rec?.app.transportId;
1266
+ if (rec === undefined) {
1267
+ const tracked = this.nonLocalSessions().find((s) => s.sessionId === sessionId);
1268
+ if (tracked === undefined) return bad("session_error", "unknown session");
1269
+ client = this.sinkService.clientOf(tracked.sinkId);
1270
+ transportId = tracked.transportId;
1271
+ if (client === undefined) return bad("channel_error", "no open channel to the device");
702
1272
  }
703
- this.dropClient(deviceId, client);
1273
+ if (client === undefined || transportId === undefined) return bad("session_error", "unknown session");
1274
+ try {
1275
+ if (rec === undefined) client.connectTransport(transportId);
1276
+ client.send(namespace, transportId, message);
1277
+ } catch (err) {
1278
+ return bad("channel_error", describe(err));
1279
+ }
1280
+ return good(undefined);
704
1281
  }
705
1282
 
706
- /** Forget a client and close it; a no-op unless it is still the registered entry for the device. */
707
- private dropClient(deviceId: string, client: CastClientLike): void {
708
- const entry = this.clients.get(deviceId);
709
- if (entry === undefined || entry.client !== client) return;
710
- this.clients.delete(deviceId);
711
- client.close();
1283
+ /** The volume the receiver last reported for a session, local or not (Chrome: `receiver.volume` of the CastSession). */
1284
+ sessionVolume(sessionId: string): ReceiverVolume | undefined {
1285
+ return this.sessions.get(sessionId)?.lastVolume ?? this.tracker.sessionById(sessionId)?.volume;
1286
+ }
1287
+
1288
+ /**
1289
+ * Detach every page from the session and close its transport; the receiver app keeps running (and is then listed as a
1290
+ * non-local route). A route no page holds has nothing to detach.
1291
+ */
1292
+ detachSession(sessionId: string): BridgeResult {
1293
+ const rec = this.sessions.get(sessionId);
1294
+ if (rec === undefined) {
1295
+ return this.nonLocalSessions().some((s) => s.sessionId === sessionId) ? good(undefined) : bad("session_error", "unknown session");
1296
+ }
1297
+ for (const member of rec.members) {
1298
+ this.emitTo(member, { type: "sessionEnded", sessionId, status: "disconnected" });
1299
+ }
1300
+ rec.members.clear();
1301
+ rec.parked.clear();
1302
+ rec.client.closeTransport(rec.app.transportId);
1303
+ this.sessions.delete(sessionId);
1304
+ return good(undefined);
1305
+ }
1306
+
1307
+ /** Tell the route observers something changed (the arguments only fit the ObservedMap callback). */
1308
+ private notifySession(_kind: "started" | "updated" | "ended", _rec: SessionRecord): void {
1309
+ if (this.disposed) return;
1310
+ if (_kind === "ended") this.sinkService.releaseUnused();
1311
+ this.lastRoutesKey = JSON.stringify(this.mediaRoutes());
1312
+ this.fireSessionListeners();
1313
+ }
1314
+
1315
+ /** The tracker learned something; route observers hear of it only if the routes they see differ now. */
1316
+ private onTrackerChanged(): void {
1317
+ if (this.disposed) return;
1318
+ const key = JSON.stringify(this.mediaRoutes());
1319
+ if (key === this.lastRoutesKey) return;
1320
+ this.lastRoutesKey = key;
1321
+ this.fireSessionListeners();
1322
+ }
1323
+
1324
+ private fireSessionListeners(): void {
1325
+ for (const listener of [...this.sessionListeners]) {
1326
+ try {
1327
+ listener();
1328
+ } catch (err) {
1329
+ console.error("electron-cast: a session listener failed", err);
1330
+ }
1331
+ }
1332
+ }
1333
+
1334
+ private routeOf(rec: SessionRecord): MediaRoute {
1335
+ return {
1336
+ id: rec.app.sessionId,
1337
+ providerId: "cast",
1338
+ mediaSource: `cast:${rec.appId}`,
1339
+ sinkId: rec.device.id,
1340
+ sinkName: rec.device.name,
1341
+ description: rec.app.statusText || rec.app.displayName,
1342
+ isLocal: true,
1343
+ appId: rec.appId,
1344
+ displayName: rec.app.displayName,
1345
+ origin: rec.origin,
1346
+ volume: { level: rec.lastVolume.level ?? null, muted: rec.lastVolume.muted ?? null },
1347
+ members: [...rec.members],
1348
+ parkedSenders: [...rec.parked],
1349
+ parked: rec.members.size === 0,
1350
+ };
1351
+ }
1352
+
1353
+ /** Chrome's `AddNonLocalActivity`: the route has no page, and its description is the session's (`GetRouteDescription`). */
1354
+ private nonLocalRouteOf(session: TrackedSession): MediaRoute {
1355
+ return {
1356
+ id: session.sessionId,
1357
+ providerId: "cast",
1358
+ mediaSource: `cast:${session.appId}`,
1359
+ sinkId: session.sinkId,
1360
+ sinkName: this.sinkService.sinkOf(session.sinkId)?.name ?? "",
1361
+ description: session.statusText || session.displayName,
1362
+ isLocal: false,
1363
+ appId: session.appId,
1364
+ displayName: session.displayName,
1365
+ origin: "",
1366
+ volume: { level: session.volume.level ?? null, muted: session.volume.muted ?? null },
1367
+ members: [],
1368
+ parkedSenders: [],
1369
+ parked: false,
1370
+ };
712
1371
  }
713
1372
 
714
1373
  private snapshotOf(rec: SessionRecord): SessionSnapshot {