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,26 +1,90 @@
1
- import { CastSessionService, type CastClientLike, type DeviceSource, type CastRequestInfo, type DeviceChoice } from "./session-service.ts";
1
+ import { CastSessionService, type CastClientLike, type DeviceSource, type CastRequestInfo, type DeviceChoice, type DisplayChoice } from "./session-service.ts";
2
2
  import type { CastDevice } from "../discovery/index.ts";
3
3
  import { CastDiscovery } from "../discovery/index.ts";
4
4
  import { CastClient } from "../client/index.ts";
5
- import { CastPresentationHandler, type PresentationConnectionHandle } from "./presentation-handler.ts";
5
+ import { CastPresentationHandler, PresentationError, type PresentationConnectionHandle } from "./presentation-handler.ts";
6
+ import { randomBytes, randomUUID } from "node:crypto";
7
+ import {
8
+ MediaRouteError, combineSources, createManagerApi, createSinksObservers, presentationUrlForDevice,
9
+ type CastManagerApi, type DevicePickerContext, type MediaRoute,
10
+ } from "./manager-api.ts";
11
+ import { parsePresentationUrl } from "./presentation-envelope.ts";
12
+ import { DefaultPresentationRequests, samePresentationRequest } from "./default-presentation.ts";
13
+ import type { CastSinkOptions, ChannelParams } from "./sink-service.ts";
14
+ import { MAX_MESSAGE_SIZE, asBytes, messageFields } from "./wired-display/local-presentation.ts";
15
+ import type { WiredDisplaySink } from "./wired-display/displays.ts";
16
+ import { createWiredDisplay, isWiredDisplaySource, type LocalConnectionHandle, type WiredDisplayApi, type WiredDisplayOptions } from "./wired-display/index.ts";
17
+ import type { AccessCodeDevice, AccessCodeOptions, AccessCodeRememberedSink, AddSinkResult } from "./access-code.ts";
18
+ import { DiscoveryNetworkMonitor, NETWORK_ID_DISCONNECTED, type NetworkMonitor } from "./network-monitor.ts";
19
+ import { MirroringManager, MirroringRouteError, type MirroringRoute } from "../mirroring/mirroring-manager.ts";
20
+ import { createElectronMirroringServiceFactory, type ElectronMirroringOptions } from "../mirroring/electron/host.ts";
21
+ import type { MirroringClient, MirroringServiceFactory } from "../mirroring/mirroring-activity.ts";
22
+ import type { BridgeMethod } from "../polyfill/bridge-types.ts";
23
+ import { DialService, type DialHttp, type DialHttpOptions, type DialRegistryOptions } from "../dial/index.ts";
24
+ import { appNameFromDialSource, isDialSource, processDeviceUuid } from "../dial/parse.ts";
6
25
 
7
- export type { CastRequestInfo, DeviceChoice } from "./session-service.ts";
26
+ /**
27
+ * The methods a page may call over IPC: the bridge's own. The service also handles methods that only main calls
28
+ * (joinRunningSession, from the presentation handler), which a page must not reach directly.
29
+ */
30
+ const PAGE_METHODS: ReadonlySet<string> = new Set<BridgeMethod>([
31
+ "initialize", "requestSession", "joinSession", "sendMessage", "stopSession",
32
+ "leaveSession", "setReceiverVolume", "setReceiverMuted",
33
+ ]);
34
+
35
+ export type { CastRequestInfo, DeviceChoice, DisplayChoice } from "./session-service.ts";
8
36
  export type { CastDevice } from "../discovery/index.ts";
37
+ export { MediaRouteError } from "./manager-api.ts";
38
+ export type { CastSinkOptions, ChannelParams, SinkRetryOptions } from "./sink-service.ts";
39
+ export type { DialHttp, DialHttpOptions, DialRegistryOptions } from "../dial/index.ts";
40
+ export {
41
+ createReceiverWindows, isWiredDisplaySource, availableDisplays, wiredDisplaySinks,
42
+ type WiredDisplayApi, type WiredDisplayOptions, type WiredDisplayRequest, type WiredDisplaySink, type WiredDisplayRoute,
43
+ type ReceiverWindows, type ReceiverWindowsOptions, type ScreenLike, type DisplayLike, type WindowLike, type PresentationMessage,
44
+ } from "./wired-display/index.ts";
45
+ export { AccessCodeCastDisabledError, createAccessCodeSink } from "./access-code.ts";
46
+ export type { NetworkMonitor } from "./network-monitor.ts";
47
+ export type { MirroringCastMode } from "../mirroring/mirroring-manager.ts";
48
+ export type { ElectronMirroringApi, ElectronMirroringOptions } from "../mirroring/electron/host.ts";
49
+ export type {
50
+ AccessCodeCapabilities, AccessCodeDevice, AccessCodeOptions, AccessCodeRememberedSink, AccessCodeStore, AddSinkResult,
51
+ SinkCreationFailure,
52
+ } from "./access-code.ts";
53
+ export type {
54
+ MediaController, MediaImage, MediaPlayState, MediaStatus, MediaStatusObserver,
55
+ } from "./media-controller.ts";
56
+ export type {
57
+ CastManagerApi, DevicePickerContext, MediaCastMode, StartCastingOptions, MediaRoute, MediaRouteController, MediaRoutesObserver, MediaSink,
58
+ MediaSinksObserver, MediaSinksQuery, PresentationObserver, PresentationRequest, SinkIconType,
59
+ } from "./manager-api.ts";
9
60
 
10
61
  /** The subset of an Electron `WebFrameMain` the manager needs. */
11
- export interface FrameLike { url: string; origin?: string }
62
+ export interface FrameLike {
63
+ url: string;
64
+ origin?: string;
65
+ /** Browser-global and constant for the frame's lifetime, across navigations; identifies a subframe. */
66
+ frameTreeNodeId?: number;
67
+ /** Null or absent on a main frame. */
68
+ parent?: unknown;
69
+ processId?: number;
70
+ routingId?: number;
71
+ /** Reaches only this frame (and needs the preload to run there: `nodeIntegrationInSubFrames`). */
72
+ send?(channel: string, ...args: any[]): void;
73
+ }
12
74
  /** The subset of an Electron `WebContents` the manager needs. */
13
75
  export interface WebContentsLike {
14
76
  readonly id: number;
15
77
  send(channel: string, ...args: any[]): void;
16
78
  isDestroyed(): boolean;
79
+ /** The frame tree; consulted to find a subframe's current frame and to notice frames that were removed. */
80
+ readonly mainFrame?: { readonly framesInSubtree: readonly FrameLike[] };
17
81
  once(event: "destroyed", listener: () => void): unknown;
18
- on(event: "did-navigate", listener: (...args: any[]) => void): unknown;
82
+ on(event: "did-navigate" | "did-frame-navigate", listener: (...args: any[]) => void): unknown;
19
83
  }
20
84
  /** The subset of an Electron `IpcMainInvokeEvent` the manager needs. */
21
85
  export interface IpcMainInvokeEventLike { sender: WebContentsLike; senderFrame: FrameLike | null }
22
86
  /** The subset of an Electron `IpcMainEvent` the manager needs. */
23
- export interface IpcMainEventLike { sender: WebContentsLike }
87
+ export interface IpcMainEventLike { sender: WebContentsLike; senderFrame?: FrameLike | null }
24
88
  /** The subset of an Electron `IpcMain` the manager needs (`on`/`removeListener` unless `presentation: false`). */
25
89
  export interface IpcMainLike {
26
90
  handle(channel: string, listener: (event: IpcMainInvokeEventLike, ...args: any[]) => unknown): void;
@@ -31,10 +95,11 @@ export interface IpcMainLike {
31
95
  /** Dependencies of {@link createCastManager}; the Electron objects are passed in, never imported. */
32
96
  export interface CastManagerOptions {
33
97
  ipcMain: IpcMainLike;
34
- selectDevice(devices: CastDevice[], request: CastRequestInfo): Promise<DeviceChoice>;
98
+ /** `picker` (optional to declare) streams device changes while the picker is open; see {@link DevicePickerContext}. */
99
+ selectDevice(devices: CastDevice[], request: CastRequestInfo, picker: DevicePickerContext): Promise<DeviceChoice | DisplayChoice>;
35
100
  allowOrigin?(origin: string): boolean;
36
101
  discovery?: DeviceSource & { start(): Promise<void>; stop(): void }; // default new CastDiscovery()
37
- createClient?(device: CastDevice): CastClientLike; // default new CastClient({ host, port })
102
+ createClient?(device: CastDevice, params: ChannelParams): CastClientLike; // default new CastClient({ host, port, timeouts })
38
103
  channel?: string; // default "electron-cast"
39
104
  /**
40
105
  * Answer the preload's presentation mode (the page's own Google Cast SDK running on a stand-in Presentation API),
@@ -42,16 +107,89 @@ export interface CastManagerOptions {
42
107
  * the preload's `mode: "chromecast"`.
43
108
  */
44
109
  presentation?: boolean;
110
+ /**
111
+ * Verify each device is a genuine Google-certified Cast device when connecting, as Chrome does (CastClient's
112
+ * `verifyDevice`). Default true; false allows uncertified receivers. Ignored when `createClient` is given.
113
+ */
114
+ verifyDevices?: boolean;
115
+ /**
116
+ * How the manager opens and keeps a channel to each discovered device, whose open channels are what it lists as
117
+ * devices. The defaults are Chrome's, and there is no way to turn the tracking off: Chrome has none.
118
+ */
119
+ sinks?: CastSinkOptions;
120
+ /**
121
+ * DIAL (SSDP discovery, `cast-dial:` sources), which Chrome runs next to Cast discovery. Default true, except that a
122
+ * custom `discovery` turns it off unless this is given. `false` disables it; an object replaces its network layers
123
+ * (tests, mostly). `log` receives discovery problems (a socket that failed, ...), which are never thrown.
124
+ *
125
+ * DIAL sinks are listed next to the Cast sinks, except a device that is a Cast sink too (its UDN is the Cast id), which
126
+ * is listed as Cast only, as in Chrome. In `mode: "chromecast"` the sinks observers of the manager API still list
127
+ * DIAL sinks (they have port 0 and no Cast capabilities); only the Presentation API path can start a DIAL route.
128
+ */
129
+ dial?: boolean | { registry?: DialRegistryOptions; http?: DialHttp; httpOptions?: DialHttpOptions; settleMs?: number; log?: (message: string) => void };
130
+ /**
131
+ * Show a page's presentation on another screen, as Chrome's wired display provider does (presentation mode only).
132
+ * Chrome has it always on; here the app passes Electron's `screen` and a window factory, because this package never
133
+ * imports Electron. See {@link WiredDisplayOptions}.
134
+ */
135
+ wiredDisplay?: WiredDisplayOptions;
136
+ /**
137
+ * Adding a device by its address, as Chrome's access code casting adds the device a code resolved to (see
138
+ * {@link CastManager.addSinkByAddress}). Off unless `enabled`, as Chrome's `AccessCodeCastEnabled` policy is.
139
+ */
140
+ accessCode?: AccessCodeOptions;
141
+ /**
142
+ * Enables tab and desktop mirroring (`startCasting` with `castMode: "tab_mirror"` or `"desktop_mirror"`). Pass the
143
+ * Electron classes it needs (`BrowserWindow`, `utilityProcess`, `MessageChannelMain`, `webContents`,
144
+ * and optionally `app`); off by default. `createService` replaces the Electron engine (tests, or a different one).
145
+ */
146
+ mirroring?: ElectronMirroringOptions | { createService: MirroringServiceFactory };
147
+ /**
148
+ * Network changes, as Chrome's `DiscoveryNetworkMonitor` tells its media router: the DIAL devices given up on are tried
149
+ * again, the sinks of a network the machine comes back to are reopened, and devices added by address are removed and
150
+ * re-added. DIAL discovery forgets its devices and searches again at once, and stops while there is no network. By default the manager reads `os.networkInterfaces()` every 5 s while started (docs/DESIGN.md, "Network
151
+ * changes"); a custom `discovery` turns that off unless this is given. `false` turns it off; your own
152
+ * {@link NetworkMonitor} drives it in tests.
153
+ */
154
+ networkMonitor?: NetworkMonitor | false;
45
155
  }
46
- /** The main-process cast manager. */
47
- export interface CastManager {
156
+ /** The main-process cast manager; the observers, routes and route control come from {@link CastManagerApi}. */
157
+ export interface CastManager extends CastManagerApi {
48
158
  start(): Promise<void>;
159
+ /** The devices that have an open, authenticated channel (what Chrome lists as sinks), not everything mDNS has heard. */
49
160
  devices(): CastDevice[];
161
+ /** Try again now to open a channel to each discovered device that has none (Chrome: `CastMediaSinkService::DiscoverSinksNow`). */
162
+ discoverSinksNow(): void;
163
+ /**
164
+ * Resolves when no channel is being opened and none is waiting to be (a retry waiting out its backoff does not count),
165
+ * so the devices that can be reached are listed. Discovery and Chrome's random delay (up to 5 s) come first, so call it
166
+ * some time after `start()`.
167
+ */
168
+ settled(options?: { retries?: boolean }): Promise<void>;
169
+ /**
170
+ * Add a device whose address the app knows, as Chrome adds the device an access code resolved to. electron-cast does not
171
+ * resolve codes: that is a private Google service (docs/DESIGN.md, "Access code casting"), so the app supplies the
172
+ * address. A channel is opened to the device and it is listed like a discovered one, then removed 20 s after
173
+ * `accessCode.deviceDuration` seconds, or once a route to it ends after that. Rejects with `AccessCodeCastDisabledError`
174
+ * unless `accessCode.enabled`. The address rules of discovered devices apply: a public or loopback address is refused
175
+ * (`channel_open_error`) unless `sinks.allowAllIps` is set.
176
+ */
177
+ addSinkByAddress(device: AccessCodeDevice): Promise<AddSinkResult>;
178
+ /** Change `accessCode.enabled` and `accessCode.deviceDuration` while running, as Chrome applies its policies. */
179
+ configureAccessCode(settings: { enabled?: boolean; deviceDuration?: number }): Promise<void>;
180
+ /** The devices added by address that are remembered now. */
181
+ rememberedSinks(): AccessCodeRememberedSink[];
50
182
  dispose(): Promise<void>;
183
+ /** The wired display sinks and routes; present when the `wiredDisplay` option was given. */
184
+ readonly wiredDisplay?: WiredDisplayApi;
51
185
  }
52
186
 
53
- /** Availability watches kept per webContents; the page never releases one, so the oldest are dropped. */
187
+ /** Availability watches kept per frame; the page never releases one, so the oldest are dropped. */
54
188
  const MAX_WATCHES_PER_PAGE = 32;
189
+ /** Sender ids of subframes start here, far above any webContents id (a main frame's sender id is its webContents id). */
190
+ const SUBFRAME_ID_BASE = 2 ** 30;
191
+ /** How often removed iframes (which Electron does not announce) are noticed and their sessions released. */
192
+ const FRAME_SWEEP_MS = 5000;
55
193
  /** Largest page-to-receiver message accepted (Cast messages are capped at 64 KiB on the wire). */
56
194
  const MAX_MESSAGE_CHARS = 1 << 20;
57
195
 
@@ -72,88 +210,339 @@ function originOf(frame: FrameLike | null): string | null {
72
210
  * a sender that leaves its sessions when it is destroyed or navigates. Unless
73
211
  * `presentation: false` it also serves `<channel>:presentation` (see {@link CastManagerOptions.presentation}).
74
212
  */
213
+ /**
214
+ * A mirroring activity's view of the device's shared channel. Chrome runs every activity on a sink over its one
215
+ * `CastSocket`; here the activity's `connect()` only checks that channel is open and `close()` detaches the activity
216
+ * (its listeners go quiet) without closing the channel the other sessions and the session tracker use.
217
+ */
218
+ function sharedChannelClient(deviceId: string, channel: CastClientLike | undefined): MirroringClient {
219
+ let attached = true;
220
+ const need = (): CastClientLike => {
221
+ if (channel === undefined || !channel.connected) throw new Error(`no open channel to ${deviceId}`);
222
+ if (channel.senderId === undefined) throw new Error("the Cast client has no senderId, which mirroring needs");
223
+ return channel;
224
+ };
225
+ return {
226
+ get senderId() {
227
+ return channel?.senderId ?? "";
228
+ },
229
+ async connect() {
230
+ need();
231
+ },
232
+ close() {
233
+ attached = false;
234
+ },
235
+ send: (namespace, destinationId, payload) => {
236
+ if (attached) need().send(namespace, destinationId, payload);
237
+ },
238
+ launch: (appId) => need().launch(appId),
239
+ stop: (sessionId) => need().stop(sessionId),
240
+ connectTransport: (transportId) => need().connectTransport(transportId),
241
+ closeTransport: (transportId) => need().closeTransport(transportId),
242
+ on(event: string, listener: (...args: any[]) => void) {
243
+ (channel as { on(event: string, l: (...args: any[]) => void): unknown } | undefined)?.on(event, (...args: any[]) => {
244
+ if (attached) listener(...args);
245
+ });
246
+ return this;
247
+ },
248
+ } as MirroringClient;
249
+ }
250
+
251
+ /** How long starting a default request on a DIAL sink waits for its app info, as a page's request does. */
252
+ const DIAL_APP_WAIT_MS = 5000;
253
+
254
+ /**
255
+ * The first `cast-dial:` URL, in order, whose app the DIAL sink has for a page at `origin`. Apps not known yet are
256
+ * asked about and the answers waited for (up to 5 s), as the Cast path asks the device (`checkAppAvailability`).
257
+ */
258
+ async function dialUrlFor(dial: DialService, device: CastDevice, urls: readonly string[], origin: string): Promise<string | null> {
259
+ const candidates = urls.map((url) => ({ url, app: isDialSource(url) ? appNameFromDialSource(url) : "" })).filter((c) => c.app !== "");
260
+ const first = (): string | null =>
261
+ candidates.find((c) => dial.availableFor(c.app, origin).some((d) => d.id === device.id))?.url ?? null;
262
+ if (candidates.length === 0 || first() !== null) return first();
263
+ const releases = candidates.map((c) => dial.monitorApp(c.app, origin)).filter((r): r is () => void => r !== null);
264
+ if (releases.length === 0) return null;
265
+ try {
266
+ await new Promise<void>((resolve) => {
267
+ const done = (): void => {
268
+ clearTimeout(timer);
269
+ dial.off("change", check);
270
+ resolve();
271
+ };
272
+ const check = (): void => {
273
+ if (first() !== null) done();
274
+ };
275
+ const timer = setTimeout(done, DIAL_APP_WAIT_MS);
276
+ dial.on("change", check);
277
+ });
278
+ } finally {
279
+ for (const release of releases) release();
280
+ }
281
+ return first();
282
+ }
283
+
75
284
  export function createCastManager(options: CastManagerOptions): CastManager {
76
285
  const channel = options.channel ?? "electron-cast";
77
286
  if (options.presentation !== false && (typeof options.ipcMain.on !== "function" || typeof options.ipcMain.removeListener !== "function")) {
78
287
  throw new TypeError("presentation mode needs ipcMain.on and ipcMain.removeListener");
79
288
  }
80
289
  const discovery = options.discovery ?? new CastDiscovery();
81
- const createClient = options.createClient ?? ((d: CastDevice) => new CastClient({ host: d.host, port: d.port }));
82
- const contents = new Map<number, WebContentsLike>();
290
+ const networkMonitor = options.networkMonitor === false ? undefined
291
+ : options.networkMonitor ?? (options.discovery === undefined ? new DiscoveryNetworkMonitor() : undefined);
292
+ const createClient = options.createClient ?? ((d: CastDevice, params: ChannelParams) =>
293
+ new CastClient({
294
+ host: d.host,
295
+ port: d.port,
296
+ verifyDevice: options.verifyDevices !== false,
297
+ connectTimeoutMs: params.connectTimeoutMs,
298
+ heartbeatTimeoutMs: params.livenessTimeoutMs,
299
+ heartbeatIntervalMs: params.pingIntervalMs,
300
+ }));
301
+ // The receiver labels of Cast and DIAL routes are hashed with one per-process token, as in Chrome.
302
+ const hashToken = randomBytes(16).toString("hex");
303
+ const dialOption = options.dial ?? (options.discovery === undefined);
304
+ // A DIAL sink whose processed UDN is a Cast sink's id is removed: CastMediaSinkServiceImpl removes it from the DIAL
305
+ // sink service when the Cast channel opens (OnChannelOpenSucceeded) and when the DIAL sink comes after the Cast one
306
+ // (TryConnectDialDiscoveredSink), and only DIAL discovery adds it back. A Cast sink is a device with an open, authenticated channel, not one mDNS merely
307
+ // heard, so this asks the session service; `service` exists before DIAL is started.
308
+ let castSinks: () => CastDevice[] = () => [];
309
+ const dial = dialOption === false ? undefined : new DialService({
310
+ hashToken,
311
+ isCastDevice: (uuid) => castSinks().some((d) => processDeviceUuid(d.id) === uuid),
312
+ ...(typeof dialOption === "object" ? dialOption : {}),
313
+ });
314
+ const routeListeners = new Set<() => void>();
315
+ const notifyRoutes = (): void => {
316
+ for (const l of [...routeListeners]) l();
317
+ };
318
+ dial?.provider.on("routes", notifyRoutes);
319
+ // DIAL discovery follows the connection itself, as Chrome's DialRegistry observes the NetworkConnectionTracker.
320
+ const unobserveDialNetwork = dial === undefined || networkMonitor === undefined ? () => {}
321
+ : networkMonitor.observe((networkId) => dial.networksChanged(networkId !== NETWORK_ID_DISCONNECTED));
322
+ /** The DIAL sinks as a device source: their one change event stands for a changed device. */
323
+ const dialSource: DeviceSource | undefined = dial === undefined ? undefined : {
324
+ devices: () => dial.devices(),
325
+ on: (event, listener) => (event === "deviceChanged" ? dial.on("change", listener as () => void) : undefined),
326
+ off: (event, listener) => (event === "deviceChanged" ? dial.off("change", listener as () => void) : undefined),
327
+ };
328
+ /**
329
+ * A sender is one frame. A main frame's sender id is its webContents id; a subframe gets an id of its own, kept for
330
+ * the frame's lifetime (across its navigations, so a reloaded iframe can rejoin its session). The frame is looked up
331
+ * in the frame tree on every use, never held, because navigating can swap the frame object.
332
+ */
333
+ interface SenderEntry { wc: WebContentsLike; frameTreeNodeId: number | null }
334
+ const senders = new Map<number, SenderEntry>();
335
+ const subframeIds = new Map<number, number>(); // frameTreeNodeId -> sender id
336
+ const watched = new Set<number>(); // webContents ids
337
+ let nextSubframeId = SUBFRAME_ID_BASE;
338
+ let sweepTimer: ReturnType<typeof setInterval> | undefined;
83
339
  let handler: CastPresentationHandler | undefined;
340
+ /** Each window's default presentation request (Chrome keeps it per WebContents, from its outermost frame only). */
341
+ const defaults = new DefaultPresentationRequests();
342
+ /** The route each window's main frame launched last (CastActivityManager's routes_by_frame_). */
343
+ const launchedFrom = new Map<number, string>();
344
+
345
+ const findFrame = (wc: WebContentsLike, frameTreeNodeId: number): FrameLike | undefined => {
346
+ try {
347
+ return wc.mainFrame?.framesInSubtree.find((f) => f.frameTreeNodeId === frameTreeNodeId);
348
+ } catch {
349
+ return undefined; // the webContents is gone
350
+ }
351
+ };
352
+ const isMainFrame = (frame: FrameLike): boolean => frame.parent === undefined || frame.parent === null;
353
+ /**
354
+ * The sender id of an IPC event's frame, without registering it; undefined when it is not a known sender. An event
355
+ * with no `senderFrame` property at all is the main frame's; a null one is a frame that is already gone.
356
+ */
357
+ const senderIdOf = (wc: WebContentsLike, frame: FrameLike | null | undefined): number | undefined => {
358
+ if (frame === undefined) return wc.id;
359
+ if (frame === null) return undefined;
360
+ if (isMainFrame(frame)) return wc.id;
361
+ return typeof frame.frameTreeNodeId === "number" ? subframeIds.get(frame.frameTreeNodeId) : undefined;
362
+ };
363
+ /** The sender id of an IPC event's frame, registering a subframe (and watching its webContents) on first use. */
364
+ const registerSender = (wc: WebContentsLike, frame: FrameLike): number | null => {
365
+ if (isMainFrame(frame)) {
366
+ watch(wc);
367
+ if (!senders.has(wc.id)) senders.set(wc.id, { wc, frameTreeNodeId: null });
368
+ return wc.id;
369
+ }
370
+ const node = frame.frameTreeNodeId;
371
+ if (typeof node !== "number") return null;
372
+ watch(wc);
373
+ let id = subframeIds.get(node);
374
+ if (id === undefined) {
375
+ id = nextSubframeId++;
376
+ subframeIds.set(node, id);
377
+ senders.set(id, { wc, frameTreeNodeId: node });
378
+ sweepTimer ??= setInterval(sweepFrames, FRAME_SWEEP_MS);
379
+ sweepTimer.unref?.();
380
+ }
381
+ return id;
382
+ };
383
+ /** Forget a subframe sender for good: its connections, watches and sessions end. */
384
+ const dropSubframe = (id: number): void => {
385
+ const entry = senders.get(id);
386
+ if (entry === undefined || entry.frameTreeNodeId === null) return;
387
+ presentation?.senderGone(id);
388
+ senders.delete(id);
389
+ subframeIds.delete(entry.frameTreeNodeId);
390
+ service.senderGone(id);
391
+ if (subframeIds.size === 0 && sweepTimer !== undefined) {
392
+ clearInterval(sweepTimer);
393
+ sweepTimer = undefined;
394
+ }
395
+ };
396
+ /** Drop subframe senders whose frame was removed (Electron announces neither an iframe's removal nor its page's). */
397
+ function sweepFrames(): void {
398
+ for (const [id, entry] of [...senders]) {
399
+ if (entry.frameTreeNodeId === null) continue;
400
+ if (entry.wc.isDestroyed() || findFrame(entry.wc, entry.frameTreeNodeId) === undefined) dropSubframe(id);
401
+ }
402
+ }
403
+ /** Send to the one frame that owns the sender; a frame that is gone ends the sender. */
404
+ const deliver = (senderId: number, ch: string, payload: unknown): void => {
405
+ const entry = senders.get(senderId);
406
+ if (entry === undefined || entry.wc.isDestroyed()) return;
407
+ try {
408
+ if (entry.frameTreeNodeId === null) {
409
+ entry.wc.send(ch, payload);
410
+ return;
411
+ }
412
+ const frame = findFrame(entry.wc, entry.frameTreeNodeId);
413
+ if (frame?.send === undefined) {
414
+ queueMicrotask(() => dropSubframe(senderId)); // not inside the service's own emit
415
+ return;
416
+ }
417
+ frame.send(ch, payload);
418
+ } catch {
419
+ // the webContents or frame was destroyed between the check and the send
420
+ }
421
+ };
422
+ /**
423
+ * Starts whose URLs a display and a Cast device can both serve, while their picker is open. The one picker lists both kinds
424
+ * of sink, as Chrome's dialog does; a display chosen in it is recorded here and the Cast flow is told the user cancelled.
425
+ */
426
+ const mixedStarts = new Map<number, { displays: WiredDisplaySink[]; chosen: string | null }>();
427
+ /** Tab and desktop mirroring, when the `mirroring` option is given (set up once the service exists). */
428
+ let mirroring: MirroringManager | undefined;
84
429
  const service = new CastSessionService({
85
430
  devices: discovery,
86
431
  createClient,
87
- selectDevice: options.selectDevice,
432
+ // a mirroring route's session, or one being launched on the sink, is a local activity
433
+ ownsSession: (sessionId, sinkId) =>
434
+ mirroring?.routes().some((r) => r.sinkId === sinkId && (r.sessionId === sessionId || r.sessionId === "")) ?? false,
435
+ async selectDevice(devices, request, picker) {
436
+ const mixed = mixedStarts.get(request.senderId);
437
+ const choice = await options.selectDevice(devices, mixed === undefined ? request : { ...request, displays: mixed.displays }, picker);
438
+ if (typeof choice === "object" && choice !== null && "display" in choice) {
439
+ if (mixed !== undefined && mixed.displays.some((d) => d.id === choice.display)) mixed.chosen = choice.display;
440
+ return null;
441
+ }
442
+ return choice;
443
+ },
88
444
  allowOrigin: options.allowOrigin,
445
+ sinks: options.sinks,
446
+ ...(dial !== undefined && dialSource !== undefined ? {
447
+ dial: {
448
+ source: dialSource,
449
+ available: (app: string, origin: string) => dial.availableFor(app, origin),
450
+ monitor: (app: string, origin: string) => dial.monitorApp(app, origin),
451
+ },
452
+ } : {}),
453
+ accessCode: options.accessCode,
454
+ ...(networkMonitor !== undefined ? { networkMonitor } : {}),
455
+ // The same one-route-per-frame rule (`routes_by_frame_`) from the other side: a page's Cast SDK or Presentation API
456
+ // session ends a mirroring route of that page. Subframe sender ids never match a tab, as only the main frame is one.
457
+ onSessionLaunched: (senderId) => mirroring?.stopMirroringRoutesOfTab(senderId),
89
458
  emit(senderId, event) {
90
459
  try {
91
460
  handler?.onServiceEvent(senderId, event);
92
461
  } catch (err) {
93
462
  console.error("electron-cast: presentation handler failed on a service event", err);
94
463
  }
95
- const wc = contents.get(senderId);
96
- if (wc === undefined || wc.isDestroyed()) return;
97
- try {
98
- wc.send(`${channel}:event`, event);
99
- } catch {
100
- // the webContents was destroyed between the check and the send
101
- }
464
+ deliver(senderId, `${channel}:event`, event);
102
465
  },
103
466
  });
104
467
 
105
- /** Record a webContents on first use and watch it once. */
106
- const watch = (wc: WebContentsLike): void => {
107
- if (contents.has(wc.id)) return;
108
- contents.set(wc.id, wc);
468
+ /** Watch a webContents once: its destruction and its navigations end or park its senders. */
469
+ function watch(wc: WebContentsLike): void {
470
+ if (watched.has(wc.id)) return;
471
+ watched.add(wc.id);
472
+ const subframes = (): number[] =>
473
+ [...senders].filter(([, e]) => e.wc === wc && e.frameTreeNodeId !== null).map(([id]) => id);
109
474
  wc.once("destroyed", () => {
475
+ watched.delete(wc.id);
476
+ for (const id of subframes()) dropSubframe(id);
110
477
  presentation?.senderGone(wc.id);
111
- contents.delete(wc.id);
478
+ defaults.forget(wc.id);
479
+ senders.delete(wc.id);
112
480
  service.senderGone(wc.id);
113
481
  });
114
482
  // A navigation ends the page's connections, but its sessions stay open (parked) so the next page can rejoin them
115
483
  wc.on("did-navigate", () => {
116
484
  presentation?.senderGone(wc.id);
117
485
  service.senderNavigated(wc.id);
486
+ for (const id of subframes()) dropSubframe(id); // the page's iframes went with it
118
487
  });
119
- };
488
+ // An iframe that navigates on its own is treated the same way (the main frame is handled above).
489
+ wc.on("did-frame-navigate", (_event, _url, _code, _text, mainFrame, processId, routingId) => {
490
+ if (mainFrame === true) return;
491
+ const frame = wc.mainFrame?.framesInSubtree.find((f) => f.processId === processId && f.routingId === routingId);
492
+ const id = typeof frame?.frameTreeNodeId === "number" ? subframeIds.get(frame.frameTreeNodeId) : undefined;
493
+ if (id !== undefined) {
494
+ presentation?.senderGone(id);
495
+ service.senderNavigated(id);
496
+ }
497
+ sweepFrames();
498
+ });
499
+ }
120
500
 
121
501
  options.ipcMain.handle(`${channel}:call`, (event, method, params) => {
122
502
  const origin = originOf(event.senderFrame);
123
- if (origin === null) {
503
+ if (origin === null || event.senderFrame === null) {
124
504
  return Promise.resolve({ ok: false, code: "extension_missing", description: "No sender frame" });
125
505
  }
126
- watch(event.sender);
127
- return service.handle({ id: event.sender.id, origin }, method, params);
506
+ // mode "chromecast" is main-frame only; iframes are served in presentation mode, as in Chrome
507
+ if (!isMainFrame(event.senderFrame)) {
508
+ return Promise.resolve({ ok: false, code: "extension_missing", description: "chrome.cast is not available in iframes; use presentation mode" });
509
+ }
510
+ if (typeof method !== "string" || !PAGE_METHODS.has(method)) {
511
+ return Promise.resolve({ ok: false, code: "invalid_parameter", description: `unknown method: ${String(method)}` });
512
+ }
513
+ const id = registerSender(event.sender, event.senderFrame)!;
514
+ return service.handle({ id, origin }, method, params);
128
515
  });
129
516
 
517
+ const wired = options.wiredDisplay !== undefined && options.presentation !== false ? createWiredDisplay(options.wiredDisplay) : undefined;
518
+
130
519
  /** Presentation mode: IPC for the page's stand-in Presentation API, answered by a CastPresentationHandler. */
131
520
  const setUpPresentation = () => {
132
521
  const ipc = options.ipcMain;
133
522
  const on = ipc.on!.bind(ipc);
134
523
  const off = ipc.removeListener!.bind(ipc);
135
- const h = new CastPresentationHandler({ service });
524
+ const h = new CastPresentationHandler({ service, token: hashToken, ...(dial !== undefined ? { dial } : {}), dialRoutesChanged: notifyRoutes });
136
525
  handler = h;
137
- const connections = new Map<string, { conn: PresentationConnectionHandle | null; wcId: number }>();
138
- const watches = new Map<string, { ac: AbortController; wcId: number }>();
139
- const sendTo = (wcId: number, payload: unknown): void => {
140
- const wc = contents.get(wcId);
141
- if (wc === undefined || wc.isDestroyed()) return;
142
- try {
143
- wc.send(`${channel}:presentation:event`, payload);
144
- } catch {
145
- // destroyed between the check and the send
146
- }
526
+ const connections = new Map<string, { conn: PresentationConnectionHandle | null; sid: number }>();
527
+ const watches = new Map<string, { ac: AbortController; sid: number }>();
528
+ const sendTo = (sid: number, payload: unknown): void => {
529
+ deliver(sid, `${channel}:presentation:event`, payload);
147
530
  };
148
- const invalid = (description: string) => ({ ok: false, code: "InvalidAccessError", description });
531
+ const invalid = (description: string) => ({ ok: false as const, code: "InvalidAccessError", description });
149
532
  const isId = (v: unknown): v is string => typeof v === "string" && v !== "" && v.length <= 256;
150
533
  const isUrls = (v: unknown): v is string[] =>
151
534
  Array.isArray(v) && v.length > 0 && v.length <= 16 && v.every((u) => typeof u === "string");
152
535
 
153
536
  /** Finish a start/reconnect: register the connection and route its traffic to the page. */
154
- const open = async (wcId: number, connectionId: string, opening: () => Promise<PresentationConnectionHandle>) => {
537
+ const open = async (
538
+ sid: number,
539
+ connectionId: string,
540
+ opening: () => Promise<PresentationConnectionHandle>,
541
+ /** Runs once the connection exists and before any of its messages reach the page; false declines it. */
542
+ opened?: (conn: PresentationConnectionHandle) => boolean,
543
+ ): Promise<{ ok: false; code: string; description: string } | { ok: true; value: { id: string; url: string } }> => {
155
544
  if (connections.has(connectionId)) return invalid("connectionId in use");
156
- const slot: { conn: PresentationConnectionHandle | null; wcId: number } = { conn: null, wcId };
545
+ const slot: { conn: PresentationConnectionHandle | null; sid: number } = { conn: null, sid };
157
546
  connections.set(connectionId, slot); // reserved while the picker or join is pending
158
547
  let conn: PresentationConnectionHandle;
159
548
  try {
@@ -164,25 +553,85 @@ export function createCastManager(options: CastManagerOptions): CastManager {
164
553
  const name = typeof e?.name === "string" && e.name !== "" && e.name !== "Error" ? e.name : "OperationError";
165
554
  return { ok: false, code: name, description: typeof e?.message === "string" ? e.message : String(err) };
166
555
  }
167
- if (!contents.has(wcId) || connections.get(connectionId) !== slot) {
556
+ if (!senders.has(sid) || connections.get(connectionId) !== slot) {
168
557
  conn.close();
169
558
  return { ok: false, code: "AbortError", description: "The page went away" };
170
559
  }
171
560
  slot.conn = conn;
561
+ // A wired display connection starts out connecting, as Blink's does, and connects when the receiver page is ready
562
+ const local = conn as Partial<LocalConnectionHandle>;
563
+ if (typeof local.connecting === "boolean") {
564
+ (conn as LocalConnectionHandle).onconnect = () => sendTo(sid, { type: "statechange", connectionId, state: "connected" });
565
+ }
172
566
  conn.onclose = (state) => {
173
567
  if (connections.get(connectionId) === slot) connections.delete(connectionId);
174
- sendTo(wcId, { type: "statechange", connectionId, state });
568
+ sendTo(sid, { type: "statechange", connectionId, state });
175
569
  };
570
+ if (opened?.(conn) === false) {
571
+ // Declined (OnPresentationResponse hands a connection only to the request it answers): the session stays, but the
572
+ // connection belongs to nobody, so nothing of it reaches the page that has the sender id now
573
+ connections.delete(connectionId);
574
+ conn.onclose = null;
575
+ conn.onmessage = () => {};
576
+ return { ok: true as const, value: { id: conn.id, url: conn.url } };
577
+ }
176
578
  // The stand-in registered this connectionId before calling, so it buffers anything sent before the reply.
177
- conn.onmessage = (data) => sendTo(wcId, { type: "message", connectionId, data });
178
- return { ok: true, value: { id: conn.id, url: conn.url } };
579
+ if (typeof local.connecting === "boolean") {
580
+ (conn as LocalConnectionHandle).onmessage = (data) => sendTo(sid, { type: "message", connectionId, ...messageFields(data) });
581
+ } else {
582
+ conn.onmessage = (data) => sendTo(sid, { type: "message", connectionId, data });
583
+ }
584
+ return { ok: true, value: { id: conn.id, url: conn.url, ...(local.connecting === true ? { state: "connecting" } : {}), ...(typeof local.connecting === "boolean" ? { local: true } : {}) } };
179
585
  };
180
586
 
587
+ /** A start() whose URLs only a display can show: the app picks a display, a window opens on it. */
588
+ const startOnDisplay = async (sid: number, origin: string, urls: string[]): Promise<PresentationConnectionHandle> => {
589
+ const w = wired!;
590
+ const wc = senders.get(sid)?.wc;
591
+ const sinks = w.sinksFor(wc);
592
+ const request = { urls: [...urls], origin, senderId: sid, observeSinks: (l: Parameters<typeof w.observeSinksFor>[1]) => w.observeSinksFor(wc, l) };
593
+ const choice = await w.selectDisplay(sinks, request);
594
+ if (choice === null) throw new PresentationError("AbortError", "The user cancelled the request");
595
+ return w.controllers.start(request, choice);
596
+ };
597
+ /** A URL the Cast provider serves (a Cast app URL), or the DIAL provider (a `cast-dial:` URL, when DIAL is on). */
598
+ const providerUrl = (u: string): boolean => parsePresentationUrl(u) !== null || (dial !== undefined && isDialSource(u));
599
+ /** The URLs of a request that the Cast and DIAL providers serve. */
600
+ const castUrlsOf = (urls: string[]): string[] => urls.filter(providerUrl);
601
+ /**
602
+ * A start() whose URLs a display and a Cast device can both serve: one picker offers both kinds and the sink it returns
603
+ * decides (a display starts a wired display presentation, a device the Cast flow). With no displays or no devices there
604
+ * is nothing to choose between and the flow of the one kind that exists runs.
605
+ */
606
+ const startMixed = async (sid: number, origin: string, urls: string[]): Promise<PresentationConnectionHandle> => {
607
+ const w = wired!;
608
+ const wc = senders.get(sid)?.wc;
609
+ const displays = w.sinksFor(wc);
610
+ const castUrls = castUrlsOf(urls);
611
+ if (displays.length === 0 || service.devices().length + (dial?.devices().length ?? 0) === 0) {
612
+ return displays.length > 0 ? startOnDisplay(sid, origin, urls) : h.startPresentation({ urls: castUrls, origin, senderId: sid });
613
+ }
614
+ const mixed = { displays, chosen: null as string | null };
615
+ mixedStarts.set(sid, mixed);
616
+ try {
617
+ return await h.startPresentation({ urls: castUrls, origin, senderId: sid });
618
+ } catch (err) {
619
+ if (mixed.chosen === null) throw err;
620
+ return w.controllers.start({ urls: [...urls], origin, senderId: sid }, mixed.chosen);
621
+ } finally {
622
+ mixedStarts.delete(sid);
623
+ }
624
+ };
625
+
626
+ /** Only a display serves these URLs: one is a standard presentation source and none is a Cast or DIAL one. */
627
+ const displayOnly = (urls: string[]): boolean =>
628
+ wired !== undefined && urls.some((u) => isWiredDisplaySource(u)) && !urls.some(providerUrl);
629
+
181
630
  ipc.handle(`${channel}:presentation`, async (event, method, params) => {
182
631
  const origin = originOf(event.senderFrame);
183
- if (origin === null) return { ok: false, code: "NotAllowedError", description: "No sender frame" };
184
- watch(event.sender);
185
- const wcId = event.sender.id;
632
+ if (origin === null || event.senderFrame === null) return { ok: false, code: "NotAllowedError", description: "No sender frame" };
633
+ const sid = registerSender(event.sender, event.senderFrame);
634
+ if (sid === null) return { ok: false, code: "NotAllowedError", description: "Unidentifiable sender frame" };
186
635
  const p = (typeof params === "object" && params !== null ? params : {}) as Record<string, unknown>;
187
636
  try {
188
637
  switch (method) {
@@ -190,28 +639,70 @@ export function createCastManager(options: CastManagerOptions): CastManager {
190
639
  if (!isId(p.watchId) || !isUrls(p.urls)) return invalid("getAvailability expects { watchId, urls }");
191
640
  const watchId = p.watchId;
192
641
  const old = watches.get(watchId);
193
- if (old !== undefined && old.wcId !== wcId) return invalid("watchId in use");
642
+ if (old !== undefined && old.sid !== sid) return invalid("watchId in use");
194
643
  old?.ac.abort();
195
644
  watches.delete(watchId);
196
- const mine = [...watches].filter(([, w]) => w.wcId === wcId);
645
+ const mine = [...watches].filter(([, w]) => w.sid === sid);
197
646
  for (const [id, w] of mine.slice(0, Math.max(0, mine.length - (MAX_WATCHES_PER_PAGE - 1)))) {
198
647
  w.ac.abort();
199
648
  watches.delete(id);
200
649
  }
201
650
  const ac = new AbortController();
202
- watches.set(watchId, { ac, wcId });
651
+ watches.set(watchId, { ac, sid });
652
+ if (wired !== undefined && p.urls.some((u) => isWiredDisplaySource(u))) {
653
+ // Blink ORs the sources of a request: available when displays exist for a standard source, or the
654
+ // request's Cast URLs have a device. Displays are not hidden here (only the dialog hides one).
655
+ const castUrls = p.urls.filter((u) => !isWiredDisplaySource(u));
656
+ let displays = false;
657
+ let cast = false;
658
+ let ready = false;
659
+ let last = false;
660
+ const update = (): void => {
661
+ const next = displays || cast;
662
+ if (!ready || next === last) return;
663
+ last = next;
664
+ sendTo(sid, { type: "availability", watchId, value: next });
665
+ };
666
+ const stop = wired.provider.observeSinks((sinks) => {
667
+ displays = sinks.length > 0;
668
+ update();
669
+ });
670
+ ac.signal.addEventListener("abort", stop, { once: true });
671
+ if (ac.signal.aborted) stop();
672
+ if (castUrls.length > 0) {
673
+ cast = await h.getAvailability({
674
+ urls: castUrls, origin, senderId: sid, signal: ac.signal,
675
+ onchange: (value) => {
676
+ cast = value;
677
+ update();
678
+ },
679
+ });
680
+ }
681
+ ready = true;
682
+ last = displays || cast;
683
+ return { ok: true, value: { available: last } };
684
+ }
203
685
  const available = await h.getAvailability({
204
686
  urls: p.urls,
205
687
  origin,
206
- senderId: wcId,
688
+ senderId: sid,
207
689
  signal: ac.signal,
208
- onchange: (value) => sendTo(wcId, { type: "availability", watchId, value }),
690
+ onchange: (value) => sendTo(sid, { type: "availability", watchId, value }),
209
691
  });
210
692
  return { ok: true, value: { available } };
211
693
  }
694
+ case "setDefaultRequest": {
695
+ // Blink's Presentation::setDefaultRequest sends the URLs on every set (none when it is cleared)
696
+ if (!Array.isArray(p.urls) || p.urls.length > 16 || !p.urls.every((u) => typeof u === "string" && u.length <= 4096)) {
697
+ return invalid("setDefaultRequest expects { urls }");
698
+ }
699
+ // PresentationServiceImpl::SetDefaultPresentationUrls ignores a frame that is not the outermost document
700
+ if (isMainFrame(event.senderFrame)) defaults.set(event.sender.id, p.urls as string[], origin);
701
+ return { ok: true, value: undefined };
702
+ }
212
703
  case "stopWatching": {
213
704
  const w = isId(p.watchId) ? watches.get(p.watchId) : undefined;
214
- if (w !== undefined && w.wcId === wcId) {
705
+ if (w !== undefined && w.sid === sid) {
215
706
  w.ac.abort();
216
707
  watches.delete(p.watchId as string);
217
708
  }
@@ -219,9 +710,21 @@ export function createCastManager(options: CastManagerOptions): CastManager {
219
710
  }
220
711
  case "start":
221
712
  if (!isId(p.connectionId) || !isUrls(p.urls)) return invalid("start expects { connectionId, urls }");
713
+ // PresentationServiceImpl::StartPresentation: PRESENTATION_REQUEST_CANCELLED, which the page sees as AbortError
714
+ if (p.hasGesture !== true) {
715
+ return { ok: false, code: "AbortError", description: "PresentationRequest::start() requires user gesture." };
716
+ }
222
717
  {
223
718
  const urls = p.urls;
224
- return await open(wcId, p.connectionId, () => h.startPresentation({ urls, origin, senderId: wcId }));
719
+ // A display-only request opens a window on a display; a request a display and a sink can both serve goes to
720
+ // the one picker; the rest is the Cast (and DIAL) provider's
721
+ if (displayOnly(urls)) return await open(sid, p.connectionId, () => startOnDisplay(sid, origin, urls));
722
+ const started = wired !== undefined && urls.some((u) => isWiredDisplaySource(u))
723
+ ? await open(sid, p.connectionId, () => startMixed(sid, origin, urls))
724
+ : await open(sid, p.connectionId, () => h.startPresentation({ urls, origin, senderId: sid }));
725
+ // CastActivityManager's earlier-route rule is for Cast sessions only
726
+ if (started.ok && started.value.id.startsWith("cast-session_")) launchedFrom.set(sid, started.value.id.slice("cast-session_".length));
727
+ return started;
225
728
  }
226
729
  case "reconnect":
227
730
  if (!isId(p.connectionId) || !isUrls(p.urls) || !isId(p.presentationId)) {
@@ -229,13 +732,18 @@ export function createCastManager(options: CastManagerOptions): CastManager {
229
732
  }
230
733
  {
231
734
  const { urls, presentationId } = p;
232
- return await open(wcId, p.connectionId,
233
- () => h.reconnectPresentation({ urls, origin, senderId: wcId, presentationId }));
735
+ // A local presentation (a display) is joined again from any page, by its id; for a URL only a display
736
+ // serves, an id that is not local is the provider's unknown error, which the page sees as NotFoundError
737
+ if (wired !== undefined && (displayOnly(urls) || wired.provider.local.isLocalPresentation(presentationId))) {
738
+ return await open(sid, p.connectionId, async () => wired.controllers.reconnect({ urls, senderId: sid }, presentationId));
739
+ }
740
+ return await open(sid, p.connectionId,
741
+ () => h.reconnectPresentation({ urls: wired !== undefined && castUrlsOf(urls).length > 0 ? castUrlsOf(urls) : urls, origin, senderId: sid, presentationId }));
234
742
  }
235
743
  case "close":
236
744
  case "terminate": {
237
745
  const c = isId(p.connectionId) ? connections.get(p.connectionId) : undefined;
238
- if (c !== undefined && c.wcId === wcId && c.conn !== null) {
746
+ if (c !== undefined && c.sid === sid && c.conn !== null) {
239
747
  if (method === "close") c.conn.close();
240
748
  else c.conn.terminate();
241
749
  }
@@ -250,25 +758,123 @@ export function createCastManager(options: CastManagerOptions): CastManager {
250
758
  });
251
759
 
252
760
  const onSend = (event: IpcMainEventLike, connectionId: unknown, data: unknown): void => {
253
- if (typeof connectionId !== "string" || typeof data !== "string") return;
761
+ if (typeof connectionId !== "string") return;
254
762
  const c = connections.get(connectionId);
255
- if (c === undefined || c.wcId !== event.sender.id || c.conn === null) return;
256
- if (data.length > MAX_MESSAGE_CHARS) return;
257
- c.conn.send(data);
763
+ if (c === undefined || c.sid !== senderIdOf(event.sender, event.senderFrame) || c.conn === null) return;
764
+ const isLocal = typeof (c.conn as Partial<LocalConnectionHandle>).connecting === "boolean";
765
+ if (typeof data === "string") {
766
+ // The stand-in refuses an oversized send() with a RangeError; this is for a page that does not use it
767
+ if (data.length <= (isLocal ? MAX_MESSAGE_SIZE : MAX_MESSAGE_CHARS)) c.conn.send(data);
768
+ return;
769
+ }
770
+ const bytes = asBytes(data);
771
+ if (bytes === null || bytes.byteLength > MAX_MESSAGE_SIZE) return;
772
+ // A wired display carries bytes (PresentationConnectionMessage.data). A Cast connection drops them, as Chrome's
773
+ // CastSessionClientImpl::OnMessage does with anything that is not text.
774
+ if (isLocal) (c.conn as LocalConnectionHandle).send(bytes);
258
775
  };
259
776
  on(`${channel}:presentation:send`, onSend);
260
777
 
261
778
  return {
262
779
  /** Close the page's connections and watches before the service drops it. */
263
- senderGone(wcId: number): void {
264
- h.senderGone(wcId);
265
- for (const [id, c] of connections) if (c.wcId === wcId) connections.delete(id);
780
+ senderGone(sid: number): void {
781
+ // PresentationServiceImpl::Reset (navigation, frame gone): the frame's default request goes with it
782
+ defaults.clear(sid);
783
+ launchedFrom.delete(sid);
784
+ h.senderGone(sid);
785
+ wired?.controllers.senderGone(sid);
786
+ for (const [id, c] of connections) if (c.sid === sid) connections.delete(id);
266
787
  for (const [id, w] of watches) {
267
- if (w.wcId !== wcId) continue;
788
+ if (w.sid !== sid) continue;
268
789
  w.ac.abort();
269
790
  watches.delete(id);
270
791
  }
271
792
  },
793
+ /**
794
+ * MediaRouterUI::StartCasting(sink, PRESENTATION) on the window's default request: MediaRouteStarter's
795
+ * CreateRouteParameters, MediaRouter::CreateRoute, then WebContentsPresentationManager::OnPresentationResponse.
796
+ */
797
+ async startDefaultPresentation(webContentsId: number, sinkId: string): Promise<MediaRoute> {
798
+ const request = defaults.get(webContentsId);
799
+ const serial = defaults.serialOf(webContentsId);
800
+ // No source for the sink (no default request, or its URLs do not suit the sink): CreateRouteParameters is null
801
+ const unable = (why: string) => new MediaRouteError("unable_to_cast", why);
802
+ if (request === null) throw unable("the window has no default presentation request");
803
+ // The dialog's sinks are the Cast sinks and the DIAL sinks (QueryResultManager observes each URL's source, and
804
+ // the DIAL provider serves the cast-dial: ones)
805
+ const device = [...service.devices(), ...(dial?.devices() ?? [])].find((d) => d.id === sinkId);
806
+ if (device === undefined) throw unable(`no sink ${sinkId}`);
807
+ // GetSourceForCastModeAndSink: the first URL, in order, the sink is compatible with. A Cast sink is a source's
808
+ // once the device says the app is there (CastAppAvailabilityTracker::GetAvailableSinks); a DIAL sink once its
809
+ // DIAL server says so for the page's origin (DialMediaSinkServiceImpl, DialMediaRouteProvider::GetOrigins).
810
+ let url: string | null = null;
811
+ if (device.dial !== undefined) {
812
+ url = dial === undefined ? null : await dialUrlFor(dial, device, request.presentationUrls, request.frameOrigin);
813
+ } else {
814
+ for (const candidate of request.presentationUrls) {
815
+ const parsed = parsePresentationUrl(candidate);
816
+ if (parsed === null || presentationUrlForDevice(device, [candidate]) === null) continue;
817
+ if (await service.checkAppAvailability(device, parsed.appId)) {
818
+ url = candidate;
819
+ break;
820
+ }
821
+ }
822
+ }
823
+ if (url === null) throw unable("the sink cannot run the default presentation request's URLs");
824
+ // the page navigated or cleared its request while the device was asked
825
+ if (defaults.serialOf(webContentsId) !== serial) {
826
+ throw new MediaRouteError("cancelled", "the window's default presentation request changed while the device was asked");
827
+ }
828
+ // CastActivityManager::DoLaunchSession stops the route this frame launched before, then launches
829
+ const earlier = launchedFrom.get(webContentsId);
830
+ if (earlier !== undefined && routes().some((r) => r.id === earlier)) void service.terminateSession(earlier);
831
+ if (!senders.has(webContentsId)) throw unable("the window is gone");
832
+ const connectionId = randomUUID();
833
+ let failure: unknown;
834
+ const result = await open(
835
+ webContentsId,
836
+ connectionId,
837
+ () => h.startPresentation({
838
+ urls: [url, ...request.presentationUrls.filter((u) => u !== url)],
839
+ origin: request.frameOrigin,
840
+ senderId: webContentsId,
841
+ deviceId: device.id,
842
+ }).catch((err: unknown) => {
843
+ failure = err;
844
+ throw err;
845
+ }),
846
+ (conn) => {
847
+ // OnPresentationResponse hands the connection to the page only while the request it answers is still the default one
848
+ const current = defaults.get(webContentsId);
849
+ if (current === null || !samePresentationRequest(current, request)) return false;
850
+ sendTo(webContentsId, { type: "connectionavailable", connectionId, id: conn.id, url: conn.url });
851
+ return true;
852
+ },
853
+ );
854
+ if (!result.ok) {
855
+ // RouteRequestResultCode, from the cause the handler saw
856
+ const bridge = (failure as { bridgeCode?: unknown } | undefined)?.bridgeCode;
857
+ const code = bridge === "timeout" ? "timed_out"
858
+ : bridge === "cancel" || (failure === undefined && result.code === "AbortError") ? "cancelled"
859
+ : bridge === "receiver_unavailable" ? "sink_not_found"
860
+ : bridge === "extension_missing" ? "invalid_origin"
861
+ : bridge === "invalid_parameter" ? "redundant_request"
862
+ : "unknown_error";
863
+ throw new MediaRouteError(code, result.description);
864
+ }
865
+ if (device.dial !== undefined) {
866
+ // A DIAL route's id is MediaRoute::GetMediaRouteId(presentation id, sink id, source)
867
+ const prefix = `urn:x-org.chromium:media:route:${result.value.id}/`;
868
+ const dialRoute = routes().find((r) => r.providerId === "dial" && r.id.startsWith(prefix));
869
+ if (dialRoute === undefined) throw new MediaRouteError("route_not_found", `no route for ${result.value.id}`);
870
+ return dialRoute;
871
+ }
872
+ const sessionId = result.value.id.slice("cast-session_".length);
873
+ launchedFrom.set(webContentsId, sessionId);
874
+ const route = routes().find((r) => r.id === sessionId);
875
+ if (route === undefined) throw new MediaRouteError("route_not_found", `no route ${sessionId}`);
876
+ return route;
877
+ },
272
878
  dispose(): void {
273
879
  ipc.removeHandler(`${channel}:presentation`);
274
880
  off(`${channel}:presentation:send`, onSend);
@@ -280,18 +886,206 @@ export function createCastManager(options: CastManagerOptions): CastManager {
280
886
  };
281
887
  const presentation = options.presentation !== false ? setUpPresentation() : undefined;
282
888
 
889
+ // The service knows frames by sender id (an iframe has a synthetic one); the API speaks of windows, so a route's
890
+ // pages are reported as `webContents.id`s, once each however many of its frames hold the route.
891
+ const windowIds = (ids: number[]): number[] => [...new Set(ids.map((id) => senders.get(id)?.wc.id ?? id))];
892
+ // Tab and desktop mirroring: MirroringActivity, a CastActivity that runs on the device's one channel
893
+ let disposeMirroring: (() => void) | undefined;
894
+ if (options.mirroring !== undefined) {
895
+ const m = options.mirroring;
896
+ let createService: MirroringServiceFactory;
897
+ if ("createService" in m) {
898
+ createService = m.createService;
899
+ } else {
900
+ const electron = createElectronMirroringServiceFactory(m);
901
+ createService = electron.factory;
902
+ disposeMirroring = () => electron.dispose();
903
+ }
904
+ mirroring = new MirroringManager({
905
+ // only a sink (an open, authenticated channel) can be cast to
906
+ findDevice: (id) => service.devices().find((d) => d.id === id),
907
+ createClient: (d) => sharedChannelClient(d.id, service.channelClient(d.id)),
908
+ createService,
909
+ terminateRoutesOfPage: (wcId) => {
910
+ for (const id of service.sessionsLaunchedBy(wcId)) void service.terminateSession(id);
911
+ },
912
+ commandLine: "electron" in m ? m.electron.app?.commandLine : undefined,
913
+ log: (level, message) => (level === "error" ? console.error : console.debug)(`electron-cast mirroring: ${message}`),
914
+ });
915
+ }
916
+ const dialRoutes = (): MediaRoute[] =>
917
+ (dial?.provider.routes() ?? []).map((r) => {
918
+ const members = windowIds(handler?.dialMembers(r.id) ?? []);
919
+ return {
920
+ id: r.id,
921
+ providerId: "dial" as const,
922
+ mediaSource: r.mediaSource,
923
+ sinkId: r.sinkId,
924
+ sinkName: dial?.devices().find((d) => d.id === r.sinkId)?.name ?? "",
925
+ isLocal: true,
926
+ description: r.appName,
927
+ appId: "",
928
+ displayName: r.appName,
929
+ origin: r.origin,
930
+ volume: { level: null, muted: null },
931
+ members,
932
+ parkedSenders: [],
933
+ parked: members.length === 0,
934
+ };
935
+ });
936
+ const mirroringRoutes = (): MediaRoute[] => (mirroring?.routes() ?? []).map((r: MirroringRoute) => ({
937
+ id: r.id,
938
+ providerId: "cast" as const,
939
+ mediaSource: r.mediaSource,
940
+ sinkId: r.sinkId,
941
+ sinkName: service.devices().find((d) => d.id === r.sinkId)?.name ?? "",
942
+ isLocal: true,
943
+ description: r.description,
944
+ appId: r.appId,
945
+ displayName: r.displayName,
946
+ origin: "",
947
+ volume: { level: null, muted: null },
948
+ members: r.webContentsId === undefined ? [] : [r.webContentsId],
949
+ parkedSenders: [],
950
+ parked: false,
951
+ freezeInfo: r.freezeInfo,
952
+ }));
953
+ const routes = (): MediaRoute[] => [
954
+ ...service.mediaRoutes().map((r) => ({ ...r, members: windowIds(r.members), parkedSenders: windowIds(r.parkedSenders) })),
955
+ ...dialRoutes(),
956
+ ...mirroringRoutes(),
957
+ ];
958
+ const isMirroringRoute = (id: string): boolean => mirroring?.routes().some((r) => r.id === id) ?? false;
959
+ const isDialRoute = (id: string): boolean => dial?.provider.routes().some((r) => r.id === id) ?? false;
960
+ const notDial = { ok: false as const, code: "invalid_parameter", description: "A DIAL route has no volume or media control" };
961
+ const castDevices = service.deviceSource();
962
+ const api = createManagerApi(createSinksObservers(dialSource === undefined ? castDevices : combineSources(castDevices, dialSource), {
963
+ cached: (d, appId) => service.cachedAppAvailability(d, appId),
964
+ check: (d, appId) => service.checkAppAvailability(d, appId),
965
+ ...(dial === undefined ? {} : {
966
+ dial: {
967
+ available: (d: CastDevice, app: string, origin: string) => dial.availableFor(app, origin).some((x) => x.id === d.id),
968
+ monitor: (app: string, origin: string) => dial.monitorApp(app, origin),
969
+ },
970
+ }),
971
+ }), {
972
+ mediaRoutes: routes,
973
+ observeSessions: (l) => {
974
+ const off = service.observeSessions(l);
975
+ const offMirroring = mirroring?.observe(l);
976
+ routeListeners.add(l);
977
+ return () => {
978
+ off();
979
+ offMirroring?.();
980
+ routeListeners.delete(l);
981
+ };
982
+ },
983
+ async terminateSession(id) {
984
+ if (mirroring?.terminate(id) === true) return { ok: true, value: undefined };
985
+ if (!isDialRoute(id)) return service.terminateSession(id);
986
+ try {
987
+ await dial!.provider.terminateRoute(id);
988
+ return { ok: true, value: undefined };
989
+ } catch (err) {
990
+ const e = err as { code?: unknown; message?: unknown };
991
+ return { ok: false, code: typeof e.code === "string" ? e.code : "session_error", description: String(e.message ?? err) };
992
+ }
993
+ },
994
+ setSessionVolume: async (id, v) => (isDialRoute(id) ? notDial : service.setSessionVolume(id, v)),
995
+ startCasting:
996
+ mirroring === undefined
997
+ ? undefined
998
+ : async (sinkId, o) => {
999
+ const m = mirroring!;
1000
+ try {
1001
+ const route = await m.startMirroring(sinkId, o);
1002
+ const listed = routes().find((r) => r.id === route.id);
1003
+ if (listed === undefined) throw new MediaRouteError("route_not_found", `no route ${route.id}`);
1004
+ return listed;
1005
+ } catch (err) {
1006
+ if (err instanceof MirroringRouteError) throw new MediaRouteError(err.code, err.message);
1007
+ throw err;
1008
+ }
1009
+ },
1010
+ detachSession(id) {
1011
+ if (isMirroringRoute(id)) return { ok: false, code: "invalid_parameter", description: "A mirroring route cannot be detached" };
1012
+ if (!isDialRoute(id)) return service.detachSession(id);
1013
+ handler?.detachDialRoute(id);
1014
+ return { ok: true, value: undefined };
1015
+ },
1016
+ sendSessionMessage: (id, ns, m) => (isDialRoute(id) ? notDial : service.sendSessionMessage(id, ns, m)),
1017
+ observeSessionStatus: (l) => service.observeSessionStatus(l),
1018
+ observeMediaStatus: (l) => service.observeMediaStatus(l),
1019
+ sessionVolume: (id) => service.sessionVolume(id),
1020
+ switchMirroringSourceTab:
1021
+ mirroring === undefined
1022
+ ? undefined
1023
+ : (id, webContentsId) => {
1024
+ try {
1025
+ mirroring!.switchMirroringSourceTab(id, webContentsId);
1026
+ } catch (err) {
1027
+ if (err instanceof MirroringRouteError) throw new MediaRouteError(err.code, err.message);
1028
+ throw err;
1029
+ }
1030
+ },
1031
+ freezeRoute: (id) => mirroring?.freezeRoute(id) ?? false,
1032
+ unfreezeRoute: (id) => mirroring?.unfreezeRoute(id) ?? false,
1033
+ mirroringStats:
1034
+ mirroring === undefined
1035
+ ? undefined
1036
+ : {
1037
+ get: (id) => mirroring.getMirroringStats(id),
1038
+ setEnabled: (enabled) => mirroring.setMirroringStatsEnabled(enabled),
1039
+ isEnabled: () => mirroring.isMirroringStatsEnabled(),
1040
+ observe: (observer) => mirroring.observeMirroringStats(observer),
1041
+ },
1042
+ }, {
1043
+ requests: defaults,
1044
+ start: (webContentsId, sinkId) => {
1045
+ if (presentation === undefined) return Promise.reject(new MediaRouteError("unable_to_cast", "presentation mode is off"));
1046
+ return presentation.startDefaultPresentation(webContentsId, sinkId);
1047
+ },
1048
+ });
1049
+ // Cast sinks coming and going decide which DIAL sinks are shown; the DIAL sinks are looked up again.
1050
+ castSinks = () => service.devices();
1051
+ const castChanged = (): void => dial?.castDevicesChanged();
1052
+ if (dial !== undefined) for (const e of ["deviceUp", "deviceDown", "deviceChanged"] as const) castDevices.on(e, castChanged);
283
1053
  let disposed = false;
284
1054
  return {
285
- start: () => discovery.start(),
286
- devices: () => discovery.devices(),
1055
+ ...api,
1056
+ ...(wired !== undefined ? { wiredDisplay: wired.api } : {}),
1057
+ async start() {
1058
+ networkMonitor?.start();
1059
+ await discovery.start();
1060
+ dial?.start({ online: networkMonitor === undefined || networkMonitor.networkId() !== NETWORK_ID_DISCONNECTED });
1061
+ },
1062
+ devices: () => service.devices(),
1063
+ discoverSinksNow: () => service.discoverSinksNow(),
1064
+ settled: (options) => service.settled(options),
1065
+ addSinkByAddress: (device) => service.addSinkByAddress(device),
1066
+ configureAccessCode: (settings) => service.configureAccessCode(settings),
1067
+ rememberedSinks: () => service.rememberedSinks(),
287
1068
  async dispose() {
288
1069
  if (disposed) return;
289
1070
  disposed = true;
1071
+ api.dispose();
1072
+ defaults.dispose();
1073
+ mirroring?.dispose();
1074
+ disposeMirroring?.();
290
1075
  options.ipcMain.removeHandler(`${channel}:call`);
291
1076
  presentation?.dispose();
1077
+ if (dial !== undefined) for (const e of ["deviceUp", "deviceDown", "deviceChanged"] as const) castDevices.off(e, castChanged);
1078
+ unobserveDialNetwork();
1079
+ dial?.stop();
1080
+ networkMonitor?.stop();
1081
+ wired?.dispose();
292
1082
  discovery.stop();
293
1083
  await service.dispose();
294
- contents.clear();
1084
+ senders.clear();
1085
+ subframeIds.clear();
1086
+ watched.clear();
1087
+ if (sweepTimer !== undefined) clearInterval(sweepTimer);
1088
+ sweepTimer = undefined;
295
1089
  },
296
1090
  };
297
1091
  }