[Bugfix][MiniCPM-o] Bind omni-duplex video frames to the unit they close (#6404)

Signed-off-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
amy-why-3459 authored and GitHub committed 2026-08-28 16:58:20 +08:00
1 parent 13173d6bbb
commit da94c3ebe1
17 files changed
+1530 -144

No files matched your search

+78 -10
View File
@@ -11,7 +11,7 @@ import json
import math
import time
import wave
from collections.abc import Callable
from collections.abc import Callable, Iterator, Sequence
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@@ -28,6 +28,20 @@ except ImportError as exc: # pragma: no cover - example dependency
PCM16_SAMPLE_RATE = 16_000
PCM16_BYTES_PER_SAMPLE = 2
# Server-side model unit boundaries, in cumulative appended audio.
# Stage0 configures the streaming mel processor with first_chunk_ms=1035 and
# chunk_ms=1000; the processor aligns the first chunk down to a hop_length (160
# samples) multiple, so unit 0 closes at 16480 samples and every later unit
# closes 16000 samples after it. Camera frames must ride the append that closes
# a unit, otherwise Stage0 cannot bind them to that unit's audio.
DUPLEX_FIRST_UNIT_MS = 1030
DUPLEX_UNIT_MS = 1000
def duplex_unit_boundary_ms(unit_index: int) -> int:
"""Cumulative appended audio, in ms, that closes model unit ``unit_index``."""
return DUPLEX_FIRST_UNIT_MS + max(0, int(unit_index)) * DUPLEX_UNIT_MS
def _rounded_ms(value: float) -> float:
return round(float(value), 3)
@@ -561,24 +575,71 @@ class RealtimeDuplexClient:
*,
chunk_ms: int = 200,
realtime: bool = True,
) -> None:
video_frames: Sequence[str] | None = None,
stacked_video_frames: Sequence[str | None] | None = None,
) -> int:
"""Append PCM16 audio, optionally interleaving omni camera frames.
``video_frames`` holds base64 JPEG/PNG frames in capture order, one per
second of the clip. Frame ``k`` rides the append that closes model unit
``k`` (see ``duplex_unit_boundary_ms``), which reproduces the official
``streaming_prefill(audio_waveform=<1 s>, frame_list=[frame])`` pairing:
a second of audio and the picture captured during it enter the same
unit. Sending on whole-second boundaries instead would strand frame 0 on
an append that cannot close a unit yet, and shift every later frame one
unit ahead of its audio.
``stacked_video_frames`` is the optional parallel track of composites
built by ``video_stacking.concat_frames``: entry ``k`` tiles the
sub-frames captured *inside* unit ``k``, and rides the same append right
after the base frame, giving the official ``frame_list=[base,
composite]``. The audio is untouched — a unit stays one second however
many sub-frames the composite carries. ``None`` entries send the base
frame alone.
Returns the number of base frames actually sent (a clip shorter than the
frame list leaves the tail unsent).
"""
chunk_bytes = max(
PCM16_SAMPLE_RATE * PCM16_BYTES_PER_SAMPLE * chunk_ms // 1000,
PCM16_BYTES_PER_SAMPLE,
)
await self.stream_av_units(
((pcm16[offset : offset + chunk_bytes], None) for offset in range(0, len(pcm16), chunk_bytes)),
realtime=realtime,
)
frames = list(video_frames or [])
stacked = list(stacked_video_frames or [])
def units() -> Iterator[tuple[bytes, list[str] | None]]:
audio_end_ms = 0
frames_sent = 0
for offset in range(0, len(pcm16), chunk_bytes):
chunk = pcm16[offset : offset + chunk_bytes]
audio_end_ms += len(chunk) * 1000 // (PCM16_SAMPLE_RATE * PCM16_BYTES_PER_SAMPLE)
if not frames or audio_end_ms < duplex_unit_boundary_ms(frames_sent):
yield chunk, None
continue
# A video advances one frame per unit and holds its last frame
# when the audio outlives the clip; a still image is a
# one-element list and therefore repeats.
index = min(frames_sent, len(frames) - 1)
composite = stacked[index] if index < len(stacked) else None
frames_sent += 1
yield chunk, [frames[index]] if composite is None else [frames[index], composite]
return await self.stream_av_units(units(), realtime=realtime)
async def stream_av_units(
self,
units: Any,
*,
realtime: bool = True,
) -> None:
"""Stream PCM16 units, optionally attaching a JPEG to each unit."""
) -> int:
"""Stream PCM16 units, optionally attaching camera frames to each unit.
A unit's frame slot takes a single JPEG/PNG (raw bytes or base64) or a
sequence of them, which the Realtime wire caps at two per append.
Returns the number of appends that carried at least one frame.
"""
audio_end_ms = 0
frames_sent = 0
for chunk, frame in units:
if not chunk:
continue
@@ -593,11 +654,18 @@ class RealtimeDuplexClient:
"duration_ms": duration_ms,
"audio_end_ms": audio_end_ms,
}
if frame is not None:
event["video_frames"] = [base64.b64encode(frame).decode("ascii") if isinstance(frame, bytes) else frame]
encoded_frames = [
base64.b64encode(item).decode("ascii") if isinstance(item, bytes | bytearray) else item
for item in (frame if isinstance(frame, list | tuple) else [frame])
if item is not None
]
if encoded_frames:
event["video_frames"] = encoded_frames
frames_sent += 1
await self.send(event)
if realtime:
await asyncio.sleep(duration_ms / 1000)
return frames_sent
async def commit(self) -> None:
await self.send({"type": "input_audio_buffer.commit", "final": True})