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 
initialize(metadata_vendor_id_t id)53     void initialize(metadata_vendor_id_t id) { mVendorTagId = id; }
54 
55     // Parse tag name list (comma-separated) and if valid, enable monitoring
56     // If invalid, do nothing.
57     // Recognizes "3a" as a shortcut for enabling tracking 3A state, mode, and
58     // triggers
59     void parseTagsToMonitor(String8 tagNames);
60 
61     // Disable monitoring; does not clear the event log
62     void disableMonitoring();
63 
64     // Scan through the metadata and update the monitoring information
65     void monitorMetadata(eventSource source, int64_t frameNumber,
66             nsecs_t timestamp, const CameraMetadata& metadata,
67             const std::unordered_map<std::string, CameraMetadata>& physicalMetadata);
68 
69     // Dump current event log to the provided fd
70     void dumpMonitoredMetadata(int fd);
71 
72   private:
73 
74     static void printData(int fd, const uint8_t *data_ptr, uint32_t tag,
75             int type, int count, int indentation);
76 
77     void monitorSingleMetadata(TagMonitor::eventSource source, int64_t frameNumber,
78             nsecs_t timestamp, const std::string& cameraId, uint32_t tag,
79             const CameraMetadata& metadata);
80 
81     std::atomic<bool> mMonitoringEnabled;
82     std::mutex mMonitorMutex;
83 
84     // Current tags to monitor and record changes to
85     std::vector<uint32_t> mMonitoredTagList;
86 
87     // Latest-seen values of tracked tags
88     CameraMetadata mLastMonitoredRequestValues;
89     CameraMetadata mLastMonitoredResultValues;
90 
91     std::unordered_map<std::string, CameraMetadata> mLastMonitoredPhysicalRequestKeys;
92     std::unordered_map<std::string, CameraMetadata> mLastMonitoredPhysicalResultKeys;
93 
94     /**
95      * A monitoring event
96      * Stores a new metadata field value and the timestamp at which it changed.
97      * Copies the source metadata value array and frees it on destruct.
98      */
99     struct MonitorEvent {
100         template<typename T>
101         MonitorEvent(eventSource src, uint32_t frameNumber, nsecs_t timestamp,
102                 const T &newValue, const std::string& cameraId);
103         ~MonitorEvent();
104 
105         eventSource source;
106         uint32_t frameNumber;
107         nsecs_t timestamp;
108         uint32_t tag;
109         uint8_t type;
110         std::vector<uint8_t> newData;
111         std::string cameraId;
112     };
113 
114     // A ring buffer for tracking the last kMaxMonitorEvents metadata changes
115     static const int kMaxMonitorEvents = 100;
116     RingBuffer<MonitorEvent> mMonitoringEvents;
117 
118     // 3A fields to use with the "3a" option
119     static const char *k3aTags;
120     metadata_vendor_id_t mVendorTagId;
121 };
122 
123 } // namespace android
124 
125 #endif
126