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