bind method

  1. @override
Stream<List<num>> bind(
  1. Stream<Uint8List?> stream
)
override

Binds this stream to transfrom an other stream. A null element in the input stream is interpreted as packet loss.

The runtime type of the returned stream is either Stream<Float32List>, Stream<Int16List> or Stream<Uint8List> depending on what constructor was used, so it is safe to use the returned streams cast method to obtain the stream in that format.

Implementation

@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) {
          // We've also lost the previous packet, which we can not restore now
          // So tell the decoder
          _reportPacketLoss();
        }
      } else {
        // There is no fec, so tell the decoder we lost a package
        _reportPacketLoss();
      }
      _lastPacketLost = true;
    } else {
      _decoder.inputBuffer.setAll(0, packet);
      _decoder.inputBufferIndex = packet.length;
      if (_lastPacketLost && forwardErrorCorrection) {
        // The last packet was lost, attempt to restore it
        if (floats) {
          _decoder.decodeFloat(
              fec: true, loss: _decoder.lastPacketDurationMs);
        } else {
          _decoder.decode(fec: true, loss: _decoder.lastPacketDurationMs);
        }
        yield _output();
      }
      _lastPacketLost = false;
      if (floats) {
        _decoder.decodeFloat(fec: false);
      } else {
        _decoder.decode(fec: false);
      }
      yield _output();
    }
  }
}