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