Skip to content

Commit d924629

Browse files
authored
🤖 Merge PR DefinitelyTyped#74154 [w3c-screen-capture] Add type definitions for Screen capture by @paulinagacek
1 parent 296b101 commit d924629

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @see https://w3c.github.io/mediacapture-screen-share
3+
*/
4+
5+
declare global {
6+
interface MediaDevices {
7+
getDisplayMedia(options?: DisplayMediaStreamOptions): Promise<MediaStream>;
8+
}
9+
}
10+
11+
export interface UserMediaStreamConstraints {
12+
/** @default false */
13+
video?: boolean | MediaTrackConstraints;
14+
/** @default false */
15+
audio?: boolean | MediaTrackConstraints;
16+
}
17+
18+
export type CaptureStartFocusBehavior =
19+
| "focus-capturing-application"
20+
| "focus-captured-surface"
21+
| "no-focus-change";
22+
23+
declare global {
24+
class CaptureController extends EventTarget {
25+
constructor();
26+
setFocusBehavior(focusBehavior: CaptureStartFocusBehavior): void;
27+
}
28+
}
29+
30+
export type SelfCapturePreferenceEnum =
31+
| "include"
32+
| "exclude";
33+
34+
export type SystemAudioPreferenceEnum =
35+
| "include"
36+
| "exclude";
37+
38+
export type WindowAudioPreferenceEnum =
39+
| "system"
40+
| "window"
41+
| "exclude";
42+
43+
export type SurfaceSwitchingPreferenceEnum =
44+
| "include"
45+
| "exclude";
46+
47+
export type MonitorTypeSurfacesEnum =
48+
| "include"
49+
| "exclude";
50+
51+
export interface DisplayMediaStreamOptions {
52+
/** @default true */
53+
video?: boolean | MediaTrackConstraints;
54+
/** @default false */
55+
audio?: boolean | MediaTrackConstraints;
56+
controller?: CaptureController;
57+
selfBrowserSurface?: SelfCapturePreferenceEnum;
58+
systemAudio?: SystemAudioPreferenceEnum;
59+
windowAudio?: WindowAudioPreferenceEnum;
60+
surfaceSwitching?: SurfaceSwitchingPreferenceEnum;
61+
monitorTypeSurfaces?: MonitorTypeSurfacesEnum;
62+
}
63+
64+
export interface MediaTrackSupportedConstraints {
65+
/** @default true */
66+
displaySurface?: boolean;
67+
/** @default true */
68+
logicalSurface?: boolean;
69+
/** @default true */
70+
cursor?: boolean;
71+
/** @default true */
72+
restrictOwnAudio?: boolean;
73+
/** @default true */
74+
suppressLocalAudioPlayback?: boolean;
75+
}
76+
77+
export interface MediaTrackConstraintSet {
78+
displaySurface?: ConstrainDOMString;
79+
logicalSurface?: ConstrainBoolean;
80+
cursor?: ConstrainDOMString;
81+
restrictOwnAudio?: ConstrainBoolean;
82+
suppressLocalAudioPlayback?: ConstrainBoolean;
83+
}
84+
85+
export interface MediaTrackSettings {
86+
displaySurface?: string;
87+
logicalSurface?: boolean;
88+
cursor?: string;
89+
restrictOwnAudio?: boolean;
90+
suppressLocalAudioPlayback?: boolean;
91+
screenPixelRatio?: number;
92+
}
93+
94+
export interface MediaTrackCapabilities {
95+
displaySurface?: string;
96+
logicalSurface?: boolean;
97+
cursor?: string[];
98+
}
99+
100+
export type DisplayCaptureSurfaceType =
101+
| "monitor"
102+
| "window"
103+
| "browser";
104+
105+
export type CursorCaptureConstraint =
106+
| "never"
107+
| "always"
108+
| "motion";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"private": true,
3+
"nonNpm": true,
4+
"nonNpmDescription": "Implementation of Isolated Web App (IWA) APIs resides in Chromium.",
5+
"name": "@types/w3c-screen-capture",
6+
"version": "0.0.9999",
7+
"projects": [
8+
"https://github.com/WICG/isolated-web-apps"
9+
],
10+
"devDependencies": {
11+
"@types/web": "*",
12+
"@types/w3c-screen-capture": "workspace:."
13+
},
14+
"owners": [
15+
{
16+
"name": "Paulina Gacek",
17+
"githubUsername": "paulinagacek"
18+
},
19+
{
20+
"name": "Andrew Rayskiy",
21+
"githubUsername": "GrapeGreen"
22+
},
23+
{
24+
"name": "Simon Hangl",
25+
"githubUsername": "shangl"
26+
}
27+
]
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictFunctionTypes": true,
11+
"strictNullChecks": true,
12+
"types": [],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"paths": {
16+
"w3c-screen-capture": [
17+
"./index.d.ts"
18+
]
19+
}
20+
},
21+
"files": [
22+
"index.d.ts",
23+
"w3c-screen-capture-tests.ts"
24+
]
25+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { DisplayMediaStreamOptions, MediaTrackConstraintSet } from "w3c-screen-capture";
2+
3+
function testCaptureController() {
4+
const controller = new CaptureController();
5+
6+
// Valid behaviors
7+
// $ExpectType void
8+
controller.setFocusBehavior("focus-captured-surface");
9+
10+
// $ExpectType void
11+
controller.setFocusBehavior("focus-capturing-application");
12+
13+
// $ExpectType void
14+
controller.setFocusBehavior("no-focus-change");
15+
16+
// Inheritance check (EventTarget)
17+
controller.addEventListener("focus", () => {});
18+
19+
// @ts-expect-error - Invalid enum value
20+
controller.setFocusBehavior("focus-unknown");
21+
}
22+
23+
async function testGetDisplayMedia() {
24+
const controller = new CaptureController();
25+
26+
const options: DisplayMediaStreamOptions = {
27+
video: true,
28+
audio: false,
29+
30+
// Advanced Enum preferences
31+
controller: controller,
32+
selfBrowserSurface: "include",
33+
systemAudio: "exclude",
34+
windowAudio: "window",
35+
surfaceSwitching: "include",
36+
monitorTypeSurfaces: "exclude",
37+
};
38+
39+
const stream = await navigator.mediaDevices.getDisplayMedia(options);
40+
const track = stream.getTracks()[0];
41+
track.stop();
42+
}
43+
44+
function testConstraints() {
45+
// Verifying the extension of constraints
46+
const constraints: MediaTrackConstraintSet = {
47+
// New Screen Share specific constraints
48+
displaySurface: "monitor",
49+
logicalSurface: true,
50+
cursor: "motion",
51+
restrictOwnAudio: false,
52+
suppressLocalAudioPlayback: true,
53+
};
54+
55+
const complexOptions: DisplayMediaStreamOptions = {
56+
video: constraints,
57+
audio: { echoCancellation: true },
58+
};
59+
}
60+
61+
function testSettings(track: MediaStreamTrack) {
62+
const settings: MediaTrackSettings = track.getSettings();
63+
64+
if (settings.displaySurface === "window") {
65+
console.log("Captured a window");
66+
}
67+
68+
// @ts-expect-error - Type 'number' is not comparable to type 'string'
69+
if (settings.cursor === 123) {}
70+
}

0 commit comments

Comments
 (0)