getUserMedia method

  1. @override
Future<MediaStream> getUserMedia(
  1. Map<String, dynamic> mediaConstraints
)
override

Calling this method will prompts the user to select and grant permission to capture the contents of a display or portion thereof (such as a window) as a MediaStream. The resulting stream can then be recorded using the MediaStream Recording API or transmitted as part of a WebRTC session.

Implementation

@override
Future<MediaStream> getUserMedia(
    Map<String, dynamic> mediaConstraints) async {
  try {
    if (mediaConstraints['video'] is Map) {
      if (mediaConstraints['video']['facingMode'] != null) {
        mediaConstraints['video'].remove('facingMode');
      }
    }

    mediaConstraints.putIfAbsent('video', () => false);
    mediaConstraints.putIfAbsent('audio', () => false);

    final mediaDevices = html.window.navigator.mediaDevices;
    if (mediaDevices == null) throw Exception('MediaDevices is null');

    if (jsutil.hasProperty(mediaDevices, 'getUserMedia')) {
      var args = jsutil.jsify(mediaConstraints);
      final jsStream = await jsutil.promiseToFuture<html.MediaStream>(
          jsutil.callMethod(mediaDevices, 'getUserMedia', [args]));

      return MediaStreamWeb(jsStream, 'local');
    } else {
      final jsStream = await html.window.navigator.getUserMedia(
        audio: mediaConstraints['audio'],
        video: mediaConstraints['video'],
      );
      return MediaStreamWeb(jsStream, 'local');
    }
  } catch (e) {
    throw 'Unable to getUserMedia: ${e.toString()}';
  }
}