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