pickOutputDevice static method

Future<DeviceDetails?> pickOutputDevice({
  1. String? outputId,
  2. DeviceDetails? microphone,
  3. List<DeviceDetails> devices = const [],
})

Picks a DeviceDetails suitable for the provided microphone, if any.

Implementation

static Future<DeviceDetails?> pickOutputDevice({
  String? outputId,
  DeviceDetails? microphone,
  List<DeviceDetails> devices = const [],
}) async {
  DeviceDetails? compatible;

  if (PlatformUtils.isWindows && await PlatformUtils.isWindows10) {
    if (outputId != null) {
      final DeviceDetails? output = devices.firstWhereOrNull(
        (e) => e.id() == outputId,
      );

      if (output != null) {
        // If it's the same group, then it's the same physical device.
        if (output.groupId() == microphone?.groupId()) {
          // Check whether this device is in communication mode or not.
          bool isCommunicationDevice = false;

          for (var compared in devices.output()) {
            if (compared.groupId() == output.groupId()) {
              if (!isCommunicationDevice) {
                final int? ourRate = output.numChannels();
                final int? theirRate = compared.numChannels();

                if (ourRate != null && theirRate != null) {
                  // This is a communication device, if its channel number is
                  // lower than the same physical device with higher channel
                  // number.
                  if (ourRate > theirRate) {
                    compatible ??= compared;
                  }
                }
              }
            }
          }
        }
      }
    }

    Log.debug(
      'pickOutputDevice($microphone) -> compatible device is $compatible',
      'MicrophoneSwitchController',
    );
  }

  return compatible;
}