Bug: ByteStreamWriter sends first chunk with index 1, causing client errors
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: https://github.com/livekit/python-sdks/blob/ea42eb8a77ee2954efb002687b7e04b03c3f79ab/livekit-rtc/livekit/rtc/data_stream.py#L342-L348 Correct TextStreamWriter logic: https://github.com/livekit/python-sdks/blob/ea42eb8a77ee2954efb002687b7e04b03c3f79ab/livekit-rtc/livekit/rtc/data_stream.py#L286-L294
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.