Ocean
ALiveVideo.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MEDIA_ANDROID_A_LIVE_VIDEO_H
9 #define META_OCEAN_MEDIA_ANDROID_A_LIVE_VIDEO_H
10 
15 
16 #include "ocean/math/AnyCamera.h"
17 
18 #include "ocean/media/LiveVideo.h"
19 
20 #include <jni.h>
21 
22 namespace Ocean
23 {
24 
25 namespace Media
26 {
27 
28 namespace Android
29 {
30 
31 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 24
32 
33 /**
34  * This class implements an live video class for Android.
35  * @ingroup mediaandroid
36  */
37 class OCEAN_MEDIA_A_EXPORT ALiveVideo final :
38  virtual public AMedium,
39  virtual public LiveVideo
40 {
41  friend class ALibrary;
42  friend class ALiveVideoManager;
43 
44  private:
45 
46  /**
47  * Definition of Android image formats that are missing from the NDK
48  * These values are used but they are missing from the NDK header, media/NdkImage.h.
49  * @see https://developer.android.com/reference/kotlin/android/graphics/ImageFormat
50  */
52  {
53  // Android YUV P010 format.
54  AIMAGE_FORMAT_YCBCR_P010 = 54,
55  };
56 
57  public:
58 
59  /**
60  * Returns whether the medium is started currently.
61  * @see Medium::isStarted().
62  */
63  bool isStarted() const override;
64 
65  /**
66  * Returns the start timestamp.
67  * @see FiniteMedium::startTimestamp().
68  */
69  Timestamp startTimestamp() const override;
70 
71  /**
72  * Returns the pause timestamp.
73  * @see FiniteMedium::pauseTimestamp().
74  */
75  Timestamp pauseTimestamp() const override;
76 
77  /**
78  * Returns the stop timestamp.
79  * @see FiniteMedium::stopTimestamp().
80  */
81  Timestamp stopTimestamp() const override;
82 
83  /**
84  * Returns the transformation between the camera and device.
85  * @see FrameMedium::device_T_camera().
86  */
88 
89  /**
90  * Returns the current exposure duration of this device.
91  * @see exposureDuration().
92  */
93  double exposureDuration(double* minDuration = nullptr, double* maxDuration = nullptr, ControlMode* exposureMode = nullptr) const override;
94 
95  /**
96  * Returns the current ISO of this device.
97  * @see iso().
98  */
99  float iso(float* minISO = nullptr, float* maxISO = nullptr, ControlMode* isoMode = nullptr) const override;
100 
101  /**
102  * Returns the current focus of this device.
103  * @see focus().
104  */
105  float focus(ControlMode* focusMode = nullptr) const override;
106 
107  /**
108  * Sets the exposure duration of this device.
109  * @see setExposureDuration().
110  */
111  bool setExposureDuration(const double duration) override;
112 
113  /**
114  * Sets the ISO of this device.
115  * @see setISO().
116  */
117  bool setISO(const float iso) override;
118 
119  /**
120  * Sets the focus of this device.
121  * @see setFocus().
122  */
123  bool setFocus(const float position) override;
124 
125  /**
126  * Explicitly feeds a new external frame this live video.
127  * This function is intended for situations in which this live video does not receive the frame anymore from the system (e.g., when ARCore is accessing the video stream).<br>
128  * Do not call this function in case the live video is still receiving pixel buffers from the Android media system.
129  * @param frame The frame to be fed, must be valid
130  * @param anyCamera The camera profile defining the projection, must be valid
131  * @param exposureMode The exposure mode of the camera, CM_INVALID if unknown
132  * @param exposureDuration The exposure duration of the camera, in seconds, -1 if unknown
133  * @param isoMode The ISO mode of the camera, CM_INVALID if unknown
134  * @param iso The ISO of the camera, -1 if unknown
135  * @param focusMode The focus mode of the camera, CM_INVALID if unknown
136  * @param focusValue The focus value of the camera, in the same domain as ACAMERA_LENS_INFO_MINIMUM_FOCUS_DISTANCE, -1 if unknown
137  */
138  void feedNewFrame(Frame&& frame, SharedAnyCamera&& anyCamera, const ControlMode exposureMode, const double exposureDuration, const ControlMode isoMode, const float iso, const ControlMode focusMode, const float focusValue);
139 
140  /**
141  * Forces a restart of the live video for situations in which the camera was used by an external resource.
142  * This function is intended for situations in which e.g., ARCore has used the borrowed the camera resource but does not use it anymore.
143  * Do not call this function in case the live video is still receiving pixel buffers from the Android media system.
144  * @return True, if succeeded
145  */
146  bool forceRestart();
147 
148  protected:
149 
150  /**
151  * Creates a new medium by a given url.
152  * @param url Url of the medium
153  */
154  explicit ALiveVideo(const std::string& url);
155 
156  /**
157  * Destructs the live video object.
158  */
159  ~ALiveVideo() override;
160 
161  /**
162  * Starts the medium.
163  * @see Medium::start().
164  */
165  bool start() override;
166 
167  /**
168  * Pauses the medium.
169  * @see Medium::pause():
170  */
171  bool pause() override;
172 
173  /**
174  * Stops the medium.
175  * @see Medium::stop().
176  */
177  bool stop() override;
178 
179  /**
180  * Initializes the camera and all corresponding resources.
181  * @return True, if succeeded
182  */
183  bool initialize();
184 
185  /**
186  * Releases the camera and all corresponding resources.
187  * @return True, if succeeded
188  */
189  bool release();
190 
191  /**
192  * Creates an Android camera for the URL of this medium.
193  * @param frameType The resulting frame type matching with the Android camera
194  * @return True, if succeeded
195  */
196  bool createCamera(FrameType& frameType);
197 
198  /**
199  * Releases the Android camera.
200  * @return True, if succeeded
201  */
203 
204  /**
205  * Creates a new image reader based on a given frame type, needs a valid camera.
206  * @param frameType The frame type of the image reader, must be valid
207  * @return True, if succeeded
208  */
209  bool createImageReader(const FrameType& frameType);
210 
211  /**
212  * Releases the image reader.
213  */
215 
216  /**
217  * Creates a new capture session, needs a valid camera and image reader.
218  * @return True, if succeeded
219  */
221 
222  /**
223  * Releases the capture session.
224  */
226 
227  /**
228  * The callback function for camera images.
229  * @param imageReader The image reader of the event, must be valid
230  * @see onCameraImageCallbackStatic().
231  */
232  void onCameraImageCallback(AImageReader* imageReader);
233 
234  /**
235  * The callback function for a disconnected camera.
236  * @param cameraDevice The camera device of the event, must be valid
237  * @see onCameraDisconnectedStatic().
238  */
239  void onCameraDisconnected(ACameraDevice* cameraDevice);
240 
241  /**
242  * The callback function for errors.
243  * @param cameraDevice The camera device of the event, must be valid
244  * @param error The error value of the event
245  * @see onCameraErrorStatic().
246  */
247  void onCameraError(ACameraDevice* cameraDevice, int error);
248 
249  /**
250  * The callback function for an active session.
251  * @param session The session, must be valid
252  */
253  void onSessionActive(ACameraCaptureSession* session);
254 
255  /**
256  * The callback function for a closed session.
257  * @param session The session, must be valid
258  */
259  void onSessionClosed(ACameraCaptureSession* session);
260 
261  /**
262  * The callback function for a started capture.
263  * @param session The session, must be valid
264  * @param request The capture request, must be valid
265  * @param timestamp The capture results, must be valid
266  */
267  void onCaptureStarted(ACameraCaptureSession* session, const ACaptureRequest* request, int64_t timestamp);
268 
269  /**
270  * The callback function for a completed capture.
271  * @param session The session, must be valid
272  * @param request The capture request, must be valid
273  * @param result The capture results, must be valid
274  */
275  void onCaptureCompleted(ACameraCaptureSession* session, ACaptureRequest* request, const ACameraMetadata* result);
276 
277  /**
278  * Event function for new camera frames.
279  * @param frame The new camera frame, must be valid
280  * @param anyCamera The camera profile defining the projection, invalid if unknown
281  * @return True, if succeeded
282  */
283  bool onNewFrame(Frame&& frame, SharedAnyCamera&& anyCamera);
284 
285  /**
286  * Determines the camera id and associated frame type for a given URL.
287  * @param cameraManager The camera manager from which the camera id will be determined, must be valid
288  * @param url The URL for which the camera id will be determined
289  * @param preferredFrameType The preferred frame type (may contain a valid image dimension, or just a pixel format), can be invalid
290  * @param frameType The resulting frame type matching with the resulting id
291  * @return The resulting id of the camera
292  */
293  static std::string cameraIdForMedium(ACameraManager* cameraManager, const std::string& url, const FrameType& preferredFrameType, FrameType& frameType);
294 
295  /**
296  * Determines the range of the exposure duration of the camera sensor.
297  * @param cameraManager The camera manager, must be valid
298  * @param cameraId The id of the camera, must be valid
299  * @param minExposureDuration The minimal exposure duration, in seconds
300  * @param maxExposureDuration The maximal exposure duration, in seconds
301  * @return True, if succeeded
302  */
303  static bool cameraExposureDurationRange(ACameraManager* cameraManager, const std::string& cameraId, double& minExposureDuration, double& maxExposureDuration);
304 
305  /**
306  * Determines the range of the ISO of the camera sensor.
307  * @param cameraManager The camera manager, must be valid
308  * @param cameraId The id of the camera, must be valid
309  * @param minISO The minimal ISO
310  * @param maxISO The maximal ISO
311  * @return True, if succeeded
312  */
313  static bool cameraISORange(ACameraManager* cameraManager, const std::string& cameraId, float& minISO, float& maxISO);
314 
315  /**
316  * Determines the minimal focus distance of the camera sensor.
317  * @param cameraManager The camera manager, must be valid
318  * @param cameraId The id of the camera, must be valid
319  * @param minFocusPosition The minimal focus distance, which is the reciprocal of the focus distance in meters
320  * @return True, if succeeded
321  */
322  static bool cameraMinFocus(ACameraManager* cameraManager, const std::string& cameraId, float& minFocusPosition);
323 
324  /**
325  * Determines the physical size of the camera sensor.
326  * @param cameraManager The camera manager, must be valid
327  * @param cameraId The id of the camera, must be valid
328  * @param cameraSensorPhysicalSizeX Resulting horizontal physical size of the sensor
329  * @return True, if succeeded
330  */
331  static bool cameraSensorPysicalSize(ACameraManager* cameraManager, const std::string& cameraId, float& cameraSensorPhysicalSizeX);
332 
333  /**
334  * Determines the horizontal field of view from the camera's metadata based on `ACAMERA_LENS_FOCAL_LENGTH`.
335  * @param cameraSensorPhysicalSizeX The horizontal physical size of camera sensor, as provided by `ACAMERA_SENSOR_INFO_PHYSICAL_SIZE`.
336  * @param cameraMetadata The camera's metadata for a recent frame, must be valid
337  * @param fovX The resulting horizontal field of view of the camera, in radian
338  * @return True, if succeeded
339  */
340  static bool horizontalFieldOfView(const float cameraSensorPhysicalSizeX, const ACameraMetadata* cameraMetadata, float& fovX);
341 
342  /**
343  * Extracts the next frame from an Android image object.
344  * @param image The Android image object from which the next frame will be extracted
345  * @param frame The resulting frame containing the extracted image
346  * @return True, if succeeded
347  */
348  static bool frameFromImage(AImage* image, Frame& frame);
349 
350  /**
351  * Translates an Android pixel format to an Ocean pixel format.
352  * @param androidFormat The Android pixel format to translate
353  * @return The corresponding Ocean pixel format, FORMAT_UNDEFINED if no corresponding Ocean pixel format exists
354  */
355  static FrameType::PixelFormat androidFormatToPixelFormat(const int32_t androidFormat);
356 
357  /**
358  * Translates Android formats to readable strings.
359  * @param androidFormat The Android pixel format to translate
360  * @return The corresponding readable string of the Android format
361  */
362  static std::string androidFormatToString(const int32_t androidFormat);
363 
364  /**
365  * Translates an Ocean pixel format to an Android pixel format.
366  * @param pixelFormat The Ocean pixel format to translate
367  * @return The corresponding Android pixel format, 0 if no corresponding Android pixel format exists
368  */
369  static int32_t pixelFormatToAndroidFormat(const FrameType::PixelFormat pixelFormat);
370 
371  /**
372  * The static callback function for camera images.
373  * @param context The event's context, must be valid
374  * @param imageReader The image reader of the event, must be valid
375  * @see onCameraImageCallback().
376  */
377  static void onCameraImageCallbackStatic(void* context, AImageReader* imageReader);
378 
379  /**
380  * The static callback function for a disconnected camera.
381  * @param context The event's context, must be valid
382  * @param cameraDevice The camera device of the event, must be valid
383  * @see onCameraDisconnected().
384  */
385  static void onCameraDisconnectedStatic(void* context, ACameraDevice* cameraDevice);
386 
387  /**
388  * The static callback function for errors.
389  * @param context The event's context, must be valid
390  * @param cameraDevice The camera device of the event, must be valid
391  * @param error The error value of the event
392  * @see onCameraError().
393  */
394  static void onCameraErrorStatic(void* context, ACameraDevice* cameraDevice, int error);
395 
396  /**
397  * The static callback function for an active session.
398  * @param context The event's context, must be valid
399  * @param session The session, must be valid
400  */
401  static void onSessionActiveStatic(void* context, ACameraCaptureSession* session);
402 
403  /**
404  * The static callback function for a ready session.
405  * @param context The event's context, must be valid
406  * @param session The session, must be valid
407  */
408  static void onSessionReadyStatic(void* context, ACameraCaptureSession* session);
409 
410  /**
411  * The static callback function for a closed session.
412  * @param context The event's context, must be valid
413  * @param session The session, must be valid
414  */
415  static void onSessionClosedStatic(void* context, ACameraCaptureSession* session);
416 
417  /**
418  * The static callback function for a failed capture.
419  * @param context The event's context, must be valid
420  * @param session The session, must be valid
421  * @param request The capture request, must be valid
422  * @param failure The failure, must be valid
423  */
424  static void onCaptureFailedStatic(void* context, ACameraCaptureSession* session, ACaptureRequest* request, ACameraCaptureFailure* failure);
425 
426  /**
427  * The static callback function for a completed capture sequence.
428  * @param context The event's context, must be valid
429  * @param session The session, must be valid
430  * @param sequenceId The sequence id
431  * @param frameNumber The number of frames
432  */
433  static void onCaptureSequenceCompletedStatic(void* context, ACameraCaptureSession* session, int sequenceId, int64_t frameNumber);
434 
435  /**
436  * The static callback function for an aborted sequence.
437  * @param context The event's context, must be valid
438  * @param session The session, must be valid
439  * @param sequenceId The sequence id
440  */
441  static void onCaptureSequenceAbortedStatic(void* context, ACameraCaptureSession* session, int sequenceId);
442 
443  /**
444  * The static callback function for a started capture.
445  * @param context The event's context, must be valid
446  * @param session The session, must be valid
447  * @param request The capture request, must be valid
448  * @param timestamp The capture timestamp
449  */
450  static void onCaptureStartedStatic(void *context, ACameraCaptureSession *session, const ACaptureRequest *request, int64_t timestamp);
451 
452  /**
453  * The static callback function for a completed capture.
454  * @param context The event's context, must be valid
455  * @param session The session, must be valid
456  * @param request The capture request, must be valid
457  * @param result The capture results, must be valid
458  */
459  static void onCaptureCompletedStatic(void* context, ACameraCaptureSession* session, ACaptureRequest* request, const ACameraMetadata* result);
460 
461  /**
462  * Translates a camera status to a readable strings.
463  * @param cameraStatus The camera status
464  * @return The readable camera status.
465  */
466  static std::string translateCameraStatus(const camera_status_t cameraStatus);
467 
468  protected:
469 
470  /// Unique camera id defined by Android, may be valid even if 'cameraDevice_' is not yet valid.
471  std::string cameraId_;
472 
473  /// The camera device.
474  ACameraDevice* cameraDevice_ = nullptr;
475 
476  /// The image reader.
477  AImageReader* imageReader_ = nullptr;
478 
479  /// The native Android window associated with the video.
480  ANativeWindow* nativeWindow_ = nullptr;
481 
482  /// The session output.
484 
485  /// The session output container.
487 
488  /// The capture session.
489  ACameraCaptureSession* captureSession_ = nullptr;
490 
491  /// The capture request.
492  ACaptureRequest* captureRequest_ = nullptr;
493 
494  /// The output target.
495  ACameraOutputTarget* outputTarget_ = nullptr;
496 
497  /// The horizontal size of the physical camera sensor, if known; -1 if unknown.
498  float cameraSensorPhysicalSizeX_ = -1.0f;
499 
500  /// Recent frame type.
502 
503  /// The current exposure mode of this device.
504  ControlMode exposureMode_ = CM_INVALID;
505 
506  /// The current exposure duration of this device.
507  double exposureDuration_ = -1.0;
508 
509  /// Minimal duration to set, in seconds, with range (0, infinity), -1 if unknown.
510  double exposureDurationMin_ = -1.0;
511 
512  /// Maximal duration to set, in seconds, with range [exposureDurationMin_, infinity), -1 if unknown
513  double exposureDurationMax_ = -1.0;
514 
515  /// The current ISO mode of this device.
516  ControlMode isoMode_ = CM_INVALID;
517 
518  /// The current ISO, with range [isoMin_, isoMax_], 0 for auto IOS, -1 if unknown.
519  float iso_ = -1.0f;
520 
521  /// Minimal ISO to set, with range (0, infinity), -1 if unknown.
522  float isoMin_ = -1.0f;
523 
524  /// Maximal ISO to set, with range (isoMin_, infinity), -1 if unknown.
525  float isoMax_ = -1.0f;
526 
527  /// The current focus mode of this device.
528  ControlMode focusMode_ = CM_INVALID;
529 
530  /// The focus position to be set, with range [0, 1] with 0 shortest distance and 1 furthest distance, -1 if unknown.
531  float focusPosition_ = -1.0f;
532 
533  /// The minimal focus distance (reciprocal of the focus distance in meters), -1 if unknown.
534  float focusPositionMin_ = -1.0f;
535 
536  /// Start timestamp.
538 
539  /// Pause timestamp.
541 
542  /// Stop timestamp.
544 };
545 
546 #else // #if __ANDROID_API__ >= 24
547 
548 /**
549  * Java native interface function for new camera framebuffer events.
550  * @param env JNI environment object
551  * @param javaThis JNI caller object
552  * @param objectId Id of the camera object
553  * @param imageBuffer New camera image buffer
554  * @param width The width of the new camera image buffer in pixel
555  * @param height The height of the new camera image buffer in pixel
556  * @param format The format of the camera image buffer
557  * @param fovX Horizontal field of view in radian
558  * @param bufferSize The size of the given image buffer in bytes
559  * @param timestamp Unix timestamp of the frame in seconds (time since 1970)
560  * @return True, if succeeded
561  * @ingroup mediaandroid
562  */
563 extern "C" jboolean Java_com_meta_ocean_media_android_MediaAndroidJni_onNewCameraFrame(JNIEnv* env, jobject javaThis, jint objectId, jbyteArray imageBuffer, jint width, jint height, jint format, jfloat fovX, jint bufferSize, jdouble timestamp);
564 
565 /**
566  * This class implements an live video class for Android.
567  * @ingroup mediaandroid
568  */
569 class OCEAN_MEDIA_A_EXPORT ALiveVideo final :
570  virtual public AMedium,
571  virtual public LiveVideo
572 {
573  friend class ALibrary;
574  friend class ALiveVideoManager;
575 
576  public:
577 
578  /**
579  * This class implements a camera manager.
580  */
581  class ALiveVideoManager : public Singleton<ALiveVideoManager>
582  {
583  friend class Singleton<ALiveVideoManager>;
584  friend class ALiveVideo;
585 
586  private:
587 
588  /**
589  * Definition of a map mapping camera id to ocean camera objects.
590  */
591  typedef std::map<int, ALiveVideo*> ObjectMap;
592 
593  public:
594 
595  /**
596  * Event function for a new camera frame.
597  * @param cameraId Id of the camera the new frame belongs to
598  * @param frame The new camera frame, must be valid
599  * @param anyCamera The camera profile defining the projection, can be invalid
600  * @return True, if succeeded
601  */
602  bool onNewFrame(const int cameraId, Frame&& frame, SharedAnyCamera&& anyCamera);
603 
604  private:
605 
606  /**
607  * Creates a new camera manager.
608  */
610 
611  /**
612  * Destructs a camera manager.
613  */
615 
616  /**
617  * Creates a new camera object.
618  * @param object Ocean video object to connect with the android camera object
619  * @param url The camera url to create the object for
620  * @param preferredWidth Preferred frame width in pixel
621  * @param preferredHeight Preferred frame height in pixel
622  * @param preferredFrequency Preferred frame frequency in Hz
623  * @return Unique camera id if succeeded, -1 otherwise
624  */
625  int createCamera(ALiveVideo* object, const std::string& url, const unsigned int preferredWidth, const unsigned int preferredHeight, const float preferredFrequency);
626 
627  /**
628  * Releases a camera object.
629  * @param cameraId Unique camera id of the camera to be released
630  */
631  void releaseCamera(const int cameraId);
632 
633  /**
634  * Starts a specified camera object.
635  * @param cameraId Id of the camera to be started
636  * @return True, if succeeded
637  */
638  bool startCamera(const int cameraId);
639 
640  /**
641  * Pauses a specified camera object.
642  * @param cameraId Id of the camera to be paused
643  * @return True, if succeeded
644  */
645  bool pauseCamera(const int cameraId);
646 
647  /**
648  * Stops a specified camera object.
649  * @param cameraId Id of the camera to be stopped
650  * @return True, if succeeded
651  */
652  bool stopCamera(const int cameraId);
653 
654  private:
655 
656  /// The camera object map.
658 
659  /// Manager lock.
661  };
662 
663  /**
664  * Returns whether the medium is started currently.
665  * @see Medium::isStarted().
666  */
667  bool isStarted() const override;
668 
669  /**
670  * Returns the start timestamp.
671  * @see FiniteMedium::startTimestamp().
672  */
673  Timestamp startTimestamp() const override;
674 
675  /**
676  * Returns the pause timestamp.
677  * @see FiniteMedium::pauseTimestamp().
678  */
679  Timestamp pauseTimestamp() const override;
680 
681  /**
682  * Returns the stop timestamp.
683  * @see FiniteMedium::stopTimestamp().
684  */
685  Timestamp stopTimestamp() const override;
686 
687  /**
688  * Returns the transformation between the camera and device.
689  * @see FrameMedium::device_T_camera().
690  */
692 
693  /**
694  * Explicitly feeds a new external frame this live video.
695  * This function is intended for situations in which this live video does not receive the frame anymore from the system (e.g., when ARCore is accessing the video stream).<br>
696  * Do not call this function in case the live video is still receiving pixel buffers from the Android media system.
697  * @param frame The frame to be fed, must be valid
698  * @param anyCamera The camera profile defining the projection, must be valid
699  * @param exposureMode The exposure mode of the camera, CM_INVALID if unknown
700  * @param exposureDuration The exposure duration of the camera, in seconds, -1 if unknown
701  * @param isoMode The ISO mode of the camera, CM_INVALID if unknown
702  * @param iso The ISO of the camera, -1 if unknown
703  * @param focusMode The focus mode of the camera, CM_INVALID if unknown
704  * @param focusValue The focus value of the camera, in the same domain as ACAMERA_LENS_INFO_MINIMUM_FOCUS_DISTANCE, -1 if unknown
705  */
706  void feedNewFrame(Frame&& frame, SharedAnyCamera&& anyCamera, const ControlMode exposureMode, const double exposureDuration, const ControlMode isoMode, const float iso, const ControlMode focusMode, const float focusValue);
707 
708 
709  /**
710  * Forces a restart of the live video for situations in which the camera was used by an external resource.
711  * This function is intended for situations in which e.g., ARCore has used the borrowed the camera resource but does not use it anymore.
712  * Do not call this function in case the live video is still receiving pixel buffers from the Android media system.
713  * @return True, if succeeded
714  */
715  bool forceRestart();
716 
717  protected:
718 
719  /**
720  * Creates a new medium by a given url.
721  * @param url Url of the medium
722  */
723  explicit ALiveVideo(const std::string& url);
724 
725  /**
726  * Destructs the live video object.
727  */
728  ~ALiveVideo() override;
729 
730  /**
731  * Starts the medium.
732  * @see Medium::start().
733  */
734  bool start() override;
735 
736  /**
737  * Pauses the medium.
738  * @see Medium::pause():
739  */
740  bool pause() override;
741 
742  /**
743  * Stops the medium.
744  * @see Medium::stop().
745  */
746  bool stop() override;
747 
748  /**
749  * Event function for new camera frames.
750  * @param frame The new camera frame, must be valid
751  * @param anyCamera The camera profile defining the projection, invalid if unknown
752  * @return True, if succeeded
753  */
754  bool onNewFrame(Frame&& frame, SharedAnyCamera&& anyCamera);
755 
756  protected:
757 
758  /// Unique camera id received by the android system.
759  int mediumCameraId = -1;
760 
761  /// Recent frame type.
762  FrameType imageRecentFrameType;
763 
764  /// State determining whether the camera is currently started.
765  bool mediumIsStarted = false;
766 
767  /// Start timestamp.
769 
770  /// Pause timestamp.
772 
773  /// Stop timestamp.
775 };
776 
777 #endif // __ANDROID_API__ >= 24
778 
779 }
780 
781 }
782 
783 }
784 
785 #endif // META_OCEAN_MEDIA_ANDROID_A_LIVE_VIDEO_H
This class implements Ocean's image class.
Definition: Frame.h:1760
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition: Frame.h:30
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements the android library.
Definition: ALibrary.h:36
This class implements a camera manager.
Definition: ALiveVideo.h:582
Lock managerLock
Manager lock.
Definition: ALiveVideo.h:660
virtual ~ALiveVideoManager()
Destructs a camera manager.
std::map< int, ALiveVideo * > ObjectMap
Definition of a map mapping camera id to ocean camera objects.
Definition: ALiveVideo.h:591
bool onNewFrame(const int cameraId, Frame &&frame, SharedAnyCamera &&anyCamera)
Event function for a new camera frame.
ALiveVideoManager()
Creates a new camera manager.
bool stopCamera(const int cameraId)
Stops a specified camera object.
ObjectMap managerObjectMap
The camera object map.
Definition: ALiveVideo.h:657
bool startCamera(const int cameraId)
Starts a specified camera object.
bool pauseCamera(const int cameraId)
Pauses a specified camera object.
int createCamera(ALiveVideo *object, const std::string &url, const unsigned int preferredWidth, const unsigned int preferredHeight, const float preferredFrequency)
Creates a new camera object.
void releaseCamera(const int cameraId)
Releases a camera object.
This class implements an live video class for Android.
Definition: ALiveVideo.h:40
Timestamp mediumStartTimestamp
Start timestamp.
Definition: ALiveVideo.h:768
Timestamp mediumPauseTimestamp
Pause timestamp.
Definition: ALiveVideo.h:771
bool onNewFrame(Frame &&frame, SharedAnyCamera &&anyCamera)
Event function for new camera frames.
bool start() override
Starts the medium.
float focus(ControlMode *focusMode=nullptr) const override
Returns the current focus of this device.
static bool cameraISORange(ACameraManager *cameraManager, const std::string &cameraId, float &minISO, float &maxISO)
Determines the range of the ISO of the camera sensor.
static void onCameraImageCallbackStatic(void *context, AImageReader *imageReader)
The static callback function for camera images.
bool stop() override
Stops the medium.
static bool cameraSensorPysicalSize(ACameraManager *cameraManager, const std::string &cameraId, float &cameraSensorPhysicalSizeX)
Determines the physical size of the camera sensor.
bool releaseCamera()
Releases the Android camera.
void onCameraError(ACameraDevice *cameraDevice, int error)
The callback function for errors.
static bool cameraMinFocus(ACameraManager *cameraManager, const std::string &cameraId, float &minFocusPosition)
Determines the minimal focus distance of the camera sensor.
void onCaptureStarted(ACameraCaptureSession *session, const ACaptureRequest *request, int64_t timestamp)
The callback function for a started capture.
bool setFocus(const float position) override
Sets the focus of this device.
static void onSessionClosedStatic(void *context, ACameraCaptureSession *session)
The static callback function for a closed session.
void releaseCaptureSession()
Releases the capture session.
static void onCaptureStartedStatic(void *context, ACameraCaptureSession *session, const ACaptureRequest *request, int64_t timestamp)
The static callback function for a started capture.
bool createCaptureSession()
Creates a new capture session, needs a valid camera and image reader.
HomogenousMatrixD4 device_T_camera() const override
Returns the transformation between the camera and device.
AIMAGE_FORMATS_MISSING_FROM_NDK
Definition of Android image formats that are missing from the NDK These values are used but they are ...
Definition: ALiveVideo.h:52
void releaseImageReader()
Releases the image reader.
void onCameraDisconnected(ACameraDevice *cameraDevice)
The callback function for a disconnected camera.
Timestamp pauseTimestamp() const override
Returns the pause timestamp.
bool release()
Releases the camera and all corresponding resources.
bool forceRestart()
Forces a restart of the live video for situations in which the camera was used by an external resourc...
FrameType imageRecentFrameType
Recent frame type.
Definition: ALiveVideo.h:501
static void onCaptureSequenceAbortedStatic(void *context, ACameraCaptureSession *session, int sequenceId)
The static callback function for an aborted sequence.
bool initialize()
Initializes the camera and all corresponding resources.
static FrameType::PixelFormat androidFormatToPixelFormat(const int32_t androidFormat)
Translates an Android pixel format to an Ocean pixel format.
static void onCaptureCompletedStatic(void *context, ACameraCaptureSession *session, ACaptureRequest *request, const ACameraMetadata *result)
The static callback function for a completed capture.
Timestamp startTimestamp() const override
Returns the start timestamp.
static void onCameraErrorStatic(void *context, ACameraDevice *cameraDevice, int error)
The static callback function for errors.
Timestamp mediumStopTimestamp
Stop timestamp.
Definition: ALiveVideo.h:774
static bool horizontalFieldOfView(const float cameraSensorPhysicalSizeX, const ACameraMetadata *cameraMetadata, float &fovX)
Determines the horizontal field of view from the camera's metadata based on ACAMERA_LENS_FOCAL_LENGTH...
static std::string translateCameraStatus(const camera_status_t cameraStatus)
Translates a camera status to a readable strings.
void onCameraImageCallback(AImageReader *imageReader)
The callback function for camera images.
Timestamp stopTimestamp_
Stop timestamp.
Definition: ALiveVideo.h:543
NativeCameraLibrary::ScopedACaptureSessionOutput sessionOutput_
The session output.
Definition: ALiveVideo.h:483
Timestamp stopTimestamp() const override
Returns the stop timestamp.
bool createCamera(FrameType &frameType)
Creates an Android camera for the URL of this medium.
static void onCaptureSequenceCompletedStatic(void *context, ACameraCaptureSession *session, int sequenceId, int64_t frameNumber)
The static callback function for a completed capture sequence.
static bool cameraExposureDurationRange(ACameraManager *cameraManager, const std::string &cameraId, double &minExposureDuration, double &maxExposureDuration)
Determines the range of the exposure duration of the camera sensor.
static void onSessionActiveStatic(void *context, ACameraCaptureSession *session)
The static callback function for an active session.
bool setISO(const float iso) override
Sets the ISO of this device.
static void onSessionReadyStatic(void *context, ACameraCaptureSession *session)
The static callback function for a ready session.
bool pause() override
Pauses the medium.
NativeCameraLibrary::ScopedACaptureSessionOutputContainer sessionOutputContainer_
The session output container.
Definition: ALiveVideo.h:486
void feedNewFrame(Frame &&frame, SharedAnyCamera &&anyCamera, const ControlMode exposureMode, const double exposureDuration, const ControlMode isoMode, const float iso, const ControlMode focusMode, const float focusValue)
Explicitly feeds a new external frame this live video.
bool isStarted() const override
Returns whether the medium is started currently.
void onSessionClosed(ACameraCaptureSession *session)
The callback function for a closed session.
bool createImageReader(const FrameType &frameType)
Creates a new image reader based on a given frame type, needs a valid camera.
static std::string androidFormatToString(const int32_t androidFormat)
Translates Android formats to readable strings.
float iso(float *minISO=nullptr, float *maxISO=nullptr, ControlMode *isoMode=nullptr) const override
Returns the current ISO of this device.
void onSessionActive(ACameraCaptureSession *session)
The callback function for an active session.
bool setExposureDuration(const double duration) override
Sets the exposure duration of this device.
std::string cameraId_
Unique camera id defined by Android, may be valid even if 'cameraDevice_' is not yet valid.
Definition: ALiveVideo.h:471
static int32_t pixelFormatToAndroidFormat(const FrameType::PixelFormat pixelFormat)
Translates an Ocean pixel format to an Android pixel format.
~ALiveVideo() override
Destructs the live video object.
ALiveVideo(const std::string &url)
Creates a new medium by a given url.
void onCaptureCompleted(ACameraCaptureSession *session, ACaptureRequest *request, const ACameraMetadata *result)
The callback function for a completed capture.
Timestamp startTimestamp_
Start timestamp.
Definition: ALiveVideo.h:537
Timestamp pauseTimestamp_
Pause timestamp.
Definition: ALiveVideo.h:540
static void onCaptureFailedStatic(void *context, ACameraCaptureSession *session, ACaptureRequest *request, ACameraCaptureFailure *failure)
The static callback function for a failed capture.
double exposureDuration(double *minDuration=nullptr, double *maxDuration=nullptr, ControlMode *exposureMode=nullptr) const override
Returns the current exposure duration of this device.
static bool frameFromImage(AImage *image, Frame &frame)
Extracts the next frame from an Android image object.
static std::string cameraIdForMedium(ACameraManager *cameraManager, const std::string &url, const FrameType &preferredFrameType, FrameType &frameType)
Determines the camera id and associated frame type for a given URL.
static void onCameraDisconnectedStatic(void *context, ACameraDevice *cameraDevice)
The static callback function for a disconnected camera.
This class implements the base class for all Medium objects in the Android library.
Definition: AMedium.h:35
Definition of a scoped object for ACaptureSessionOutputContainer objects.
Definition: NativeCameraLibrary.h:95
Definition of a scoped object for ACaptureSessionOutput objects.
Definition: NativeCameraLibrary.h:114
This class is the base class for all live videos.
Definition: LiveVideo.h:38
ControlMode
Definition of individual control modes.
Definition: LiveVideo.h:46
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a timestamp.
Definition: Timestamp.h:36
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition: AnyCamera.h:60
jboolean Java_com_meta_ocean_media_android_MediaAndroidJni_onNewCameraFrame(JNIEnv *env, jobject javaThis, jint objectId, jbyteArray imageBuffer, jint width, jint height, jint format, jfloat fovX, jint bufferSize, jdouble timestamp)
Java native interface function for new camera framebuffer events.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15