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