Skip to content

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

Closed
@5Hyeons

Description

@5Hyeons

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions