Skip to content

Bug: ByteStreamWriter sends first chunk with index 1, causing client errors #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5Hyeons opened this issue May 2, 2025 · 0 comments
Closed

Comments

@5Hyeons
Copy link
Contributor

5Hyeons commented May 2, 2025

Description:
There is an inconsistency in chunk indexing between ByteStreamWriter and TextStreamWriter in livekit/rtc/data_stream.py. The ByteStreamWriter.write method incorrectly increments the _next_chunk_index before using it for the chunk_index field in the proto_DataStream.Chunk message. This results in the first chunk being sent with index 1.

Code References:
Incorrect ByteStreamWriter logic:

for chunk in chunked_data:
self._next_chunk_index += 1
chunk_msg = proto_DataStream.Chunk(
stream_id=self._header.stream_id,
chunk_index=self._next_chunk_index,
content=chunk,
)

Correct TextStreamWriter logic:
for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
content = chunk
chunk_index = self._next_chunk_index
self._next_chunk_index += 1
chunk_msg = proto_DataStream.Chunk(
stream_id=self._header.stream_id,
chunk_index=chunk_index,
content=content,
)

Problem:
Most stream consumers, including the LiveKit Unity SDK (ByteStreamReader), expect the first chunk index of a stream to be 0.
When receiving a chunk with index 1 without seeing index 0 first, the client raises an error, typically like "expected chunk index to be exactly one more than the previous". This prevents byte streams from working correctly with such clients.

Text streams work correctly because TextStreamWriter uses the current _next_chunk_index (which starts at 0) before incrementing it.

Proposed Solution:
Modify ByteStreamWriter.write to use _next_chunk_index for the chunk_index before incrementing it, consistent with TextStreamWriter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants