Blancduman / mediasoup-client-flutter

Flutter version of the mediasoup-client library.
MIT License
106 stars 53 forks source link

How do we switch video/audio streams? #57

Open ollyde opened 2 years ago

ollyde commented 2 years ago

I'm disabling and re-enabling the stream

Calling producer close, then converting the transport to producer again with a new stream.

The other device seems to get the original stream, but when switching it becomes a grey screen.

I have also tried re-initializing the WebRTC render and consumers, no luck.

Can't seem to find it in the example (Provider would be better than Bloc 🙈)

Future<void> enableWebcam(BuildContext context) async {
  try {
    if (kDebugMode) {
      print("!--- enableWebcam");
    }

    RtpCodecCapability? codec;
    if (isWeb) {
      codec = device!.rtpCapabilities.codecs.where((element) => element.mimeType.contains("VP9")).toList()[0];
    } else {
      codec = device!.rtpCapabilities.codecs.where((element) => element.mimeType.contains("VP8")).toList()[0];
    }

    if (videoStream != null && sendTransport != null) {
      final self = rtcPeerProviderRead(context).selfPeer;
      await closeProducer(
        context,
        producerId: self.connections.producer!.id,
      );
    }

    videoStream = await createVideoStream(context);
    final track = videoStream!.getVideoTracks().first;

    // List what the device supports
    sendTransport!.produce(
      track: track,
      codecOptions: ProducerCodecOptions(
        videoGoogleStartBitrate: 1000,
      ),
      encodings: kIsWeb
          ? [
              RtpEncodingParameters(scalabilityMode: 'S3T3_KEY', scaleResolutionDownBy: 1.0),
            ]
          : [],
      stream: videoStream!,
      appData: {
        'source': 'webcam',
        'transportId': sendTransport!.id,
      },
      source: 'webcam',
      codec: codec,
    );
  } catch (e) {
    if (kDebugMode) {
      print("!--- Event: Failed to enable webcam");
      print(e);
    }
  }
}
ollyde commented 2 years ago

I tried using

    await Helper.switchCamera(
       videoStream!.getVideoTracks()[0],
        videoInputDeviceId,
        videoStream,
    );

But it just freezes on the other devices.

ollyde commented 2 years ago

I got it working but it shows a grey screen on the other devices. If they rejoin it works. Not sure what that could be, any help would be great.

Current code

final oldTrack = videoStream?.getVideoTracks().first;
await oldTrack!.stop();

// Throws PlatformException(mediaStreamRemoveTrack: Track is nil, null, null, null)
// await videoStream?.removeTrack(oldTrack, removeFromNative: true);

MediaStream newTrackStream = await getDeviceStream(getRootContext());

final newTrack = newTrackStream.getVideoTracks().first;
await videoStream!.addTrack(newTrack, addToNative: true);

videoStream = newTrackStream;

sendProduce(
  stream: newTrackStream,
  codec: codec,
  track: newTrack,
);
ollyde commented 2 years ago

I've logged a bug with Flutter Web RTC https://github.com/flutter-webrtc/flutter-webrtc/issues/1020