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