forked from ardoviniandrea/ViniPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast.js
More file actions
334 lines (294 loc) · 13.1 KB
/
Copy pathcast.js
File metadata and controls
334 lines (294 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* cast.js
* Manages all Google Cast related functionality.
*/
import { showNotification } from './ui.js';
import { UIElements, guideState, appState } from './state.js';
import { stopStream } from './api.js';
import { forceRefreshStream } from './player.js';
const APPLICATION_ID = 'CC1AD845'; // Default Media Receiver App ID
export const castState = {
isAvailable: false,
isCasting: false,
session: null,
player: null,
playerController: null,
currentMedia: null,
currentCastStreamUrl: null, // Track the current Cast stream URL for cleanup
localPlayerState: {
streamUrl: null,
name: null,
logo: null
}
};
/**
* Stops a Cast stream on the server by sending a stop request.
* @param {string} streamUrl - The stream URL to stop.
*/
async function stopCastStream(streamUrl) {
try {
console.log(`[CAST] Sending stop request for Cast stream: ${streamUrl}`);
const activeCastProfileId = guideState.settings?.activeCastProfileId || 'cast-default';
const response = await fetch('/api/stream/stop', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: streamUrl, profileId: activeCastProfileId })
});
if (response.ok) {
console.log('[CAST] Cast stream stopped successfully on server.');
} else {
console.warn('[CAST] Failed to stop Cast stream on server:', response.status);
}
} catch (error) {
console.error('[CAST] Error stopping Cast stream:', error);
}
}
/**
* Stores the details of the currently playing local media.
* This is called from player.js whenever a channel starts playing locally.
* @param {string} streamUrl - The URL of the stream.
* @param {string} name - The name of the channel.
* @param {string} logo - The URL for the channel's logo.
* @param {string} originalUrl - The original stream URL (for stopping server stream).
* @param {string} profileId - The profile ID (for stopping server stream).
*/
export function setLocalPlayerState(streamUrl, name, logo, originalUrl = null, profileId = null) {
castState.localPlayerState.streamUrl = streamUrl;
castState.localPlayerState.name = name;
castState.localPlayerState.logo = logo;
castState.localPlayerState.originalUrl = originalUrl;
castState.localPlayerState.profileId = profileId;
console.log(`[CAST] Local player state updated: ${name}`);
}
/**
* Initializes the Google Cast API and sets up listeners.
* THIS IS NO LONGER CALLED DIRECTLY. It's wrapped in the __onGCastApiAvailable callback.
*/
function initializeCastApi() {
console.log('[CAST] Cast SDK is available. Initializing context...');
const castContext = cast.framework.CastContext.getInstance();
castContext.setOptions({
receiverApplicationId: APPLICATION_ID,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.TAB_AND_ORIGIN_SCOPED
});
castContext.addEventListener(
cast.framework.CastContextEventType.SESSION_STATE_CHANGED,
handleSessionStateChange
);
castState.player = new cast.framework.RemotePlayer();
castState.playerController = new cast.framework.RemotePlayerController(castState.player);
castState.playerController.addEventListener(
cast.framework.RemotePlayerEventType.IS_CONNECTED_CHANGED,
handleRemotePlayerConnectionChange
);
}
// --- FINAL FIX ---
// This is the official callback provided by the Google Cast SDK.
// It will be executed automatically by the SDK script once it has fully loaded and is ready.
// We wrap our entire initialization logic in here to prevent timing issues.
window['__onGCastApiAvailable'] = (isAvailable) => {
if (isAvailable) {
castState.isAvailable = true;
initializeCastApi();
} else {
console.warn('[CAST] Cast SDK is not available on this device.');
castState.isAvailable = false;
// Optionally hide the cast button if the SDK is not available at all
if (UIElements.castBtn) {
UIElements.castBtn.style.display = 'none';
}
}
};
/**
* Handles changes in the Cast session state.
* @param {chrome.cast.SessionStateEventData} event - The session state event.
*/
function handleSessionStateChange(event) {
console.log(`[CAST] Session state changed: ${event.sessionState}`);
const castContext = cast.framework.CastContext.getInstance();
castState.session = castContext.getCurrentSession(); // Update session reference
switch (event.sessionState) {
case cast.framework.SessionState.SESSION_STARTED:
case cast.framework.SessionState.SESSION_RESUMED:
castState.isCasting = true;
showNotification(`Casting to ${castState.session.getCastDevice().friendlyName}`, false, 4000);
// Auto-cast if local player is active and not already casting
if (castState.localPlayerState.streamUrl && !castState.currentMedia) {
console.log('[CAST] Automatically casting local content after session start.');
// CRITICAL FIX: Stop the server-side stream FIRST
const { originalUrl, profileId } = castState.localPlayerState;
if (originalUrl && profileId) {
console.log(`[CAST] Stopping server stream: ${originalUrl} with profile ${profileId}`);
stopStream(originalUrl, profileId).catch(err => {
console.error('[CAST] Error stopping server stream:', err);
});
}
// CRITICAL FIX: Stop the local player before casting
if (appState.player) {
console.log('[CAST] Stopping local player before casting.');
appState.player.destroy();
appState.player = null;
// Clear the video element
if (UIElements.videoElement) {
UIElements.videoElement.src = "";
UIElements.videoElement.removeAttribute('src');
UIElements.videoElement.load();
}
}
const { streamUrl, name, logo } = castState.localPlayerState;
// CRITICAL FIX: Convert relative URLs to absolute for Chromecast
const absoluteUrl = streamUrl.startsWith('http')
? streamUrl
: `${window.location.origin}${streamUrl}`;
loadMedia(absoluteUrl, name, logo);
}
break;
case cast.framework.SessionState.SESSION_ENDED:
console.log('[CAST] Session ended, stopping Cast stream on server.');
// Stop the Cast stream on the server
if (castState.currentCastStreamUrl) {
stopCastStream(castState.currentCastStreamUrl);
castState.currentCastStreamUrl = null;
}
castState.session = null;
castState.isCasting = false;
castState.currentMedia = null;
showNotification('Casting session ended.', false, 4000);
updatePlayerUI();
forceRefreshStream().catch(err => {
console.error('[CAST] Error refreshing stream after cast ended:', err);
});
break;
case cast.framework.SessionState.NO_SESSION:
castState.session = null;
castState.isCasting = false;
castState.currentMedia = null;
updatePlayerUI();
break;
}
}
/**
* Handles changes in the remote player's connection status and updates the UI.
*/
function handleRemotePlayerConnectionChange() {
updatePlayerUI();
}
/**
* Updates the local player modal UI based on the casting state.
*/
function updatePlayerUI() {
const videoElement = UIElements.videoElement;
const castStatusDiv = UIElements.castStatus;
const castBtn = UIElements.castBtn;
if (castState.isCasting && castState.player.isConnected) {
videoElement.classList.add('hidden');
castStatusDiv.classList.remove('hidden');
castStatusDiv.classList.add('flex');
UIElements.castStatusText.textContent = `Casting to ${castState.session.getCastDevice().friendlyName}`;
UIElements.castStatusChannel.textContent = castState.player.mediaInfo ? castState.player.mediaInfo.metadata.title : 'No media loaded.';
// Add class to our custom button to indicate connected state
if (castBtn) castBtn.classList.add('cast-connected');
} else {
videoElement.classList.remove('hidden');
castStatusDiv.classList.add('hidden');
castStatusDiv.classList.remove('flex');
// Remove connected state class
if (castBtn) castBtn.classList.remove('cast-connected');
}
}
/**
* Loads a media stream onto the connected Cast device.
* @param {string} url - The URL of the stream.
* @param {string} name - The name of the channel.
* @param {string} logo - The URL of the channel's logo.
*/
export async function loadMedia(url, name, logo) {
if (!castState.session) {
showNotification('Not connected to a Cast device.', true);
return;
}
console.log(`[CAST] Loading media: "${name}" from URL: ${url}`);
// ONLY modify URL to use cast profile if NOT already using it
// This ensures we switch to the active cast profile for Chromecast
const activeCastProfileId = guideState.settings?.activeCastProfileId || 'cast-default';
const userAgentId = guideState.settings.activeUserAgentId;
const activeStreamProfileId = guideState.settings?.activeStreamProfileId;
let castUrl = url;
const streamProfile = activeStreamProfileId
? (guideState.settings.streamProfiles || []).find(p => p.id === activeStreamProfileId)
: null;
const isRedirectProfile = streamProfile?.command === 'redirect';
if (isRedirectProfile) {
// Raw stream URL from Redirect profile - build proper stream URL with cast profile
castUrl = `/stream?url=${encodeURIComponent(url)}&profileId=${activeCastProfileId}&userAgentId=${userAgentId}`;
console.log(`[CAST] Built stream URL for redirect profile: ${castUrl}`);
} else {
// Already a /stream endpoint - ensure correct cast profile is set
if (url.includes(`profileId=${activeCastProfileId}`)) {
castUrl = url;
} else if (url.includes('profileId=')) {
// Replace existing profileId with active cast profile
castUrl = url.replace(/profileId=[^&]+/, `profileId=${activeCastProfileId}`);
console.log(`[CAST] Replaced profile with ${activeCastProfileId}`);
} else {
// Add cast profile to existing stream URL
const separator = url.includes('?') ? '&' : '?';
castUrl = `${url}${separator}profileId=${activeCastProfileId}`;
console.log(`[CAST] Added ${activeCastProfileId} profile`);
}
}
// Convert to absolute URL if needed
if (!castUrl.startsWith('http')) {
castUrl = `${window.location.origin}${castUrl}`;
}
// Generate and append cast authentication token
try {
console.log('[CAST] Requesting authentication token...');
const response = await fetch('/api/cast/generate-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ streamUrl: url })
});
if (!response.ok) {
throw new Error(`Token generation failed: ${response.status}`);
}
const { token } = await response.json();
const separator = castUrl.includes('?') ? '&' : '?';
castUrl = `${castUrl}${separator}castToken=${token}`;
console.log('[CAST] Authentication token added to URL');
} catch (error) {
console.error('[CAST] Failed to generate cast token:', error);
showNotification('Failed to generate cast authentication token', true);
return;
}
console.log(`[CAST] Cast URL (${castUrl}) ready for Chromecast`);
// Use video/mp4 instead of video/mp2t for Chromecast compatibility
const mediaInfo = new chrome.cast.media.MediaInfo(castUrl, 'video/mp4');
mediaInfo.streamType = chrome.cast.media.StreamType.LIVE;
mediaInfo.metadata = new chrome.cast.media.TvShowMediaMetadata();
mediaInfo.metadata.title = name;
if (logo) {
mediaInfo.metadata.images = [new chrome.cast.Image(logo)];
}
const request = new chrome.cast.media.LoadRequest(mediaInfo);
castState.session.loadMedia(request).then(
() => {
console.log('[CAST] Media loaded successfully.');
castState.currentMedia = castState.session.getMediaSession();
castState.currentCastStreamUrl = url; // Track for cleanup
updatePlayerUI();
},
(errorCode) => {
console.error('[CAST] Error loading media:', errorCode);
showNotification('Failed to load media on Cast device. Check console.', true);
}
);
}
/**
* Ends the entire Cast session, disconnecting from the device.
*/
export function endCastSession() {
if (castState.session) {
castState.session.endSession(true); // true to stop any playing media
}
}