@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
@@ -6,29 +6,30 @@ import type { CameraPreviewPoint } from './types'
6
6
  * above the preview still see it. Platforms whose runtime never
7
7
  * dispatches 'tap' simply never fire the callback. */
8
8
  export function observePreviewTap(
9
- view: any,
10
- onTap: ((point: CameraPreviewPoint) => void) | undefined,
9
+ view: any,
10
+ onTap: ((point: CameraPreviewPoint) => void) | undefined,
11
11
  ): () => void {
12
- if (!view || !onTap || typeof view.on !== 'function') {
13
- return () => {}
14
- }
12
+ if (!view || !onTap || typeof view.on !== 'function') {
13
+ return () => {}
14
+ }
15
15
 
16
- const handler = (args: any) => {
17
- const size = view.getActualSize?.()
18
- const width = size?.width ?? 0
19
- const height = size?.height ?? 0
20
- const x = typeof args?.getX === 'function' ? args.getX() : NaN
21
- const y = typeof args?.getY === 'function' ? args.getY() : NaN
22
- if (!(width > 0) || !(height > 0) || !Number.isFinite(x) || !Number.isFinite(y)) {
23
- return
24
- }
16
+ const handler = (args: any) => {
17
+ const size = view.getActualSize?.()
18
+ const width = size?.width ?? 0
19
+ const height = size?.height ?? 0
20
+ const x = typeof args?.getX === 'function' ? args.getX() : NaN
21
+ const y = typeof args?.getY === 'function' ? args.getY() : NaN
25
22
 
26
- onTap({
27
- x: Math.min(1, Math.max(0, x / width)),
28
- y: Math.min(1, Math.max(0, y / height)),
29
- })
30
- }
23
+ if (!(width > 0) || !(height > 0) || !Number.isFinite(x) || !Number.isFinite(y)) {
24
+ return
25
+ }
31
26
 
32
- view.on('tap', handler)
33
- return () => view.off?.('tap', handler)
27
+ onTap({
28
+ x: Math.min(1, Math.max(0, x / width)),
29
+ y: Math.min(1, Math.max(0, y / height)),
30
+ })
31
+ }
32
+
33
+ view.on('tap', handler)
34
+ return () => view.off?.('tap', handler)
34
35
  }
package/src/props.ts CHANGED
@@ -1,77 +1,127 @@
1
1
  import type { Octane } from 'octane/jsx-runtime'
2
- import type { CameraPreviewPoint, CameraSession } from './types'
2
+ import type { CameraPinchEvent, CameraPreviewPoint, CameraSession } from './types'
3
3
 
4
+ /** Accessibility roles the preview accepts. */
4
5
  export type CameraAccessibilityRole =
5
- | 'button'
6
- | 'link'
7
- | 'search'
8
- | 'image'
9
- | 'heading'
10
- | 'adjustable'
11
- | 'summary'
12
- | 'text'
13
- | 'none'
14
- | 'progressbar'
15
- | 'checkbox'
16
- | 'switch'
17
- | 'radio'
18
- | 'spinbutton'
19
- | 'tab'
6
+ | 'button'
7
+ | 'link'
8
+ | 'search'
9
+ | 'image'
10
+ | 'heading'
11
+ | 'adjustable'
12
+ | 'summary'
13
+ | 'text'
14
+ | 'none'
15
+ | 'progressbar'
16
+ | 'checkbox'
17
+ | 'switch'
18
+ | 'radio'
19
+ | 'spinbutton'
20
+ | 'tab'
20
21
 
22
+ /** Imperative handle exposed through the CameraView `ref`. */
21
23
  export interface CameraViewHandle {
22
- /** The native preview surface (`HTMLVideoElement` on web). */
23
- native: any
24
+ /** The native preview surface (`HTMLVideoElement` on web). */
25
+ native: any
24
26
  }
25
27
 
28
+ /** Props for the CameraView element. */
26
29
  export interface CameraViewProps {
27
- className?: any
28
- style?: any
29
- id?: string
30
- row?: number
31
- col?: number
32
- rowSpan?: number
33
- colSpan?: number
34
- dock?: 'left' | 'top' | 'right' | 'bottom'
35
- left?: number
36
- top?: number
37
- flexGrow?: number
38
- flexShrink?: number
39
- alignSelf?: string
40
- order?: number
41
- accessible?: boolean
42
- accessibilityLabel?: string
43
- accessibilityRole?: CameraAccessibilityRole
44
- accessibilityHint?: string
45
- accessibilityValue?: string
46
- accessibilityState?: {
47
- disabled?: boolean
48
- selected?: boolean
49
- checked?: boolean
50
- }
51
- accessibilityLiveRegion?: 'none' | 'polite' | 'assertive'
52
- /** Shared camera owner enabling movie recording. When set, the preview
53
- * attaches to the session's acquisition instead of owning one, and
54
- * `facing` must be omitted — camera selection lives on the session. */
55
- session?: CameraSession
56
- /** Lens — defaults to the rear camera. Invalid together with `session`. */
57
- facing?: 'front' | 'back'
58
- /** Default true. False releases the preview session. */
59
- active?: boolean
60
- /** Fires after the preview surface starts. */
61
- onReady?: () => void
62
- onError?: (error: { message?: string }) => void
63
- /** Notification-only tap event — fires with the normalized
64
- * preview-space position (`CameraPreviewPoint`) and never consumes
65
- * the gesture, so recognizers layered above the preview (double-tap,
66
- * pan) still run. Pair with `session.focusAtPoint` for tap-to-focus.
67
- * Delivered on iOS, Android, and web; desktop runtimes do not dispatch
68
- * it yet. */
69
- onTap?: (point: CameraPreviewPoint) => void
70
- /** Native preview surface for platform-specific controls. */
71
- ref?: Octane.Ref<CameraViewHandle>
72
- /** Platform-specific properties applied to the preview surface. */
73
- ios?: Record<string, any>
74
- android?: Record<string, any>
75
- web?: Record<string, any>
76
- windows?: Record<string, any>
30
+ /** Class list applied to the preview element. */
31
+ className?: any
32
+ /** Inline style (string CSS or object). */
33
+ style?: any
34
+ /** Element id passed through to the host element. */
35
+ id?: string
36
+ /** Zero-based row index in the parent grid. */
37
+ row?: number
38
+ /** Zero-based column index. */
39
+ col?: number
40
+ /** Row tracks to span (>= 1). */
41
+ rowSpan?: number
42
+ /** Column tracks to span (>= 1). */
43
+ colSpan?: number
44
+ /** Edge of the parent dock layout this child pins to. */
45
+ dock?: 'left' | 'top' | 'right' | 'bottom'
46
+ /** Absolute x offset in dips. */
47
+ left?: number
48
+ /** Absolute y offset in dips. */
49
+ top?: number
50
+ /** Flex grow factor. */
51
+ flexGrow?: number
52
+ /** Flex shrink factor. */
53
+ flexShrink?: number
54
+ /** Cross-axis self alignment. */
55
+ alignSelf?: string
56
+ /** Sibling ordering. */
57
+ order?: number
58
+ /** Expose to assistive tech as an accessibility element. */
59
+ accessible?: boolean
60
+ /** Spoken name for assistive tech. */
61
+ accessibilityLabel?: string
62
+ /** Semantic role mapped per platform. */
63
+ accessibilityRole?: CameraAccessibilityRole
64
+ /** Spoken hint describing what activating does. */
65
+ accessibilityHint?: string
66
+ /** Spoken current value text. */
67
+ accessibilityValue?: string
68
+ /** State flags merged into the platform a11y state. */
69
+ accessibilityState?: {
70
+ disabled?: boolean
71
+ selected?: boolean
72
+ checked?: boolean
73
+ }
74
+ /** Live-region announcements: polite queues, assertive interrupts. */
75
+ accessibilityLiveRegion?: 'none' | 'polite' | 'assertive'
76
+ /** Shared camera owner enabling movie recording. When set, the preview
77
+ * attaches to the session's acquisition instead of owning one, and
78
+ * `facing` must be omitted — camera selection lives on the session. */
79
+ session?: CameraSession
80
+ /** Lens — defaults to the rear camera. Invalid together with `session`. */
81
+ facing?: 'front' | 'back'
82
+ /** Default true. False releases the preview session. */
83
+ active?: boolean
84
+ /** Fires after the preview surface starts — session-backed previews
85
+ * deliver it when the camera source is feeding the shared session and
86
+ * recording can be admitted (`session.startRecording` would not be
87
+ * rejected for an idle source). It is an admission signal, not a
88
+ * displayed-frame signal: use `onDisplayReady` when a visible first
89
+ * frame must precede an automatic recording. */
90
+ onReady?: () => void
91
+ /** Fires when the attached surface first displays a live camera frame,
92
+ * separate from `onReady` admission. Apps that record automatically
93
+ * should wait for this so capture never starts before the user can
94
+ * see the preview. iOS 13+ reports `AVCaptureVideoPreviewLayer`
95
+ * `previewing` in non-empty bounds; Android reports the PreviewView
96
+ * `STREAMING` state; web reports the first presented video frame.
97
+ * Fires again when the displayed preview resumes after an
98
+ * interruption. Hosts without a distinct display signal — macOS,
99
+ * Windows, Linux — never fire it. */
100
+ onDisplayReady?: () => void
101
+ /** Camera/preview errors. */
102
+ onError?: (error: { message?: string }) => void
103
+ /** Notification-only tap event — fires with the normalized
104
+ * preview-space position (`CameraPreviewPoint`) and never consumes
105
+ * the gesture, so recognizers layered above the preview (double-tap,
106
+ * pan) still run. Pair with `session.focusAtPoint` for tap-to-focus.
107
+ * Delivered on iOS, Android, and web; desktop runtimes do not dispatch
108
+ * it yet. */
109
+ onTap?: (point: CameraPreviewPoint) => void
110
+ /** Pinch gesture on the preview — fires with the cumulative `scale`
111
+ * and a preview-space `focus` (`CameraPinchEvent`). Notification-only:
112
+ * it never consumes the gesture. Pair with `session.setZoom` for
113
+ * pinch-to-zoom; a pinch state of `began` is also the point to suspend
114
+ * any ancestor pull-down gesture for. Delivered on iOS and Android;
115
+ * desktop runtimes do not dispatch it yet. */
116
+ onPinch?: (event: CameraPinchEvent) => void
117
+ /** Native preview surface for platform-specific controls. */
118
+ ref?: Octane.Ref<CameraViewHandle>
119
+ /** Platform-specific properties applied to the preview surface. */
120
+ ios?: Record<string, any>
121
+ /** Platform escape hatch — same bag contract as `ios`. */
122
+ android?: Record<string, any>
123
+ /** Platform escape hatch — same bag contract as `ios`. */
124
+ web?: Record<string, any>
125
+ /** Platform escape hatch — same bag contract as `ios`. */
126
+ windows?: Record<string, any>
77
127
  }
@@ -0,0 +1,39 @@
1
+ import { AVAudioSession } from '@octane-xplat/platform/ios'
2
+
3
+ // All camera owners in this JS runtime share the app's AVAudioSession policy.
4
+ // Do not change its category, routing, or activation state.
5
+ let owners = 0
6
+ let previous = false
7
+
8
+ export type RecordingHapticsLease = () => void
9
+
10
+ export function acquireRecordingHaptics(): RecordingHapticsLease {
11
+ const session = AVAudioSession.sharedInstance()
12
+
13
+ if (owners === 0) {
14
+ previous = session.allowHapticsAndSystemSoundsDuringRecording
15
+ if (!previous && !session.setAllowHapticsAndSystemSoundsDuringRecordingError(true, undefined)) {
16
+ throw new Error('Could not allow haptics and system sounds during recording')
17
+ }
18
+ }
19
+
20
+ owners++
21
+
22
+ let released = false
23
+
24
+ return () => {
25
+ if (released) {
26
+ return
27
+ }
28
+
29
+ released = true
30
+ owners--
31
+ // Respect an external owner that has already disabled the policy. External
32
+ // code setting true while a lease is active must coordinate with the app.
33
+ if (owners === 0 && !previous && session.allowHapticsAndSystemSoundsDuringRecording) {
34
+ if (!session.setAllowHapticsAndSystemSoundsDuringRecordingError(false, undefined)) {
35
+ throw new Error('Could not restore recording haptics policy')
36
+ }
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,159 @@
1
+ import { CameraCaptureError } from './types'
2
+
3
+ const failed = (message: string) =>
4
+ new CameraCaptureError({ kind: 'unavailable', operation: 'switchCamera', message })
5
+
6
+ /** Fixed-size video track. Hardware video sources can be replaced without
7
+ * replacing the track observed by MediaRecorder or the preview. */
8
+ export interface RecordingVideoSource {
9
+ readonly stream: MediaStream
10
+ /** Retain the last canvas frame while the old camera is released. */
11
+ hold(): void
12
+ /** Resolve only after drawing a decoded frame from this source. */
13
+ setSource(stream: MediaStream): Promise<void>
14
+ release(): void
15
+ }
16
+
17
+ export function supportsRecordingVideoSource(): boolean {
18
+ return (
19
+ typeof HTMLCanvasElement !== 'undefined' &&
20
+ typeof HTMLCanvasElement.prototype.captureStream === 'function' &&
21
+ typeof CanvasCaptureMediaStreamTrack !== 'undefined' &&
22
+ typeof CanvasCaptureMediaStreamTrack.prototype.requestFrame === 'function'
23
+ )
24
+ }
25
+
26
+ export function createRecordingVideoSource(
27
+ width: number,
28
+ height: number,
29
+ frameRate: number,
30
+ ): RecordingVideoSource {
31
+ const canvas = document.createElement('canvas')
32
+
33
+ canvas.width = width
34
+ canvas.height = height
35
+
36
+ const context = canvas.getContext('2d', { alpha: false })
37
+
38
+ if (!context) {
39
+ throw failed('A 2D recording surface is unavailable')
40
+ }
41
+
42
+ const stream = canvas.captureStream(0)
43
+ const track = stream.getVideoTracks()[0] as CanvasCaptureMediaStreamTrack
44
+ let active: HTMLVideoElement | undefined
45
+ let pending:
46
+ | { video: HTMLVideoElement; resolve: () => void; reject: (error: Error) => void }
47
+ | undefined
48
+
49
+ let released = false
50
+ const clearVideo = (video: HTMLVideoElement) => {
51
+ video.pause()
52
+ video.srcObject = null
53
+ }
54
+
55
+ const hold = () => {
56
+ if (active) {
57
+ clearVideo(active)
58
+ active = undefined
59
+ }
60
+
61
+ if (pending) {
62
+ clearVideo(pending.video)
63
+ pending.reject(failed('Camera switching was cancelled'))
64
+ pending = undefined
65
+ }
66
+ }
67
+
68
+ const forgetPending = (video: HTMLVideoElement) => {
69
+ if (pending?.video === video) {
70
+ pending = undefined
71
+ }
72
+ }
73
+
74
+ const timer = setInterval(
75
+ () => {
76
+ if (released) {
77
+ return
78
+ }
79
+
80
+ try {
81
+ const video = pending && pending.video.readyState >= 2 ? pending.video : active
82
+
83
+ if (video && video.videoWidth > 0 && video.videoHeight > 0) {
84
+ const scale = Math.max(width / video.videoWidth, height / video.videoHeight)
85
+ const w = video.videoWidth * scale
86
+ const h = video.videoHeight * scale
87
+
88
+ context.drawImage(video, (width - w) / 2, (height - h) / 2, w, h)
89
+ } else {
90
+ // requestFrame alone may not encode an unchanged canvas. Mark
91
+ // the retained content dirty without changing its pixels.
92
+ context.drawImage(canvas, 0, 0)
93
+ }
94
+
95
+ track.requestFrame()
96
+ if (video && video.videoWidth > 0 && video.videoHeight > 0 && pending?.video === video) {
97
+ const completed = pending
98
+
99
+ pending = undefined
100
+ if (active) {
101
+ clearVideo(active)
102
+ }
103
+
104
+ active = video
105
+ completed.resolve()
106
+ }
107
+ } catch (error) {
108
+ pending?.reject(error instanceof Error ? error : failed('Could not draw a camera frame'))
109
+ }
110
+ },
111
+ 1000 / Math.min(60, Math.max(1, frameRate)),
112
+ )
113
+
114
+ return {
115
+ stream,
116
+ hold,
117
+ setSource: async (source) => {
118
+ if (released) {
119
+ throw failed('The recording source is released')
120
+ }
121
+
122
+ if (pending) {
123
+ throw failed('Another camera source is pending')
124
+ }
125
+
126
+ const video = document.createElement('video')
127
+
128
+ video.muted = true
129
+ video.playsInline = true
130
+ video.srcObject = source
131
+
132
+ let timeout: ReturnType<typeof setTimeout> | undefined
133
+
134
+ try {
135
+ await new Promise<void>((resolve, reject) => {
136
+ pending = { video, resolve, reject }
137
+ timeout = setTimeout(
138
+ () => reject(failed('The camera did not provide a frame before timeout')),
139
+ 5000,
140
+ )
141
+
142
+ void video.play().catch(reject)
143
+ })
144
+ } catch (error) {
145
+ forgetPending(video)
146
+ clearVideo(video)
147
+ throw error
148
+ } finally {
149
+ clearTimeout(timeout)
150
+ }
151
+ },
152
+ release: () => {
153
+ released = true
154
+ clearInterval(timer)
155
+ hold()
156
+ stream.getTracks().forEach((track) => track.stop())
157
+ },
158
+ }
159
+ }