private calculateMediaStreamConstraints()

in src/devicecontroller/DefaultDeviceController.ts [1641:1716]


  private calculateMediaStreamConstraints(
    kind: string,
    device: Device
  ): MediaStreamConstraints | null {
    let trackConstraints: MediaTrackConstraints = {};
    if (device === '') {
      device = null;
    }
    const stream = this.intrinsicDeviceAsMediaStream(device);
    if (device === null) {
      return null;
    } else if (typeof device === 'string') {
      if (
        this.browserBehavior.requiresNoExactMediaStreamConstraints() &&
        this.browserBehavior.requiresGroupIdMediaStreamConstraints()
      ) {
        // In Samsung Internet browser, navigator.mediaDevices.enumerateDevices()
        // returns same deviceId but different groupdId for some audioinput and videoinput devices.
        // To handle this, we select appropriate device using deviceId + groupId.
        trackConstraints.deviceId = device;
        trackConstraints.groupId = this.getGroupIdFromDeviceId(kind, device);
      } else if (this.browserBehavior.requiresNoExactMediaStreamConstraints()) {
        trackConstraints.deviceId = device;
      } else {
        trackConstraints.deviceId = { exact: device };
      }
    } else if (stream) {
      // @ts-ignore - create a fake track constraint using the stream id
      trackConstraints.streamId = stream.id;
    } else if (isMediaDeviceInfo(device)) {
      trackConstraints.deviceId = device.deviceId;
      trackConstraints.groupId = device.groupId;
    } else {
      // Take the input set of constraints. Note that this allows
      // the builder to specify overrides for properties like `autoGainControl`.
      // @ts-ignore - device is a MediaTrackConstraints
      trackConstraints = device;
    }
    if (kind === 'video') {
      trackConstraints.width = trackConstraints.width || {
        ideal: this.videoInputQualitySettings.videoWidth,
      };
      trackConstraints.height = trackConstraints.height || {
        ideal: this.videoInputQualitySettings.videoHeight,
      };
      trackConstraints.frameRate = trackConstraints.frameRate || {
        ideal: this.videoInputQualitySettings.videoFrameRate,
      };
    }
    if (kind === 'audio' && this.supportSampleRateConstraint()) {
      trackConstraints.sampleRate = { ideal: DefaultDeviceController.defaultSampleRate };
    }
    if (kind === 'audio' && this.supportSampleSizeConstraint()) {
      trackConstraints.sampleSize = { ideal: DefaultDeviceController.defaultSampleSize };
    }
    if (kind === 'audio' && this.supportChannelCountConstraint()) {
      trackConstraints.channelCount = { ideal: DefaultDeviceController.defaultChannelCount };
    }
    if (kind === 'audio') {
      const augmented = {
        echoCancellation: true,
        googEchoCancellation: true,
        googEchoCancellation2: true,
        googAutoGainControl: true,
        googAutoGainControl2: true,
        googNoiseSuppression: true,
        googNoiseSuppression2: true,
        googHighpassFilter: true,

        // We allow the provided constraints to override these sensible defaults.
        ...trackConstraints,
      };
      trackConstraints = augmented as MediaTrackConstraints;
    }
    return kind === 'audio' ? { audio: trackConstraints } : { video: trackConstraints };
  }