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 
22 #include <utils/String8.h>
23 #include <utils/Vector.h>
24 #include <binder/Parcelable.h>
25 #include <camera/VendorTagDescriptor.h>
26 
27 namespace android {
28 
29 class VendorTagDescriptor;
30 
31 /**
32  * A convenience wrapper around the C-based camera_metadata_t library.
33  */
34 class CameraMetadata: public Parcelable {
35   public:
36     /** Creates an empty object; best used when expecting to acquire contents
37      * from elsewhere */
38     CameraMetadata();
39     /** Creates an object with space for entryCapacity entries, with
40      * dataCapacity extra storage */
41     CameraMetadata(size_t entryCapacity, size_t dataCapacity = 10);
42 
43     /**
44      * Move constructor, acquires other's metadata buffer
45      */
46     CameraMetadata(CameraMetadata &&other);
47 
48     ~CameraMetadata();
49 
50     /** Takes ownership of passed-in buffer */
51     CameraMetadata(camera_metadata_t *buffer);
52     /** Clones the metadata */
53     CameraMetadata(const CameraMetadata &other);
54 
55     /**
56      * Assignment clones metadata buffer.
57      */
58     CameraMetadata &operator=(const CameraMetadata &other);
59     CameraMetadata &operator=(const camera_metadata_t *buffer);
60 
61     /**
62      * Move assignment operator, acquires other's metadata buffer
63      */
64     CameraMetadata &operator=(CameraMetadata &&other);
65 
66     /**
67      * Get reference to the underlying metadata buffer. Ownership remains with
68      * the CameraMetadata object, but non-const CameraMetadata methods will not
69      * work until unlock() is called. Note that the lock has nothing to do with
70      * thread-safety, it simply prevents the camera_metadata_t pointer returned
71      * here from being accidentally invalidated by CameraMetadata operations.
72      */
73     const camera_metadata_t* getAndLock() const;
74 
75     /**
76      * Unlock the CameraMetadata for use again. After this unlock, the pointer
77      * given from getAndLock() may no longer be used. The pointer passed out
78      * from getAndLock must be provided to guarantee that the right object is
79      * being unlocked.
80      */
81     status_t unlock(const camera_metadata_t *buffer) const;
82 
83     /**
84      * Release a raw metadata buffer to the caller. After this call,
85      * CameraMetadata no longer references the buffer, and the caller takes
86      * responsibility for freeing the raw metadata buffer (using
87      * free_camera_metadata()), or for handing it to another CameraMetadata
88      * instance.
89      */
90     camera_metadata_t* release();
91 
92     /**
93      * Clear the metadata buffer and free all storage used by it
94      */
95     void clear();
96 
97     /**
98      * Acquire a raw metadata buffer from the caller. After this call,
99      * the caller no longer owns the raw buffer, and must not free or manipulate it.
100      * If CameraMetadata already contains metadata, it is freed.
101      */
102     void acquire(camera_metadata_t* buffer);
103 
104     /**
105      * Acquires raw buffer from other CameraMetadata object. After the call, the argument
106      * object no longer has any metadata.
107      */
108     void acquire(CameraMetadata &other);
109 
110     /**
111      * Append metadata from another CameraMetadata object.
112      */
113     status_t append(const CameraMetadata &other);
114 
115     /**
116      * Append metadata from a raw camera_metadata buffer
117      */
118     status_t append(const camera_metadata* other);
119 
120     /**
121      * Number of metadata entries.
122      */
123     size_t entryCount() const;
124 
125     /**
126      * Is the buffer empty (no entires)
127      */
128     bool isEmpty() const;
129 
130     /**
131      * Return the allocated camera metadata buffer size in bytes.
132      */
133     size_t bufferSize() const;
134 
135     /**
136      * Sort metadata buffer for faster find
137      */
138     status_t sort();
139 
140     /**
141      * Update metadata entry. Will create entry if it doesn't exist already, and
142      * will reallocate the buffer if insufficient space exists. Overloaded for
143      * the various types of valid data.
144      */
145     status_t update(uint32_t tag,
146             const uint8_t *data, size_t data_count);
147     status_t update(uint32_t tag,
148             const int32_t *data, size_t data_count);
149     status_t update(uint32_t tag,
150             const float *data, size_t data_count);
151     status_t update(uint32_t tag,
152             const int64_t *data, size_t data_count);
153     status_t update(uint32_t tag,
154             const double *data, size_t data_count);
155     status_t update(uint32_t tag,
156             const camera_metadata_rational_t *data, size_t data_count);
157     status_t update(uint32_t tag,
158             const String8 &string);
159     status_t update(const camera_metadata_ro_entry &entry);
160 
161 
162     template<typename T>
update(uint32_t tag,Vector<T> data)163     status_t update(uint32_t tag, Vector<T> data) {
164         return update(tag, data.array(), data.size());
165     }
166 
167     /**
168      * Check if a metadata entry exists for a given tag id
169      *
170      */
171     bool exists(uint32_t tag) const;
172 
173     /**
174      * Get metadata entry by tag id
175      */
176     camera_metadata_entry find(uint32_t tag);
177 
178     /**
179      * Get metadata entry by tag id, with no editing
180      */
181     camera_metadata_ro_entry find(uint32_t tag) const;
182 
183     /**
184      * Delete metadata entry by tag
185      */
186     status_t erase(uint32_t tag);
187 
188     /**
189      * Remove metadata entries that need additional permissions.
190      */
191     status_t removePermissionEntries(metadata_vendor_id_t vendorId,
192             std::vector<int32_t> *tagsRemoved /*out*/);
193 
194     /**
195      * Swap the underlying camera metadata between this and the other
196      * metadata object.
197      */
198     void swap(CameraMetadata &other);
199 
200     /**
201      * Dump contents into FD for debugging. The verbosity levels are
202      * 0: Tag entry information only, no data values
203      * 1: Level 0 plus at most 16 data values per entry
204      * 2: All information
205      *
206      * The indentation parameter sets the number of spaces to add to the start
207      * each line of output.
208      */
209     void dump(int fd, int verbosity = 1, int indentation = 0) const;
210 
211     /**
212      * Serialization over Binder
213      */
214 
215     // Metadata object is unchanged when reading from parcel fails.
216     virtual status_t readFromParcel(const Parcel *parcel) override;
217     virtual status_t writeToParcel(Parcel *parcel) const override;
218 
219     /**
220       * Caller becomes the owner of the new metadata
221       * 'const Parcel' doesnt prevent us from calling the read functions.
222       *  which is interesting since it changes the internal state
223       *
224       * NULL can be returned when no metadata was sent, OR if there was an issue
225       * unpacking the serialized data (i.e. bad parcel or invalid structure).
226       */
227     static status_t readFromParcel(const Parcel &parcel,
228                                    camera_metadata_t** out);
229     /**
230       * Caller retains ownership of metadata
231       * - Write 2 (int32 + blob) args in the current position
232       */
233     static status_t writeToParcel(Parcel &parcel,
234                                   const camera_metadata_t* metadata);
235 
236     /**
237      * Find tag id for a given tag name, also checking vendor tags if available.
238      * On success, returns OK and writes the tag id into tag.
239      *
240      * This is a slow method.
241      */
242     static status_t getTagFromName(const char *name,
243             const VendorTagDescriptor* vTags, uint32_t *tag);
244 
245     /**
246      * Return the current vendor tag id associated with this metadata.
247      */
248     metadata_vendor_id_t getVendorId() const;
249 
250   private:
251     camera_metadata_t *mBuffer;
252     mutable bool       mLocked;
253 
254     /**
255      * Check if tag has a given type
256      */
257     status_t checkType(uint32_t tag, uint8_t expectedType);
258 
259     /**
260      * Base update entry method
261      */
262     status_t updateImpl(uint32_t tag, const void *data, size_t data_count);
263 
264     /**
265      * Resize metadata buffer if needed by reallocating it and copying it over.
266      */
267     status_t resizeIfNeeded(size_t extraEntries, size_t extraData);
268 
269 };
270 
271 namespace hardware {
272 namespace camera2 {
273 namespace impl {
274 using ::android::CameraMetadata;
275 typedef CameraMetadata CameraMetadataNative;
276 }
277 }
278 }
279 
280 } // namespace android
281 
282 #endif
283