Re: [mediacapture-screen-share] Auto-pause capture when user switches captured content (#255)

My concern remains that this API becomes a way for apps to intentionally or accidentally block a useful existing feature (source-switching through track injection) — I think the accidental part can be fixed however by adjusting the API shape.

I'd prefer an API that centers the old injection behavior as the norm. Something like this:
```js
const controller = new CaptureController;
video.srcObject = await navigator.mediaDevices.getDisplayMedia({video: true, audio: true, controller});
controller.onsourceswitch = event => {
  video.srcObject = event.stream;
  event.preventDefault();
};
```
This is a [cancelable event](https://dom.spec.whatwg.org/#dom-event-cancelable) that notifies the webapp that the source of the capture is about to change.

1. The default action is to switch all tracks from the old source to the new one, and stop the tracks in `event.stream`.
2. The webapp can prevent this with [preventDefault()](https://dom.spec.whatwg.org/#dom-event-preventdefault), which instead stops all tracks from the old source.

This absolves the webapp from stopping tracks in the common cases, avoiding issues with multiple listeners. I.e. `event.stream` is the same object for all events.

The webapp can inspect the new tracks before making a decision.

> This would allow easy migration in the simple cases (just do nothing for video, check for new audio).

```js
controller.onsourceswitch = event => {
  if (video.srcObject.getTracks().length == event.stream.getTracks().length) return;
  video.srcObject = event.stream;
  event.preventDefault();
};
```

-- 
GitHub Notification of comment by jan-ivar
Please view or discuss this issue at https://github.com/w3c/mediacapture-screen-share/issues/255#issuecomment-1804411115 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Thursday, 9 November 2023 18:58:13 UTC