demuxWorker function
Opens a container over the bytes it was spawned with, then serves packets.
Implementation
Future<void> demuxWorker(WorkerChannel channel) async {
final init = channel.initialMessage! as Map<String, Object?>;
PlatformDemuxer? demuxer;
channel.handleRequests((Object? request) async {
switch (request) {
case 'open':
final containerName = init['container'] as String?;
final opened = ContainerFramingBackend.openInProcess(
init['bytes']! as Uint8List,
containerName == null ? null : Container.values.byName(containerName),
);
if (opened == null) {
throw const CodecInitException(
'container-worker',
'no pure-Dart parser claims this container',
);
}
demuxer = opened;
return TracksMessage(
opened.tracks,
opened.durationUs,
opened.isSeekable,
);
case 'read':
final packet = await demuxer!.readPacket();
return packet == null ? null : PacketMessage(packet);
case ['seek', final int timestampUs]:
await demuxer!.seek(timestampUs);
return null;
default:
throw StateError('unknown op: $request');
}
});
await channel.onClose;
try {
await demuxer?.close();
} on Object {
// Teardown is best effort; the host is already gone.
}
}