@octane-xplat/camera 0.18.0 → 0.20.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.
Files changed (67) hide show
  1. package/README.md +2 -132
  2. package/package.json +108 -103
  3. package/platforms/android/java/com/octanexplat/camera/CameraPreviewView.java +24 -0
  4. package/platforms/android/java/com/octanexplat/camera/CameraSessionBridge.java +153 -21
  5. package/platforms/ios/src/XplatSwitchingMovieWriter.swift +557 -0
  6. package/src/CameraView.android.tsrx +194 -118
  7. package/src/CameraView.ios.tsrx +129 -76
  8. package/src/CameraView.macos.tsrx +108 -70
  9. package/src/CameraView.tsrx +2 -0
  10. package/src/CameraView.web.tsrx +160 -125
  11. package/src/CameraView.windows.tsrx +141 -92
  12. package/src/camera-host.macos.ts +1059 -0
  13. package/src/camera-permission.mobile.ts +85 -81
  14. package/src/camera-permission.windows.ts +56 -53
  15. package/src/camera-session.ios.ts +89 -61
  16. package/src/camera-session.macos.ts +51 -49
  17. package/src/camera-session.windows.ts +113 -105
  18. package/src/host-movie.web.ts +266 -222
  19. package/src/index.ts +28 -26
  20. package/src/index.web.ts +27 -25
  21. package/src/layout-child.ts +88 -79
  22. package/src/layout-child.web.ts +57 -51
  23. package/src/layout-child.windows.ts +82 -71
  24. package/src/movie-metadata.web.ts +87 -74
  25. package/src/movie-storage.web.ts +142 -135
  26. package/src/movie-writer.ios.ts +170 -0
  27. package/src/photo-lifecycle.macos.ts +60 -0
  28. package/src/photo-sheet.macos.ts +505 -0
  29. package/src/preview-display.ios.ts +69 -0
  30. package/src/preview-pinch.ts +41 -0
  31. package/src/preview-tap.ts +22 -21
  32. package/src/props.ts +118 -68
  33. package/src/recording-haptics.ios.ts +39 -0
  34. package/src/recording-source.web.ts +159 -0
  35. package/src/session-backend.android.ts +552 -410
  36. package/src/session-backend.ios.ts +1500 -1148
  37. package/src/session-backend.linux.ts +886 -860
  38. package/src/session-backend.macos.ts +697 -679
  39. package/src/session-backend.ts +51 -49
  40. package/src/session-backend.web.ts +1181 -867
  41. package/src/session-backend.windows.ts +1556 -1467
  42. package/src/session-core.ts +925 -711
  43. package/src/session.ts +12 -12
  44. package/src/style-merge.ts +43 -43
  45. package/src/synthetic-session.ts +392 -333
  46. package/src/types.ts +384 -239
  47. package/types/generated/.tsrx-typegen-manifest.json +18 -0
  48. package/types/generated/CameraView.d.ts +2 -0
  49. package/types/generated/CameraView.web.d.ts +4 -0
  50. package/types/generated/camera-host.macos.d.ts +33 -0
  51. package/types/generated/index.d.ts +34 -0
  52. package/types/generated/layout-child.web.d.ts +7 -0
  53. package/types/generated/photo-lifecycle.macos.d.ts +12 -0
  54. package/types/generated/photo-sheet.macos.d.ts +36 -0
  55. package/types/generated/props.d.ts +111 -0
  56. package/types/generated/session-backend.d.ts +6 -0
  57. package/types/{session-core.d.ts → generated/session-core.d.ts} +30 -1
  58. package/types/generated/session.d.ts +16 -0
  59. package/types/{synthetic-session.d.ts → generated/synthetic-session.d.ts} +15 -1
  60. package/types/{types.d.ts → generated/types.d.ts} +145 -2
  61. package/LICENSE +0 -21
  62. package/platforms/macos/src/CameraOwnership.swift +0 -22
  63. package/platforms/macos/src/PhotoCaptureLifecycle.swift +0 -34
  64. package/platforms/macos/src/XplatCameraHost.swift +0 -639
  65. package/platforms/macos/src/XplatPhotoCaptureSheet.swift +0 -192
  66. package/types/index.d.ts +0 -48
  67. package/types/props.d.ts +0 -61
@@ -1,28 +1,28 @@
1
1
  import { CameraCaptureError } from './types'
2
2
  import {
3
- cameraHost,
4
- commitHostMovie,
5
- openHostMovie,
6
- removeHostReservation,
7
- reserveHostMovie,
8
- type CameraHost,
3
+ cameraHost,
4
+ commitHostMovie,
5
+ openHostMovie,
6
+ removeHostReservation,
7
+ reserveHostMovie,
8
+ type CameraHost,
9
9
  } from './host-movie.web'
10
10
 
11
11
  import { inspectMovie } from './movie-metadata.web'
12
12
  import type {
13
- BackendCaptureSink,
14
- BackendObserver,
15
- BackendPreviewListener,
16
- CameraSessionBackend,
13
+ BackendCaptureSink,
14
+ BackendObserver,
15
+ BackendPreviewListener,
16
+ CameraSessionBackend,
17
17
  } from './session-core'
18
18
 
19
19
  import type {
20
- CameraPermissionKind,
21
- CameraPermissionStatus,
22
- CameraSessionConfig,
23
- CaptureIssue,
24
- EffectiveCaptureConfig,
25
- MovieClip,
20
+ CameraPermissionKind,
21
+ CameraPermissionStatus,
22
+ CameraSessionConfig,
23
+ CaptureIssue,
24
+ EffectiveCaptureConfig,
25
+ MovieClip,
26
26
  } from './types'
27
27
 
28
28
  // The probe order mirrors the browser adapter; shipped WebKitGTK hosts may
@@ -30,858 +30,884 @@ import type {
30
30
  // encoder plugins, and MP4 needs the h264/aac encoder + parser plugins). The
31
31
  // finalized container is always reported, never assumed.
32
32
  const mimeCandidates = [
33
- 'video/webm;codecs=vp8,opus',
34
- 'video/webm;codecs=vp9,opus',
35
- 'video/mp4;codecs=avc1.424028,mp4a.40.2',
36
- 'video/mp4',
33
+ 'video/webm;codecs=vp8,opus',
34
+ 'video/webm;codecs=vp9,opus',
35
+ 'video/mp4;codecs=avc1.424028,mp4a.40.2',
36
+ 'video/mp4',
37
37
  ]
38
38
 
39
39
  let owner: object | undefined
40
40
 
41
41
  const error = (kind: CaptureIssue['kind'], operation: string, message: string, cause?: unknown) =>
42
- new CameraCaptureError({ kind, operation, message, cause })
42
+ new CameraCaptureError({ kind, operation, message, cause })
43
43
 
44
44
  const policyAllows = (kind: CameraPermissionKind) => {
45
- const policy =
46
- (
47
- document as Document & {
48
- permissionsPolicy?: { allowsFeature(feature: string): boolean }
49
- featurePolicy?: { allowsFeature(feature: string): boolean }
50
- }
51
- ).permissionsPolicy ?? (document as any).featurePolicy
52
-
53
- return policy?.allowsFeature(kind) !== false
45
+ const policy =
46
+ (
47
+ document as Document & {
48
+ permissionsPolicy?: { allowsFeature(feature: string): boolean }
49
+ featurePolicy?: { allowsFeature(feature: string): boolean }
50
+ }
51
+ ).permissionsPolicy ?? (document as any).featurePolicy
52
+
53
+ return policy?.allowsFeature(kind) !== false
54
54
  }
55
55
 
56
56
  export function createSessionBackend(config: CameraSessionConfig): CameraSessionBackend {
57
- let currentConfig = { ...config }
58
- let observer: BackendObserver | undefined
59
- let host: HTMLVideoElement | undefined
60
- let listener: BackendPreviewListener | undefined
61
- let stream: MediaStream | undefined
62
- let ready = false
63
- let released = false
64
- let generation = 0
65
- let recorder: MediaRecorder | undefined
66
- let sink: BackendCaptureSink | undefined
67
- let startedAt = 0
68
- let stoppedAt = 0
69
- let stopTimer: ReturnType<typeof setTimeout> | undefined
70
- let watchdog: ReturnType<typeof setTimeout> | undefined
71
- let recordingAudio = false
72
- let captureFailure: ((cause: unknown) => Promise<void>) | undefined
73
- // Packaged Linux WebKitGTK host probe: 'unknown' until the capability
74
- // request settles; 'available' means the host owns media permission
75
- // decisions and app-private movie storage for this session.
76
- let hostState: 'unknown' | 'available' | 'absent' = 'unknown'
77
- const hostProbe = cameraHost().then((host) => {
78
- hostState = host ? 'available' : 'absent'
79
- return host
80
- })
81
-
82
- const lease = {}
83
- const known: Record<CameraPermissionKind, CameraPermissionStatus> = {
84
- camera: 'unknown',
85
- microphone: 'unknown',
86
- }
87
-
88
- // The packaged Linux host is mandatory: it owns media authorization and
89
- // durable app-private storage — there is no weaker fallback origin store.
90
- const supported = () =>
91
- typeof navigator !== 'undefined' &&
92
- globalThis.isSecureContext === true &&
93
- !!navigator.mediaDevices?.getUserMedia &&
94
- typeof MediaRecorder !== 'undefined' &&
95
- !!mimeType() &&
96
- hostState === 'available'
97
-
98
- const mimeType = () =>
99
- typeof MediaRecorder === 'undefined'
100
- ? ''
101
- : (mimeCandidates.find((mime) => MediaRecorder.isTypeSupported(mime)) ?? '')
102
-
103
- const markReady = (value: boolean) => {
104
- ready = value
105
- observer?.previewReady(value)
106
- }
107
-
108
- const dropStream = () => {
109
- if (host) {
110
- host.srcObject = null
111
- }
112
-
113
- stream?.getTracks().forEach((track) => track.stop())
114
- stream = undefined
115
- markReady(false)
116
- if (owner === lease) {
117
- owner = undefined
118
- }
119
- }
120
-
121
- const onVisibility = () => {
122
- if (document.visibilityState === 'hidden') {
123
- markReady(false)
124
- observer?.interruption('began', 'background')
125
- } else {
126
- observer?.interruption('ended', 'background')
127
- markReady(!!host && stream?.getVideoTracks()[0]?.readyState === 'live')
128
- }
129
- }
130
-
131
- const onPageHide = () => {
132
- markReady(false)
133
- observer?.interruption('began', 'background')
134
- }
135
-
136
- // Track-caused interruptions (ended/mute) need an explicit 'ended' —
137
- // a fresh live stream resolves them, and without the event the core
138
- // keeps interrupted=true until the session is recreated.
139
- let deviceInterrupted = false
140
-
141
- const observeTracks = (target: MediaStream) => {
142
- for (const track of target.getTracks()) {
143
- track.addEventListener('ended', () => {
144
- if (track.kind === 'audio' && (!sink || !recordingAudio)) {
145
- return
146
- }
147
-
148
- if (stream !== target) {
149
- return
150
- }
151
-
152
- markReady(false)
153
- deviceInterrupted = true
154
- observer?.availability(false)
155
- observer?.interruption('began', 'unknown')
156
- })
157
-
158
- track.addEventListener('mute', () => {
159
- if (track.kind === 'audio' && (!sink || !recordingAudio)) {
160
- return
161
- }
162
-
163
- if (stream === target) {
164
- markReady(false)
165
- deviceInterrupted = true
166
- observer?.interruption('began', 'unknown')
167
- }
168
- })
169
-
170
- track.addEventListener('unmute', () => {
171
- if (stream === target && document.visibilityState !== 'hidden') {
172
- deviceInterrupted = false
173
- observer?.interruption('ended', 'unknown')
174
- observer?.availability(true)
175
- markReady(!!host)
176
- }
177
- })
178
- }
179
- }
180
-
181
- const constraints = (): MediaTrackConstraints => {
182
- const camera = currentConfig.camera
183
- return {
184
- width: { ideal: 1280 },
185
- height: { ideal: 720 },
186
- frameRate: { ideal: 30 },
187
- ...(typeof camera === 'object'
188
- ? { deviceId: { exact: camera.deviceId } }
189
- : camera === 'front' || camera === 'back'
190
- ? { facingMode: { exact: camera === 'front' ? 'user' : 'environment' } }
191
- : {}),
192
- }
193
- }
194
-
195
- let acquisition: Promise<void> | undefined
196
- const acquireCameraOnce = async () => {
197
- if (stream?.getVideoTracks()[0]?.readyState === 'live') {
198
- return
199
- }
200
-
201
- if (owner && owner !== lease) {
202
- throw error('busy', 'preview', 'Another camera session still owns the source')
203
- }
204
-
205
- owner = lease
206
- const epoch = generation
207
- try {
208
- const acquired = await navigator.mediaDevices.getUserMedia({
209
- video: constraints(),
210
- audio: false,
211
- })
212
-
213
- if (released || epoch !== generation) {
214
- acquired.getTracks().forEach((track) => track.stop())
215
- if (!stream && owner === lease) {
216
- owner = undefined
217
- }
218
-
219
- return
220
- }
221
-
222
- stream = acquired
223
- known.camera = 'granted'
224
- observeTracks(acquired)
225
- if (deviceInterrupted) {
226
- deviceInterrupted = false
227
- observer?.interruption('ended', 'unknown')
228
- }
229
-
230
- observer?.availability(true)
231
- } catch (cause) {
232
- if (owner === lease) {
233
- owner = undefined
234
- }
235
-
236
- throw cause
237
- }
238
- }
239
-
240
- const acquireCamera = () => {
241
- acquisition ??= acquireCameraOnce().finally(() => {
242
- acquisition = undefined
243
- })
244
-
245
- return acquisition
246
- }
247
-
248
- // WebKitGTK's GStreamer recorder loses the video pipeline when an audio
249
- // track from a separate acquisition joins a video track that has already
250
- // recorded — audio must come from one combined getUserMedia acquisition.
251
- // When audio is required, swap the previewed stream for a combined one on
252
- // the same camera selection.
253
- const acquireCombined = async () => {
254
- const epoch = generation
255
- const acquired = await navigator.mediaDevices.getUserMedia({
256
- video: constraints(),
257
- audio: true,
258
- })
259
-
260
- if (released || epoch !== generation) {
261
- acquired.getTracks().forEach((track) => track.stop())
262
- return
263
- }
264
-
265
- const previous = stream
266
- stream = acquired
267
- previous?.getTracks().forEach((track) => track.stop())
268
- observeTracks(acquired)
269
- if (deviceInterrupted) {
270
- deviceInterrupted = false
271
- observer?.interruption('ended', 'unknown')
272
- }
273
-
274
- observer?.availability(true)
275
- }
276
-
277
- const ensureAudio = async () => {
278
- if (stream?.getAudioTracks().some((track) => track.readyState === 'live')) {
279
- return
280
- }
281
-
282
- if (stream) {
283
- await acquireCombined()
284
- if (host) {
285
- await showPreview()
286
- }
287
-
288
- return
289
- }
290
-
291
- // No preview yet — a standalone acquisition proves the grant; the
292
- // combined swap happens once the camera stream exists.
293
- const audio = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
294
- audio.getTracks().forEach((track) => track.stop())
295
- }
296
-
297
- const showPreview = async () => {
298
- if (!host || released) {
299
- return
300
- }
301
-
302
- const element = host
303
- const epoch = generation
304
- if ((await checkPermission('camera')) !== 'granted') {
305
- listener?.onError?.(
306
- error(
307
- 'permissionBlocked',
308
- 'preview',
309
- 'Request camera access from a user action before activating this preview',
310
- ),
311
- )
312
-
313
- return
314
- }
315
-
316
- await acquireCamera()
317
- if (host !== element || !stream || released || epoch !== generation) {
318
- return
319
- }
320
-
321
- element.srcObject = stream
322
- element.muted = true
323
- const facing = stream.getVideoTracks()[0]?.getSettings().facingMode
324
- element.style.transform = facing === 'user' ? 'scaleX(-1)' : ''
325
- await element.play()
326
- if (host !== element || released || epoch !== generation) {
327
- return
328
- }
329
-
330
- markReady(document.visibilityState !== 'hidden')
331
- listener?.onReady?.()
332
- }
333
-
334
- const hostMediaKind = (kind: CameraPermissionKind) =>
335
- kind === 'microphone' ? 'microphone' : 'camera'
336
-
337
- async function checkPermission(kind: CameraPermissionKind): Promise<CameraPermissionStatus> {
338
- if (globalThis.isSecureContext !== true || !navigator.mediaDevices?.getUserMedia) {
339
- return 'unavailable'
340
- }
341
-
342
- if (!policyAllows(kind)) {
343
- return 'blocked'
344
- }
345
-
346
- const desktop = await hostProbe
347
- if (!desktop) {
348
- return 'unavailable'
349
- }
350
-
351
- try {
352
- const { status } = await desktop.call('camera', 'permissionStatus', {
353
- kind: hostMediaKind(kind),
354
- })
355
-
356
- if (status === 'granted' || status === 'notDetermined') {
357
- return status
358
- }
359
-
360
- if (status === 'restricted') {
361
- return 'restricted'
362
- }
363
-
364
- if (status === 'denied' || status === 'blocked') {
365
- return 'denied'
366
- }
367
-
368
- // 'undeclared' and unknown statuses stay honest but unadorned.
369
- return 'unknown'
370
- } catch {
371
- return 'unknown'
372
- }
373
- }
374
-
375
- const settle = (value: Parameters<BackendCaptureSink['settled']>[0], expected = sink) => {
376
- const target = sink
377
- if (!target || target !== expected) {
378
- return
379
- }
380
-
381
- sink = undefined
382
- recorder = undefined
383
- captureFailure = undefined
384
- clearTimeout(stopTimer)
385
- clearTimeout(watchdog)
386
- if (!host) {
387
- dropStream()
388
- }
389
-
390
- target.settled(value)
391
- }
392
-
393
- const stop = () => {
394
- if (!recorder || !sink) {
395
- return
396
- }
397
-
398
- stoppedAt ||= performance.now()
399
- clearTimeout(watchdog)
400
- const target = sink
401
- const fail = captureFailure
402
- watchdog = setTimeout(() => {
403
- if (sink !== target) {
404
- return
405
- }
406
-
407
- dropStream()
408
- void fail?.(error('finalizationFailed', 'stop', 'Recorder finalization callback timed out'))
409
- }, 30_000)
410
-
411
- if (recorder.state === 'inactive') {
412
- return
413
- }
414
-
415
- try {
416
- recorder.stop()
417
- } catch (cause) {
418
- void fail?.(error('captureFailed', 'stop', 'The recorder could not stop', cause))
419
- }
420
- }
421
-
422
- return {
423
- platform: 'web',
424
- get supported() {
425
- return supported()
426
- },
427
- checkPermission,
428
- requestPermission: async (kind) => {
429
- const status = await checkPermission(kind)
430
- if (status === 'blocked' || status === 'unavailable') {
431
- return status
432
- }
433
-
434
- const desktop = await hostProbe
435
- if (!desktop) {
436
- return 'unavailable'
437
- }
438
-
439
- const { status: hostStatus } = await desktop.call('camera', 'requestPermission', {
440
- kind: hostMediaKind(kind),
441
- })
442
-
443
- if (hostStatus !== 'granted') {
444
- known[kind] =
445
- hostStatus === 'denied' || hostStatus === 'blocked'
446
- ? 'denied'
447
- : hostStatus === 'restricted'
448
- ? 'restricted'
449
- : 'unknown'
450
-
451
- return known[kind]
452
- }
453
-
454
- // The host granted the request — its permission-request handler now
455
- // allows this app's acquisition without another decision.
456
-
457
- try {
458
- if (kind === 'camera') {
459
- await acquireCamera()
460
- } else if (sink) {
461
- // Mid-take the recording stream cannot be swapped; prove the
462
- // grant only — audio joins on the next take.
463
- const audio = await navigator.mediaDevices.getUserMedia({
464
- audio: true,
465
- video: false,
466
- })
467
-
468
- audio.getTracks().forEach((track) => track.stop())
469
- } else {
470
- await ensureAudio()
471
- }
472
-
473
- known[kind] = 'granted'
474
- if (kind === 'camera' && host) {
475
- await showPreview()
476
- }
477
-
478
- return 'granted'
479
- } catch (cause) {
480
- known[kind] =
481
- cause instanceof DOMException &&
482
- (cause.name === 'NotAllowedError' || cause.name === 'SecurityError')
483
- ? 'blocked'
484
- : 'unavailable'
485
-
486
- return known[kind]
487
- }
488
- },
489
- getCapabilities: async () => {
490
- const desktop = await hostProbe
491
- const devices = (await navigator.mediaDevices?.enumerateDevices?.().catch(() => [])) ?? []
492
- const settings = stream?.getVideoTracks()[0]?.getSettings()
493
- const mime = mimeType()
494
- const storageReady = !!desktop
495
- return {
496
- supported: supported(),
497
- available: ready && storageReady,
498
- reason: !supported()
499
- ? 'Secure media capture, a supported WebM/MP4 codec, and the packaged Linux host are required'
500
- : !storageReady
501
- ? 'Movie output requires the packaged Linux WebKitGTK host'
502
- : !ready
503
- ? 'An active permitted preview is required'
504
- : undefined,
505
- cameras: devices
506
- .filter((device) => device.kind === 'videoinput')
507
- .map((device) => ({ id: device.deviceId, label: device.label })),
508
- profiles:
509
- settings?.width && settings.height && settings.frameRate
510
- ? [
511
- {
512
- profile: 'standard' as const,
513
- width: settings.width,
514
- height: settings.height,
515
- frameRate: settings.frameRate,
516
- },
517
- ]
518
- : [],
519
- audio: devices.some((device) => device.kind === 'audioinput'),
520
- // No point-of-interest metering in the WebKitGTK path.
521
- focusPointOfInterest: false,
522
- exposurePointOfInterest: false,
523
- orientations: [],
524
- durationLimit: 'bestEffort',
525
- elapsedTime: 'estimated',
526
- output: {
527
- mimeType: mime,
528
- container: !mime ? '' : mime.startsWith('video/mp4') ? 'mp4' : 'webm',
529
- storage: desktop ? 'appPrivateFile' : 'originLocal',
530
- destinationFileUrl: !!desktop,
531
- },
532
- }
533
- },
534
- previewReady: () => ready && !!host && !released,
535
- validateStart: (options) => {
536
- // A pending host probe defers checks to the async start path; once
537
- // the probe settles absent, recording cannot produce durable output.
538
- if (hostState === 'absent') {
539
- throw error(
540
- 'destinationUnavailable',
541
- 'startRecording',
542
- 'Movie output requires the packaged Linux WebKitGTK host',
543
- )
544
- }
545
-
546
- if (options.orientation && options.orientation !== 'unspecified') {
547
- throw error(
548
- 'unsupportedConfiguration',
549
- 'startRecording',
550
- 'This browser cannot lock a recording orientation',
551
- )
552
- }
553
- },
554
- attachPreview: (target, callbacks) => {
555
- if (released || sink) {
556
- throw error('invalidState', 'attachPreview', 'The camera is disposed or still finalizing')
557
- }
558
-
559
- host = target as HTMLVideoElement
560
- listener = callbacks
561
- document.addEventListener('visibilitychange', onVisibility)
562
- globalThis.addEventListener('pagehide', onPageHide)
563
- void showPreview().catch((cause) =>
564
- callbacks?.onError?.(
565
- error('unavailable', 'preview', 'Could not activate camera preview', cause),
566
- ),
567
- )
568
-
569
- return () => {
570
- if (host) {
571
- host.srcObject = null
572
- }
573
-
574
- host = undefined
575
- listener = undefined
576
- generation += 1
577
- markReady(false)
578
- document.removeEventListener('visibilitychange', onVisibility)
579
- globalThis.removeEventListener('pagehide', onPageHide)
580
- if (!sink) {
581
- dropStream()
582
- }
583
- }
584
- },
585
- configure: (next) => {
586
- const sourceChanged =
587
- JSON.stringify(next.camera) !== JSON.stringify(currentConfig.camera) ||
588
- next.profile !== currentConfig.profile
589
-
590
- currentConfig = { ...next }
591
- if (sourceChanged) {
592
- generation += 1
593
- dropStream()
594
- if (host) {
595
- void showPreview().catch((cause) =>
596
- listener?.onError?.(
597
- error('unavailable', 'configure', 'Could not change camera', cause),
598
- ),
599
- )
600
- }
601
- }
602
- },
603
- startCapture: (request, target) => {
604
- sink = target
605
- recordingAudio = request.audio
606
- startedAt = 0
607
- stoppedAt = 0
608
- let desktop: CameraHost | null = null
609
- let reservation: string | undefined
610
- let stage: 'preparation' | 'capture' | 'finalization' | 'storage' = 'preparation'
611
- const chunks: Blob[] = []
612
- let captureError: unknown
613
- let limitReached = false
614
- let failing = false
615
- let didStart = false
616
- captureFailure = fail
617
- void (async () => {
618
- try {
619
- desktop = await hostProbe
620
- if (!desktop) {
621
- throw error(
622
- 'destinationUnavailable',
623
- 'startRecording',
624
- 'Movie output requires the packaged Linux WebKitGTK host',
625
- )
626
- }
627
-
628
- if (request.audio) {
629
- // A take never prompts — microphone must already be granted;
630
- // the combined swap below needs the grant to exist first.
631
- if ((await checkPermission('microphone')) !== 'granted') {
632
- throw error(
633
- 'permissionDenied',
634
- 'startRecording',
635
- 'Requested microphone access is missing',
636
- )
637
- }
638
-
639
- await ensureAudio().catch((cause) => {
640
- throw error(
641
- 'captureFailed',
642
- 'startRecording',
643
- 'Could not acquire microphone for recording',
644
- cause,
645
- )
646
- })
647
- }
648
-
649
- const source = stream
650
- const video = source?.getVideoTracks()[0]
651
- const audio =
652
- source?.getAudioTracks().filter((track) => track.readyState === 'live') ?? []
653
-
654
- if (!video || (request.audio && !audio.length)) {
655
- throw error(
656
- request.audio ? 'permissionDenied' : 'unavailable',
657
- 'startRecording',
658
- request.audio
659
- ? 'Requested microphone access is missing'
660
- : 'Video source is unavailable',
661
- )
662
- }
663
-
664
- const recordingStream = new MediaStream([video, ...(request.audio ? audio : [])])
665
-
666
- reservation = (
667
- await reserveHostMovie(desktop, {
668
- destinationFileUrl: request.destinationPath
669
- ? `file://${request.destinationPath.split('/').map(encodeURIComponent).join('/')}`
670
- : undefined,
671
- container: mimeType().startsWith('video/mp4') ? 'mp4' : 'webm',
672
- })
673
- ).fileUrl
674
-
675
- if (!source || video.readyState !== 'live' || document.visibilityState === 'hidden') {
676
- throw error(
677
- 'unavailable',
678
- 'startRecording',
679
- 'Camera source became inactive before recording',
680
- )
681
- }
682
-
683
- const activeRecorder = new MediaRecorder(recordingStream, { mimeType: mimeType() })
684
- recorder = activeRecorder
685
- activeRecorder.ondataavailable = (event) => {
686
- if (sink !== target || failing) {
687
- return
688
- }
689
-
690
- if (event.data.size) {
691
- chunks.push(event.data)
692
- }
693
- }
694
-
695
- activeRecorder.onerror = (event) => {
696
- if (sink !== target || failing) {
697
- return
698
- }
699
-
700
- captureError = event
701
- stop()
702
- }
703
-
704
- activeRecorder.onstart = () => {
705
- if (sink !== target || failing) {
706
- return
707
- }
708
-
709
- if (didStart) {
710
- return
711
- }
712
-
713
- didStart = true
714
- clearTimeout(watchdog)
715
- startedAt = performance.now()
716
- stage = 'capture'
717
- const settings = video.getSettings()
718
- const effective: EffectiveCaptureConfig = {
719
- cameraId: settings.deviceId ?? '',
720
- audio: request.audio,
721
- width: settings.width ?? 0,
722
- height: settings.height ?? 0,
723
- frameRate: settings.frameRate ?? 0,
724
- orientation: 'unspecified',
725
- mimeType: activeRecorder.mimeType,
726
- container: activeRecorder.mimeType.startsWith('video/mp4') ? 'mp4' : 'webm',
727
- }
728
-
729
- // The adapter owns a limit even when capabilities() was not called.
730
- if (request.maximumDurationMs) {
731
- const limit = request.maximumDurationMs
732
- const checkLimit = () => {
733
- if (sink !== target || failing) {
734
- return
735
- }
736
-
737
- const remaining = limit - (performance.now() - startedAt)
738
- if (remaining > 0) {
739
- stopTimer = setTimeout(checkLimit, Math.min(remaining, 2_147_483_647))
740
- } else {
741
- limitReached = true
742
- stop()
743
- }
744
- }
745
-
746
- stopTimer = setTimeout(checkLimit, Math.min(limit, 2_147_483_647))
747
- }
748
-
749
- target.started(effective)
750
- }
751
-
752
- activeRecorder.onstop = () => {
753
- if (sink !== target || failing) {
754
- return
755
- }
756
-
757
- target.finishing(
758
- captureError ? 'captureError' : limitReached ? 'maximumDuration' : undefined,
759
- )
760
-
761
- stoppedAt ||= performance.now()
762
- clearTimeout(stopTimer)
763
- clearTimeout(watchdog)
764
- void (async () => {
765
- try {
766
- stage = 'finalization'
767
- if (!startedAt) {
768
- throw error('noMedia', 'finalize', 'No recording start was observed')
769
- }
770
-
771
- const bytes = new Blob(chunks, { type: activeRecorder.mimeType })
772
- if ((globalThis as any).__xplatDebugMovie) {
773
- ;(globalThis as any).__xplatLastMovieBlob = bytes
774
- }
775
-
776
- const metadata = await inspectMovie(bytes, request.audio)
777
- if (sink !== target || failing) {
778
- return
779
- }
780
-
781
- stage = 'storage'
782
- const output = await commitHostMovie(desktop!, reservation!, bytes)
783
- const clip: MovieClip = { ...metadata, output }
784
-
785
- settle(
786
- {
787
- kind: 'clip',
788
- ...clip,
789
- limitReached,
790
- warning: captureError
791
- ? error(
792
- 'captureFailed',
793
- 'capture',
794
- 'Recorder reported an error; finalized media was verified',
795
- captureError,
796
- )
797
- : undefined,
798
- },
799
- target,
800
- )
801
- } catch (cause) {
802
- await fail(cause)
803
- }
804
- })()
805
- }
806
-
807
- activeRecorder.start(1000)
808
- watchdog = setTimeout(() => {
809
- if (!startedAt && sink === target) {
810
- stop()
811
- void fail(
812
- error('captureFailed', 'startRecording', 'Recorder start callback timed out'),
813
- )
814
- }
815
- }, 15_000)
816
- } catch (cause) {
817
- await fail(cause)
818
- }
819
- })()
820
-
821
- async function fail(cause: unknown) {
822
- if (sink !== target || failing) {
823
- return
824
- }
825
-
826
- failing = true
827
- if (reservation && desktop) {
828
- await removeHostReservation(desktop, reservation)
829
- }
830
-
831
- settle(
832
- {
833
- kind: 'failed',
834
- stage,
835
- issue:
836
- cause instanceof CameraCaptureError
837
- ? cause
838
- : error(
839
- stage === 'storage'
840
- ? 'storageFailed'
841
- : stage === 'preparation' || stage === 'capture'
842
- ? 'captureFailed'
843
- : 'finalizationFailed',
844
- stage,
845
- 'Movie recording could not complete',
846
- cause,
847
- ),
848
- },
849
- target,
850
- )
851
- }
852
- },
853
- stopCapture: stop,
854
- elapsedMs: () => (startedAt ? Math.max(0, (stoppedAt || performance.now()) - startedAt) : 0),
855
- openOutput: async (output) => {
856
- if (output.kind !== 'nativeFile') {
857
- throw error(
858
- 'unavailable',
859
- 'openOutput',
860
- 'Only host file-backed output exists on this platform',
861
- )
862
- }
863
-
864
- const desktop = await hostProbe
865
- if (!desktop) {
866
- throw error(
867
- 'unavailable',
868
- 'openOutput',
869
- 'File-backed output requires the desktop host that recorded it',
870
- )
871
- }
872
-
873
- return openHostMovie(desktop, output)
874
- },
875
- release: async () => {
876
- released = true
877
- generation += 1
878
- document.removeEventListener('visibilitychange', onVisibility)
879
- globalThis.removeEventListener('pagehide', onPageHide)
880
- dropStream()
881
- host = undefined
882
- },
883
- setObserver: (value) => {
884
- observer = value
885
- },
886
- }
57
+ let currentConfig = { ...config }
58
+ let observer: BackendObserver | undefined
59
+ let host: HTMLVideoElement | undefined
60
+ let listener: BackendPreviewListener | undefined
61
+ let stream: MediaStream | undefined
62
+ let ready = false
63
+ let released = false
64
+ let generation = 0
65
+ let recorder: MediaRecorder | undefined
66
+ let sink: BackendCaptureSink | undefined
67
+ let startedAt = 0
68
+ let stoppedAt = 0
69
+ let stopTimer: ReturnType<typeof setTimeout> | undefined
70
+ let watchdog: ReturnType<typeof setTimeout> | undefined
71
+ let recordingAudio = false
72
+ let captureFailure: ((cause: unknown) => Promise<void>) | undefined
73
+ // Packaged Linux WebKitGTK host probe: 'unknown' until the capability
74
+ // request settles; 'available' means the host owns media permission
75
+ // decisions and app-private movie storage for this session.
76
+ let hostState: 'unknown' | 'available' | 'absent' = 'unknown'
77
+ const hostProbe = cameraHost().then((host) => {
78
+ hostState = host ? 'available' : 'absent'
79
+ return host
80
+ })
81
+
82
+ const lease = {}
83
+ const known: Record<CameraPermissionKind, CameraPermissionStatus> = {
84
+ camera: 'unknown',
85
+ microphone: 'unknown',
86
+ }
87
+
88
+ // The packaged Linux host is mandatory: it owns media authorization and
89
+ // durable app-private storage — there is no weaker fallback origin store.
90
+ const supported = () =>
91
+ typeof navigator !== 'undefined' &&
92
+ globalThis.isSecureContext === true &&
93
+ !!navigator.mediaDevices?.getUserMedia &&
94
+ typeof MediaRecorder !== 'undefined' &&
95
+ !!mimeType() &&
96
+ hostState === 'available'
97
+
98
+ const mimeType = () =>
99
+ typeof MediaRecorder === 'undefined'
100
+ ? ''
101
+ : (mimeCandidates.find((mime) => MediaRecorder.isTypeSupported(mime)) ?? '')
102
+
103
+ const markReady = (value: boolean) => {
104
+ ready = value
105
+ observer?.previewReady(value)
106
+ }
107
+
108
+ const dropStream = () => {
109
+ if (host) {
110
+ host.srcObject = null
111
+ }
112
+
113
+ stream?.getTracks().forEach((track) => track.stop())
114
+ stream = undefined
115
+ markReady(false)
116
+ if (owner === lease) {
117
+ owner = undefined
118
+ }
119
+ }
120
+
121
+ const onVisibility = () => {
122
+ if (document.visibilityState === 'hidden') {
123
+ markReady(false)
124
+ observer?.interruption('began', 'background')
125
+ } else {
126
+ observer?.interruption('ended', 'background')
127
+ markReady(!!host && stream?.getVideoTracks()[0]?.readyState === 'live')
128
+ }
129
+ }
130
+
131
+ const onPageHide = () => {
132
+ markReady(false)
133
+ observer?.interruption('began', 'background')
134
+ }
135
+
136
+ // Track-caused interruptions (ended/mute) need an explicit 'ended' —
137
+ // a fresh live stream resolves them, and without the event the core
138
+ // keeps interrupted=true until the session is recreated.
139
+ let deviceInterrupted = false
140
+
141
+ const observeTracks = (target: MediaStream) => {
142
+ for (const track of target.getTracks()) {
143
+ track.addEventListener('ended', () => {
144
+ if (track.kind === 'audio' && (!sink || !recordingAudio)) {
145
+ return
146
+ }
147
+
148
+ if (stream !== target) {
149
+ return
150
+ }
151
+
152
+ markReady(false)
153
+ deviceInterrupted = true
154
+ observer?.availability(false)
155
+ observer?.interruption('began', 'unknown')
156
+ })
157
+
158
+ track.addEventListener('mute', () => {
159
+ if (track.kind === 'audio' && (!sink || !recordingAudio)) {
160
+ return
161
+ }
162
+
163
+ if (stream === target) {
164
+ markReady(false)
165
+ deviceInterrupted = true
166
+ observer?.interruption('began', 'unknown')
167
+ }
168
+ })
169
+
170
+ track.addEventListener('unmute', () => {
171
+ if (stream === target && document.visibilityState !== 'hidden') {
172
+ deviceInterrupted = false
173
+ observer?.interruption('ended', 'unknown')
174
+ observer?.availability(true)
175
+ markReady(!!host)
176
+ }
177
+ })
178
+ }
179
+ }
180
+
181
+ const constraints = (): MediaTrackConstraints => {
182
+ const camera = currentConfig.camera
183
+
184
+ return {
185
+ width: { ideal: 1280 },
186
+ height: { ideal: 720 },
187
+ frameRate: { ideal: 30 },
188
+ ...(typeof camera === 'object'
189
+ ? { deviceId: { exact: camera.deviceId } }
190
+ : camera === 'front' || camera === 'back'
191
+ ? { facingMode: { exact: camera === 'front' ? 'user' : 'environment' } }
192
+ : {}),
193
+ }
194
+ }
195
+
196
+ let acquisition: Promise<void> | undefined
197
+ const acquireCameraOnce = async () => {
198
+ if (stream?.getVideoTracks()[0]?.readyState === 'live') {
199
+ return
200
+ }
201
+
202
+ if (owner && owner !== lease) {
203
+ throw error('busy', 'preview', 'Another camera session still owns the source')
204
+ }
205
+
206
+ owner = lease
207
+
208
+ const epoch = generation
209
+
210
+ try {
211
+ const acquired = await navigator.mediaDevices.getUserMedia({
212
+ video: constraints(),
213
+ audio: false,
214
+ })
215
+
216
+ if (released || epoch !== generation) {
217
+ acquired.getTracks().forEach((track) => track.stop())
218
+ if (!stream && owner === lease) {
219
+ owner = undefined
220
+ }
221
+
222
+ return
223
+ }
224
+
225
+ stream = acquired
226
+ known.camera = 'granted'
227
+ observeTracks(acquired)
228
+ if (deviceInterrupted) {
229
+ deviceInterrupted = false
230
+ observer?.interruption('ended', 'unknown')
231
+ }
232
+
233
+ observer?.availability(true)
234
+ } catch (cause) {
235
+ if (owner === lease) {
236
+ owner = undefined
237
+ }
238
+
239
+ throw cause
240
+ }
241
+ }
242
+
243
+ const acquireCamera = () => {
244
+ acquisition ??= acquireCameraOnce().finally(() => {
245
+ acquisition = undefined
246
+ })
247
+
248
+ return acquisition
249
+ }
250
+
251
+ // WebKitGTK's GStreamer recorder loses the video pipeline when an audio
252
+ // track from a separate acquisition joins a video track that has already
253
+ // recorded — audio must come from one combined getUserMedia acquisition.
254
+ // When audio is required, swap the previewed stream for a combined one on
255
+ // the same camera selection.
256
+ const acquireCombined = async () => {
257
+ const epoch = generation
258
+ const acquired = await navigator.mediaDevices.getUserMedia({
259
+ video: constraints(),
260
+ audio: true,
261
+ })
262
+
263
+ if (released || epoch !== generation) {
264
+ acquired.getTracks().forEach((track) => track.stop())
265
+ return
266
+ }
267
+
268
+ const previous = stream
269
+
270
+ stream = acquired
271
+ previous?.getTracks().forEach((track) => track.stop())
272
+ observeTracks(acquired)
273
+ if (deviceInterrupted) {
274
+ deviceInterrupted = false
275
+ observer?.interruption('ended', 'unknown')
276
+ }
277
+
278
+ observer?.availability(true)
279
+ }
280
+
281
+ const ensureAudio = async () => {
282
+ if (stream?.getAudioTracks().some((track) => track.readyState === 'live')) {
283
+ return
284
+ }
285
+
286
+ if (stream) {
287
+ await acquireCombined()
288
+ if (host) {
289
+ await showPreview()
290
+ }
291
+
292
+ return
293
+ }
294
+
295
+ // No preview yet — a standalone acquisition proves the grant; the
296
+ // combined swap happens once the camera stream exists.
297
+ const audio = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
298
+
299
+ audio.getTracks().forEach((track) => track.stop())
300
+ }
301
+
302
+ const showPreview = async () => {
303
+ if (!host || released) {
304
+ return
305
+ }
306
+
307
+ const element = host
308
+ const epoch = generation
309
+
310
+ if ((await checkPermission('camera')) !== 'granted') {
311
+ listener?.onError?.(
312
+ error(
313
+ 'permissionBlocked',
314
+ 'preview',
315
+ 'Request camera access from a user action before activating this preview',
316
+ ),
317
+ )
318
+
319
+ return
320
+ }
321
+
322
+ await acquireCamera()
323
+ if (host !== element || !stream || released || epoch !== generation) {
324
+ return
325
+ }
326
+
327
+ element.srcObject = stream
328
+ element.muted = true
329
+
330
+ const facing = stream.getVideoTracks()[0]?.getSettings().facingMode
331
+
332
+ element.style.transform = facing === 'user' ? 'scaleX(-1)' : ''
333
+ await element.play()
334
+ if (host !== element || released || epoch !== generation) {
335
+ return
336
+ }
337
+
338
+ markReady(document.visibilityState !== 'hidden')
339
+ listener?.onReady?.()
340
+ }
341
+
342
+ const hostMediaKind = (kind: CameraPermissionKind) =>
343
+ kind === 'microphone' ? 'microphone' : 'camera'
344
+
345
+ async function checkPermission(kind: CameraPermissionKind): Promise<CameraPermissionStatus> {
346
+ if (globalThis.isSecureContext !== true || !navigator.mediaDevices?.getUserMedia) {
347
+ return 'unavailable'
348
+ }
349
+
350
+ if (!policyAllows(kind)) {
351
+ return 'blocked'
352
+ }
353
+
354
+ const desktop = await hostProbe
355
+
356
+ if (!desktop) {
357
+ return 'unavailable'
358
+ }
359
+
360
+ try {
361
+ const { status } = await desktop.call('camera', 'permissionStatus', {
362
+ kind: hostMediaKind(kind),
363
+ })
364
+
365
+ if (status === 'granted' || status === 'notDetermined') {
366
+ return status
367
+ }
368
+
369
+ if (status === 'restricted') {
370
+ return 'restricted'
371
+ }
372
+
373
+ if (status === 'denied' || status === 'blocked') {
374
+ return 'denied'
375
+ }
376
+
377
+ // 'undeclared' and unknown statuses stay honest but unadorned.
378
+ return 'unknown'
379
+ } catch {
380
+ return 'unknown'
381
+ }
382
+ }
383
+
384
+ const settle = (value: Parameters<BackendCaptureSink['settled']>[0], expected = sink) => {
385
+ const target = sink
386
+
387
+ if (!target || target !== expected) {
388
+ return
389
+ }
390
+
391
+ sink = undefined
392
+ recorder = undefined
393
+ captureFailure = undefined
394
+ clearTimeout(stopTimer)
395
+ clearTimeout(watchdog)
396
+ if (!host) {
397
+ dropStream()
398
+ }
399
+
400
+ target.settled(value)
401
+ }
402
+
403
+ const stop = () => {
404
+ if (!recorder || !sink) {
405
+ return
406
+ }
407
+
408
+ stoppedAt ||= performance.now()
409
+ clearTimeout(watchdog)
410
+
411
+ const target = sink
412
+ const fail = captureFailure
413
+
414
+ watchdog = setTimeout(() => {
415
+ if (sink !== target) {
416
+ return
417
+ }
418
+
419
+ dropStream()
420
+ void fail?.(error('finalizationFailed', 'stop', 'Recorder finalization callback timed out'))
421
+ }, 30_000)
422
+
423
+ if (recorder.state === 'inactive') {
424
+ return
425
+ }
426
+
427
+ try {
428
+ recorder.stop()
429
+ } catch (cause) {
430
+ void fail?.(error('captureFailed', 'stop', 'The recorder could not stop', cause))
431
+ }
432
+ }
433
+
434
+ return {
435
+ platform: 'web',
436
+ get supported() {
437
+ return supported()
438
+ },
439
+ checkPermission,
440
+ requestPermission: async (kind) => {
441
+ const status = await checkPermission(kind)
442
+
443
+ if (status === 'blocked' || status === 'unavailable') {
444
+ return status
445
+ }
446
+
447
+ const desktop = await hostProbe
448
+
449
+ if (!desktop) {
450
+ return 'unavailable'
451
+ }
452
+
453
+ const { status: hostStatus } = await desktop.call('camera', 'requestPermission', {
454
+ kind: hostMediaKind(kind),
455
+ })
456
+
457
+ if (hostStatus !== 'granted') {
458
+ known[kind] =
459
+ hostStatus === 'denied' || hostStatus === 'blocked'
460
+ ? 'denied'
461
+ : hostStatus === 'restricted'
462
+ ? 'restricted'
463
+ : 'unknown'
464
+
465
+ return known[kind]
466
+ }
467
+
468
+ // The host granted the request — its permission-request handler now
469
+ // allows this app's acquisition without another decision.
470
+
471
+ try {
472
+ if (kind === 'camera') {
473
+ await acquireCamera()
474
+ } else if (sink) {
475
+ // Mid-take the recording stream cannot be swapped; prove the
476
+ // grant only — audio joins on the next take.
477
+ const audio = await navigator.mediaDevices.getUserMedia({
478
+ audio: true,
479
+ video: false,
480
+ })
481
+
482
+ audio.getTracks().forEach((track) => track.stop())
483
+ } else {
484
+ await ensureAudio()
485
+ }
486
+
487
+ known[kind] = 'granted'
488
+ if (kind === 'camera' && host) {
489
+ await showPreview()
490
+ }
491
+
492
+ return 'granted'
493
+ } catch (cause) {
494
+ known[kind] =
495
+ cause instanceof DOMException &&
496
+ (cause.name === 'NotAllowedError' || cause.name === 'SecurityError')
497
+ ? 'blocked'
498
+ : 'unavailable'
499
+
500
+ return known[kind]
501
+ }
502
+ },
503
+ getCapabilities: async () => {
504
+ const desktop = await hostProbe
505
+ const devices = (await navigator.mediaDevices?.enumerateDevices?.().catch(() => [])) ?? []
506
+ const settings = stream?.getVideoTracks()[0]?.getSettings()
507
+ const mime = mimeType()
508
+ const storageReady = !!desktop
509
+
510
+ return {
511
+ supported: supported(),
512
+ available: ready && storageReady,
513
+ reason: !supported()
514
+ ? 'Secure media capture, a supported WebM/MP4 codec, and the packaged Linux host are required'
515
+ : !storageReady
516
+ ? 'Movie output requires the packaged Linux WebKitGTK host'
517
+ : !ready
518
+ ? 'An active permitted preview is required'
519
+ : undefined,
520
+ cameras: devices
521
+ .filter((device) => device.kind === 'videoinput')
522
+ .map((device) => ({ id: device.deviceId, label: device.label })),
523
+ profiles:
524
+ settings?.width && settings.height && settings.frameRate
525
+ ? [
526
+ {
527
+ profile: 'standard' as const,
528
+ width: settings.width,
529
+ height: settings.height,
530
+ frameRate: settings.frameRate,
531
+ },
532
+ ]
533
+ : [],
534
+ audio: devices.some((device) => device.kind === 'audioinput'),
535
+ // No point-of-interest metering in the WebKitGTK path.
536
+ focusPointOfInterest: false,
537
+ exposurePointOfInterest: false,
538
+ zoom: false,
539
+ orientations: [],
540
+ cameraSwitchDuringRecording: false,
541
+ durationLimit: 'bestEffort',
542
+ elapsedTime: 'estimated',
543
+ output: {
544
+ mimeType: mime,
545
+ container: !mime ? '' : mime.startsWith('video/mp4') ? 'mp4' : 'webm',
546
+ storage: desktop ? 'appPrivateFile' : 'originLocal',
547
+ destinationFileUrl: !!desktop,
548
+ },
549
+ }
550
+ },
551
+ previewReady: () => ready && !!host && !released,
552
+ validateStart: (options) => {
553
+ // A pending host probe defers checks to the async start path; once
554
+ // the probe settles absent, recording cannot produce durable output.
555
+ if (hostState === 'absent') {
556
+ throw error(
557
+ 'destinationUnavailable',
558
+ 'startRecording',
559
+ 'Movie output requires the packaged Linux WebKitGTK host',
560
+ )
561
+ }
562
+
563
+ if (options.orientation && options.orientation !== 'unspecified') {
564
+ throw error(
565
+ 'unsupportedConfiguration',
566
+ 'startRecording',
567
+ 'This browser cannot lock a recording orientation',
568
+ )
569
+ }
570
+ },
571
+ attachPreview: (target, callbacks) => {
572
+ if (released || sink) {
573
+ throw error('invalidState', 'attachPreview', 'The camera is disposed or still finalizing')
574
+ }
575
+
576
+ host = target as HTMLVideoElement
577
+ listener = callbacks
578
+ document.addEventListener('visibilitychange', onVisibility)
579
+ globalThis.addEventListener('pagehide', onPageHide)
580
+ void showPreview().catch((cause) =>
581
+ callbacks?.onError?.(
582
+ error('unavailable', 'preview', 'Could not activate camera preview', cause),
583
+ ),
584
+ )
585
+
586
+ return () => {
587
+ if (host) {
588
+ host.srcObject = null
589
+ }
590
+
591
+ host = undefined
592
+ listener = undefined
593
+ generation += 1
594
+ markReady(false)
595
+ document.removeEventListener('visibilitychange', onVisibility)
596
+ globalThis.removeEventListener('pagehide', onPageHide)
597
+ if (!sink) {
598
+ dropStream()
599
+ }
600
+ }
601
+ },
602
+ configure: (next) => {
603
+ const sourceChanged =
604
+ JSON.stringify(next.camera) !== JSON.stringify(currentConfig.camera) ||
605
+ next.profile !== currentConfig.profile
606
+
607
+ currentConfig = { ...next }
608
+ if (sourceChanged) {
609
+ generation += 1
610
+ dropStream()
611
+ if (host) {
612
+ void showPreview().catch((cause) =>
613
+ listener?.onError?.(
614
+ error('unavailable', 'configure', 'Could not change camera', cause),
615
+ ),
616
+ )
617
+ }
618
+ }
619
+ },
620
+ startCapture: (request, target) => {
621
+ sink = target
622
+ recordingAudio = request.audio
623
+ startedAt = 0
624
+ stoppedAt = 0
625
+
626
+ let desktop: CameraHost | null = null
627
+ let reservation: string | undefined
628
+ let stage: 'preparation' | 'capture' | 'finalization' | 'storage' = 'preparation'
629
+ const chunks: Blob[] = []
630
+ let captureError: unknown
631
+ let limitReached = false
632
+ let failing = false
633
+ let didStart = false
634
+
635
+ captureFailure = fail
636
+ void (async () => {
637
+ try {
638
+ desktop = await hostProbe
639
+ if (!desktop) {
640
+ throw error(
641
+ 'destinationUnavailable',
642
+ 'startRecording',
643
+ 'Movie output requires the packaged Linux WebKitGTK host',
644
+ )
645
+ }
646
+
647
+ if (request.audio) {
648
+ // A take never prompts — microphone must already be granted;
649
+ // the combined swap below needs the grant to exist first.
650
+ if ((await checkPermission('microphone')) !== 'granted') {
651
+ throw error(
652
+ 'permissionDenied',
653
+ 'startRecording',
654
+ 'Requested microphone access is missing',
655
+ )
656
+ }
657
+
658
+ await ensureAudio().catch((cause) => {
659
+ throw error(
660
+ 'captureFailed',
661
+ 'startRecording',
662
+ 'Could not acquire microphone for recording',
663
+ cause,
664
+ )
665
+ })
666
+ }
667
+
668
+ const source = stream
669
+ const video = source?.getVideoTracks()[0]
670
+ const audio =
671
+ source?.getAudioTracks().filter((track) => track.readyState === 'live') ?? []
672
+
673
+ if (!video || (request.audio && !audio.length)) {
674
+ throw error(
675
+ request.audio ? 'permissionDenied' : 'unavailable',
676
+ 'startRecording',
677
+ request.audio
678
+ ? 'Requested microphone access is missing'
679
+ : 'Video source is unavailable',
680
+ )
681
+ }
682
+
683
+ const recordingStream = new MediaStream([video, ...(request.audio ? audio : [])])
684
+
685
+ reservation = (
686
+ await reserveHostMovie(desktop, {
687
+ destinationFileUrl: request.destinationPath
688
+ ? `file://${request.destinationPath.split('/').map(encodeURIComponent).join('/')}`
689
+ : undefined,
690
+ container: mimeType().startsWith('video/mp4') ? 'mp4' : 'webm',
691
+ })
692
+ ).fileUrl
693
+
694
+ if (!source || video.readyState !== 'live' || document.visibilityState === 'hidden') {
695
+ throw error(
696
+ 'unavailable',
697
+ 'startRecording',
698
+ 'Camera source became inactive before recording',
699
+ )
700
+ }
701
+
702
+ const activeRecorder = new MediaRecorder(recordingStream, { mimeType: mimeType() })
703
+
704
+ recorder = activeRecorder
705
+ activeRecorder.ondataavailable = (event) => {
706
+ if (sink !== target || failing) {
707
+ return
708
+ }
709
+
710
+ if (event.data.size) {
711
+ chunks.push(event.data)
712
+ }
713
+ }
714
+
715
+ activeRecorder.onerror = (event) => {
716
+ if (sink !== target || failing) {
717
+ return
718
+ }
719
+
720
+ captureError = event
721
+ stop()
722
+ }
723
+
724
+ activeRecorder.onstart = () => {
725
+ if (sink !== target || failing) {
726
+ return
727
+ }
728
+
729
+ if (didStart) {
730
+ return
731
+ }
732
+
733
+ didStart = true
734
+ clearTimeout(watchdog)
735
+ startedAt = performance.now()
736
+ stage = 'capture'
737
+
738
+ const settings = video.getSettings()
739
+ const effective: EffectiveCaptureConfig = {
740
+ cameraId: settings.deviceId ?? '',
741
+ audio: request.audio,
742
+ width: settings.width ?? 0,
743
+ height: settings.height ?? 0,
744
+ frameRate: settings.frameRate ?? 0,
745
+ orientation: 'unspecified',
746
+ mimeType: activeRecorder.mimeType,
747
+ container: activeRecorder.mimeType.startsWith('video/mp4') ? 'mp4' : 'webm',
748
+ }
749
+
750
+ // The adapter owns a limit even when capabilities() was not called.
751
+ if (request.maximumDurationMs) {
752
+ const limit = request.maximumDurationMs
753
+ const checkLimit = () => {
754
+ if (sink !== target || failing) {
755
+ return
756
+ }
757
+
758
+ const remaining = limit - (performance.now() - startedAt)
759
+
760
+ if (remaining > 0) {
761
+ stopTimer = setTimeout(checkLimit, Math.min(remaining, 2_147_483_647))
762
+ } else {
763
+ limitReached = true
764
+ stop()
765
+ }
766
+ }
767
+
768
+ stopTimer = setTimeout(checkLimit, Math.min(limit, 2_147_483_647))
769
+ }
770
+
771
+ target.started(effective)
772
+ }
773
+
774
+ activeRecorder.onstop = () => {
775
+ if (sink !== target || failing) {
776
+ return
777
+ }
778
+
779
+ target.finishing(
780
+ captureError ? 'captureError' : limitReached ? 'maximumDuration' : undefined,
781
+ )
782
+
783
+ stoppedAt ||= performance.now()
784
+ clearTimeout(stopTimer)
785
+ clearTimeout(watchdog)
786
+ void (async () => {
787
+ try {
788
+ stage = 'finalization'
789
+ if (!startedAt) {
790
+ throw error('noMedia', 'finalize', 'No recording start was observed')
791
+ }
792
+
793
+ const bytes = new Blob(chunks, { type: activeRecorder.mimeType })
794
+
795
+ if ((globalThis as any).__xplatDebugMovie) {
796
+ ;(globalThis as any).__xplatLastMovieBlob = bytes
797
+ }
798
+
799
+ const metadata = await inspectMovie(bytes, request.audio)
800
+
801
+ if (sink !== target || failing) {
802
+ return
803
+ }
804
+
805
+ stage = 'storage'
806
+
807
+ const output = await commitHostMovie(desktop!, reservation!, bytes)
808
+ const clip: MovieClip = { ...metadata, output }
809
+
810
+ settle(
811
+ {
812
+ kind: 'clip',
813
+ ...clip,
814
+ limitReached,
815
+ warning: captureError
816
+ ? error(
817
+ 'captureFailed',
818
+ 'capture',
819
+ 'Recorder reported an error; finalized media was verified',
820
+ captureError,
821
+ )
822
+ : undefined,
823
+ },
824
+ target,
825
+ )
826
+ } catch (cause) {
827
+ await fail(cause)
828
+ }
829
+ })()
830
+ }
831
+
832
+ activeRecorder.start(1000)
833
+ watchdog = setTimeout(() => {
834
+ if (!startedAt && sink === target) {
835
+ stop()
836
+ void fail(
837
+ error('captureFailed', 'startRecording', 'Recorder start callback timed out'),
838
+ )
839
+ }
840
+ }, 15_000)
841
+ } catch (cause) {
842
+ await fail(cause)
843
+ }
844
+ })()
845
+
846
+ async function fail(cause: unknown) {
847
+ if (sink !== target || failing) {
848
+ return
849
+ }
850
+
851
+ failing = true
852
+ if (reservation && desktop) {
853
+ await removeHostReservation(desktop, reservation)
854
+ }
855
+
856
+ settle(
857
+ {
858
+ kind: 'failed',
859
+ stage,
860
+ issue:
861
+ cause instanceof CameraCaptureError
862
+ ? cause
863
+ : error(
864
+ stage === 'storage'
865
+ ? 'storageFailed'
866
+ : stage === 'preparation' || stage === 'capture'
867
+ ? 'captureFailed'
868
+ : 'finalizationFailed',
869
+ stage,
870
+ 'Movie recording could not complete',
871
+ cause,
872
+ ),
873
+ },
874
+ target,
875
+ )
876
+ }
877
+ },
878
+ stopCapture: stop,
879
+ elapsedMs: () => (startedAt ? Math.max(0, (stoppedAt || performance.now()) - startedAt) : 0),
880
+ openOutput: async (output) => {
881
+ if (output.kind !== 'nativeFile') {
882
+ throw error(
883
+ 'unavailable',
884
+ 'openOutput',
885
+ 'Only host file-backed output exists on this platform',
886
+ )
887
+ }
888
+
889
+ const desktop = await hostProbe
890
+
891
+ if (!desktop) {
892
+ throw error(
893
+ 'unavailable',
894
+ 'openOutput',
895
+ 'File-backed output requires the desktop host that recorded it',
896
+ )
897
+ }
898
+
899
+ return openHostMovie(desktop, output)
900
+ },
901
+ release: async () => {
902
+ released = true
903
+ generation += 1
904
+ document.removeEventListener('visibilitychange', onVisibility)
905
+ globalThis.removeEventListener('pagehide', onPageHide)
906
+ dropStream()
907
+ host = undefined
908
+ },
909
+ setObserver: (value) => {
910
+ observer = value
911
+ },
912
+ }
887
913
  }