@pexip/media-control 17.2.0 → 17.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,416 @@
1
+ import type { MediaDeviceInfoLike, MediaInput, MediaDeviceRequest, InputDeviceConstraint, InputConstraintSet, InputDevicePermission, FacingMode } from './types';
2
+ import { MediaDeviceKinds } from './types';
3
+ /**
4
+ * DeviceChange Event
5
+ *
6
+ * ```
7
+ * devices (raw)
8
+ * |--- authorized
9
+ * | |--- found
10
+ * | |--- lost
11
+ * |--- unauthorized
12
+ * ```
13
+ *
14
+ * @beta
15
+ */
16
+ export type DeviceChangedChanges = {
17
+ authorized: MediaDeviceInfoLike[];
18
+ unauthorized: MediaDeviceInfoLike[];
19
+ found: MediaDeviceInfoLike[];
20
+ lost: MediaDeviceInfoLike[];
21
+ devices: MediaDeviceInfoLike[];
22
+ };
23
+ interface ToKeyOptions {
24
+ id: string;
25
+ kind: string;
26
+ label: string;
27
+ }
28
+ /**
29
+ * Convert provided info to key for Map
30
+ *
31
+ * @beta
32
+ */
33
+ export declare const toKey: ({ id, kind, label }: ToKeyOptions) => string;
34
+ /**
35
+ * Future proofing: in case we want to alter the values or type returned
36
+ * by {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices | enumerateDevices}
37
+ *
38
+ * @returns
39
+ * a list of currently available {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo | devices}
40
+ *
41
+ * @example
42
+ * ```javascript
43
+ * import {getDevices} from "media-control";
44
+ *
45
+ * const devices = await getDevices();
46
+ * // return MediaDeviceInfo[]
47
+ * ```
48
+ *
49
+ * @beta
50
+ */
51
+ export declare const getDevices: () => Promise<MediaDeviceInfo[]>;
52
+ export declare const toDeviceKey: (device: MediaDeviceInfoLike) => string;
53
+ export declare const toDeviceTuple: (device: MediaDeviceInfoLike) => readonly [string, MediaDeviceInfoLike];
54
+ export declare const toDevicesMap: (devices: MediaDeviceInfoLike[]) => Map<string, MediaDeviceInfoLike>;
55
+ export declare const toUniqueDevices: (devices: MediaDeviceInfoLike[]) => MediaDeviceInfoLike[];
56
+ export declare const createTrackDevicesChanges: (prevDevices?: MediaDeviceInfoLike[]) => (devices: MediaDeviceInfoLike[]) => {
57
+ unauthorized: MediaDeviceInfoLike[];
58
+ authorized: MediaDeviceInfoLike[];
59
+ found: MediaDeviceInfoLike[];
60
+ lost: MediaDeviceInfoLike[];
61
+ devices: MediaDeviceInfoLike[];
62
+ };
63
+ /**
64
+ * Unified interface for subscribing {@link DeviceChangedChanges} Event
65
+ *
66
+ * @beta
67
+ */
68
+ export declare const deviceChanged: (fn: (event: DeviceChangedChanges) => void) => () => void;
69
+ export declare const extractDeviceInfo: (track: MediaStreamTrack) => {
70
+ kind: MediaDeviceKinds;
71
+ deviceId: string;
72
+ groupId: string;
73
+ label: string;
74
+ settings: {
75
+ aspectRatio?: number | undefined;
76
+ autoGainControl?: boolean | undefined;
77
+ channelCount?: number | undefined;
78
+ displaySurface?: string | undefined;
79
+ echoCancellation?: boolean | undefined;
80
+ facingMode?: string | undefined;
81
+ frameRate?: number | undefined;
82
+ height?: number | undefined;
83
+ noiseSuppression?: boolean | undefined;
84
+ sampleRate?: number | undefined;
85
+ sampleSize?: number | undefined;
86
+ width?: number | undefined;
87
+ latency?: number | undefined;
88
+ };
89
+ };
90
+ /**
91
+ * Convert `MediaStreamTrack` to `MediaDeviceInfoLike`
92
+ *
93
+ * @beta
94
+ */
95
+ export declare const toMediaDeviceInfoLike: (track: MediaStreamTrack) => MediaDeviceInfoLike | undefined;
96
+ /**
97
+ * Compare the provide the MediaStreamTrack and MediaDeviceInfo to see if they
98
+ * are the same with best efforts
99
+ *
100
+ * @param track - `MediaStreamTrack` used for the comparison
101
+ * @param device - `MediaDeviceInfo` used for the comparison
102
+ *
103
+ * @returns `true` if they are the same else `false`
104
+ *
105
+ * @remarks
106
+ * Old browser may not have `deviceId` from getSettings()
107
+ * https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings
108
+ * The `label` attribute will then be used for the comparison instead of the
109
+ * `deviceId`
110
+ *
111
+ * @beta
112
+ */
113
+ export declare const compareMediaDeviceToMediaTrack: (track: Pick<MediaStreamTrack, 'kind' | 'getSettings' | 'label'>) => (device: MediaDeviceInfoLike) => boolean;
114
+ export declare const toMediaDeviceInfo: (info: MediaDeviceInfoLike) => {
115
+ deviceId: string;
116
+ groupId: string;
117
+ kind: MediaDeviceKind;
118
+ label: string;
119
+ };
120
+ /**
121
+ * Find the MediaDeviceInfo from provided MediaDeviceInfo[] by comparing with
122
+ * provided MediaStreamTrack
123
+ *
124
+ * @param devices - A device list used for the searching
125
+ * @param track - A track for the searching criteria
126
+ *
127
+ * @returns `MediaDeviceInfo` if found, otherwise `undefined`
128
+ *
129
+ * @beta
130
+ */
131
+ export declare const findMediaInputFromMediaStreamTrack: (devices: MediaDeviceInfoLike[]) => (track?: MediaStreamTrack | undefined) => MediaDeviceInfoLike | undefined;
132
+ /**
133
+ * Find media input from media stream
134
+ *
135
+ * @param devices - A list of media devices from
136
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
137
+ * @param stream - A media stream used for the search criteria
138
+ *
139
+ * @returns A object may contains the devices
140
+ *
141
+ * @beta
142
+ */
143
+ export declare const findMediaInputFromStream: (devices: MediaDeviceInfoLike[]) => (stream: MediaStream | undefined) => MediaInput;
144
+ export declare const compareDeviceConstraintAndTrackDevice: (device: MediaDeviceInfoLike | undefined) => (constraint: InputConstraintSet['device']) => boolean;
145
+ export declare const isRequestedResolution: (request: InputDeviceConstraint | undefined, response: MediaDeviceInfoLike | undefined) => boolean;
146
+ export declare const isRequestedInputDevice: (request: InputDeviceConstraint | undefined, response: MediaDeviceInfoLike | undefined) => boolean;
147
+ /**
148
+ * Compare request and the input to see if the request has been fulfilled
149
+ */
150
+ export declare const isRequestedInputTrack: (request: InputDeviceConstraint | undefined, current: MediaStreamTrack | MediaDeviceInfoLike | undefined) => boolean;
151
+ export declare const hasRequestingDevice: (request: InputDeviceConstraint | undefined, kind: MediaDeviceKind, currentDevices: MediaDeviceInfoLike[]) => boolean;
152
+ /**
153
+ * Decide if we should send a new gUM request based on the inputs
154
+ *
155
+ * @param request - Requesting input device constraint
156
+ * @param tracks - The current media stream tracks
157
+ * @param currentDevices - The current list of device of the same kind of the
158
+ * request
159
+ *
160
+ * @returns `true` means the request should be conducted, otherwise `false`.
161
+ */
162
+ export declare const shouldRequestDevice: (request: InputDeviceConstraint | undefined, tracks: MediaStreamTrack[], currentDevices: MediaDeviceInfoLike[]) => boolean;
163
+ export declare const resolveInputDevice: (tracksOrDevice: MediaDeviceInfoLike | MediaStreamTrack[] | undefined, findInput: ReturnType<typeof findMediaInputFromMediaStreamTrack>) => MediaDeviceInfo | MediaDeviceInfoLike | MediaStreamTrack | undefined;
164
+ export declare const isStreamingRequestedDevicesBase: (request: MediaDeviceRequest, tracksOrDevices: {
165
+ audio?: MediaDeviceInfoLike | MediaStreamTrack[];
166
+ video?: MediaDeviceInfoLike | MediaStreamTrack[];
167
+ }, devices: MediaDeviceInfoLike[]) => {
168
+ audio: boolean;
169
+ video: boolean;
170
+ };
171
+ /**
172
+ * Check if provided request has already been fulfilled
173
+ *
174
+ * @param request - A media request constraints
175
+ * @param stream - Current media stream
176
+ *
177
+ * @returns
178
+ * audio - The stream is using the same device as requested if `true`
179
+ * video - The stream is using the same device as requested if `true`
180
+ */
181
+ export declare const isStreamingRequestedDevices: (request: MediaDeviceRequest, stream: MediaStream | undefined, devices: MediaDeviceInfoLike[]) => {
182
+ audio: boolean;
183
+ video: boolean;
184
+ };
185
+ /**
186
+ * Find in the list of devices which has permissions granted
187
+ *
188
+ * From MDN:
189
+ * For security reasons,the label field is always blank unless an active media stream
190
+ * exists or the user has granted persistent permission for media device access.
191
+ *
192
+ * @param devices - A list of media devices from
193
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
194
+ *
195
+ * @returns List of devices that has permission granted
196
+ *
197
+ * @beta
198
+ */
199
+ export declare const findPermissionGrantedDevices: (devices: MediaDeviceInfoLike[]) => MediaDeviceInfoLike[];
200
+ /**
201
+ * Use provided devices to guess the permission state. When there is no active
202
+ * stream and there is no device with label, 'prompt' will be returned
203
+ * otherwise, 'denied'.
204
+ *
205
+ * @param devices - The current devices
206
+ * @param anyActiveStream - Has ever got an active stream to help the fallback to
207
+ * guess the state more accurately
208
+ */
209
+ export declare const toPermissionState: (devices: MediaDeviceInfoLike[], anyActiveStream?: boolean) => PermissionState;
210
+ /**
211
+ * A wrapper for `navigator.permissions.query` with fallback to use
212
+ * `navigator.mediaDevices.enumerateDevices` to guess the `PermissionState`
213
+ *
214
+ * @param anyActiveStream - Has ever got an active stream to help the fallback to
215
+ * guess the state more accurately
216
+ */
217
+ export declare const getInputDevicePermissionState: (anyActiveStream?: boolean) => Promise<InputDevicePermission>;
218
+ /**
219
+ * Find current audio output id to be used to set as sinkId
220
+ *
221
+ * If you set the stale deviceId to setSink it will throw exception.
222
+ * So we want to check if audio output id still exist.
223
+ *
224
+ * @param audioOutput - Audio output as `MediaDeviceInfo`
225
+ * @param devices - A list of media devices from
226
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
227
+ *
228
+ * @returns Audio output id or empty string if couldn't find it.
229
+ *
230
+ * @beta
231
+ */
232
+ export declare const findCurrentAudioOutputId: (audioOutput?: MediaDeviceInfoLike, devices?: MediaDeviceInfoLike[]) => string;
233
+ /**
234
+ * Find videoinput device id in the stream
235
+ *
236
+ * @param stream - Media stream to do the lookup
237
+ *
238
+ * @returns A object may contains the devices
239
+ *
240
+ * @beta
241
+ */
242
+ export declare const findCurrentVideoInputDeviceIdFromStream: (stream: MediaStream) => string | undefined;
243
+ /**
244
+ * Finds device with given deviceId
245
+ *
246
+ * @param devices - A list of media devices from
247
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
248
+ * @param deviceId - id that represents desired device
249
+ *
250
+ * @returns device with given deviceId
251
+ *
252
+ * @beta
253
+ */
254
+ export declare const findDeviceWithDeviceId: (devices: MediaDeviceInfoLike[], deviceId: string) => MediaDeviceInfoLike | undefined;
255
+ /**
256
+ * Find in the list of devices with given MediaDeviceKind
257
+ *
258
+ * @param devices - A list of media devices from
259
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
260
+ * @param kind - A list of media devices from
261
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
262
+ *
263
+ * @returns List of devices that have required MediaDeviceKind
264
+ *
265
+ * @beta
266
+ */
267
+ export declare const findDevicesByKind: (kind: MediaDeviceKind) => (devices: MediaDeviceInfoLike[]) => MediaDeviceInfoLike[];
268
+ /**
269
+ * Find in the list of devices only the audio input ones
270
+ *
271
+ * @param devices - A list of media devices from
272
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
273
+ *
274
+ * @returns List of devices that are audio inputs
275
+ *
276
+ * @beta
277
+ */
278
+ export declare const findAudioInputDevices: (devices: MediaDeviceInfoLike[]) => MediaDeviceInfoLike[];
279
+ /**
280
+ * Find in the list of devices only the video input ones
281
+ *
282
+ * @param devices - A list of media devices from
283
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
284
+ *
285
+ * @returns List of devices that are video inputs
286
+ *
287
+ * @beta
288
+ */
289
+ export declare const findVideoInputDevices: (devices: MediaDeviceInfoLike[]) => MediaDeviceInfoLike[];
290
+ /**
291
+ * Find in the list of devices only the audio output ones
292
+ *
293
+ * @param devices - A list of media devices from
294
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
295
+ *
296
+ * @returns List of devices that are audio outputs
297
+ *
298
+ * @beta
299
+ */
300
+ export declare const findAudioOutputDevices: (devices: MediaDeviceInfoLike[]) => MediaDeviceInfoLike[];
301
+ /**
302
+ * Set `MediaStreamTrack['enabled']` according to `mute` param for the provided `stream`
303
+ *
304
+ * @param stream - Media stream
305
+ * @param mute - disable or enable the audio stream
306
+ * @param mediaType - Can either be 'audio', 'video' or 'all'
307
+ * @defaultValue
308
+ * 'all'
309
+ *
310
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/enabled
311
+ *
312
+ * @beta
313
+ */
314
+ export declare const muteStreamTrack: (stream?: MediaStream) => (mute: boolean, mediaType?: 'audio' | 'video' | 'all') => void;
315
+ /**
316
+ * Stops all tracks in the given stream
317
+ *
318
+ * Immediately after calling stop(), the readyState property is set to `ended`.
319
+ * Note that the `ended` event will not be fired in this situation
320
+ * https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/stop#description
321
+ *
322
+ * @param stream - `MediaStream` which we mutate
323
+ * @param onStopped - callback to be called when the track is stopped
324
+ */
325
+ export declare const stopMediaStream: (stream: MediaStream | undefined, onStopped?: ((track: MediaStreamTrack) => void) | undefined) => void;
326
+ /**
327
+ * Checks that tracks with a given type are enabled in the stream
328
+ *
329
+ * @param stream - `MediaStream` used for comparison
330
+ * @param type - `MediaInput` used for comparison
331
+ *
332
+ * @returns Return true when all the tracks are enabled false otherwise
333
+ *
334
+ * @beta
335
+ */
336
+ export declare const areTracksEnabled: (stream: MediaStream | undefined, type: 'audio' | 'video') => boolean;
337
+ /**
338
+ * Check if list contains media inputs
339
+ *
340
+ * @param devices - A list of media devices from
341
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
342
+ *
343
+ * @returns True when some media inputs false otherwise
344
+ *
345
+ * @beta
346
+ */
347
+ export declare const hasAudioOrVideoInputs: (devices: MediaDeviceInfoLike[]) => boolean;
348
+ /**
349
+ * Check if list contains audio inputs
350
+ *
351
+ * @param devices - A list of media devices from
352
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
353
+ *
354
+ * @returns True when some audio inputs false otherwise
355
+ *
356
+ * @beta
357
+ */
358
+ export declare const hasAudioInputs: (devices: MediaDeviceInfoLike[]) => boolean;
359
+ /**
360
+ * Check if list contains video inputs
361
+ *
362
+ * @param devices - A list of media devices from
363
+ * `navigator.mediaDevices.enumerateDevices()`. Will be used for search target
364
+ *
365
+ * @returns True when some video inputs false otherwise
366
+ *
367
+ * @beta
368
+ */
369
+ export declare const hasVideoInputs: (devices: MediaDeviceInfoLike[]) => boolean;
370
+ /**
371
+ * Check if provided devices have any granted input device
372
+ *
373
+ * @param devices - The devices to check
374
+ */
375
+ export declare const hasAnyGrantedInput: (devices: MediaDeviceInfoLike[]) => boolean;
376
+ /**
377
+ * Check if provided devices have any video and audio inputs
378
+ *
379
+ * @param devices - The devices to lookup
380
+ * @param grantedOnly - When it is `true`, the device label is also taken into
381
+ * consideration
382
+ * @defaultValue `false`
383
+ *
384
+ * @returns A tuple of `[anyAudioInput, anyVideoInput]`, e.g. `[true, false]`
385
+ * means there is audio input but no video input
386
+ */
387
+ export declare const hasAnyInputs: (devices: MediaDeviceInfoLike[], grantedOnly?: boolean) => [boolean, boolean];
388
+ /**
389
+ * An utility function to check the input device has been changed
390
+ *
391
+ * @param oldInput - The previous input device
392
+ * @param newInput - The current input device
393
+ */
394
+ export declare const hasChangedInput: (oldInput: MediaDeviceInfoLike | undefined, newInput: MediaDeviceInfoLike | undefined) => boolean;
395
+ /**
396
+ * An utility function to check if facing mode is supported by interpreting the
397
+ * device label and the API `getSupportedConstraints`
398
+ *
399
+ * @remarks
400
+ * Only support snooping the label for English
401
+ *
402
+ * @param currentDevices - Current list of devices available
403
+ * @param getSupportedConstraints - A function to get supported constraints,
404
+ * i.e. `navigator.mediaDevices.getSupportedConstraints()`
405
+ * @param tracks - Current video input track
406
+ */
407
+ export declare const areMultipleFacingModeSupported: (currentDevices: MediaDeviceInfoLike[], getSupportedConstraints?: () => MediaTrackSupportedConstraints) => boolean;
408
+ /**
409
+ * Interpret the current facing mode from the provided track, and try to get the
410
+ * mode from settings, or use the label to guess the facing mode when facingMode
411
+ * is not supported from settings
412
+ *
413
+ * @param currentTrack - The current video input track
414
+ */
415
+ export declare const interpretCurrentFacingMode: (currentTrack: MediaStreamTrack | undefined) => FacingMode | undefined;
416
+ export {};