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
125 changes: 89 additions & 36 deletions opus_dart/lib/src/opus_dart_streaming.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,15 @@ class StreamOpusEncoder<T> extends StreamTransformerBase<List<T>, Uint8List> {
/// the partial frame: either zero-pads and encodes it
/// ([fillUpLastFrame] = true) or throws [UnfinishedFrameException].
///
/// The encoder is always destroyed in the `finally` block, regardless of
/// whether the stream completes normally or with an error.
/// The encoder is destroyed when the stream completes, errors, or the
/// listener cancels its subscription.
@override
Stream<Uint8List> bind(Stream<List<T>> stream) async* {
try {
await for (Uint8List pcm in _mapStream(stream)) {
for (final packet in _processChunk(pcm)) {
yield packet;
}
}
for (final packet in _flushRemaining()) {
yield packet;
}
} finally {
destroy();
}
}
Stream<Uint8List> bind(Stream<List<T>> stream) => _bindNative(
_mapStream(stream),
onData: (pcm, sink) => _processChunk(pcm).forEach(sink.add),
onDone: (sink) => _flushRemaining().forEach(sink.add),
cleanup: destroy,
);

/// Converts the typed input stream into a uniform [Stream<Uint8List>].
///
Expand Down Expand Up @@ -354,30 +346,91 @@ class StreamOpusDecoder extends StreamTransformerBase<Uint8List?, List<num>> {
return output;
}

/// The decoder is destroyed when the stream completes, errors, or the
/// listener cancels its subscription.
@override
Stream<List<num>> bind(Stream<Uint8List?> stream) async* {
await for (Uint8List? packet in stream) {
if (packet == null) {
if (forwardErrorCorrection) {
_decoder.inputBufferIndex = 0;
if (_lastPacketLost) {
_reportPacketLoss();
}
} else {
Stream<List<num>> bind(Stream<Uint8List?> stream) => _bindNative(
stream,
onData: _onPacket,
cleanup: destroy,
);

void _onPacket(Uint8List? packet, EventSink<List<num>> sink) {
if (packet == null) {
if (forwardErrorCorrection) {
_decoder.inputBufferIndex = 0;
if (_lastPacketLost) {
_reportPacketLoss();
}
_lastPacketLost = true;
continue;
} else {
_reportPacketLoss();
}
_decoder.inputBuffer.setAll(0, packet);
_decoder.inputBufferIndex = packet.length;
if (_lastPacketLost && forwardErrorCorrection) {
_decodeFec(true);
yield _output();
}
_lastPacketLost = false;
_decodeFec(false);
yield _output();
_lastPacketLost = true;
return;
}
_decoder.inputBuffer.setAll(0, packet);
_decoder.inputBufferIndex = packet.length;
if (_lastPacketLost && forwardErrorCorrection) {
_decodeFec(true);
sink.add(_output());
}
_lastPacketLost = false;
_decodeFec(false);
sink.add(_output());
}

/// Manually destroys this decoder.
void destroy() => _decoder.destroy();
}

/// Binds [source] through [onData]/[onDone], ending the stream on the first
/// error, and calls [cleanup] once the listener is gone (done, error or cancel).
///
/// Not an `async*` generator on purpose: a generator parked in `await for`
/// only sees a cancel when the next upstream event arrives, so cancelling
/// while the source is idle would hang and leak the native codec.
Stream<R> _bindNative<S, R>(
Stream<S> source, {
required void Function(S event, EventSink<R> sink) onData,
void Function(EventSink<R> sink)? onDone,
required void Function() cleanup,
}) {
late StreamSubscription<S> upstream;
late StreamController<R> controller;
void fail(Object error, StackTrace stackTrace) {
upstream.cancel();
controller
..addError(error, stackTrace)
..close();
}

controller = StreamController<R>(
onListen: () {
upstream = source.listen(
(event) {
try {
onData(event, controller);
} catch (e, s) {
fail(e, s);
}
},
onError: fail,
onDone: () {
try {
onDone?.call(controller);
} catch (e, s) {
return fail(e, s);
}
controller.close();
},
);
},
onPause: () => upstream.pause(),
onResume: () => upstream.resume(),
onCancel: () {
cleanup();
return upstream.cancel();
},
);
return controller.stream;
}
39 changes: 39 additions & 0 deletions opus_dart/test/opus_dart_streaming_mock_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
Expand Down Expand Up @@ -393,6 +394,26 @@ void main() {
verify(mockEncoder.opus_encoder_destroy(any)).called(1);
});

test('cancel while source is idle completes and destroys (#21)', () async {
stubEncoderCreate();
when(mockEncoder.opus_encoder_destroy(any)).thenReturn(null);
var upstreamCancelled = false;
final source =
StreamController<Int16List>(onCancel: () => upstreamCancelled = true);

final sub = StreamOpusEncoder.s16le(
frameTime: _frameTime,
sampleRate: _sampleRate,
channels: _channels,
application: Application.voip,
).bind(source.stream).listen(null);
await pumpEventQueue();

await sub.cancel().timeout(const Duration(seconds: 1));
expect(upstreamCancelled, isTrue);
verify(mockEncoder.opus_encoder_destroy(any)).called(1);
});

test('copyOutput=true returns a copy of the output buffer', () async {
stubEncoderCreate();
stubEncode([0x11, 0x22]);
Expand Down Expand Up @@ -683,6 +704,24 @@ void main() {
// ---------------------------------------------------------------------------

group('StreamOpusDecoder.s16le bind()', () {
test('cancel while source is idle completes and destroys (#21)', () async {
stubDecoderCreate();
when(mockDecoder.opus_decoder_destroy(any)).thenReturn(null);
var upstreamCancelled = false;
final source = StreamController<Uint8List?>(
onCancel: () => upstreamCancelled = true);

final sub = StreamOpusDecoder.s16le(
sampleRate: _sampleRate,
channels: _channels,
).bind(source.stream).listen(null);
await pumpEventQueue();

await sub.cancel().timeout(const Duration(seconds: 1));
expect(upstreamCancelled, isTrue);
verify(mockDecoder.opus_decoder_destroy(any)).called(1);
});

test('one packet emits one Int16List', () async {
stubDecoderCreate();
stubDecode([100, 200, 300]);
Expand Down
Loading