electron: `mandatory` property missing from `MediaTrackConstraints`

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version: v11.1.0
  • Operating System: macOS 11
  • Last Known Working Electron version: -

Expected Behavior

TypeScript compiles with no type errors.

Actual Behavior

error TS2322: Type '{ mandatory: { chromeMediaSource: string; chromeMediaSourceId: string; }; }' is not assignable to type 'boolean | MediaTrackConstraints | undefined'.
  Object literal may only specify known properties, and 'mandatory' does not exist in type 'MediaTrackConstraints'.

To Reproduce

In a TypeScript project, use the desktopCapturer API as described in its docs, e.g. just use the example code on that page, or:

const screenCaptureStream = await navigator.mediaDevices.getUserMedia({
  audio: false,
  video: {
    mandatory: {
      chromeMediaSource: 'desktop',
      chromeMediaSourceId: source.id,
      // etc
    }
  }
});

Additional Information

It looks like this API is part of Chrome rather than part of Electron, and are parsed somewhere here in media_constraints_impl.cc. It also looks like this mandatory section is part of some old/deprecated getUserMedia API. It would be great if we could use desktopCapturer API with the modern getUserMedia API - maybe we can, but the docs don’t say how.

It would also be great to know what other constraints we can pass in this mandatory section. For example, can we set the frame rate for desktop capture?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Based on this https://stackoverflow.com/questions/37613981/how-to-use-a-typescript-cast-with-jsx-tsx Seems like TS follows the latest WebRTC specification. Each browser can have differences in API as WebRTC specification is not an official standard yet. Hence TS code needs to cast to any, which worked for me: const stream = await (navigator.mediaDevices as any).getUserMedia(constraints)

@jameshfisher hmm ok i’ll check into it and see what i come up with 😃

Hey, thanks @codebytere - however when I try using these properties using the modern getUserMedia API, they’re ignored: I get the default camera video stream instead of a screen capture. Here’s what I’ve tried:

// This works as expected - gets a screen capture
screenCaptureStream = await navigator.mediaDevices.getUserMedia({
  audio: false,
  video: {
    mandatory: {
      chromeMediaSource: 'desktop',
      chromeMediaSourceId: source.id,
    }
  }
});

// But this just gets the default camera stream instead of the screen -
// same behavior as if these params were not present
const screenCaptureStream = await navigator.mediaDevices.getUserMedia({
  audio: false,
  video: {
    chromeMediaSource: 'desktop',
    chromeMediaSourceId: source.id,
  }
});

// Similarly, using `ConstrainDOMStringParameters` long form -
// same behavior, gets the default camera stream instead of the screen
const screenCaptureStream = await navigator.mediaDevices.getUserMedia({
  audio: false,
  video: {
    chromeMediaSource: { exact: 'desktop' },
    chromeMediaSourceId: { exact: source.id },
  }
});

So it seems like this API really is stuck in the dark ages of getUserMedia. It would be lovely if it were updated to use the modern API, but I think that’s a separate problem to the lack of type definitions for the current API.

(Edit: above testing is on Electron 11, on macOS 11.)

Would also like some updates on this as have the same questions as OP, can we get this ticket reopened?