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
1 change: 1 addition & 0 deletions .changes/data-stream-error-livekit-exception
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" "`DataStreamError` is now a `LiveKitException`, so `on LiveKitException` also catches data stream failures, such as a write cut off by disposing the room"
9 changes: 7 additions & 2 deletions lib/src/data_stream/errors.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import '../exceptions.dart';

enum DataStreamErrorReason {
// Unable to open a stream with the same ID more than once.
AlreadyOpened,

// Stream closed abnormally by remote participant.
// Stream ended before completing: the sender disconnected mid-stream, the local room was
// disconnected or disposed, or sending or receiving failed. Also used for failures that map to no
// more specific reason.
AbnormalEnd,

// Incoming chunk data could not be decoded.
Expand All @@ -24,8 +28,9 @@ enum DataStreamErrorReason {
AlreadyClosed,
}

class DataStreamError implements Exception {
class DataStreamError implements LiveKitException {
final DataStreamErrorReason reason;
@override
final String message;

DataStreamError({required this.reason, required this.message});
Expand Down
18 changes: 18 additions & 0 deletions test/core/data_stream_v2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,24 @@ void main() {
expect(streamPackets().where((p) => !p.hasStreamTrailer()), isEmpty);
});

test('writes cut off by disposing the room fail with a LiveKitException', () async {
await container.simulateRemoteParticipantJoin('alice', clientProtocol: 2);
final writer = await room.localParticipant!.streamBytes(StreamBytesOptions(topic: 'cut-off'));

// Not awaited: the room is disposed while these are still in the core, which fails them with
// a DataStreamError. Apps catch the SDK's errors as LiveKitException, so it has to be one.
final errors = <Object>[];
final writes = [
for (var i = 0; i < 50; i++)
writer.write(Uint8List(30 * 1024)).then((_) {}, onError: (Object e) => errors.add(e)),
];
await room.dispose();
await Future.wait(writes);

expect(errors, isNotEmpty, reason: 'no write was cut off, so there is nothing to check');
expect(errors, everyElement(isA<LiveKitException>()));
});

test('a stream packet still on its way in does not open a stream', () async {
final ds = room.dataStreams as NativeDataStreams;
var opened = 0;
Expand Down
Loading