@capgo/camera-preview 8.13.3 → 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.
Files changed (27) hide show
  1. package/README.md +40 -39
  2. package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraPreview.java +15 -0
  3. package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +21 -0
  4. package/android/src/main/java/app/capgo/capacitor/camera/preview/VideoRecordingOptions.java +53 -0
  5. package/android/src/main/java/app/capgo/capacitor/camera/preview/model/CameraSessionConfiguration.java +9 -0
  6. package/android/src/test/java/app/capgo/capacitor/camera/preview/VideoRecordingOptionsTest.java +65 -0
  7. package/dist/docs.json +21 -1
  8. package/dist/esm/definitions.d.ts +9 -1
  9. package/dist/esm/definitions.js.map +1 -1
  10. package/dist/esm/web.d.ts +3 -0
  11. package/dist/esm/web.js +23 -2
  12. package/dist/esm/web.js.map +1 -1
  13. package/dist/plugin.cjs.js +23 -1
  14. package/dist/plugin.cjs.js.map +1 -1
  15. package/dist/plugin.js +23 -1
  16. package/dist/plugin.js.map +1 -1
  17. package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +125 -17
  18. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureImageExifEncoder.swift +4 -20
  19. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureLifecycleCoordination.swift +43 -0
  20. package/ios/Sources/CapgoCameraPreviewPlugin/ExifGpsMetadata.swift +44 -0
  21. package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +63 -4
  22. package/ios/Sources/CapgoCameraPreviewPlugin/VideoRecordingOptions.swift +56 -0
  23. package/ios/Tests/CameraPreviewPluginTests/CaptureImageExifEncoderTests.swift +121 -0
  24. package/ios/Tests/CameraPreviewPluginTests/CaptureLifecycleCoordinationTests.swift +62 -0
  25. package/ios/Tests/CameraPreviewPluginTests/ExifGpsMetadataTests.swift +187 -0
  26. package/ios/Tests/CameraPreviewPluginTests/VideoRecordingOptionsTests.swift +58 -0
  27. package/package.json +2 -2
package/README.md CHANGED
@@ -799,7 +799,7 @@ startRecordVideo(options: CameraPreviewOptions) => Promise<void>
799
799
 
800
800
  Starts recording a video.
801
801
 
802
- Supports `frameRate`, `videoCodec`, `videoStabilizationMode`, `maxDuration`, `maxFileSize`, `disableAudio`, `allowHapticsAndSystemSoundsDuringRecording`, and `mirrorFrontCamera` on each call.
802
+ Supports `frameRate`, `videoCodec`, `videoBitrate`, `videoStabilizationMode`, `maxDuration`, `maxFileSize`, `disableAudio`, `allowHapticsAndSystemSoundsDuringRecording`, and `mirrorFrontCamera` on each call.
803
803
 
804
804
  | Param | Type | Description |
805
805
  | ------------- | --------------------------------------------------------------------- | ---------------------------------- |
@@ -1563,44 +1563,45 @@ Get the native Capacitor plugin version
1563
1563
 
1564
1564
  Defines the configuration options for starting the camera preview.
1565
1565
 
1566
- | Prop | Type | Description | Default | Since |
1567
- | ------------------------------------------------ | ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------ |
1568
- | **`parent`** | <code>string</code> | The parent element to attach the video preview to. | | |
1569
- | **`className`** | <code>string</code> | A CSS class name to add to the preview element. | | |
1570
- | **`width`** | <code>number</code> | The width of the preview in pixels. Defaults to the screen width. | | |
1571
- | **`height`** | <code>number</code> | The height of the preview in pixels. Defaults to the screen height. | | |
1572
- | **`x`** | <code>number</code> | The horizontal origin of the preview, in pixels. | | |
1573
- | **`y`** | <code>number</code> | The vertical origin of the preview, in pixels. | | |
1574
- | **`aspectRatio`** | <code><a href="#camerapreviewaspectratio">CameraPreviewAspectRatio</a></code> | The aspect ratio of the camera preview, '4:3' or '16:9' or 'fill'. Cannot be set if width or height is provided, otherwise the call will be rejected. Use setPreviewSize to adjust size after starting. | | 2.0.0 |
1575
- | **`aspectMode`** | <code>'cover' \| 'contain'</code> | Controls how the camera preview fills the available space. - 'contain': Fits the entire preview within the space, may show letterboxing (default). - 'cover': Fills the entire space, may crop edges of the preview. | <code>"contain"</code> | |
1576
- | **`gridMode`** | <code><a href="#gridmode">GridMode</a></code> | The grid overlay to display on the camera preview. | <code>"none"</code> | 2.1.0 |
1577
- | **`includeSafeAreaInsets`** | <code>boolean</code> | Adjusts the y-position to account for safe areas (e.g., notches). | <code>false</code> | |
1578
- | **`toBack`** | <code>boolean</code> | If true, places the preview behind the webview. | <code>true</code> | |
1579
- | **`paddingBottom`** | <code>number</code> | Bottom padding for the preview, in pixels. | | |
1580
- | **`rotateWhenOrientationChanged`** | <code>boolean</code> | Whether to rotate the preview when the device orientation changes. | <code>true</code> | |
1581
- | **`position`** | <code>string</code> | The camera to use. | <code>"rear"</code> | |
1582
- | **`storeToFile`** | <code>boolean</code> | If true, saves the captured image to a file and returns the file path. If false, returns a base64 encoded string. | <code>false</code> | |
1583
- | **`disableExifHeaderStripping`** | <code>boolean</code> | If true, prevents the plugin from rotating the image based on EXIF data. | <code>false</code> | |
1584
- | **`disableAudio`** | <code>boolean</code> | If true, disables the audio stream, preventing audio permission requests. | <code>true</code> | |
1585
- | **`lockAndroidOrientation`** | <code>boolean</code> | If true, locks the device orientation while the camera is active. | <code>false</code> | |
1586
- | **`enableOpacity`** | <code>boolean</code> | If true, allows the camera preview's opacity to be changed. | <code>false</code> | |
1587
- | **`disableFocusIndicator`** | <code>boolean</code> | If true, disables the visual focus indicator when tapping to focus. | <code>false</code> | |
1588
- | **`deviceId`** | <code>string</code> | The `deviceId` of the camera to use. If provided, `position` is ignored. | | |
1589
- | **`enablePhysicalDeviceSelection`** | <code>boolean</code> | On Android, attempts to bind a physical camera directly when `deviceId` refers to a physical lens. Disabled by default because OEM support is inconsistent; when false, Android keeps the current logical-camera fallback behavior. | <code>false</code> | |
1590
- | **`initialZoomLevel`** | <code>number</code> | The initial zoom level when starting the camera preview. If the requested zoom level is not available, the native plugin will reject. | <code>1.0</code> | 2.2.0 |
1591
- | **`positioning`** | <code><a href="#camerapositioning">CameraPositioning</a></code> | The vertical positioning of the camera preview. | <code>"center"</code> | 2.3.0 |
1592
- | **`enableVideoMode`** | <code>boolean</code> | If true, enables video capture capabilities when the camera starts. | <code>false</code> | 7.11.0 |
1593
- | **`captureResolution`** | <code><a href="#cameracaptureresolution">CameraCaptureResolution</a></code> | Sets the still-capture sensor resolution cap at session start. Preview resolution is unchanged. The shared ViewPort crop from the preview still applies to captures. On Android this binds the `ImageCapture` use case to the requested size. On iOS 16+ this sets `AVCapturePhotoSettings.maxPhotoDimensions` (and disables high-resolution photo capture when capped) to the largest supported sensor size at or below the requested cap. When the active format exposes no supported size under the cap, iOS uses the smallest supported `maxPhotoDimensions` entry (same fallback as Android) so `start()` never assigns an unsupported dimension. On iOS 15, `start()` rejects this option. When omitted, capture uses the highest resolution supported by the active camera (same as versions before 8.11.10 on Android, and the format default on iOS). Set this to cap capture size for faster shots on high-megapixel sensors, for example `{ width: 1920, height: 1080 }` or `{ width: 1600, height: 1200 }`. | | 8.12.0 |
1594
- | **`force`** | <code>boolean</code> | If true, forces the camera to start/restart even if it's already running or busy. This will kill the current camera session and start a new one, ignoring all state checks. | <code>false</code> | |
1595
- | **`videoQuality`** | <code><a href="#videoquality">VideoQuality</a></code> | Sets the quality of video for recording. Options: 'low', 'medium', 'high', '2160p', '1080p', '720p', '480p', '4:3' | <code>"high"</code> | |
1596
- | **`maxDuration`** | <code>number</code> | Maximum recording duration in seconds. Recording stops automatically when reached. | | |
1597
- | **`maxFileSize`** | <code>number</code> | Maximum recording file size in bytes. Recording stops automatically when reached. | | |
1598
- | **`videoCodec`** | <code><a href="#videocodec">VideoCodec</a></code> | Preferred video codec for recording. | <code>"avc1"</code> | |
1599
- | **`frameRate`** | <code>number</code> | Target video frame rate in frames per second. Applied when recording starts. Use `getSupportedVideoFrameRates()` to list supported values. | | 8.6.0 |
1600
- | **`videoStabilizationMode`** | <code><a href="#videostabilizationmode">VideoStabilizationMode</a></code> | Preferred video stabilization mode for recording. | <code>"off"</code> | |
1601
- | **`allowHapticsAndSystemSoundsDuringRecording`** | <code>boolean</code> | When `true`, allows haptic feedback and system sounds while recording video with audio. iOS suppresses haptics during audio input recording unless this is enabled. Requires `disableAudio: false`. | <code>false</code> | 8.8.0 |
1602
- | **`mirrorFrontCamera`** | <code>boolean</code> | When `true` and the front camera is active, mirrors the recorded video file so it matches the selfie-style preview (text and gestures are not reversed). Does not affect the live preview. Defaults to `false` to preserve existing behavior. | <code>false</code> | 8.10.0 |
1603
- | **`barcodeScanner`** | <code>boolean \| <a href="#barcodescanneroptions">BarcodeScannerOptions</a></code> | Starts barcode scanning together with the camera preview. Set to `true` or pass options to scan all supported formats. Omit this option to keep barcode scanning disabled at startup. | | 8.8.0 |
1566
+ | Prop | Type | Description | Default | Since |
1567
+ | ------------------------------------------------ | ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ------ |
1568
+ | **`parent`** | <code>string</code> | The parent element to attach the video preview to. | | |
1569
+ | **`className`** | <code>string</code> | A CSS class name to add to the preview element. | | |
1570
+ | **`width`** | <code>number</code> | The width of the preview in pixels. Defaults to the screen width. | | |
1571
+ | **`height`** | <code>number</code> | The height of the preview in pixels. Defaults to the screen height. | | |
1572
+ | **`x`** | <code>number</code> | The horizontal origin of the preview, in pixels. | | |
1573
+ | **`y`** | <code>number</code> | The vertical origin of the preview, in pixels. | | |
1574
+ | **`aspectRatio`** | <code><a href="#camerapreviewaspectratio">CameraPreviewAspectRatio</a></code> | The aspect ratio of the camera preview, '4:3' or '16:9' or 'fill'. Cannot be set if width or height is provided, otherwise the call will be rejected. Use setPreviewSize to adjust size after starting. | | 2.0.0 |
1575
+ | **`aspectMode`** | <code>'cover' \| 'contain'</code> | Controls how the camera preview fills the available space. - 'contain': Fits the entire preview within the space, may show letterboxing (default). - 'cover': Fills the entire space, may crop edges of the preview. | <code>"contain"</code> | |
1576
+ | **`gridMode`** | <code><a href="#gridmode">GridMode</a></code> | The grid overlay to display on the camera preview. | <code>"none"</code> | 2.1.0 |
1577
+ | **`includeSafeAreaInsets`** | <code>boolean</code> | Adjusts the y-position to account for safe areas (e.g., notches). | <code>false</code> | |
1578
+ | **`toBack`** | <code>boolean</code> | If true, places the preview behind the webview. | <code>true</code> | |
1579
+ | **`paddingBottom`** | <code>number</code> | Bottom padding for the preview, in pixels. | | |
1580
+ | **`rotateWhenOrientationChanged`** | <code>boolean</code> | Whether to rotate the preview when the device orientation changes. | <code>true</code> | |
1581
+ | **`position`** | <code>string</code> | The camera to use. | <code>"rear"</code> | |
1582
+ | **`storeToFile`** | <code>boolean</code> | If true, saves the captured image to a file and returns the file path. If false, returns a base64 encoded string. | <code>false</code> | |
1583
+ | **`disableExifHeaderStripping`** | <code>boolean</code> | If true, prevents the plugin from rotating the image based on EXIF data. | <code>false</code> | |
1584
+ | **`disableAudio`** | <code>boolean</code> | If true, disables the audio stream, preventing audio permission requests. | <code>true</code> | |
1585
+ | **`lockAndroidOrientation`** | <code>boolean</code> | If true, locks the device orientation while the camera is active. | <code>false</code> | |
1586
+ | **`enableOpacity`** | <code>boolean</code> | If true, allows the camera preview's opacity to be changed. | <code>false</code> | |
1587
+ | **`disableFocusIndicator`** | <code>boolean</code> | If true, disables the visual focus indicator when tapping to focus. | <code>false</code> | |
1588
+ | **`deviceId`** | <code>string</code> | The `deviceId` of the camera to use. If provided, `position` is ignored. | | |
1589
+ | **`enablePhysicalDeviceSelection`** | <code>boolean</code> | On Android, attempts to bind a physical camera directly when `deviceId` refers to a physical lens. Disabled by default because OEM support is inconsistent; when false, Android keeps the current logical-camera fallback behavior. | <code>false</code> | |
1590
+ | **`initialZoomLevel`** | <code>number</code> | The initial zoom level when starting the camera preview. If the requested zoom level is not available, the native plugin will reject. | <code>1.0</code> | 2.2.0 |
1591
+ | **`positioning`** | <code><a href="#camerapositioning">CameraPositioning</a></code> | The vertical positioning of the camera preview. | <code>"center"</code> | 2.3.0 |
1592
+ | **`enableVideoMode`** | <code>boolean</code> | If true, enables video capture capabilities when the camera starts. | <code>false</code> | 7.11.0 |
1593
+ | **`captureResolution`** | <code><a href="#cameracaptureresolution">CameraCaptureResolution</a></code> | Sets the still-capture sensor resolution cap at session start. Preview resolution is unchanged. The shared ViewPort crop from the preview still applies to captures. On Android this binds the `ImageCapture` use case to the requested size. On iOS 16+ this sets `AVCapturePhotoSettings.maxPhotoDimensions` (and disables high-resolution photo capture when capped) to the largest supported sensor size at or below the requested cap. When the active format exposes no supported size under the cap, iOS uses the smallest supported `maxPhotoDimensions` entry (same fallback as Android) so `start()` never assigns an unsupported dimension. On iOS 15, `start()` rejects this option. When omitted, capture uses the highest resolution supported by the active camera (same as versions before 8.11.10 on Android, and the format default on iOS). Set this to cap capture size for faster shots on high-megapixel sensors, for example `{ width: 1920, height: 1080 }` or `{ width: 1600, height: 1200 }`. | | 8.12.0 |
1594
+ | **`force`** | <code>boolean</code> | If true, forces the camera to start/restart even if it's already running or busy. This will kill the current camera session and start a new one, ignoring all state checks. | <code>false</code> | |
1595
+ | **`videoQuality`** | <code><a href="#videoquality">VideoQuality</a></code> | Sets the quality of video for recording. Options: 'low', 'medium', 'high', '2160p', '1080p', '720p', '480p', '4:3' | <code>"high"</code> | |
1596
+ | **`maxDuration`** | <code>number</code> | Maximum recording duration in seconds. Recording stops automatically when reached. | | |
1597
+ | **`maxFileSize`** | <code>number</code> | Maximum recording file size in bytes. Recording stops automatically when reached. | | |
1598
+ | **`videoCodec`** | <code><a href="#videocodec">VideoCodec</a></code> | Preferred video codec for recording. | <code>"avc1"</code> | |
1599
+ | **`frameRate`** | <code>number</code> | Target video frame rate in frames per second. Applied when recording starts. Use `getSupportedVideoFrameRates()` to list supported values. | | 8.6.0 |
1600
+ | **`videoBitrate`** | <code>number</code> | Target video encoding bitrate in bits per second for recording. When omitted, the platform encoder chooses its default bitrate. | <code>platform default</code> | 8.13.0 |
1601
+ | **`videoStabilizationMode`** | <code><a href="#videostabilizationmode">VideoStabilizationMode</a></code> | Preferred video stabilization mode for recording. | <code>"off"</code> | |
1602
+ | **`allowHapticsAndSystemSoundsDuringRecording`** | <code>boolean</code> | When `true`, allows haptic feedback and system sounds while recording video with audio. iOS suppresses haptics during audio input recording unless this is enabled. Requires `disableAudio: false`. | <code>false</code> | 8.8.0 |
1603
+ | **`mirrorFrontCamera`** | <code>boolean</code> | When `true` and the front camera is active, mirrors the recorded video file so it matches the selfie-style preview (text and gestures are not reversed). Does not affect the live preview. Defaults to `false` to preserve existing behavior. | <code>false</code> | 8.10.0 |
1604
+ | **`barcodeScanner`** | <code>boolean \| <a href="#barcodescanneroptions">BarcodeScannerOptions</a></code> | Starts barcode scanning together with the camera preview. Set to `true` or pass options to scan all supported formats. Omit this option to keep barcode scanning disabled at startup. | | 8.8.0 |
1604
1605
 
1605
1606
 
1606
1607
  #### CameraCaptureResolution
@@ -126,6 +126,7 @@ public class CameraPreview extends Plugin implements CameraXView.CameraXViewList
126
126
  }
127
127
 
128
128
  private CameraSessionConfiguration lastSessionConfig;
129
+ private Integer startVideoBitrate;
129
130
 
130
131
  private static final String TAG = "CameraPreview CameraXView";
131
132
  private static final int DEFAULT_WEB_VIEW_BACKGROUND = Color.WHITE;
@@ -1654,6 +1655,12 @@ public class CameraPreview extends Plugin implements CameraXView.CameraXViewList
1654
1655
  config.setEnablePhysicalDeviceSelection(enablePhysicalDeviceSelection);
1655
1656
  config.setCaptureResolution(captureResolutionWidth, captureResolutionHeight);
1656
1657
  config.setBarcodeScannerEnabled(barcodeScannerOptions != null);
1658
+ String startVideoCodec = call.getString("videoCodec");
1659
+ if (startVideoCodec != null) {
1660
+ config.setVideoCodec(startVideoCodec);
1661
+ }
1662
+ startVideoBitrate = VideoRecordingOptions.parseVideoBitrate(call.getData());
1663
+ config.setVideoBitrate(startVideoBitrate);
1657
1664
  setPendingStartBarcodeScanner(barcodeScannerOptions);
1658
1665
  if (barcodeScannerOptions == null) {
1659
1666
  clearActiveBarcodeScanner();
@@ -2787,6 +2794,13 @@ public class CameraPreview extends Plugin implements CameraXView.CameraXViewList
2787
2794
  }
2788
2795
  }
2789
2796
 
2797
+ private void applyVideoBitrateFromCall(PluginCall call) {
2798
+ if (cameraXView == null) {
2799
+ return;
2800
+ }
2801
+ cameraXView.setVideoBitrateSetting(VideoRecordingOptions.resolveRecordingVideoBitrate(startVideoBitrate, call.getData()));
2802
+ }
2803
+
2790
2804
  private void applyVideoStabilizationFromCall(PluginCall call) {
2791
2805
  String mode = call.getString("videoStabilizationMode");
2792
2806
  if (mode != null && cameraXView != null) {
@@ -2820,6 +2834,7 @@ public class CameraPreview extends Plugin implements CameraXView.CameraXViewList
2820
2834
  }
2821
2835
  try {
2822
2836
  applyVideoCodecFromCall(call);
2837
+ applyVideoBitrateFromCall(call);
2823
2838
  applyVideoStabilizationFromCall(call);
2824
2839
  Integer frameRate = call.getInt("frameRate");
2825
2840
  boolean mirrorFrontCamera = Boolean.TRUE.equals(call.getBoolean("mirrorFrontCamera", false));
@@ -1170,6 +1170,10 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
1170
1170
  if ("hvc1".equalsIgnoreCase(videoCodec)) {
1171
1171
  recorderBuilder.setVideoCapabilitiesSource(Recorder.VIDEO_CAPABILITIES_SOURCE_CODEC_CAPABILITIES);
1172
1172
  }
1173
+ Integer videoBitrate = sessionConfig.getVideoBitrate();
1174
+ if (videoBitrate != null && videoBitrate > 0) {
1175
+ recorderBuilder.setTargetVideoEncodingBitRate(videoBitrate);
1176
+ }
1173
1177
  Recorder recorder = recorderBuilder.build();
1174
1178
  VideoCapture.Builder<Recorder> videoCaptureBuilder = new VideoCapture.Builder<>(recorder);
1175
1179
  videoCaptureBuilder = applyTargetFpsToVideoCaptureBuilder(videoCaptureBuilder);
@@ -2149,6 +2153,8 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
2149
2153
  target.setEnablePhysicalDeviceSelection(source.isPhysicalDeviceSelectionEnabled());
2150
2154
  target.setBarcodeScannerEnabled(source.isBarcodeScannerEnabled());
2151
2155
  target.copyCaptureResolutionFrom(source);
2156
+ target.setVideoCodec(source.getVideoCodec());
2157
+ target.setVideoBitrate(source.getVideoBitrate());
2152
2158
  }
2153
2159
 
2154
2160
  private void requestEnumeratedDeviceCacheRefresh() {
@@ -5594,6 +5600,21 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
5594
5600
  }
5595
5601
  }
5596
5602
 
5603
+ public void setVideoBitrateSetting(Integer videoBitrate) {
5604
+ if (sessionConfig == null) {
5605
+ throw new IllegalStateException("Camera session is not running");
5606
+ }
5607
+ Integer normalized = videoBitrate != null && videoBitrate > 0 ? videoBitrate : null;
5608
+ Integer current = sessionConfig.getVideoBitrate();
5609
+ if ((current == null && normalized == null) || (current != null && current.equals(normalized))) {
5610
+ return;
5611
+ }
5612
+ sessionConfig.setVideoBitrate(normalized);
5613
+ if (sessionConfig.isVideoModeEnabled() && isRunning) {
5614
+ mainExecutor.execute(this::bindCameraUseCases);
5615
+ }
5616
+ }
5617
+
5597
5618
  public List<String> getSupportedVideoCodecs() {
5598
5619
  List<String> codecs = new ArrayList<>();
5599
5620
  codecs.add("avc1");
@@ -0,0 +1,53 @@
1
+ package app.capgo.capacitor.camera.preview;
2
+
3
+ import org.json.JSONObject;
4
+
5
+ /**
6
+ * Helpers for parsing video recording options from plugin call payloads.
7
+ */
8
+ public final class VideoRecordingOptions {
9
+
10
+ private VideoRecordingOptions() {}
11
+
12
+ /**
13
+ * Parses {@code videoBitrate} from a plugin options object.
14
+ * Returns {@code null} when omitted, null, non-positive, or not a finite positive integer.
15
+ */
16
+ public static Integer parseVideoBitrate(JSONObject data) {
17
+ if (data == null || !data.has("videoBitrate") || data.isNull("videoBitrate")) {
18
+ return null;
19
+ }
20
+ Object raw = data.opt("videoBitrate");
21
+ if (raw instanceof Number) {
22
+ double value = ((Number) raw).doubleValue();
23
+ if (!Double.isFinite(value) || value <= 0D || value > Integer.MAX_VALUE || value != Math.floor(value)) {
24
+ return null;
25
+ }
26
+ return (int) value;
27
+ }
28
+ if (raw instanceof String) {
29
+ try {
30
+ double value = Double.parseDouble((String) raw);
31
+ if (!Double.isFinite(value) || value <= 0D || value > Integer.MAX_VALUE || value != Math.floor(value)) {
32
+ return null;
33
+ }
34
+ return (int) value;
35
+ } catch (NumberFormatException ignored) {
36
+ return null;
37
+ }
38
+ }
39
+ return null;
40
+ }
41
+
42
+ /**
43
+ * Bitrate for a single {@code startRecordVideo} call: a valid per-call override, otherwise the preview
44
+ * bitrate from {@code start()} (may be {@code null} for the platform default).
45
+ */
46
+ public static Integer resolveRecordingVideoBitrate(Integer previewBitrate, JSONObject data) {
47
+ if (data == null || !data.has("videoBitrate") || data.isNull("videoBitrate")) {
48
+ return previewBitrate;
49
+ }
50
+ Integer parsed = parseVideoBitrate(data);
51
+ return parsed != null ? parsed : previewBitrate;
52
+ }
53
+ }
@@ -27,6 +27,7 @@ public class CameraSessionConfiguration {
27
27
  private boolean isCentered = false;
28
28
  private String videoQuality;
29
29
  private String videoCodec = "avc1";
30
+ private Integer videoBitrate;
30
31
  private String videoStabilizationMode = "off";
31
32
  private boolean enablePhysicalDeviceSelection = false;
32
33
  private boolean barcodeScannerEnabled = false;
@@ -164,6 +165,14 @@ public class CameraSessionConfiguration {
164
165
  this.videoCodec = codec != null ? codec : "avc1";
165
166
  }
166
167
 
168
+ public Integer getVideoBitrate() {
169
+ return videoBitrate;
170
+ }
171
+
172
+ public void setVideoBitrate(Integer videoBitrate) {
173
+ this.videoBitrate = videoBitrate != null && videoBitrate > 0 ? videoBitrate : null;
174
+ }
175
+
167
176
  public String getVideoStabilizationMode() {
168
177
  return videoStabilizationMode;
169
178
  }
@@ -0,0 +1,65 @@
1
+ package app.capgo.capacitor.camera.preview;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+ import static org.junit.Assert.assertNull;
5
+
6
+ import org.json.JSONObject;
7
+ import org.junit.Test;
8
+ import org.junit.runner.RunWith;
9
+ import org.robolectric.RobolectricTestRunner;
10
+ import org.robolectric.annotation.Config;
11
+
12
+ @RunWith(RobolectricTestRunner.class)
13
+ @Config(manifest = Config.NONE, sdk = 28)
14
+ public class VideoRecordingOptionsTest {
15
+
16
+ @Test
17
+ public void parseVideoBitrate_omittedOrNull() throws Exception {
18
+ assertNull(VideoRecordingOptions.parseVideoBitrate(null));
19
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject()));
20
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":null}")));
21
+ }
22
+
23
+ @Test
24
+ public void parseVideoBitrate_positiveInteger() throws Exception {
25
+ assertEquals(Integer.valueOf(2_500_000), VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":2500000}")));
26
+ assertEquals(Integer.valueOf(900_000), VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":\"900000\"}")));
27
+ }
28
+
29
+ @Test
30
+ public void parseVideoBitrate_invalidValuesIgnored() throws Exception {
31
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":0}")));
32
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":-1}")));
33
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":\"bad\"}")));
34
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":9223372036854775808}")));
35
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":1000000.5}")));
36
+ assertNull(VideoRecordingOptions.parseVideoBitrate(new JSONObject("{\"videoBitrate\":0.4}")));
37
+ }
38
+
39
+ @Test
40
+ public void resolveRecordingVideoBitrate_restoresPreviewAfterPerCallOverride() throws Exception {
41
+ Integer previewBitrate = 1_000_000;
42
+ assertEquals(
43
+ Integer.valueOf(5_000_000),
44
+ VideoRecordingOptions.resolveRecordingVideoBitrate(previewBitrate, new JSONObject("{\"videoBitrate\":5000000}"))
45
+ );
46
+ assertEquals(previewBitrate, VideoRecordingOptions.resolveRecordingVideoBitrate(previewBitrate, new JSONObject()));
47
+ assertEquals(
48
+ previewBitrate,
49
+ VideoRecordingOptions.resolveRecordingVideoBitrate(previewBitrate, new JSONObject("{\"videoBitrate\":null}"))
50
+ );
51
+ }
52
+
53
+ @Test
54
+ public void resolveRecordingVideoBitrate_invalidPerCallValueRestoresPreview() throws Exception {
55
+ Integer previewBitrate = 1_000_000;
56
+ assertEquals(
57
+ previewBitrate,
58
+ VideoRecordingOptions.resolveRecordingVideoBitrate(previewBitrate, new JSONObject("{\"videoBitrate\":0}"))
59
+ );
60
+ assertEquals(
61
+ previewBitrate,
62
+ VideoRecordingOptions.resolveRecordingVideoBitrate(previewBitrate, new JSONObject("{\"videoBitrate\":\"bad\"}"))
63
+ );
64
+ }
65
+ }
package/dist/docs.json CHANGED
@@ -539,7 +539,7 @@
539
539
  "text": "0.0.1"
540
540
  }
541
541
  ],
542
- "docs": "Starts recording a video.\n\nSupports `frameRate`, `videoCodec`, `videoStabilizationMode`, `maxDuration`, `maxFileSize`, `disableAudio`, `allowHapticsAndSystemSoundsDuringRecording`, and `mirrorFrontCamera` on each call.",
542
+ "docs": "Starts recording a video.\n\nSupports `frameRate`, `videoCodec`, `videoBitrate`, `videoStabilizationMode`, `maxDuration`, `maxFileSize`, `disableAudio`, `allowHapticsAndSystemSoundsDuringRecording`, and `mirrorFrontCamera` on each call.",
543
543
  "complexTypes": [
544
544
  "CameraPreviewOptions"
545
545
  ],
@@ -2204,6 +2204,26 @@
2204
2204
  "complexTypes": [],
2205
2205
  "type": "number | undefined"
2206
2206
  },
2207
+ {
2208
+ "name": "videoBitrate",
2209
+ "tags": [
2210
+ {
2211
+ "text": "ios, android, web",
2212
+ "name": "platform"
2213
+ },
2214
+ {
2215
+ "text": "platform default",
2216
+ "name": "default"
2217
+ },
2218
+ {
2219
+ "text": "8.13.0",
2220
+ "name": "since"
2221
+ }
2222
+ ],
2223
+ "docs": "Target video encoding bitrate in bits per second for recording.\nWhen omitted, the platform encoder chooses its default bitrate.",
2224
+ "complexTypes": [],
2225
+ "type": "number | undefined"
2226
+ },
2207
2227
  {
2208
2228
  "name": "videoStabilizationMode",
2209
2229
  "tags": [
@@ -373,6 +373,14 @@ export interface CameraPreviewOptions {
373
373
  * @since 8.6.0
374
374
  */
375
375
  frameRate?: number;
376
+ /**
377
+ * Target video encoding bitrate in bits per second for recording.
378
+ * When omitted, the platform encoder chooses its default bitrate.
379
+ * @platform ios, android, web
380
+ * @default platform default
381
+ * @since 8.13.0
382
+ */
383
+ videoBitrate?: number;
376
384
  /**
377
385
  * Preferred video stabilization mode for recording.
378
386
  * @platform ios, android
@@ -775,7 +783,7 @@ export interface CameraPreviewPlugin {
775
783
  /**
776
784
  * Starts recording a video.
777
785
  *
778
- * Supports `frameRate`, `videoCodec`, `videoStabilizationMode`, `maxDuration`, `maxFileSize`, `disableAudio`, `allowHapticsAndSystemSoundsDuringRecording`, and `mirrorFrontCamera` on each call.
786
+ * Supports `frameRate`, `videoCodec`, `videoBitrate`, `videoStabilizationMode`, `maxDuration`, `maxFileSize`, `disableAudio`, `allowHapticsAndSystemSoundsDuringRecording`, and `mirrorFrontCamera` on each call.
779
787
  *
780
788
  * @param {CameraPreviewOptions} options - The options for video recording.
781
789
  * @returns {Promise<void>} A promise that resolves when video recording starts.