| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/os/semaphore.h" |
| 34 |
#include "core/templates/safe_refcount.h" |
| 35 |
#include "servers/camera/camera_feed.h" |
| 36 |
#include "servers/camera/camera_server.h" |
| 37 |
|
| 38 |
#include <camera/NdkCameraDevice.h> |
| 39 |
#include <camera/NdkCameraError.h> |
| 40 |
#include <camera/NdkCameraManager.h> |
| 41 |
#include <camera/NdkCameraMetadataTags.h> |
| 42 |
#include <media/NdkImageReader.h> |
| 43 |
#include <optional> |
| 44 |
|
| 45 |
enum class CameraFacing { |
| 46 |
BACK = 0, |
| 47 |
FRONT = 1, |
| 48 |
}; |
| 49 |
|
| 50 |
struct CameraRotationParams { |
| 51 |
int sensor_orientation; |
| 52 |
CameraFacing camera_facing; |
| 53 |
int display_rotation; |
| 54 |
}; |
| 55 |
|
| 56 |
class CameraFeedAndroid : public CameraFeed { |
| 57 |
GDSOFTCLASS(CameraFeedAndroid, CameraFeed); |
| 58 |
|
| 59 |
private: |
| 60 |
String camera_id; |
| 61 |
int32_t orientation; |
| 62 |
Ref<Image> image_y; |
| 63 |
Ref<Image> image_uv; |
| 64 |
Vector<uint8_t> data_y; |
| 65 |
Vector<uint8_t> data_uv; |
| 66 |
|
| 67 |
Vector<uint8_t> scratch_y; |
| 68 |
Vector<uint8_t> scratch_uv; |
| 69 |
|
| 70 |
ACameraManager *manager = nullptr; |
| 71 |
ACameraMetadata *metadata = nullptr; |
| 72 |
ACameraDevice *device = nullptr; |
| 73 |
AImageReader *reader = nullptr; |
| 74 |
ACameraCaptureSession *session = nullptr; |
| 75 |
ACaptureRequest *request = nullptr; |
| 76 |
ACaptureSessionOutput *session_output = nullptr; |
| 77 |
ACaptureSessionOutputContainer *session_output_container = nullptr; |
| 78 |
ACameraOutputTarget *camera_output_target = nullptr; |
| 79 |
Mutex callback_mutex; |
| 80 |
Semaphore session_closed_semaphore; |
| 81 |
SafeFlag is_deactivating; |
| 82 |
SafeFlag session_close_pending; |
| 83 |
SafeFlag device_error_occurred; |
| 84 |
bool was_active_before_pause = false; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
ACameraDevice_stateCallbacks device_callbacks = {}; |
| 89 |
AImageReader_ImageListener image_listener = {}; |
| 90 |
ACameraCaptureSession_stateCallbacks session_callbacks = {}; |
| 91 |
|
| 92 |
void _add_formats(); |
| 93 |
void _set_rotation(); |
| 94 |
void refresh_camera_metadata(); |
| 95 |
static std::optional<int> calculate_rotation(const CameraRotationParams &p_params); |
| 96 |
static int normalize_angle(int p_angle); |
| 97 |
static int get_display_rotation(); |
| 98 |
static int get_app_orientation(); |
| 99 |
static void compact_stride_inplace(uint8_t *data, size_t width, int height, size_t stride); |
| 100 |
|
| 101 |
static void onError(void *context, ACameraDevice *p_device, int error); |
| 102 |
static void onDisconnected(void *context, ACameraDevice *p_device); |
| 103 |
static void onImage(void *context, AImageReader *p_reader); |
| 104 |
static void onSessionReady(void *context, ACameraCaptureSession *session); |
| 105 |
static void onSessionActive(void *context, ACameraCaptureSession *session); |
| 106 |
static void onSessionClosed(void *context, ACameraCaptureSession *session); |
| 107 |
|
| 108 |
protected: |
| 109 |
public: |
| 110 |
bool activate_feed() override; |
| 111 |
void deactivate_feed() override; |
| 112 |
bool set_format(int p_index, const Dictionary &p_parameters) override; |
| 113 |
Array get_formats() const override; |
| 114 |
FeedFormat get_format() const override; |
| 115 |
void handle_pause(); |
| 116 |
void handle_resume(); |
| 117 |
void handle_rotation_change(); |
| 118 |
|
| 119 |
CameraFeedAndroid(ACameraManager *manager, ACameraMetadata *metadata, const char *id, |
| 120 |
CameraFeed::FeedPosition position, int32_t orientation); |
| 121 |
~CameraFeedAndroid() override; |
| 122 |
}; |
| 123 |
|
| 124 |
class CameraAndroid : public CameraServer { |
| 125 |
GDSOFTCLASS(CameraAndroid, CameraServer); |
| 126 |
|
| 127 |
private: |
| 128 |
ACameraManager *cameraManager = nullptr; |
| 129 |
|
| 130 |
void update_feeds(); |
| 131 |
void remove_all_feeds(); |
| 132 |
|
| 133 |
public: |
| 134 |
void set_monitoring_feeds(bool p_monitoring_feeds) override; |
| 135 |
void handle_application_pause() override; |
| 136 |
void handle_application_resume() override; |
| 137 |
void handle_display_rotation_change(int p_orientation) override; |
| 138 |
|
| 139 |
~CameraAndroid(); |
| 140 |
}; |