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);
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?