Reputation: 11
Is it possible to send MediaStream
between isolates?
Since only primitive types can be directly transferred between isolates, I've implemented the following extensions to handle this:
MediaStream Extension:
extension MediaStreamExtension on MediaStream {
Map<String, dynamic> toMap() {
final Map<String, dynamic> map = {
"streamId": id,
"ownerTag": ownerTag,
"audioTracks": getAudioTracks()
.map((final MediaStreamTrack track) => track.toMap())
.toList(),
"videoTracks": getVideoTracks()
.map((final MediaStreamTrack track) => track.toMap())
.toList(),
};
return map;
}
}
MediaStreamTrack Extension:
extension MediaStreamTrackJson on MediaStreamTrack {
Map<String, dynamic> toMap() => {
"id": id,
"label": label,
"kind": kind,
"enabled": enabled,
};
}
After converting the MediaStream to a map format, we send it to the main isolate.
In the main isolate, we attempt to convert the map back into a MediaStream to use with RTCVideoRenderer. The conversion is done as follows:
final MediaStream mediaStream = MediaStreamNative.fromMap(streamData);
final tracks = mediaStream.getTracks();
print(tracks);
log: [Track(id: 8716d90b-4131-46d1-8846-32061d0fac62, kind: audio, label: audio, enabled: true, muted: false), Track(id: 03315f09-90d9-45dd-b39e-2b389ff6dcd0, kind: video, label: video, enabled: true, muted: false)]
We are able to obtain the MediaStream, but it only displays a black screen.
Will the above approach work?, or is there something specific we need to do to send the MediaStream between isolates?
Upvotes: 0
Views: 45