• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "MetadataUtil"
19 
20 #include "MetadataUtil.h"
21 
22 #include <algorithm>
23 #include <cstdint>
24 #include <iterator>
25 #include <memory>
26 #include <optional>
27 #include <string>
28 #include <utility>
29 #include <variant>
30 #include <vector>
31 
32 #include "CameraMetadata.h"
33 #include "aidl/android/hardware/camera/device/CameraMetadata.h"
34 #include "log/log.h"
35 #include "system/camera_metadata.h"
36 #include "util/Util.h"
37 #include "utils/Errors.h"
38 
39 namespace android {
40 namespace companion {
41 namespace virtualcamera {
42 
43 namespace {
44 
45 using ::android::hardware::camera::common::helper::CameraMetadata;
46 
47 template <typename To, typename From>
convertTo(const std::vector<From> & from)48 std::vector<To> convertTo(const std::vector<From>& from) {
49   std::vector<To> to;
50   to.reserve(from.size());
51   std::transform(from.begin(), from.end(), std::back_inserter(to),
52                  [](const From& x) { return static_cast<To>(x); });
53   return to;
54 }
55 
56 template <typename To, typename From>
asVectorOf(const From from)57 std::vector<To> asVectorOf(const From from) {
58   return std::vector<To>({static_cast<To>(from)});
59 }
60 
61 }  // namespace
62 
setSupportedHardwareLevel(const camera_metadata_enum_android_info_supported_hardware_level_t hwLevel)63 MetadataBuilder& MetadataBuilder::setSupportedHardwareLevel(
64     const camera_metadata_enum_android_info_supported_hardware_level_t hwLevel) {
65   mEntryMap[ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL] =
66       asVectorOf<uint8_t>(hwLevel);
67   return *this;
68 }
69 
setDeviceId(int32_t deviceId)70 MetadataBuilder& MetadataBuilder::setDeviceId(int32_t deviceId) {
71   mEntryMap[ANDROID_INFO_DEVICE_ID] = std::vector<int32_t>({deviceId});
72   return *this;
73 }
74 
setFlashAvailable(bool flashAvailable)75 MetadataBuilder& MetadataBuilder::setFlashAvailable(bool flashAvailable) {
76   const uint8_t metadataVal = flashAvailable
77                                   ? ANDROID_FLASH_INFO_AVAILABLE_TRUE
78                                   : ANDROID_FLASH_INFO_AVAILABLE_FALSE;
79   mEntryMap[ANDROID_FLASH_INFO_AVAILABLE] = asVectorOf<uint8_t>(metadataVal);
80   return *this;
81 }
82 
setFlashState(const camera_metadata_enum_android_flash_state_t flashState)83 MetadataBuilder& MetadataBuilder::setFlashState(
84     const camera_metadata_enum_android_flash_state_t flashState) {
85   mEntryMap[ANDROID_FLASH_STATE] = asVectorOf<uint8_t>(flashState);
86   return *this;
87 }
88 
setFlashMode(const camera_metadata_enum_android_flash_mode_t flashMode)89 MetadataBuilder& MetadataBuilder::setFlashMode(
90     const camera_metadata_enum_android_flash_mode_t flashMode) {
91   mEntryMap[ANDROID_FLASH_MODE] = asVectorOf<uint8_t>(flashMode);
92   return *this;
93 }
94 
setLensFacing(const camera_metadata_enum_android_lens_facing lensFacing)95 MetadataBuilder& MetadataBuilder::setLensFacing(
96     const camera_metadata_enum_android_lens_facing lensFacing) {
97   mEntryMap[ANDROID_LENS_FACING] = asVectorOf<uint8_t>(lensFacing);
98   return *this;
99 }
100 
setSensorReadoutTimestamp(const camera_metadata_enum_android_sensor_readout_timestamp_t sensorReadoutTimestamp)101 MetadataBuilder& MetadataBuilder::setSensorReadoutTimestamp(
102     const camera_metadata_enum_android_sensor_readout_timestamp_t
103         sensorReadoutTimestamp) {
104   mEntryMap[ANDROID_SENSOR_READOUT_TIMESTAMP] =
105       asVectorOf<uint8_t>(sensorReadoutTimestamp);
106   return *this;
107 }
108 
setAvailableFocalLengths(const std::vector<float> & focalLengths)109 MetadataBuilder& MetadataBuilder::setAvailableFocalLengths(
110     const std::vector<float>& focalLengths) {
111   mEntryMap[ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS] = focalLengths;
112   return *this;
113 }
114 
setFocalLength(float focalLength)115 MetadataBuilder& MetadataBuilder::setFocalLength(float focalLength) {
116   mEntryMap[ANDROID_LENS_FOCAL_LENGTH] = asVectorOf<float>(focalLength);
117   return *this;
118 }
119 
setSensorOrientation(int32_t sensorOrientation)120 MetadataBuilder& MetadataBuilder::setSensorOrientation(int32_t sensorOrientation) {
121   mEntryMap[ANDROID_SENSOR_ORIENTATION] = asVectorOf<int32_t>(sensorOrientation);
122   return *this;
123 }
124 
setSensorTimestampSource(const camera_metadata_enum_android_sensor_info_timestamp_source_t timestampSource)125 MetadataBuilder& MetadataBuilder::setSensorTimestampSource(
126     const camera_metadata_enum_android_sensor_info_timestamp_source_t
127         timestampSource) {
128   mEntryMap[ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE] =
129       asVectorOf<uint8_t>(timestampSource);
130   return *this;
131 }
132 
setSensorTimestamp(std::chrono::nanoseconds timestamp)133 MetadataBuilder& MetadataBuilder::setSensorTimestamp(
134     std::chrono::nanoseconds timestamp) {
135   mEntryMap[ANDROID_SENSOR_TIMESTAMP] = asVectorOf<int64_t>(timestamp.count());
136   return *this;
137 }
138 
setAvailableFaceDetectModes(const std::vector<camera_metadata_enum_android_statistics_face_detect_mode_t> & faceDetectModes)139 MetadataBuilder& MetadataBuilder::setAvailableFaceDetectModes(
140     const std::vector<camera_metadata_enum_android_statistics_face_detect_mode_t>&
141         faceDetectModes) {
142   mEntryMap[ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES] =
143       convertTo<uint8_t>(faceDetectModes);
144   return *this;
145 }
146 
setAvailableTestPatternModes(const std::vector<camera_metadata_enum_android_sensor_test_pattern_mode> & testPatternModes)147 MetadataBuilder& MetadataBuilder::setAvailableTestPatternModes(
148     const std::vector<camera_metadata_enum_android_sensor_test_pattern_mode>&
149         testPatternModes) {
150   mEntryMap[ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES] =
151       convertTo<int32_t>(testPatternModes);
152   return *this;
153 }
154 
setAvailableStreamUseCases(const std::vector<camera_metadata_enum_android_scaler_available_stream_use_cases> & availableUseCases)155 MetadataBuilder& MetadataBuilder::setAvailableStreamUseCases(
156     const std::vector<
157         camera_metadata_enum_android_scaler_available_stream_use_cases>&
158         availableUseCases) {
159   mEntryMap[ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES] =
160       convertTo<int64_t>(availableUseCases);
161   return *this;
162 }
163 
setFaceDetectMode(const camera_metadata_enum_android_statistics_face_detect_mode_t faceDetectMode)164 MetadataBuilder& MetadataBuilder::setFaceDetectMode(
165     const camera_metadata_enum_android_statistics_face_detect_mode_t
166         faceDetectMode) {
167   mEntryMap[ANDROID_STATISTICS_FACE_DETECT_MODE] =
168       asVectorOf<uint8_t>(faceDetectMode);
169   return *this;
170 }
171 
setControlAvailableModes(const std::vector<camera_metadata_enum_android_control_mode_t> & availableModes)172 MetadataBuilder& MetadataBuilder::setControlAvailableModes(
173     const std::vector<camera_metadata_enum_android_control_mode_t>&
174         availableModes) {
175   mEntryMap[ANDROID_CONTROL_AVAILABLE_MODES] =
176       convertTo<uint8_t>(availableModes);
177   return *this;
178 }
179 
setControlMode(const camera_metadata_enum_android_control_mode_t mode)180 MetadataBuilder& MetadataBuilder::setControlMode(
181     const camera_metadata_enum_android_control_mode_t mode) {
182   mEntryMap[ANDROID_CONTROL_MODE] = asVectorOf<uint8_t>(mode);
183   return *this;
184 }
185 
setControlAvailableSceneModes(const std::vector<camera_metadata_enum_android_control_scene_mode> & availableSceneModes)186 MetadataBuilder& MetadataBuilder::setControlAvailableSceneModes(
187     const std::vector<camera_metadata_enum_android_control_scene_mode>&
188         availableSceneModes) {
189   mEntryMap[ANDROID_CONTROL_AVAILABLE_SCENE_MODES] =
190       convertTo<uint8_t>(availableSceneModes);
191   return *this;
192 }
193 
setControlSceneMode(const camera_metadata_enum_android_control_scene_mode sceneMode)194 MetadataBuilder& MetadataBuilder::setControlSceneMode(
195     const camera_metadata_enum_android_control_scene_mode sceneMode) {
196   mEntryMap[ANDROID_CONTROL_SCENE_MODE] = asVectorOf<uint8_t>(sceneMode);
197   return *this;
198 }
199 
setControlAvailableEffects(const std::vector<camera_metadata_enum_android_control_effect_mode> & availableEffects)200 MetadataBuilder& MetadataBuilder::setControlAvailableEffects(
201     const std::vector<camera_metadata_enum_android_control_effect_mode>&
202         availableEffects) {
203   mEntryMap[ANDROID_CONTROL_AVAILABLE_EFFECTS] =
204       convertTo<uint8_t>(availableEffects);
205   return *this;
206 }
207 
setControlEffectMode(const camera_metadata_enum_android_control_effect_mode_t effectMode)208 MetadataBuilder& MetadataBuilder::setControlEffectMode(
209     const camera_metadata_enum_android_control_effect_mode_t effectMode) {
210   mEntryMap[ANDROID_CONTROL_EFFECT_MODE] = asVectorOf<uint8_t>(effectMode);
211   return *this;
212 }
213 
setControlAvailableVideoStabilizationModes(const std::vector<camera_metadata_enum_android_control_video_stabilization_mode_t> & videoStabilizationModes)214 MetadataBuilder& MetadataBuilder::setControlAvailableVideoStabilizationModes(
215     const std::vector<
216         camera_metadata_enum_android_control_video_stabilization_mode_t>&
217         videoStabilizationModes) {
218   mEntryMap[ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES] =
219       convertTo<uint8_t>(videoStabilizationModes);
220   return *this;
221 }
222 
setControlVideoStabilizationMode(const camera_metadata_enum_android_control_video_stabilization_mode stabilizationMode)223 MetadataBuilder& MetadataBuilder::setControlVideoStabilizationMode(
224     const camera_metadata_enum_android_control_video_stabilization_mode
225         stabilizationMode) {
226   mEntryMap[ANDROID_CONTROL_VIDEO_STABILIZATION_MODE] =
227       asVectorOf<uint8_t>(stabilizationMode);
228   return *this;
229 }
230 
setControlAfAvailableModes(const std::vector<camera_metadata_enum_android_control_af_mode_t> & availableModes)231 MetadataBuilder& MetadataBuilder::setControlAfAvailableModes(
232     const std::vector<camera_metadata_enum_android_control_af_mode_t>&
233         availableModes) {
234   mEntryMap[ANDROID_CONTROL_AF_AVAILABLE_MODES] =
235       convertTo<uint8_t>(availableModes);
236   return *this;
237 }
238 
setControlAfMode(const camera_metadata_enum_android_control_af_mode_t mode)239 MetadataBuilder& MetadataBuilder::setControlAfMode(
240     const camera_metadata_enum_android_control_af_mode_t mode) {
241   mEntryMap[ANDROID_CONTROL_AF_MODE] = asVectorOf<uint8_t>(mode);
242   return *this;
243 }
244 
setControlAfState(const camera_metadata_enum_android_control_af_state afState)245 MetadataBuilder& MetadataBuilder::setControlAfState(
246     const camera_metadata_enum_android_control_af_state afState) {
247   mEntryMap[ANDROID_CONTROL_AF_STATE] = asVectorOf<uint8_t>(afState);
248   return *this;
249 }
250 
251 // See ANDROID_CONTROL_AF_TRIGGER_MODE in CameraMetadataTag.aidl.
setControlAfTrigger(const camera_metadata_enum_android_control_af_trigger_t trigger)252 MetadataBuilder& MetadataBuilder::setControlAfTrigger(
253     const camera_metadata_enum_android_control_af_trigger_t trigger) {
254   mEntryMap[ANDROID_CONTROL_AF_TRIGGER] = asVectorOf<uint8_t>(trigger);
255   return *this;
256 }
257 
setControlAeAvailableFpsRanges(const std::vector<FpsRange> & fpsRanges)258 MetadataBuilder& MetadataBuilder::setControlAeAvailableFpsRanges(
259     const std::vector<FpsRange>& fpsRanges) {
260   std::vector<int32_t> ranges;
261   ranges.reserve(2 * fpsRanges.size());
262   for (const FpsRange fpsRange : fpsRanges) {
263     ranges.push_back(fpsRange.minFps);
264     ranges.push_back(fpsRange.maxFps);
265   }
266   mEntryMap[ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES] = std::move(ranges);
267   return *this;
268 }
269 
setControlAeTargetFpsRange(const FpsRange fpsRange)270 MetadataBuilder& MetadataBuilder::setControlAeTargetFpsRange(
271     const FpsRange fpsRange) {
272   mEntryMap[ANDROID_CONTROL_AE_TARGET_FPS_RANGE] =
273       std::vector<int32_t>({fpsRange.minFps, fpsRange.maxFps});
274   return *this;
275 }
276 
setControlAeMode(const camera_metadata_enum_android_control_ae_mode_t mode)277 MetadataBuilder& MetadataBuilder::setControlAeMode(
278     const camera_metadata_enum_android_control_ae_mode_t mode) {
279   mEntryMap[ANDROID_CONTROL_AE_MODE] = asVectorOf<uint8_t>(mode);
280   return *this;
281 }
282 
setControlAeAvailableModes(const std::vector<camera_metadata_enum_android_control_ae_mode_t> & modes)283 MetadataBuilder& MetadataBuilder::setControlAeAvailableModes(
284     const std::vector<camera_metadata_enum_android_control_ae_mode_t>& modes) {
285   mEntryMap[ANDROID_CONTROL_AE_AVAILABLE_MODES] = convertTo<uint8_t>(modes);
286   return *this;
287 }
288 
setControlAePrecaptureTrigger(const camera_metadata_enum_android_control_ae_precapture_trigger_t trigger)289 MetadataBuilder& MetadataBuilder::setControlAePrecaptureTrigger(
290     const camera_metadata_enum_android_control_ae_precapture_trigger_t trigger) {
291   mEntryMap[ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER] =
292       asVectorOf<uint8_t>(trigger);
293   return *this;
294 }
295 
setControlMaxRegions(int32_t maxAeRegions,int32_t maxAwbRegions,int32_t maxAfRegions)296 MetadataBuilder& MetadataBuilder::setControlMaxRegions(int32_t maxAeRegions,
297                                                        int32_t maxAwbRegions,
298                                                        int32_t maxAfRegions) {
299   mEntryMap[ANDROID_CONTROL_MAX_REGIONS] =
300       std::vector<int32_t>({maxAeRegions, maxAwbRegions, maxAfRegions});
301   return *this;
302 }
303 
setControlAvailableAwbModes(const std::vector<camera_metadata_enum_android_control_awb_mode> & awbModes)304 MetadataBuilder& MetadataBuilder::setControlAvailableAwbModes(
305     const std::vector<camera_metadata_enum_android_control_awb_mode>& awbModes) {
306   mEntryMap[ANDROID_CONTROL_AWB_AVAILABLE_MODES] = convertTo<uint8_t>(awbModes);
307   return *this;
308 }
309 
setControlAwbMode(const camera_metadata_enum_android_control_awb_mode awbMode)310 MetadataBuilder& MetadataBuilder::setControlAwbMode(
311     const camera_metadata_enum_android_control_awb_mode awbMode) {
312   mEntryMap[ANDROID_CONTROL_AWB_MODE] = asVectorOf<uint8_t>(awbMode);
313   return *this;
314 }
315 
setControlAwbState(const camera_metadata_enum_android_control_awb_state awbState)316 MetadataBuilder& MetadataBuilder::setControlAwbState(
317     const camera_metadata_enum_android_control_awb_state awbState) {
318   mEntryMap[ANDROID_CONTROL_AWB_STATE] = asVectorOf<uint8_t>(awbState);
319   return *this;
320 }
321 
setControlAwbLockAvailable(const bool awbLockAvailable)322 MetadataBuilder& MetadataBuilder::setControlAwbLockAvailable(
323     const bool awbLockAvailable) {
324   const uint8_t lockAvailable = awbLockAvailable
325                                     ? ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE
326                                     : ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
327   mEntryMap[ANDROID_CONTROL_AWB_LOCK_AVAILABLE] =
328       std::vector<uint8_t>({lockAvailable});
329   return *this;
330 }
331 
setControlAwbLock(const camera_metadata_enum_android_control_awb_lock awbLock)332 MetadataBuilder& MetadataBuilder::setControlAwbLock(
333     const camera_metadata_enum_android_control_awb_lock awbLock) {
334   mEntryMap[ANDROID_CONTROL_AWB_LOCK] = asVectorOf<uint8_t>(awbLock);
335   return *this;
336 }
337 
setControlAeAvailableAntibandingModes(const std::vector<camera_metadata_enum_android_control_ae_antibanding_mode_t> & antibandingModes)338 MetadataBuilder& MetadataBuilder::setControlAeAvailableAntibandingModes(
339     const std::vector<camera_metadata_enum_android_control_ae_antibanding_mode_t>&
340         antibandingModes) {
341   mEntryMap[ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES] =
342       convertTo<uint8_t>(antibandingModes);
343   return *this;
344 }
345 
setControlAeAntibandingMode(const camera_metadata_enum_android_control_ae_antibanding_mode_t antibandingMode)346 MetadataBuilder& MetadataBuilder::setControlAeAntibandingMode(
347     const camera_metadata_enum_android_control_ae_antibanding_mode_t
348         antibandingMode) {
349   mEntryMap[ANDROID_CONTROL_AE_ANTIBANDING_MODE] =
350       asVectorOf<uint8_t>(antibandingMode);
351   return *this;
352 }
353 
setControlAeLockAvailable(const bool aeLockAvailable)354 MetadataBuilder& MetadataBuilder::setControlAeLockAvailable(
355     const bool aeLockAvailable) {
356   const uint8_t lockAvailable = aeLockAvailable
357                                     ? ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE
358                                     : ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
359   mEntryMap[ANDROID_CONTROL_AE_LOCK_AVAILABLE] =
360       asVectorOf<uint8_t>(lockAvailable);
361   return *this;
362 }
363 
setControlAeLock(const camera_metadata_enum_android_control_ae_lock aeLock)364 MetadataBuilder& MetadataBuilder::setControlAeLock(
365     const camera_metadata_enum_android_control_ae_lock aeLock) {
366   mEntryMap[ANDROID_CONTROL_AE_LOCK] = asVectorOf<uint8_t>(aeLock);
367   return *this;
368 }
369 
setControlAeRegions(const std::vector<ControlRegion> & aeRegions)370 MetadataBuilder& MetadataBuilder::setControlAeRegions(
371     const std::vector<ControlRegion>& aeRegions) {
372   std::vector<int32_t> regions;
373   regions.reserve(5 * aeRegions.size());
374   for (const ControlRegion& region : aeRegions) {
375     regions.push_back(region.x0);
376     regions.push_back(region.y0);
377     regions.push_back(region.x1);
378     regions.push_back(region.y1);
379     regions.push_back(region.weight);
380   }
381   mEntryMap[ANDROID_CONTROL_AE_REGIONS] = std::move(regions);
382   return *this;
383 }
384 
setControlAfRegions(const std::vector<ControlRegion> & afRegions)385 MetadataBuilder& MetadataBuilder::setControlAfRegions(
386     const std::vector<ControlRegion>& afRegions) {
387   std::vector<int32_t> regions;
388   regions.reserve(5 * afRegions.size());
389   for (const ControlRegion& region : afRegions) {
390     regions.push_back(region.x0);
391     regions.push_back(region.y0);
392     regions.push_back(region.x1);
393     regions.push_back(region.y1);
394     regions.push_back(region.weight);
395   }
396   mEntryMap[ANDROID_CONTROL_AF_REGIONS] = std::move(regions);
397   return *this;
398 }
399 
setControlAwbRegions(const std::vector<ControlRegion> & awbRegions)400 MetadataBuilder& MetadataBuilder::setControlAwbRegions(
401     const std::vector<ControlRegion>& awbRegions) {
402   std::vector<int32_t> regions;
403   regions.reserve(5 * awbRegions.size());
404   for (const ControlRegion& region : awbRegions) {
405     regions.push_back(region.x0);
406     regions.push_back(region.y0);
407     regions.push_back(region.x1);
408     regions.push_back(region.y1);
409     regions.push_back(region.weight);
410   }
411   mEntryMap[ANDROID_CONTROL_AWB_REGIONS] = std::move(regions);
412   return *this;
413 }
414 
setControlCaptureIntent(const camera_metadata_enum_android_control_capture_intent_t intent)415 MetadataBuilder& MetadataBuilder::setControlCaptureIntent(
416     const camera_metadata_enum_android_control_capture_intent_t intent) {
417   mEntryMap[ANDROID_CONTROL_CAPTURE_INTENT] = asVectorOf<uint8_t>(intent);
418   return *this;
419 }
420 
setCropRegion(const int32_t x,const int32_t y,const int32_t width,const int32_t height)421 MetadataBuilder& MetadataBuilder::setCropRegion(const int32_t x, const int32_t y,
422                                                 const int32_t width,
423                                                 const int32_t height) {
424   mEntryMap[ANDROID_SCALER_CROP_REGION] =
425       std::vector<int32_t>({x, y, width, height});
426   return *this;
427 }
428 
setMaxJpegSize(const int32_t size)429 MetadataBuilder& MetadataBuilder::setMaxJpegSize(const int32_t size) {
430   mEntryMap[ANDROID_JPEG_MAX_SIZE] = asVectorOf<int32_t>(size);
431   return *this;
432 }
433 
setMaxFrameDuration(const std::chrono::nanoseconds duration)434 MetadataBuilder& MetadataBuilder::setMaxFrameDuration(
435     const std::chrono::nanoseconds duration) {
436   mEntryMap[ANDROID_SENSOR_INFO_MAX_FRAME_DURATION] =
437       asVectorOf<int64_t>(duration.count());
438   return *this;
439 }
440 
setJpegAvailableThumbnailSizes(const std::vector<Resolution> & thumbnailSizes)441 MetadataBuilder& MetadataBuilder::setJpegAvailableThumbnailSizes(
442     const std::vector<Resolution>& thumbnailSizes) {
443   std::vector<int32_t> sizes;
444   sizes.reserve(thumbnailSizes.size() * 2);
445   for (const Resolution& resolution : thumbnailSizes) {
446     sizes.push_back(resolution.width);
447     sizes.push_back(resolution.height);
448   }
449   mEntryMap[ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES] = std::move(sizes);
450   return *this;
451 }
452 
setJpegGpsCoordinates(const GpsCoordinates & gpsCoordinates)453 MetadataBuilder& MetadataBuilder::setJpegGpsCoordinates(
454     const GpsCoordinates& gpsCoordinates) {
455   mEntryMap[ANDROID_JPEG_GPS_COORDINATES] =
456       std::vector<double>({gpsCoordinates.latitude, gpsCoordinates.longitude,
457                            gpsCoordinates.altitude});
458 
459   if (!gpsCoordinates.provider.empty()) {
460     mEntryMap[ANDROID_JPEG_GPS_PROCESSING_METHOD] = std::vector<uint8_t>{
461         gpsCoordinates.provider.begin(), gpsCoordinates.provider.end()};
462   }
463 
464   if (gpsCoordinates.timestamp.has_value()) {
465     mEntryMap[ANDROID_JPEG_GPS_TIMESTAMP] =
466         asVectorOf<int64_t>(gpsCoordinates.timestamp.value());
467   }
468   return *this;
469 }
470 
setJpegOrientation(const int32_t orientation)471 MetadataBuilder& MetadataBuilder::setJpegOrientation(const int32_t orientation) {
472   mEntryMap[ANDROID_JPEG_ORIENTATION] = asVectorOf<int32_t>(orientation);
473   return *this;
474 }
475 
setJpegQuality(const uint8_t quality)476 MetadataBuilder& MetadataBuilder::setJpegQuality(const uint8_t quality) {
477   mEntryMap[ANDROID_JPEG_QUALITY] = asVectorOf<uint8_t>(quality);
478   return *this;
479 }
480 
setJpegThumbnailSize(const int width,const int height)481 MetadataBuilder& MetadataBuilder::setJpegThumbnailSize(const int width,
482                                                        const int height) {
483   mEntryMap[ANDROID_JPEG_THUMBNAIL_SIZE] = std::vector<int32_t>({width, height});
484   return *this;
485 }
486 
setJpegThumbnailQuality(const uint8_t quality)487 MetadataBuilder& MetadataBuilder::setJpegThumbnailQuality(const uint8_t quality) {
488   mEntryMap[ANDROID_JPEG_THUMBNAIL_QUALITY] = asVectorOf<uint8_t>(quality);
489   return *this;
490 }
491 
setMaxNumberOutputStreams(const int32_t maxRawStreams,const int32_t maxProcessedStreams,const int32_t maxStallStreams)492 MetadataBuilder& MetadataBuilder::setMaxNumberOutputStreams(
493     const int32_t maxRawStreams, const int32_t maxProcessedStreams,
494     const int32_t maxStallStreams) {
495   mEntryMap[ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS] = std::vector<int32_t>(
496       {maxRawStreams, maxProcessedStreams, maxStallStreams});
497   return *this;
498 }
499 
setSyncMaxLatency(const camera_metadata_enum_android_sync_max_latency latency)500 MetadataBuilder& MetadataBuilder::setSyncMaxLatency(
501     const camera_metadata_enum_android_sync_max_latency latency) {
502   mEntryMap[ANDROID_SYNC_MAX_LATENCY] = asVectorOf<int32_t>(latency);
503   return *this;
504 }
505 
setPipelineMaxDepth(const uint8_t maxDepth)506 MetadataBuilder& MetadataBuilder::setPipelineMaxDepth(const uint8_t maxDepth) {
507   mEntryMap[ANDROID_REQUEST_PIPELINE_MAX_DEPTH] = asVectorOf<uint8_t>(maxDepth);
508   return *this;
509 }
510 
setPipelineDepth(const uint8_t depth)511 MetadataBuilder& MetadataBuilder::setPipelineDepth(const uint8_t depth) {
512   mEntryMap[ANDROID_REQUEST_PIPELINE_DEPTH] = asVectorOf<uint8_t>(depth);
513   return *this;
514 }
515 
setAvailableRequestCapabilities(const std::vector<camera_metadata_enum_android_request_available_capabilities_t> & requestCapabilities)516 MetadataBuilder& MetadataBuilder::setAvailableRequestCapabilities(
517     const std::vector<camera_metadata_enum_android_request_available_capabilities_t>&
518         requestCapabilities) {
519   mEntryMap[ANDROID_REQUEST_AVAILABLE_CAPABILITIES] =
520       convertTo<uint8_t>(requestCapabilities);
521   return *this;
522 }
523 
setAvailableOutputStreamConfigurations(const std::vector<StreamConfiguration> & streamConfigurations)524 MetadataBuilder& MetadataBuilder::setAvailableOutputStreamConfigurations(
525     const std::vector<StreamConfiguration>& streamConfigurations) {
526   std::vector<int32_t> metadataStreamConfigs;
527   std::vector<int64_t> metadataMinFrameDurations;
528   std::vector<int64_t> metadataStallDurations;
529   metadataStreamConfigs.reserve(streamConfigurations.size());
530   metadataMinFrameDurations.reserve(streamConfigurations.size());
531   metadataStallDurations.reserve(streamConfigurations.size());
532 
533   for (const auto& config : streamConfigurations) {
534     metadataStreamConfigs.push_back(config.format);
535     metadataStreamConfigs.push_back(config.width);
536     metadataStreamConfigs.push_back(config.height);
537     metadataStreamConfigs.push_back(
538         ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
539 
540     metadataMinFrameDurations.push_back(config.format);
541     metadataMinFrameDurations.push_back(config.width);
542     metadataMinFrameDurations.push_back(config.height);
543     metadataMinFrameDurations.push_back(config.minFrameDuration.count());
544 
545     metadataStallDurations.push_back(config.format);
546     metadataStallDurations.push_back(config.width);
547     metadataStallDurations.push_back(config.height);
548     metadataStallDurations.push_back(config.minStallDuration.count());
549   }
550 
551   mEntryMap[ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS] =
552       std::move(metadataStreamConfigs);
553   mEntryMap[ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS] =
554       std::move(metadataMinFrameDurations);
555   mEntryMap[ANDROID_SCALER_AVAILABLE_STALL_DURATIONS] =
556       std::move(metadataStallDurations);
557 
558   return *this;
559 }
560 
setAvailableAberrationCorrectionModes(const std::vector<camera_metadata_enum_android_color_correction_aberration_mode> & aberrationCorectionModes)561 MetadataBuilder& MetadataBuilder::setAvailableAberrationCorrectionModes(
562     const std::vector<camera_metadata_enum_android_color_correction_aberration_mode>&
563         aberrationCorectionModes) {
564   mEntryMap[ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES] =
565       convertTo<uint8_t>(aberrationCorectionModes);
566   return *this;
567 }
568 
setAberrationCorrectionMode(const camera_metadata_enum_android_color_correction_aberration_mode aberrationCorrectionMode)569 MetadataBuilder& MetadataBuilder::setAberrationCorrectionMode(
570     const camera_metadata_enum_android_color_correction_aberration_mode
571         aberrationCorrectionMode) {
572   mEntryMap[ANDROID_COLOR_CORRECTION_ABERRATION_MODE] =
573       asVectorOf<uint8_t>(aberrationCorrectionMode);
574   return *this;
575 }
576 
setAvailableNoiseReductionModes(const std::vector<camera_metadata_enum_android_noise_reduction_mode> & noiseReductionModes)577 MetadataBuilder& MetadataBuilder::setAvailableNoiseReductionModes(
578     const std::vector<camera_metadata_enum_android_noise_reduction_mode>&
579         noiseReductionModes) {
580   mEntryMap[ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES] =
581       convertTo<uint8_t>(noiseReductionModes);
582   return *this;
583 }
584 
setNoiseReductionMode(const camera_metadata_enum_android_noise_reduction_mode noiseReductionMode)585 MetadataBuilder& MetadataBuilder::setNoiseReductionMode(
586     const camera_metadata_enum_android_noise_reduction_mode noiseReductionMode) {
587   mEntryMap[ANDROID_NOISE_REDUCTION_MODE] =
588       asVectorOf<uint8_t>(noiseReductionMode);
589   return *this;
590 }
591 
setRequestPartialResultCount(const int partialResultCount)592 MetadataBuilder& MetadataBuilder::setRequestPartialResultCount(
593     const int partialResultCount) {
594   mEntryMap[ANDROID_REQUEST_PARTIAL_RESULT_COUNT] =
595       asVectorOf<int32_t>(partialResultCount);
596   return *this;
597 }
598 
setCroppingType(const camera_metadata_enum_android_scaler_cropping_type croppingType)599 MetadataBuilder& MetadataBuilder::setCroppingType(
600     const camera_metadata_enum_android_scaler_cropping_type croppingType) {
601   mEntryMap[ANDROID_SCALER_CROPPING_TYPE] = asVectorOf<uint8_t>(croppingType);
602   return *this;
603 }
604 
setMaxFaceCount(const int maxFaceCount)605 MetadataBuilder& MetadataBuilder::setMaxFaceCount(const int maxFaceCount) {
606   mEntryMap[ANDROID_STATISTICS_INFO_MAX_FACE_COUNT] =
607       asVectorOf<int32_t>(maxFaceCount);
608   return *this;
609 }
610 
setAvailableMaxDigitalZoom(const float maxZoom)611 MetadataBuilder& MetadataBuilder::setAvailableMaxDigitalZoom(const float maxZoom) {
612   mEntryMap[ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM] =
613       asVectorOf<float>(maxZoom);
614   return *this;
615 }
616 
setControlZoomRatioRange(const float min,const float max)617 MetadataBuilder& MetadataBuilder::setControlZoomRatioRange(const float min,
618                                                            const float max) {
619   mEntryMap[ANDROID_CONTROL_ZOOM_RATIO_RANGE] = std::vector<float>({min, max});
620   return *this;
621 }
622 
setSensorActiveArraySize(int x0,int y0,int x1,int y1)623 MetadataBuilder& MetadataBuilder::setSensorActiveArraySize(int x0, int y0,
624                                                            int x1, int y1) {
625   mEntryMap[ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE] =
626       std::vector<int32_t>({x0, y0, x1, y1});
627   return *this;
628 }
629 
setSensorPixelArraySize(int width,int height)630 MetadataBuilder& MetadataBuilder::setSensorPixelArraySize(int width,
631                                                           int height) {
632   mEntryMap[ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE] =
633       std::vector<int32_t>({width, height});
634   return *this;
635 }
636 
setSensorPhysicalSize(float width,float height)637 MetadataBuilder& MetadataBuilder::setSensorPhysicalSize(float width,
638                                                         float height) {
639   mEntryMap[ANDROID_SENSOR_INFO_PHYSICAL_SIZE] =
640       std::vector<float>({width, height});
641   return *this;
642 }
643 
setControlAeCompensationRange(int32_t min,int32_t max)644 MetadataBuilder& MetadataBuilder::setControlAeCompensationRange(int32_t min,
645                                                                 int32_t max) {
646   mEntryMap[ANDROID_CONTROL_AE_COMPENSATION_RANGE] =
647       std::vector<int32_t>({min, max});
648   return *this;
649 }
650 
setControlAeCompensationStep(const camera_metadata_rational step)651 MetadataBuilder& MetadataBuilder::setControlAeCompensationStep(
652     const camera_metadata_rational step) {
653   mEntryMap[ANDROID_CONTROL_AE_COMPENSATION_STEP] =
654       asVectorOf<camera_metadata_rational>(step);
655   return *this;
656 }
657 
setControlAeExposureCompensation(const int32_t exposureCompensation)658 MetadataBuilder& MetadataBuilder::setControlAeExposureCompensation(
659     const int32_t exposureCompensation) {
660   mEntryMap[ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION] =
661       asVectorOf<int32_t>(exposureCompensation);
662   return *this;
663 }
664 
setControlAeState(const camera_metadata_enum_android_control_ae_state aeState)665 MetadataBuilder& MetadataBuilder::setControlAeState(
666     const camera_metadata_enum_android_control_ae_state aeState) {
667   mEntryMap[ANDROID_CONTROL_AE_STATE] = asVectorOf<uint8_t>(aeState);
668   return *this;
669 }
670 
setStatisticsSceneFlicker(const camera_metadata_enum_android_statistics_scene_flicker sceneFlicker)671 MetadataBuilder& MetadataBuilder::setStatisticsSceneFlicker(
672     const camera_metadata_enum_android_statistics_scene_flicker sceneFlicker) {
673   mEntryMap[ANDROID_STATISTICS_SCENE_FLICKER] =
674       asVectorOf<uint8_t>(sceneFlicker);
675   return *this;
676 }
677 
setStatisticsHotPixelMapMode(const camera_metadata_enum_android_statistics_hot_pixel_map_mode hotPixelMapMode)678 MetadataBuilder& MetadataBuilder::setStatisticsHotPixelMapMode(
679     const camera_metadata_enum_android_statistics_hot_pixel_map_mode
680         hotPixelMapMode) {
681   mEntryMap[ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE] =
682       asVectorOf<uint8_t>(hotPixelMapMode);
683   return *this;
684 }
685 
setStatisticsLensShadingMapMode(const camera_metadata_enum_android_statistics_lens_shading_map_mode lensShadingMapMode)686 MetadataBuilder& MetadataBuilder::setStatisticsLensShadingMapMode(
687     const camera_metadata_enum_android_statistics_lens_shading_map_mode
688         lensShadingMapMode) {
689   mEntryMap[ANDROID_STATISTICS_LENS_SHADING_MAP_MODE] =
690       asVectorOf<uint8_t>(lensShadingMapMode);
691   return *this;
692 }
693 
setLensOpticalStabilizationMode(const camera_metadata_enum_android_lens_optical_stabilization_mode_t opticalStabilizationMode)694 MetadataBuilder& MetadataBuilder::setLensOpticalStabilizationMode(
695     const camera_metadata_enum_android_lens_optical_stabilization_mode_t
696         opticalStabilizationMode) {
697   mEntryMap[ANDROID_LENS_OPTICAL_STABILIZATION_MODE] =
698       asVectorOf<uint8_t>(opticalStabilizationMode);
699   return *this;
700 }
701 
setAvailableRequestKeys(const std::vector<int32_t> & keys)702 MetadataBuilder& MetadataBuilder::setAvailableRequestKeys(
703     const std::vector<int32_t>& keys) {
704   mEntryMap[ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS] = keys;
705   return *this;
706 }
707 
setAvailableResultKeys(const std::vector<int32_t> & keys)708 MetadataBuilder& MetadataBuilder::setAvailableResultKeys(
709     const std::vector<int32_t>& keys) {
710   mEntryMap[ANDROID_REQUEST_AVAILABLE_RESULT_KEYS] = keys;
711   return *this;
712 }
713 
setAvailableCapabilities(const std::vector<camera_metadata_enum_android_request_available_capabilities_t> & capabilities)714 MetadataBuilder& MetadataBuilder::setAvailableCapabilities(
715     const std::vector<camera_metadata_enum_android_request_available_capabilities_t>&
716         capabilities) {
717   mEntryMap[ANDROID_REQUEST_AVAILABLE_CAPABILITIES] =
718       convertTo<uint8_t>(capabilities);
719   return *this;
720 }
721 
setAvailableCharacteristicKeys(const std::vector<camera_metadata_tag_t> & keys)722 MetadataBuilder& MetadataBuilder::setAvailableCharacteristicKeys(
723     const std::vector<camera_metadata_tag_t>& keys) {
724   mEntryMap[ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS] =
725       convertTo<int32_t>(keys);
726   return *this;
727 }
728 
setAvailableCharacteristicKeys()729 MetadataBuilder& MetadataBuilder::setAvailableCharacteristicKeys() {
730   mExtendWithAvailableCharacteristicsKeys = true;
731   return *this;
732 }
733 
734 std::unique_ptr<aidl::android::hardware::camera::device::CameraMetadata>
build()735 MetadataBuilder::build() {
736   if (mExtendWithAvailableCharacteristicsKeys) {
737     std::vector<camera_metadata_tag_t> availableKeys;
738     availableKeys.reserve(mEntryMap.size());
739     for (const auto& [key, _] : mEntryMap) {
740       if (key != ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS) {
741         availableKeys.push_back(key);
742       }
743     }
744     setAvailableCharacteristicKeys(availableKeys);
745   }
746 
747   CameraMetadata metadataHelper;
748   for (const auto& entry : mEntryMap) {
749     status_t ret = std::visit(
750         [&](auto&& arg) {
751           return metadataHelper.update(entry.first, arg.data(), arg.size());
752         },
753         entry.second);
754     if (ret != NO_ERROR) {
755       ALOGE("Failed to update metadata with key %d - %s: %s", entry.first,
756             get_camera_metadata_tag_name(entry.first),
757             ::android::statusToString(ret).c_str());
758       return nullptr;
759     }
760   }
761 
762   const camera_metadata_t* metadata = metadataHelper.getAndLock();
763   if (metadata == nullptr) {
764     ALOGE(
765         "Failure when constructing metadata -> CameraMetadata helper returned "
766         "nullptr");
767     return nullptr;
768   }
769 
770   auto aidlMetadata =
771       std::make_unique<aidl::android::hardware::camera::device::CameraMetadata>();
772   const uint8_t* data_ptr = reinterpret_cast<const uint8_t*>(metadata);
773   aidlMetadata->metadata.assign(data_ptr,
774                                 data_ptr + get_camera_metadata_size(metadata));
775   metadataHelper.unlock(metadata);
776 
777   return aidlMetadata;
778 }
779 
getJpegQuality(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)780 std::optional<int32_t> getJpegQuality(
781     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
782   auto metadata =
783       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
784 
785   camera_metadata_ro_entry_t entry;
786   if (find_camera_metadata_ro_entry(metadata, ANDROID_JPEG_QUALITY, &entry) !=
787       OK) {
788     return std::nullopt;
789   }
790 
791   return *entry.data.i32;
792 }
793 
getJpegOrientation(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)794 int32_t getJpegOrientation(
795     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
796   auto metadata =
797       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
798 
799   camera_metadata_ro_entry_t entry;
800   if (find_camera_metadata_ro_entry(metadata, ANDROID_JPEG_ORIENTATION,
801                                     &entry) != OK) {
802     return 0;
803   }
804 
805   return *entry.data.i32;
806 }
807 
getJpegThumbnailSize(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)808 std::optional<Resolution> getJpegThumbnailSize(
809     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
810   auto metadata =
811       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
812 
813   camera_metadata_ro_entry_t entry;
814   if (find_camera_metadata_ro_entry(metadata, ANDROID_JPEG_THUMBNAIL_SIZE,
815                                     &entry) != OK) {
816     return std::nullopt;
817   }
818 
819   return Resolution(entry.data.i32[0], entry.data.i32[1]);
820 }
821 
getJpegThumbnailQuality(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)822 std::optional<int32_t> getJpegThumbnailQuality(
823     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
824   auto metadata =
825       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
826 
827   camera_metadata_ro_entry_t entry;
828   if (find_camera_metadata_ro_entry(metadata, ANDROID_JPEG_THUMBNAIL_QUALITY,
829                                     &entry) != OK) {
830     return std::nullopt;
831   }
832 
833   return *entry.data.i32;
834 }
835 
getJpegAvailableThumbnailSizes(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)836 std::vector<Resolution> getJpegAvailableThumbnailSizes(
837     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
838   auto metadata =
839       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
840 
841   camera_metadata_ro_entry_t entry;
842   if (find_camera_metadata_ro_entry(
843           metadata, ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, &entry) != OK) {
844     return {};
845   }
846 
847   std::vector<Resolution> thumbnailSizes;
848   thumbnailSizes.reserve(entry.count / 2);
849   for (int i = 0; i < entry.count; i += 2) {
850     thumbnailSizes.emplace_back(entry.data.i32[i], entry.data.i32[i + 1]);
851   }
852   return thumbnailSizes;
853 }
854 
getFpsRange(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)855 std::optional<FpsRange> getFpsRange(
856     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
857   auto metadata =
858       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
859 
860   camera_metadata_ro_entry_t entry;
861   if (find_camera_metadata_ro_entry(
862           metadata, ANDROID_CONTROL_AE_TARGET_FPS_RANGE, &entry) != OK ||
863       entry.count != 2) {
864     return {};
865   }
866 
867   FpsRange range{.minFps = entry.data.i32[0], .maxFps = entry.data.i32[1]};
868   return range;
869 }
870 
871 std::optional<camera_metadata_enum_android_control_capture_intent>
getCaptureIntent(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)872 getCaptureIntent(const aidl::android::hardware::camera::device::CameraMetadata&
873                      cameraMetadata) {
874   auto metadata =
875       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
876 
877   camera_metadata_ro_entry_t entry;
878   if (find_camera_metadata_ro_entry(metadata, ANDROID_CONTROL_CAPTURE_INTENT,
879                                     &entry) != OK) {
880     return {};
881   }
882 
883   return static_cast<camera_metadata_enum_android_control_capture_intent>(
884       entry.data.u8[0]);
885 }
886 
getGpsCoordinates(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)887 std::optional<GpsCoordinates> getGpsCoordinates(
888     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
889   auto metadata =
890       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
891 
892   camera_metadata_ro_entry_t entry;
893   if (find_camera_metadata_ro_entry(metadata, ANDROID_JPEG_GPS_COORDINATES,
894                                     &entry) != OK) {
895     return std::nullopt;
896   }
897 
898   GpsCoordinates coordinates{.latitude = entry.data.d[0],
899                              .longitude = entry.data.d[1],
900                              .altitude = entry.data.d[2]};
901 
902   if (find_camera_metadata_ro_entry(metadata, ANDROID_JPEG_GPS_TIMESTAMP,
903                                     &entry) == OK) {
904     coordinates.timestamp = entry.data.i64[0];
905   }
906 
907   // According to types.hal, the string describing the GPS processing method has
908   // a 32 characters size
909   static constexpr float kGpsProviderStringLength = 32;
910   if (find_camera_metadata_ro_entry(
911           metadata, ANDROID_JPEG_GPS_PROCESSING_METHOD, &entry) == OK) {
912     coordinates.provider.assign(
913         reinterpret_cast<const char*>(entry.data.u8),
914         std::min(entry.count, static_cast<size_t>(kGpsProviderStringLength)));
915   }
916 
917   return coordinates;
918 }
919 
getLensFacing(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)920 std::optional<camera_metadata_enum_android_lens_facing> getLensFacing(
921     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
922   auto metadata =
923       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
924 
925   camera_metadata_ro_entry_t entry;
926   if (find_camera_metadata_ro_entry(metadata, ANDROID_LENS_FACING, &entry) !=
927       OK) {
928     return std::nullopt;
929   }
930 
931   return static_cast<camera_metadata_enum_android_lens_facing>(entry.data.u8[0]);
932 }
933 
934 std::optional<camera_metadata_enum_android_control_ae_precapture_trigger>
getPrecaptureTrigger(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)935 getPrecaptureTrigger(
936     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
937   auto metadata =
938       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
939 
940   camera_metadata_ro_entry_t entry;
941   if (find_camera_metadata_ro_entry(
942           metadata, ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &entry) != OK) {
943     return std::nullopt;
944   }
945 
946   return static_cast<camera_metadata_enum_android_control_ae_precapture_trigger>(
947       entry.data.u8[0]);
948 }
949 
getDeviceId(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)950 std::optional<int32_t> getDeviceId(
951     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
952   auto metadata =
953       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
954 
955   camera_metadata_ro_entry_t entry;
956   if (find_camera_metadata_ro_entry(metadata, ANDROID_INFO_DEVICE_ID, &entry) !=
957       OK) {
958     return std::nullopt;
959   }
960 
961   return static_cast<int32_t>(entry.data.i32[0]);
962 }
963 
getSensorOrientation(const aidl::android::hardware::camera::device::CameraMetadata & cameraMetadata)964 std::optional<int32_t> getSensorOrientation(
965     const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
966   auto metadata =
967       reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());
968 
969   camera_metadata_ro_entry_t entry;
970   if (find_camera_metadata_ro_entry(metadata, ANDROID_SENSOR_ORIENTATION,
971                                     &entry) != OK) {
972     return std::nullopt;
973   }
974 
975   return static_cast<int32_t>(entry.data.i32[0]);
976 }
977 
978 }  // namespace virtualcamera
979 }  // namespace companion
980 }  // namespace android
981