Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/camera/camera_android.h
20841 views
1
/**************************************************************************/
2
/* camera_android.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
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
// Scratch buffers to avoid reallocations when compacting stride-aligned planes.
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
// Callback structures - must be instance members, not static, to ensure
87
// correct 'this' pointer is captured for each camera feed instance.
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
};
141
142