Skip to content

Build and run agents you can see, understand and trust.

feat(realtime): add video recording in realtime agent example and typo fix in realtime event (#1225)

qbc14be0ab60906
diff --git a/examples/agent/realtime_voice_agent/README.md b/examples/agent/realtime_voice_agent/README.md
index 0e7fd5d78..c28af1053 100644
--- a/examples/agent/realtime_voice_agent/README.md
+++ b/examples/agent/realtime_voice_agent/README.md
@@ -39,8 +39,10 @@ http://localhost:8000
You will see a web interface with:
- Configuration panel (instructions and user name)
- Voice control buttons (Start Recording, Stop Recording, Disconnect)
+- Video recording button (Start Video Recording)
- Text input field
- Message display area
+- Video preview area (when video recording is active)
### 3. Start Conversation
@@ -57,6 +59,13 @@ You will see a web interface with:
3. **Stop Recording**:
- Click "⏹️ Stop Recording" to pause voice input
+4. **Video Recording** (Optional):
+ - Click the "📹 Start Video Recording" button to start video recording
+ - Allow camera access when prompted by your browser
+ - The system will automatically capture and send video frames to the server at 1 frame per second (1 fps)
+ - A video preview will be displayed while recording
+ - Click "🔴 Stop Video Recording" to stop recording
+ - **Note**: Video recording requires an active voice chat session. Please start voice chat first before starting video recording.
## Switching Models
diff --git a/examples/agent/realtime_voice_agent/chatbot.html b/examples/agent/realtime_voice_agent/chatbot.html
index 1b7d8f92f..385de35f4 100644
--- a/examples/agent/realtime_voice_agent/chatbot.html
+++ b/examples/agent/realtime_voice_agent/chatbot.html
@@ -139,13 +139,15 @@
border-color: hsl(221.2, 83.2%, 45%);
}
- button.btn-primary.recording {
+ button.btn-primary.recording,
+ button.btn-primary.recording-video {
background: hsl(0, 84.2%, 60.2%);
border-color: hsl(0, 84.2%, 60.2%);
animation: pulse 1.5s ease-in-out infinite;
}
- button.btn-primary.recording:hover:not(:disabled) {
+ button.btn-primary.recording:hover:not(:disabled),
+ button.btn-primary.recording-video:hover:not(:disabled) {
background: hsl(0, 84.2%, 50%);
border-color: hsl(0, 84.2%, 50%);
}
@@ -194,11 +196,12 @@
flex-shrink: 0;
}
- .controls #voiceBtn {
+ .controls #voiceBtn,
+ .controls #videoBtn {
flex: 1;
}
- .controls button:not(#voiceBtn) {
+ .controls button:not(#voiceBtn):not(#videoBtn) {
width: 140px;
flex-shrink: 0;
}
@@ -425,6 +428,28 @@
border-color: hsl(214.3, 31.8%, 91.4%);
opacity: 0.6;
}
+
+ #videoPreview {
+ width: 100%;
+ max-width: 640px;
+ border-radius: 0.5rem;
+ border: 1px solid hsl(214.3, 31.8%, 91.4%);
+ background: hsl(0, 0%, 0%);
+ display: none;
+ margin: 1rem 0;
+ flex-shrink: 0;
+ }
+
+ #videoPreview.active {
+ display: block;
+ }
+
+ .video-container {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ flex-shrink: 0;
+ }
</style>
</head>
<body>
@@ -515,9 +540,14 @@
<div class="controls">
<button id="voiceBtn" class="btn-primary" onclick="toggleVoice()">🎤 Start Voice Chat</button>
+ <button id="videoBtn" class="btn-primary" onclick="toggleVideo()">📹 Start Video Recording</button>
<button class="btn-secondary" onclick="disconnect()">❌ Disconnect</button>
</div>
+ <div class="video-container">
+ <video id="videoPreview" autoplay muted playsinline></video>
+ </div>
+
<div class="text-input-container">
<input type="text" id="textInput" placeholder="Type your message here..." />
<button id="sendBtn" class="btn-primary" onclick="sendTextMessage()" disabled>📤 Send</button>
@@ -531,7 +561,12 @@
let audioContext = null; // For recording, 16kHz
let playbackAudioContext = null; // For playback, 24kHz
let mediaStream = null;
+ let videoStream = null; // For video recording
+ let videoFrameInterval = null; // Interval for sending video frames
+ let videoCanvas = null; // Canvas for capturing video frames
+ let videoCanvasCtx = null; // Canvas context
let isRecording = false;
+ let isRecordingVideo = false;
let isPlaying = false;
let audioPlaybackNode = null;
let audioPlaybackQueue = []; // Store decoded Float32Array
@@ -825,6 +860,7 @@
ws.onclose = function() {
addMessage("System", "❌ Disconnected");
stopVoice();
+ stopVideoRecording(); // Also stop video recording on disconnect
sessionCreated = false; // Reset session state
updateSendButtonState();
};
@@ -1125,8 +1161,168 @@
}
+ /**
+ * Toggles video recording on and off by calling the
+ * start or stop functions.
+ */
+ async function toggleVideo() {
+ if (!isRecordingVideo) {
+ await startVideoRecording();
+ } else {
+ stopVideoRecording();
+ }
+ }
+
+ /**
+ * Starts video recording by requesting camera access, displaying a
+ * live preview, and initiating frame capture to the server at 1 fps.
+ * Requires an active voice chat session (WebSocket connected and
+ * session created).
+ */
+ async function startVideoRecording() {
+ // Prevent duplicate starts
+ if (isRecordingVideo) {
+ return;
+ }
+
+ try {
+ const videoBtn = document.getElementById("videoBtn");
+ const videoPreview = document.getElementById("videoPreview");
+
+ // Check if WebSocket is connected
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
+ showError("⚠️ WebSocket is not connected! Please wait for connection.");
+ return;
+ }
+
+ // Check if session is created
+ if (!sessionCreated) {
+ showError("⚠️ Session not created yet! Please start voice chat first.");
+ return;
+ }
+
+ // Request video stream only (audio is already captured by voice chat)
+ videoStream = await navigator.mediaDevices.getUserMedia({
+ video: {
+ width: { ideal: 1280 },
+ height: { ideal: 720 },
+ facingMode: "user"
+ },
+ audio: false
+ });
+
+ // Show video preview
+ videoPreview.srcObject = videoStream;
+ videoPreview.classList.add("active");
+
+ // Start sending video frames to server at 1 fps
+ startSendingVideoFrames();
+
+ isRecordingVideo = true;
+ videoBtn.classList.add("recording-video");
+ videoBtn.innerText = "🔴 Stop Video Recording";
+ addMessage("System", "📹 Video recording started, sending frames to server at 1 fps...");
+
+ } catch (err) {
+ console.error("Failed to start video recording:", err);
+ showError("⚠️ Unable to access camera: " + err.message);
+ addMessage("System", "⚠️ Unable to access camera: " + err.message);
+ }
+ }
+
+ /**
+ * Captures video frames from the preview element at 1 fps and sends
+ * them to the server as base64-encoded JPEG images via WebSocket
+ * using the client_image_append event.
+ */
+ function startSendingVideoFrames() {
+ if (videoFrameInterval) {
+ clearInterval(videoFrameInterval);
+ }
+
+ const videoPreview = document.getElementById("videoPreview");
+
+ // Create canvas if needed
+ if (!videoCanvas) {
+ videoCanvas = document.createElement('canvas');
+ videoCanvasCtx = videoCanvas.getContext('2d');
+ }
+
+ // Send frame every second (1 fps)
+ videoFrameInterval = setInterval(function() {
+ if (!isRecordingVideo || !videoStream || !videoPreview) {
+ return;
+ }
+
+ try {
+ // Check if video is ready
+ if (videoPreview.readyState < 2 || !videoPreview.videoWidth || !videoPreview.videoHeight) {
+ return;
+ }
+
+ // Set canvas size to match video
+ const videoWidth = videoPreview.videoWidth;
+ const videoHeight = videoPreview.videoHeight;
+ if (videoCanvas.width !== videoWidth || videoCanvas.height !== videoHeight) {
+ videoCanvas.width = videoWidth;
+ videoCanvas.height = videoHeight;
+ }
+
+ // Capture frame and convert to base64 JPEG
+ videoCanvasCtx.drawImage(videoPreview, 0, 0, videoWidth, videoHeight);
+ const base64Data = videoCanvas.toDataURL('image/jpeg', 0.8).split(',')[1];
+
+ // Send to server
+ if (ws && ws.readyState === WebSocket.OPEN && sessionCreated) {
+ ws.send(JSON.stringify({
+ type: "client_image_append",
+ session_id: sessionId,
+ data: base64Data,
+ format: {
+ type: "image/jpeg",
+ mime_type: "image/jpeg"
+ }
+ }));
+ }
+ } catch (err) {
+ console.error("Failed to capture video frame:", err);
+ }
+ }, 1000);
+ }
+
+ /**
+ * Stops video recording by clearing the frame capture interval,
+ * releasing the camera stream, and resetting the UI state.
+ */
+ function stopVideoRecording() {
+ if (videoFrameInterval) {
+ clearInterval(videoFrameInterval);
+ videoFrameInterval = null;
+ }
+
+ if (videoStream) {
+ videoStream.getTracks().forEach(track => track.stop());
+ videoStream = null;
+ }
+
+ const videoPreview = document.getElementById("videoPreview");
+ if (videoPreview) {
+ videoPreview.srcObject = null;
+ videoPreview.classList.remove("active");
+ }
+
+ const videoBtn = document.getElementById("videoBtn");
+ isRecordingVideo = false;
+ if (videoBtn) {
+ videoBtn.classList.remove("recording-video");
+ videoBtn.innerText = "📹 Start Video Recording";
+ }
+ addMessage("System", "⏹️ Video recording stopped");
+ }
+
function disconnect() {
stopVoice();
+ stopVideoRecording();
stopAudioPlayback();
if (ws) {
ws.close();
diff --git a/src/agentscope/realtime/_events/_client_event.py b/src/agentscope/realtime/_events/_client_event.py
index 9487927e0..0f89ef13b 100644
--- a/src/agentscope/realtime/_events/_client_event.py
+++ b/src/agentscope/realtime/_events/_client_event.py
@@ -94,7 +94,7 @@ class ClientEvents:
session_id: str
"""The session ID."""
- data: str
+ image: str
"""The image data, encoded as base64 string."""
format: dict
diff --git a/tests/realtime_event_test.py b/tests/realtime_event_test.py
index c115116de..6658c40c7 100644
--- a/tests/realtime_event_test.py
+++ b/tests/realtime_event_test.py
@@ -409,7 +409,7 @@ class TestClientEventsFromJson(IsolatedAsyncioTestCase):
json_data = {
"type": "client_image_append",
"session_id": "session_001",
- "data": "base64_image_data",
+ "image": "base64_image_data",
"format": {"mime_type": "image/png"},
}
@@ -418,7 +418,7 @@ class TestClientEventsFromJson(IsolatedAsyncioTestCase):
self.assertIsInstance(event, ClientEvents.ClientImageAppendEvent)
self.assertEqual(event.type, "client_image_append")
self.assertEqual(event.session_id, "session_001")
- self.assertEqual(event.data, "base64_image_data")
+ self.assertEqual(event.image, "base64_image_data")
self.assertEqual(event.format, {"mime_type": "image/png"})
async def test_client_text_append_event(self) -> None: