The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3296 lines
146KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. //==============================================================================
  20. // This byte-code is generated from:
  21. //
  22. // native/java/com/roli/juce/CameraCaptureSessionCaptureCallback.java
  23. // native/java/com/roli/juce/CameraCaptureSessionStateCallback.java
  24. // native/java/com/roli/juce/CameraDeviceStateCallback.java
  25. // native/java/com/roli/juce/JuceOrientationEventListener.java
  26. //
  27. // files with min sdk version 21
  28. // See juce_core/native/java/README.txt on how to generate this byte-code.
  29. static const uint8 CameraSupportByteCode[] = {
  30. 31,139,8,8,45,45,227,91,0,3,67,97,109,101,114,97,83,117,112,112,111,114,116,46,100,101,120,0,149,152,93,108,28,213,21,
  31. 199,207,157,157,221,89,239,174,215,227,181,243,73,18,236,124,216,14,196,108,190,140,162,174,227,196,56,9,216,93,197,169,
  32. 215,118,35,3,45,147,221,73,60,116,189,179,204,174,151,84,173,74,136,168,8,47,85,80,105,1,9,33,16,1,209,7,36,183,4,194,3,
  33. 15,169,138,10,85,133,148,34,170,246,33,149,120,40,60,180,84,138,80,133,242,192,67,255,247,99,236,217,245,218,53,150,126,
  34. 251,63,51,231,220,115,239,61,247,206,120,102,10,246,249,216,222,3,3,244,159,203,87,222,62,245,197,223,255,124,233,193,91,
  35. 39,95,59,122,248,179,137,191,189,250,253,83,157,191,127,189,47,69,84,38,162,243,211,7,97,201,191,75,237,68,167,73,158,
  36. 239,0,55,24,209,38,232,22,141,40,12,125,32,68,212,199,253,80,29,186,128,159,219,9,162,189,112,190,23,37,186,14,254,5,82,
  37. 45,68,251,193,0,56,4,30,4,53,112,13,124,13,122,99,68,63,0,79,131,223,129,127,0,35,78,116,47,248,33,120,14,124,8,190,2,93,
  38. 200,191,3,244,130,187,121,95,96,0,100,192,81,112,63,24,7,167,193,35,160,0,28,224,130,26,248,41,120,2,60,13,94,3,127,2,95,
  39. 130,142,86,162,35,96,14,60,11,222,2,159,128,175,64,107,146,168,7,28,7,15,131,26,120,2,188,8,94,1,191,1,239,130,247,193,
  40. 135,224,47,224,115,240,53,136,183,17,237,2,195,32,7,30,6,101,240,51,240,11,240,60,120,21,92,3,215,193,199,224,38,248,28,
  41. 252,27,252,23,124,3,162,38,81,59,216,14,250,76,89,111,190,6,6,64,137,9,101,36,148,141,80,34,194,116,8,195,38,116,79,104,
  42. 74,8,39,44,37,165,212,58,118,130,117,96,61,232,81,218,174,214,124,131,178,23,144,120,163,178,223,51,228,186,115,251,58,
  43. 236,59,148,253,17,236,205,202,254,52,96,223,12,196,252,19,246,22,101,223,130,189,85,217,183,97,111,83,182,142,9,220,169,
  44. 236,78,216,93,202,222,25,176,15,6,236,99,176,183,43,123,18,118,183,178,31,10,156,47,192,222,161,236,34,236,157,202,62,15,
  45. 123,151,178,159,10,216,151,3,246,75,129,156,111,6,114,46,68,121,93,25,13,138,250,166,104,92,212,88,30,183,41,53,149,134,
  46. 84,61,117,165,17,165,9,177,34,188,125,84,105,146,118,11,77,208,93,66,91,233,110,161,45,180,71,104,140,250,133,198,233,30,
  47. 161,235,233,136,208,78,58,42,116,29,13,11,109,167,251,212,184,70,132,118,208,49,49,62,77,244,147,196,110,233,83,154,86,
  48. 186,87,233,62,165,251,133,154,52,164,244,184,210,19,74,239,87,154,21,243,149,121,219,48,179,3,74,15,10,53,104,64,29,223,
  49. 43,116,131,200,195,245,1,161,27,105,84,29,143,137,122,201,10,153,168,216,33,85,191,239,146,220,191,44,80,71,166,244,83,
  50. 147,212,124,228,57,191,190,154,210,148,218,200,109,202,239,215,221,207,115,66,249,77,229,79,52,172,143,167,252,97,100,
  51. 230,254,3,166,188,190,202,38,143,159,193,37,244,80,39,67,239,9,248,249,181,119,216,148,57,114,23,137,166,158,98,100,60,
  52. 105,252,202,120,195,184,86,51,194,20,140,59,177,98,92,164,46,110,124,197,56,67,196,197,48,19,62,214,211,166,188,214,115,
  53. 30,226,170,136,123,204,248,57,251,117,45,18,21,81,114,61,249,184,31,241,243,61,139,184,95,34,238,178,113,133,189,163,255,
  54. 177,22,109,17,81,41,244,202,239,27,142,41,107,154,251,45,226,222,70,220,130,241,129,241,87,253,139,48,110,60,181,88,76,
  55. 196,234,232,155,215,164,98,202,122,149,77,38,106,162,9,159,38,214,226,39,202,151,235,210,168,124,52,69,218,190,37,223,
  56. 197,58,95,71,157,239,153,58,95,103,157,239,114,157,111,93,157,239,249,58,223,122,229,147,227,124,121,113,156,154,24,103,
  57. 40,48,206,55,22,219,133,208,110,107,93,206,183,234,124,219,132,47,140,156,252,126,123,53,232,155,188,179,174,221,251,117,
  58. 237,186,132,47,34,90,18,253,193,31,203,222,16,13,135,103,82,122,96,44,31,47,182,211,209,174,71,180,243,247,59,83,104,129,
  59. 235,129,169,221,46,207,133,149,26,202,103,4,124,41,145,159,107,152,228,255,11,223,246,175,13,121,220,66,76,100,90,58,142,
  60. 214,197,183,136,61,18,60,142,171,62,252,113,133,149,29,86,99,244,251,144,255,167,100,254,144,178,163,139,49,50,175,111,
  61. 199,85,27,67,229,226,26,25,116,74,78,117,136,250,70,172,57,219,179,70,172,114,117,222,179,115,118,165,226,184,37,117,52,
  62. 98,21,139,103,172,252,143,238,121,212,170,89,212,211,44,50,87,181,170,13,113,219,100,220,49,187,230,228,237,38,126,54,74,
  63. 108,140,186,199,230,243,246,184,231,216,37,68,32,209,241,26,172,172,83,169,218,37,219,147,129,155,178,86,169,224,185,78,
  64. 33,157,119,75,56,95,77,143,112,61,95,205,208,145,69,215,172,229,21,30,183,60,59,157,23,189,238,79,55,27,229,206,134,9,
  65. 101,232,240,183,76,80,55,143,12,165,191,93,243,12,13,252,191,6,178,92,141,253,244,174,173,89,134,118,175,22,40,134,114,
  66. 194,114,138,144,181,132,78,216,143,205,219,21,148,185,111,13,161,149,249,34,34,251,87,142,156,116,171,86,177,33,124,105,
  67. 94,53,199,126,60,189,210,62,200,208,129,108,222,157,75,123,110,209,73,63,138,13,211,180,186,203,22,119,223,26,26,53,20,
  68. 122,119,211,38,77,246,112,134,246,52,132,174,182,145,51,196,166,73,155,30,5,99,20,154,30,27,229,63,89,210,241,35,204,25,
  69. 10,227,7,118,68,200,24,25,92,179,99,99,242,68,54,139,134,89,4,34,66,159,230,254,240,180,240,226,0,62,54,67,17,89,101,218,
  70. 156,111,50,199,225,124,213,169,217,180,179,153,207,47,154,59,87,46,218,85,187,64,221,171,68,241,189,131,144,93,171,132,
  71. 156,242,220,115,30,206,32,236,174,85,194,114,124,107,149,242,246,240,25,215,227,189,238,89,67,236,210,24,183,175,22,93,
  72. 181,68,198,166,149,24,41,186,124,100,59,154,250,220,210,89,231,220,210,44,183,173,26,84,160,77,205,252,19,182,85,248,49,
  73. 109,200,47,219,54,178,227,173,203,28,199,156,10,238,106,37,59,207,199,188,126,153,251,184,231,185,94,147,124,227,101,108,
  74. 171,2,25,121,121,39,164,141,5,225,11,236,191,145,89,171,116,14,33,97,91,164,48,206,202,235,158,226,103,61,36,59,57,63,
  75. 119,198,246,72,159,117,43,85,106,225,191,147,238,84,197,166,232,226,110,73,53,217,27,109,141,59,161,163,217,186,111,92,
  76. 113,149,55,175,178,166,230,178,21,140,46,174,87,251,242,213,73,212,173,69,210,45,213,21,210,192,245,39,166,141,28,170,84,
  77. 157,176,150,87,199,240,151,44,238,46,57,169,181,140,17,56,86,81,222,166,200,40,123,54,191,63,145,238,161,244,100,120,242,
  78. 190,72,17,79,250,99,21,53,155,81,36,172,200,141,64,109,149,89,119,190,88,184,15,149,81,141,171,179,78,133,90,170,206,28,
  79. 218,90,115,101,234,208,134,166,141,228,96,127,63,109,9,77,77,228,140,228,85,218,14,35,195,141,221,48,198,185,145,214,166,
  80. 38,141,228,235,52,16,154,154,28,228,39,6,245,169,137,211,176,62,161,36,27,226,205,105,29,155,50,146,51,116,135,148,110,
  81. 41,189,82,250,165,180,170,208,78,214,199,15,55,75,233,210,250,190,195,181,71,30,38,66,67,135,190,103,36,15,243,176,147,
  82. 252,4,105,120,120,214,152,166,95,184,160,47,196,216,69,60,229,53,33,206,110,196,24,187,13,94,137,51,246,17,184,5,94,72,
  83. 16,139,132,53,173,29,109,63,72,52,107,183,129,125,150,96,236,27,240,66,43,99,87,193,13,188,252,133,245,144,182,5,109,190,
  84. 108,109,140,239,102,151,146,140,93,1,215,193,77,60,194,105,76,215,118,61,121,65,191,157,228,254,94,246,92,155,124,175,
  85. 241,159,235,124,245,191,177,240,103,30,255,59,11,127,38,242,191,181,248,239,250,252,123,11,87,255,155,75,132,150,190,187,
  86. 176,46,249,94,207,191,189,48,83,190,187,243,119,120,173,75,230,231,223,99,66,42,134,191,211,240,151,105,222,86,188,79,
  87. 153,114,28,252,123,207,255,0,68,14,12,167,40,18,0,0};
  88. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  89. STATICMETHOD (valueOf, "valueOf", "(Ljava/lang/String;)Landroid/graphics/Bitmap$CompressFormat;")
  90. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidBitmapCompressFormat, "android/graphics/Bitmap$CompressFormat", 21)
  91. #undef JNI_CLASS_MEMBERS
  92. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  93. METHOD (close, "close", "()V") \
  94. METHOD (createCaptureRequest, "createCaptureRequest", "(I)Landroid/hardware/camera2/CaptureRequest$Builder;") \
  95. METHOD (createCaptureSession, "createCaptureSession", "(Ljava/util/List;Landroid/hardware/camera2/CameraCaptureSession$StateCallback;Landroid/os/Handler;)V")
  96. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidCameraDevice, "android/hardware/camera2/CameraDevice", 21)
  97. #undef JNI_CLASS_MEMBERS
  98. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  99. METHOD (close, "close", "()V") \
  100. METHOD (getPlanes, "getPlanes", "()[Landroid/media/Image$Plane;")
  101. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidImage, "android/media/Image", 21)
  102. #undef JNI_CLASS_MEMBERS
  103. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  104. METHOD (getBuffer, "getBuffer", "()Ljava/nio/ByteBuffer;")
  105. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidImagePlane, "android/media/Image$Plane", 21)
  106. #undef JNI_CLASS_MEMBERS
  107. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  108. METHOD (acquireLatestImage, "acquireLatestImage", "()Landroid/media/Image;") \
  109. METHOD (close, "close", "()V") \
  110. METHOD (getSurface, "getSurface", "()Landroid/view/Surface;") \
  111. METHOD (setOnImageAvailableListener, "setOnImageAvailableListener", "(Landroid/media/ImageReader$OnImageAvailableListener;Landroid/os/Handler;)V") \
  112. STATICMETHOD (newInstance, "newInstance", "(IIII)Landroid/media/ImageReader;")
  113. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidImageReader, "android/media/ImageReader", 21)
  114. #undef JNI_CLASS_MEMBERS
  115. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  116. METHOD (constructor, "<init>", "()V") \
  117. METHOD (getSurface, "getSurface", "()Landroid/view/Surface;") \
  118. METHOD (prepare, "prepare", "()V") \
  119. METHOD (release, "release", "()V") \
  120. METHOD (setAudioEncoder, "setAudioEncoder", "(I)V") \
  121. METHOD (setAudioSource, "setAudioSource", "(I)V") \
  122. METHOD (setOnErrorListener, "setOnErrorListener", "(Landroid/media/MediaRecorder$OnErrorListener;)V") \
  123. METHOD (setOnInfoListener, "setOnInfoListener", "(Landroid/media/MediaRecorder$OnInfoListener;)V") \
  124. METHOD (setOrientationHint, "setOrientationHint", "(I)V") \
  125. METHOD (setOutputFile, "setOutputFile", "(Ljava/lang/String;)V") \
  126. METHOD (setOutputFormat, "setOutputFormat", "(I)V") \
  127. METHOD (setVideoEncoder, "setVideoEncoder", "(I)V") \
  128. METHOD (setVideoEncodingBitRate, "setVideoEncodingBitRate", "(I)V") \
  129. METHOD (setVideoFrameRate, "setVideoFrameRate", "(I)V") \
  130. METHOD (setVideoSize, "setVideoSize", "(II)V") \
  131. METHOD (setVideoSource, "setVideoSource", "(I)V") \
  132. METHOD (start, "start", "()V") \
  133. METHOD (stop, "stop", "()V")
  134. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidMediaRecorder, "android/media/MediaRecorder", 21)
  135. #undef JNI_CLASS_MEMBERS
  136. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  137. METHOD (constructor, "<init>", "(Landroid/content/Context;)V") \
  138. METHOD (getSurfaceTexture, "getSurfaceTexture", "()Landroid/graphics/SurfaceTexture;") \
  139. METHOD (isAvailable, "isAvailable", "()Z") \
  140. METHOD (setSurfaceTextureListener, "setSurfaceTextureListener", "(Landroid/view/TextureView$SurfaceTextureListener;)V") \
  141. METHOD (setTransform, "setTransform", "(Landroid/graphics/Matrix;)V")
  142. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidTextureView, "android/view/TextureView", 21)
  143. #undef JNI_CLASS_MEMBERS
  144. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  145. METHOD (constructor, "<init>", "(Landroid/graphics/SurfaceTexture;)V")
  146. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidSurface, "android/view/Surface", 21)
  147. #undef JNI_CLASS_MEMBERS
  148. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  149. METHOD (setDefaultBufferSize, "setDefaultBufferSize", "(II)V")
  150. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidSurfaceTexture, "android/graphics/SurfaceTexture", 21)
  151. #undef JNI_CLASS_MEMBERS
  152. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  153. METHOD (getOutputSizesForClass, "getOutputSizes", "(Ljava/lang/Class;)[Landroid/util/Size;") \
  154. METHOD (getOutputSizesForFormat, "getOutputSizes", "(I)[Landroid/util/Size;") \
  155. METHOD (isOutputSupportedFor, "isOutputSupportedFor", "(I)Z") \
  156. METHOD (isOutputSupportedForSurface, "isOutputSupportedFor", "(Landroid/view/Surface;)Z")
  157. DECLARE_JNI_CLASS_WITH_MIN_SDK (AndroidStreamConfigurationMap, "android/hardware/camera2/params/StreamConfigurationMap", 21)
  158. #undef JNI_CLASS_MEMBERS
  159. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  160. METHOD (constructor, "<init>", "()V") \
  161. METHOD (toByteArray, "toByteArray", "()[B") \
  162. METHOD (size, "size", "()I")
  163. DECLARE_JNI_CLASS_WITH_MIN_SDK (ByteArrayOutputStream, "java/io/ByteArrayOutputStream", 21)
  164. #undef JNI_CLASS_MEMBERS
  165. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  166. METHOD (abortCaptures, "abortCaptures", "()V") \
  167. METHOD (capture, "capture", "(Landroid/hardware/camera2/CaptureRequest;Landroid/hardware/camera2/CameraCaptureSession$CaptureCallback;Landroid/os/Handler;)I") \
  168. METHOD (close, "close", "()V") \
  169. METHOD (setRepeatingRequest, "setRepeatingRequest", "(Landroid/hardware/camera2/CaptureRequest;Landroid/hardware/camera2/CameraCaptureSession$CaptureCallback;Landroid/os/Handler;)I") \
  170. METHOD (stopRepeating, "stopRepeating", "()V")
  171. DECLARE_JNI_CLASS_WITH_MIN_SDK (CameraCaptureSession, "android/hardware/camera2/CameraCaptureSession", 21)
  172. #undef JNI_CLASS_MEMBERS
  173. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  174. METHOD (get, "get", "(Landroid/hardware/camera2/CameraCharacteristics$Key;)Ljava/lang/Object;") \
  175. METHOD (getKeys, "getKeys", "()Ljava/util/List;") \
  176. STATICFIELD (CONTROL_AF_AVAILABLE_MODES, "CONTROL_AF_AVAILABLE_MODES", "Landroid/hardware/camera2/CameraCharacteristics$Key;") \
  177. STATICFIELD (LENS_FACING, "LENS_FACING", "Landroid/hardware/camera2/CameraCharacteristics$Key;") \
  178. STATICFIELD (SCALER_STREAM_CONFIGURATION_MAP, "SCALER_STREAM_CONFIGURATION_MAP", "Landroid/hardware/camera2/CameraCharacteristics$Key;") \
  179. STATICFIELD (SENSOR_ORIENTATION, "SENSOR_ORIENTATION", "Landroid/hardware/camera2/CameraCharacteristics$Key;")
  180. DECLARE_JNI_CLASS_WITH_MIN_SDK (CameraCharacteristics, "android/hardware/camera2/CameraCharacteristics", 21)
  181. #undef JNI_CLASS_MEMBERS
  182. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  183. METHOD (getName, "getName", "()Ljava/lang/String;")
  184. DECLARE_JNI_CLASS_WITH_MIN_SDK (CameraCharacteristicsKey, "android/hardware/camera2/CameraCharacteristics$Key", 21)
  185. #undef JNI_CLASS_MEMBERS
  186. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  187. METHOD (getCameraCharacteristics, "getCameraCharacteristics", "(Ljava/lang/String;)Landroid/hardware/camera2/CameraCharacteristics;") \
  188. METHOD (getCameraIdList, "getCameraIdList", "()[Ljava/lang/String;") \
  189. METHOD (openCamera, "openCamera", "(Ljava/lang/String;Landroid/hardware/camera2/CameraDevice$StateCallback;Landroid/os/Handler;)V")
  190. DECLARE_JNI_CLASS_WITH_MIN_SDK (CameraManager, "android/hardware/camera2/CameraManager", 21)
  191. #undef JNI_CLASS_MEMBERS
  192. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  193. STATICFIELD (CONTROL_AE_PRECAPTURE_TRIGGER, "CONTROL_AE_PRECAPTURE_TRIGGER", "Landroid/hardware/camera2/CaptureRequest$Key;") \
  194. STATICFIELD (CONTROL_AF_MODE, "CONTROL_AF_MODE", "Landroid/hardware/camera2/CaptureRequest$Key;") \
  195. STATICFIELD (CONTROL_AF_TRIGGER, "CONTROL_AF_TRIGGER", "Landroid/hardware/camera2/CaptureRequest$Key;") \
  196. STATICFIELD (CONTROL_MODE, "CONTROL_MODE", "Landroid/hardware/camera2/CaptureRequest$Key;")
  197. DECLARE_JNI_CLASS_WITH_MIN_SDK (CaptureRequest, "android/hardware/camera2/CaptureRequest", 21)
  198. #undef JNI_CLASS_MEMBERS
  199. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  200. METHOD (addTarget, "addTarget", "(Landroid/view/Surface;)V") \
  201. METHOD (build, "build", "()Landroid/hardware/camera2/CaptureRequest;") \
  202. METHOD (set, "set", "(Landroid/hardware/camera2/CaptureRequest$Key;Ljava/lang/Object;)V")
  203. DECLARE_JNI_CLASS_WITH_MIN_SDK (CaptureRequestBuilder, "android/hardware/camera2/CaptureRequest$Builder", 21)
  204. #undef JNI_CLASS_MEMBERS
  205. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  206. METHOD (get, "get", "(Landroid/hardware/camera2/CaptureResult$Key;)Ljava/lang/Object;") \
  207. STATICFIELD (CONTROL_AE_STATE, "CONTROL_AE_STATE", "Landroid/hardware/camera2/CaptureResult$Key;") \
  208. STATICFIELD (CONTROL_AF_STATE, "CONTROL_AF_STATE", "Landroid/hardware/camera2/CaptureResult$Key;")
  209. DECLARE_JNI_CLASS_WITH_MIN_SDK (CaptureResult, "android/hardware/camera2/CaptureResult", 21)
  210. #undef JNI_CLASS_MEMBERS
  211. //==============================================================================
  212. class AndroidRunnable : public juce::AndroidInterfaceImplementer
  213. {
  214. public:
  215. struct Owner
  216. {
  217. virtual ~Owner() {}
  218. virtual void run() = 0;
  219. };
  220. AndroidRunnable (Owner& ownerToUse)
  221. : owner (ownerToUse)
  222. {}
  223. private:
  224. Owner& owner;
  225. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  226. {
  227. auto* env = getEnv();
  228. auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
  229. if (methodName == "run")
  230. {
  231. owner.run();
  232. return nullptr;
  233. }
  234. // invoke base class
  235. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  236. }
  237. };
  238. //==============================================================================
  239. class TextureViewSurfaceTextureListener : public AndroidInterfaceImplementer
  240. {
  241. public:
  242. struct Owner
  243. {
  244. virtual ~Owner() {}
  245. virtual void onSurfaceTextureAvailable (LocalRef<jobject>& surface, int width, int height) = 0;
  246. virtual bool onSurfaceTextureDestroyed (LocalRef<jobject>& surface) = 0;
  247. virtual void onSurfaceTextureSizeChanged (LocalRef<jobject>& surface, int width, int height) = 0;
  248. virtual void onSurfaceTextureUpdated (LocalRef<jobject>& surface) = 0;
  249. };
  250. TextureViewSurfaceTextureListener (Owner& ownerToUse)
  251. : owner (ownerToUse)
  252. {}
  253. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  254. {
  255. auto* env = getEnv();
  256. auto methodName = juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
  257. int numArgs = args != nullptr ? env->GetArrayLength (args) : 0;
  258. if (methodName == "onSurfaceTextureAvailable" && numArgs == 3)
  259. {
  260. auto surface = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  261. auto width = LocalRef<jobject> (env->GetObjectArrayElement (args, 1));
  262. auto height = LocalRef<jobject> (env->GetObjectArrayElement (args, 2));
  263. auto widthInt = env->CallIntMethod (width, JavaInteger.intValue);
  264. auto heightInt = env->CallIntMethod (height, JavaInteger.intValue);
  265. owner.onSurfaceTextureAvailable (surface, widthInt, heightInt);
  266. return nullptr;
  267. }
  268. else if (methodName == "onSurfaceTextureDestroyed" && numArgs == 1)
  269. {
  270. auto surface = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  271. auto result = owner.onSurfaceTextureDestroyed (surface);
  272. return env->CallStaticObjectMethod (JavaBoolean, JavaBoolean.valueOf, result);
  273. }
  274. else if (methodName == "onSurfaceTextureSizeChanged" && numArgs == 3)
  275. {
  276. auto surface = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  277. auto width = LocalRef<jobject> (env->GetObjectArrayElement (args, 1));
  278. auto height = LocalRef<jobject> (env->GetObjectArrayElement (args, 2));
  279. auto widthInt = env->CallIntMethod (width, JavaInteger.intValue);
  280. auto heightInt = env->CallIntMethod (height, JavaInteger.intValue);
  281. owner.onSurfaceTextureSizeChanged (surface, widthInt, heightInt);
  282. return nullptr;
  283. }
  284. else if (methodName == "onSurfaceTextureUpdated" && numArgs == 1)
  285. {
  286. auto surface = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  287. owner.onSurfaceTextureUpdated (surface);
  288. return nullptr;
  289. }
  290. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  291. }
  292. private:
  293. Owner& owner;
  294. };
  295. //==============================================================================
  296. class ImageReaderOnImageAvailableListener : public AndroidInterfaceImplementer
  297. {
  298. public:
  299. struct Owner
  300. {
  301. virtual ~Owner() {}
  302. virtual void onImageAvailable (LocalRef<jobject>& imageReader) = 0;
  303. };
  304. ImageReaderOnImageAvailableListener (Owner& ownerToUse)
  305. : owner (ownerToUse)
  306. {}
  307. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  308. {
  309. auto* env = getEnv();
  310. auto methodName = juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
  311. int numArgs = args != nullptr ? env->GetArrayLength (args) : 0;
  312. if (methodName == "onImageAvailable" && numArgs == 1)
  313. {
  314. auto imageReader = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  315. owner.onImageAvailable (imageReader);
  316. return nullptr;
  317. }
  318. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  319. }
  320. private:
  321. Owner& owner;
  322. };
  323. //==============================================================================
  324. class MediaRecorderOnInfoListener : public AndroidInterfaceImplementer
  325. {
  326. public:
  327. struct Owner
  328. {
  329. virtual ~Owner() {}
  330. virtual void onInfo (LocalRef<jobject>& mediaRecorder, int what, int extra) = 0;
  331. };
  332. MediaRecorderOnInfoListener (Owner& ownerToUse)
  333. : owner (ownerToUse)
  334. {}
  335. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  336. {
  337. auto* env = getEnv();
  338. auto methodName = juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
  339. int numArgs = args != nullptr ? env->GetArrayLength (args) : 0;
  340. if (methodName == "onInfo" && numArgs == 3)
  341. {
  342. auto mediaRecorder = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  343. auto what = LocalRef<jobject> (env->GetObjectArrayElement (args, 1));
  344. auto extra = LocalRef<jobject> (env->GetObjectArrayElement (args, 2));
  345. auto whatInt = (int) env->CallIntMethod (what, JavaInteger.intValue);
  346. auto extraInt = (int) env->CallIntMethod (extra, JavaInteger.intValue);
  347. owner.onInfo (mediaRecorder, whatInt, extraInt);
  348. return nullptr;
  349. }
  350. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  351. }
  352. private:
  353. Owner& owner;
  354. };
  355. //==============================================================================
  356. class MediaRecorderOnErrorListener : public AndroidInterfaceImplementer
  357. {
  358. public:
  359. struct Owner
  360. {
  361. virtual ~Owner() {}
  362. virtual void onError (LocalRef<jobject>& mediaRecorder, int what, int extra) = 0;
  363. };
  364. MediaRecorderOnErrorListener (Owner& ownerToUse)
  365. : owner (ownerToUse)
  366. {}
  367. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  368. {
  369. auto* env = getEnv();
  370. auto methodName = juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
  371. int numArgs = args != nullptr ? env->GetArrayLength (args) : 0;
  372. if (methodName == "onError" && numArgs == 3)
  373. {
  374. auto mediaRecorder = LocalRef<jobject> (env->GetObjectArrayElement (args, 0));
  375. auto what = LocalRef<jobject> (env->GetObjectArrayElement (args, 1));
  376. auto extra = LocalRef<jobject> (env->GetObjectArrayElement (args, 2));
  377. auto whatInt = (int) env->CallIntMethod (what, JavaInteger.intValue);
  378. auto extraInt = (int) env->CallIntMethod (extra, JavaInteger.intValue);
  379. owner.onError (mediaRecorder, whatInt, extraInt);
  380. return nullptr;
  381. }
  382. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  383. }
  384. private:
  385. Owner& owner;
  386. };
  387. //==============================================================================
  388. struct CameraDevice::Pimpl
  389. : private ActivityLifecycleCallbacks
  390. {
  391. using InternalOpenCameraResultCallback = std::function<void (const String& /*cameraId*/, const String& /*error*/)>;
  392. Pimpl (CameraDevice& ownerToUse, const String& cameraIdToUse, int /*index*/,
  393. int minWidthToUse, int minHeightToUse, int maxWidthToUse, int maxHeightToUse,
  394. bool /*useHighQuality*/)
  395. : owner (ownerToUse),
  396. minWidth (minWidthToUse),
  397. minHeight (minHeightToUse),
  398. maxWidth (maxWidthToUse),
  399. maxHeight (maxHeightToUse),
  400. cameraId (cameraIdToUse),
  401. activityLifeListener (CreateJavaInterface (this, "android/app/Application$ActivityLifecycleCallbacks")),
  402. cameraManager (initialiseCameraManager()),
  403. cameraCharacteristics (initialiseCameraCharacteristics (cameraManager, cameraId)),
  404. streamConfigurationMap (cameraCharacteristics),
  405. previewDisplay (streamConfigurationMap.getPreviewBufferSize()),
  406. deviceOrientationChangeListener (previewDisplay)
  407. {
  408. startBackgroundThread();
  409. }
  410. ~Pimpl()
  411. {
  412. auto* env = getEnv();
  413. env->CallVoidMethod (getAppContext().get(), AndroidApplication.unregisterActivityLifecycleCallbacks, activityLifeListener.get());
  414. activityLifeListener.clear();
  415. }
  416. JUCE_DECLARE_WEAK_REFERENCEABLE (Pimpl)
  417. String getCameraId() const noexcept { return cameraId; }
  418. void open (InternalOpenCameraResultCallback cameraOpenCallbackToUse)
  419. {
  420. cameraOpenCallback = static_cast<InternalOpenCameraResultCallback&&> (cameraOpenCallbackToUse);
  421. // A valid camera open callback must be passed.
  422. jassert (cameraOpenCallback != nullptr);
  423. // The same camera can be opened only once!
  424. jassert (scopedCameraDevice == nullptr);
  425. if (cameraOpenCallback == nullptr || scopedCameraDevice != nullptr)
  426. return;
  427. WeakReference<Pimpl> safeThis (this);
  428. RuntimePermissions::request (RuntimePermissions::camera, [safeThis] (bool granted) mutable
  429. {
  430. if (safeThis != nullptr)
  431. safeThis->continueOpenRequest (granted);
  432. });
  433. }
  434. void continueOpenRequest (bool granted)
  435. {
  436. if (getAndroidSDKVersion() >= 21)
  437. {
  438. if (granted)
  439. {
  440. getEnv()->CallVoidMethod (getAppContext().get(), AndroidApplication.registerActivityLifecycleCallbacks, activityLifeListener.get());
  441. scopedCameraDevice.reset (new ScopedCameraDevice (*this, cameraId, cameraManager, handler, getAutoFocusModeToUse()));
  442. }
  443. else
  444. {
  445. invokeCameraOpenCallback ("Camera permission not granted");
  446. }
  447. }
  448. else
  449. {
  450. invokeCameraOpenCallback ("Camera requires android sdk version 21 or greater");
  451. }
  452. }
  453. bool openedOk() const noexcept { return scopedCameraDevice->openedOk(); }
  454. void takeStillPicture (std::function<void (const Image&)> pictureTakenCallbackToUse)
  455. {
  456. if (pictureTakenCallbackToUse == nullptr || currentCaptureSessionMode == nullptr)
  457. {
  458. jassertfalse;
  459. return;
  460. }
  461. if (currentCaptureSessionMode->isVideoRecordSession())
  462. {
  463. // Taking still pictures while recording video is not supported on Android.
  464. jassertfalse;
  465. return;
  466. }
  467. pictureTakenCallback = static_cast<std::function<void (const Image&)>&&> (pictureTakenCallbackToUse);
  468. triggerStillPictureCapture();
  469. }
  470. void startRecordingToFile (const File& file, int /*quality*/)
  471. {
  472. if (! openedOk())
  473. {
  474. jassertfalse;
  475. return;
  476. }
  477. if (! previewDisplay.isReady())
  478. {
  479. // Did you remember to create and show a preview display?
  480. jassertfalse;
  481. return;
  482. }
  483. file.deleteFile();
  484. file.create();
  485. jassert (file.existsAsFile());
  486. // MediaRecorder can't handle videos larger than 1080p
  487. auto videoSize = chooseBestSize (minWidth, minHeight, jmin (maxWidth, 1080), maxHeight,
  488. streamConfigurationMap.getSupportedVideoRecordingOutputSizes());
  489. mediaRecorder.reset (new MediaRecorder (file.getFullPathName(), videoSize.getWidth(), videoSize.getHeight(),
  490. getCameraSensorOrientation(), getCameraLensFacing()));
  491. firstRecordedFrameTimeMs = Time::getCurrentTime();
  492. currentCaptureSessionMode.reset();
  493. startVideoRecordingMode (*mediaRecorder);
  494. }
  495. void stopRecording()
  496. {
  497. currentCaptureSessionMode.reset();
  498. mediaRecorder.reset();
  499. startPreviewMode (*imageReader);
  500. }
  501. Time getTimeOfFirstRecordedFrame() const
  502. {
  503. return firstRecordedFrameTimeMs;
  504. }
  505. static StringArray getAvailableDevices()
  506. {
  507. if (getAndroidSDKVersion() < 21)
  508. return StringArray(); // Camera requires SDK version 21 or later
  509. StringArray results;
  510. auto* env = getEnv();
  511. auto cameraManagerToUse = initialiseCameraManager();
  512. auto cameraIdArray = LocalRef<jobjectArray> ((jobjectArray) env->CallObjectMethod (cameraManagerToUse,
  513. CameraManager.getCameraIdList));
  514. results = javaStringArrayToJuce (cameraIdArray);
  515. for (auto& result : results)
  516. printDebugCameraInfo (cameraManagerToUse, result);
  517. return results;
  518. }
  519. void addListener (CameraDevice::Listener* listenerToAdd)
  520. {
  521. const ScopedLock sl (listenerLock);
  522. listeners.add (listenerToAdd);
  523. if (listeners.size() == 1)
  524. triggerStillPictureCapture();
  525. }
  526. void removeListener (CameraDevice::Listener* listenerToRemove)
  527. {
  528. const ScopedLock sl (listenerLock);
  529. listeners.remove (listenerToRemove);
  530. }
  531. private:
  532. enum
  533. {
  534. ERROR_CAMERA_IN_USE = 1,
  535. ERROR_MAX_CAMERAS_IN_USE = 2,
  536. ERROR_CAMERA_DISABLED = 3,
  537. ERROR_CAMERA_DEVICE = 4,
  538. ERROR_CAMERA_SERVICE = 5
  539. };
  540. static String cameraErrorCodeToString (int errorCode)
  541. {
  542. switch (errorCode)
  543. {
  544. case ERROR_CAMERA_IN_USE: return "Camera already in use.";
  545. case ERROR_MAX_CAMERAS_IN_USE: return "Too many opened camera devices.";
  546. case ERROR_CAMERA_DISABLED: return "Camera disabled.";
  547. case ERROR_CAMERA_DEVICE: return "Fatal error.";
  548. case ERROR_CAMERA_SERVICE: return "Fatal error. Reboot required or persistent hardware problem.";
  549. default: return "Unknown error.";
  550. }
  551. }
  552. static LocalRef<jobject> initialiseCameraManager()
  553. {
  554. return LocalRef<jobject> (getEnv()->CallObjectMethod (getAppContext().get(), AndroidContext.getSystemService,
  555. javaString ("camera").get()));
  556. }
  557. static LocalRef<jobject> initialiseCameraCharacteristics (const GlobalRef& cameraManager, const String& cameraId)
  558. {
  559. return LocalRef<jobject> (getEnv()->CallObjectMethod (cameraManager,
  560. CameraManager.getCameraCharacteristics,
  561. javaString (cameraId).get()));
  562. }
  563. static void printDebugCameraInfo (const LocalRef<jobject>& cameraManagerToUse, const String& cameraId)
  564. {
  565. auto* env = getEnv();
  566. auto characteristics = LocalRef<jobject> (env->CallObjectMethod (cameraManagerToUse,
  567. CameraManager.getCameraCharacteristics,
  568. javaString (cameraId).get()));
  569. auto keysList = LocalRef<jobject> (env->CallObjectMethod (characteristics, CameraCharacteristics.getKeys));
  570. const int size = env->CallIntMethod (keysList, JavaList.size);
  571. JUCE_CAMERA_LOG ("Camera id: " + cameraId + ", characteristics keys num: " + String (size));
  572. for (int i = 0; i < size; ++i)
  573. {
  574. auto key = LocalRef<jobject> (env->CallObjectMethod (keysList, JavaList.get, i));
  575. auto jKeyName = LocalRef<jstring> ((jstring) env->CallObjectMethod (key, CameraCharacteristicsKey.getName));
  576. auto keyName = juceString (jKeyName);
  577. auto keyValue = LocalRef<jobject> (env->CallObjectMethod (characteristics, CameraCharacteristics.get, key.get()));
  578. auto jKeyValueString = LocalRef<jstring> ((jstring) env->CallObjectMethod (keyValue, JavaObject.toString));
  579. auto keyValueString = juceString (jKeyValueString);
  580. auto &kvs = keyValueString;
  581. if (kvs.startsWith ("[I") || kvs.startsWith ("[F") || kvs.startsWith ("[Z") || kvs.startsWith ("[B"))
  582. {
  583. printPrimitiveArrayElements (keyValue, keyName, keyValueString);
  584. }
  585. else if (kvs.startsWith ("[Landroid.util.Range"))
  586. {
  587. printRangeArrayElements (keyValue, keyName);
  588. }
  589. else
  590. {
  591. int chunkSize = 256;
  592. if (keyValueString.length() > chunkSize)
  593. {
  594. JUCE_CAMERA_LOG ("Key: " + keyName);
  595. for (int j = 0, k = 1; j < keyValueString.length(); j += chunkSize, ++k)
  596. JUCE_CAMERA_LOG ("value part " + String (k) + ": " + keyValueString.substring (j, k + chunkSize));
  597. }
  598. else
  599. {
  600. JUCE_CAMERA_LOG ("Key: " + keyName + ", value: " + keyValueString);
  601. }
  602. }
  603. ignoreUnused (keyName);
  604. }
  605. }
  606. static void printPrimitiveArrayElements (const LocalRef<jobject>& keyValue, const String& keyName,
  607. const String& keyValueString)
  608. {
  609. ignoreUnused (keyName);
  610. String result = "[";
  611. auto* env = getEnv();
  612. #define PRINT_ELEMENTS(elem_type, array_type, fun_name_middle) \
  613. { \
  614. elem_type* elements = env->Get##fun_name_middle##ArrayElements ((array_type) keyValue.get(), 0); \
  615. int size = env->GetArrayLength ((array_type) keyValue.get()); \
  616. \
  617. for (int i = 0; i < size - 1; ++i) \
  618. result << String (elements[i]) << " "; \
  619. \
  620. if (size > 0) \
  621. result << String (elements[size - 1]); \
  622. \
  623. env->Release##fun_name_middle##ArrayElements ((array_type) keyValue.get(), elements, 0); \
  624. }
  625. if (keyValueString.startsWith ("[I"))
  626. PRINT_ELEMENTS (jint, jintArray, Int)
  627. else if (keyValueString.startsWith ("[F"))
  628. PRINT_ELEMENTS (float, jfloatArray, Float)
  629. else if (keyValueString.startsWith ("[Z"))
  630. PRINT_ELEMENTS (jboolean, jbooleanArray, Boolean)
  631. else if (keyValueString.startsWith ("[B"))
  632. PRINT_ELEMENTS (jbyte, jbyteArray, Byte);
  633. #undef PRINT_ELEMENTS
  634. result << "]";
  635. JUCE_CAMERA_LOG ("Key: " + keyName + ", value: " + result);
  636. }
  637. static void printRangeArrayElements (const LocalRef<jobject>& rangeArray, const String& keyName)
  638. {
  639. auto* env = getEnv();
  640. jobjectArray ranges = static_cast<jobjectArray> (rangeArray.get());
  641. int numRanges = env->GetArrayLength (ranges);
  642. String result;
  643. for (int i = 0; i < numRanges; ++i)
  644. {
  645. auto range = LocalRef<jobject> (env->GetObjectArrayElement (ranges, i));
  646. auto jRangeString = LocalRef<jstring> ((jstring) env->CallObjectMethod (range, AndroidRange.toString));
  647. result << juceString (jRangeString) << " ";
  648. }
  649. ignoreUnused (keyName);
  650. JUCE_CAMERA_LOG ("Key: " + keyName + ", value: " + result);
  651. }
  652. //==============================================================================
  653. class StreamConfigurationMap
  654. {
  655. public:
  656. StreamConfigurationMap (const GlobalRef& cameraCharacteristicsToUse)
  657. : scalerStreamConfigurationMap (getStreamConfigurationMap (cameraCharacteristicsToUse)),
  658. supportedPreviewOutputSizes (retrieveOutputSizes (scalerStreamConfigurationMap,
  659. getClassForName ("android.graphics.SurfaceTexture"),
  660. -1)),
  661. supportedStillImageOutputSizes (retrieveOutputSizes (scalerStreamConfigurationMap,
  662. LocalRef<jobject>(),
  663. jpegImageFormat)),
  664. supportedVideoRecordingOutputSizes (retrieveOutputSizes (scalerStreamConfigurationMap,
  665. getClassForName ("android.media.MediaRecorder"),
  666. -1)),
  667. defaultPreviewSize (getSmallestSize (supportedPreviewOutputSizes)),
  668. previewBufferSize (getLargestSize (supportedPreviewOutputSizes))
  669. {
  670. printSizesLog (supportedPreviewOutputSizes, "SurfaceTexture");
  671. printSizesLog (supportedStillImageOutputSizes, "JPEG");
  672. printSizesLog (supportedVideoRecordingOutputSizes, "MediaRecorder");
  673. }
  674. Array<Rectangle<int>> getSupportedPreviewOutputSizes() const noexcept { return supportedPreviewOutputSizes; }
  675. Array<Rectangle<int>> getSupportedStillImageOutputSizes() const noexcept { return supportedStillImageOutputSizes; }
  676. Array<Rectangle<int>> getSupportedVideoRecordingOutputSizes() const noexcept { return supportedVideoRecordingOutputSizes; }
  677. Rectangle<int> getDefaultPreviewSize() const noexcept { return defaultPreviewSize; }
  678. Rectangle<int> getPreviewBufferSize() const noexcept { return previewBufferSize; }
  679. bool isOutputSupportedForSurface (const LocalRef<jobject>& surface) const
  680. {
  681. return getEnv()->CallBooleanMethod (scalerStreamConfigurationMap, AndroidStreamConfigurationMap.isOutputSupportedForSurface, surface.get()) != 0;
  682. }
  683. static constexpr int jpegImageFormat = 256;
  684. private:
  685. GlobalRef scalerStreamConfigurationMap;
  686. Array<Rectangle<int>> supportedPreviewOutputSizes;
  687. Array<Rectangle<int>> supportedStillImageOutputSizes;
  688. Array<Rectangle<int>> supportedVideoRecordingOutputSizes;
  689. Rectangle<int> defaultPreviewSize, previewBufferSize;
  690. GlobalRef getStreamConfigurationMap (const GlobalRef& cameraCharacteristicsToUse)
  691. {
  692. auto* env = getEnv();
  693. auto scalerStreamConfigurationMapKey = LocalRef<jobject> (env->GetStaticObjectField (CameraCharacteristics,
  694. CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP));
  695. return GlobalRef (LocalRef<jobject> (env->CallObjectMethod (cameraCharacteristicsToUse,
  696. CameraCharacteristics.get,
  697. scalerStreamConfigurationMapKey.get())));
  698. }
  699. static Array<Rectangle<int>> retrieveOutputSizes (GlobalRef& scalerStreamConfigurationMap,
  700. const LocalRef<jobject>& outputClass,
  701. int format)
  702. {
  703. Array<Rectangle<int>> result;
  704. auto* env = getEnv();
  705. auto outputSizes = outputClass.get() != nullptr
  706. ? LocalRef<jobjectArray> ((jobjectArray) env->CallObjectMethod (scalerStreamConfigurationMap,
  707. AndroidStreamConfigurationMap.getOutputSizesForClass,
  708. outputClass.get()))
  709. : LocalRef<jobjectArray> ((jobjectArray) env->CallObjectMethod (scalerStreamConfigurationMap,
  710. AndroidStreamConfigurationMap.getOutputSizesForFormat,
  711. (jint) format));
  712. if (format != -1)
  713. {
  714. auto supported = (env->CallBooleanMethod (scalerStreamConfigurationMap, AndroidStreamConfigurationMap.isOutputSupportedFor, (jint) format) != 0);
  715. if (! supported)
  716. {
  717. // The output format is not supported by this device, still image capture will not work!
  718. jassertfalse;
  719. return {};
  720. }
  721. }
  722. int numSizes = env->GetArrayLength (outputSizes);
  723. jassert (numSizes > 0);
  724. for (int i = 0; i < numSizes; ++i)
  725. {
  726. auto size = LocalRef<jobject> (env->GetObjectArrayElement (outputSizes, i));
  727. auto width = env->CallIntMethod (size, AndroidSize.getWidth);
  728. auto height = env->CallIntMethod (size, AndroidSize.getHeight);
  729. result.add (Rectangle<int> (0, 0, width, height));
  730. }
  731. return result;
  732. }
  733. static LocalRef<jobject> getClassForName (const String& name)
  734. {
  735. return LocalRef<jobject> (getEnv()->CallStaticObjectMethod (JavaClass, JavaClass.forName,
  736. javaString (name).get()));
  737. }
  738. static void printSizesLog (const Array<Rectangle<int>>& sizes, const String& className)
  739. {
  740. ignoreUnused (sizes, className);
  741. JUCE_CAMERA_LOG ("Sizes for class " + className);
  742. #if JUCE_CAMERA_LOG_ENABLED
  743. for (auto& s : sizes)
  744. JUCE_CAMERA_LOG (s.toString() + "\n");
  745. #endif
  746. }
  747. Rectangle<int> getSmallestSize (const Array<Rectangle<int>>& sizes) const
  748. {
  749. if (sizes.size() == 0)
  750. return {};
  751. auto smallestSize = sizes[0];
  752. for (auto& size : sizes)
  753. {
  754. if (size.getWidth() < smallestSize.getWidth() && size.getHeight() < smallestSize.getHeight())
  755. smallestSize = size;
  756. }
  757. return smallestSize;
  758. }
  759. Rectangle<int> getLargestSize (const Array<Rectangle<int>>& sizes) const
  760. {
  761. if (sizes.size() == 0)
  762. return {};
  763. auto largestSize = sizes[0];
  764. for (auto& size : sizes)
  765. {
  766. if (size.getWidth() > largestSize.getWidth() && size.getHeight() > largestSize.getHeight())
  767. largestSize = size;
  768. }
  769. return largestSize;
  770. }
  771. };
  772. //==============================================================================
  773. class PreviewDisplay : private TextureViewSurfaceTextureListener::Owner
  774. {
  775. public:
  776. struct Listener
  777. {
  778. virtual ~Listener() {}
  779. virtual void previewDisplayReady() = 0;
  780. virtual void previewDisplayAboutToBeDestroyed() = 0;
  781. };
  782. PreviewDisplay (Rectangle<int> bufferSize)
  783. : textureViewSurfaceTextureListener (*this),
  784. textureView (LocalRef<jobject> (getEnv()->NewObject (AndroidTextureView, AndroidTextureView.constructor,
  785. getAppContext().get()))),
  786. bufferWidth (bufferSize.getWidth()),
  787. bufferHeight (bufferSize.getHeight())
  788. {
  789. auto* env = getEnv();
  790. if (! isReady())
  791. env->CallVoidMethod (textureView, AndroidTextureView.setSurfaceTextureListener,
  792. CreateJavaInterface (&textureViewSurfaceTextureListener,
  793. "android/view/TextureView$SurfaceTextureListener").get());
  794. }
  795. ~PreviewDisplay()
  796. {
  797. getEnv()->CallVoidMethod (textureView, AndroidTextureView.setSurfaceTextureListener, nullptr);
  798. }
  799. void addListener (Listener* l)
  800. {
  801. if (l == nullptr)
  802. {
  803. jassertfalse;
  804. return;
  805. }
  806. listeners.add (l);
  807. if (isReady())
  808. l->previewDisplayReady();
  809. }
  810. void removeListener (Listener* l)
  811. {
  812. if (l == nullptr)
  813. {
  814. jassertfalse;
  815. return;
  816. }
  817. listeners.remove (l);
  818. }
  819. bool isReady() const
  820. {
  821. return (getEnv()->CallBooleanMethod (textureView, AndroidTextureView.isAvailable) != 0)
  822. && width > 0 && height > 0;
  823. }
  824. LocalRef<jobject> createSurface()
  825. {
  826. // Surface may get destroyed while session is being configured, if
  827. // the preview gets hidden in the meantime, so bailout.
  828. if (! isReady())
  829. return LocalRef<jobject> (nullptr);
  830. auto* env = getEnv();
  831. auto surfaceTexture = LocalRef<jobject> (env->CallObjectMethod (textureView,
  832. AndroidTextureView.getSurfaceTexture));
  833. // NB: too small buffer will result in pixelated preview. A buffer with wrong aspect ratio
  834. // can result in a cropped preview.
  835. env->CallVoidMethod (surfaceTexture, AndroidSurfaceTexture.setDefaultBufferSize, (jint) bufferWidth, (jint) bufferHeight);
  836. auto surface = LocalRef<jobject> (env->NewObject (AndroidSurface, AndroidSurface.constructor, surfaceTexture.get()));
  837. return surface;
  838. }
  839. const GlobalRef& getNativeView() { return textureView; }
  840. void updateSurfaceTransform()
  841. {
  842. auto* env = getEnv();
  843. auto windowManager = LocalRef<jobject> (env->CallObjectMethod (getAppContext(), AndroidContext.getSystemService, javaString ("window").get()));
  844. auto display = LocalRef<jobject> (env->CallObjectMethod (windowManager, AndroidWindowManager.getDefaultDisplay));
  845. auto rotation = env->CallIntMethod (display, AndroidDisplay.getRotation);
  846. static constexpr int rotation90 = 1;
  847. static constexpr int rotation270 = 3;
  848. auto matrix = LocalRef<jobject> (env->NewObject (AndroidMatrix, AndroidMatrix.constructor));
  849. if (rotation == rotation90 || rotation == rotation270)
  850. {
  851. env->CallBooleanMethod (matrix, AndroidMatrix.postScale, jfloat (height / (float) width), jfloat (width / (float) height), (jfloat) 0, (jfloat) 0);
  852. env->CallBooleanMethod (matrix, AndroidMatrix.postRotate, (jfloat) 90 * (rotation - 2), (jfloat) 0, (jfloat) 0);
  853. env->CallBooleanMethod (matrix, AndroidMatrix.postTranslate, (jfloat) (rotation == 3 ? width : 0), (jfloat) (rotation == 1 ? height : 0));
  854. }
  855. env->CallVoidMethod (textureView, AndroidTextureView.setTransform, matrix.get());
  856. }
  857. private:
  858. ListenerList<Listener> listeners;
  859. TextureViewSurfaceTextureListener textureViewSurfaceTextureListener;
  860. GlobalRef textureView;
  861. int width = -1, height = -1;
  862. int bufferWidth, bufferHeight;
  863. void onSurfaceTextureAvailable (LocalRef<jobject>& /*surface*/, int widthToUse, int heightToUse) override
  864. {
  865. JUCE_CAMERA_LOG ("onSurfaceTextureAvailable()");
  866. width = widthToUse;
  867. height = heightToUse;
  868. updateSurfaceTransform();
  869. listeners.call (&Listener::previewDisplayReady);
  870. }
  871. bool onSurfaceTextureDestroyed (LocalRef<jobject>& /*surface*/) override
  872. {
  873. JUCE_CAMERA_LOG ("onSurfaceTextureDestroyed()");
  874. listeners.call (&Listener::previewDisplayAboutToBeDestroyed);
  875. return true;
  876. }
  877. void onSurfaceTextureSizeChanged (LocalRef<jobject>& /*surface*/, int widthToUse, int heightToUse) override
  878. {
  879. JUCE_CAMERA_LOG ("onSurfaceTextureSizeChanged()");
  880. width = widthToUse;
  881. height = heightToUse;
  882. updateSurfaceTransform();
  883. }
  884. void onSurfaceTextureUpdated (LocalRef<jobject>& /*surface*/) override
  885. {
  886. JUCE_CAMERA_LOG ("onSurfaceTextureUpdated()");
  887. }
  888. JUCE_DECLARE_NON_COPYABLE (PreviewDisplay)
  889. };
  890. //==============================================================================
  891. class ImageReader : private ImageReaderOnImageAvailableListener::Owner
  892. {
  893. public:
  894. ImageReader (Pimpl& ownerToUse, GlobalRef& handlerToUse,
  895. int imageWidth, int imageHeight, int cameraSensorOrientationToUse)
  896. : owner (ownerToUse),
  897. cameraSensorOrientation (cameraSensorOrientationToUse),
  898. imageReader (LocalRef<jobject> (getEnv()->CallStaticObjectMethod (AndroidImageReader, AndroidImageReader.newInstance,
  899. imageWidth, imageHeight, StreamConfigurationMap::jpegImageFormat,
  900. numImagesToKeep))),
  901. onImageAvailableListener (*this)
  902. {
  903. getEnv()->CallVoidMethod (imageReader, AndroidImageReader.setOnImageAvailableListener,
  904. CreateJavaInterface (&onImageAvailableListener,
  905. "android/media/ImageReader$OnImageAvailableListener").get(),
  906. handlerToUse.get());
  907. }
  908. ~ImageReader()
  909. {
  910. getEnv()->CallVoidMethod (imageReader, AndroidImageReader.close);
  911. }
  912. LocalRef<jobject> getSurface() const
  913. {
  914. return LocalRef<jobject> (getEnv()->CallObjectMethod (imageReader, AndroidImageReader.getSurface));
  915. }
  916. void resetNotificationFlag()
  917. {
  918. hasNotifiedListeners.set (0);
  919. }
  920. private:
  921. Pimpl& owner;
  922. int cameraSensorOrientation;
  923. GlobalRef imageReader;
  924. ImageReaderOnImageAvailableListener onImageAvailableListener;
  925. static constexpr int numImagesToKeep = 2;
  926. Atomic<int> hasNotifiedListeners { 0 };
  927. JUCE_DECLARE_WEAK_REFERENCEABLE (ImageReader)
  928. void onImageAvailable (LocalRef<jobject>& /*imageReader*/) override
  929. {
  930. JUCE_CAMERA_LOG ("onImageAvailable()");
  931. auto* env = getEnv();
  932. auto jImage = LocalRef<jobject> (env->CallObjectMethod (imageReader, AndroidImageReader.acquireLatestImage));
  933. if (jImage.get() == nullptr)
  934. return;
  935. auto cameraLensFrontFacing = owner.getCameraLensFacing() == 0;
  936. // NB: could use sensor orientation here to get real-world orientation, but then the resulting
  937. // image could not match the UI orientation.
  938. auto image = androidImageToJuceWithFixedOrientation (jImage, owner.deviceOrientationChangeListener.getDeviceOrientation(),
  939. Desktop::getInstance().getCurrentOrientation(),
  940. cameraLensFrontFacing,
  941. cameraSensorOrientation);
  942. env->CallVoidMethod (jImage, AndroidImage.close);
  943. WeakReference<ImageReader> safeThis (this);
  944. owner.callListeners (image);
  945. // Android may take multiple pictures before it handles a request to stop.
  946. if (hasNotifiedListeners.compareAndSetBool (1, 0))
  947. MessageManager::callAsync ([safeThis, image]() mutable { if (safeThis != nullptr) safeThis->owner.notifyPictureTaken (image); });
  948. }
  949. struct ImageBuffer
  950. {
  951. LocalRef<jbyteArray> byteArray;
  952. int size;
  953. };
  954. static Image androidImageToJuceWithFixedOrientation (const LocalRef<jobject>& androidImage,
  955. Desktop::DisplayOrientation deviceOrientationFromAccelerometerSensor,
  956. Desktop::DisplayOrientation targetOrientation,
  957. bool cameraLensFrontFacing,
  958. int cameraSensorOrientation)
  959. {
  960. auto* env = getEnv();
  961. auto planes = LocalRef<jobjectArray> ((jobjectArray) env->CallObjectMethod (androidImage, AndroidImage.getPlanes));
  962. jassert (env->GetArrayLength (planes) > 0);
  963. auto plane = LocalRef<jobject> (env->GetObjectArrayElement (planes, 0));
  964. auto byteBuffer = LocalRef<jobject> (env->CallObjectMethod (plane, AndroidImagePlane.getBuffer));
  965. ImageBuffer correctedBuffer = getImageBufferWithCorrectedOrientationFrom (byteBuffer, deviceOrientationFromAccelerometerSensor,
  966. targetOrientation, cameraLensFrontFacing, cameraSensorOrientation);
  967. jbyte* rawBytes = env->GetByteArrayElements (correctedBuffer.byteArray, nullptr);
  968. Image result = ImageFileFormat::loadFrom (rawBytes, (size_t) correctedBuffer.size);
  969. env->ReleaseByteArrayElements (correctedBuffer.byteArray, rawBytes, 0);
  970. return result;
  971. }
  972. static ImageBuffer getImageBufferWithCorrectedOrientationFrom (const LocalRef<jobject>& imagePlaneBuffer,
  973. Desktop::DisplayOrientation deviceOrientationFromAccelerometerSensor,
  974. Desktop::DisplayOrientation targetOrientation,
  975. bool cameraLensFrontFacing,
  976. int cameraSensorOrientation)
  977. {
  978. auto* env = getEnv();
  979. auto bufferSize = env->CallIntMethod (imagePlaneBuffer, JavaByteBuffer.remaining);
  980. auto byteArray = LocalRef<jbyteArray> (env->NewByteArray (bufferSize));
  981. env->CallObjectMethod (imagePlaneBuffer, JavaByteBuffer.get, byteArray.get());
  982. auto rotationAngle = getRotationAngle (deviceOrientationFromAccelerometerSensor, targetOrientation,
  983. cameraLensFrontFacing, cameraSensorOrientation);
  984. if (rotationAngle == 0)
  985. {
  986. // Nothing to do, just get the bytes
  987. return { byteArray, bufferSize };
  988. }
  989. auto origBitmap = LocalRef<jobject> (env->CallStaticObjectMethod (AndroidBitmapFactory,
  990. AndroidBitmapFactory.decodeByteArray,
  991. byteArray.get(), (jint) 0, (jint) bufferSize));
  992. if (origBitmap == nullptr)
  993. {
  994. // Nothing to do, just get the bytes
  995. return { byteArray, bufferSize };
  996. }
  997. auto correctedBitmap = getBitmapWithCorrectOrientationFrom (origBitmap, rotationAngle);
  998. auto byteArrayOutputStream = LocalRef<jobject> (env->NewObject (ByteArrayOutputStream,
  999. ByteArrayOutputStream.constructor));
  1000. auto jCompressFormatString = javaString ("JPEG");
  1001. auto compressFormat = LocalRef<jobject> (env->CallStaticObjectMethod (AndroidBitmapCompressFormat,
  1002. AndroidBitmapCompressFormat.valueOf,
  1003. jCompressFormatString.get()));
  1004. if (env->CallBooleanMethod (correctedBitmap, AndroidBitmap.compress, compressFormat.get(),
  1005. (jint) 100, byteArrayOutputStream.get()) != 0)
  1006. {
  1007. auto correctedByteArray = LocalRef<jbyteArray> ((jbyteArray) env->CallObjectMethod (byteArrayOutputStream,
  1008. ByteArrayOutputStream.toByteArray));
  1009. int correctedByteArraySize = env->CallIntMethod (byteArrayOutputStream, ByteArrayOutputStream.size);
  1010. return { correctedByteArray, correctedByteArraySize };
  1011. }
  1012. jassertfalse;
  1013. // fallback, return original bitmap
  1014. return { byteArray, bufferSize };
  1015. }
  1016. static int getRotationAngle (Desktop::DisplayOrientation deviceOrientationFromAccelerometerSensor,
  1017. Desktop::DisplayOrientation targetOrientation,
  1018. bool cameraLensFrontFacing,
  1019. int cameraSensorOrientation)
  1020. {
  1021. auto isSensorOrientationHorizontal = deviceOrientationFromAccelerometerSensor == Desktop::rotatedAntiClockwise
  1022. || deviceOrientationFromAccelerometerSensor == Desktop::rotatedClockwise;
  1023. if (cameraLensFrontFacing && isSensorOrientationHorizontal)
  1024. {
  1025. // flip angles for front camera
  1026. return getRotationAngle (deviceOrientationFromAccelerometerSensor, targetOrientation, false, (cameraSensorOrientation + 180) % 360);
  1027. }
  1028. switch (targetOrientation)
  1029. {
  1030. case Desktop::rotatedAntiClockwise:
  1031. return cameraSensorOrientation == 90 ? 0 : 180;
  1032. case Desktop::rotatedClockwise:
  1033. return cameraSensorOrientation == 90 ? 180 : 0;
  1034. case Desktop::upright:
  1035. case Desktop::upsideDown:
  1036. if ((targetOrientation == Desktop::upright && ! cameraLensFrontFacing)
  1037. || (targetOrientation == Desktop::upsideDown && cameraLensFrontFacing))
  1038. {
  1039. return cameraSensorOrientation;
  1040. }
  1041. else
  1042. {
  1043. if (deviceOrientationFromAccelerometerSensor == Desktop::upright || deviceOrientationFromAccelerometerSensor == Desktop::upsideDown)
  1044. return cameraSensorOrientation;
  1045. else
  1046. return (cameraSensorOrientation + 180) % 360;
  1047. }
  1048. break;
  1049. default:
  1050. return 0;
  1051. }
  1052. }
  1053. static LocalRef<jobject> getBitmapWithCorrectOrientationFrom (LocalRef<jobject>& origBitmap, int rotationAngle)
  1054. {
  1055. auto* env = getEnv();
  1056. auto origBitmapWidth = env->CallIntMethod (origBitmap, AndroidBitmap.getWidth);
  1057. auto origBitmapHeight = env->CallIntMethod (origBitmap, AndroidBitmap.getHeight);
  1058. auto matrix = LocalRef<jobject> (env->NewObject (AndroidMatrix, AndroidMatrix.constructor));
  1059. env->CallBooleanMethod (matrix, AndroidMatrix.postRotate, (jfloat) rotationAngle, (jfloat) 0, (jfloat) 0);
  1060. auto rotatedBitmap = LocalRef<jobject> (env->CallStaticObjectMethod (AndroidBitmap, AndroidBitmap.createBitmapFrom,
  1061. origBitmap.get(), (jint) 0, (jint) 0,
  1062. (jint) origBitmapWidth, (jint) origBitmapHeight,
  1063. matrix.get(), true));
  1064. env->CallVoidMethod (origBitmap, AndroidBitmap.recycle);
  1065. return rotatedBitmap;
  1066. }
  1067. };
  1068. //==============================================================================
  1069. class MediaRecorder : private MediaRecorderOnInfoListener::Owner,
  1070. private MediaRecorderOnErrorListener::Owner
  1071. {
  1072. public:
  1073. MediaRecorder (const String& outputFilePath, int videoWidth, int videoHeight,
  1074. int sensorOrientation, int cameraLensFacing)
  1075. : onInfoListener (*this),
  1076. onErrorListener (*this),
  1077. mediaRecorder (LocalRef<jobject> (getEnv()->NewObject (AndroidMediaRecorder,
  1078. AndroidMediaRecorder.constructor)))
  1079. {
  1080. auto* env = getEnv();
  1081. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setOnInfoListener,
  1082. CreateJavaInterface (&onInfoListener,
  1083. "android/media/MediaRecorder$OnInfoListener").get());
  1084. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setOnErrorListener,
  1085. CreateJavaInterface (&onErrorListener,
  1086. "android/media/MediaRecorder$OnErrorListener").get());
  1087. // NB: the order of function calls here is enforced, and exceptions will be thrown if
  1088. // the order is changed.
  1089. static constexpr int audioSourceMic = 1;
  1090. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setAudioSource, (jint) audioSourceMic);
  1091. static constexpr int videoSourceSurface = 2;
  1092. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setVideoSource, (jint) videoSourceSurface);
  1093. static constexpr int outputFormatMPEG4 = 2;
  1094. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setOutputFormat, (jint) outputFormatMPEG4);
  1095. static constexpr int audioEncoderAAC = 3;
  1096. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setAudioEncoder, (jint) audioEncoderAAC);
  1097. static constexpr int videoEncoderH264 = 2;
  1098. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setVideoEncoder, (jint) videoEncoderH264);
  1099. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setVideoEncodingBitRate, (jint) 10000000);
  1100. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setVideoFrameRate, (jint) 30);
  1101. auto frontFacing = cameraLensFacing == 0;
  1102. auto useInverseDegrees = frontFacing && sensorOrientation == 90;
  1103. int orientationHint = getOrientationHint (useInverseDegrees, sensorOrientation);
  1104. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setOrientationHint, (jint) orientationHint);
  1105. getEnv()->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setVideoSize, (jint) videoWidth, (jint) videoHeight);
  1106. getEnv()->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.setOutputFile, javaString (outputFilePath).get());
  1107. getEnv()->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.prepare);
  1108. }
  1109. ~MediaRecorder()
  1110. {
  1111. getEnv()->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.release);
  1112. }
  1113. LocalRef<jobject> getSurface()
  1114. {
  1115. return LocalRef<jobject> (getEnv()->CallObjectMethod (mediaRecorder, AndroidMediaRecorder.getSurface));
  1116. }
  1117. void start()
  1118. {
  1119. lockScreenOrientation();
  1120. getEnv()->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.start);
  1121. hasStartedRecording = true;
  1122. }
  1123. void stop()
  1124. {
  1125. // A request to stop can be sent before recording has had a chance to start, so
  1126. // ignore the request rather than calling AndroidMediaRecorder.stop because
  1127. // otherwise MediaRecorder will throw an exception and...
  1128. if (! hasStartedRecording)
  1129. return;
  1130. hasStartedRecording = false;
  1131. auto* env = getEnv();
  1132. env->CallVoidMethod (mediaRecorder, AndroidMediaRecorder.stop);
  1133. // ... ignore RuntimeException that can be thrown if stop() was called after recording
  1134. // has started but before any frame was written to a file. This is not an error.
  1135. jniCheckHasExceptionOccurredAndClear();
  1136. unlockScreenOrientation();
  1137. }
  1138. private:
  1139. MediaRecorderOnInfoListener onInfoListener;
  1140. MediaRecorderOnErrorListener onErrorListener;
  1141. GlobalRef mediaRecorder;
  1142. bool hasStartedRecording = false;
  1143. int orientationsEnabled = -1;
  1144. void lockScreenOrientation()
  1145. {
  1146. orientationsEnabled = Desktop::getInstance().getOrientationsEnabled();
  1147. auto o = Desktop::getInstance().getCurrentOrientation();
  1148. Desktop::getInstance().setOrientationsEnabled (o);
  1149. }
  1150. static jint juceOrientationToNativeOrientation (int orientations) noexcept
  1151. {
  1152. enum
  1153. {
  1154. SCREEN_ORIENTATION_LANDSCAPE = 0,
  1155. SCREEN_ORIENTATION_PORTRAIT = 1,
  1156. SCREEN_ORIENTATION_USER = 2,
  1157. SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8,
  1158. SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9,
  1159. SCREEN_ORIENTATION_USER_LANDSCAPE = 11,
  1160. SCREEN_ORIENTATION_USER_PORTRAIT = 12,
  1161. };
  1162. switch (orientations)
  1163. {
  1164. case Desktop::upright: return (jint) SCREEN_ORIENTATION_PORTRAIT;
  1165. case Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  1166. case Desktop::upright + Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_USER_PORTRAIT;
  1167. case Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_LANDSCAPE;
  1168. case Desktop::rotatedClockwise: return (jint) SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  1169. case Desktop::rotatedClockwise + Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_USER_LANDSCAPE;
  1170. default: return (jint) SCREEN_ORIENTATION_USER;
  1171. }
  1172. }
  1173. void unlockScreenOrientation()
  1174. {
  1175. Desktop::getInstance().setOrientationsEnabled (orientationsEnabled);
  1176. }
  1177. void onInfo (LocalRef<jobject>& recorder, int what, int extra) override
  1178. {
  1179. ignoreUnused (recorder, what, extra);
  1180. JUCE_CAMERA_LOG ("MediaRecorder::OnInfo: " + getInfoStringFromCode (what)
  1181. + ", extra code = " + String (extra));
  1182. }
  1183. void onError (LocalRef<jobject>& recorder, int what, int extra) override
  1184. {
  1185. ignoreUnused (recorder, what, extra);
  1186. JUCE_CAMERA_LOG ("MediaRecorder::onError: " + getErrorStringFromCode (what)
  1187. + ", extra code = " + String (extra));
  1188. }
  1189. static String getInfoStringFromCode (int what)
  1190. {
  1191. enum
  1192. {
  1193. MEDIA_RECORDER_INFO_UNKNOWN = 1,
  1194. MEDIA_RECORDER_INFO_MAX_DURATION_REACHED = 800,
  1195. MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED = 801,
  1196. MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING = 802,
  1197. MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED = 803
  1198. };
  1199. switch (what)
  1200. {
  1201. case MEDIA_RECORDER_INFO_UNKNOWN: return { "Unknown info" };
  1202. case MEDIA_RECORDER_INFO_MAX_DURATION_REACHED: return { "Max duration reached" };
  1203. case MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED: return { "Max filesize reached" };
  1204. case MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING: return { "Max filesize approaching" };
  1205. case MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED: return { "Next output file started" };
  1206. default: return String (what);
  1207. };
  1208. }
  1209. static String getErrorStringFromCode (int what)
  1210. {
  1211. enum
  1212. {
  1213. MEDIA_RECORDER_ERROR_UNKNOWN = 1,
  1214. MEDIA_ERROR_SERVER_DIED = 100
  1215. };
  1216. switch (what)
  1217. {
  1218. case MEDIA_RECORDER_ERROR_UNKNOWN: return { "Unknown error" };
  1219. case MEDIA_ERROR_SERVER_DIED: return { "Server died" };
  1220. default: return String (what);
  1221. };
  1222. }
  1223. static int getOrientationHint (bool useInverseDegrees, int cameraSensorOrientation)
  1224. {
  1225. auto* env = getEnv();
  1226. auto windowManager = LocalRef<jobject> (env->CallObjectMethod (getAppContext(), AndroidContext.getSystemService, javaString ("window").get()));
  1227. auto display = LocalRef<jobject> (env->CallObjectMethod (windowManager, AndroidWindowManager.getDefaultDisplay));
  1228. auto rotation = env->CallIntMethod (display, AndroidDisplay.getRotation);
  1229. enum
  1230. {
  1231. ROTATION_0 = 0,
  1232. ROTATION_90,
  1233. ROTATION_180,
  1234. ROTATION_270
  1235. };
  1236. int hint = 0;
  1237. switch (rotation)
  1238. {
  1239. case ROTATION_0: hint = cameraSensorOrientation; break;
  1240. case ROTATION_90: hint = useInverseDegrees ? 180 : 0; break;
  1241. case ROTATION_180: hint = cameraSensorOrientation + 180; break;
  1242. case ROTATION_270: hint = useInverseDegrees ? 0 : 180; break;
  1243. default: jassertfalse;
  1244. }
  1245. return (hint + 360) % 360;
  1246. }
  1247. };
  1248. //==============================================================================
  1249. class ScopedCameraDevice
  1250. {
  1251. public:
  1252. //==============================================================================
  1253. class CaptureSession
  1254. {
  1255. public:
  1256. struct ConfiguredCallback
  1257. {
  1258. virtual ~ConfiguredCallback() {}
  1259. virtual void captureSessionConfigured (CaptureSession*) = 0;
  1260. };
  1261. ~CaptureSession()
  1262. {
  1263. bool calledClose = false;
  1264. auto* env = getEnv();
  1265. {
  1266. const ScopedLock lock (captureSessionLock);
  1267. if (captureSession.get() != nullptr)
  1268. {
  1269. calledClose = true;
  1270. env->CallVoidMethod (captureSession, CameraCaptureSession.close);
  1271. }
  1272. }
  1273. // When exception occurs, CameraCaptureSession.close will never finish, so
  1274. // we should not wait for it. For fatal error an exception does occur, but
  1275. // it is catched internally in Java...
  1276. if (jniCheckHasExceptionOccurredAndClear() || scopedCameraDevice.fatalErrorOccurred.get())
  1277. {
  1278. JUCE_CAMERA_LOG ("Exception or fatal error occurred while closing Capture Session, closing by force");
  1279. }
  1280. else if (calledClose)
  1281. {
  1282. pendingClose.set (1);
  1283. closedEvent.wait (-1);
  1284. }
  1285. }
  1286. bool openedOk() const noexcept { return captureSession != nullptr; }
  1287. const GlobalRef& getNativeSession() const { return captureSession; }
  1288. bool start (const LocalRef<jobject>& targetSurfacesList, GlobalRef& handlerToUse)
  1289. {
  1290. if (! openedOk())
  1291. {
  1292. jassertfalse;
  1293. return false;
  1294. }
  1295. auto* env = getEnv();
  1296. auto numSurfaces = env->CallIntMethod (targetSurfacesList, JavaArrayList.size);
  1297. for (int i = 0; i < numSurfaces; ++i)
  1298. {
  1299. auto surface = LocalRef<jobject> (env->CallObjectMethod (targetSurfacesList, JavaArrayList.get, (jint) i));
  1300. env->CallVoidMethod (captureRequestBuilder, CaptureRequestBuilder.addTarget, surface.get());
  1301. }
  1302. previewCaptureRequest = GlobalRef (LocalRef<jobject>(env->CallObjectMethod (captureRequestBuilder, CaptureRequestBuilder.build)));
  1303. env->CallIntMethod (captureSession, CameraCaptureSession.setRepeatingRequest,
  1304. previewCaptureRequest.get(), nullptr, handlerToUse.get());
  1305. return true;
  1306. }
  1307. void takeStillPicture (jobject targetSurface)
  1308. {
  1309. if (stillPictureTaker == nullptr)
  1310. {
  1311. // Can only take picture once session was successfully configured!
  1312. jassertfalse;
  1313. return;
  1314. }
  1315. auto* env = getEnv();
  1316. static constexpr int templateStillCapture = 2;
  1317. auto builder = LocalRef<jobject> (env->CallObjectMethod (scopedCameraDevice.cameraDevice,
  1318. AndroidCameraDevice.createCaptureRequest,
  1319. (jint) templateStillCapture));
  1320. env->CallVoidMethod (builder, CaptureRequestBuilder.addTarget, targetSurface);
  1321. setCaptureRequestBuilderIntegerKey (builder, CaptureRequest.CONTROL_AF_MODE, autoFocusMode);
  1322. auto stillPictureCaptureRequest = LocalRef<jobject> (env->CallObjectMethod (builder, CaptureRequestBuilder.build));
  1323. stillPictureTaker->takePicture (stillPictureCaptureRequest);
  1324. }
  1325. private:
  1326. //==============================================================================
  1327. class StillPictureTaker : private AndroidRunnable::Owner
  1328. {
  1329. public:
  1330. StillPictureTaker (GlobalRef& captureSessionToUse, GlobalRef& captureRequestBuilderToUse,
  1331. GlobalRef& previewCaptureRequestToUse, GlobalRef& handlerToUse,
  1332. int autoFocusModeToUse)
  1333. : captureSession (captureSessionToUse),
  1334. captureRequestBuilder (captureRequestBuilderToUse),
  1335. previewCaptureRequest (previewCaptureRequestToUse),
  1336. handler (handlerToUse),
  1337. runnable (*this),
  1338. captureSessionPreviewCaptureCallback (createCaptureSessionCallback (true)),
  1339. captureSessionStillPictureCaptureCallback (createCaptureSessionCallback (false)),
  1340. autoFocusMode (autoFocusModeToUse)
  1341. {
  1342. }
  1343. void takePicture (const LocalRef<jobject>& stillPictureCaptureRequestToUse)
  1344. {
  1345. JUCE_CAMERA_LOG ("Taking picture...");
  1346. stillPictureCaptureRequest = GlobalRef (LocalRef<jobject>(stillPictureCaptureRequestToUse));
  1347. lockFocus();
  1348. }
  1349. private:
  1350. GlobalRef& captureSession;
  1351. GlobalRef& captureRequestBuilder;
  1352. GlobalRef& previewCaptureRequest;
  1353. GlobalRef& handler;
  1354. AndroidRunnable runnable;
  1355. GlobalRef delayedCaptureRunnable;
  1356. GlobalRef captureSessionPreviewCaptureCallback;
  1357. GlobalRef stillPictureCaptureRequest;
  1358. GlobalRef captureSessionStillPictureCaptureCallback;
  1359. int autoFocusMode;
  1360. //==============================================================================
  1361. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  1362. METHOD (constructor, "<init>", "(JZ)V") \
  1363. CALLBACK (cameraCaptureSessionCaptureCompletedCallback, "cameraCaptureSessionCaptureCompleted", "(JZLandroid/hardware/camera2/CameraCaptureSession;Landroid/hardware/camera2/CaptureRequest;Landroid/hardware/camera2/TotalCaptureResult;)V") \
  1364. CALLBACK (cameraCaptureSessionCaptureFailedCallback, "cameraCaptureSessionCaptureFailed", "(JZLandroid/hardware/camera2/CameraCaptureSession;Landroid/hardware/camera2/CaptureRequest;Landroid/hardware/camera2/CaptureFailure;)V") \
  1365. CALLBACK (cameraCaptureSessionCaptureProgressedCallback, "cameraCaptureSessionCaptureProgressed", "(JZLandroid/hardware/camera2/CameraCaptureSession;Landroid/hardware/camera2/CaptureRequest;Landroid/hardware/camera2/CaptureResult;)V") \
  1366. CALLBACK (cameraCaptureSessionCaptureStartedCallback, "cameraCaptureSessionCaptureStarted", "(JZLandroid/hardware/camera2/CameraCaptureSession;Landroid/hardware/camera2/CaptureRequest;JJ)V") \
  1367. CALLBACK (cameraCaptureSessionCaptureSequenceAbortedCallback, "cameraCaptureSessionCaptureSequenceAborted", "(JZLandroid/hardware/camera2/CameraCaptureSession;I)V") \
  1368. CALLBACK (cameraCaptureSessionCaptureSequenceCompletedCallback, "cameraCaptureSessionCaptureSequenceCompleted", "(JZLandroid/hardware/camera2/CameraCaptureSession;IJ)V")
  1369. DECLARE_JNI_CLASS_WITH_BYTECODE (CameraCaptureSessionCaptureCallback, "com/roli/juce/CameraCaptureSessionCaptureCallback", 21, CameraSupportByteCode, sizeof(CameraSupportByteCode))
  1370. #undef JNI_CLASS_MEMBERS
  1371. LocalRef<jobject> createCaptureSessionCallback (bool createPreviewSession)
  1372. {
  1373. return LocalRef<jobject>(getEnv()->NewObject (CameraCaptureSessionCaptureCallback,
  1374. CameraCaptureSessionCaptureCallback.constructor,
  1375. reinterpret_cast<jlong> (this),
  1376. createPreviewSession ? 1 : 0));
  1377. }
  1378. //==============================================================================
  1379. enum class State
  1380. {
  1381. idle = 0,
  1382. pendingFocusLock,
  1383. pendingExposurePrecapture,
  1384. pendingExposurePostPrecapture,
  1385. pictureTaken
  1386. };
  1387. State currentState = State::idle;
  1388. void lockFocus()
  1389. {
  1390. if (jniCheckHasExceptionOccurredAndClear())
  1391. return;
  1392. JUCE_CAMERA_LOG ("Performing auto-focus if possible...");
  1393. currentState = State::pendingFocusLock;
  1394. auto* env = getEnv();
  1395. // NB: auto-focus may be unavailable on a device, in which case it may have already
  1396. // automatically adjusted the exposure. We check for that in updateState().
  1397. static constexpr int controlAfTriggerStart = 1;
  1398. CaptureSession::setCaptureRequestBuilderIntegerKey (captureRequestBuilder.get(),
  1399. CaptureRequest.CONTROL_AF_TRIGGER,
  1400. controlAfTriggerStart);
  1401. auto previewRequest = LocalRef<jobject> (env->CallObjectMethod (captureRequestBuilder,
  1402. CaptureRequestBuilder.build));
  1403. env->CallIntMethod (captureSession, CameraCaptureSession.capture, previewRequest.get(),
  1404. captureSessionPreviewCaptureCallback.get(), handler.get());
  1405. }
  1406. void updateState (jobject captureResult)
  1407. {
  1408. // IllegalStateException can be thrown when accessing CaptureSession,
  1409. // claiming that capture session was already closed but we may not
  1410. // get relevant callback yet, so check for this and bailout when needed.
  1411. if (jniCheckHasExceptionOccurredAndClear())
  1412. return;
  1413. switch (currentState)
  1414. {
  1415. case State::pendingFocusLock:
  1416. {
  1417. JUCE_CAMERA_LOG ("Still picture capture, updateState(), State::pendingFocusLock...");
  1418. auto controlAfStateValue = getCaptureResultIntegerKeyValue (CaptureResult.CONTROL_AF_STATE, captureResult);
  1419. if (controlAfStateValue.get() == nullptr)
  1420. {
  1421. captureStillPictureDelayed();
  1422. return;
  1423. }
  1424. auto autoToFocusNotAvailable = autoFocusMode == 0;
  1425. if (autoToFocusNotAvailable || autoFocusHasFinished (controlAfStateValue))
  1426. {
  1427. auto controlAeStateIntValue = getControlAEState (captureResult);
  1428. static constexpr int controlAeStateConverged = 2;
  1429. if (controlAeStateIntValue == -1 || controlAeStateIntValue == controlAeStateConverged)
  1430. {
  1431. currentState = State::pictureTaken;
  1432. captureStillPictureDelayed();
  1433. }
  1434. else
  1435. {
  1436. runPrecaptureSequence();
  1437. }
  1438. }
  1439. break;
  1440. }
  1441. case State::pendingExposurePrecapture:
  1442. {
  1443. JUCE_CAMERA_LOG ("Still picture capture, updateState(), State::pendingExposurePrecapture...");
  1444. auto controlAeStateIntValue = getControlAEState (captureResult);
  1445. static constexpr int controlAeStateFlashRequired = 4;
  1446. static constexpr int controlAeStatePrecapture = 5;
  1447. if (controlAeStateIntValue == -1 || controlAeStateIntValue == controlAeStateFlashRequired
  1448. || controlAeStateIntValue == controlAeStatePrecapture)
  1449. {
  1450. currentState = State::pendingExposurePostPrecapture;
  1451. }
  1452. break;
  1453. }
  1454. case State::pendingExposurePostPrecapture:
  1455. {
  1456. JUCE_CAMERA_LOG ("Still picture capture, updateState(), State::pendingExposurePostPrecapture...");
  1457. auto controlAeStateIntValue = getControlAEState (captureResult);
  1458. static constexpr int controlAeStatePrecapture = 5;
  1459. if (controlAeStateIntValue == -1 || controlAeStateIntValue != controlAeStatePrecapture)
  1460. {
  1461. currentState = State::pictureTaken;
  1462. captureStillPictureDelayed();
  1463. }
  1464. break;
  1465. }
  1466. case State::idle:
  1467. case State::pictureTaken:
  1468. { /* do nothing */ break; }
  1469. };
  1470. }
  1471. static int getControlAEState (jobject captureResult)
  1472. {
  1473. auto controlAeStateValue = getCaptureResultIntegerKeyValue (CaptureResult.CONTROL_AE_STATE, captureResult);
  1474. return controlAeStateValue.get() != nullptr
  1475. ? getEnv()->CallIntMethod (controlAeStateValue, JavaInteger.intValue) : -1;
  1476. }
  1477. static bool autoFocusHasFinished (const LocalRef<jobject>& controlAfStateValue)
  1478. {
  1479. static constexpr int controlAfStateFocusedLocked = 4;
  1480. static constexpr int controlAfStateNotFocusedLocked = 5;
  1481. auto controlAfStateIntValue = getEnv()->CallIntMethod (controlAfStateValue, JavaInteger.intValue);
  1482. return controlAfStateIntValue == controlAfStateFocusedLocked || controlAfStateIntValue == controlAfStateNotFocusedLocked;
  1483. }
  1484. static LocalRef<jobject> getCaptureResultIntegerKeyValue (jfieldID key, jobject captureResult)
  1485. {
  1486. auto* env = getEnv();
  1487. auto jKey = LocalRef<jobject> (env->GetStaticObjectField (CaptureResult, key));
  1488. return LocalRef<jobject> (env->CallObjectMethod (captureResult, CaptureResult.get, jKey.get()));
  1489. }
  1490. void captureStillPictureDelayed()
  1491. {
  1492. if (jniCheckHasExceptionOccurredAndClear())
  1493. return;
  1494. JUCE_CAMERA_LOG ("Still picture capture, device ready, capturing now...");
  1495. auto* env = getEnv();
  1496. env->CallVoidMethod (captureSession, CameraCaptureSession.stopRepeating);
  1497. if (jniCheckHasExceptionOccurredAndClear())
  1498. return;
  1499. env->CallVoidMethod (captureSession, CameraCaptureSession.abortCaptures);
  1500. if (jniCheckHasExceptionOccurredAndClear())
  1501. return;
  1502. // Delay still picture capture for devices that can't handle it right after
  1503. // stopRepeating/abortCaptures calls.
  1504. if (delayedCaptureRunnable.get() == nullptr)
  1505. delayedCaptureRunnable = GlobalRef (CreateJavaInterface (&runnable, "java/lang/Runnable"));
  1506. env->CallBooleanMethod (handler, AndroidHandler.postDelayed, delayedCaptureRunnable.get(), (jlong) 200);
  1507. }
  1508. void runPrecaptureSequence()
  1509. {
  1510. if (jniCheckHasExceptionOccurredAndClear())
  1511. return;
  1512. auto* env = getEnv();
  1513. static constexpr int controlAePrecaptureTriggerStart = 1;
  1514. CaptureSession::setCaptureRequestBuilderIntegerKey (captureRequestBuilder.get(),
  1515. CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
  1516. controlAePrecaptureTriggerStart);
  1517. currentState = State::pendingExposurePrecapture;
  1518. auto previewRequest = LocalRef<jobject> (env->CallObjectMethod (captureRequestBuilder,
  1519. CaptureRequestBuilder.build));
  1520. env->CallIntMethod (captureSession, CameraCaptureSession.capture, previewRequest.get(),
  1521. captureSessionPreviewCaptureCallback.get(), handler.get());
  1522. }
  1523. void unlockFocus()
  1524. {
  1525. if (jniCheckHasExceptionOccurredAndClear())
  1526. return;
  1527. JUCE_CAMERA_LOG ("Unlocking focus...");
  1528. currentState = State::idle;
  1529. auto* env = getEnv();
  1530. static constexpr int controlAfTriggerCancel = 2;
  1531. CaptureSession::setCaptureRequestBuilderIntegerKey (captureRequestBuilder.get(),
  1532. CaptureRequest.CONTROL_AF_TRIGGER,
  1533. controlAfTriggerCancel);
  1534. auto resetAutoFocusRequest = LocalRef<jobject> (env->CallObjectMethod (captureRequestBuilder,
  1535. CaptureRequestBuilder.build));
  1536. env->CallIntMethod (captureSession, CameraCaptureSession.capture, resetAutoFocusRequest.get(),
  1537. nullptr, handler.get());
  1538. if (jniCheckHasExceptionOccurredAndClear())
  1539. return;
  1540. // NB: for preview, using preview capture request again
  1541. env->CallIntMethod (captureSession, CameraCaptureSession.setRepeatingRequest, previewCaptureRequest.get(),
  1542. nullptr, handler.get());
  1543. }
  1544. //==============================================================================
  1545. void run() override
  1546. {
  1547. captureStillPicture();
  1548. }
  1549. void captureStillPicture()
  1550. {
  1551. getEnv()->CallIntMethod (captureSession, CameraCaptureSession.capture,
  1552. stillPictureCaptureRequest.get(), captureSessionStillPictureCaptureCallback.get(),
  1553. nullptr);
  1554. }
  1555. //==============================================================================
  1556. void cameraCaptureSessionCaptureCompleted (bool isPreview, jobject session, jobject request, jobject result)
  1557. {
  1558. JUCE_CAMERA_LOG ("cameraCaptureSessionCaptureCompleted()");
  1559. ignoreUnused (session, request);
  1560. if (isPreview)
  1561. updateState (result);
  1562. else if (currentState != State::idle)
  1563. unlockFocus();
  1564. }
  1565. void cameraCaptureSessionCaptureFailed (bool isPreview, jobject session, jobject request, jobject failure)
  1566. {
  1567. JUCE_CAMERA_LOG ("cameraCaptureSessionCaptureFailed()");
  1568. ignoreUnused (isPreview, session, request, failure);
  1569. }
  1570. void cameraCaptureSessionCaptureProgressed (bool isPreview, jobject session, jobject request, jobject partialResult)
  1571. {
  1572. JUCE_CAMERA_LOG ("cameraCaptureSessionCaptureProgressed()");
  1573. ignoreUnused (session, request);
  1574. if (isPreview)
  1575. updateState (partialResult);
  1576. }
  1577. void cameraCaptureSessionCaptureSequenceAborted (bool isPreview, jobject session, int sequenceId)
  1578. {
  1579. JUCE_CAMERA_LOG ("cameraCaptureSessionCaptureSequenceAborted()");
  1580. ignoreUnused (isPreview, isPreview, session, sequenceId);
  1581. }
  1582. void cameraCaptureSessionCaptureSequenceCompleted (bool isPreview, jobject session, int sequenceId, int64 frameNumber)
  1583. {
  1584. JUCE_CAMERA_LOG ("cameraCaptureSessionCaptureSequenceCompleted()");
  1585. ignoreUnused (isPreview, session, sequenceId, frameNumber);
  1586. }
  1587. void cameraCaptureSessionCaptureStarted (bool isPreview, jobject session, jobject request, int64 timestamp, int64 frameNumber)
  1588. {
  1589. JUCE_CAMERA_LOG ("cameraCaptureSessionCaptureStarted()");
  1590. ignoreUnused (isPreview, session, request, timestamp, frameNumber);
  1591. }
  1592. //==============================================================================
  1593. static void cameraCaptureSessionCaptureCompletedCallback (JNIEnv*, jobject /*object*/, jlong host, jboolean isPreview, jobject rawSession, jobject rawRequest, jobject rawResult)
  1594. {
  1595. if (auto* myself = reinterpret_cast<StillPictureTaker*> (host))
  1596. {
  1597. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1598. LocalRef<jobject> request (getEnv()->NewLocalRef(rawRequest));
  1599. LocalRef<jobject> result (getEnv()->NewLocalRef(rawResult));
  1600. myself->cameraCaptureSessionCaptureCompleted (isPreview != 0, session, request, result);
  1601. }
  1602. }
  1603. static void cameraCaptureSessionCaptureFailedCallback (JNIEnv*, jobject /*object*/, jlong host, jboolean isPreview, jobject rawSession, jobject rawRequest, jobject rawResult)
  1604. {
  1605. if (auto* myself = reinterpret_cast<StillPictureTaker*> (host))
  1606. {
  1607. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1608. LocalRef<jobject> request (getEnv()->NewLocalRef(rawRequest));
  1609. LocalRef<jobject> result (getEnv()->NewLocalRef(rawResult));
  1610. myself->cameraCaptureSessionCaptureFailed (isPreview != 0, session, request, result);
  1611. }
  1612. }
  1613. static void cameraCaptureSessionCaptureProgressedCallback (JNIEnv*, jobject /*object*/, jlong host, jboolean isPreview, jobject rawSession, jobject rawRequest, jobject rawResult)
  1614. {
  1615. if (auto* myself = reinterpret_cast<StillPictureTaker*> (host))
  1616. {
  1617. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1618. LocalRef<jobject> request (getEnv()->NewLocalRef(rawRequest));
  1619. LocalRef<jobject> result (getEnv()->NewLocalRef(rawResult));
  1620. myself->cameraCaptureSessionCaptureProgressed (isPreview != 0, session, request, result);
  1621. }
  1622. }
  1623. static void cameraCaptureSessionCaptureSequenceAbortedCallback (JNIEnv*, jobject /*object*/, jlong host, jboolean isPreview, jobject rawSession, jint sequenceId)
  1624. {
  1625. if (auto* myself = reinterpret_cast<StillPictureTaker*> (host))
  1626. {
  1627. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1628. myself->cameraCaptureSessionCaptureSequenceAborted (isPreview != 0, session, sequenceId);
  1629. }
  1630. }
  1631. static void cameraCaptureSessionCaptureSequenceCompletedCallback (JNIEnv*, jobject /*object*/, jlong host, jboolean isPreview, jobject rawSession, jint sequenceId, jlong frameNumber)
  1632. {
  1633. if (auto* myself = reinterpret_cast<StillPictureTaker*> (host))
  1634. {
  1635. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1636. myself->cameraCaptureSessionCaptureSequenceCompleted (isPreview != 0, session, sequenceId, frameNumber);
  1637. }
  1638. }
  1639. static void cameraCaptureSessionCaptureStartedCallback (JNIEnv*, jobject /*object*/, jlong host, jboolean isPreview, jobject rawSession, jobject rawRequest, jlong timestamp, jlong frameNumber)
  1640. {
  1641. if (auto* myself = reinterpret_cast<StillPictureTaker*> (host))
  1642. {
  1643. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1644. LocalRef<jobject> request (getEnv()->NewLocalRef(rawRequest));
  1645. myself->cameraCaptureSessionCaptureStarted (isPreview != 0, session, request, timestamp, frameNumber);
  1646. }
  1647. }
  1648. };
  1649. //==============================================================================
  1650. ScopedCameraDevice& scopedCameraDevice;
  1651. ConfiguredCallback& configuredCallback;
  1652. GlobalRef& handler;
  1653. GlobalRef captureRequestBuilder;
  1654. GlobalRef previewCaptureRequest;
  1655. GlobalRef captureSessionStateCallback;
  1656. int autoFocusMode;
  1657. GlobalRef captureSession;
  1658. CriticalSection captureSessionLock;
  1659. Atomic<int> pendingClose { 0 };
  1660. std::unique_ptr<StillPictureTaker> stillPictureTaker;
  1661. WaitableEvent closedEvent;
  1662. JUCE_DECLARE_WEAK_REFERENCEABLE (CaptureSession)
  1663. //==============================================================================
  1664. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  1665. METHOD (constructor, "<init>", "(J)V") \
  1666. CALLBACK(cameraCaptureSessionActiveCallback, "cameraCaptureSessionActive", "(JLandroid/hardware/camera2/CameraCaptureSession;)V") \
  1667. CALLBACK(cameraCaptureSessionClosedCallback, "cameraCaptureSessionClosed", "(JLandroid/hardware/camera2/CameraCaptureSession;)V") \
  1668. CALLBACK(cameraCaptureSessionConfigureFailedCallback, "cameraCaptureSessionConfigureFailed", "(JLandroid/hardware/camera2/CameraCaptureSession;)V") \
  1669. CALLBACK(cameraCaptureSessionConfiguredCallback, "cameraCaptureSessionConfigured", "(JLandroid/hardware/camera2/CameraCaptureSession;)V") \
  1670. CALLBACK(cameraCaptureSessionReadyCallback, "cameraCaptureSessionReady", "(JLandroid/hardware/camera2/CameraCaptureSession;)V")
  1671. DECLARE_JNI_CLASS_WITH_MIN_SDK (CameraCaptureSessionStateCallback, "com/roli/juce/CameraCaptureSessionStateCallback", 21)
  1672. #undef JNI_CLASS_MEMBERS
  1673. //==============================================================================
  1674. CaptureSession (ScopedCameraDevice& scopedCameraDeviceToUse, ConfiguredCallback& configuredCallbackToUse,
  1675. const LocalRef<jobject>& surfacesList, GlobalRef& handlerToUse,
  1676. int captureSessionTemplate, int autoFocusModeToUse)
  1677. : scopedCameraDevice (scopedCameraDeviceToUse),
  1678. configuredCallback (configuredCallbackToUse),
  1679. handler (handlerToUse),
  1680. captureRequestBuilder (LocalRef<jobject> (getEnv()->CallObjectMethod (scopedCameraDevice.cameraDevice,
  1681. AndroidCameraDevice.createCaptureRequest,
  1682. (jint) captureSessionTemplate))),
  1683. captureSessionStateCallback (LocalRef<jobject> (getEnv()->NewObject (CameraCaptureSessionStateCallback,
  1684. CameraCaptureSessionStateCallback.constructor,
  1685. reinterpret_cast<jlong> (this)))),
  1686. autoFocusMode (autoFocusModeToUse)
  1687. {
  1688. auto* env = getEnv();
  1689. env->CallVoidMethod (scopedCameraDevice.cameraDevice, AndroidCameraDevice.createCaptureSession,
  1690. surfacesList.get(), captureSessionStateCallback.get(), handler.get());
  1691. static constexpr int controlModeAuto = 1;
  1692. setCaptureRequestBuilderIntegerKey (captureRequestBuilder.get(), CaptureRequest.CONTROL_MODE, controlModeAuto);
  1693. setCaptureRequestBuilderIntegerKey (captureRequestBuilder.get(), CaptureRequest.CONTROL_AF_MODE, autoFocusMode);
  1694. }
  1695. static void setCaptureRequestBuilderIntegerKey (jobject captureRequestBuilder, jfieldID key, int value)
  1696. {
  1697. auto* env = getEnv();
  1698. auto jKey = LocalRef<jobject> (env->GetStaticObjectField (CaptureRequest, key));
  1699. auto jValue = LocalRef<jobject> (env->CallStaticObjectMethod (JavaInteger, JavaInteger.valueOf, (jint) value));
  1700. env->CallVoidMethod (captureRequestBuilder, CaptureRequestBuilder.set, jKey.get(), jValue.get());
  1701. }
  1702. void cameraCaptureSessionActive (jobject session)
  1703. {
  1704. JUCE_CAMERA_LOG ("cameraCaptureSessionActive()");
  1705. ignoreUnused (session);
  1706. }
  1707. void cameraCaptureSessionClosed (jobject session)
  1708. {
  1709. JUCE_CAMERA_LOG ("cameraCaptureSessionClosed()");
  1710. ignoreUnused (session);
  1711. closedEvent.signal();
  1712. }
  1713. void cameraCaptureSessionConfigureFailed (jobject session)
  1714. {
  1715. JUCE_CAMERA_LOG ("cameraCaptureSessionConfigureFailed()");
  1716. ignoreUnused (session);
  1717. WeakReference<CaptureSession> weakRef (this);
  1718. MessageManager::callAsync ([this, weakRef]()
  1719. {
  1720. if (weakRef == nullptr)
  1721. return;
  1722. configuredCallback.captureSessionConfigured (nullptr);
  1723. });
  1724. }
  1725. void cameraCaptureSessionConfigured (const LocalRef<jobject>& session)
  1726. {
  1727. JUCE_CAMERA_LOG ("cameraCaptureSessionConfigured()");
  1728. if (pendingClose.get() == 1)
  1729. {
  1730. // Already closing, bailout.
  1731. closedEvent.signal();
  1732. GlobalRef s (session);
  1733. MessageManager::callAsync ([s]()
  1734. {
  1735. getEnv()->CallVoidMethod (s, CameraCaptureSession.close);
  1736. });
  1737. return;
  1738. }
  1739. {
  1740. const ScopedLock lock (captureSessionLock);
  1741. captureSession = GlobalRef (session);
  1742. }
  1743. WeakReference<CaptureSession> weakRef (this);
  1744. MessageManager::callAsync ([this, weakRef]()
  1745. {
  1746. if (weakRef == nullptr)
  1747. return;
  1748. stillPictureTaker.reset (new StillPictureTaker (captureSession, captureRequestBuilder,
  1749. previewCaptureRequest, handler, autoFocusMode));
  1750. configuredCallback.captureSessionConfigured (this);
  1751. });
  1752. }
  1753. void cameraCaptureSessionReady (const LocalRef<jobject>& session)
  1754. {
  1755. JUCE_CAMERA_LOG ("cameraCaptureSessionReady()");
  1756. ignoreUnused (session);
  1757. }
  1758. //==============================================================================
  1759. static void cameraCaptureSessionActiveCallback (JNIEnv*, jobject, jlong host, jobject rawSession)
  1760. {
  1761. if (auto* myself = reinterpret_cast<CaptureSession*> (host))
  1762. {
  1763. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1764. myself->cameraCaptureSessionActive (session);
  1765. }
  1766. }
  1767. static void cameraCaptureSessionClosedCallback (JNIEnv*, jobject, jlong host, jobject rawSession)
  1768. {
  1769. if (auto* myself = reinterpret_cast<CaptureSession*> (host))
  1770. {
  1771. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1772. myself->cameraCaptureSessionClosed (session);
  1773. }
  1774. }
  1775. static void cameraCaptureSessionConfigureFailedCallback (JNIEnv*, jobject, jlong host, jobject rawSession)
  1776. {
  1777. if (auto* myself = reinterpret_cast<CaptureSession*> (host))
  1778. {
  1779. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1780. myself->cameraCaptureSessionConfigureFailed (session);
  1781. }
  1782. }
  1783. static void cameraCaptureSessionConfiguredCallback (JNIEnv*, jobject, jlong host, jobject rawSession)
  1784. {
  1785. if (auto* myself = reinterpret_cast<CaptureSession*> (host))
  1786. {
  1787. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1788. myself->cameraCaptureSessionConfigured (session);
  1789. }
  1790. }
  1791. static void cameraCaptureSessionReadyCallback (JNIEnv*, jobject, jlong host, jobject rawSession)
  1792. {
  1793. if (auto* myself = reinterpret_cast<CaptureSession*> (host))
  1794. {
  1795. LocalRef<jobject> session (getEnv()->NewLocalRef(rawSession));
  1796. myself->cameraCaptureSessionReady (session);
  1797. }
  1798. }
  1799. //==============================================================================
  1800. friend class ScopedCameraDevice;
  1801. JUCE_DECLARE_NON_COPYABLE (CaptureSession)
  1802. };
  1803. //==============================================================================
  1804. ScopedCameraDevice (Pimpl& ownerToUse, const String& cameraIdToUse, GlobalRef& cameraManagerToUse,
  1805. GlobalRef& handlerToUse, int autoFocusModeToUse)
  1806. : owner (ownerToUse),
  1807. cameraId (cameraIdToUse),
  1808. cameraManager (cameraManagerToUse),
  1809. handler (handlerToUse),
  1810. cameraStateCallback (createCameraStateCallbackObject()),
  1811. autoFocusMode (autoFocusModeToUse)
  1812. {
  1813. open();
  1814. }
  1815. ~ScopedCameraDevice()
  1816. {
  1817. close();
  1818. }
  1819. void open()
  1820. {
  1821. pendingOpen.set (1);
  1822. auto* env = getEnv();
  1823. env->CallVoidMethod (cameraManager, CameraManager.openCamera,
  1824. javaString (cameraId).get(),
  1825. cameraStateCallback.get(), handler.get());
  1826. // If something went wrong we will be pinged in cameraDeviceStateError()
  1827. // callback, silence the redundant exception.
  1828. jniCheckHasExceptionOccurredAndClear();
  1829. }
  1830. void close()
  1831. {
  1832. if (pendingClose.compareAndSetBool (1, 0))
  1833. {
  1834. auto* env = getEnv();
  1835. if (cameraDevice.get() != nullptr)
  1836. {
  1837. env->CallVoidMethod (cameraDevice, AndroidCameraDevice.close);
  1838. closedEvent.wait (-1);
  1839. }
  1840. pendingClose.set (0);
  1841. pendingOpen .set (0);
  1842. cameraDevice.clear();
  1843. }
  1844. }
  1845. bool openedOk() const { return cameraDevice != nullptr; }
  1846. bool hasErrorOccurred() const { return fatalErrorOccurred.get(); }
  1847. CaptureSession* createCaptureSession (CaptureSession::ConfiguredCallback& cc,
  1848. const LocalRef<jobject>& surfacesList,
  1849. GlobalRef& handlerToUse,
  1850. int captureSessionTemplate)
  1851. {
  1852. if (! openedOk())
  1853. {
  1854. jassertfalse;
  1855. return nullptr;
  1856. }
  1857. return new CaptureSession (*this, cc, surfacesList, handlerToUse, captureSessionTemplate, autoFocusMode);
  1858. }
  1859. private:
  1860. Pimpl& owner;
  1861. const String cameraId;
  1862. GlobalRef& cameraManager;
  1863. GlobalRef& handler;
  1864. GlobalRef cameraStateCallback;
  1865. int autoFocusMode;
  1866. GlobalRef cameraDevice;
  1867. Atomic<int> pendingOpen { 0 };
  1868. Atomic<int> pendingClose { 0 };
  1869. Atomic<int> fatalErrorOccurred { 0 };
  1870. String openError;
  1871. WaitableEvent closedEvent;
  1872. //==============================================================================
  1873. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  1874. METHOD (constructor, "<init>", "(J)V") \
  1875. CALLBACK (cameraDeviceStateClosedCallback, "cameraDeviceStateClosed", "(JLandroid/hardware/camera2/CameraDevice;)V") \
  1876. CALLBACK (cameraDeviceStateDisconnectedCallback, "cameraDeviceStateDisconnected", "(JLandroid/hardware/camera2/CameraDevice;)V") \
  1877. CALLBACK (cameraDeviceStateErrorCallback, "cameraDeviceStateError", "(JLandroid/hardware/camera2/CameraDevice;I)V") \
  1878. CALLBACK (cameraDeviceStateOpenedCallback, "cameraDeviceStateOpened", "(JLandroid/hardware/camera2/CameraDevice;)V")
  1879. DECLARE_JNI_CLASS_WITH_MIN_SDK (CameraDeviceStateCallback, "com/roli/juce/CameraDeviceStateCallback", 21)
  1880. #undef JNI_CLASS_MEMBERS
  1881. LocalRef<jobject> createCameraStateCallbackObject()
  1882. {
  1883. return LocalRef<jobject> (getEnv()->NewObject (CameraDeviceStateCallback,
  1884. CameraDeviceStateCallback.constructor,
  1885. reinterpret_cast<jlong> (this)));
  1886. }
  1887. //==============================================================================
  1888. void cameraDeviceStateClosed()
  1889. {
  1890. JUCE_CAMERA_LOG ("cameraDeviceStateClosed()");
  1891. closedEvent.signal();
  1892. }
  1893. void cameraDeviceStateDisconnected()
  1894. {
  1895. JUCE_CAMERA_LOG ("cameraDeviceStateDisconnected()");
  1896. if (pendingOpen.compareAndSetBool (0, 1))
  1897. {
  1898. openError = "Device disconnected";
  1899. notifyOpenResult();
  1900. }
  1901. MessageManager::callAsync ([this]() { close(); });
  1902. }
  1903. void cameraDeviceStateError (int errorCode)
  1904. {
  1905. String error = cameraErrorCodeToString (errorCode);
  1906. JUCE_CAMERA_LOG ("cameraDeviceStateError(), error: " + error);
  1907. if (pendingOpen.compareAndSetBool (0, 1))
  1908. {
  1909. openError = error;
  1910. notifyOpenResult();
  1911. }
  1912. fatalErrorOccurred.set (1);
  1913. MessageManager::callAsync ([this, error]()
  1914. {
  1915. owner.cameraDeviceError (error);
  1916. close();
  1917. });
  1918. }
  1919. void cameraDeviceStateOpened (const LocalRef<jobject>& cameraDeviceToUse)
  1920. {
  1921. JUCE_CAMERA_LOG ("cameraDeviceStateOpened()");
  1922. pendingOpen.set (0);
  1923. cameraDevice = GlobalRef (cameraDeviceToUse);
  1924. notifyOpenResult();
  1925. }
  1926. void notifyOpenResult()
  1927. {
  1928. MessageManager::callAsync ([this]() { owner.cameraOpenFinished (openError); });
  1929. }
  1930. //==============================================================================
  1931. static void JNICALL cameraDeviceStateClosedCallback (JNIEnv*, jobject, jlong host, jobject)
  1932. {
  1933. if (auto* myself = reinterpret_cast<ScopedCameraDevice*>(host))
  1934. myself->cameraDeviceStateClosed();
  1935. }
  1936. static void JNICALL cameraDeviceStateDisconnectedCallback (JNIEnv*, jobject, jlong host, jobject)
  1937. {
  1938. if (auto* myself = reinterpret_cast<ScopedCameraDevice*>(host))
  1939. myself->cameraDeviceStateDisconnected();
  1940. }
  1941. static void JNICALL cameraDeviceStateErrorCallback (JNIEnv*, jobject, jlong host, jobject, jint error)
  1942. {
  1943. if (auto* myself = reinterpret_cast<ScopedCameraDevice*>(host))
  1944. myself->cameraDeviceStateError (error);
  1945. }
  1946. static void JNICALL cameraDeviceStateOpenedCallback (JNIEnv*, jobject, jlong host, jobject rawCamera)
  1947. {
  1948. if (auto* myself = reinterpret_cast<ScopedCameraDevice*>(host))
  1949. {
  1950. LocalRef<jobject> camera(getEnv()->NewLocalRef(rawCamera));
  1951. myself->cameraDeviceStateOpened (camera);
  1952. }
  1953. }
  1954. };
  1955. //==============================================================================
  1956. struct CaptureSessionModeBase
  1957. {
  1958. virtual ~CaptureSessionModeBase() { }
  1959. virtual bool isVideoRecordSession() const = 0;
  1960. virtual void triggerStillPictureCapture() = 0;
  1961. };
  1962. //==============================================================================
  1963. template <typename Mode>
  1964. struct CaptureSessionMode : public CaptureSessionModeBase,
  1965. private PreviewDisplay::Listener,
  1966. private ScopedCameraDevice::CaptureSession::ConfiguredCallback
  1967. {
  1968. ~CaptureSessionMode()
  1969. {
  1970. captureSession.reset();
  1971. previewDisplay.removeListener (this);
  1972. }
  1973. bool isVideoRecordSession() const override
  1974. {
  1975. return Mode::isVideoRecord();
  1976. }
  1977. void triggerStillPictureCapture() override
  1978. {
  1979. if (captureSession == nullptr)
  1980. {
  1981. // The capture session must be ready before taking a still picture.
  1982. // Did you remember to create and show a preview display?
  1983. jassertfalse;
  1984. return;
  1985. }
  1986. crtp().takeStillPicture();
  1987. }
  1988. protected:
  1989. CaptureSessionMode (Pimpl& ownerToUse, ScopedCameraDevice& cameraDeviceToUse,
  1990. GlobalRef& handlerToUse, PreviewDisplay& pd, int cameraSensorOrientationToUse,
  1991. int cameraLensFacingToUse, StreamConfigurationMap& streamConfigurationMapToUse)
  1992. : owner (ownerToUse),
  1993. scopedCameraDevice (cameraDeviceToUse),
  1994. handler (handlerToUse),
  1995. previewDisplay (pd),
  1996. cameraSensorOrientation (cameraSensorOrientationToUse),
  1997. cameraLensFacing (cameraLensFacingToUse),
  1998. streamConfigurationMap (streamConfigurationMapToUse)
  1999. {
  2000. WeakReference<CaptureSessionMode<Mode>> weakRef (this);
  2001. if (weakRef == nullptr)
  2002. return;
  2003. // async so that the object is fully constructed before the callback gets invoked
  2004. MessageManager::callAsync ([this, weakRef]()
  2005. {
  2006. if (weakRef == nullptr)
  2007. return;
  2008. previewDisplay.addListener (this);
  2009. });
  2010. }
  2011. Mode& crtp() { return static_cast<Mode&> (*this); }
  2012. void previewDisplayReady() override
  2013. {
  2014. jassert (previewDisplay.isReady());
  2015. JUCE_CAMERA_LOG ("previewDisplayReady()");
  2016. // close previous capture session first
  2017. captureSession.reset();
  2018. if (scopedCameraDevice.hasErrorOccurred())
  2019. {
  2020. JUCE_CAMERA_LOG ("Device error detected, not recreating a new camera session. The device needs to be reopened.");
  2021. return;
  2022. }
  2023. captureSession.reset (scopedCameraDevice.createCaptureSession (*this, crtp().getCaptureSessionSurfaces(),
  2024. handler, Mode::getTemplate()));
  2025. }
  2026. void previewDisplayAboutToBeDestroyed() override
  2027. {
  2028. JUCE_CAMERA_LOG ("previewDisplayAboutToBeDestroyed()");
  2029. stopPreview();
  2030. }
  2031. void captureSessionConfigured (ScopedCameraDevice::CaptureSession* session) override
  2032. {
  2033. if (session == nullptr)
  2034. {
  2035. owner.cameraDeviceError ("Failed to configure camera session.");
  2036. return;
  2037. }
  2038. jassert (session == captureSession.get());
  2039. startSession();
  2040. }
  2041. void startSession()
  2042. {
  2043. if (! captureSession->start (crtp().getTargetSurfaces(), handler))
  2044. {
  2045. jassertfalse;
  2046. JUCE_CAMERA_LOG ("Could not start capture session");
  2047. }
  2048. crtp().sessionStarted();
  2049. }
  2050. void stopPreview()
  2051. {
  2052. if (captureSession != nullptr)
  2053. {
  2054. auto session = captureSession->getNativeSession();
  2055. auto* env = getEnv();
  2056. env->CallVoidMethod (session, CameraCaptureSession.stopRepeating);
  2057. if (jniCheckHasExceptionOccurredAndClear())
  2058. return;
  2059. env->CallVoidMethod (session, CameraCaptureSession.abortCaptures);
  2060. jniCheckHasExceptionOccurredAndClear();
  2061. }
  2062. }
  2063. Pimpl& owner;
  2064. ScopedCameraDevice& scopedCameraDevice;
  2065. GlobalRef& handler;
  2066. PreviewDisplay& previewDisplay;
  2067. int cameraSensorOrientation;
  2068. int cameraLensFacing;
  2069. StreamConfigurationMap& streamConfigurationMap;
  2070. std::unique_ptr<ScopedCameraDevice::CaptureSession> captureSession;
  2071. JUCE_DECLARE_WEAK_REFERENCEABLE (CaptureSessionMode<Mode>)
  2072. };
  2073. //==============================================================================
  2074. struct CaptureSessionPreviewMode : public CaptureSessionMode<CaptureSessionPreviewMode>
  2075. {
  2076. CaptureSessionPreviewMode (Pimpl& ownerToUse, ScopedCameraDevice& cameraDeviceToUse, GlobalRef& handlerToUse,
  2077. PreviewDisplay& pd, ImageReader& ir, int cameraSensorOrientation,
  2078. int cameraLensFacingToUse, StreamConfigurationMap& streamConfigurationMapToUse)
  2079. : CaptureSessionMode<CaptureSessionPreviewMode> (ownerToUse, cameraDeviceToUse, handlerToUse, pd,
  2080. cameraSensorOrientation, cameraLensFacingToUse, streamConfigurationMapToUse),
  2081. imageReader (ir)
  2082. {
  2083. }
  2084. // Surfaces passed to newly created capture session.
  2085. LocalRef<jobject> getCaptureSessionSurfaces() const
  2086. {
  2087. auto* env = getEnv();
  2088. auto previewSurface = LocalRef<jobject> (previewDisplay.createSurface());
  2089. auto imageSurface = LocalRef<jobject> (imageReader.getSurface());
  2090. auto arrayList = LocalRef<jobject> (env->NewObject (JavaArrayList, JavaArrayList.constructor, 2));
  2091. env->CallBooleanMethod (arrayList, JavaArrayList.add, previewSurface.get());
  2092. env->CallBooleanMethod (arrayList, JavaArrayList.add, imageSurface.get());
  2093. auto supported = streamConfigurationMap.isOutputSupportedForSurface (imageSurface);
  2094. // Output surface is not supported by this device, still image capture will not work!
  2095. jassert (supported);
  2096. return arrayList;
  2097. }
  2098. // Surfaces set as target during capture.
  2099. LocalRef<jobject> getTargetSurfaces() const
  2100. {
  2101. auto* env = getEnv();
  2102. auto previewSurface = LocalRef<jobject> (previewDisplay.createSurface());
  2103. auto arrayList = LocalRef<jobject> (env->NewObject (JavaArrayList, JavaArrayList.constructor, 1));
  2104. env->CallBooleanMethod (arrayList, JavaArrayList.add, previewSurface.get());
  2105. return arrayList;
  2106. }
  2107. static int getTemplate()
  2108. {
  2109. static constexpr int templatePreview = 1;
  2110. return templatePreview;
  2111. }
  2112. static bool isVideoRecord() { return false; }
  2113. void sessionStarted() {}
  2114. void takeStillPicture()
  2115. {
  2116. imageReader.resetNotificationFlag();
  2117. captureSession->takeStillPicture (imageReader.getSurface());
  2118. }
  2119. private:
  2120. ImageReader& imageReader;
  2121. };
  2122. //==============================================================================
  2123. struct CaptureSessionVideoRecordingMode : public CaptureSessionMode<CaptureSessionVideoRecordingMode>
  2124. {
  2125. CaptureSessionVideoRecordingMode (Pimpl& ownerToUse, ScopedCameraDevice& cameraDeviceToUse, GlobalRef& handlerToUse,
  2126. PreviewDisplay& pd, MediaRecorder& mr, int cameraSensorOrientation,
  2127. int cameraLensFacingToUse, StreamConfigurationMap& streamConfigurationMapToUse)
  2128. : CaptureSessionMode<CaptureSessionVideoRecordingMode> (ownerToUse, cameraDeviceToUse, handlerToUse, pd,
  2129. cameraSensorOrientation, cameraLensFacingToUse, streamConfigurationMapToUse),
  2130. mediaRecorder (mr)
  2131. {
  2132. }
  2133. ~CaptureSessionVideoRecordingMode()
  2134. {
  2135. // We need to explicitly stop the preview before stopping the media recorder,
  2136. // because legacy devices can't handle recording stop before stopping the preview.
  2137. stopPreview();
  2138. mediaRecorder.stop();
  2139. }
  2140. // Surfaces passed to newly created capture session.
  2141. LocalRef<jobject> getCaptureSessionSurfaces() const
  2142. {
  2143. auto* env = getEnv();
  2144. auto previewSurface = LocalRef<jobject> (previewDisplay.createSurface());
  2145. auto mediaRecorderSurface = LocalRef<jobject> (mediaRecorder.getSurface());
  2146. auto arrayList = LocalRef<jobject> (env->NewObject (JavaArrayList, JavaArrayList.constructor, 2));
  2147. env->CallBooleanMethod (arrayList, JavaArrayList.add, previewSurface.get());
  2148. env->CallBooleanMethod (arrayList, JavaArrayList.add, mediaRecorderSurface.get());
  2149. return arrayList;
  2150. }
  2151. // Surfaces set as target during capture.
  2152. LocalRef<jobject> getTargetSurfaces() const
  2153. {
  2154. // Same surfaces used.
  2155. return getCaptureSessionSurfaces();
  2156. }
  2157. static int getTemplate()
  2158. {
  2159. static constexpr int templateRecord = 3;
  2160. return templateRecord;
  2161. }
  2162. static bool isVideoRecord() { return true; }
  2163. void sessionStarted()
  2164. {
  2165. MessageManager::callAsync ([this]() { mediaRecorder.start(); });
  2166. }
  2167. void takeStillPicture()
  2168. {
  2169. // Taking still pictures while recording video is not supported on Android.
  2170. jassertfalse;
  2171. }
  2172. private:
  2173. MediaRecorder& mediaRecorder;
  2174. };
  2175. //==============================================================================
  2176. class DeviceOrientationChangeListener : private Timer
  2177. {
  2178. public:
  2179. DeviceOrientationChangeListener (PreviewDisplay& pd)
  2180. : previewDisplay (pd),
  2181. orientationEventListener (createOrientationEventListener()),
  2182. canDetectChange (getEnv()->CallBooleanMethod (orientationEventListener,
  2183. OrientationEventListener.canDetectOrientation) != 0),
  2184. deviceOrientation (Desktop::getInstance().getCurrentOrientation()),
  2185. lastKnownScreenOrientation (deviceOrientation)
  2186. {
  2187. setEnabled (true);
  2188. }
  2189. ~DeviceOrientationChangeListener()
  2190. {
  2191. setEnabled (false);
  2192. }
  2193. void setEnabled (bool shouldBeEnabled)
  2194. {
  2195. if (shouldBeEnabled && ! canDetectChange)
  2196. {
  2197. // This device does not support orientation listening, photos may have wrong orientation!
  2198. jassertfalse;
  2199. return;
  2200. }
  2201. if (shouldBeEnabled)
  2202. getEnv()->CallVoidMethod (orientationEventListener, OrientationEventListener.enable);
  2203. else
  2204. getEnv()->CallVoidMethod (orientationEventListener, OrientationEventListener.disable);
  2205. }
  2206. bool isSupported() const noexcept { return canDetectChange; }
  2207. Desktop::DisplayOrientation getDeviceOrientation() const noexcept
  2208. {
  2209. return deviceOrientation;
  2210. }
  2211. private:
  2212. PreviewDisplay& previewDisplay;
  2213. GlobalRef orientationEventListener;
  2214. static constexpr jint sensorDelayUI = 2;
  2215. bool canDetectChange;
  2216. Desktop::DisplayOrientation deviceOrientation;
  2217. Desktop::DisplayOrientation lastKnownScreenOrientation;
  2218. int numChecksForOrientationChange = 10;
  2219. //==============================================================================
  2220. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  2221. METHOD (canDetectOrientation, "canDetectOrientation", "()Z") \
  2222. METHOD (constructor, "<init>", "(JLandroid/content/Context;I)V") \
  2223. METHOD (disable, "disable", "()V") \
  2224. METHOD (enable, "enable", "()V") \
  2225. CALLBACK (deviceOrientationChanged, "deviceOrientationChanged", "(JI)V")
  2226. DECLARE_JNI_CLASS_WITH_MIN_SDK (OrientationEventListener, "com/roli/juce/JuceOrientationEventListener", 21)
  2227. #undef JNI_CLASS_MEMBERS
  2228. LocalRef<jobject> createOrientationEventListener()
  2229. {
  2230. return LocalRef<jobject> (getEnv()->NewObject (OrientationEventListener,
  2231. OrientationEventListener.constructor,
  2232. reinterpret_cast<jlong> (this),
  2233. getAppContext().get(),
  2234. sensorDelayUI));
  2235. }
  2236. //==============================================================================
  2237. void orientationChanged (int orientation)
  2238. {
  2239. jassert (orientation < 360);
  2240. // -1 == unknown
  2241. if (orientation < 0)
  2242. return;
  2243. auto oldOrientation = deviceOrientation;
  2244. // NB: this assumes natural position to be portrait always, but some devices may be landscape...
  2245. if (orientation > (360 - 45) || orientation < 45)
  2246. deviceOrientation = Desktop::upright;
  2247. else if (orientation < 135)
  2248. deviceOrientation = Desktop::rotatedClockwise;
  2249. else if (orientation < 225)
  2250. deviceOrientation = Desktop::upsideDown;
  2251. else
  2252. deviceOrientation = Desktop::rotatedAntiClockwise;
  2253. if (oldOrientation != deviceOrientation)
  2254. {
  2255. lastKnownScreenOrientation = Desktop::getInstance().getCurrentOrientation();
  2256. // Need to update preview transform, but screen orientation will change slightly
  2257. // later than sensor orientation.
  2258. startTimer (500);
  2259. }
  2260. }
  2261. void timerCallback() override
  2262. {
  2263. auto currentOrientation = Desktop::getInstance().getCurrentOrientation();
  2264. if (lastKnownScreenOrientation != currentOrientation)
  2265. {
  2266. lastKnownScreenOrientation = currentOrientation;
  2267. stopTimer();
  2268. numChecksForOrientationChange = 10;
  2269. previewDisplay.updateSurfaceTransform();
  2270. return;
  2271. }
  2272. if (--numChecksForOrientationChange == 0)
  2273. {
  2274. stopTimer();
  2275. numChecksForOrientationChange = 10;
  2276. }
  2277. }
  2278. static void deviceOrientationChanged (JNIEnv*, jobject /*obj*/, jlong host, jint orientation)
  2279. {
  2280. if (auto* myself = reinterpret_cast<DeviceOrientationChangeListener*> (host))
  2281. myself->orientationChanged (orientation);
  2282. }
  2283. };
  2284. //==============================================================================
  2285. CameraDevice& owner;
  2286. int minWidth, minHeight, maxWidth, maxHeight;
  2287. String cameraId;
  2288. InternalOpenCameraResultCallback cameraOpenCallback;
  2289. GlobalRef activityLifeListener;
  2290. GlobalRef cameraManager;
  2291. GlobalRef cameraCharacteristics;
  2292. GlobalRef handlerThread;
  2293. GlobalRef handler;
  2294. StreamConfigurationMap streamConfigurationMap;
  2295. PreviewDisplay previewDisplay;
  2296. DeviceOrientationChangeListener deviceOrientationChangeListener;
  2297. std::unique_ptr<ImageReader> imageReader;
  2298. std::unique_ptr<MediaRecorder> mediaRecorder;
  2299. std::unique_ptr<CaptureSessionModeBase> currentCaptureSessionMode;
  2300. std::unique_ptr<ScopedCameraDevice> scopedCameraDevice;
  2301. CriticalSection listenerLock;
  2302. ListenerList<Listener> listeners;
  2303. std::function<void (const Image&)> pictureTakenCallback;
  2304. Time firstRecordedFrameTimeMs;
  2305. bool notifiedOfCameraOpening = false;
  2306. bool appWasPaused = false;
  2307. //==============================================================================
  2308. int getCameraSensorOrientation() const
  2309. {
  2310. return getCameraCharacteristicsIntegerKeyValue (CameraCharacteristics.SENSOR_ORIENTATION);
  2311. }
  2312. int getAutoFocusModeToUse() const
  2313. {
  2314. auto supportedModes = getSupportedAutoFocusModes();
  2315. enum
  2316. {
  2317. CONTROL_AF_MODE_OFF = 0,
  2318. CONTROL_AF_MODE_AUTO = 1,
  2319. CONTROL_AF_MODE_CONTINUOUS_PICTURE = 4
  2320. };
  2321. if (supportedModes.contains (CONTROL_AF_MODE_CONTINUOUS_PICTURE))
  2322. return CONTROL_AF_MODE_CONTINUOUS_PICTURE;
  2323. if (supportedModes.contains (CONTROL_AF_MODE_AUTO))
  2324. return CONTROL_AF_MODE_AUTO;
  2325. return CONTROL_AF_MODE_OFF;
  2326. }
  2327. Array<int> getSupportedAutoFocusModes() const
  2328. {
  2329. auto* env = getEnv();
  2330. auto jKey = LocalRef<jobject> (env->GetStaticObjectField (CameraCharacteristics, CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES));
  2331. auto supportedModes = LocalRef<jintArray> ((jintArray) env->CallObjectMethod (cameraCharacteristics,
  2332. CameraCharacteristics.get,
  2333. jKey.get()));
  2334. return jintArrayToJuceArray (supportedModes);
  2335. }
  2336. static Array<int> jintArrayToJuceArray (const LocalRef<jintArray>& jArray)
  2337. {
  2338. auto* env = getEnv();
  2339. auto* jArrayElems = env->GetIntArrayElements (jArray, 0);
  2340. auto numElems = env->GetArrayLength (jArray);
  2341. Array<int> juceArray;
  2342. for (int s = 0; s < numElems; ++s)
  2343. juceArray.add (jArrayElems[s]);
  2344. env->ReleaseIntArrayElements (jArray, jArrayElems, 0);
  2345. return juceArray;
  2346. }
  2347. int getCameraCharacteristicsIntegerKeyValue (jfieldID key) const
  2348. {
  2349. auto* env = getEnv();
  2350. auto jKey = LocalRef<jobject> (env->GetStaticObjectField (CameraCharacteristics, key));
  2351. auto jValue = LocalRef<jobject> (env->CallObjectMethod (cameraCharacteristics,
  2352. CameraCharacteristics.get,
  2353. jKey.get()));
  2354. return env->CallIntMethod (jValue, JavaInteger.intValue);
  2355. }
  2356. int getCameraLensFacing() const
  2357. {
  2358. return getCameraCharacteristicsIntegerKeyValue (CameraCharacteristics.LENS_FACING);
  2359. }
  2360. //==============================================================================
  2361. void cameraOpenFinished (const String& error)
  2362. {
  2363. JUCE_CAMERA_LOG ("cameraOpenFinished(), error = " + error);
  2364. if (error.isEmpty())
  2365. {
  2366. setupStillImageSize();
  2367. startPreviewMode (*imageReader);
  2368. }
  2369. // Do not notify about camera being reopened on app resume.
  2370. if (! notifiedOfCameraOpening)
  2371. {
  2372. notifiedOfCameraOpening = true;
  2373. invokeCameraOpenCallback (error);
  2374. }
  2375. }
  2376. void cameraDeviceError (const String& error)
  2377. {
  2378. if (owner.onErrorOccurred != nullptr)
  2379. owner.onErrorOccurred (error);
  2380. }
  2381. void invokeCameraOpenCallback (const String& error)
  2382. {
  2383. JUCE_CAMERA_LOG ("invokeCameraOpenCallback(), error = " + error);
  2384. if (cameraOpenCallback != nullptr)
  2385. cameraOpenCallback (cameraId, error);
  2386. }
  2387. //==============================================================================
  2388. void callListeners (const Image& image)
  2389. {
  2390. const ScopedLock sl (listenerLock);
  2391. listeners.call ([=] (Listener& l) { l.imageReceived (image); });
  2392. }
  2393. void notifyPictureTaken (const Image& image)
  2394. {
  2395. JUCE_CAMERA_LOG ("notifyPictureTaken()");
  2396. if (pictureTakenCallback != nullptr)
  2397. pictureTakenCallback (image);
  2398. }
  2399. void triggerStillPictureCapture()
  2400. {
  2401. currentCaptureSessionMode->triggerStillPictureCapture();
  2402. }
  2403. //==============================================================================
  2404. void setupStillImageSize()
  2405. {
  2406. imageReader.reset();
  2407. auto imageSize = chooseBestSize (minWidth, minHeight, maxWidth, maxHeight,
  2408. streamConfigurationMap.getSupportedStillImageOutputSizes());
  2409. imageReader.reset (new ImageReader (*this, handler, imageSize.getWidth(), imageSize.getHeight(),
  2410. getCameraSensorOrientation()));
  2411. }
  2412. static Rectangle<int> chooseBestSize (int minWidth, int minHeight, int maxWidth, int maxHeight,
  2413. Array<Rectangle<int>> supportedSizes)
  2414. {
  2415. Rectangle<int> result;
  2416. for (auto& size : supportedSizes)
  2417. {
  2418. auto width = size.getWidth();
  2419. auto height = size.getHeight();
  2420. if (width < minWidth || width > maxWidth || height < minHeight || height > maxHeight)
  2421. continue;
  2422. if (size.contains (result))
  2423. result = size;
  2424. }
  2425. // None of the supported sizes matches required width & height limitations, picking
  2426. // the first one available...
  2427. jassert (! result.isEmpty());
  2428. if (result.isEmpty())
  2429. result = supportedSizes[0];
  2430. return result;
  2431. }
  2432. //==============================================================================
  2433. void startPreviewMode (ImageReader& ir)
  2434. {
  2435. if (currentCaptureSessionMode != nullptr && ! currentCaptureSessionMode->isVideoRecordSession())
  2436. return;
  2437. // previous mode has to be stopped first
  2438. jassert (currentCaptureSessionMode.get() == nullptr);
  2439. if (scopedCameraDevice == nullptr || ! scopedCameraDevice->openedOk())
  2440. return;
  2441. currentCaptureSessionMode.reset (new CaptureSessionPreviewMode (*this, *scopedCameraDevice, handler,
  2442. previewDisplay, ir,
  2443. getCameraSensorOrientation(),
  2444. getCameraLensFacing(),
  2445. streamConfigurationMap));
  2446. }
  2447. void startVideoRecordingMode (MediaRecorder& mr)
  2448. {
  2449. if (currentCaptureSessionMode != nullptr && currentCaptureSessionMode->isVideoRecordSession())
  2450. return;
  2451. // previous mode has to be stopped first
  2452. jassert (currentCaptureSessionMode.get() == nullptr);
  2453. jassert (scopedCameraDevice != nullptr && scopedCameraDevice->openedOk());
  2454. if (scopedCameraDevice == nullptr || ! scopedCameraDevice->openedOk())
  2455. return;
  2456. currentCaptureSessionMode.reset (new CaptureSessionVideoRecordingMode (*this, *scopedCameraDevice, handler,
  2457. previewDisplay, mr,
  2458. getCameraSensorOrientation(),
  2459. getCameraLensFacing(),
  2460. streamConfigurationMap));
  2461. }
  2462. //==============================================================================
  2463. void onActivityPaused (jobject) override
  2464. {
  2465. JUCE_CAMERA_LOG ("appPaused, closing camera...");
  2466. appWasPaused = true;
  2467. deviceOrientationChangeListener.setEnabled (false);
  2468. // We need to restart the whole session mode when the app gets resumed.
  2469. currentCaptureSessionMode.reset();
  2470. if (scopedCameraDevice != nullptr)
  2471. scopedCameraDevice->close();
  2472. stopBackgroundThread();
  2473. }
  2474. void onActivityResumed (jobject) override
  2475. {
  2476. // Only care about resumed event when paused event was called first.
  2477. if (! appWasPaused)
  2478. return;
  2479. JUCE_CAMERA_LOG ("appResumed, opening camera...");
  2480. deviceOrientationChangeListener.setEnabled (true);
  2481. startBackgroundThread();
  2482. if (scopedCameraDevice != nullptr)
  2483. scopedCameraDevice->open();
  2484. }
  2485. void startBackgroundThread()
  2486. {
  2487. auto* env = getEnv();
  2488. handlerThread = GlobalRef (LocalRef<jobject> (env->NewObject (AndroidHandlerThread,
  2489. AndroidHandlerThread.constructor,
  2490. javaString ("JuceCameraDeviceBackgroundThread").get())));
  2491. // handler thread has to be started before its looper can be fetched
  2492. env->CallVoidMethod (handlerThread, AndroidHandlerThread.start);
  2493. handler = GlobalRef (LocalRef<jobject> (env->NewObject (AndroidHandler,
  2494. AndroidHandler.constructorWithLooper,
  2495. env->CallObjectMethod (handlerThread, AndroidHandlerThread.getLooper))));
  2496. }
  2497. void stopBackgroundThread()
  2498. {
  2499. auto* env = getEnv();
  2500. auto quitSafelyMethod = env->GetMethodID(AndroidHandlerThread, "quitSafely", "()Z");
  2501. // this code will only run on SDK >= 21
  2502. jassert(quitSafelyMethod != nullptr);
  2503. env->CallBooleanMethod (handlerThread, quitSafelyMethod);
  2504. env->CallVoidMethod (handlerThread, AndroidHandlerThread.join);
  2505. jniCheckHasExceptionOccurredAndClear();
  2506. handlerThread.clear();
  2507. handler.clear();
  2508. }
  2509. friend struct CameraDevice::ViewerComponent;
  2510. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  2511. };
  2512. //==============================================================================
  2513. struct CameraDevice::ViewerComponent : public Component,
  2514. private ComponentMovementWatcher
  2515. {
  2516. ViewerComponent (CameraDevice& device) : ComponentMovementWatcher (this)
  2517. {
  2518. auto previewSize = device.pimpl->streamConfigurationMap.getDefaultPreviewSize();
  2519. targetAspectRatio = previewSize.getWidth() / (float) previewSize.getHeight();
  2520. if (isOrientationLandscape())
  2521. setBounds (previewSize);
  2522. else
  2523. setBounds (0, 0, previewSize.getHeight(), previewSize.getWidth());
  2524. addAndMakeVisible (viewerComponent);
  2525. viewerComponent.setView (device.pimpl->previewDisplay.getNativeView());
  2526. }
  2527. private:
  2528. AndroidViewComponent viewerComponent;
  2529. float targetAspectRatio = 1.0f;
  2530. void componentMovedOrResized (bool, bool) override
  2531. {
  2532. auto b = getLocalBounds();
  2533. auto targetWidth = b.getWidth();
  2534. auto targetHeight = b.getHeight();
  2535. if (isOrientationLandscape())
  2536. {
  2537. auto currentAspectRatio = b.getWidth() / (float) b.getHeight();
  2538. if (currentAspectRatio > targetAspectRatio)
  2539. targetWidth = static_cast<int> (targetWidth * targetAspectRatio / currentAspectRatio);
  2540. else
  2541. targetHeight = static_cast<int> (targetHeight * currentAspectRatio / targetAspectRatio);
  2542. }
  2543. else
  2544. {
  2545. auto currentAspectRatio = b.getHeight() / (float) b.getWidth();
  2546. if (currentAspectRatio > targetAspectRatio)
  2547. targetHeight = static_cast<int> (targetHeight * targetAspectRatio / currentAspectRatio);
  2548. else
  2549. targetWidth = static_cast<int> (targetWidth * currentAspectRatio / targetAspectRatio);
  2550. }
  2551. viewerComponent.setBounds (Rectangle<int> (0, 0, targetWidth, targetHeight).withCentre (b.getCentre()));
  2552. }
  2553. bool isOrientationLandscape() const
  2554. {
  2555. auto o = Desktop::getInstance().getCurrentOrientation();
  2556. return o == Desktop::rotatedClockwise || o == Desktop::rotatedAntiClockwise;
  2557. }
  2558. void componentPeerChanged() override {}
  2559. void componentVisibilityChanged() override {}
  2560. JUCE_DECLARE_NON_COPYABLE (ViewerComponent)
  2561. };
  2562. String CameraDevice::getFileExtension()
  2563. {
  2564. return ".mp4";
  2565. }
  2566. //==============================================================================
  2567. CameraDevice::Pimpl::ScopedCameraDevice::CaptureSession::StillPictureTaker::CameraCaptureSessionCaptureCallback_Class CameraDevice::Pimpl::ScopedCameraDevice::CaptureSession::StillPictureTaker::CameraCaptureSessionCaptureCallback;
  2568. CameraDevice::Pimpl::ScopedCameraDevice::CameraDeviceStateCallback_Class CameraDevice::Pimpl::ScopedCameraDevice::CameraDeviceStateCallback;
  2569. CameraDevice::Pimpl::ScopedCameraDevice::CaptureSession::CameraCaptureSessionStateCallback_Class CameraDevice::Pimpl::ScopedCameraDevice::CaptureSession::CameraCaptureSessionStateCallback;
  2570. CameraDevice::Pimpl::DeviceOrientationChangeListener::OrientationEventListener_Class CameraDevice::Pimpl::DeviceOrientationChangeListener::OrientationEventListener;