1 /*
2  * Copyright (C) 2014 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_HARDWARE_CAMERA_PARAMETERS2_H
18 #define ANDROID_HARDWARE_CAMERA_PARAMETERS2_H
19 
20 #include <utils/Vector.h>
21 #include <utils/String8.h>
22 #include "CameraParameters.h"
23 
24 namespace android {
25 
26 /**
27  * A copy of CameraParameters plus ABI-breaking changes. Needed
28  * because some camera HALs directly link to CameraParameters and cannot
29  * tolerate an ABI change.
30  */
31 class CameraParameters2
32 {
33 public:
34     CameraParameters2();
CameraParameters2(const String8 & params)35     CameraParameters2(const String8 &params) { unflatten(params); }
36     ~CameraParameters2();
37 
38     String8 flatten() const;
39     void unflatten(const String8 &params);
40 
41     void set(const char *key, const char *value);
42     void set(const char *key, int value);
43     void setFloat(const char *key, float value);
44     // Look up string value by key.
45     // -- The string remains valid until the next set/remove of the same key,
46     //    or until the map gets cleared.
47     const char *get(const char *key) const;
48     int getInt(const char *key) const;
49     float getFloat(const char *key) const;
50 
51     // Compare the order that key1 was set vs the order that key2 was set.
52     //
53     // Sets the order parameter to an integer less than, equal to, or greater
54     // than zero if key1's set order was respectively, to be less than, to
55     // match, or to be greater than key2's set order.
56     //
57     // Error codes:
58     //  * NAME_NOT_FOUND - if either key has not been set previously
59     //  * BAD_VALUE - if any of the parameters are NULL
60     status_t compareSetOrder(const char *key1, const char *key2,
61             /*out*/
62             int *order) const;
63 
64     void remove(const char *key);
65 
66     void setPreviewSize(int width, int height);
67     void getPreviewSize(int *width, int *height) const;
68     void getSupportedPreviewSizes(Vector<Size> &sizes) const;
69 
70     // Set the dimensions in pixels to the given width and height
71     // for video frames. The given width and height must be one
72     // of the supported dimensions returned from
73     // getSupportedVideoSizes(). Must not be called if
74     // getSupportedVideoSizes() returns an empty Vector of Size.
75     void setVideoSize(int width, int height);
76     // Retrieve the current dimensions (width and height)
77     // in pixels for video frames, which must be one of the
78     // supported dimensions returned from getSupportedVideoSizes().
79     // Must not be called if getSupportedVideoSizes() returns an
80     // empty Vector of Size.
81     void getVideoSize(int *width, int *height) const;
82     // Retrieve a Vector of supported dimensions (width and height)
83     // in pixels for video frames. If sizes returned from the method
84     // is empty, the camera does not support calls to setVideoSize()
85     // or getVideoSize(). In adddition, it also indicates that
86     // the camera only has a single output, and does not have
87     // separate output for video frames and preview frame.
88     void getSupportedVideoSizes(Vector<Size> &sizes) const;
89     // Retrieve the preferred preview size (width and height) in pixels
90     // for video recording. The given width and height must be one of
91     // supported preview sizes returned from getSupportedPreviewSizes().
92     // Must not be called if getSupportedVideoSizes() returns an empty
93     // Vector of Size. If getSupportedVideoSizes() returns an empty
94     // Vector of Size, the width and height returned from this method
95     // is invalid, and is "-1x-1".
96     void getPreferredPreviewSizeForVideo(int *width, int *height) const;
97 
98     void setPreviewFrameRate(int fps);
99     int getPreviewFrameRate() const;
100     void getPreviewFpsRange(int *min_fps, int *max_fps) const;
101     void setPreviewFpsRange(int min_fps, int max_fps);
102     void setPreviewFormat(const char *format);
103     const char *getPreviewFormat() const;
104     void setPictureSize(int width, int height);
105     void getPictureSize(int *width, int *height) const;
106     void getSupportedPictureSizes(Vector<Size> &sizes) const;
107     void setPictureFormat(const char *format);
108     const char *getPictureFormat() const;
109 
110     void dump() const;
111     status_t dump(int fd, const Vector<String16>& args) const;
112 
113 private:
114 
115     // Quick and dirty map that maintains insertion order
116     template <typename KeyT, typename ValueT>
117     struct OrderedKeyedVector {
118 
addOrderedKeyedVector119         ssize_t add(const KeyT& key, const ValueT& value) {
120             return mList.add(Pair(key, value));
121         }
122 
sizeOrderedKeyedVector123         size_t size() const {
124             return mList.size();
125         }
126 
keyAtOrderedKeyedVector127         const KeyT& keyAt(size_t idx) const {
128             return mList[idx].mKey;
129         }
130 
valueAtOrderedKeyedVector131         const ValueT& valueAt(size_t idx) const {
132             return mList[idx].mValue;
133         }
134 
valueForOrderedKeyedVector135         const ValueT& valueFor(const KeyT& key) const {
136             ssize_t i = indexOfKey(key);
137             LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__);
138 
139             return valueAt(i);
140         }
141 
indexOfKeyOrderedKeyedVector142         ssize_t indexOfKey(const KeyT& key) const {
143                 size_t vectorIdx = 0;
144                 for (; vectorIdx < mList.size(); ++vectorIdx) {
145                     if (mList[vectorIdx].mKey == key) {
146                         return (ssize_t) vectorIdx;
147                     }
148                 }
149 
150                 return NAME_NOT_FOUND;
151         }
152 
removeItemOrderedKeyedVector153         ssize_t removeItem(const KeyT& key) {
154             size_t vectorIdx = (size_t) indexOfKey(key);
155 
156             if (vectorIdx < 0) {
157                 return vectorIdx;
158             }
159 
160             return mList.removeAt(vectorIdx);
161         }
162 
clearOrderedKeyedVector163         void clear() {
164             mList.clear();
165         }
166 
167         // Same as removing and re-adding. The key's index changes to max.
replaceValueForOrderedKeyedVector168         ssize_t replaceValueFor(const KeyT& key, const ValueT& value) {
169             removeItem(key);
170             return add(key, value);
171         }
172 
173     private:
174 
175         struct Pair {
PairOrderedKeyedVector::Pair176             Pair() : mKey(), mValue() {}
PairOrderedKeyedVector::Pair177             Pair(const KeyT& key, const ValueT& value) :
178                     mKey(key),
179                     mValue(value) {}
180             KeyT   mKey;
181             ValueT mValue;
182         };
183 
184         Vector<Pair> mList;
185     };
186 
187     /**
188      * Order matters: Keys that are set() later are stored later in the map.
189      *
190      * If two keys have meaning that conflict, then the later-set key
191      * wins.
192      *
193      * For example, preview FPS and preview FPS range conflict since only
194      * we only want to use the FPS range if that's the last thing that was set.
195      * So in that case, only use preview FPS range if it was set later than
196      * the preview FPS.
197      */
198     OrderedKeyedVector<String8,String8>    mMap;
199 };
200 
201 }; // namespace android
202 
203 #endif
204