fetchStream method
Future<MediaStream?>
fetchStream({
- String? deviceId,
- VideoTrackConfig encoderConfig = VideoTrackConfig.h360p_w640p,
- String facingMode = "user",
- required CustomMediaDevice mediaDevice,
Fetch the stream from the device
This stream is not managed by the Huddle01 SDK, i.e. it will not be closed by the SDK the user has to close it manually by calling {stream.getTracks().forEach(track => track.stop())}
NOTE: using stopTrackOnClose = true
while producing will stop the track when producing is stopped
Implementation
Future<MediaStream?> fetchStream({
String? deviceId,
VideoTrackConfig encoderConfig = VideoTrackConfig.h360p_w640p,
String facingMode = "user",
required CustomMediaDevice mediaDevice,
}) async {
late Map<String, dynamic> mediaConstraints;
try {
var optional = deviceId != null
? [
{
'sourceId': deviceId,
}
]
: [];
if (deviceId != null && Platform.isAndroid) {
MediaDeviceInfo? device = mediaDevices[MediaDeviceType.videoInput]!
.where((element) => element.deviceId.toLowerCase() == deviceId)
.first;
if (device.deviceId.isNotEmpty && Platform.isAndroid) {
optional = [
{
'sourceId': device.label,
}
];
}
}
if (mediaDevice == CustomMediaDevice.cam) {
mediaConstraints = <String, dynamic>{
'audio': false,
'video': {
'facingMode': facingMode,
'width': videotrackConfigMap[encoderConfig]!["width"],
'height': videotrackConfigMap[encoderConfig]!["height"],
'frameRate': videotrackConfigMap[encoderConfig]!["frameRate"],
'optional': optional,
},
};
logger.i("mediaConstraints | cam => $mediaConstraints");
}
if (mediaDevice == CustomMediaDevice.mic) {
mediaConstraints = <String, dynamic>{'audio': true, 'video': false};
}
MediaStream stream =
await navigator.mediaDevices.getUserMedia(mediaConstraints);
return stream;
} catch (error) {
logger.e('❌ Error in fetchStream: $error');
return null;
}
}