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