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