CameraMetadata.h revision 3b53bc9b41c262d22f094406e3751bc5a41ef2ef
1/*
2 * Copyright (C) 2012 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_CLIENT_CAMERA2_CAMERAMETADATA_CPP
18#define ANDROID_CLIENT_CAMERA2_CAMERAMETADATA_CPP
19
20#include "system/camera_metadata.h"
21#include <utils/String8.h>
22#include <utils/Vector.h>
23
24namespace android {
25
26/**
27 * A convenience wrapper around the C-based camera_metadata_t library.
28 */
29class CameraMetadata {
30  public:
31    /** Creates an empty object; best used when expecting to acquire contents
32     * from elsewhere */
33    CameraMetadata();
34    /** Creates an object with space for entryCapacity entries, with
35     * dataCapacity extra storage */
36    CameraMetadata(size_t entryCapacity, size_t dataCapacity = 10);
37
38    ~CameraMetadata();
39
40    /** Takes ownership of passed-in buffer */
41    CameraMetadata(camera_metadata_t *buffer);
42    /** Clones the metadata */
43    CameraMetadata(const CameraMetadata &other);
44
45    /**
46     * Assignment clones metadata buffer.
47     */
48    CameraMetadata &operator=(const CameraMetadata &other);
49    CameraMetadata &operator=(const camera_metadata_t *buffer);
50
51    /**
52     * Get reference to the underlying metadata buffer. Ownership remains with
53     * the CameraMetadata object, but non-const CameraMetadata methods will not
54     * work until unlock() is called. Note that the lock has nothing to do with
55     * thread-safety, it simply prevents the camera_metadata_t pointer returned
56     * here from being accidentally invalidated by CameraMetadata operations.
57     */
58    const camera_metadata_t* getAndLock();
59
60    /**
61     * Unlock the CameraMetadata for use again. After this unlock, the pointer
62     * given from getAndLock() may no longer be used. The pointer passed out
63     * from getAndLock must be provided to guarantee that the right object is
64     * being unlocked.
65     */
66    status_t unlock(const camera_metadata_t *buffer);
67
68    /**
69     * Release a raw metadata buffer to the caller. After this call,
70     * CameraMetadata no longer references the buffer, and the caller takes
71     * responsibility for freeing the raw metadata buffer (using
72     * free_camera_metadata()), or for handing it to another CameraMetadata
73     * instance.
74     */
75    camera_metadata_t* release();
76
77    /**
78     * Clear the metadata buffer and free all storage used by it
79     */
80    void clear();
81
82    /**
83     * Acquire a raw metadata buffer from the caller. After this call,
84     * the caller no longer owns the raw buffer, and must not free or manipulate it.
85     * If CameraMetadata already contains metadata, it is freed.
86     */
87    void acquire(camera_metadata_t* buffer);
88
89    /**
90     * Acquires raw buffer from other CameraMetadata object. After the call, the argument
91     * object no longer has any metadata.
92     */
93    void acquire(CameraMetadata &other);
94
95    /**
96     * Append metadata from another CameraMetadata object.
97     */
98    status_t append(const CameraMetadata &other);
99
100    /**
101     * Number of metadata entries.
102     */
103    size_t entryCount() const;
104
105    /**
106     * Is the buffer empty (no entires)
107     */
108    bool isEmpty() const;
109
110    /**
111     * Sort metadata buffer for faster find
112     */
113    status_t sort();
114
115    /**
116     * Update metadata entry. Will create entry if it doesn't exist already, and
117     * will reallocate the buffer if insufficient space exists. Overloaded for
118     * the various types of valid data.
119     */
120    status_t update(uint32_t tag,
121            const uint8_t *data, size_t data_count);
122    status_t update(uint32_t tag,
123            const int32_t *data, size_t data_count);
124    status_t update(uint32_t tag,
125            const float *data, size_t data_count);
126    status_t update(uint32_t tag,
127            const int64_t *data, size_t data_count);
128    status_t update(uint32_t tag,
129            const double *data, size_t data_count);
130    status_t update(uint32_t tag,
131            const camera_metadata_rational_t *data, size_t data_count);
132    status_t update(uint32_t tag,
133            const String8 &string);
134
135    template<typename T>
136    status_t update(uint32_t tag, Vector<T> data) {
137        return update(tag, data.array(), data.size());
138    }
139
140    /**
141     * Check if a metadata entry exists for a given tag id
142     *
143     */
144    bool exists(uint32_t tag) const;
145
146    /**
147     * Get metadata entry by tag id
148     */
149    camera_metadata_entry find(uint32_t tag);
150
151    /**
152     * Get metadata entry by tag id, with no editing
153     */
154    camera_metadata_ro_entry find(uint32_t tag) const;
155
156    /**
157     * Delete metadata entry by tag
158     */
159    status_t erase(uint32_t tag);
160
161    /**
162     * Dump contents into FD for debugging. The verbosity levels are
163     * 0: Tag entry information only, no data values
164     * 1: Level 0 plus at most 16 data values per entry
165     * 2: All information
166     *
167     * The indentation parameter sets the number of spaces to add to the start
168     * each line of output.
169     */
170    void dump(int fd, int verbosity = 1, int indentation = 0) const;
171
172  private:
173    camera_metadata_t *mBuffer;
174    bool               mLocked;
175
176    /**
177     * Check if tag has a given type
178     */
179    status_t checkType(uint32_t tag, uint8_t expectedType);
180
181    /**
182     * Base update entry method
183     */
184    status_t updateImpl(uint32_t tag, const void *data, size_t data_count);
185
186    /**
187     * Resize metadata buffer if needed by reallocating it and copying it over.
188     */
189    status_t resizeIfNeeded(size_t extraEntries, size_t extraData);
190
191};
192
193}; // namespace android
194
195#endif
196