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      * Sort metadata buffer for faster find
132      */
133     status_t sort();
134 
135     /**
136      * Update metadata entry. Will create entry if it doesn't exist already, and
137      * will reallocate the buffer if insufficient space exists. Overloaded for
138      * the various types of valid data.
139      */
140     status_t update(uint32_t tag,
141             const uint8_t *data, size_t data_count);
142     status_t update(uint32_t tag,
143             const int32_t *data, size_t data_count);
144     status_t update(uint32_t tag,
145             const float *data, size_t data_count);
146     status_t update(uint32_t tag,
147             const int64_t *data, size_t data_count);
148     status_t update(uint32_t tag,
149             const double *data, size_t data_count);
150     status_t update(uint32_t tag,
151             const camera_metadata_rational_t *data, size_t data_count);
152     status_t update(uint32_t tag,
153             const String8 &string);
154     status_t update(const camera_metadata_ro_entry &entry);
155 
156 
157     template<typename T>
update(uint32_t tag,Vector<T> data)158     status_t update(uint32_t tag, Vector<T> data) {
159         return update(tag, data.array(), data.size());
160     }
161 
162     /**
163      * Check if a metadata entry exists for a given tag id
164      *
165      */
166     bool exists(uint32_t tag) const;
167 
168     /**
169      * Get metadata entry by tag id
170      */
171     camera_metadata_entry find(uint32_t tag);
172 
173     /**
174      * Get metadata entry by tag id, with no editing
175      */
176     camera_metadata_ro_entry find(uint32_t tag) const;
177 
178     /**
179      * Delete metadata entry by tag
180      */
181     status_t erase(uint32_t tag);
182 
183     /**
184      * Remove metadata entries that need additional permissions.
185      */
186     status_t removePermissionEntries(metadata_vendor_id_t vendorId,
187             std::vector<int32_t> *tagsRemoved /*out*/);
188 
189     /**
190      * Swap the underlying camera metadata between this and the other
191      * metadata object.
192      */
193     void swap(CameraMetadata &other);
194 
195     /**
196      * Dump contents into FD for debugging. The verbosity levels are
197      * 0: Tag entry information only, no data values
198      * 1: Level 0 plus at most 16 data values per entry
199      * 2: All information
200      *
201      * The indentation parameter sets the number of spaces to add to the start
202      * each line of output.
203      */
204     void dump(int fd, int verbosity = 1, int indentation = 0) const;
205 
206     /**
207      * Serialization over Binder
208      */
209 
210     // Metadata object is unchanged when reading from parcel fails.
211     virtual status_t readFromParcel(const Parcel *parcel) override;
212     virtual status_t writeToParcel(Parcel *parcel) const override;
213 
214     /**
215       * Caller becomes the owner of the new metadata
216       * 'const Parcel' doesnt prevent us from calling the read functions.
217       *  which is interesting since it changes the internal state
218       *
219       * NULL can be returned when no metadata was sent, OR if there was an issue
220       * unpacking the serialized data (i.e. bad parcel or invalid structure).
221       */
222     static status_t readFromParcel(const Parcel &parcel,
223                                    camera_metadata_t** out);
224     /**
225       * Caller retains ownership of metadata
226       * - Write 2 (int32 + blob) args in the current position
227       */
228     static status_t writeToParcel(Parcel &parcel,
229                                   const camera_metadata_t* metadata);
230 
231     /**
232      * Find tag id for a given tag name, also checking vendor tags if available.
233      * On success, returns OK and writes the tag id into tag.
234      *
235      * This is a slow method.
236      */
237     static status_t getTagFromName(const char *name,
238             const VendorTagDescriptor* vTags, uint32_t *tag);
239 
240   private:
241     camera_metadata_t *mBuffer;
242     mutable bool       mLocked;
243 
244     /**
245      * Check if tag has a given type
246      */
247     status_t checkType(uint32_t tag, uint8_t expectedType);
248 
249     /**
250      * Base update entry method
251      */
252     status_t updateImpl(uint32_t tag, const void *data, size_t data_count);
253 
254     /**
255      * Resize metadata buffer if needed by reallocating it and copying it over.
256      */
257     status_t resizeIfNeeded(size_t extraEntries, size_t extraData);
258 
259 };
260 
261 namespace hardware {
262 namespace camera2 {
263 namespace impl {
264 using ::android::CameraMetadata;
265 typedef CameraMetadata CameraMetadataNative;
266 }
267 }
268 }
269 
270 } // namespace android
271 
272 #endif
273