@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,136 +1,212 @@
1
- /** @jsxImportSource @nativescript-community/octane */
1
+ /** @jsxImportSource @octane-xplat/jsx */
2
2
  import { useImperativeHandle, useCallback, useEffect, useRef } from 'octane'
3
3
  import { Application, View } from '@nativescript/core'
4
4
  import { registerElement } from '@nativescript-community/octane'
5
- import type { Attributes } from '@nativescript-community/octane/intrinsics'
5
+ import type { Attributes } from '@octane-xplat/jsx/intrinsics'
6
6
  import type { CameraViewProps } from './props'
7
7
  import { attachSessionPreview } from './session'
8
8
  import { cameraHardwarePresent, ensureCameraPermission } from './camera-permission.mobile'
9
- import { applyEscapeProps, layoutChildProps, nativeAccessibilityRole, nativeAccessibilityState } from './layout-child'
9
+ import {
10
+ applyEscapeProps,
11
+ layoutChildProps,
12
+ nativeAccessibilityRole,
13
+ nativeAccessibilityState,
14
+ } from './layout-child'
15
+
16
+ import { observePreviewPinch } from './preview-pinch'
10
17
  import { observePreviewTap } from './preview-tap'
11
18
  import { java } from '@octane-xplat/platform/android'
12
19
 
13
-
14
20
  class CameraPreview extends View {
15
- createNativeView() {
16
- const nativeClass = (globalThis as any).com.octanexplat.camera.CameraPreviewView
17
- return new nativeClass(this._context)
18
- }
21
+ createNativeView() {
22
+ const nativeClass = (globalThis as any).com.octanexplat.camera.CameraPreviewView
23
+
24
+ return new nativeClass(this._context)
25
+ }
19
26
  }
20
27
 
21
- declare module '@nativescript-community/octane/intrinsics' {
22
- interface NativeScriptElements {
23
- camerapreview: Attributes<typeof CameraPreview>
24
- }
28
+ declare module '@octane-xplat/jsx/intrinsics' {
29
+ interface NativeElements<TElement> {
30
+ camerapreview: Attributes<TElement, {}>
31
+ }
25
32
  }
26
33
 
27
34
  registerElement('camerapreview', CameraPreview)
28
35
 
29
36
  /** Live Android preview backed by CameraX. */
30
- export function CameraView(props: CameraViewProps) @{
31
- const host = useRef<any>(null)
32
- const active = props.active !== false
33
- const facing = props.facing ?? 'back'
34
- useImperativeHandle(props.ref, () => ({ native: host.current.android }), []);
35
-
36
- const bindHost = useCallback((view: any) => {
37
- host.current = view
38
- }, [])
39
-
40
- useEffect(() => {
41
- const nativeView = host.current?.android
42
- if (!nativeView) return
43
- let disposed = false
44
- let provider: any
45
- let preview: any
46
- nativeView.setCameraActive(active)
47
- if (!active) return () => nativeView.setCameraActive(false)
48
-
49
- const fail = (message: string) => props.onError?.({ message })
50
- if (props.session) {
51
- if (props.facing !== undefined) {
52
- fail('camera session conflicts with facing')
53
- return () => nativeView.setCameraActive(false)
54
- }
55
-
56
- try {
57
- const detach = attachSessionPreview(props.session, nativeView, {
58
- onReady: props.onReady,
59
- onError: (issue) => fail(issue.message),
60
- })
61
-
62
- return () => { detach(); nativeView.setCameraActive(false) }
63
- } catch (error: any) {
64
- fail(error?.message ?? 'camera session error')
65
- return () => nativeView.setCameraActive(false)
66
- }
67
- }
68
-
69
- if (!cameraHardwarePresent()) {
70
- fail('camera unsupported')
71
- return () => nativeView.setCameraActive(false)
72
- }
73
-
74
- ensureCameraPermission().then((granted) => {
75
- if (disposed) return
76
- if (!granted) {
77
- fail('camera permission denied')
78
- return
79
- }
80
-
81
- try {
82
- const context = Application.android.context
83
- const cameraX = (globalThis as any).androidx.camera
84
- const future = cameraX.lifecycle.ProcessCameraProvider.getInstance(context)
85
- const executor = (globalThis as any).androidx.core.content.ContextCompat.getMainExecutor(context)
86
- future.addListener(
87
- new java.lang.Runnable({
88
- run() {
89
- if (disposed) return
90
- try {
91
- provider = future.get()
92
- const selectorClass = cameraX.core.CameraSelector
93
- const lens = facing === 'front'
94
- ? selectorClass.LENS_FACING_FRONT
95
- : selectorClass.LENS_FACING_BACK
96
-
97
- const selector = new selectorClass.Builder().requireLensFacing(lens).build()
98
- preview = new cameraX.core.Preview.Builder().build()
99
- preview.setSurfaceProvider(nativeView.getSurfaceProvider())
100
- provider.bindToLifecycle(nativeView, selector, preview)
101
- props.onReady?.()
102
- } catch (error: any) {
103
- fail(error?.message ?? 'camera error')
104
- }
105
- },
106
- }),
107
- executor,
108
- )
109
- } catch (error: any) {
110
- fail(error?.message ?? 'camera error')
111
- }
112
- })
113
-
114
- return () => {
115
- disposed = true
116
- nativeView.setCameraActive(false)
117
- if (provider && preview) {
118
- try {
119
- provider.unbind(preview)
120
- } catch {}
121
- }
122
- }
123
- }, [active, facing, props.session])
124
-
125
- useEffect(() => {
126
- const view = host.current
127
- if (!view) return
128
- applyEscapeProps(view.android, props)
129
- }, [props.ref, props.android])
130
-
131
- useEffect(() => observePreviewTap(host.current, props.onTap), [props.onTap])
132
-
133
- <masonview id={props.id} className={['vx-layers', 'vx-cameraview', props.className]} {...layoutChildProps(props, {display: "grid", gridTemplateRows: "1fr", gridTemplateColumns: "1fr", ...({ backgroundColor: '#000', ...props.style })})} accessible={props.accessible} accessibilityLabel={props.accessibilityLabel} accessibilityRole={nativeAccessibilityRole(props.accessibilityRole) as any} accessibilityHint={props.accessibilityHint} accessibilityValue={props.accessibilityValue} accessibilityState={nativeAccessibilityState(props.accessibilityState) as any} accessibilityLiveRegion={props.accessibilityLiveRegion as any}>
134
- <camerapreview ref={bindHost} style={{gridRow: '1', gridColumn: '1'}} />
135
- </masonview>
37
+ export function CameraView(props: CameraViewProps): unknown @{
38
+ const host = useRef<any>(null)
39
+ const active = props.active !== false
40
+ const facing = props.facing ?? 'back'
41
+
42
+ useImperativeHandle(props.ref, () => ({ native: host.current.android }), [])
43
+
44
+ const bindHost = useCallback((view: any) => {
45
+ host.current = view
46
+ }, [])
47
+
48
+ useEffect(() => {
49
+ const nativeView = host.current?.android
50
+
51
+ if (!nativeView) {
52
+ return
53
+ }
54
+
55
+ let disposed = false
56
+ let provider: any
57
+ let preview: any
58
+
59
+ nativeView.setCameraActive(active)
60
+ if (!active) {
61
+ return () => nativeView.setCameraActive(false)
62
+ }
63
+
64
+ const fail = (message: string) => props.onError?.({ message })
65
+
66
+ if (props.session) {
67
+ if (props.facing !== undefined) {
68
+ fail('camera session conflicts with facing')
69
+ return () => nativeView.setCameraActive(false)
70
+ }
71
+
72
+ try {
73
+ const detach = attachSessionPreview(props.session, nativeView, {
74
+ onReady: props.onReady,
75
+ onDisplayReady: props.onDisplayReady,
76
+ onError: (issue) => fail(issue.message),
77
+ })
78
+
79
+ return () => {
80
+ detach()
81
+ nativeView.setCameraActive(false)
82
+ }
83
+ } catch (error: any) {
84
+ fail(error?.message ?? 'camera session error')
85
+ return () => nativeView.setCameraActive(false)
86
+ }
87
+ }
88
+
89
+ if (!cameraHardwarePresent()) {
90
+ fail('camera unsupported')
91
+ return () => nativeView.setCameraActive(false)
92
+ }
93
+
94
+ ensureCameraPermission().then((granted) => {
95
+ if (disposed) {
96
+ return
97
+ }
98
+
99
+ if (!granted) {
100
+ fail('camera permission denied')
101
+ return
102
+ }
103
+
104
+ try {
105
+ const context = Application.android.context
106
+ const cameraX = (globalThis as any).androidx.camera
107
+ const future = cameraX.lifecycle.ProcessCameraProvider.getInstance(context)
108
+ const executor = (globalThis as any).androidx.core.content.ContextCompat.getMainExecutor(
109
+ context,
110
+ )
111
+
112
+ future.addListener(
113
+ new java.lang.Runnable({
114
+ run() {
115
+ if (disposed) {
116
+ return
117
+ }
118
+
119
+ try {
120
+ provider = future.get()
121
+
122
+ const selectorClass = cameraX.core.CameraSelector
123
+ const lens =
124
+ facing === 'front'
125
+ ? selectorClass.LENS_FACING_FRONT
126
+ : selectorClass.LENS_FACING_BACK
127
+
128
+ const selector = new selectorClass.Builder().requireLensFacing(lens).build()
129
+
130
+ preview = new cameraX.core.Preview.Builder().build()
131
+ preview.setSurfaceProvider(nativeView.getSurfaceProvider())
132
+ provider.bindToLifecycle(nativeView, selector, preview)
133
+ props.onReady?.()
134
+
135
+ // CameraX STREAMING is the displayed-frame edge; the
136
+ // PreviewView is its own LifecycleOwner here. LiveData can
137
+ // re-emit a value, so dedupe into displayed transitions.
138
+ let displayed = false
139
+
140
+ nativeView.getStreamState?.().observe(
141
+ nativeView,
142
+ new (globalThis as any).androidx.lifecycle.Observer({
143
+ onChanged(state: any) {
144
+ const streaming =
145
+ state ===
146
+ (globalThis as any).androidx.camera.view.PreviewView.StreamState.STREAMING
147
+
148
+ if (!disposed && streaming && !displayed) {
149
+ props.onDisplayReady?.()
150
+ }
151
+
152
+ displayed = streaming
153
+ },
154
+ }),
155
+ )
156
+ } catch (error: any) {
157
+ fail(error?.message ?? 'camera error')
158
+ }
159
+ },
160
+ }),
161
+ executor,
162
+ )
163
+ } catch (error: any) {
164
+ fail(error?.message ?? 'camera error')
165
+ }
166
+ })
167
+
168
+ return () => {
169
+ disposed = true
170
+ nativeView.setCameraActive(false)
171
+ if (provider && preview) {
172
+ try {
173
+ provider.unbind(preview)
174
+ } catch {}
175
+ }
176
+ }
177
+ }, [active, facing, props.session])
178
+
179
+ useEffect(() => {
180
+ const view = host.current
181
+
182
+ if (!view) {
183
+ return
184
+ }
185
+
186
+ applyEscapeProps(view.android, props)
187
+ }, [props.ref, props.android])
188
+
189
+ useEffect(() => observePreviewTap(host.current, props.onTap), [props.onTap])
190
+ useEffect(() => observePreviewPinch(host.current, props.onPinch), [props.onPinch])
191
+
192
+ <masonview
193
+ id={props.id}
194
+ className={['vx-layers', 'vx-cameraview', props.className]}
195
+ {...layoutChildProps(props, {
196
+ display: 'grid',
197
+ gridTemplateRows: '1fr',
198
+ gridTemplateColumns: '1fr',
199
+ backgroundColor: '#000',
200
+ ...props.style,
201
+ })}
202
+ accessible={props.accessible}
203
+ accessibilityLabel={props.accessibilityLabel}
204
+ accessibilityRole={nativeAccessibilityRole(props.accessibilityRole) as any}
205
+ accessibilityHint={props.accessibilityHint}
206
+ accessibilityValue={props.accessibilityValue}
207
+ accessibilityState={nativeAccessibilityState(props.accessibilityState) as any}
208
+ accessibilityLiveRegion={props.accessibilityLiveRegion as any}
209
+ >
210
+ <camerapreview ref={bindHost} style={{ gridRow: '1', gridColumn: '1' }} />
211
+ </masonview>
136
212
  }
@@ -1,87 +1,140 @@
1
- /** @jsxImportSource @nativescript-community/octane */
1
+ /** @jsxImportSource @octane-xplat/jsx */
2
2
  import { useImperativeHandle, useCallback, useEffect, useRef } from 'octane'
3
3
  import type { CameraViewProps } from './props'
4
4
  import { cameraHardwarePresent, ensureCameraPermission } from './camera-permission.mobile'
5
5
  import { startCameraPreview } from './camera-session.ios'
6
6
  import { attachSessionPreview } from './session'
7
- import { applyEscapeProps, layoutChildProps, nativeAccessibilityRole, nativeAccessibilityState } from './layout-child'
7
+ import {
8
+ applyEscapeProps,
9
+ layoutChildProps,
10
+ nativeAccessibilityRole,
11
+ nativeAccessibilityState,
12
+ } from './layout-child'
13
+
14
+ import { observePreviewPinch } from './preview-pinch'
8
15
  import { observePreviewTap } from './preview-tap'
9
16
 
10
17
  /** Live iOS preview backed by AVFoundation. With `session`, the preview
11
18
  * attaches to the session-owned AVCaptureSession so movie recording can
12
19
  * share it; without one the view owns its session as before. */
13
- export function CameraView(props: CameraViewProps) @{
14
- const host = useRef<any>(null)
15
- const active = props.active !== false
16
- const facing = props.facing ?? 'back'
17
- useImperativeHandle(props.ref, () => ({ native: host.current.ios ?? host.current.nativeViewProtected }), []);
18
-
19
- const bindHost = useCallback((view: any) => {
20
- host.current = view
21
- }, [])
22
-
23
- useEffect(() => {
24
- const view = host.current
25
- if (!view || !active) return
26
- let disposed = false
27
- let stopPreview: (() => void) | undefined
28
- const fail = (message: string) => props.onError?.({ message })
29
-
30
- if (props.session) {
31
- if (props.facing !== undefined) {
32
- fail('camera session conflicts with facing')
33
- return
34
- }
35
-
36
- try {
37
- const detach = attachSessionPreview(props.session, view, {
38
- onReady: () => {
39
- if (!disposed) props.onReady?.()
40
- },
41
- })
42
-
43
- return () => {
44
- disposed = true
45
- detach()
46
- }
47
- } catch (error: any) {
48
- fail(error?.message ?? 'camera session error')
49
- return
50
- }
51
- }
52
-
53
- if (!cameraHardwarePresent()) {
54
- fail('camera unsupported')
55
- return
56
- }
57
-
58
- ensureCameraPermission().then((granted) => {
59
- if (disposed) return
60
- if (!granted) {
61
- fail('camera permission denied')
62
- return
63
- }
64
-
65
- try {
66
- stopPreview = startCameraPreview(view, facing, props.onReady)
67
- } catch (error: any) {
68
- fail(error?.message ?? 'camera error')
69
- }
70
- })
71
-
72
- return () => {
73
- disposed = true
74
- stopPreview?.()
75
- }
76
- }, [active, facing, props.session])
77
-
78
- useEffect(() => {
79
- const view = host.current
80
- if (!view) return
81
- applyEscapeProps(view.ios ?? view.nativeViewProtected, props)
82
- }, [props.ref, props.ios])
83
-
84
- useEffect(() => observePreviewTap(host.current, props.onTap), [props.onTap])
85
-
86
- <masonview ref={bindHost} id={props.id} className={['vx-layers', 'vx-cameraview', props.className]} {...layoutChildProps(props, {display: "grid", gridTemplateRows: "1fr", gridTemplateColumns: "1fr", ...({ backgroundColor: '#000', ...props.style })})} accessible={props.accessible} accessibilityLabel={props.accessibilityLabel} accessibilityRole={nativeAccessibilityRole(props.accessibilityRole) as any} accessibilityHint={props.accessibilityHint} accessibilityValue={props.accessibilityValue} accessibilityState={nativeAccessibilityState(props.accessibilityState) as any} accessibilityLiveRegion={props.accessibilityLiveRegion as any}/>
20
+ export function CameraView(props: CameraViewProps): unknown @{
21
+ const host = useRef<any>(null)
22
+ const active = props.active !== false
23
+ const facing = props.facing ?? 'back'
24
+
25
+ useImperativeHandle(
26
+ props.ref,
27
+ () => ({ native: host.current.ios ?? host.current.nativeViewProtected }),
28
+ [],
29
+ )
30
+
31
+ const bindHost = useCallback((view: any) => {
32
+ host.current = view
33
+ }, [])
34
+
35
+ useEffect(() => {
36
+ const view = host.current
37
+
38
+ if (!view || !active) {
39
+ return
40
+ }
41
+
42
+ let disposed = false
43
+ let stopPreview: (() => void) | undefined
44
+ const fail = (message: string) => props.onError?.({ message })
45
+
46
+ if (props.session) {
47
+ if (props.facing !== undefined) {
48
+ fail('camera session conflicts with facing')
49
+ return
50
+ }
51
+
52
+ try {
53
+ const detach = attachSessionPreview(props.session, view, {
54
+ onReady: () => {
55
+ if (!disposed) {
56
+ props.onReady?.()
57
+ }
58
+ },
59
+ onDisplayReady: () => {
60
+ if (!disposed) {
61
+ props.onDisplayReady?.()
62
+ }
63
+ },
64
+ })
65
+
66
+ return () => {
67
+ disposed = true
68
+ detach()
69
+ }
70
+ } catch (error: any) {
71
+ fail(error?.message ?? 'camera session error')
72
+ return
73
+ }
74
+ }
75
+
76
+ if (!cameraHardwarePresent()) {
77
+ fail('camera unsupported')
78
+ return
79
+ }
80
+
81
+ ensureCameraPermission().then((granted) => {
82
+ if (disposed) {
83
+ return
84
+ }
85
+
86
+ if (!granted) {
87
+ fail('camera permission denied')
88
+ return
89
+ }
90
+
91
+ try {
92
+ stopPreview = startCameraPreview(view, facing, props.onReady, () => {
93
+ if (!disposed) {
94
+ props.onDisplayReady?.()
95
+ }
96
+ })
97
+ } catch (error: any) {
98
+ fail(error?.message ?? 'camera error')
99
+ }
100
+ })
101
+
102
+ return () => {
103
+ disposed = true
104
+ stopPreview?.()
105
+ }
106
+ }, [active, facing, props.session])
107
+
108
+ useEffect(() => {
109
+ const view = host.current
110
+
111
+ if (!view) {
112
+ return
113
+ }
114
+
115
+ applyEscapeProps(view.ios ?? view.nativeViewProtected, props)
116
+ }, [props.ref, props.ios])
117
+
118
+ useEffect(() => observePreviewTap(host.current, props.onTap), [props.onTap])
119
+ useEffect(() => observePreviewPinch(host.current, props.onPinch), [props.onPinch])
120
+
121
+ <masonview
122
+ ref={bindHost}
123
+ id={props.id}
124
+ className={['vx-layers', 'vx-cameraview', props.className]}
125
+ {...layoutChildProps(props, {
126
+ display: 'grid',
127
+ gridTemplateRows: '1fr',
128
+ gridTemplateColumns: '1fr',
129
+ backgroundColor: '#000',
130
+ ...props.style,
131
+ })}
132
+ accessible={props.accessible}
133
+ accessibilityLabel={props.accessibilityLabel}
134
+ accessibilityRole={nativeAccessibilityRole(props.accessibilityRole) as any}
135
+ accessibilityHint={props.accessibilityHint}
136
+ accessibilityValue={props.accessibilityValue}
137
+ accessibilityState={nativeAccessibilityState(props.accessibilityState) as any}
138
+ accessibilityLiveRegion={props.accessibilityLiveRegion as any}
139
+ />
87
140
  }