camera_android.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #pragma once
  31. #include "core/os/semaphore.h"
  32. #include "core/templates/safe_refcount.h"
  33. #include "servers/camera/camera_feed.h"
  34. #include "servers/camera/camera_server.h"
  35. #include <camera/NdkCameraDevice.h>
  36. #include <camera/NdkCameraError.h>
  37. #include <camera/NdkCameraManager.h>
  38. #include <camera/NdkCameraMetadataTags.h>
  39. #include <media/NdkImageReader.h>
  40. #include <optional>
  41. enum class CameraFacing {
  42. BACK = 0,
  43. FRONT = 1,
  44. };
  45. struct CameraRotationParams {
  46. int sensor_orientation;
  47. CameraFacing camera_facing;
  48. int display_rotation;
  49. };
  50. class CameraFeedAndroid : public CameraFeed {
  51. GDSOFTCLASS(CameraFeedAndroid, CameraFeed);
  52. private:
  53. String camera_id;
  54. int32_t orientation;
  55. Ref<Image> image_y;
  56. Ref<Image> image_uv;
  57. Vector<uint8_t> data_y;
  58. Vector<uint8_t> data_uv;
  59. // Scratch buffers to avoid reallocations when compacting stride-aligned planes.
  60. Vector<uint8_t> scratch_y;
  61. Vector<uint8_t> scratch_uv;
  62. ACameraManager *manager = nullptr;
  63. ACameraMetadata *metadata = nullptr;
  64. ACameraDevice *device = nullptr;
  65. AImageReader *reader = nullptr;
  66. ACameraCaptureSession *session = nullptr;
  67. ACaptureRequest *request = nullptr;
  68. ACaptureSessionOutput *session_output = nullptr;
  69. ACaptureSessionOutputContainer *session_output_container = nullptr;
  70. ACameraOutputTarget *camera_output_target = nullptr;
  71. Mutex callback_mutex;
  72. Semaphore session_closed_semaphore;
  73. SafeFlag is_deactivating;
  74. SafeFlag session_close_pending;
  75. SafeFlag device_error_occurred;
  76. bool was_active_before_pause = false;
  77. // Callback structures - must be instance members, not static, to ensure
  78. // correct 'this' pointer is captured for each camera feed instance.
  79. ACameraDevice_stateCallbacks device_callbacks = {};
  80. AImageReader_ImageListener image_listener = {};
  81. ACameraCaptureSession_stateCallbacks session_callbacks = {};
  82. void _add_formats();
  83. void _set_rotation();
  84. void refresh_camera_metadata();
  85. static std::optional<int> calculate_rotation(const CameraRotationParams &p_params);
  86. static int normalize_angle(int p_angle);
  87. static int get_display_rotation();
  88. static int get_app_orientation();
  89. static void compact_stride_inplace(uint8_t *data, size_t width, int height, size_t stride);
  90. static void onError(void *context, ACameraDevice *p_device, int error);
  91. static void onDisconnected(void *context, ACameraDevice *p_device);
  92. static void onImage(void *context, AImageReader *p_reader);
  93. static void onSessionReady(void *context, ACameraCaptureSession *session);
  94. static void onSessionActive(void *context, ACameraCaptureSession *session);
  95. static void onSessionClosed(void *context, ACameraCaptureSession *session);
  96. protected:
  97. public:
  98. bool activate_feed() override;
  99. void deactivate_feed() override;
  100. bool set_format(int p_index, const Dictionary &p_parameters) override;
  101. Array get_formats() const override;
  102. FeedFormat get_format() const override;
  103. void handle_pause();
  104. void handle_resume();
  105. void handle_rotation_change();
  106. CameraFeedAndroid(ACameraManager *manager, ACameraMetadata *metadata, const char *id,
  107. CameraFeed::FeedPosition position, int32_t orientation);
  108. ~CameraFeedAndroid() override;
  109. };
  110. class CameraAndroid : public CameraServer {
  111. GDSOFTCLASS(CameraAndroid, CameraServer);
  112. private:
  113. ACameraManager *cameraManager = nullptr;
  114. void update_feeds();
  115. void remove_all_feeds();
  116. public:
  117. void set_monitoring_feeds(bool p_monitoring_feeds) override;
  118. void handle_application_pause() override;
  119. void handle_application_resume() override;
  120. void handle_display_rotation_change(int p_orientation) override;
  121. ~CameraAndroid();
  122. };