enumerateDevices method
- MediaDeviceKind? kind
Returns the DeviceDetails currently available with the provided
kind, if specified.
Implementation
Future<List<DeviceDetails>> enumerateDevices([MediaDeviceKind? kind]) async {
final List<DeviceDetails> devices =
(await (await _mediaManager)?.enumerateDevices() ?? [])
.where((e) => e.deviceId().isNotEmpty)
.where((e) => !e.isFailed())
.where((e) => kind == null || e.kind() == kind)
.whereType<MediaDeviceDetails>()
.map(DeviceDetails.new)
.toList();
// Add the [DefaultMediaDeviceDetails] to the retrieved list of devices.
//
// Browsers and mobiles already may include their own default devices.
if (kind == null || kind == MediaDeviceKind.audioInput) {
final DeviceDetails? hasDefault = devices.firstWhereOrNull(
(d) => d.kind() == MediaDeviceKind.audioInput && d.id() == 'default',
);
if (hasDefault == null) {
if (PlatformUtils.isDesktop || PlatformUtils.isWeb) {
final DeviceDetails? device = devices.firstWhereOrNull(
(e) => e.kind() == MediaDeviceKind.audioInput,
);
if (device != null) {
devices.insert(0, DefaultDeviceDetails(device));
}
}
} else {
// Sort the default device to the top, as it might be somewhere else.
if (PlatformUtils.isWeb) {
devices.removeWhere(
(e) =>
e.kind() == MediaDeviceKind.audioInput &&
e.label() == e.label() &&
e.id() == hasDefault.id(),
);
devices.insert(0, hasDefault);
}
// Audio input on mobile devices is handled by `medea_jason`, and we
// should not interfere, as otherwise we may run into
// [MediaSettingsUpdateException].
if (PlatformUtils.isMobile && !PlatformUtils.isWeb) {
devices.removeWhere(
(e) =>
e.kind() == MediaDeviceKind.audioInput &&
e.label() == e.label() &&
e.id() == hasDefault.id(),
);
}
}
}
if (kind == null || kind == MediaDeviceKind.audioOutput) {
final DeviceDetails? hasDefault = devices.firstWhereOrNull(
(d) => d.kind() == MediaDeviceKind.audioOutput && d.id() == 'default',
);
if (hasDefault == null) {
if (!PlatformUtils.isMobile || PlatformUtils.isWeb) {
final DeviceDetails? device = devices.firstWhereOrNull(
(e) => e.kind() == MediaDeviceKind.audioOutput,
);
if (device != null) {
devices.insert(0, DefaultDeviceDetails(device));
}
}
} else {
// Sort the default device to the top, as it might be somewhere else.
if (PlatformUtils.isWeb) {
devices.removeWhere(
(e) =>
e.kind() == MediaDeviceKind.audioOutput &&
e.label() == e.label() &&
e.id() == hasDefault.id(),
);
devices.insert(0, hasDefault);
}
}
}
return devices;
}