initializeMediaDevices method
method that generates MediaStream from your device camera that will be automatically added to peer connection instance internally used by janus client
useDisplayMediaDevices
: setting this true will give you capabilities to stream your device screen over PeerConnection.
mediaConstraints
: using this map you can specify media contraits such as resolution and fps etc.
you can use this method to get the stream and show live preview of your camera to RTCVideoRendererView
Implementation
Future<MediaStream?> initializeMediaDevices(
{bool? useDisplayMediaDevices = false,
Map<String, dynamic>? mediaConstraints}) async {
await _disposeMediaStreams(ignoreRemote: true);
List<MediaDeviceInfo> videoDevices = await getVideoInputDevices();
List<MediaDeviceInfo> audioDevices = await getAudioInputDevices();
if (videoDevices.isEmpty && audioDevices.isEmpty) {
throw Exception("No device found for media generation");
}
if (mediaConstraints == null) {
if (videoDevices.isEmpty && audioDevices.isNotEmpty) {
mediaConstraints = {"audio": true, "video": false};
} else if (videoDevices.length == 1 && audioDevices.isNotEmpty) {
mediaConstraints = {"audio": true, 'video': true};
} else {
mediaConstraints = {
"audio": audioDevices.length > 0,
'video': {
'deviceId': {'exact': videoDevices.first.deviceId},
},
};
}
}
_context._logger.fine(mediaConstraints);
if (webRTCHandle != null) {
if (useDisplayMediaDevices == true) {
webRTCHandle!.localStream =
await navigator.mediaDevices.getDisplayMedia(mediaConstraints);
} else {
webRTCHandle!.localStream =
await navigator.mediaDevices.getUserMedia(mediaConstraints);
}
if (_context._isUnifiedPlan && !_context._usePlanB) {
_context._logger.finest('using unified plan');
webRTCHandle!.localStream!.getTracks().forEach((element) async {
_context._logger.finest('adding track in peerconnection');
_context._logger.finest(element.toString());
await webRTCHandle!.peerConnection!
.addTrack(element, webRTCHandle!.localStream!);
});
} else {
_localStreamController!.sink.add(webRTCHandle!.localStream);
await webRTCHandle!.peerConnection!
.addStream(webRTCHandle!.localStream!);
}
return webRTCHandle!.localStream;
} else {
_context._logger.severe("error webrtchandle cant be null");
return null;
}
}