@capgo/camera-preview 8.13.2 → 8.14.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.
- package/README.md +40 -39
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraPreview.java +15 -0
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +21 -0
- package/android/src/main/java/app/capgo/capacitor/camera/preview/VideoRecordingOptions.java +53 -0
- package/android/src/main/java/app/capgo/capacitor/camera/preview/model/CameraSessionConfiguration.java +9 -0
- package/android/src/test/java/app/capgo/capacitor/camera/preview/VideoRecordingOptionsTest.java +65 -0
- package/dist/docs.json +21 -1
- package/dist/esm/definitions.d.ts +9 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +3 -0
- package/dist/esm/web.js +23 -2
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +23 -1
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +23 -1
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +125 -50
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureImageExifEncoder.swift +4 -20
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureLifecycleCoordination.swift +43 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ExifGpsMetadata.swift +44 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +63 -4
- package/ios/Sources/CapgoCameraPreviewPlugin/VideoRecordingOptions.swift +56 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureImageExifEncoderTests.swift +168 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureLifecycleCoordinationTests.swift +62 -0
- package/ios/Tests/CameraPreviewPluginTests/ExifGpsMetadataTests.swift +187 -0
- package/ios/Tests/CameraPreviewPluginTests/VideoRecordingOptionsTests.swift +58 -0
- package/package.json +2 -2
|
@@ -4,6 +4,32 @@ import UIKit
|
|
|
4
4
|
import XCTest
|
|
5
5
|
@testable import CapgoCameraPreview
|
|
6
6
|
|
|
7
|
+
private final class CaptureExifGpsEncoderTestHeading: CLHeading {
|
|
8
|
+
private let storedTrue: CLLocationDirection
|
|
9
|
+
private let storedMagnetic: CLLocationDirection
|
|
10
|
+
private let storedAccuracy: CLLocationAccuracy
|
|
11
|
+
|
|
12
|
+
init(
|
|
13
|
+
trueHeading: CLLocationDirection,
|
|
14
|
+
magneticHeading: CLLocationDirection,
|
|
15
|
+
headingAccuracy: CLLocationAccuracy
|
|
16
|
+
) {
|
|
17
|
+
self.storedTrue = trueHeading
|
|
18
|
+
self.storedMagnetic = magneticHeading
|
|
19
|
+
self.storedAccuracy = headingAccuracy
|
|
20
|
+
super.init()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@available(*, unavailable)
|
|
24
|
+
required init?(coder: NSCoder) {
|
|
25
|
+
fatalError("init(coder:) has not been implemented")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
override var trueHeading: CLLocationDirection { storedTrue }
|
|
29
|
+
override var magneticHeading: CLLocationDirection { storedMagnetic }
|
|
30
|
+
override var headingAccuracy: CLLocationAccuracy { storedAccuracy }
|
|
31
|
+
}
|
|
32
|
+
|
|
7
33
|
final class CaptureImageExifEncoderTests: XCTestCase {
|
|
8
34
|
func testSkipsJpegEncodeWhenOriginalPhotoDataProvided() {
|
|
9
35
|
let image = makeTestImage()
|
|
@@ -29,6 +55,53 @@ final class CaptureImageExifEncoderTests: XCTestCase {
|
|
|
29
55
|
XCTAssertEqual(encodeCallCount, 0)
|
|
30
56
|
}
|
|
31
57
|
|
|
58
|
+
func testWritesGpsMetadataWhenLocationProvided() {
|
|
59
|
+
let image = makeTestImage()
|
|
60
|
+
guard let originalPhotoData = image.jpegData(compressionQuality: 0.9) else {
|
|
61
|
+
XCTFail("Failed to build fixture JPEG")
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let location = CLLocation(
|
|
66
|
+
coordinate: CLLocationCoordinate2D(latitude: 32.65, longitude: -16.91),
|
|
67
|
+
altitude: 100,
|
|
68
|
+
horizontalAccuracy: 5,
|
|
69
|
+
verticalAccuracy: 5,
|
|
70
|
+
timestamp: Date(timeIntervalSince1970: 1_700_000_000)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
let result = CaptureImageExifEncoder.createImageData(
|
|
74
|
+
from: image,
|
|
75
|
+
quality: 85,
|
|
76
|
+
location: location,
|
|
77
|
+
heading: nil,
|
|
78
|
+
originalPhotoData: originalPhotoData
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
guard let result,
|
|
82
|
+
let source = CGImageSourceCreateWithData(result as CFData, nil),
|
|
83
|
+
let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any],
|
|
84
|
+
let gps = properties[kCGImagePropertyGPSDictionary as String] as? [String: Any] else {
|
|
85
|
+
XCTFail("Expected JPEG output with GPS metadata")
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
guard let latitude = gps[kCGImagePropertyGPSLatitude as String] as? Double,
|
|
90
|
+
let longitude = gps[kCGImagePropertyGPSLongitude as String] as? Double,
|
|
91
|
+
let altitude = gps[kCGImagePropertyGPSAltitude as String] as? Double else {
|
|
92
|
+
XCTFail("Expected GPS latitude, longitude, and altitude as Double")
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
XCTAssertEqual(latitude, 32.65, accuracy: 0.0001)
|
|
97
|
+
XCTAssertEqual(gps[kCGImagePropertyGPSLatitudeRef as String] as? String, "N")
|
|
98
|
+
XCTAssertEqual(longitude, 16.91, accuracy: 0.0001)
|
|
99
|
+
XCTAssertEqual(gps[kCGImagePropertyGPSLongitudeRef as String] as? String, "W")
|
|
100
|
+
XCTAssertEqual(altitude, 100, accuracy: 0.0001)
|
|
101
|
+
XCTAssertEqual(gps[kCGImagePropertyGPSAltitudeRef as String] as? Int, 0)
|
|
102
|
+
XCTAssertNotNil(gps[kCGImagePropertyGPSTimeStamp as String] as? String)
|
|
103
|
+
}
|
|
104
|
+
|
|
32
105
|
func testProducesJpegWithTiffMetadata() {
|
|
33
106
|
let image = makeTestImage()
|
|
34
107
|
guard let originalPhotoData = image.jpegData(compressionQuality: 0.9) else {
|
|
@@ -111,6 +184,50 @@ final class CaptureImageExifEncoderTests: XCTestCase {
|
|
|
111
184
|
XCTAssertEqual(encodeCallCount, 1)
|
|
112
185
|
}
|
|
113
186
|
|
|
187
|
+
func testEmbedsGpsDateTimeAndImageDirectionWhenLocationAndHeadingProvided() {
|
|
188
|
+
let image = makeTestImage()
|
|
189
|
+
guard let originalPhotoData = image.jpegData(compressionQuality: 0.9) else {
|
|
190
|
+
XCTFail("Failed to build fixture JPEG")
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let location = CLLocation(
|
|
195
|
+
coordinate: CLLocationCoordinate2D(latitude: 38.72, longitude: -9.14),
|
|
196
|
+
altitude: 120.5,
|
|
197
|
+
horizontalAccuracy: 5,
|
|
198
|
+
verticalAccuracy: 5,
|
|
199
|
+
timestamp: Date(timeIntervalSince1970: 1_760_867_494.06)
|
|
200
|
+
)
|
|
201
|
+
let heading = CaptureExifGpsEncoderTestHeading(trueHeading: 88.0, magneticHeading: 90.0, headingAccuracy: 4)
|
|
202
|
+
|
|
203
|
+
guard let result = CaptureImageExifEncoder.createImageData(
|
|
204
|
+
from: image,
|
|
205
|
+
quality: 85,
|
|
206
|
+
location: location,
|
|
207
|
+
heading: heading,
|
|
208
|
+
originalPhotoData: originalPhotoData
|
|
209
|
+
),
|
|
210
|
+
let source = CGImageSourceCreateWithData(result as CFData, nil),
|
|
211
|
+
let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any],
|
|
212
|
+
let gps = properties[kCGImagePropertyGPSDictionary as String] as? [String: Any] else {
|
|
213
|
+
XCTFail("Expected JPEG output with GPS metadata")
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
XCTAssertEqual(gps[kCGImagePropertyGPSDateStamp as String] as? String, ExifGpsMetadata.gpsDateStamp(from: location.timestamp))
|
|
218
|
+
let writtenTimeStamp = ExifGpsMetadata.gpsTimeStamp(from: location.timestamp)
|
|
219
|
+
XCTAssertTrue(writtenTimeStamp.hasSuffix(".06"), "Round-trip test must use nonzero centiseconds in source metadata")
|
|
220
|
+
guard let decodedTimeStamp = Self.decodeGpsTimeStamp(gps[kCGImagePropertyGPSTimeStamp as String]) else {
|
|
221
|
+
XCTFail("Unsupported or malformed GPSTimeStamp: \(String(describing: gps[kCGImagePropertyGPSTimeStamp as String]))")
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
// ImageIO JPEG encode/decode keeps GPSTimeStamp at whole-second precision only.
|
|
225
|
+
let expectedAfterRoundTrip = Self.gpsTimeStampAfterImageIORoundTrip(from: location.timestamp)
|
|
226
|
+
XCTAssertEqual(decodedTimeStamp, expectedAfterRoundTrip)
|
|
227
|
+
XCTAssertEqual(gps[kCGImagePropertyGPSImgDirection as String] as? Double, 88.0)
|
|
228
|
+
XCTAssertEqual(gps[kCGImagePropertyGPSImgDirectionRef as String] as? String, "T")
|
|
229
|
+
}
|
|
230
|
+
|
|
114
231
|
func testEncodesWhenOriginalPhotoDataMissing() {
|
|
115
232
|
let image = makeTestImage()
|
|
116
233
|
|
|
@@ -131,6 +248,57 @@ final class CaptureImageExifEncoderTests: XCTestCase {
|
|
|
131
248
|
XCTAssertGreaterThanOrEqual(encodeCallCount, 1)
|
|
132
249
|
}
|
|
133
250
|
|
|
251
|
+
private static func gpsTimeStampAfterImageIORoundTrip(from date: Date) -> String {
|
|
252
|
+
let wholeSeconds = floor(date.timeIntervalSince1970)
|
|
253
|
+
return ExifGpsMetadata.gpsTimeStamp(from: Date(timeIntervalSince1970: wholeSeconds))
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private static func decodeGpsTimeStamp(_ value: Any?) -> String? {
|
|
257
|
+
guard let value = value else { return nil }
|
|
258
|
+
if let string = value as? String {
|
|
259
|
+
return normalizeExifGpsTimeStampString(string)
|
|
260
|
+
}
|
|
261
|
+
guard let components = value as? [Any], components.count == 3 else { return nil }
|
|
262
|
+
guard let hours = rationalValue(components[0]),
|
|
263
|
+
let minutes = rationalValue(components[1]),
|
|
264
|
+
let seconds = rationalValue(components[2]) else {
|
|
265
|
+
return nil
|
|
266
|
+
}
|
|
267
|
+
let hour = Int(hours.rounded())
|
|
268
|
+
let minute = Int(minutes.rounded())
|
|
269
|
+
let secondWhole = Int(seconds)
|
|
270
|
+
let centiseconds = Int(((seconds - Double(secondWhole)) * 100).rounded())
|
|
271
|
+
return String(format: "%02d:%02d:%02d.%02d", hour, minute, secondWhole, centiseconds)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private static func normalizeExifGpsTimeStampString(_ value: String) -> String {
|
|
275
|
+
if let dotIndex = value.firstIndex(of: ".") {
|
|
276
|
+
let base = String(value[..<dotIndex])
|
|
277
|
+
var fraction = String(value[value.index(after: dotIndex)...])
|
|
278
|
+
if fraction.count < 2 {
|
|
279
|
+
fraction += String(repeating: "0", count: 2 - fraction.count)
|
|
280
|
+
} else if fraction.count > 2 {
|
|
281
|
+
fraction = String(fraction.prefix(2))
|
|
282
|
+
}
|
|
283
|
+
return "\(base).\(fraction)"
|
|
284
|
+
}
|
|
285
|
+
return "\(value).00"
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private static func rationalValue(_ component: Any) -> Double? {
|
|
289
|
+
if let number = component as? NSNumber {
|
|
290
|
+
return number.doubleValue
|
|
291
|
+
}
|
|
292
|
+
if let rational = component as? [String: Any],
|
|
293
|
+
let numerator = rational["numerator"] as? Int,
|
|
294
|
+
let denominator = rational["denominator"] as? Int {
|
|
295
|
+
if numerator == 0 && denominator == 0 { return nil }
|
|
296
|
+
guard denominator != 0 else { return nil }
|
|
297
|
+
return Double(numerator) / Double(denominator)
|
|
298
|
+
}
|
|
299
|
+
return nil
|
|
300
|
+
}
|
|
301
|
+
|
|
134
302
|
private func makeTestImage() -> UIImage {
|
|
135
303
|
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 32, height: 32))
|
|
136
304
|
return renderer.image { context in
|
|
@@ -2,6 +2,68 @@ import XCTest
|
|
|
2
2
|
@testable import CapgoCameraPreview
|
|
3
3
|
|
|
4
4
|
final class CaptureLifecycleCoordinationTests: XCTestCase {
|
|
5
|
+
func testCaptureReadinessRequiresInitializedSessionAndRunningCaptureSession() {
|
|
6
|
+
XCTAssertFalse(CaptureReadiness.canPerformCapture(isInitialized: false, isCaptureSessionRunning: false))
|
|
7
|
+
XCTAssertFalse(CaptureReadiness.canPerformCapture(isInitialized: false, isCaptureSessionRunning: true))
|
|
8
|
+
XCTAssertFalse(CaptureReadiness.canPerformCapture(isInitialized: true, isCaptureSessionRunning: false))
|
|
9
|
+
XCTAssertTrue(CaptureReadiness.canPerformCapture(isInitialized: true, isCaptureSessionRunning: true))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
func testPhotoCaptureSubmissionBlockedWhenTeardownStartedOrSessionStopped() {
|
|
13
|
+
XCTAssertTrue(CaptureSubmissionGate.blocksPhotoCaptureSubmission(sessionTeardownStarted: true, isCaptureSessionRunning: true))
|
|
14
|
+
XCTAssertTrue(CaptureSubmissionGate.blocksPhotoCaptureSubmission(sessionTeardownStarted: false, isCaptureSessionRunning: false))
|
|
15
|
+
XCTAssertTrue(CaptureSubmissionGate.blocksPhotoCaptureSubmission(sessionTeardownStarted: true, isCaptureSessionRunning: false))
|
|
16
|
+
XCTAssertFalse(CaptureSubmissionGate.blocksPhotoCaptureSubmission(sessionTeardownStarted: false, isCaptureSessionRunning: true))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
func testTryClaimPhotoCaptureSetsFlagOnlyWhenSubmissionAllowed() {
|
|
20
|
+
var isCapturing = false
|
|
21
|
+
XCTAssertFalse(
|
|
22
|
+
CaptureSubmissionGate.tryClaimPhotoCapture(
|
|
23
|
+
sessionTeardownStarted: true,
|
|
24
|
+
isCaptureSessionRunning: true,
|
|
25
|
+
isCapturingPhoto: &isCapturing
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
XCTAssertFalse(isCapturing)
|
|
29
|
+
|
|
30
|
+
XCTAssertTrue(
|
|
31
|
+
CaptureSubmissionGate.tryClaimPhotoCapture(
|
|
32
|
+
sessionTeardownStarted: false,
|
|
33
|
+
isCaptureSessionRunning: true,
|
|
34
|
+
isCapturingPhoto: &isCapturing
|
|
35
|
+
)
|
|
36
|
+
)
|
|
37
|
+
XCTAssertTrue(isCapturing)
|
|
38
|
+
|
|
39
|
+
XCTAssertFalse(
|
|
40
|
+
CaptureSubmissionGate.tryClaimPhotoCapture(
|
|
41
|
+
sessionTeardownStarted: false,
|
|
42
|
+
isCaptureSessionRunning: false,
|
|
43
|
+
isCapturingPhoto: &isCapturing
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
XCTAssertTrue(isCapturing)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
func testClaimAwaitingSampleCaptureIsOneShot() {
|
|
50
|
+
var state = CaptureLifecycleState()
|
|
51
|
+
XCTAssertFalse(CaptureLifecycleCoordination.claimAwaitingSampleCapture(state: &state))
|
|
52
|
+
state.isAwaitingSampleCapture = true
|
|
53
|
+
XCTAssertTrue(CaptureLifecycleCoordination.claimAwaitingSampleCapture(state: &state))
|
|
54
|
+
XCTAssertFalse(state.isAwaitingSampleCapture)
|
|
55
|
+
XCTAssertFalse(CaptureLifecycleCoordination.claimAwaitingSampleCapture(state: &state))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
func testCancelAwaitingSampleCaptureOnlyWhenAwaiting() {
|
|
59
|
+
var cancelCount = 0
|
|
60
|
+
CaptureLifecycleCoordination.cancelAwaitingSampleCaptureIfNeeded(isAwaiting: false) { cancelCount += 1 }
|
|
61
|
+
XCTAssertEqual(cancelCount, 0)
|
|
62
|
+
|
|
63
|
+
CaptureLifecycleCoordination.cancelAwaitingSampleCaptureIfNeeded(isAwaiting: true) { cancelCount += 1 }
|
|
64
|
+
XCTAssertEqual(cancelCount, 1)
|
|
65
|
+
}
|
|
66
|
+
|
|
5
67
|
func testDeferredStopRunsCleanupWhenCaptureFinishes() {
|
|
6
68
|
var state = CaptureLifecycleState()
|
|
7
69
|
state.isCapturingPhoto = true
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import CoreLocation
|
|
2
|
+
import ImageIO
|
|
3
|
+
import XCTest
|
|
4
|
+
@testable import CapgoCameraPreview
|
|
5
|
+
|
|
6
|
+
private final class ExifGpsTestCLHeading: CLHeading {
|
|
7
|
+
private let storedTrue: CLLocationDirection
|
|
8
|
+
private let storedMagnetic: CLLocationDirection
|
|
9
|
+
private let storedAccuracy: CLLocationAccuracy
|
|
10
|
+
|
|
11
|
+
init(
|
|
12
|
+
trueHeading: CLLocationDirection,
|
|
13
|
+
magneticHeading: CLLocationDirection,
|
|
14
|
+
headingAccuracy: CLLocationAccuracy
|
|
15
|
+
) {
|
|
16
|
+
self.storedTrue = trueHeading
|
|
17
|
+
self.storedMagnetic = magneticHeading
|
|
18
|
+
self.storedAccuracy = headingAccuracy
|
|
19
|
+
super.init()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@available(*, unavailable)
|
|
23
|
+
required init?(coder: NSCoder) {
|
|
24
|
+
fatalError("init(coder:) has not been implemented")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
override var trueHeading: CLLocationDirection { storedTrue }
|
|
28
|
+
override var magneticHeading: CLLocationDirection { storedMagnetic }
|
|
29
|
+
override var headingAccuracy: CLLocationAccuracy { storedAccuracy }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
final class ExifGpsMetadataTests: XCTestCase {
|
|
33
|
+
func testGpsDateStampUsesExifFormatInUtc() {
|
|
34
|
+
var components = DateComponents()
|
|
35
|
+
components.calendar = Calendar(identifier: .gregorian)
|
|
36
|
+
components.timeZone = TimeZone(secondsFromGMT: 0)
|
|
37
|
+
components.year = 2026
|
|
38
|
+
components.month = 10
|
|
39
|
+
components.day = 19
|
|
40
|
+
components.hour = 14
|
|
41
|
+
components.minute = 30
|
|
42
|
+
components.second = 45
|
|
43
|
+
let date = components.date!
|
|
44
|
+
XCTAssertEqual(ExifGpsMetadata.gpsDateStamp(from: date), "2026:10:19")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func testGpsTimeStampUsesExifFormatInUtc() {
|
|
48
|
+
var components = DateComponents()
|
|
49
|
+
components.calendar = Calendar(identifier: .gregorian)
|
|
50
|
+
components.timeZone = TimeZone(secondsFromGMT: 0)
|
|
51
|
+
components.year = 2026
|
|
52
|
+
components.month = 10
|
|
53
|
+
components.day = 19
|
|
54
|
+
components.hour = 10
|
|
55
|
+
components.minute = 31
|
|
56
|
+
components.second = 34
|
|
57
|
+
components.nanosecond = 60_000_000
|
|
58
|
+
let date = components.date!
|
|
59
|
+
XCTAssertEqual(ExifGpsMetadata.gpsTimeStamp(from: date), "10:31:34.06")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
func testGpsDictionaryIncludesImageDirectionWhenHeadingAvailable() {
|
|
63
|
+
let location = CLLocation(
|
|
64
|
+
coordinate: CLLocationCoordinate2D(latitude: 38.72, longitude: -9.14),
|
|
65
|
+
altitude: 120.5,
|
|
66
|
+
horizontalAccuracy: 5,
|
|
67
|
+
verticalAccuracy: 5,
|
|
68
|
+
timestamp: Date(timeIntervalSince1970: 1_760_867_494)
|
|
69
|
+
)
|
|
70
|
+
let heading = ExifGpsTestCLHeading(trueHeading: 175.4, magneticHeading: 180.0, headingAccuracy: 5)
|
|
71
|
+
let gpsDict = ExifGpsMetadata.gpsDictionary(for: location, heading: heading)
|
|
72
|
+
|
|
73
|
+
XCTAssertEqual(gpsDict[kCGImagePropertyGPSImgDirection as String] as? Double, 175.4)
|
|
74
|
+
XCTAssertEqual(gpsDict[kCGImagePropertyGPSImgDirectionRef as String] as? String, "T")
|
|
75
|
+
XCTAssertEqual(
|
|
76
|
+
gpsDict[kCGImagePropertyGPSDateStamp as String] as? String,
|
|
77
|
+
ExifGpsMetadata.gpsDateStamp(from: location.timestamp)
|
|
78
|
+
)
|
|
79
|
+
XCTAssertEqual(
|
|
80
|
+
gpsDict[kCGImagePropertyGPSTimeStamp as String] as? String,
|
|
81
|
+
ExifGpsMetadata.gpsTimeStamp(from: location.timestamp)
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
func testGpsDictionaryRoundTripThroughImageIOAvoidsZeroRationals() throws {
|
|
86
|
+
let location = CLLocation(
|
|
87
|
+
coordinate: CLLocationCoordinate2D(latitude: 38.72, longitude: -9.14),
|
|
88
|
+
altitude: 120.5,
|
|
89
|
+
horizontalAccuracy: 5,
|
|
90
|
+
verticalAccuracy: 5,
|
|
91
|
+
timestamp: Date(timeIntervalSince1970: 1_760_867_494.06)
|
|
92
|
+
)
|
|
93
|
+
let gpsDict = ExifGpsMetadata.gpsDictionary(for: location)
|
|
94
|
+
XCTAssertNotNil(gpsDict[kCGImagePropertyGPSDateStamp as String])
|
|
95
|
+
XCTAssertNotNil(gpsDict[kCGImagePropertyGPSTimeStamp as String])
|
|
96
|
+
|
|
97
|
+
let size = CGSize(width: 4, height: 4)
|
|
98
|
+
let renderer = UIGraphicsImageRenderer(size: size)
|
|
99
|
+
let image = renderer.image { context in
|
|
100
|
+
UIColor.green.setFill()
|
|
101
|
+
context.fill(CGRect(origin: .zero, size: size))
|
|
102
|
+
}
|
|
103
|
+
guard let cgImage = image.cgImage else {
|
|
104
|
+
XCTFail("Failed to create test CGImage")
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let properties: [String: Any] = [
|
|
109
|
+
kCGImagePropertyGPSDictionary as String: gpsDict
|
|
110
|
+
]
|
|
111
|
+
guard let jpegData = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: properties, qualityPercent: 90) else {
|
|
112
|
+
XCTFail("JPEG encode failed")
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
guard let imageSource = CGImageSourceCreateWithData(jpegData as CFData, nil),
|
|
117
|
+
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any],
|
|
118
|
+
let readGps = imageProperties[kCGImagePropertyGPSDictionary as String] as? [String: Any] else {
|
|
119
|
+
XCTFail("Failed to read GPS metadata from JPEG")
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let dateStamp = readGps[kCGImagePropertyGPSDateStamp as String] as? String
|
|
124
|
+
XCTAssertEqual(dateStamp, ExifGpsMetadata.gpsDateStamp(from: location.timestamp))
|
|
125
|
+
|
|
126
|
+
let writtenTimeStamp = ExifGpsMetadata.gpsTimeStamp(from: location.timestamp)
|
|
127
|
+
XCTAssertTrue(writtenTimeStamp.hasSuffix(".06"), "Round-trip test must use nonzero centiseconds in source metadata")
|
|
128
|
+
guard let decodedTimeStamp = Self.decodeGpsTimeStamp(readGps[kCGImagePropertyGPSTimeStamp as String]) else {
|
|
129
|
+
XCTFail("Unsupported or malformed GPSTimeStamp: \(String(describing: readGps[kCGImagePropertyGPSTimeStamp as String]))")
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
// ImageIO JPEG encode/decode keeps GPSTimeStamp at whole-second precision only.
|
|
133
|
+
let expectedAfterRoundTrip = Self.gpsTimeStampAfterImageIORoundTrip(from: location.timestamp)
|
|
134
|
+
XCTAssertEqual(decodedTimeStamp, expectedAfterRoundTrip)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private static func gpsTimeStampAfterImageIORoundTrip(from date: Date) -> String {
|
|
138
|
+
let wholeSeconds = floor(date.timeIntervalSince1970)
|
|
139
|
+
return ExifGpsMetadata.gpsTimeStamp(from: Date(timeIntervalSince1970: wholeSeconds))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private static func decodeGpsTimeStamp(_ value: Any?) -> String? {
|
|
143
|
+
guard let value = value else { return nil }
|
|
144
|
+
if let string = value as? String {
|
|
145
|
+
return normalizeExifGpsTimeStampString(string)
|
|
146
|
+
}
|
|
147
|
+
guard let components = value as? [Any], components.count == 3 else { return nil }
|
|
148
|
+
guard let hours = rationalValue(components[0]),
|
|
149
|
+
let minutes = rationalValue(components[1]),
|
|
150
|
+
let seconds = rationalValue(components[2]) else {
|
|
151
|
+
return nil
|
|
152
|
+
}
|
|
153
|
+
let hour = Int(hours.rounded())
|
|
154
|
+
let minute = Int(minutes.rounded())
|
|
155
|
+
let secondWhole = Int(seconds)
|
|
156
|
+
let centiseconds = Int(((seconds - Double(secondWhole)) * 100).rounded())
|
|
157
|
+
return String(format: "%02d:%02d:%02d.%02d", hour, minute, secondWhole, centiseconds)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private static func normalizeExifGpsTimeStampString(_ value: String) -> String {
|
|
161
|
+
if let dotIndex = value.firstIndex(of: ".") {
|
|
162
|
+
let base = String(value[..<dotIndex])
|
|
163
|
+
var fraction = String(value[value.index(after: dotIndex)...])
|
|
164
|
+
if fraction.count < 2 {
|
|
165
|
+
fraction += String(repeating: "0", count: 2 - fraction.count)
|
|
166
|
+
} else if fraction.count > 2 {
|
|
167
|
+
fraction = String(fraction.prefix(2))
|
|
168
|
+
}
|
|
169
|
+
return "\(base).\(fraction)"
|
|
170
|
+
}
|
|
171
|
+
return "\(value).00"
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private static func rationalValue(_ component: Any) -> Double? {
|
|
175
|
+
if let number = component as? NSNumber {
|
|
176
|
+
return number.doubleValue
|
|
177
|
+
}
|
|
178
|
+
if let rational = component as? [String: Any],
|
|
179
|
+
let numerator = rational["numerator"] as? Int,
|
|
180
|
+
let denominator = rational["denominator"] as? Int {
|
|
181
|
+
if numerator == 0 && denominator == 0 { return nil }
|
|
182
|
+
guard denominator != 0 else { return nil }
|
|
183
|
+
return Double(numerator) / Double(denominator)
|
|
184
|
+
}
|
|
185
|
+
return nil
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapgoCameraPreview
|
|
4
|
+
|
|
5
|
+
final class VideoRecordingOptionsTests: XCTestCase {
|
|
6
|
+
func testParseVideoBitrateAcceptsPositiveIntegers() {
|
|
7
|
+
XCTAssertEqual(VideoRecordingOptions.parseVideoBitrate(from: 2_500_000), 2_500_000)
|
|
8
|
+
XCTAssertEqual(VideoRecordingOptions.parseVideoBitrate(from: 900_000.0), 900_000)
|
|
9
|
+
XCTAssertEqual(VideoRecordingOptions.parseVideoBitrate(from: "1500000"), 1_500_000)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
func testParseVideoBitrateIgnoresInvalidValues() {
|
|
13
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: nil))
|
|
14
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: 0))
|
|
15
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: -1))
|
|
16
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: "not-a-number"))
|
|
17
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: "9223372036854775808"))
|
|
18
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: 1_000_000.5))
|
|
19
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: 0.4))
|
|
20
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: true))
|
|
21
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: NSNumber(value: true)))
|
|
22
|
+
XCTAssertNil(VideoRecordingOptions.parseVideoBitrate(from: kCFBooleanTrue))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
func testMovieFileOutputSettingsIncludesBitrateWhenSet() {
|
|
26
|
+
let settings = VideoRecordingOptions.movieFileOutputSettings(codecType: .h264, videoBitrate: 1_000_000)
|
|
27
|
+
XCTAssertEqual(settings[AVVideoCodecKey] as? AVVideoCodecType, .h264)
|
|
28
|
+
let compression = settings[AVVideoCompressionPropertiesKey] as? [String: Any]
|
|
29
|
+
XCTAssertEqual(compression?[AVVideoAverageBitRateKey] as? Int, 1_000_000)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func testMovieFileOutputSettingsOmitsBitrateWhenUnset() {
|
|
33
|
+
let settings = VideoRecordingOptions.movieFileOutputSettings(codecType: .h264, videoBitrate: nil)
|
|
34
|
+
XCTAssertEqual(settings[AVVideoCodecKey] as? AVVideoCodecType, .h264)
|
|
35
|
+
XCTAssertNil(settings[AVVideoCompressionPropertiesKey])
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func testResolveRecordingVideoBitrateOverrideRestoresPreviewOnSecondRecording() {
|
|
39
|
+
let controller = CameraController()
|
|
40
|
+
controller.setPreviewVideoBitrate(1_000_000)
|
|
41
|
+
controller.applyRecordingVideoBitrateOverride(5_000_000)
|
|
42
|
+
XCTAssertEqual(controller.getVideoBitrate(), 5_000_000)
|
|
43
|
+
|
|
44
|
+
let secondOverride = VideoRecordingOptions.resolveRecordingVideoBitrateOverride(from: nil, isKeyPresent: false)
|
|
45
|
+
controller.applyRecordingVideoBitrateOverride(secondOverride)
|
|
46
|
+
XCTAssertEqual(controller.getVideoBitrate(), 1_000_000)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
func testResolveRecordingVideoBitrateOverrideInvalidValueRestoresPreview() {
|
|
50
|
+
let controller = CameraController()
|
|
51
|
+
controller.setPreviewVideoBitrate(1_000_000)
|
|
52
|
+
controller.applyRecordingVideoBitrateOverride(5_000_000)
|
|
53
|
+
|
|
54
|
+
let invalidOverride = VideoRecordingOptions.resolveRecordingVideoBitrateOverride(from: 0, isKeyPresent: true)
|
|
55
|
+
controller.applyRecordingVideoBitrateOverride(invalidOverride)
|
|
56
|
+
XCTAssertEqual(controller.getVideoBitrate(), 1_000_000)
|
|
57
|
+
}
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/camera-preview",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.14.0",
|
|
4
4
|
"description": "Camera preview",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"test:ios": "bash ./scripts/test-ios.sh",
|
|
46
46
|
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
47
47
|
"verify:web": "bun run build && bun run test:web",
|
|
48
|
-
"test:web": "bun test src/web.stop.test.ts",
|
|
48
|
+
"test:web": "bun test src/web.stop.test.ts src/web.videoBitrate.test.ts",
|
|
49
49
|
"lint": "bun run eslint && bun run prettier -- --check && bun run swiftlint -- lint",
|
|
50
50
|
"fmt": "bun run eslint -- --fix && bun run prettier -- --write && bun run swiftlint -- --fix --format",
|
|
51
51
|
"eslint": "eslint .",
|