@capgo/camera-preview 8.12.2 → 8.12.4
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.
- package/README.md +0 -1
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +26 -28
- package/android/src/main/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolver.java +72 -0
- package/android/src/test/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolverTest.java +90 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionRestoreCoordinator.swift +136 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionSnapshot.swift +28 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +52 -10
- package/ios/Sources/CapgoCameraPreviewPlugin/PhysicalOrientationResolver.swift +59 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +1 -1
- package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionRestoreCoordinatorTests.swift +279 -0
- package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionSnapshotTests.swift +25 -0
- package/ios/Tests/CameraPreviewPluginTests/PhysicalOrientationResolverTests.swift +89 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@ Show a live camera preview inside your Capacitor app, behind or around your own
|
|
|
8
8
|
<p><b>Capgo</b>: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.</p>
|
|
9
9
|
<h2><a href="https://capgo.app/register/?ref=plugin_camera_preview">➡️ Get started for free</a></h2>
|
|
10
10
|
<p>14-day unlimited free trial. No credit card required</p>
|
|
11
|
-
<p><a href="https://capgo.app/consulting/?ref=plugin_camera_preview">Missing a feature? We'll build the plugin for you 💪</a></p>
|
|
12
11
|
</div>
|
|
13
12
|
|
|
14
13
|
> 🤖 **Building with an AI coding agent?** Install [Capgo skills](https://capgo.app/skills/?ref=plugin_camera_preview) so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use `@capgo/camera-preview` in your Capacitor app: `npx skills add Cap-go/capgo-skills`. Then ask your agent: *"Add @capgo/camera-preview to my Capacitor app and wire it up."* [Agent setup guides](https://capgo.app/agents/?ref=plugin_camera_preview) · [Hosted MCP](https://capgo.app/docs/ai/mcp/?ref=plugin_camera_preview)
|
|
@@ -223,6 +223,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
223
223
|
private final float[] lastAccelerometerValues = new float[3]; // x,y, and z
|
|
224
224
|
private final Object accelerometerLock = new Object();
|
|
225
225
|
private volatile int lastCaptureRotation = -1; // -1 unknown
|
|
226
|
+
private volatile int lastReliableCaptureRotation = -1;
|
|
226
227
|
|
|
227
228
|
// Compass heading (degrees, 0-360, true north); -1 means not yet available
|
|
228
229
|
private Sensor rotationVectorSensor;
|
|
@@ -565,6 +566,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
565
566
|
lastAccelerometerValues[2] = 0f;
|
|
566
567
|
}
|
|
567
568
|
lastCaptureRotation = -1;
|
|
569
|
+
lastReliableCaptureRotation = -1;
|
|
568
570
|
|
|
569
571
|
// Start accelerometer for orientation detection regardless of lock
|
|
570
572
|
if (sensorManager == null) {
|
|
@@ -2207,43 +2209,38 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
2207
2209
|
* This works even when portrait lock is enabled.
|
|
2208
2210
|
* Falls back to display rotation if accelerometer data is not available.
|
|
2209
2211
|
*/
|
|
2212
|
+
private int getDisplayRotationFallback() {
|
|
2213
|
+
if (previewView != null && previewView.getDisplay() != null) {
|
|
2214
|
+
return previewView.getDisplay().getRotation();
|
|
2215
|
+
}
|
|
2216
|
+
return android.view.Surface.ROTATION_0;
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2210
2219
|
private int getRotationFromAccelerometer() {
|
|
2211
|
-
float x, y;
|
|
2220
|
+
float x, y, z;
|
|
2212
2221
|
synchronized (accelerometerLock) {
|
|
2213
2222
|
x = lastAccelerometerValues[0];
|
|
2214
2223
|
y = lastAccelerometerValues[1];
|
|
2224
|
+
z = lastAccelerometerValues[2];
|
|
2215
2225
|
}
|
|
2216
2226
|
|
|
2217
|
-
|
|
2218
|
-
final float
|
|
2219
|
-
if (Math.abs(x) <
|
|
2220
|
-
|
|
2221
|
-
return previewView.getDisplay().getRotation();
|
|
2222
|
-
}
|
|
2223
|
-
return android.view.Surface.ROTATION_0;
|
|
2227
|
+
final float gravityEarth = SensorManager.GRAVITY_EARTH;
|
|
2228
|
+
final float epsilonG = 0.1f;
|
|
2229
|
+
if (Math.abs(x) < gravityEarth * epsilonG && Math.abs(y) < gravityEarth * epsilonG && Math.abs(z) < gravityEarth * epsilonG) {
|
|
2230
|
+
return getDisplayRotationFallback();
|
|
2224
2231
|
}
|
|
2225
2232
|
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
return android.view.Surface.ROTATION_270;
|
|
2236
|
-
}
|
|
2237
|
-
} else {
|
|
2238
|
-
// Portrait orientation
|
|
2239
|
-
if (y > 0) {
|
|
2240
|
-
// Normal portrait (top of device points up)
|
|
2241
|
-
return android.view.Surface.ROTATION_0;
|
|
2242
|
-
} else {
|
|
2243
|
-
// Upside down portrait
|
|
2244
|
-
return android.view.Surface.ROTATION_180;
|
|
2245
|
-
}
|
|
2233
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
|
|
2234
|
+
x / gravityEarth,
|
|
2235
|
+
y / gravityEarth,
|
|
2236
|
+
z / gravityEarth,
|
|
2237
|
+
lastReliableCaptureRotation,
|
|
2238
|
+
getDisplayRotationFallback()
|
|
2239
|
+
);
|
|
2240
|
+
if (result.reliable) {
|
|
2241
|
+
lastReliableCaptureRotation = result.rotation;
|
|
2246
2242
|
}
|
|
2243
|
+
return result.rotation;
|
|
2247
2244
|
}
|
|
2248
2245
|
|
|
2249
2246
|
public void capturePhoto(
|
|
@@ -5410,6 +5407,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
5410
5407
|
|
|
5411
5408
|
// Update video capture rotation from accelerometer for device orientation
|
|
5412
5409
|
int rotation = getRotationFromAccelerometer();
|
|
5410
|
+
lastCaptureRotation = rotation;
|
|
5413
5411
|
videoCapture.setTargetRotation(rotation);
|
|
5414
5412
|
Log.d(TAG, "startRecordVideo: Using rotation " + rotation + " from accelerometer");
|
|
5415
5413
|
|
package/android/src/main/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolver.java
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package app.capgo.capacitor.camera.preview;
|
|
2
|
+
|
|
3
|
+
import android.view.Surface;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolves device physical rotation from gravity (accelerometer) for photo and video capture.
|
|
7
|
+
* When the device is near flat, x/y gravity is noisy; keep the last reliable rotation or UI fallback.
|
|
8
|
+
*/
|
|
9
|
+
final class PhysicalOrientationResolver {
|
|
10
|
+
|
|
11
|
+
/** |z| above this means gravity mostly points through the screen (camera toward table or ceiling). */
|
|
12
|
+
static final float FLAT_GRAVITY_Z_THRESHOLD = 0.75f;
|
|
13
|
+
/** Below this, horizontal gravity is too weak to infer portrait vs landscape reliably. */
|
|
14
|
+
static final float MIN_HORIZONTAL_GRAVITY = 0.35f;
|
|
15
|
+
/** When |x| and |y| are within this margin, do not flip between landscape and portrait. */
|
|
16
|
+
static final float AXIS_DECISION_MARGIN = 0.12f;
|
|
17
|
+
|
|
18
|
+
static final class Result {
|
|
19
|
+
|
|
20
|
+
final int rotation;
|
|
21
|
+
/** True when rotation came from a strong gravity signal (safe to remember for hysteresis). */
|
|
22
|
+
final boolean reliable;
|
|
23
|
+
|
|
24
|
+
Result(int rotation, boolean reliable) {
|
|
25
|
+
this.rotation = rotation;
|
|
26
|
+
this.reliable = reliable;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private PhysicalOrientationResolver() {}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param x gravity component on x in g (divide raw m/s^2 by {@link android.hardware.SensorManager#GRAVITY_EARTH})
|
|
34
|
+
* @param y gravity component on y in g
|
|
35
|
+
* @param z gravity component on z in g
|
|
36
|
+
* @param lastReliableRotation {@link Surface#ROTATION_0} etc., or -1 if unknown
|
|
37
|
+
* @param fallbackRotation display rotation when gravity is ambiguous
|
|
38
|
+
*/
|
|
39
|
+
static Result resolve(float x, float y, float z, int lastReliableRotation, int fallbackRotation) {
|
|
40
|
+
float absX = Math.abs(x);
|
|
41
|
+
float absY = Math.abs(y);
|
|
42
|
+
float absZ = Math.abs(z);
|
|
43
|
+
float horizontalMax = Math.max(absX, absY);
|
|
44
|
+
|
|
45
|
+
boolean isNearFlat = absZ >= FLAT_GRAVITY_Z_THRESHOLD || horizontalMax < MIN_HORIZONTAL_GRAVITY;
|
|
46
|
+
if (isNearFlat) {
|
|
47
|
+
int rotation = lastReliableRotation != -1 ? lastReliableRotation : fallbackRotation;
|
|
48
|
+
return new Result(rotation, false);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (Math.abs(absX - absY) < AXIS_DECISION_MARGIN) {
|
|
52
|
+
int rotation = lastReliableRotation != -1 ? lastReliableRotation : fallbackRotation;
|
|
53
|
+
return new Result(rotation, false);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
int rotation;
|
|
57
|
+
if (absX > absY) {
|
|
58
|
+
if (x > 0) {
|
|
59
|
+
rotation = Surface.ROTATION_90;
|
|
60
|
+
} else {
|
|
61
|
+
rotation = Surface.ROTATION_270;
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
if (y > 0) {
|
|
65
|
+
rotation = Surface.ROTATION_0;
|
|
66
|
+
} else {
|
|
67
|
+
rotation = Surface.ROTATION_180;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return new Result(rotation, true);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
package app.capgo.capacitor.camera.preview;
|
|
2
|
+
|
|
3
|
+
import static org.junit.Assert.assertEquals;
|
|
4
|
+
import static org.junit.Assert.assertFalse;
|
|
5
|
+
import static org.junit.Assert.assertTrue;
|
|
6
|
+
|
|
7
|
+
import android.view.Surface;
|
|
8
|
+
import org.junit.Test;
|
|
9
|
+
import org.junit.runner.RunWith;
|
|
10
|
+
import org.robolectric.RobolectricTestRunner;
|
|
11
|
+
import org.robolectric.annotation.Config;
|
|
12
|
+
|
|
13
|
+
@RunWith(RobolectricTestRunner.class)
|
|
14
|
+
@Config(manifest = Config.NONE, sdk = 28)
|
|
15
|
+
public class PhysicalOrientationResolverTest {
|
|
16
|
+
|
|
17
|
+
@Test
|
|
18
|
+
public void nearFlatUsesFallbackWhenNoLastReliable() {
|
|
19
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.04f, -0.03f, -0.99f, -1, Surface.ROTATION_0);
|
|
20
|
+
assertEquals(Surface.ROTATION_0, result.rotation);
|
|
21
|
+
assertFalse(result.reliable);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@Test
|
|
25
|
+
public void nearFlatKeepsLastReliableRotation() {
|
|
26
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
|
|
27
|
+
0.05f,
|
|
28
|
+
0.02f,
|
|
29
|
+
0.92f,
|
|
30
|
+
Surface.ROTATION_90,
|
|
31
|
+
Surface.ROTATION_0
|
|
32
|
+
);
|
|
33
|
+
assertEquals(Surface.ROTATION_90, result.rotation);
|
|
34
|
+
assertFalse(result.reliable);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Test
|
|
38
|
+
public void clearPortrait() {
|
|
39
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.08f, 0.95f, 0.15f, -1, Surface.ROTATION_0);
|
|
40
|
+
assertEquals(Surface.ROTATION_0, result.rotation);
|
|
41
|
+
assertTrue(result.reliable);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Test
|
|
45
|
+
public void clearLandscapeLeft() {
|
|
46
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.92f, 0.08f, 0.05f, -1, Surface.ROTATION_0);
|
|
47
|
+
assertEquals(Surface.ROTATION_90, result.rotation);
|
|
48
|
+
assertTrue(result.reliable);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@Test
|
|
52
|
+
public void clearLandscapeRight() {
|
|
53
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(-0.9f, 0.1f, 0.05f, -1, Surface.ROTATION_0);
|
|
54
|
+
assertEquals(Surface.ROTATION_270, result.rotation);
|
|
55
|
+
assertTrue(result.reliable);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Test
|
|
59
|
+
public void upsideDownPortrait() {
|
|
60
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.05f, -0.94f, 0.1f, -1, Surface.ROTATION_0);
|
|
61
|
+
assertEquals(Surface.ROTATION_180, result.rotation);
|
|
62
|
+
assertTrue(result.reliable);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Test
|
|
66
|
+
public void ambiguousAxisUsesLastReliable() {
|
|
67
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
|
|
68
|
+
0.52f,
|
|
69
|
+
-0.48f,
|
|
70
|
+
0.2f,
|
|
71
|
+
Surface.ROTATION_0,
|
|
72
|
+
Surface.ROTATION_90
|
|
73
|
+
);
|
|
74
|
+
assertEquals(Surface.ROTATION_0, result.rotation);
|
|
75
|
+
assertFalse(result.reliable);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Test
|
|
79
|
+
public void weakHorizontalGravityTreatedAsFlat() {
|
|
80
|
+
PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
|
|
81
|
+
0.12f,
|
|
82
|
+
-0.18f,
|
|
83
|
+
0.55f,
|
|
84
|
+
Surface.ROTATION_270,
|
|
85
|
+
Surface.ROTATION_0
|
|
86
|
+
);
|
|
87
|
+
assertEquals(Surface.ROTATION_270, result.rotation);
|
|
88
|
+
assertFalse(result.reliable);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// Serializes capture, deferred restore, and completion for the app `AVAudioSession` around preview and recording.
|
|
5
|
+
final class ApplicationAudioSessionRestoreCoordinator {
|
|
6
|
+
private let queue = DispatchQueue(label: "com.capgo.camera.applicationAudioSessionRestore")
|
|
7
|
+
|
|
8
|
+
private var snapshot: ApplicationAudioSessionSnapshot?
|
|
9
|
+
private var snapshotPreviewGeneration: UInt64?
|
|
10
|
+
private var previewGeneration: UInt64 = 0
|
|
11
|
+
private var activeRecordingId: UInt64?
|
|
12
|
+
private var nextRecordingId: UInt64 = 1
|
|
13
|
+
private var deferredRecordingIds: Set<UInt64> = []
|
|
14
|
+
private var deferredPreviewGeneration: UInt64?
|
|
15
|
+
private var captureSessionTeardownComplete = false
|
|
16
|
+
private var finishedBeforeDeferral: Set<UInt64> = []
|
|
17
|
+
|
|
18
|
+
var isDeferredRestorePending: Bool {
|
|
19
|
+
self.queue.sync { !self.deferredRecordingIds.isEmpty }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func beginPreviewSession() {
|
|
23
|
+
self.queue.sync {
|
|
24
|
+
self.previewGeneration += 1
|
|
25
|
+
if !self.deferredRecordingIds.isEmpty {
|
|
26
|
+
self.clearDeferredRestoreWithoutApplying()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func captureSnapshotIfNeeded() {
|
|
32
|
+
self.queue.sync {
|
|
33
|
+
guard self.snapshot == nil else { return }
|
|
34
|
+
self.snapshot = ApplicationAudioSessionSnapshot.capture()
|
|
35
|
+
self.snapshotPreviewGeneration = self.previewGeneration
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// Registers deferred restore before capture session teardown. Returns `true` if the snapshot should be applied after teardown.
|
|
40
|
+
func beginCaptureSessionTeardown(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) -> Bool {
|
|
41
|
+
self.queue.sync {
|
|
42
|
+
if !self.deferredRecordingIds.isEmpty {
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
var ids = pendingRecordingIds
|
|
46
|
+
if isRecording, let active = self.activeRecordingId {
|
|
47
|
+
ids.insert(active)
|
|
48
|
+
}
|
|
49
|
+
ids.subtract(self.finishedBeforeDeferral)
|
|
50
|
+
self.finishedBeforeDeferral.removeAll()
|
|
51
|
+
guard !ids.isEmpty else {
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
self.deferredRecordingIds = ids
|
|
55
|
+
self.deferredPreviewGeneration = self.previewGeneration
|
|
56
|
+
self.captureSessionTeardownComplete = false
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Call after capture session `stopRunning()` and input/output removal when restore was deferred for active recordings.
|
|
62
|
+
func completeCaptureSessionTeardown() {
|
|
63
|
+
self.queue.sync {
|
|
64
|
+
guard self.deferredPreviewGeneration != nil else { return }
|
|
65
|
+
self.captureSessionTeardownComplete = true
|
|
66
|
+
self.tryCompleteDeferredRestore()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func restoreAfterCaptureSessionTeardown() {
|
|
71
|
+
self.queue.sync {
|
|
72
|
+
self.restoreNow()
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func beginRecording() -> UInt64 {
|
|
77
|
+
self.queue.sync {
|
|
78
|
+
let id = self.nextRecordingId
|
|
79
|
+
self.nextRecordingId += 1
|
|
80
|
+
self.activeRecordingId = id
|
|
81
|
+
return id
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
func finishRecording(recordingId: UInt64) {
|
|
86
|
+
self.queue.sync {
|
|
87
|
+
self.noteRecordingFinished(recordingId)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
func finishRecordingAsync(recordingId: UInt64) {
|
|
92
|
+
self.queue.async {
|
|
93
|
+
self.noteRecordingFinished(recordingId)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
func syncWithQueueForTesting() {
|
|
98
|
+
self.queue.sync {}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private func noteRecordingFinished(_ recordingId: UInt64) {
|
|
102
|
+
if self.activeRecordingId == recordingId {
|
|
103
|
+
self.activeRecordingId = nil
|
|
104
|
+
}
|
|
105
|
+
if self.deferredRecordingIds.isEmpty {
|
|
106
|
+
self.finishedBeforeDeferral.insert(recordingId)
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
self.deferredRecordingIds.remove(recordingId)
|
|
110
|
+
self.tryCompleteDeferredRestore()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private func tryCompleteDeferredRestore() {
|
|
114
|
+
guard self.deferredRecordingIds.isEmpty else { return }
|
|
115
|
+
guard self.captureSessionTeardownComplete else { return }
|
|
116
|
+
if self.deferredPreviewGeneration != self.previewGeneration {
|
|
117
|
+
self.clearDeferredRestoreWithoutApplying()
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
self.deferredPreviewGeneration = nil
|
|
121
|
+
self.restoreNow()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private func clearDeferredRestoreWithoutApplying() {
|
|
125
|
+
self.deferredRecordingIds.removeAll()
|
|
126
|
+
self.deferredPreviewGeneration = nil
|
|
127
|
+
self.captureSessionTeardownComplete = false
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private func restoreNow() {
|
|
131
|
+
guard let snap = self.snapshot else { return }
|
|
132
|
+
self.snapshot = nil
|
|
133
|
+
self.snapshotPreviewGeneration = nil
|
|
134
|
+
snap.apply()
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// Captures and restores the app `AVAudioSession` category, mode, and options.
|
|
5
|
+
struct ApplicationAudioSessionSnapshot {
|
|
6
|
+
private let category: AVAudioSession.Category
|
|
7
|
+
private let mode: AVAudioSession.Mode
|
|
8
|
+
private let categoryOptions: AVAudioSession.CategoryOptions
|
|
9
|
+
|
|
10
|
+
static func capture(from session: AVAudioSession = .sharedInstance()) -> ApplicationAudioSessionSnapshot {
|
|
11
|
+
ApplicationAudioSessionSnapshot(
|
|
12
|
+
category: session.category,
|
|
13
|
+
mode: session.mode,
|
|
14
|
+
categoryOptions: session.categoryOptions
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@discardableResult
|
|
19
|
+
func apply(to session: AVAudioSession = .sharedInstance()) -> Bool {
|
|
20
|
+
do {
|
|
21
|
+
try session.setCategory(category, mode: mode, options: categoryOptions)
|
|
22
|
+
return true
|
|
23
|
+
} catch {
|
|
24
|
+
print("[CameraPreview] Failed to restore AVAudioSession: \(error)")
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -56,21 +56,27 @@ class CameraController: NSObject, CXCallObserverDelegate {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
// For capture only - uses accelerometer to detect physical orientation to properly position videos/images
|
|
59
|
+
private var lastReliableCaptureOrientation: AVCaptureVideoOrientation?
|
|
60
|
+
|
|
59
61
|
private func getPhysicalOrientation() -> AVCaptureVideoOrientation {
|
|
62
|
+
let fallback = getVideoOrientation()
|
|
60
63
|
guard let accelerometerData = motionManager.accelerometerData else {
|
|
61
|
-
return
|
|
64
|
+
return lastReliableCaptureOrientation ?? lastCaptureOrientation ?? fallback
|
|
62
65
|
}
|
|
63
66
|
|
|
64
|
-
let
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
68
|
+
acceleration: PhysicalOrientationResolver.Acceleration(
|
|
69
|
+
x: accelerometerData.acceleration.x,
|
|
70
|
+
y: accelerometerData.acceleration.y,
|
|
71
|
+
z: accelerometerData.acceleration.z
|
|
72
|
+
),
|
|
73
|
+
lastReliableOrientation: lastReliableCaptureOrientation,
|
|
74
|
+
fallbackOrientation: fallback
|
|
75
|
+
)
|
|
76
|
+
if result.isReliable {
|
|
77
|
+
lastReliableCaptureOrientation = result.orientation
|
|
73
78
|
}
|
|
79
|
+
return result.orientation
|
|
74
80
|
}
|
|
75
81
|
|
|
76
82
|
private func setVideoOrientation(_ orientation: AVCaptureVideoOrientation, on connection: AVCaptureConnection) {
|
|
@@ -176,6 +182,9 @@ class CameraController: NSObject, CXCallObserverDelegate {
|
|
|
176
182
|
private let sessionRecoveryQueue = DispatchQueue(label: "com.capgo.camera.sessionRecovery")
|
|
177
183
|
private var activePrepareToken: UInt = 0
|
|
178
184
|
private var audioInputEnabledByUser = false
|
|
185
|
+
private let applicationAudioSessionRestoreCoordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
186
|
+
private var recordingIdByOutputFileURL: [URL: UInt64] = [:]
|
|
187
|
+
private let recordingIdQueue = DispatchQueue(label: "com.capgo.camera.recordingIds")
|
|
179
188
|
|
|
180
189
|
var audioDevice: AVCaptureDevice?
|
|
181
190
|
var audioInput: AVCaptureDeviceInput?
|
|
@@ -692,6 +701,7 @@ extension CameraController {
|
|
|
692
701
|
func prepare(cameraPosition: String, deviceId: String? = nil, disableAudio: Bool, cameraMode: Bool, aspectRatio: String? = nil, aspectMode: String = "contain", initialZoomLevel: Float?, disableFocusIndicator: Bool = false, videoQuality: String = "high", startToken: UInt, completionHandler: @escaping (Error?) -> Void) {
|
|
693
702
|
print("[CameraPreview] 🎬 Starting prepare - position: \(cameraPosition), deviceId: \(deviceId ?? "nil"), disableAudio: \(disableAudio), cameraMode: \(cameraMode), aspectRatio: \(aspectRatio ?? "nil"), aspectMode: \(aspectMode), zoom: \(initialZoomLevel ?? 1)")
|
|
694
703
|
|
|
704
|
+
self.applicationAudioSessionRestoreCoordinator.beginPreviewSession()
|
|
695
705
|
self.activePrepareToken = startToken
|
|
696
706
|
|
|
697
707
|
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
|
|
@@ -1041,6 +1051,7 @@ extension CameraController {
|
|
|
1041
1051
|
|
|
1042
1052
|
// Add audio input if needed (skip while a phone call owns the audio session)
|
|
1043
1053
|
if self.shouldAttachAudioInput(disableAudio: disableAudio) {
|
|
1054
|
+
self.captureApplicationAudioSessionSnapshotIfNeeded()
|
|
1044
1055
|
if self.audioDevice == nil {
|
|
1045
1056
|
self.audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
|
|
1046
1057
|
}
|
|
@@ -2644,6 +2655,10 @@ extension CameraController {
|
|
|
2644
2655
|
return !disableAudio && !self.isPhoneCallActive()
|
|
2645
2656
|
}
|
|
2646
2657
|
|
|
2658
|
+
private func captureApplicationAudioSessionSnapshotIfNeeded() {
|
|
2659
|
+
self.applicationAudioSessionRestoreCoordinator.captureSnapshotIfNeeded()
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2647
2662
|
private static let insufficientAudioPriorityOSStatus = 561017449
|
|
2648
2663
|
|
|
2649
2664
|
private func isAudioPriorityError(_ error: NSError) -> Bool {
|
|
@@ -2695,6 +2710,7 @@ extension CameraController {
|
|
|
2695
2710
|
do {
|
|
2696
2711
|
session.beginConfiguration()
|
|
2697
2712
|
defer { session.commitConfiguration() }
|
|
2713
|
+
self.captureApplicationAudioSessionSnapshotIfNeeded()
|
|
2698
2714
|
if self.audioDevice == nil {
|
|
2699
2715
|
self.audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
|
|
2700
2716
|
}
|
|
@@ -2798,15 +2814,30 @@ extension CameraController {
|
|
|
2798
2814
|
self.onCameraInterrupted = nil
|
|
2799
2815
|
self.onCameraInterruptionEnded = nil
|
|
2800
2816
|
self.onStartFailure = nil
|
|
2817
|
+
let isFileOutputRecording = self.fileVideoOutput?.isRecording == true
|
|
2818
|
+
let pendingRecordingIds = self.recordingIdQueue.sync {
|
|
2819
|
+
Set(self.recordingIdByOutputFileURL.values)
|
|
2820
|
+
}
|
|
2821
|
+
let shouldRestoreAfterTeardown = self.applicationAudioSessionRestoreCoordinator.beginCaptureSessionTeardown(
|
|
2822
|
+
isRecording: isFileOutputRecording,
|
|
2823
|
+
pendingRecordingIds: pendingRecordingIds
|
|
2824
|
+
)
|
|
2801
2825
|
if let captureSession = self.captureSession {
|
|
2802
2826
|
captureSession.stopRunning()
|
|
2803
2827
|
captureSession.inputs.forEach { captureSession.removeInput($0) }
|
|
2804
2828
|
captureSession.outputs.forEach { captureSession.removeOutput($0) }
|
|
2805
2829
|
}
|
|
2830
|
+
if shouldRestoreAfterTeardown {
|
|
2831
|
+
self.applicationAudioSessionRestoreCoordinator.restoreAfterCaptureSessionTeardown()
|
|
2832
|
+
} else {
|
|
2833
|
+
self.applicationAudioSessionRestoreCoordinator.completeCaptureSessionTeardown()
|
|
2834
|
+
}
|
|
2806
2835
|
// Remove listener for subject area change
|
|
2807
2836
|
NotificationCenter.default.removeObserver(self, name: .AVCaptureDeviceSubjectAreaDidChange, object: nil)
|
|
2808
2837
|
|
|
2809
2838
|
self.motionManager.stopAccelerometerUpdates()
|
|
2839
|
+
self.lastCaptureOrientation = nil
|
|
2840
|
+
self.lastReliableCaptureOrientation = nil
|
|
2810
2841
|
self.previewLayer?.removeFromSuperlayer()
|
|
2811
2842
|
self.previewLayer = nil
|
|
2812
2843
|
|
|
@@ -3265,6 +3296,7 @@ extension CameraController {
|
|
|
3265
3296
|
// This reclaims the microphone even if other parts of the app changed the
|
|
3266
3297
|
// AVAudioSession category (e.g. for UI sound effects) between recordings.
|
|
3267
3298
|
if self.audioInput != nil {
|
|
3299
|
+
self.captureApplicationAudioSessionSnapshotIfNeeded()
|
|
3268
3300
|
do {
|
|
3269
3301
|
let audioSession = AVAudioSession.sharedInstance()
|
|
3270
3302
|
try audioSession.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
@@ -3333,6 +3365,11 @@ extension CameraController {
|
|
|
3333
3365
|
self.applyVideoStabilizationMode()
|
|
3334
3366
|
}
|
|
3335
3367
|
|
|
3368
|
+
let recordingId = self.applicationAudioSessionRestoreCoordinator.beginRecording()
|
|
3369
|
+
self.recordingIdQueue.sync {
|
|
3370
|
+
self.recordingIdByOutputFileURL[fileUrl] = recordingId
|
|
3371
|
+
}
|
|
3372
|
+
|
|
3336
3373
|
// Start recording video
|
|
3337
3374
|
fileVideoOutput.startRecording(to: fileUrl, recordingDelegate: self)
|
|
3338
3375
|
|
|
@@ -3735,6 +3772,11 @@ extension CameraController: AVCaptureFileOutputRecordingDelegate {
|
|
|
3735
3772
|
let reason = recordingFinishReason(from: error)
|
|
3736
3773
|
let stopCompletion = self.stopRecordingCompletion
|
|
3737
3774
|
self.stopRecordingCompletion = nil
|
|
3775
|
+
let recordingSessionId = self.recordingIdQueue.sync {
|
|
3776
|
+
self.recordingIdByOutputFileURL.removeValue(forKey: outputFileURL) ?? 0
|
|
3777
|
+
}
|
|
3778
|
+
|
|
3779
|
+
self.applicationAudioSessionRestoreCoordinator.finishRecordingAsync(recordingId: recordingSessionId)
|
|
3738
3780
|
|
|
3739
3781
|
if let stopCompletion = stopCompletion {
|
|
3740
3782
|
DispatchQueue.main.async {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// Resolves device physical orientation from gravity (accelerometer) for photo and video capture.
|
|
5
|
+
/// When the device is near flat, x/y gravity is noisy; keep the last reliable orientation or UI fallback.
|
|
6
|
+
enum PhysicalOrientationResolver {
|
|
7
|
+
/// |z| above this means gravity mostly points through the screen (camera toward table or ceiling).
|
|
8
|
+
static let flatGravityZThreshold: Double = 0.75
|
|
9
|
+
/// Below this, horizontal gravity is too weak to infer portrait vs landscape reliably.
|
|
10
|
+
static let minHorizontalGravity: Double = 0.35
|
|
11
|
+
/// When |x| and |y| are within this margin, do not flip between landscape and portrait.
|
|
12
|
+
static let axisDecisionMargin: Double = 0.12
|
|
13
|
+
|
|
14
|
+
struct Acceleration {
|
|
15
|
+
let x: Double
|
|
16
|
+
let y: Double
|
|
17
|
+
let z: Double
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
struct Result {
|
|
21
|
+
let orientation: AVCaptureVideoOrientation
|
|
22
|
+
/// True when orientation came from a strong gravity signal (safe to remember for hysteresis).
|
|
23
|
+
let isReliable: Bool
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static func resolve(
|
|
27
|
+
acceleration: Acceleration,
|
|
28
|
+
lastReliableOrientation: AVCaptureVideoOrientation?,
|
|
29
|
+
fallbackOrientation: AVCaptureVideoOrientation
|
|
30
|
+
) -> Result {
|
|
31
|
+
let x = acceleration.x
|
|
32
|
+
let y = acceleration.y
|
|
33
|
+
let z = acceleration.z
|
|
34
|
+
let absX = abs(x)
|
|
35
|
+
let absY = abs(y)
|
|
36
|
+
let absZ = abs(z)
|
|
37
|
+
let horizontalMax = max(absX, absY)
|
|
38
|
+
|
|
39
|
+
let isNearFlat = absZ >= flatGravityZThreshold || horizontalMax < minHorizontalGravity
|
|
40
|
+
if isNearFlat {
|
|
41
|
+
let orientation = lastReliableOrientation ?? fallbackOrientation
|
|
42
|
+
return Result(orientation: orientation, isReliable: false)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let axisMargin = axisDecisionMargin
|
|
46
|
+
if abs(absX - absY) < axisMargin {
|
|
47
|
+
let orientation = lastReliableOrientation ?? fallbackOrientation
|
|
48
|
+
return Result(orientation: orientation, isReliable: false)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let orientation: AVCaptureVideoOrientation
|
|
52
|
+
if absX > absY {
|
|
53
|
+
orientation = x > 0 ? .landscapeLeft : .landscapeRight
|
|
54
|
+
} else {
|
|
55
|
+
orientation = y > 0 ? .portraitUpsideDown : .portrait
|
|
56
|
+
}
|
|
57
|
+
return Result(orientation: orientation, isReliable: true)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -35,7 +35,7 @@ extension UIWindow {
|
|
|
35
35
|
*/
|
|
36
36
|
@objc(CameraPreview)
|
|
37
37
|
public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelegate {
|
|
38
|
-
private let pluginVersion: String = "8.12.
|
|
38
|
+
private let pluginVersion: String = "8.12.4"
|
|
39
39
|
public let identifier = "CameraPreviewPlugin"
|
|
40
40
|
public let jsName = "CameraPreview"
|
|
41
41
|
public let pluginMethods: [CAPPluginMethod] = [
|
package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionRestoreCoordinatorTests.swift
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
private extension ApplicationAudioSessionRestoreCoordinator {
|
|
6
|
+
func registerCleanup(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) {
|
|
7
|
+
if self.beginCaptureSessionTeardown(isRecording: isRecording, pendingRecordingIds: pendingRecordingIds) {
|
|
8
|
+
self.restoreAfterCaptureSessionTeardown()
|
|
9
|
+
} else {
|
|
10
|
+
self.completeCaptureSessionTeardown()
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
func registerCleanupAndRestoreIfNeeded(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) {
|
|
15
|
+
registerCleanup(isRecording: isRecording, pendingRecordingIds: pendingRecordingIds)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func beginDeferredCleanup(isRecording: Bool, pendingRecordingIds: Set<UInt64> = []) {
|
|
19
|
+
XCTAssertFalse(
|
|
20
|
+
self.beginCaptureSessionTeardown(isRecording: isRecording, pendingRecordingIds: pendingRecordingIds)
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
final class ApplicationAudioSessionRestoreCoordinatorTests: XCTestCase {
|
|
26
|
+
func testDoubleCleanupWhileRecordingActiveDefersSingleRestoreUntilRecordingFinishes() throws {
|
|
27
|
+
let session = AVAudioSession.sharedInstance()
|
|
28
|
+
let priorCategory = session.category
|
|
29
|
+
let priorMode = session.mode
|
|
30
|
+
let priorOptions = session.categoryOptions
|
|
31
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
32
|
+
|
|
33
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
34
|
+
|
|
35
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
36
|
+
coordinator.beginPreviewSession()
|
|
37
|
+
coordinator.captureSnapshotIfNeeded()
|
|
38
|
+
|
|
39
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
40
|
+
|
|
41
|
+
let recordingId = coordinator.beginRecording()
|
|
42
|
+
coordinator.registerCleanup(isRecording: true)
|
|
43
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
44
|
+
|
|
45
|
+
coordinator.registerCleanup(isRecording: true)
|
|
46
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
47
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
48
|
+
|
|
49
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
50
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
51
|
+
XCTAssertEqual(session.category, .playback)
|
|
52
|
+
XCTAssertEqual(session.mode, .default)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func testRepeatedCleanupDuringRecordingKeepsDeferralUntilRecordingFinishes() {
|
|
56
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
57
|
+
coordinator.beginPreviewSession()
|
|
58
|
+
coordinator.captureSnapshotIfNeeded()
|
|
59
|
+
|
|
60
|
+
let recordingId = coordinator.beginRecording()
|
|
61
|
+
coordinator.registerCleanup(isRecording: true)
|
|
62
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
63
|
+
|
|
64
|
+
coordinator.registerCleanup(isRecording: false)
|
|
65
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
66
|
+
|
|
67
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
68
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
func testCleanupDefersWhenPendingRecordingIdProvidedAfterOutputStops() throws {
|
|
72
|
+
let session = AVAudioSession.sharedInstance()
|
|
73
|
+
let priorCategory = session.category
|
|
74
|
+
let priorMode = session.mode
|
|
75
|
+
let priorOptions = session.categoryOptions
|
|
76
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
77
|
+
|
|
78
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
79
|
+
|
|
80
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
81
|
+
coordinator.beginPreviewSession()
|
|
82
|
+
coordinator.captureSnapshotIfNeeded()
|
|
83
|
+
|
|
84
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
85
|
+
|
|
86
|
+
let recordingId = coordinator.beginRecording()
|
|
87
|
+
coordinator.registerCleanup(isRecording: false, pendingRecordingIds: [recordingId])
|
|
88
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
89
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
90
|
+
|
|
91
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
92
|
+
XCTAssertEqual(session.category, .playback)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
func testNewPreviewBeforeRecordingCompletionSkipsLateRestore() throws {
|
|
96
|
+
let session = AVAudioSession.sharedInstance()
|
|
97
|
+
let priorCategory = session.category
|
|
98
|
+
let priorMode = session.mode
|
|
99
|
+
let priorOptions = session.categoryOptions
|
|
100
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
101
|
+
|
|
102
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
103
|
+
|
|
104
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
105
|
+
coordinator.beginPreviewSession()
|
|
106
|
+
coordinator.captureSnapshotIfNeeded()
|
|
107
|
+
|
|
108
|
+
let recordingId = coordinator.beginRecording()
|
|
109
|
+
coordinator.registerCleanup(isRecording: true)
|
|
110
|
+
|
|
111
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
112
|
+
coordinator.beginPreviewSession()
|
|
113
|
+
|
|
114
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
115
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
116
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
func testNewPreviewBeforeAsyncRecordingCompletionSkipsLateRestore() throws {
|
|
120
|
+
let session = AVAudioSession.sharedInstance()
|
|
121
|
+
let priorCategory = session.category
|
|
122
|
+
let priorMode = session.mode
|
|
123
|
+
let priorOptions = session.categoryOptions
|
|
124
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
125
|
+
|
|
126
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
127
|
+
|
|
128
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
129
|
+
coordinator.beginPreviewSession()
|
|
130
|
+
coordinator.captureSnapshotIfNeeded()
|
|
131
|
+
|
|
132
|
+
let recordingId = coordinator.beginRecording()
|
|
133
|
+
coordinator.registerCleanup(isRecording: true)
|
|
134
|
+
|
|
135
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
136
|
+
coordinator.beginPreviewSession()
|
|
137
|
+
|
|
138
|
+
coordinator.finishRecordingAsync(recordingId: recordingId)
|
|
139
|
+
coordinator.syncWithQueueForTesting()
|
|
140
|
+
|
|
141
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
142
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
func testCaptureSnapshotDoesNotReplaceExistingBaselineOnNewPreview() throws {
|
|
146
|
+
let session = AVAudioSession.sharedInstance()
|
|
147
|
+
let priorCategory = session.category
|
|
148
|
+
let priorMode = session.mode
|
|
149
|
+
let priorOptions = session.categoryOptions
|
|
150
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
151
|
+
|
|
152
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
153
|
+
|
|
154
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
155
|
+
coordinator.beginPreviewSession()
|
|
156
|
+
coordinator.captureSnapshotIfNeeded()
|
|
157
|
+
|
|
158
|
+
let recordingId = coordinator.beginRecording()
|
|
159
|
+
coordinator.registerCleanup(isRecording: true)
|
|
160
|
+
|
|
161
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
162
|
+
coordinator.beginPreviewSession()
|
|
163
|
+
coordinator.captureSnapshotIfNeeded()
|
|
164
|
+
|
|
165
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
166
|
+
coordinator.registerCleanupAndRestoreIfNeeded(isRecording: false)
|
|
167
|
+
XCTAssertEqual(session.category, .playback)
|
|
168
|
+
XCTAssertEqual(session.mode, .default)
|
|
169
|
+
XCTAssertEqual(session.categoryOptions, [.mixWithOthers])
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
func testBeginPreviewDuringDeferredRestoreKeepsSnapshotForLaterCleanup() throws {
|
|
173
|
+
let session = AVAudioSession.sharedInstance()
|
|
174
|
+
let priorCategory = session.category
|
|
175
|
+
let priorMode = session.mode
|
|
176
|
+
let priorOptions = session.categoryOptions
|
|
177
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
178
|
+
|
|
179
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
180
|
+
|
|
181
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
182
|
+
coordinator.beginPreviewSession()
|
|
183
|
+
coordinator.captureSnapshotIfNeeded()
|
|
184
|
+
|
|
185
|
+
let recordingId = coordinator.beginRecording()
|
|
186
|
+
coordinator.registerCleanup(isRecording: true)
|
|
187
|
+
|
|
188
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
189
|
+
coordinator.beginPreviewSession()
|
|
190
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
191
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
192
|
+
|
|
193
|
+
coordinator.registerCleanupAndRestoreIfNeeded(isRecording: false)
|
|
194
|
+
XCTAssertEqual(session.category, .playback)
|
|
195
|
+
XCTAssertEqual(session.mode, .default)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
func testRecordingFinishesBeforeTeardownCompleteDoesNotRestoreUntilTeardownDone() throws {
|
|
199
|
+
let session = AVAudioSession.sharedInstance()
|
|
200
|
+
let priorCategory = session.category
|
|
201
|
+
let priorMode = session.mode
|
|
202
|
+
let priorOptions = session.categoryOptions
|
|
203
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
204
|
+
|
|
205
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
206
|
+
|
|
207
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
208
|
+
coordinator.beginPreviewSession()
|
|
209
|
+
coordinator.captureSnapshotIfNeeded()
|
|
210
|
+
|
|
211
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
212
|
+
|
|
213
|
+
let recordingId = coordinator.beginRecording()
|
|
214
|
+
coordinator.beginDeferredCleanup(isRecording: true)
|
|
215
|
+
XCTAssertTrue(coordinator.isDeferredRestorePending)
|
|
216
|
+
|
|
217
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
218
|
+
coordinator.syncWithQueueForTesting()
|
|
219
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
220
|
+
|
|
221
|
+
coordinator.completeCaptureSessionTeardown()
|
|
222
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
223
|
+
XCTAssertEqual(session.category, .playback)
|
|
224
|
+
XCTAssertEqual(session.mode, .default)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
func testTeardownCompletesBeforeRecordingFinishesRestoresOnRecordingFinish() throws {
|
|
228
|
+
let session = AVAudioSession.sharedInstance()
|
|
229
|
+
let priorCategory = session.category
|
|
230
|
+
let priorMode = session.mode
|
|
231
|
+
let priorOptions = session.categoryOptions
|
|
232
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
233
|
+
|
|
234
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
235
|
+
|
|
236
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
237
|
+
coordinator.beginPreviewSession()
|
|
238
|
+
coordinator.captureSnapshotIfNeeded()
|
|
239
|
+
|
|
240
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
241
|
+
|
|
242
|
+
let recordingId = coordinator.beginRecording()
|
|
243
|
+
coordinator.beginDeferredCleanup(isRecording: true)
|
|
244
|
+
coordinator.completeCaptureSessionTeardown()
|
|
245
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
246
|
+
|
|
247
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
248
|
+
XCTAssertFalse(coordinator.isDeferredRestorePending)
|
|
249
|
+
XCTAssertEqual(session.category, .playback)
|
|
250
|
+
XCTAssertEqual(session.mode, .default)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
func testNewPreviewDuringDeferredRestoreBeforeTeardownCompleteSkipsRestore() throws {
|
|
254
|
+
let session = AVAudioSession.sharedInstance()
|
|
255
|
+
let priorCategory = session.category
|
|
256
|
+
let priorMode = session.mode
|
|
257
|
+
let priorOptions = session.categoryOptions
|
|
258
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
259
|
+
|
|
260
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
261
|
+
|
|
262
|
+
let coordinator = ApplicationAudioSessionRestoreCoordinator()
|
|
263
|
+
coordinator.beginPreviewSession()
|
|
264
|
+
coordinator.captureSnapshotIfNeeded()
|
|
265
|
+
|
|
266
|
+
let recordingId = coordinator.beginRecording()
|
|
267
|
+
coordinator.beginDeferredCleanup(isRecording: true)
|
|
268
|
+
|
|
269
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
270
|
+
coordinator.beginPreviewSession()
|
|
271
|
+
|
|
272
|
+
coordinator.finishRecording(recordingId: recordingId)
|
|
273
|
+
coordinator.completeCaptureSessionTeardown()
|
|
274
|
+
coordinator.syncWithQueueForTesting()
|
|
275
|
+
|
|
276
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
277
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
final class ApplicationAudioSessionSnapshotTests: XCTestCase {
|
|
6
|
+
func testSnapshotAndRestoreCategoryModeAndOptions() throws {
|
|
7
|
+
let session = AVAudioSession.sharedInstance()
|
|
8
|
+
let priorCategory = session.category
|
|
9
|
+
let priorMode = session.mode
|
|
10
|
+
let priorOptions = session.categoryOptions
|
|
11
|
+
defer { try? session.setCategory(priorCategory, mode: priorMode, options: priorOptions) }
|
|
12
|
+
|
|
13
|
+
try session.setCategory(.playback, mode: .default, options: [.mixWithOthers])
|
|
14
|
+
let snapshot = ApplicationAudioSessionSnapshot.capture(from: session)
|
|
15
|
+
|
|
16
|
+
try session.setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
|
|
17
|
+
XCTAssertEqual(session.category, .playAndRecord)
|
|
18
|
+
XCTAssertEqual(session.mode, .videoRecording)
|
|
19
|
+
|
|
20
|
+
XCTAssertTrue(snapshot.apply(to: session))
|
|
21
|
+
XCTAssertEqual(session.category, .playback)
|
|
22
|
+
XCTAssertEqual(session.mode, .default)
|
|
23
|
+
XCTAssertEqual(session.categoryOptions, [.mixWithOthers])
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
final class PhysicalOrientationResolverTests: XCTestCase {
|
|
6
|
+
private func accel(x: Double, y: Double, z: Double) -> PhysicalOrientationResolver.Acceleration {
|
|
7
|
+
PhysicalOrientationResolver.Acceleration(x: x, y: y, z: z)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
func testNearFlatUsesFallbackWhenNoLastReliable() {
|
|
11
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
12
|
+
acceleration: accel(x: 0.04, y: -0.03, z: -0.99),
|
|
13
|
+
lastReliableOrientation: nil,
|
|
14
|
+
fallbackOrientation: .portrait
|
|
15
|
+
)
|
|
16
|
+
XCTAssertEqual(result.orientation, .portrait)
|
|
17
|
+
XCTAssertFalse(result.isReliable)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func testNearFlatKeepsLastReliableOrientation() {
|
|
21
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
22
|
+
acceleration: accel(x: 0.05, y: 0.02, z: 0.92),
|
|
23
|
+
lastReliableOrientation: .landscapeLeft,
|
|
24
|
+
fallbackOrientation: .portrait
|
|
25
|
+
)
|
|
26
|
+
XCTAssertEqual(result.orientation, .landscapeLeft)
|
|
27
|
+
XCTAssertFalse(result.isReliable)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
func testClearPortrait() {
|
|
31
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
32
|
+
acceleration: accel(x: 0.08, y: -0.95, z: 0.15),
|
|
33
|
+
lastReliableOrientation: nil,
|
|
34
|
+
fallbackOrientation: .portrait
|
|
35
|
+
)
|
|
36
|
+
XCTAssertEqual(result.orientation, .portrait)
|
|
37
|
+
XCTAssertTrue(result.isReliable)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func testClearLandscapeLeft() {
|
|
41
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
42
|
+
acceleration: accel(x: 0.92, y: 0.08, z: 0.05),
|
|
43
|
+
lastReliableOrientation: nil,
|
|
44
|
+
fallbackOrientation: .portrait
|
|
45
|
+
)
|
|
46
|
+
XCTAssertEqual(result.orientation, .landscapeLeft)
|
|
47
|
+
XCTAssertTrue(result.isReliable)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func testClearLandscapeRight() {
|
|
51
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
52
|
+
acceleration: accel(x: -0.9, y: 0.1, z: 0.05),
|
|
53
|
+
lastReliableOrientation: nil,
|
|
54
|
+
fallbackOrientation: .portrait
|
|
55
|
+
)
|
|
56
|
+
XCTAssertEqual(result.orientation, .landscapeRight)
|
|
57
|
+
XCTAssertTrue(result.isReliable)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
func testUpsideDownPortrait() {
|
|
61
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
62
|
+
acceleration: accel(x: 0.05, y: 0.94, z: 0.1),
|
|
63
|
+
lastReliableOrientation: nil,
|
|
64
|
+
fallbackOrientation: .portrait
|
|
65
|
+
)
|
|
66
|
+
XCTAssertEqual(result.orientation, .portraitUpsideDown)
|
|
67
|
+
XCTAssertTrue(result.isReliable)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func testAmbiguousAxisUsesLastReliable() {
|
|
71
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
72
|
+
acceleration: accel(x: 0.52, y: -0.48, z: 0.2),
|
|
73
|
+
lastReliableOrientation: .portrait,
|
|
74
|
+
fallbackOrientation: .landscapeLeft
|
|
75
|
+
)
|
|
76
|
+
XCTAssertEqual(result.orientation, .portrait)
|
|
77
|
+
XCTAssertFalse(result.isReliable)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func testWeakHorizontalGravityTreatedAsFlat() {
|
|
81
|
+
let result = PhysicalOrientationResolver.resolve(
|
|
82
|
+
acceleration: accel(x: 0.12, y: -0.18, z: 0.55),
|
|
83
|
+
lastReliableOrientation: .landscapeRight,
|
|
84
|
+
fallbackOrientation: .portrait
|
|
85
|
+
)
|
|
86
|
+
XCTAssertEqual(result.orientation, .landscapeRight)
|
|
87
|
+
XCTAssertFalse(result.isReliable)
|
|
88
|
+
}
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/camera-preview",
|
|
3
|
-
"version": "8.12.
|
|
3
|
+
"version": "8.12.4",
|
|
4
4
|
"description": "Camera preview",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"rimraf": "^6.1.0",
|
|
82
82
|
"rollup": "^4.53.2",
|
|
83
83
|
"swiftlint": "^2.0.0",
|
|
84
|
-
"typescript": "^
|
|
84
|
+
"typescript": "^6.0.3",
|
|
85
85
|
"prettier-pretty-check": "^0.2.0"
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|