1 /* 2 * Copyright (C) 2016 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 #ifndef ANDROID_SERVERS_CAMERA_TAGMONITOR_H 18 #define ANDROID_SERVERS_CAMERA_TAGMONITOR_H 19 20 #include <vector> 21 #include <atomic> 22 #include <mutex> 23 #include <unordered_map> 24 25 #include <utils/RefBase.h> 26 #include <utils/String8.h> 27 #include <utils/Timers.h> 28 29 #include <media/RingBuffer.h> 30 #include <system/camera_metadata.h> 31 #include <system/camera_vendor_tags.h> 32 #include <camera/CameraMetadata.h> 33 34 namespace android { 35 36 /** 37 * A monitor for camera metadata values. 38 * Tracks changes to specified metadata values over time, keeping a circular 39 * buffer log that can be dumped at will. */ 40 class TagMonitor { 41 public: 42 43 // Monitor argument 44 static const String16 kMonitorOption; 45 46 enum eventSource { 47 REQUEST, 48 RESULT 49 }; 50 51 TagMonitor(); 52 53 TagMonitor(const TagMonitor& other); 54 initialize(metadata_vendor_id_t id)55 void initialize(metadata_vendor_id_t id) { mVendorTagId = id; } 56 57 // Parse tag name list (comma-separated) and if valid, enable monitoring 58 // If invalid, do nothing. 59 // Recognizes "3a" as a shortcut for enabling tracking 3A state, mode, and 60 // triggers 61 void parseTagsToMonitor(String8 tagNames); 62 63 // Disable monitoring; does not clear the event log 64 void disableMonitoring(); 65 66 // Scan through the metadata and update the monitoring information 67 void monitorMetadata(eventSource source, int64_t frameNumber, 68 nsecs_t timestamp, const CameraMetadata& metadata, 69 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata); 70 71 // Dump current event log to the provided fd 72 void dumpMonitoredMetadata(int fd); 73 74 private: 75 76 static void printData(int fd, const uint8_t *data_ptr, uint32_t tag, 77 int type, int count, int indentation); 78 79 void monitorSingleMetadata(TagMonitor::eventSource source, int64_t frameNumber, 80 nsecs_t timestamp, const std::string& cameraId, uint32_t tag, 81 const CameraMetadata& metadata); 82 83 std::atomic<bool> mMonitoringEnabled; 84 std::mutex mMonitorMutex; 85 86 // Current tags to monitor and record changes to 87 std::vector<uint32_t> mMonitoredTagList; 88 89 // Latest-seen values of tracked tags 90 CameraMetadata mLastMonitoredRequestValues; 91 CameraMetadata mLastMonitoredResultValues; 92 93 std::unordered_map<std::string, CameraMetadata> mLastMonitoredPhysicalRequestKeys; 94 std::unordered_map<std::string, CameraMetadata> mLastMonitoredPhysicalResultKeys; 95 96 /** 97 * A monitoring event 98 * Stores a new metadata field value and the timestamp at which it changed. 99 * Copies the source metadata value array and frees it on destruct. 100 */ 101 struct MonitorEvent { 102 template<typename T> 103 MonitorEvent(eventSource src, uint32_t frameNumber, nsecs_t timestamp, 104 const T &newValue, const std::string& cameraId); 105 ~MonitorEvent(); 106 107 eventSource source; 108 uint32_t frameNumber; 109 nsecs_t timestamp; 110 uint32_t tag; 111 uint8_t type; 112 std::vector<uint8_t> newData; 113 std::string cameraId; 114 }; 115 116 // A ring buffer for tracking the last kMaxMonitorEvents metadata changes 117 static const int kMaxMonitorEvents = 100; 118 RingBuffer<MonitorEvent> mMonitoringEvents; 119 120 // 3A fields to use with the "3a" option 121 static const char *k3aTags; 122 metadata_vendor_id_t mVendorTagId; 123 }; 124 125 } // namespace android 126 127 #endif 128