-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_player.html
More file actions
341 lines (302 loc) · 12.6 KB
/
Copy pathweb_player.html
File metadata and controls
341 lines (302 loc) · 12.6 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
335
336
337
338
339
340
341
<!DOCTYPE html>
<!--
GhostStream Web Player Example
HOW TO USE:
1. Start GhostStream: python -m ghoststream --server-only
2. Serve this file via HTTP (don't open directly from filesystem):
cd examples
python -m http.server 8080
3. Open http://localhost:8080/web_player.html in your browser
NOTE: Opening this file directly (file://) won't work due to browser
security restrictions (CORS). You must serve it over HTTP.
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GhostStream Web Player Example</title>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #1a1a2e;
color: #eee;
min-height: 100vh;
padding: 20px;
}
.container { max-width: 900px; margin: 0 auto; }
h1 { margin-bottom: 20px; color: #00d4ff; }
.card {
background: #16213e;
border-radius: 12px;
padding: 20px;
margin-bottom: 20px;
}
label { display: block; margin-bottom: 8px; font-weight: 500; }
input, select, button {
width: 100%;
padding: 12px;
border: none;
border-radius: 8px;
font-size: 14px;
margin-bottom: 12px;
}
input, select { background: #0f3460; color: #fff; }
button {
background: #00d4ff;
color: #000;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
button:hover { background: #00a8cc; }
button:disabled { background: #555; cursor: not-allowed; }
.btn-danger { background: #e94560; color: #fff; }
.btn-danger:hover { background: #c73e54; }
video {
width: 100%;
border-radius: 12px;
background: #000;
}
.status {
padding: 12px;
border-radius: 8px;
margin-top: 12px;
font-family: monospace;
font-size: 13px;
}
.status.info { background: #0f3460; }
.status.success { background: #1b4332; }
.status.error { background: #5c1a1a; }
.progress-bar {
height: 8px;
background: #0f3460;
border-radius: 4px;
overflow: hidden;
margin: 12px 0;
}
.progress-bar .fill {
height: 100%;
background: #00d4ff;
transition: width 0.3s;
}
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }
.hidden { display: none; }
</style>
</head>
<body>
<div class="container">
<h1>🎬 GhostStream Web Player</h1>
<!-- Config -->
<div class="card">
<label>GhostStream Server</label>
<input type="text" id="serverUrl" value="http://localhost:8765" placeholder="http://localhost:8765">
<button onclick="checkHealth()">Check Connection</button>
<div id="healthStatus" class="status info hidden"></div>
</div>
<!-- Transcode Form -->
<div class="card">
<label>Video Source URL</label>
<input type="text" id="sourceUrl" placeholder="https://example.com/video.mp4">
<div class="row">
<div>
<label>Mode</label>
<select id="mode">
<option value="stream">HLS Stream (single quality)</option>
<option value="abr">ABR (multiple qualities)</option>
</select>
</div>
<div>
<label>Resolution</label>
<select id="resolution">
<option value="1080p">1080p</option>
<option value="720p" selected>720p</option>
<option value="480p">480p</option>
<option value="original">Original</option>
</select>
</div>
</div>
<button id="startBtn" onclick="startTranscode()">▶ Start Transcoding</button>
</div>
<!-- Progress -->
<div id="progressCard" class="card hidden">
<label>Transcoding Progress</label>
<div class="progress-bar"><div class="fill" id="progressFill" style="width: 0%"></div></div>
<div id="progressText" class="status info">Starting...</div>
<button class="btn-danger" onclick="cancelJob()">✕ Cancel</button>
</div>
<!-- Player -->
<div id="playerCard" class="card hidden">
<video id="videoPlayer" controls></video>
<div class="status success" id="streamInfo"></div>
<button class="btn-danger" onclick="stopPlayback()">■ Stop & Cleanup</button>
</div>
</div>
<script>
let currentJobId = null;
let currentControlToken = null;
let hls = null;
let pollInterval = null;
function getServer() {
return document.getElementById('serverUrl').value.replace(/\/$/, '');
}
function controlHeaders() {
return currentControlToken
? { 'X-GhostStream-Control-Token': currentControlToken }
: {};
}
function normalizeStreamUrl(streamUrl) {
const stream = new URL(streamUrl);
const server = new URL(getServer());
stream.protocol = server.protocol;
stream.host = server.host;
return stream.toString();
}
async function checkHealth() {
const statusEl = document.getElementById('healthStatus');
statusEl.classList.remove('hidden', 'success', 'error');
statusEl.classList.add('info');
statusEl.textContent = 'Checking...';
try {
const resp = await fetch(`${getServer()}/api/health`);
const data = await resp.json();
statusEl.classList.remove('info');
statusEl.classList.add('success');
statusEl.textContent = `✅ Connected - v${data.version} | Active: ${data.current_jobs} jobs`;
} catch (e) {
statusEl.classList.remove('info');
statusEl.classList.add('error');
statusEl.textContent = `❌ Cannot connect: ${e.message}`;
}
}
async function startTranscode() {
const source = document.getElementById('sourceUrl').value;
if (!source) {
alert('Enter a video source URL');
return;
}
const mode = document.getElementById('mode').value;
const resolution = document.getElementById('resolution').value;
document.getElementById('startBtn').disabled = true;
document.getElementById('progressCard').classList.remove('hidden');
document.getElementById('playerCard').classList.add('hidden');
currentJobId = null;
currentControlToken = null;
try {
const resp = await fetch(`${getServer()}/api/transcode/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
source,
mode,
output: {
resolution: mode === 'abr' ? 'original' : resolution,
video_codec: 'h264'
}
})
});
if (!resp.ok) {
throw new Error(`Start failed (${resp.status})`);
}
const job = await resp.json();
currentJobId = job.job_id;
currentControlToken = job.control_token || null;
document.getElementById('progressText').textContent = `Job: ${currentJobId}`;
// Poll for status
pollInterval = setInterval(pollStatus, 1000);
} catch (e) {
document.getElementById('progressText').textContent = `❌ Error: ${e.message}`;
document.getElementById('startBtn').disabled = false;
}
}
async function pollStatus() {
if (!currentJobId) return;
try {
const resp = await fetch(
`${getServer()}/api/transcode/${currentJobId}/status`,
{ headers: controlHeaders() }
);
if (!resp.ok) {
throw new Error(`Status failed (${resp.status})`);
}
const status = await resp.json();
const progress = status.progress || 0;
document.getElementById('progressFill').style.width = `${progress}%`;
document.getElementById('progressText').textContent =
`${status.status.toUpperCase()} - ${progress.toFixed(1)}% | HW: ${status.hw_accel_used || 'pending'}`;
if (status.status === 'ready') {
clearInterval(pollInterval);
playStream(status.stream_url);
} else if (status.status === 'processing' && status.stream_url) {
clearInterval(pollInterval);
playStream(status.stream_url);
} else if (status.status === 'error' || status.status === 'cancelled') {
clearInterval(pollInterval);
document.getElementById('progressText').textContent = `❌ ${status.status}: ${status.error_message || 'Job ended'}`;
document.getElementById('startBtn').disabled = false;
}
} catch (e) {
clearInterval(pollInterval);
document.getElementById('progressText').textContent = `❌ ${e.message}`;
document.getElementById('startBtn').disabled = false;
}
}
function playStream(streamUrl) {
document.getElementById('progressCard').classList.add('hidden');
document.getElementById('playerCard').classList.remove('hidden');
const localStreamUrl = normalizeStreamUrl(streamUrl);
document.getElementById('streamInfo').textContent = `🎬 Playing: ${localStreamUrl}`;
const video = document.getElementById('videoPlayer');
if (Hls.isSupported()) {
hls = new Hls();
hls.loadSource(localStreamUrl);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, () => video.play());
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// Safari native HLS
video.src = localStreamUrl;
video.play();
} else {
document.getElementById('streamInfo').textContent = '❌ HLS not supported in this browser';
}
}
async function cancelJob() {
if (!currentJobId) return;
clearInterval(pollInterval);
try {
await fetch(`${getServer()}/api/transcode/${currentJobId}/cancel`, {
method: 'POST',
headers: controlHeaders(),
});
} catch (e) {}
document.getElementById('progressCard').classList.add('hidden');
document.getElementById('startBtn').disabled = false;
currentJobId = null;
currentControlToken = null;
}
async function stopPlayback() {
if (hls) {
hls.destroy();
hls = null;
}
document.getElementById('videoPlayer').src = '';
document.getElementById('playerCard').classList.add('hidden');
if (currentJobId) {
try {
await fetch(`${getServer()}/api/transcode/${currentJobId}`, {
method: 'DELETE',
headers: controlHeaders(),
});
} catch (e) {}
currentJobId = null;
currentControlToken = null;
}
document.getElementById('startBtn').disabled = false;
}
// Check health on load
checkHealth();
</script>
</body>
</html>