Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 | — |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 14 additions & 0 deletions src/ojin/stv/ojin_stv_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/ojin/stv/ojin_stv_webrtc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions tests/stv/test_ojin_stv_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
18 changes: 18 additions & 0 deletions tests/stv/test_stv_client_webrtc_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
48 changes: 45 additions & 3 deletions tests/stv/test_webrtc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading