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 #define LOG_TAG "Camera-CaptureResult"
18 #include <utils/Log.h>
19 
20 #include <camera/CaptureResult.h>
21 #include <binder/Parcel.h>
22 
23 namespace android {
24 
isValid()25 bool CaptureResultExtras::isValid() {
26     return requestId >= 0;
27 }
28 
readFromParcel(const android::Parcel * parcel)29 status_t CaptureResultExtras::readFromParcel(const android::Parcel *parcel) {
30     if (parcel == NULL) {
31         ALOGE("%s: Null parcel", __FUNCTION__);
32         return BAD_VALUE;
33     }
34 
35     parcel->readInt32(&requestId);
36     parcel->readInt32(&burstId);
37     parcel->readInt32(&afTriggerId);
38     parcel->readInt32(&precaptureTriggerId);
39     parcel->readInt64(&frameNumber);
40     parcel->readInt32(&partialResultCount);
41     parcel->readInt32(&errorStreamId);
42     auto physicalCameraIdPresent = parcel->readBool();
43     if (physicalCameraIdPresent) {
44         String16 cameraId;
45         status_t res = OK;
46         if ((res = parcel->readString16(&cameraId)) != OK) {
47             ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
48             return res;
49         }
50         errorPhysicalCameraId = cameraId;
51     }
52 
53     return OK;
54 }
55 
writeToParcel(android::Parcel * parcel) const56 status_t CaptureResultExtras::writeToParcel(android::Parcel *parcel) const {
57     if (parcel == NULL) {
58         ALOGE("%s: Null parcel", __FUNCTION__);
59         return BAD_VALUE;
60     }
61 
62     parcel->writeInt32(requestId);
63     parcel->writeInt32(burstId);
64     parcel->writeInt32(afTriggerId);
65     parcel->writeInt32(precaptureTriggerId);
66     parcel->writeInt64(frameNumber);
67     parcel->writeInt32(partialResultCount);
68     parcel->writeInt32(errorStreamId);
69     if (errorPhysicalCameraId.size() > 0) {
70         parcel->writeBool(true);
71         status_t res = OK;
72         if ((res = parcel->writeString16(errorPhysicalCameraId)) != OK) {
73             ALOGE("%s: Failed to write physical camera ID to parcel: %d", __FUNCTION__, res);
74             return res;
75         }
76     } else {
77         parcel->writeBool(false);
78     }
79 
80     return OK;
81 }
82 
readFromParcel(const android::Parcel * parcel)83 status_t PhysicalCaptureResultInfo::readFromParcel(const android::Parcel* parcel) {
84     status_t res;
85 
86     mPhysicalCameraId.remove(mPhysicalCameraId.size());
87     mPhysicalCameraMetadata.clear();
88 
89     if ((res = parcel->readString16(&mPhysicalCameraId)) != OK) {
90         ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
91         return res;
92     }
93 
94     if ((res = mPhysicalCameraMetadata.readFromParcel(parcel)) != OK) {
95         ALOGE("%s: Failed to read metadata from parcel: %d", __FUNCTION__, res);
96         return res;
97     }
98     return OK;
99 }
100 
writeToParcel(android::Parcel * parcel) const101 status_t PhysicalCaptureResultInfo::writeToParcel(android::Parcel* parcel) const {
102     status_t res;
103     if ((res = parcel->writeString16(mPhysicalCameraId)) != OK) {
104         ALOGE("%s: Failed to write physical camera ID to parcel: %d",
105                 __FUNCTION__, res);
106         return res;
107     }
108     if ((res = mPhysicalCameraMetadata.writeToParcel(parcel)) != OK) {
109         ALOGE("%s: Failed to write physical camera metadata to parcel: %d",
110                 __FUNCTION__, res);
111         return res;
112     }
113     return OK;
114 }
115 
CaptureResult()116 CaptureResult::CaptureResult() :
117         mMetadata(), mResultExtras() {
118 }
119 
CaptureResult(const CaptureResult & otherResult)120 CaptureResult::CaptureResult(const CaptureResult &otherResult) {
121     mResultExtras = otherResult.mResultExtras;
122     mMetadata = otherResult.mMetadata;
123     mPhysicalMetadatas = otherResult.mPhysicalMetadatas;
124 }
125 
readFromParcel(android::Parcel * parcel)126 status_t CaptureResult::readFromParcel(android::Parcel *parcel) {
127 
128     ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
129 
130     if (parcel == NULL) {
131         ALOGE("%s: parcel is null", __FUNCTION__);
132         return BAD_VALUE;
133     }
134 
135     mMetadata.clear();
136     mPhysicalMetadatas.clear();
137 
138     status_t res = OK;
139     res = mMetadata.readFromParcel(parcel);
140     if (res != OK) {
141         ALOGE("%s: Failed to read metadata from parcel.",
142               __FUNCTION__);
143         return res;
144     }
145     ALOGV("%s: Read metadata from parcel", __FUNCTION__);
146 
147     int32_t physicalMetadataCount;
148     if ((res = parcel->readInt32(&physicalMetadataCount)) != OK) {
149         ALOGE("%s: Failed to read the physical metadata count from parcel: %d", __FUNCTION__, res);
150         return res;
151     }
152     if (physicalMetadataCount < 0) {
153         ALOGE("%s: Invalid physical metadata count from parcel: %d",
154                 __FUNCTION__, physicalMetadataCount);
155         return BAD_VALUE;
156     }
157 
158     for (int32_t i = 0; i < physicalMetadataCount; i++) {
159         String16 cameraId;
160         if ((res = parcel->readString16(&cameraId)) != OK) {
161             ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
162             return res;
163         }
164 
165         CameraMetadata physicalMetadata;
166         if ((res = physicalMetadata.readFromParcel(parcel)) != OK) {
167             ALOGE("%s: Failed to read metadata from parcel: %d", __FUNCTION__, res);
168             return res;
169         }
170 
171         mPhysicalMetadatas.emplace(mPhysicalMetadatas.end(), cameraId, physicalMetadata);
172     }
173     ALOGV("%s: Read physical metadata from parcel", __FUNCTION__);
174 
175     res = mResultExtras.readFromParcel(parcel);
176     if (res != OK) {
177         ALOGE("%s: Failed to read result extras from parcel.",
178                 __FUNCTION__);
179         return res;
180     }
181     ALOGV("%s: Read result extras from parcel", __FUNCTION__);
182 
183     return OK;
184 }
185 
writeToParcel(android::Parcel * parcel) const186 status_t CaptureResult::writeToParcel(android::Parcel *parcel) const {
187 
188     ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
189 
190     if (parcel == NULL) {
191         ALOGE("%s: parcel is null", __FUNCTION__);
192         return BAD_VALUE;
193     }
194 
195     status_t res;
196 
197     res = mMetadata.writeToParcel(parcel);
198     if (res != OK) {
199         ALOGE("%s: Failed to write metadata to parcel", __FUNCTION__);
200         return res;
201     }
202     ALOGV("%s: Wrote metadata to parcel", __FUNCTION__);
203 
204     int32_t physicalMetadataCount = static_cast<int32_t>(mPhysicalMetadatas.size());
205     res = parcel->writeInt32(physicalMetadataCount);
206     if (res != OK) {
207         ALOGE("%s: Failed to write physical metadata count to parcel: %d",
208                 __FUNCTION__, res);
209         return BAD_VALUE;
210     }
211     for (const auto& physicalMetadata : mPhysicalMetadatas) {
212         if ((res = parcel->writeString16(physicalMetadata.mPhysicalCameraId)) != OK) {
213             ALOGE("%s: Failed to write physical camera ID to parcel: %d",
214                     __FUNCTION__, res);
215             return res;
216         }
217         if ((res = physicalMetadata.mPhysicalCameraMetadata.writeToParcel(parcel)) != OK) {
218             ALOGE("%s: Failed to write physical camera metadata to parcel: %d",
219                     __FUNCTION__, res);
220             return res;
221         }
222     }
223     ALOGV("%s: Wrote physical camera metadata to parcel", __FUNCTION__);
224 
225     res = mResultExtras.writeToParcel(parcel);
226     if (res != OK) {
227         ALOGE("%s: Failed to write result extras to parcel", __FUNCTION__);
228         return res;
229     }
230     ALOGV("%s: Wrote result extras to parcel", __FUNCTION__);
231 
232     return OK;
233 }
234 
235 }
236