-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrealtime_websocket_service.py
More file actions
593 lines (476 loc) · 22.7 KB
/
realtime_websocket_service.py
File metadata and controls
593 lines (476 loc) · 22.7 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
"""
Real-Time WebSocket Streaming Service for Ditto Talking Head
Based on the streaming documentation provided
"""
import asyncio
import base64
import json
import threading
import time
import numpy as np
import cv2
import queue
import librosa
import math
import tempfile
import os
import sys
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
import uvicorn
# Add the current directory to the path
sys.path.append('/workspace')
# Import Ditto components
from stream_pipeline_online import StreamSDK
class WebSocketStreamingWriter:
"""WebSocket video writer that streams frames in real-time"""
def __init__(self, websocket: WebSocket):
self.websocket = websocket
self.frame_queue = asyncio.Queue(maxsize=10) # Buffer for WebSocket
self.frames_streamed = 0
self.is_active = True
async def __call__(self, frame_rgb, fmt="rgb"):
"""Called by pipeline to write each frame - stream via WebSocket"""
if not self.is_active:
return
try:
# Convert to BGR for OpenCV
if fmt == "rgb":
frame_bgr = cv2.cvtColor(frame_rgb, cv2.COLOR_RGB2BGR)
else:
frame_bgr = frame_rgb
# Encode frame as JPEG
_, buffer = cv2.imencode('.jpg', frame_bgr, [cv2.IMWRITE_JPEG_QUALITY, 80])
frame_base64 = base64.b64encode(buffer).decode('utf-8')
# Create frame packet
frame_packet = {
"type": "frame",
"data": frame_base64,
"frame_number": self.frames_streamed,
"timestamp": time.time()
}
# Queue for WebSocket streaming
await self.frame_queue.put(frame_packet)
self.frames_streamed += 1
except Exception as e:
print(f"WebSocket frame streaming error: {e}")
def close(self):
"""Close the streaming writer"""
self.is_active = False
class LiveKitTalkingHeadService:
"""Real-time talking head service optimized for LiveKit integration"""
def __init__(self, cfg_pkl: str, data_root: str):
self.cfg_pkl = cfg_pkl
self.data_root = data_root
self.active_sessions = {}
async def create_session(self, session_id: str, source_path: str, websocket: WebSocket):
"""Create a new real-time streaming session"""
# Initialize SDK with LiveKit optimizations
sdk = StreamSDK(self.cfg_pkl, self.data_root)
# WebSocket streaming writer
websocket_writer = WebSocketStreamingWriter(websocket)
# LiveKit optimized setup
setup_kwargs = {
"online_mode": True,
"max_size": 1280, # Optimized for RTX 4090
"sampling_timesteps": 25, # Fast for real-time
"crop_scale": 2.3,
"smo_k_s": 5, # Fast smoothing
"smo_k_d": 2, # Minimal smoothing
"overlap_v2": 3, # Low latency
"emo": 3, # Professional appearance
"relative_d": True,
}
# Setup SDK with WebSocket output
sdk.setup(source_path, "/dev/null", **setup_kwargs) # Dummy output path
# Replace writer with WebSocket streaming writer
original_writer_worker = sdk._writer_worker
def streaming_writer_worker():
"""Custom writer worker for WebSocket streaming"""
while not sdk.stop_event.is_set():
try:
item = sdk.writer_queue.get(timeout=1)
except queue.Empty:
continue
if item is None:
break
res_frame_rgb = item
# Stream frame via WebSocket (async)
try:
loop = asyncio.get_event_loop()
loop.create_task(websocket_writer(res_frame_rgb, fmt="rgb"))
except:
# Fallback to sync if no async loop
pass
sdk.writer_pbar.update()
# Replace the writer worker
sdk._writer_worker = streaming_writer_worker
self.active_sessions[session_id] = {
"sdk": sdk,
"websocket_writer": websocket_writer,
"websocket": websocket,
"active": True,
"audio_buffer": np.array([], dtype=np.float32),
"chunk_size": 1024 # ~64ms at 16kHz
}
return sdk, websocket_writer
async def process_audio_chunk(self, session_id: str, audio_data: bytes):
"""Process real-time audio chunk for a session"""
if session_id not in self.active_sessions:
return
session = self.active_sessions[session_id]
if not session["active"]:
return
try:
# Convert bytes to numpy array (assuming float32 PCM)
audio_chunk = np.frombuffer(audio_data, dtype=np.float32)
# Add to buffer
session["audio_buffer"] = np.concatenate([session["audio_buffer"], audio_chunk])
# Process if we have enough audio
while len(session["audio_buffer"]) >= session["chunk_size"]:
# Extract chunk
chunk = session["audio_buffer"][:session["chunk_size"]]
session["audio_buffer"] = session["audio_buffer"][session["chunk_size"]:]
# Process chunk through Ditto pipeline (async)
sdk = session["sdk"]
# Run in thread to avoid blocking
def process_chunk():
sdk.run_chunk(chunk)
threading.Thread(target=process_chunk, daemon=True).start()
except Exception as e:
print(f"Audio processing error: {e}")
async def close_session(self, session_id: str):
"""Close a streaming session"""
if session_id in self.active_sessions:
session = self.active_sessions[session_id]
session["active"] = False
# Close SDK
session["sdk"].close()
session["websocket_writer"].close()
del self.active_sessions[session_id]
# FastAPI application
app = FastAPI(title="LiveKit Ditto TalkingHead Real-Time Streaming Service")
# Global service
service = None
def init_service(cfg_pkl: str, data_root: str):
global service
service = LiveKitTalkingHeadService(cfg_pkl, data_root)
@app.get("/")
async def get_livekit_demo_page():
"""Serve LiveKit-optimized demo HTML page"""
return HTMLResponse(content="""
<!DOCTYPE html>
<html>
<head>
<title>LiveKit Ditto TalkingHead Real-Time Streaming</title>
<style>
body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
.container { display: flex; gap: 20px; }
.controls { flex: 1; }
.output { flex: 1; }
video { width: 100%; max-width: 512px; border: 2px solid #ddd; border-radius: 8px; }
button { padding: 10px 20px; margin: 5px; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; }
.start { background-color: #28a745; color: white; }
.stop { background-color: #dc3545; color: white; }
.status { padding: 15px; background-color: #f8f9fa; border-radius: 5px; margin-bottom: 20px; }
.settings { background-color: #fff; border: 1px solid #ddd; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
input[type="range"] { width: 100%; }
.metric { display: inline-block; margin-right: 20px; font-weight: bold; }
</style>
</head>
<body>
<h1>🚀 LiveKit Ditto Real-Time Avatar Streaming</h1>
<div class="status" id="status">
<strong>Status:</strong> <span id="connectionStatus">Disconnected</span><br>
<strong>Frames Processed:</strong> <span id="frameCount">0</span><br>
<strong>Latency:</strong> <span id="latency">--</span>ms<br>
<strong>FPS:</strong> <span id="fps">--</span>
</div>
<div class="container">
<div class="controls">
<div class="settings">
<h3>🎛️ LiveKit Settings</h3>
<label>Avatar Image:</label><br>
<input type="file" id="imageUpload" accept="image/*" style="margin-bottom: 10px;"><br>
<label>Resolution: <span id="resolutionValue">1280</span>px</label><br>
<input type="range" id="resolution" min="512" max="1920" value="1280" step="128"><br>
<label>Quality Steps: <span id="qualityValue">25</span></label><br>
<input type="range" id="quality" min="15" max="50" value="25" step="5"><br>
<label>Mouth Movement: <span id="mouthValue">0.8</span></label><br>
<input type="range" id="mouth" min="0.3" max="1.5" value="0.8" step="0.1"><br>
<label>Head Movement: <span id="headValue">0.6</span></label><br>
<input type="range" id="head" min="0.2" max="1.2" value="0.6" step="0.1"><br>
</div>
<button id="startBtn" class="start">🎤 Start Live Avatar</button>
<button id="stopBtn" class="stop">⏹️ Stop Streaming</button>
</div>
<div class="output">
<h3>📹 Live Avatar Output</h3>
<video id="output" autoplay muted></video>
<div id="errorMsg" style="color: red; margin-top: 10px;"></div>
</div>
</div>
<div style="margin-top: 30px;">
<h3>💡 LiveKit Integration Guide</h3>
<ul>
<li><strong>Real-time Processing:</strong> ~40-80ms latency per frame</li>
<li><strong>RTX 4090 Optimized:</strong> 1280px resolution, 25 sampling steps</li>
<li><strong>Professional Avatars:</strong> Reduced movement for business calls</li>
<li><strong>Live Audio Input:</strong> Microphone → Real-time avatar generation</li>
<li><strong>WebSocket Streaming:</strong> Frame-by-frame output via WebSocket</li>
</ul>
</div>
<script>
let ws = null;
let mediaRecorder = null;
let isStreaming = false;
let frameCount = 0;
let lastFrameTime = 0;
let avatarImage = null;
const video = document.getElementById('output');
const statusEl = document.getElementById('connectionStatus');
const frameCountEl = document.getElementById('frameCount');
const latencyEl = document.getElementById('latency');
const fpsEl = document.getElementById('fps');
const errorEl = document.getElementById('errorMsg');
// Controls
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const imageUpload = document.getElementById('imageUpload');
// Settings
const resolutionSlider = document.getElementById('resolution');
const qualitySlider = document.getElementById('quality');
const mouthSlider = document.getElementById('mouth');
const headSlider = document.getElementById('head');
// Update setting displays
resolutionSlider.oninput = () => document.getElementById('resolutionValue').textContent = resolutionSlider.value;
qualitySlider.oninput = () => document.getElementById('qualityValue').textContent = qualitySlider.value;
mouthSlider.oninput = () => document.getElementById('mouthValue').textContent = mouthSlider.value;
headSlider.oninput = () => document.getElementById('headValue').textContent = headSlider.value;
startBtn.onclick = startLiveAvatar;
stopBtn.onclick = stopStreaming;
// Handle image upload
imageUpload.onchange = (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
avatarImage = e.target.result.split(',')[1]; // Remove data:image/... prefix
};
reader.readAsDataURL(file);
}
};
function updateStatus(status, error = '') {
statusEl.textContent = status;
errorEl.textContent = error;
}
async function startLiveAvatar() {
if (isStreaming) return;
if (!avatarImage) {
updateStatus('Error', 'Please upload an avatar image first');
return;
}
try {
updateStatus('Requesting microphone access...');
// Get microphone access
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
sampleRate: 16000,
channelCount: 1,
echoCancellation: true,
noiseSuppression: true
}
});
// Setup WebSocket
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const host = window.location.host;
const wsUrl = `${protocol}//${host}/ws`;
updateStatus('Connecting to LiveKit avatar service...');
ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log('WebSocket connected');
updateStatus('Initializing real-time avatar...');
// Send start command with avatar image and settings
ws.send(JSON.stringify({
type: 'start_livekit_avatar',
avatar_image_base64: avatarImage,
settings: {
max_size: parseInt(resolutionSlider.value),
sampling_timesteps: parseInt(qualitySlider.value),
mouth_amplitude: parseFloat(mouthSlider.value),
head_amplitude: parseFloat(headSlider.value)
}
}));
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
updateStatus('Connection failed', 'Failed to connect to avatar service');
};
ws.onclose = (event) => {
console.log('WebSocket closed:', event);
updateStatus('Disconnected');
isStreaming = false;
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'ready') {
updateStatus('✅ Live avatar active - speak into microphone');
isStreaming = true;
} else if (data.type === 'frame') {
displayFrame(data.data);
updateMetrics();
} else if (data.type === 'error') {
updateStatus('Error', data.message);
}
};
// Setup real-time audio streaming
const audioContext = new AudioContext({ sampleRate: 16000 });
const source = audioContext.createMediaStreamSource(stream);
const processor = audioContext.createScriptProcessor(1024, 1, 1);
processor.onaudioprocess = (event) => {
if (isStreaming && ws.readyState === WebSocket.OPEN) {
const inputBuffer = event.inputBuffer;
const inputData = inputBuffer.getChannelData(0);
// Convert to bytes for sending
const buffer = new Float32Array(inputData);
ws.send(buffer.buffer);
}
};
source.connect(processor);
processor.connect(audioContext.destination);
} catch (error) {
console.error('Error starting live avatar:', error);
updateStatus('Error', `Failed to start: ${error.message}`);
}
}
function displayFrame(base64Data) {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.toBlob(blob => {
const url = URL.createObjectURL(blob);
video.src = url;
// Cleanup previous URL
setTimeout(() => URL.revokeObjectURL(url), 1000);
}, 'image/jpeg');
};
img.src = 'data:image/jpeg;base64,' + base64Data;
}
function updateMetrics() {
frameCount++;
frameCountEl.textContent = frameCount;
const now = Date.now();
if (lastFrameTime > 0) {
const frameDuration = now - lastFrameTime;
latencyEl.textContent = frameDuration;
fpsEl.textContent = Math.round(1000 / frameDuration);
}
lastFrameTime = now;
}
function stopStreaming() {
if (!isStreaming) return;
updateStatus('Stopping...');
if (ws) {
ws.close();
}
isStreaming = false;
frameCount = 0;
updateStatus('Disconnected');
}
</script>
</body>
</html>
""")
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""WebSocket endpoint for real-time LiveKit avatar streaming"""
await websocket.accept()
session_id = f"livekit_{time.time()}"
try:
# Wait for start command
start_data = await websocket.receive_text()
start_msg = json.loads(start_data)
if start_msg["type"] == "start_livekit_avatar":
# Get avatar image
avatar_image_base64 = start_msg["avatar_image_base64"]
settings = start_msg.get("settings", {})
# Save avatar image temporarily
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
tmp_file.write(base64.b64decode(avatar_image_base64))
avatar_path = tmp_file.name
# Create real-time streaming session
sdk, websocket_writer = await service.create_session(session_id, avatar_path, websocket)
# Apply settings to session
session = service.active_sessions[session_id]
if settings:
# Update settings (would need to be applied to SDK setup)
pass
# Start frame streaming task
frame_task = asyncio.create_task(stream_frames_to_websocket(session_id, websocket))
# Send ready signal
await websocket.send_text(json.dumps({
"type": "ready",
"message": "LiveKit avatar ready for real-time streaming"
}))
# Process incoming audio chunks
while session_id in service.active_sessions:
try:
# Receive audio data
audio_bytes = await websocket.receive_bytes()
# Process audio chunk
await service.process_audio_chunk(session_id, audio_bytes)
except WebSocketDisconnect:
break
except Exception as e:
print(f"WebSocket error: {e}")
await websocket.send_text(json.dumps({
"type": "error",
"message": str(e)
}))
finally:
# Cleanup
await service.close_session(session_id)
# Clean up temp file
try:
os.unlink(avatar_path)
except:
pass
async def stream_frames_to_websocket(session_id: str, websocket: WebSocket):
"""Stream generated frames to WebSocket client"""
if session_id not in service.active_sessions:
return
session = service.active_sessions[session_id]
websocket_writer = session["websocket_writer"]
try:
while session["active"]:
# Get frame from queue
frame_data = await websocket_writer.frame_queue.get()
if not session["active"]:
break
# Send frame to client
await websocket.send_text(json.dumps(frame_data))
except Exception as e:
print(f"Frame streaming to WebSocket error: {e}")
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"service": "livekit-ditto-realtime-streaming",
"active_sessions": len(service.active_sessions) if service else 0
}
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print("Usage: python realtime_websocket_service.py <cfg_pkl> <data_root>")
sys.exit(1)
cfg_pkl = sys.argv[1]
data_root = sys.argv[2]
# Initialize service
init_service(cfg_pkl, data_root)
# Run server
uvicorn.run(app, host="0.0.0.0", port=8000)