Commit Graph

146 Commits

Author SHA1 Message Date
Marc Rousavy
30b56153db
feat: Sync Frame Processors (plus runAsync and runAtTargetFps) (#1472)
Before, Frame Processors ran on a separate Thread.

After, Frame Processors run fully synchronous and always at the same FPS as the Camera.

Two new functions have been introduced:

* `runAtTargetFps(fps: number, func: () => void)`: Runs the given code as often as the given `fps`, effectively throttling it's calls.
* `runAsync(frame: Frame, func: () => void)`: Runs the given function on a separate Thread for Frame Processing. A strong reference to the Frame is held as long as the function takes to execute.

You can use `runAtTargetFps` to throttle calls to a specific API (e.g. if your Camera is running at 60 FPS, but you only want to run face detection at ~25 FPS, use `runAtTargetFps(25, ...)`.)

You can use `runAsync` to run a heavy algorithm asynchronous, so that the Camera is not blocked while your algorithm runs. This is useful if your main sync processor draws something, and your async processor is doing some image analysis on the side. 

You can also combine both functions.

Examples:

```js
const frameProcessor = useFrameProcessor((frame) => {
  'worklet'
  console.log("I'm running at 60 FPS!")
}, [])
```

```js
const frameProcessor = useFrameProcessor((frame) => {
  'worklet'
  console.log("I'm running at 60 FPS!")

  runAtTargetFps(10, () => {
    'worklet'
    console.log("I'm running at 10 FPS!")
  })
}, [])
```



```js
const frameProcessor = useFrameProcessor((frame) => {
  'worklet'
  console.log("I'm running at 60 FPS!")

  runAsync(frame, () => {
    'worklet'
    console.log("I'm running on another Thread, I can block for longer!")
  })
}, [])
```

```js
const frameProcessor = useFrameProcessor((frame) => {
  'worklet'
  console.log("I'm running at 60 FPS!")

  runAtTargetFps(10, () => {
    'worklet'
    runAsync(frame, () => {
      'worklet'
      console.log("I'm running on another Thread at 10 FPS, I can block for longer!")
    })
  })
}, [])
```
2023-02-15 16:47:09 +01:00
Marc Rousavy
a0590dccb5
feat: Replace Reanimated with RN Worklets (#1468)
* Setup RN Worklets

* Use RN Worklets on iOS

* Fix console

* Add `installFrameProcessorBindings()` function

* Add `FrameProcessorPlugins` proxy (BREAKING CHANGE)

* Clean up docs

* Update FRAME_PROCESSORS.mdx

* Use RN Worklets 0.2.5

* feat: Android build setup

* Rewrite Android Frame Processor Part

* Update CMakeLists.txt

* fix: Add react-native-worklets Gradle dependency

* Update Podfile.lock

* fix build

* gradle:7.4.1

* Init JSI Bindings in method on Android

* Fix Folly flags

* fix: Init `FrameProcessorRuntimeManager` later

* fix: Wrap in `<GestureHandlerRootView>`

* Refactor plugins

* fix: Remove enableFrameProcessors

* Install RN Worklets from current GH master

* Update babel.config.js

* Update CameraViewModule.kt

* Update ImageProxyUtils.java

* feat: Upgrade to Reanimated v3

* fix: Fix crash on Worklet init

* Update RN Worklets to latest master

* fix: Simplify FP Plugins Proxy
2023-02-13 15:22:45 +01:00
Marc Rousavy
11d1e7178d
chore: Upgrade to RN 71 (#1465)
* chore: Upgrade Example to RN 0.71

* chore: Upgrade all libs

* fix: Fix CameraRoll installation

* Update Gradle Tools

* fix: Fix buildscripts

* Clean out build.gradle

* fix: Fix Kotlin setup

* fix: Move kotlin-android dependency to lib

* Move `_setGlobalConsole`

* Update gradle-wrapper.properties

* Rebuild lockfiles

* chore: Update build:gradle

* Update StatusBarBlurBackground.tsx

* Use Java 11 in Workflows

* Update MediaPage.tsx

* Add `google` repository to build.gradle

* Double Java Heap size

* Increase heap size

* Alternative args

* Update build.gradle
2023-02-09 11:52:41 +01:00
mrousavy
28a43f716f chore: Drop support for RN < 71 to simplify buildscript 2023-02-08 17:48:22 +01:00
zzz08900
6a094bf1b0
fix: Fix Android build error on RN < 0.71 (#1447) 2023-02-01 16:37:18 +01:00
Christoph Gritschenberger
b82d0e362d
fix: Add support for react-native 0.71 (#1438)
Co-authored-by: Christoph Gritschenberger <christoph.gritschenberger@cca.io>
2023-01-30 19:38:52 +01:00
Thibault Malbranche
c046440e0e
fix: Allowed setting custom downloads directory (#1306)
Clone of https://github.com/mrousavy/react-native-mmkv/pull/459/
2022-10-28 12:18:06 +02:00
Marc Rousavy
a7932bdc98
fix: Support new Reanimated Headers directory (2.11.10+) (#1301) 2022-10-24 14:09:56 +02:00
viniciusyoshioka
b7bb8e45da
fix: Build error "2 files found with path '.../libfolly_runtime.so'" (#1144) 2022-08-10 11:21:59 +02:00
Thibault Malbranche
205e542cb6
fix: Support RN 0.69 and use Hermes from source! 🎉 (#1186)
* wip

* wip

* Update CMakeLists.txt

* Update CMakeLists.txt

* Update android/build.gradle

Co-authored-by: Tomek Zawadzki <tomekzawadzki98@gmail.com>

Co-authored-by: Tomek Zawadzki <tomekzawadzki98@gmail.com>
2022-08-09 18:20:42 +02:00
Eduardo Borges
80df81815a
fix: Fix takePhoto not working inside an Modal (#1021)
* fix: fix takePhoto not working inside an Modal

* chore: remover unnecessary code

* chore: remove unnecessary variable
2022-07-20 14:02:20 +02:00
Marc Rousavy
230b3a6f62 Rename .save -> +save 2022-07-19 13:30:59 +02:00
Marc Rousavy
8bafd96c24
feat: Allow returning of ImageProxy in a Frame Processor (#1149)
* feat: Allow returning of ImageProxy in a Frame Processor

* chore: Clean up

* fix: Missing space

* Update useFrameProcessor.ts

* Revert "Update useFrameProcessor.ts"

This reverts commit 9c645489cdfdf2079972669756a2cd20cc81e25e.
2022-07-18 10:40:56 +02:00
davidlcorbitt
096b1cca4e
fix: (Android) Give real video resolutions, unbind/rebind preview in onHostResume, add missing Android capture errors (#1079)
* Calculate a format's video dimensions based on supported resolutions and photo dimensions

* Add Android fallback strategy for recording quality

* Ensure that session props are not ignored when app is resumed

* Simplify setting Android video dimensions in format

* Modify Android imageAnalysisBuilder to use photoSize

* Update onHostResume function to reference android preview issue

* Add missing Android capture errors
2022-06-14 10:54:33 +02:00
Menardi
3850491b9f
fix: Fix takeSnapshot not working on Android (#961)
Accessing previewView.bitmap was throwing an error because it wasn't being done on the main thread.
Any access to previewView needs to be done on the main (UI) thread. This commit fixes the issue by
ensuring this access is now run on the main thread.

Fixes #547
2022-04-04 11:03:08 +02:00
Menardi
fe01295226
fix: Fix Android focus not using correct focus point (#958)
This commit fixes #758. I was having the same issue and looked into it a bit. I found
[this StackOverflow answer](https://stackoverflow.com/a/60585382) which described a
solution to the same problem. Rather than manually calculate the focus point, we can
get the PreviewView to do it for us. This fixes the issue because the PreviewView
factors in any scaling or resizing of the view on the screen, which we weren't doing
before. The only potential issue is that this needs to run on the UI thread
(which is what the `withContext` is doing), but I've tested it with frame processors
enabled and disabled, and have found no issues in either case.
2022-03-31 18:01:21 +02:00
Christopher Janietz
bea4aa8bb6
fix: Fix issue with resolving camera view in Modal (#928)
* Fix issue with resolving camera view from frame processor for react-native Modal

* Add missing import of UIManagerHelper
2022-03-28 13:22:27 +02:00
Marc Rousavy
8957d5fcb2
fix: Fix Reanimated build (AnimatedSensor.h) (#923) 2022-03-22 10:49:57 +01:00
Marc Rousavy
4b9bcb37e0
feat: Add pauseRecording and resumeRecording 🔥 (#911)
* feat: Add `pauseRecording` and `resumeRecording` (iOS)

* feat: Add `pauseRecording` and `resumeRecording` (Android)

* feat: Add `pauseRecording` and `resumeRecording` (JS)

* fix: Simplify Swift code for Recording
2022-03-22 10:44:58 +01:00
Marc Rousavy
a7d620cf22
chore(deps): Upgrade CameraX to beta02 (#850) 2022-02-28 11:51:50 +01:00
Joshua Smith
00fc39891a
fix: Fix duplicate libhermes.so package (#741)
Co-authored-by: Joshua Smith <joshua.smith4@aggiemail.usu.edu>
2022-01-14 09:54:33 +01:00
NilsKrause
202508b500
fix: Use ViewGroupManager instead of SimpleViewManager (#735) 2022-01-13 10:33:07 +01:00
Muhammad Aditya Hilmy
0ce01b9543
fix: Fix image rotated when flipped in Android (#719) 2022-01-11 11:52:50 +01:00
Marc Rousavy
0904767cf2
fix: Log Stacktrace on Frame Processor Error (#731)
* fix: Log JS Stack on Error

* Android

* Format Stacktrace better

* Update FrameProcessorUtils.mm

* Allow unapproved C++11 headers

* Use `.c_str()`
2022-01-10 16:37:47 +01:00
Marc Rousavy
48da1819fc
feat: Custom Orientation (#715)
* feat: Custom Orientation

* Update CameraView.swift

* Update CameraView.swift

* Try outputRotation approach

* whoops

* fix: Refactor `VideoCapture` instance

* Update orientation in didSetProps

* Update Orientation in iOS

* expose to objc

* Fix Orientation values

* format
2022-01-04 16:57:40 +01:00
Marc Rousavy
adb2115d3b Update build.gradle 2022-01-04 08:49:46 +01:00
Marc Rousavy
675dec5ab3
fix: Resolve node_modules/ better (#706)
* fix: Use `rootDir` instead of `projectDir`

* Revert "fix: Use `rootDir` instead of `projectDir`"

This reverts commit 058e0a110bcf9b688e12a1cccbac2f23a29fa55c.

* fix: Find node_modules path where react-native/ lives

* fix: Figure out VisionCameraExample project

* Revert "fix: Figure out VisionCameraExample project"

This reverts commit 7ca455098244dd62280d40586062803d1ccc2c5f.
2022-01-03 13:18:38 +01:00
Marc Rousavy
1c09ded57e chore: Log target JSI Runtime 2022-01-03 13:06:21 +01:00
Marc Rousavy
d2dbcdd09d
feat: Monorepo support (#704)
* fix: Find node_modules dynamically

* Update build.gradle

* Update build.gradle

* fix: Remove `findNodeModulePath` function
2022-01-03 12:23:19 +01:00
Marc Rousavy
fc4ed60f7c
fix: Fix REA detection to build Frame Processors (#703)
* fix: Fix REA detection

* fix: Detect REA v2 only

* cleanup logic

* Log whether Frame Processors are enabled or disabled

* Log warn instead of info

* use plugin.js to check

* log more precisely

* try ctor

* Revert "try ctor"

This reverts commit bb6110a119428451389d75896234a9935c48d75b.

* fix: Fix log order

* fix: Fix dumb boolean conversion. ffs.

* Update build.gradle

* fix: Remove `VisionCamera_disableFrameProcessors=false` flag
2022-01-03 11:54:32 +01:00
Marc Rousavy
1d27f6a06b fix: Fix crash after Activity is destroyed
Fixes #664
2022-01-03 09:30:40 +01:00
Marc Rousavy
be5ec69b02
feat: Make Reanimated optional (disable Frame Processors if REA v2 is not installed) (#412)
* Fix building iOS without Reanimated

* Conditionally compile Frame Processors (gradle)

* Conditionally use externalNativeBuild

* Remove Reanimated import

* fix: Conditionally load REA/VisionCamera libraries

* fix: Add disable FP to docs

* fix: Fix dummy placeholder for Scheduler.mm

* fix: Fix dummy `Scheduler` declaration

* fix: Only init `CameraView` C++ side if frame processors are enabled

* fix: Install JSI Bindings on Frame Processor Manager ctor

* fix: Wrong conditional

* whoops
2022-01-02 17:35:26 +01:00
Marc Rousavy
77e065d961
fix: Fix Android build on React Native 0.65 and older (#694)
* fix: Fix Android build on React Native 0.65 and older

* fix: Add excludes

* fix: Ignore META-INF from package

* fix: Wrong var name
2022-01-02 16:25:43 +01:00
Janic Duplessis
dcbbae5fc7
fix: Re-install Frame Processor JSI Bindings after Reload (#691) 2022-01-02 16:02:17 +01:00
Janic Duplessis
4584d33921
fix: Fix JNI crash in FrameHostObject dtor (#689) 2022-01-02 15:57:15 +01:00
Marc Rousavy
68707322fe
feat: Use new CameraX Recorder API (Upgrade CameraX to alpha12/32) (#543)
* chore(deps): Upgrade CameraX to alpha10/30

* chore(deps): Add first stable CameraX Video library

* feat: Use new CameraX Video API

* chore(deps): Upgrade CameraX from 10 -> 12

* fix: Replace deprecated APIs

* Update CameraViewModule.kt

* fix: Fix file creation
2021-12-30 11:39:17 +01:00
Janic Duplessis
04576db975
fix: Fix extract tasks being added to clean tasks (#644) 2021-12-13 09:32:10 +01:00
Marc Rousavy
1249cde337 fix: Fix gradle build with wrong task-name and order 2021-12-10 09:53:12 +01:00
Thomas Coldwell
c3039c94c6
feat: Add pixelFormat field (#559)
* Add `mediaSubType` field to device formats

* Create FourCharCode `toString` extension

* Move pixelFormat -> `AVCaptureDevice.Format+toDictionary`

* `mediaSubType` -> `pixelFormat` + non-optional

* ts pixelFormat `string` -> specific formats

* Add default pixelFormat value of `420v` on Android

Co-authored-by: tcoldwell72 <31568400+tcoldwell72@users.noreply.github.com>
2021-12-10 09:44:30 +01:00
Marc Rousavy
6025367c72
fix: Fix duplicate JHashMap definition by moving to custom namespace (#610)
Technically `JHashMap` is duplicated now, but in separate namespaces. If I were to remove my `JHashMap` (and `JArrayList`) definitions, the user is forced to use fbjni v3.
2021-11-23 15:48:07 +01:00
Marc Rousavy
c007a08040 fix: Fix ProGuard stripping away FrameProcessorPlugin 2021-10-14 12:47:28 +02:00
Marc Rousavy
c47639d86a fix: Fix possible null-pointer error in convertJNIObjectToJSIValue 2021-10-14 12:46:42 +02:00
Marc Rousavy
4a73cb96c1
fix: Fix view-not-found race condition in C++ code (#511)
* Add custom `onViewReady` event to get layout

`componentDidMount` is async, so the native view _might_ not exist yet causing a race condition in the `setFrameProcessor` code.

This PR fixes this by calling `setFrameProcessor` only after the native view has actually mounted, and to ensure that I created a custom event that fires at that point.

* Update CameraView.swift
2021-10-11 18:27:23 +02:00
Marc Rousavy
8d24e344c9
chore: Upgrade CameraX to alpha09 (#472)
* chore: Upgrade CameraX to alpha09

* Remove custom ProGuard file

It's no longer needed, CameraX now ships one.

* Set `targetSdkVersion` to `31`

* set `compileSdkVersion` to 31

* Add `android:exported=false`
2021-10-07 12:20:21 +02:00
Marc Rousavy
fc43b1bb7c
fix: Avoid duplicate C++ symbols by using libjsi.so shared library (#494)
* fix: Avoid duplicate C++ symbols by using `libjsi.so` shared library

* exclude `libfbjni` and `libjsi` from packaging options
2021-10-07 11:16:19 +02:00
Marc Rousavy
f029e731b2
fix: Exclude libfbjni.so 2021-10-07 10:28:50 +02:00
Marc Rousavy
916278d3ea
feat: Add React Native 0.66 support (#490)
* feat: Add React Native 0.66 support

* Generate lockfiles

* Update Podfile.lock

* chore: Migrate from react-native-navigation to react-navigation (#491)

* Migrate RNN -> RN

* Migrate all screens

* Fix get permission status

* fix app name

* Update AppDelegate.m

* Fix Info.plist

* Set `UIViewControllerBasedStatusBarAppearance` to `YES`

* Only enable `audio` if user granted microphone permission

* Update App.tsx

* Fix RNGH for Android

* Use `navigate` instead of `push`

* Fix animation

* Upgrade @types/react-native

* "Splash" -> "PermissionsPage"
2021-10-05 12:22:14 +02:00
Marc Rousavy
42e791b4bd
perf: Remove FrameProcessorPlugin HybridClass (#467)
* Rename `JImageProxyHostObject` -> `FrameHostObject`

* `FrameProcessorPlugin` -> `JFrameProcessorPlugin` 1/2

* `FrameProcessorPlugin` -> `JFrameProcessorPlugin` 2/2

* Make `const`

* Make `getName()` instance based

* Update JFrameProcessorPlugin.h

* Update JImageProxy.h

* `T`

* T

* Remove default ctor

* Use `TSelf` again

* Return `local_ref<CameraView*>` instead of `CameraView*`

* Make `findCameraViewById` return a raw pointer again...

* Extract `setFrameProcessor` and `unsetFrameProcessor`

* Use `global_ref`

* Use `static_cast` for `FrameHostObject`

* Update FrameProcessorRuntimeManager.cpp

* Fix reference lint error

* linelength

* Fix `unsetFrameProcessor` call
2021-09-29 12:30:50 +02:00
Marc Rousavy
8f65427391
fix: Fix ExtensionsManager abstract method crash by adding ProGuard rule (#466) 2021-09-29 10:38:49 +02:00
Marc Rousavy
60ea779ffe
fix: De-allocate frame HybridClass with JNI class loader if using Hermes (#455)
* De-allocate `frame` HybridClass with JNI class loader if using Hermes

See 1b3a0c2612

* Don't wrap in `#if FOR_HERMES`, other `jsi::Runtime`s might also run GC on another Thread.

* Use `jni::local_ref` for `FrameHostObject`

* Update JImageProxyHostObject.cpp

* Only run with JNI `ClassLoader` if ctor Thread ID != dtor Thread ID

* Upgrade reanimated to 2.3.0-beta.1 to fix JNI crash

* Remove `this_thread::get_id()`

* Update Podfile.lock
2021-09-24 16:57:12 +02:00