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