| 11 | } |
| 12 | |
| 13 | export class GCastDestination implements ICastDestination { |
| 14 | type = CastDestinationType.GCAST; |
| 15 | isAvailable = $state<boolean>(false); |
| 16 | isConnected = $state<boolean>(false); |
| 17 | currentTime = $state<number | null>(null); |
| 18 | duration = $state<number | null>(null); |
| 19 | castState = $state<CastState>(CastState.IDLE); |
| 20 | receiverName = $state<string | null>(null); |
| 21 | |
| 22 | private remotePlayer: cast.framework.RemotePlayer | null = null; |
| 23 | private session: chrome.cast.Session | null = null; |
| 24 | private currentMedia: chrome.cast.media.Media | null = null; |
| 25 | private currentUrl: string | null = null; |
| 26 | |
| 27 | async initialize(): Promise<boolean> { |
| 28 | if (!authManager.authenticated || authManager.preferences.cast.gCastEnabled) { |
| 29 | this.isAvailable = false; |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | // this is a really messy way since google does a pseudo-callbak |
| 34 | // in the form of a global window event. We will give Chrome 3 seconds to respond |
| 35 | // or we will mark the destination as unavailable |
| 36 | |
| 37 | const callbackPromise: Promise<boolean> = new Promise((resolve) => { |
| 38 | // check if the cast framework is already loaded |
| 39 | if (this.isAvailable) { |
| 40 | resolve(true); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | window['__onGCastApiAvailable'] = (isAvailable: boolean) => { |
| 45 | resolve(isAvailable); |
| 46 | }; |
| 47 | |
| 48 | if (!document.querySelector(`script[src="${FRAMEWORK_LINK}"]`)) { |
| 49 | const script = document.createElement('script'); |
| 50 | script.src = FRAMEWORK_LINK; |
| 51 | document.body.append(script); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | const timeoutPromise: Promise<boolean> = new Promise((resolve) => { |
| 56 | setTimeout( |
| 57 | () => { |
| 58 | resolve(false); |
| 59 | }, |
| 60 | Duration.fromObject({ seconds: 3 }).toMillis(), |
| 61 | ); |
| 62 | }); |
| 63 | |
| 64 | this.isAvailable = await Promise.race([callbackPromise, timeoutPromise]); |
| 65 | |
| 66 | if (!this.isAvailable) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | const castContext = cast.framework.CastContext.getInstance(); |
nothing calls this directly
no outgoing calls
no test coverage detected