@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
@@ -23,12 +23,15 @@ export type CameraPermissionStatus = 'notDetermined' | 'granted' | 'denied' | 'r
23
23
  export type CameraCaptureErrorKind = 'permissionDenied' | 'permissionRestricted' | 'permissionBlocked' | 'unsupportedPlatform' | 'unsupportedConfiguration' | 'unavailable' | 'busy' | 'invalidState' | 'invalidArgument' | 'destinationUnavailable' | 'insufficientStorage' | 'noMedia' | 'captureFailed' | 'finalizationFailed' | 'storageFailed' | 'configurationMissing';
24
24
  /** Structured diagnostic carried by failures and non-fatal warnings. */
25
25
  export interface CaptureIssue {
26
+ /** Error category discriminator. */
26
27
  kind: CameraCaptureErrorKind;
27
28
  /** Operation being attempted, e.g. 'startRecording', 'finalize'. */
28
29
  operation: string;
30
+ /** Human-readable failure description. */
29
31
  message: string;
32
+ /** Camera/mic authorization state. */
30
33
  permission?: CameraPermissionKind;
31
- /** Host-specific detail for logging; never required for control flow. */
34
+ /** Underlying platform error when one exists — diagnostic only, never required for control flow. */
32
35
  cause?: unknown;
33
36
  }
34
37
  /** Error thrown or settled by the session API. Satisfies `CaptureIssue`
@@ -37,11 +40,13 @@ export declare class CameraCaptureError extends Error implements CaptureIssue {
37
40
  kind: CameraCaptureErrorKind;
38
41
  operation: string;
39
42
  permission?: CameraPermissionKind;
43
+ /** Underlying platform error when one exists — diagnostic only. */
40
44
  cause?: unknown;
41
45
  constructor(issue: Omit<CaptureIssue, 'cause'> & {
42
46
  cause?: unknown;
43
47
  });
44
48
  }
49
+ /** Lens direction vocabulary. */
45
50
  export type CameraFacing = 'front' | 'back';
46
51
  /** A normalized position on the live preview as the user sees it:
47
52
  * `{x: 0, y: 0}` is the top-left corner and `{x: 1, y: 1}` is
@@ -49,15 +54,31 @@ export type CameraFacing = 'front' | 'back';
49
54
  * mirroring. The `CameraView` `onTap` event and
50
55
  * `CameraSession.focusAtPoint` share this space. */
51
56
  export interface CameraPreviewPoint {
57
+ /** Focus/exposure point x, normalized 0-1. */
52
58
  x: number;
59
+ /** Focus/exposure point y, normalized 0-1. */
53
60
  y: number;
54
61
  }
62
+ /** Normalized pinch on the preview: `scale` is cumulative from gesture
63
+ * start (1 = no change) and `focus` is the focal point in the same
64
+ * preview space as `CameraPreviewPoint`. */
65
+ export interface CameraPinchEvent {
66
+ /** Scale factor relative to gesture start. */
67
+ scale: number;
68
+ /** Gesture phase — began → moved → ended/cancelled. */
69
+ state: 'began' | 'moved' | 'ended' | 'cancelled';
70
+ /** Focal point normalized to the preview; falls back to the center when
71
+ * the platform does not report one. */
72
+ focus: CameraPreviewPoint;
73
+ }
74
+ /** Capture orientation vocabulary. */
55
75
  export type CameraOrientation = 'portrait' | 'portraitUpsideDown' | 'landscapeLeft' | 'landscapeRight' | 'unspecified';
56
76
  /** Camera selection for `CameraSessionConfig.camera`. Desktop and browser
57
77
  * hosts expose device identities instead of front/back semantics. */
58
78
  export type CameraSelection = 'default' | 'front' | 'back' | {
59
79
  deviceId: string;
60
80
  };
81
+ /** Configuration for opening a session. */
61
82
  export interface CameraSessionConfig {
62
83
  /** Lens or device identity. Default `'default'`: rear when available. */
63
84
  camera?: CameraSelection;
@@ -65,6 +86,14 @@ export interface CameraSessionConfig {
65
86
  * false — silent capture is a deliberate app choice, never a
66
87
  * fallback from denied microphone access. */
67
88
  audio?: boolean;
89
+ /** iOS only: allow haptics AND system sounds while this session owns microphone
90
+ * capture (including preview). Other platforms ignore this iOS audio-session
91
+ * policy. Defaults to leaving the existing app policy unchanged. The previous
92
+ * value is restored after the last opted-in camera owner stops audio capture.
93
+ * External audio-session owners must coordinate policy changes with the app. */
94
+ ios?: {
95
+ allowHapticsAndSystemSoundsDuringRecording?: boolean;
96
+ };
68
97
  /** Capture profile. `'standard'` targets 720p at 30 fps and may select
69
98
  * a device-supported alternative; the started attempt discloses the
70
99
  * actual configuration. */
@@ -73,28 +102,57 @@ export interface CameraSessionConfig {
73
102
  /** Resolution/frame-rate combination advertised by capability discovery.
74
103
  * Entries are supported combinations, not independent lists. */
75
104
  export interface CameraProfileCapability {
105
+ /** Capture profile (resolution/framerate preset). */
76
106
  profile: 'standard';
107
+ /** Frame width in pixels. */
77
108
  width: number;
109
+ /** Frame height in pixels. */
78
110
  height: number;
111
+ /** Frames per second. */
79
112
  frameRate: number;
80
113
  }
114
+ /** Live zoom state of the active camera. Ratios are relative to the
115
+ * device's unzoomed field of view: `1` is no zoom. Bounds belong to the
116
+ * currently bound device — a camera swap can change them. */
117
+ export interface CameraZoomState {
118
+ /** Current zoom ratio. */
119
+ zoom: number;
120
+ /** Minimum ratio the active device accepts (1, or below 1 when the
121
+ * device switches to a wider constituent camera). */
122
+ minZoom: number;
123
+ /** Maximum ratio the active device accepts. */
124
+ maxZoom: number;
125
+ }
126
+ /** One usable camera device. */
81
127
  export interface CameraDeviceInfo {
128
+ /** Device-stable camera identifier. */
129
+ /** Store-assigned id. */
82
130
  id: string;
131
+ /** Human-readable device name. */
83
132
  label: string;
133
+ /** Lens direction. */
84
134
  facing?: CameraFacing;
85
135
  }
86
136
  /** What the session's host can do right now. `supported: false` means the
87
137
  * adapter or host cannot record (reason explains); `available` reflects
88
138
  * current hardware/permission state and changes over time. */
89
139
  export interface CameraSessionCapabilities {
140
+ /** A camera backend exists on this leaf. */
90
141
  supported: boolean;
91
142
  /** Why capture is unsupported or currently unavailable. */
92
143
  reason?: string;
144
+ /** At least one usable camera is present. */
93
145
  available: boolean;
146
+ /** Detected camera devices. */
94
147
  cameras: CameraDeviceInfo[];
148
+ /** Capture profiles the session can run. */
95
149
  profiles: CameraProfileCapability[];
96
150
  /** The session can add an audio track when permission allows. */
97
151
  audio: boolean;
152
+ /** Live `switchCamera` preserves the take, continuous requested audio,
153
+ * held video during acquisition, and one output file. False means
154
+ * camera selection is available only through idle `configure`. */
155
+ cameraSwitchDuringRecording: boolean;
98
156
  /** `focusAtPoint` can move autofocus metering on the active camera —
99
157
  * false on fixed-focus devices (common for front cameras) and on
100
158
  * platforms without point-of-interest metering. */
@@ -102,6 +160,9 @@ export interface CameraSessionCapabilities {
102
160
  /** `focusAtPoint` can move auto-exposure metering on the active
103
161
  * camera. */
104
162
  exposurePointOfInterest: boolean;
163
+ /** The active camera reports live zoom control through `zoomState`
164
+ * and `setZoom` (`zoomState().maxZoom > minZoom`). */
165
+ zoom: boolean;
105
166
  /** Fixed orientations the source can lock for a take. */
106
167
  orientations: CameraOrientation[];
107
168
  /** `native` enforcement can still overshoot at media boundaries;
@@ -109,8 +170,11 @@ export interface CameraSessionCapabilities {
109
170
  durationLimit: 'native' | 'bestEffort';
110
171
  /** Where `elapsedMs` comes from while recording. */
111
172
  elapsedTime: 'nativeMedia' | 'estimated';
173
+ /** Where the clip was written. */
112
174
  output: {
175
+ /** Output MIME type. */
113
176
  mimeType: string;
177
+ /** File container format. */
114
178
  container: string;
115
179
  /** `appPrivateFile` output survives dispose and app restart;
116
180
  * `originLocal` is browser storage subject to site-data clearing. */
@@ -119,20 +183,37 @@ export interface CameraSessionCapabilities {
119
183
  destinationFileUrl: boolean;
120
184
  };
121
185
  }
186
+ /** Recorder lifecycle states. */
122
187
  export type CameraRecorderState = 'idle' | 'starting' | 'recording' | 'finalizing' | 'disposed';
123
188
  /** Frozen capture configuration reported once capture actually begins. */
124
189
  export interface EffectiveCaptureConfig {
190
+ /** The CameraDeviceInfo.id in use. */
125
191
  cameraId: string;
192
+ /** Lens direction. */
126
193
  facing?: CameraFacing;
194
+ /** Whether audio capture is enabled for video. */
127
195
  audio: boolean;
196
+ /** Frame width in pixels. */
128
197
  width: number;
198
+ /** Frame height in pixels. */
129
199
  height: number;
200
+ /** Frames per second. */
130
201
  frameRate: number;
202
+ /** Capture orientation. */
131
203
  orientation: CameraOrientation;
204
+ /** Output MIME type. */
132
205
  mimeType: string;
206
+ /** File container format. */
133
207
  container: string;
134
208
  }
209
+ /** Observable session state. */
135
210
  export interface CameraSessionSnapshot {
211
+ /** A live switch is pending. Recorder state remains `recording`. */
212
+ switchingCamera: boolean;
213
+ /** Latest camera confirmed by a started take or successful live switch.
214
+ * The attempt configuration remains the immutable starting config. */
215
+ activeCamera?: CameraDeviceInfo;
216
+ /** Current lifecycle state. */
136
217
  state: CameraRecorderState;
137
218
  /** Non-prompting last-known authorization, refreshed by `permissions()`
138
219
  * and `requestPermission()`. */
@@ -141,9 +222,13 @@ export interface CameraSessionSnapshot {
141
222
  cameraAvailable: boolean;
142
223
  /** An active preview view is attached to this session. */
143
224
  previewAttached: boolean;
225
+ /** Interrupted by the OS (call, route change, …). */
144
226
  interrupted: boolean;
227
+ /** Retry count for the current operation. */
145
228
  attempt?: {
229
+ /** Device-stable camera identifier. */
146
230
  id: string;
231
+ /** Current lifecycle state. */
147
232
  state: 'starting' | 'recording' | 'finalizing';
148
233
  elapsedMs: number;
149
234
  configuration?: EffectiveCaptureConfig;
@@ -157,24 +242,37 @@ export type MovieEndReason = 'stopped' | 'maximumDuration' | 'interrupted' | 'ba
157
242
  * through `openOutput`; `fileUrl` additionally guarantees file-backed
158
243
  * native output. `synthetic` marks test-seam clips. */
159
244
  export type MovieOutput = {
245
+ /** Error category discriminator. */
160
246
  kind: 'nativeFile';
247
+ /** Store handle for the clip (gallery/native ref). */
161
248
  resourceId: string;
249
+ /** Filesystem URL of the written clip. */
250
+ /** Filesystem URL of the written clip. */
162
251
  fileUrl: string;
252
+ /** True for generated/test clips, not real captures. */
163
253
  synthetic?: boolean;
164
254
  } | {
255
+ /** Error category discriminator. */
165
256
  kind: 'browserStorage';
257
+ /** Store handle for the clip (gallery/native ref). */
166
258
  resourceId: string;
167
259
  retention: 'persistent' | 'bestEffort';
260
+ /** True for generated/test clips, not real captures. */
168
261
  synthetic?: boolean;
169
262
  };
263
+ /** One finished recording. */
170
264
  export interface MovieClip {
265
+ /** Where the clip was written. */
171
266
  output: MovieOutput;
267
+ /** Output MIME type. */
172
268
  mimeType: string;
173
269
  /** Authoritative duration read from the finalized movie. */
174
270
  durationMs: number;
175
271
  /** Presentation dimensions after the orientation transform. */
176
272
  width: number;
273
+ /** Frame height in pixels. */
177
274
  height: number;
275
+ /** Capture orientation. */
178
276
  orientation: CameraOrientation;
179
277
  /** Whether the finalized movie actually contains an audio track.
180
278
  * Missing requested audio is a failure, not silent capture. */
@@ -184,13 +282,17 @@ export interface MovieClip {
184
282
  * usable clip ended early by an external condition — still a success;
185
283
  * app review decides whether to keep it. */
186
284
  export type MovieOutcome = {
285
+ /** Error category discriminator. */
187
286
  kind: 'clip';
188
287
  clip: MovieClip;
288
+ /** Why recording stopped. */
189
289
  endReason: MovieEndReason;
190
290
  partial: boolean;
191
291
  warning?: CaptureIssue;
192
292
  } | {
293
+ /** Error category discriminator. */
193
294
  kind: 'failed';
295
+ /** Why recording stopped. */
194
296
  endReason: MovieEndReason;
195
297
  stage: 'preparation' | 'capture' | 'finalization' | 'storage';
196
298
  error: CaptureIssue;
@@ -208,6 +310,7 @@ export type CameraSessionEvent = {
208
310
  attemptId: string;
209
311
  outcome: MovieOutcome;
210
312
  } | {
313
+ /** Output kind discriminator. */
211
314
  type: 'interruption';
212
315
  phase: 'began' | 'ended';
213
316
  reason?: CameraInterruptionReason;
@@ -215,7 +318,9 @@ export type CameraSessionEvent = {
215
318
  type: 'availability';
216
319
  available: boolean;
217
320
  };
321
+ /** Why the OS interrupted capture. */
218
322
  export type CameraInterruptionReason = 'background' | 'audioInUse' | 'cameraInUse' | 'multitasking' | 'systemPressure' | 'unknown';
323
+ /** Options for session.startRecording(). */
219
324
  export interface StartRecordingOptions {
220
325
  /** Positive finite milliseconds. Enforcement precision comes from
221
326
  * `capabilities().durationLimit`; the final duration is read from
@@ -233,11 +338,14 @@ export interface StartRecordingOptions {
233
338
  * settles exactly once with the terminal outcome; repeated `stop()`
234
339
  * joins the same finalization. */
235
340
  export interface MovieTake {
341
+ /** Attempt identifier — unique per `startRecording` call. */
236
342
  readonly id: string;
343
+ /** Lifecycle phase; terminal value is `settled` (check `completion` for the outcome). */
237
344
  readonly state: 'starting' | 'recording' | 'finalizing' | 'settled';
238
345
  /** Elapsed capture time in milliseconds; excludes preparation and
239
346
  * finalization. Source identified by `capabilities().elapsedTime`. */
240
347
  readonly elapsedMs: number;
348
+ /** Terminal outcome — resolves exactly once; `stop()` returns this same promise. */
241
349
  readonly completion: Promise<MovieOutcome>;
242
350
  /** Requests stop and joins finalization. Returns `completion`. */
243
351
  stop(): Promise<MovieOutcome>;
@@ -247,9 +355,12 @@ export interface MovieTake {
247
355
  export interface MovieOutputHandle {
248
356
  /** Playable/readable location for the clip. */
249
357
  readonly url: string;
358
+ /** On-disk path when the clip is file-backed; absent for memory/remote storage. */
250
359
  readonly fileUrl?: string;
360
+ /** Free temporary access to the clip — never deletes it. */
251
361
  release(): void;
252
362
  }
363
+ /** An owned camera session — dispose when done. */
253
364
  export interface CameraSession {
254
365
  /** Non-prompting permission inspection; may return `unknown` when the
255
366
  * host cannot observe authorization. */
@@ -258,7 +369,9 @@ export interface CameraSession {
258
369
  * serialized; a status other than `notDetermined` returns without
259
370
  * prompting. */
260
371
  requestPermission(kind: CameraPermissionKind): Promise<CameraPermissionStatus>;
372
+ /** Hardware/OS limits for this session — polled live, so values can change as devices come and go. */
261
373
  capabilities(): Promise<CameraSessionCapabilities>;
374
+ /** Point-in-time session state — subscribe for changes. */
262
375
  snapshot(): CameraSessionSnapshot;
263
376
  /** Ordered lifecycle events. A caller never loses a result by
264
377
  * subscribing late — `completion` is authoritative. Listener
@@ -275,9 +388,39 @@ export interface CameraSession {
275
388
  * through `configure` returns focus/exposure to the new device's
276
389
  * default. */
277
390
  focusAtPoint(point: CameraPreviewPoint): void;
391
+ /** Live zoom state of the active camera (see `CameraZoomState`).
392
+ * Returns `undefined` while no camera is bound, after `dispose`, and
393
+ * on hosts or devices without zoom control — check
394
+ * `capabilities().zoom` before showing zoom UI. Values change with
395
+ * the bound device: re-read after `configure` or `switchCamera`. */
396
+ zoomState(): CameraZoomState | undefined;
397
+ /** Writes a zoom ratio on the active camera without reconfiguring the
398
+ * session; works while recording. Values clamp to the active
399
+ * device's live `zoomState()` bounds on every write. No-ops while no
400
+ * camera is bound, after `dispose`, and on hosts without zoom
401
+ * control. Non-finite values throw `invalidArgument`. */
402
+ setZoom(ratio: number): void;
278
403
  /** Replaces configuration while idle. Validates against current
279
- * capabilities and rejects with `invalidState` during an attempt. */
404
+ * capabilities and rejects with `invalidState` during an attempt,
405
+ * including one admitted while capability discovery was pending.
406
+ * Live camera changes use `switchCamera` when advertised; rejection
407
+ * leaves the take running and does not request stop. */
280
408
  configure(config: Partial<CameraSessionConfig>): Promise<void>;
409
+ /** Change only the camera during `recording`. Audio and elapsed time
410
+ * continue while video holds its last frame. Resolves when the replacement
411
+ * camera is producing frames on the existing recording path; this is source
412
+ * readiness, not an encoded-frame acknowledgement. Completion still belongs
413
+ * to the original take. Rejects `busy` for overlapping switches,
414
+ * `invalidState` outside recording, and `unsupportedConfiguration`
415
+ * when the host cannot preserve this contract. Use idle `configure`
416
+ * before starting a take. Cancellation never restarts recording.
417
+ * @example
418
+ * import type { CameraSession } from '@octane-xplat/camera'
419
+ * async function faceFront(session: CameraSession): Promise<void> {
420
+ * await session.switchCamera('front')
421
+ * }
422
+ */
423
+ switchCamera(camera: CameraSelection): Promise<void>;
281
424
  /** Admits one capture attempt. Throws `CameraCaptureError` before
282
425
  * admission for invalid arguments, busy, invalid state, or
283
426
  * unavailable camera — no attempt is created. */
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2026 Alec Larson
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1,22 +0,0 @@
1
- import Foundation
2
-
3
- /// Shared by preview, movie and still-photo hosts. A busy source is never stolen.
4
- enum CameraOwnership {
5
- private static let lock = NSLock()
6
- private static var owners: [String: ObjectIdentifier] = [:]
7
-
8
- static func claim(_ id: String, owner: AnyObject) -> Bool {
9
- lock.lock()
10
- defer { lock.unlock() }
11
- let token = ObjectIdentifier(owner)
12
- guard owners[id] == nil || owners[id] == token else { return false }
13
- owners[id] = token
14
- return true
15
- }
16
-
17
- static func release(_ id: String, owner: AnyObject) {
18
- lock.lock()
19
- defer { lock.unlock() }
20
- if owners[id] == ObjectIdentifier(owner) { owners.removeValue(forKey: id) }
21
- }
22
- }
@@ -1,34 +0,0 @@
1
- import Foundation
2
-
3
- /// Fences every asynchronous permission, preview and shutter result.
4
- final class PhotoCaptureLifecycle {
5
- enum Phase { case idle, preparing, preview, capturing, finished }
6
- private(set) var phase = Phase.idle
7
- private(set) var generation = 0
8
- var finished: (() -> Void)?
9
-
10
- func prepare() -> Int? {
11
- guard phase == .idle else { return nil }
12
- phase = .preparing
13
- return generation
14
- }
15
- func ready(_ token: Int) -> Bool {
16
- guard accepts(token), phase == .preparing else { return false }
17
- phase = .preview
18
- return true
19
- }
20
- func capture() -> Int? {
21
- guard phase == .preview else { return nil }
22
- phase = .capturing
23
- return generation
24
- }
25
- func accepts(_ token: Int) -> Bool { phase != .finished && token == generation }
26
- @discardableResult func finish() -> Bool {
27
- guard phase != .finished else { return false }
28
- phase = .finished
29
- generation += 1
30
- finished?()
31
- finished = nil
32
- return true
33
- }
34
- }