From 551004998ea5bc034535404c6d4743bc0ecd4c87 Mon Sep 17 00:00:00 2001 From: Faris Rahman Date: Mon, 26 Oct 2026 13:04:13 +0100 Subject: [PATCH] feat(stv): expose speech progress for reliable turn completion Direct WebRTC callers need published speech duration to distinguish a pause from a completed streaming turn without relying on metadata arrival timing. Expose cumulative original speech progress and document that it does not acknowledge playback at the listener. --- CHANGELOG.md | 9 +++++ README.md | 6 ++- pyproject.toml | 2 +- src/ojin/stv/ojin_stv_client.py | 14 +++++++ src/ojin/stv/ojin_stv_webrtc_client.py | 10 +++++ tests/stv/test_ojin_stv_client.py | 20 ++++++++++ tests/stv/test_stv_client_webrtc_mode.py | 18 +++++++++ tests/stv/test_webrtc_client.py | 48 ++++++++++++++++++++++-- uv.lock | 2 +- 9 files changed, 122 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaede59..7febc3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ All notable changes to `ojin-client` are documented here. The format follows ## Unreleased +## 0.12.3 - 2026-10-26 + +### Added + +- `OjinSTVClient.played_audio_ms` exposes cumulative original speech output, + excluding idle filler. In direct WebRTC mode it counts server-published + speech, independent of metadata arrival timing. It does not confirm that a + listener heard the audio. Turns and interruptions do not reset the counter. + ## 0.12.2 - 2026-10-15 ### Added diff --git a/README.md b/README.md index 4e75271..0355b87 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,8 @@ config = STVConfig( **Output sink.** By default frames land in a `QueueOutput` you drain with `output_stream()`. Inject a custom `STVOutput` (e.g. to push straight into a media transport) and consume it directly — `output_stream()` is only for the default queue. +**Speech progress.** `client.played_audio_ms` counts cumulative original speech output, excluding idle filler. WebSocket mode counts audio handed to the output sink; direct WebRTC mode counts server-published speech metadata. Neither confirms that the listener heard the audio. The counter starts at zero for each client and is not reset by turns or interruptions, so save a baseline to measure a particular interval. + ### Events Register handlers with `@client.on(STVEvent.X)` or `client.add_listener(STVEvent.X, cb)`. Handlers may be sync or async, and a failing handler never breaks the playback loop. @@ -206,8 +208,8 @@ Register handlers with `@client.on(STVEvent.X)` or `client.add_listener(STVEvent | `SESSION_READY` | The session is live and ready for audio | `session_data: dict \| None` | | `WEBRTC_CONNECTED` | Direct WebRTC only: the avatar has joined your room | `participant_id: str \| None` | | `FIRST_FRAME` | The avatar's first video frame is out (emitted to your output, or published to the room) | `frame_type: int` | -| `BOT_STARTED_SPEAKING` | The first speech frame of a turn is played | — | -| `BOT_STOPPED_SPEAKING` | A turn has finished playing | — | +| `BOT_STARTED_SPEAKING` | Speech starts or resumes after idle frames | None | +| `BOT_STOPPED_SPEAKING` | Speech pauses or ends; this alone does not confirm a complete streaming turn | None | | `INTERRUPTED` | A barge-in was accepted | — | | `ERROR` | A transport, server or WebRTC error occurred | `message: str`, `code: str` (server / WebRTC), `fatal: bool` | | `CLOSED` | The session has been torn down | — | diff --git a/pyproject.toml b/pyproject.toml index e57bf8d..3d8e4c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "ojin-client" -version = "0.12.2" +version = "0.12.3" description = "Ojin platform services" readme = "README.md" requires-python = ">=3.10" diff --git a/src/ojin/stv/ojin_stv_client.py b/src/ojin/stv/ojin_stv_client.py index 33cdcf1..24be8aa 100644 --- a/src/ojin/stv/ojin_stv_client.py +++ b/src/ojin/stv/ojin_stv_client.py @@ -243,6 +243,20 @@ def session_data(self) -> Optional[dict]: return self._webrtc.session_data return self._session_data + @property + def played_audio_ms(self) -> float: + """Cumulative original speech output, in milliseconds. + + WebSocket mode counts audio handed to the output sink, excluding idle + filler. Direct WebRTC mode counts server-published speech metadata, + independent of when those messages arrive. Neither is an acknowledgment + that the listener heard the audio. The counter starts at zero for this + client and is not reset by a new turn or an interruption. + """ + if self._webrtc is not None: + return self._webrtc.played_audio_ms + return self._played_real_ms + def on(self, event: STVEvent) -> Callable: """Register an event handler via decorator (see :class:`STVEvent`).""" return self._events.on(event) diff --git a/src/ojin/stv/ojin_stv_webrtc_client.py b/src/ojin/stv/ojin_stv_webrtc_client.py index 88ec3ef..9861391 100644 --- a/src/ojin/stv/ojin_stv_webrtc_client.py +++ b/src/ojin/stv/ojin_stv_webrtc_client.py @@ -247,6 +247,16 @@ def session_data(self) -> Optional[dict]: """Server-provided session parameters, available after SESSION_READY.""" return self._session_data + @property + def played_audio_ms(self) -> float: + """Cumulative server-published speech duration, in milliseconds. + + Speech metadata advances this counter by 40 ms per frame; idle and + fade-out metadata do not. New turns and interruptions do not reset it. + This measures publication, not the receiving browser's audio playback. + """ + return self._played_real_ms + def on(self, event: STVEvent) -> Callable: """Register an event handler via decorator (see :class:`STVEvent`).""" return self._events.on(event) diff --git a/tests/stv/test_ojin_stv_client.py b/tests/stv/test_ojin_stv_client.py index e068ebd..7f6297f 100644 --- a/tests/stv/test_ojin_stv_client.py +++ b/tests/stv/test_ojin_stv_client.py @@ -172,6 +172,26 @@ async def run() -> None: asyncio.run(run()) +def test_public_progress_counts_original_audio_but_not_idle_filler() -> None: + """Quiet original PCM advances progress; generated idle audio does not.""" + + async def run() -> None: + client, _, output = make_client() + assert client.played_audio_ms == 0.0 + await client._emit_tick() + assert client.played_audio_ms == 0.0 + buffer = AudioBuffer(sample_rate=16000) + buffer.bytes_.extend(b"\x00" * 1280) + client._synchronizer.current_buffer = buffer + await client._emit_tick() + assert client.played_audio_ms == 40.0 + await client._emit_tick() + assert client.played_audio_ms == 40.0 + assert len(output.audio) == 3 + + asyncio.run(run()) + + def test_interrupt_sends_cancel_only_when_interruptible() -> None: """Interrupt sends a cancel only when a live buffer is playing.""" diff --git a/tests/stv/test_stv_client_webrtc_mode.py b/tests/stv/test_stv_client_webrtc_mode.py index 1312b59..c184292 100644 --- a/tests/stv/test_stv_client_webrtc_mode.py +++ b/tests/stv/test_stv_client_webrtc_mode.py @@ -133,6 +133,24 @@ async def test_connected_session_uses_the_same_input_api() -> None: assert client.is_connected is False +async def test_public_progress_reports_direct_publishing_without_local_media() -> None: + """The common client exposes the direct engine's cumulative speech clock.""" + output = ListOutput() + client, fake_client = make_client(output=output) + connected = asyncio.Event() + first_speech = asyncio.Event() + client.add_listener(STVEvent.WEBRTC_CONNECTED, lambda **_: connected.set()) + client.add_listener(STVEvent.BOT_STARTED_SPEAKING, lambda **_: first_speech.set()) + assert client.played_audio_ms == 0.0 + await client.start() + await asyncio.wait_for(connected.wait(), 1) + await fake_client.push(_frame(FrameType.SPEECH)) + await asyncio.wait_for(first_speech.wait(), 1) + assert client.played_audio_ms == 40.0 + assert output.audio == [] and output.video == [] + await client.close() + + async def test_say_and_interrupt_work_in_webrtc_mode() -> None: """The one-shot say() helper and barge-in go through the WebRTC engine.""" client, fake_client = make_client() diff --git a/tests/stv/test_webrtc_client.py b/tests/stv/test_webrtc_client.py index 138beaf..7769062 100644 --- a/tests/stv/test_webrtc_client.py +++ b/tests/stv/test_webrtc_client.py @@ -96,10 +96,10 @@ async def test_clock_advances_40ms_per_speech_frame() -> None: await _connect(client, fake_client) await client._handle_message(_frame(FrameType.START_OF_SPEECH)) - assert client._played_real_ms == 40.0 + assert client.played_audio_ms == 40.0 await client._handle_message(_frame(FrameType.SPEECH)) await client._handle_message(_frame(FrameType.SPEECH)) - assert client._played_real_ms == 120.0 + assert client.played_audio_ms == 120.0 await client.close() @@ -111,8 +111,50 @@ async def test_idle_and_fadeout_never_advance_clock() -> None: for _ in range(5): await client._handle_message(_frame(FrameType.IDLE)) await client._handle_message(_frame(FrameType.FADE_OUT)) - assert client._played_real_ms == 0.0 + assert client.played_audio_ms == 0.0 + await client.close() + + +async def test_public_progress_counts_metadata_even_when_delivery_is_bursty() -> None: + """Elapsed arrival time cannot add or remove published speech duration.""" + now = [0.0] + client, fake_client, _ = make_client(clock=lambda: now[0]) + await _connect(client, fake_client) + starts = [] + client.add_listener( + STVEvent.BOT_STARTED_SPEAKING, + lambda **_: starts.append(client.played_audio_ms), + ) + + now[0] = 10.0 + for _ in range(25): + await client._handle_message(_frame(FrameType.SPEECH)) + assert client.played_audio_ms == 1000.0 + assert starts == [40.0] + now[0] = 100.0 + await client._handle_message(_frame(FrameType.IDLE)) + assert client.played_audio_ms == 1000.0 + await client.close() + + +async def test_progress_stays_cumulative_across_interruption_and_next_turn() -> None: + """Sending, interrupting, and starting another turn cannot invent progress.""" + client, fake_client, _ = make_client(server_feed_batching_enabled=False) + await _connect(client, fake_client) + await client.start_turn() + await client.send_tts_audio(b"\x01\x02" * 16000, 16000, 1) + assert client.played_audio_ms == 0.0 + await client._handle_message(_frame(FrameType.SPEECH)) + assert client.played_audio_ms == 40.0 + await client.interrupt() + await client._handle_message(_frame(FrameType.IDLE)) + await client.start_turn() + await client.send_tts_audio(b"\x01\x02" * 640, 16000, 1) + assert client.played_audio_ms == 40.0 + await client._handle_message(_frame(FrameType.SPEECH)) + assert client.played_audio_ms == 80.0 await client.close() + assert client.played_audio_ms == 80.0 async def test_lead_gated_sends_release_on_speech_frames() -> None: diff --git a/uv.lock b/uv.lock index 61e8799..d30d747 100644 --- a/uv.lock +++ b/uv.lock @@ -825,7 +825,7 @@ wheels = [ [[package]] name = "ojin-client" -version = "0.12.2" +version = "0.12.3" source = { editable = "." } dependencies = [ { name = "async-lru" },