Skip to content

Commit

Permalink
Update Babylon Native for native camera fixes (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoPrez authored Nov 23, 2022
1 parent cd91d2e commit 2a0fb01
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Apps/Playground/0.69/ios/Playground/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSCameraUsageDescription</key>
<string>Used to render the camera into the Babylon Scene</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down
Submodule BabylonNative updated 54 files
+5 −4 Apps/Playground/Android/app/build.gradle
+1 −1 Apps/Playground/Android/app/src/main/cpp/BabylonNativeJNI.cpp
+2 −0 Apps/Playground/Android/build.gradle
+1 −1 Apps/Playground/Win32/App.cpp
+1 −1 Apps/Playground/iOS/LibNativeBridge.mm
+1 −1 Apps/Playground/macOS/ViewController.mm
+1 −1 Apps/UnitTests/Apple/App.mm
+4 −1 Apps/UnitTests/CMakeLists.txt
+113 −0 Apps/UnitTests/Scripts/tests.js
+0 −1 Apps/UnitTests/Shared/Tests.h
+1 −1 Apps/UnitTests/Win32/App.cpp
+1 −1 Apps/UnitTests/X11/App.cpp
+ Apps/ValidationTests/ReferenceImages/gltfAlien.png
+4 −4 Apps/ValidationTests/Scripts/config.json
+5 −2 Apps/ValidationTests/Scripts/validation_native.js
+157 −281 Apps/package-lock.json
+4 −4 Apps/package.json
+174 −129 Dependencies/napi/napi-direct/source/js_native_api_javascriptcore.cc
+2 −4 Dependencies/napi/napi-jsi/include/napi/napi-inl.h
+5 −1 Dependencies/napi/napi-jsi/include/napi/napi.h
+11 −2 Plugins/NativeCamera/CMakeLists.txt
+2 −23 Plugins/NativeCamera/Include/Babylon/Plugins/NativeCamera.h
+525 −0 Plugins/NativeCamera/Source/Android/CameraDevice.cpp
+0 −477 Plugins/NativeCamera/Source/Android/NativeCameraImpl.cpp
+0 −69 Plugins/NativeCamera/Source/Android/NativeCameraImpl.h
+245 −195 Plugins/NativeCamera/Source/Apple/CameraDevice.mm
+0 −40 Plugins/NativeCamera/Source/Apple/NativeCameraImpl.h
+64 −0 Plugins/NativeCamera/Source/CameraDevice.h
+50 −0 Plugins/NativeCamera/Source/CameraDeviceSharedPImpl.h
+215 −0 Plugins/NativeCamera/Source/Capability.cpp
+69 −0 Plugins/NativeCamera/Source/Capability.h
+139 −0 Plugins/NativeCamera/Source/Constraint.cpp
+56 −0 Plugins/NativeCamera/Source/Constraint.h
+84 −0 Plugins/NativeCamera/Source/MediaDevices.cpp
+6 −0 Plugins/NativeCamera/Source/MediaDevices.h
+363 −0 Plugins/NativeCamera/Source/MediaStream.cpp
+51 −0 Plugins/NativeCamera/Source/MediaStream.h
+33 −108 Plugins/NativeCamera/Source/NativeCamera.cpp
+48 −44 Plugins/NativeCamera/Source/NativeVideo.cpp
+11 −13 Plugins/NativeCamera/Source/NativeVideo.h
+44 −0 Plugins/NativeCamera/Source/UWP/CameraDevice.cpp
+0 −35 Plugins/NativeCamera/Source/UWP/NativeCameraImpl.cpp
+0 −25 Plugins/NativeCamera/Source/UWP/NativeCameraImpl.h
+42 −0 Plugins/NativeCamera/Source/Unix/CameraDevice.cpp
+0 −35 Plugins/NativeCamera/Source/Unix/NativeCameraImpl.cpp
+0 −25 Plugins/NativeCamera/Source/Unix/NativeCameraImpl.h
+44 −0 Plugins/NativeCamera/Source/Win32/CameraDevice.cpp
+0 −35 Plugins/NativeCamera/Source/Win32/NativeCameraImpl.cpp
+0 −25 Plugins/NativeCamera/Source/Win32/NativeCameraImpl.h
+2 −0 Polyfills/Window/CMakeLists.txt
+166 −0 Polyfills/Window/Source/TimeoutDispatcher.cpp
+44 −0 Polyfills/Window/Source/TimeoutDispatcher.h
+22 −24 Polyfills/Window/Source/Window.cpp
+7 −3 Polyfills/Window/Source/Window.h
21 changes: 12 additions & 9 deletions Modules/@babylonjs/react-native/EngineHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Platform } from 'react-native';
import { PERMISSIONS, check, request } from 'react-native-permissions';
import { Engine, WebXRSessionManager, WebXRExperienceHelper, Color4, Tools, VideoTexture } from '@babylonjs/core';
import { ReactNativeEngine } from './ReactNativeEngine';
import { ensureInitialized } from './BabylonModule';

import * as base64 from 'base-64';

Expand Down Expand Up @@ -71,17 +72,19 @@ async function requestCameraPermissionAsync() : Promise<void> {
}
}

// Override the VideoTexture.CreateFromWebCamAsync to insert a camera permissions request. It would be cleaner to do this directly in the NativeCamera implementation, but there are a couple problems with that:
// 1. React Native does not provide a way to hook into the permissions request result (at least on Android).
// 2. If it is done on the native side, then we need one implementation per platform.
{
const originalCreateFromWebCamAsync = VideoTexture.CreateFromWebCamAsync;
VideoTexture.CreateFromWebCamAsync = async function (...args: Parameters<typeof originalCreateFromWebCamAsync>): ReturnType<typeof originalCreateFromWebCamAsync> {
await requestCameraPermissionAsync();
ensureInitialized().then(() => {
// Override the navigator.mediaDevices.getUserMedia to insert a camera permissions request. It would be cleaner to do this directly in the NativeCamera implementation, but there are a couple problems with that:
// 1. React Native does not provide a way to hook into the permissions request result (at least on Android).
// 2. If it is done on the native side, then we need one implementation per platform.
{
const originalGetUserMedia = navigator.mediaDevices.getUserMedia;
navigator.mediaDevices.getUserMedia = async function (...args: Parameters<typeof originalGetUserMedia>): ReturnType<typeof originalGetUserMedia> {
await requestCameraPermissionAsync();

return originalCreateFromWebCamAsync.apply(this, args);
return originalGetUserMedia.apply(this, args);
}
}
}
});

if (Platform.OS === "android" || Platform.OS === "ios") {
const originalEnterXRAsync: (...args: any[]) => Promise<WebXRSessionManager> = WebXRExperienceHelper.prototype.enterXRAsync;
Expand Down
2 changes: 1 addition & 1 deletion Modules/@babylonjs/react-native/shared/BabylonNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace BabylonNative
m_nativeInput = &Babylon::Plugins::NativeInput::CreateForJavaScript(m_env);
Babylon::Plugins::NativeOptimizations::Initialize(m_env);
Babylon::Plugins::NativeTracing::Initialize(m_env);
Babylon::Plugins::Camera::Initialize(m_env);
Babylon::Plugins::NativeCamera::Initialize(m_env);

// Initialize Babylon Native polyfills
Babylon::Polyfills::Window::Initialize(m_env);
Expand Down

0 comments on commit 2a0fb01

Please sign in to comment.