1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "picture_handle_client.h"
17
18 #include <cstdlib>
19 #include <fcntl.h>
20 #include <libexif/exif-entry.h>
21 #include <securec.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24
25 #include "exif_metadata.h"
26 #include "image_type.h"
27 #include "image_utils.h"
28 #include "medialibrary_db_const.h"
29 #include "medialibrary_errno.h"
30 #include "medialibrary_napi_utils.h"
31 #include "media_column.h"
32 #include "media_file_utils.h"
33 #include "media_log.h"
34 #include "pixel_yuv.h"
35 #include "pixel_yuv_ext.h"
36 #include "userfilemgr_uri.h"
37 #include "userfile_client.h"
38 #include <fstream>
39 #include <optional>
40
41 namespace OHOS {
42 namespace Media {
43 const int32_t MAX_VALUE = 100000000;
44 const int32_t COUNT_TWICE = 2;
45 const int32_t COUNT_THREE_TIMES = 3;
RequestPicture(const int32_t & fileId,int32_t & errCode)46 std::shared_ptr<Media::Picture> PictureHandlerClient::RequestPicture(const int32_t &fileId, int32_t &errCode)
47 {
48 MEDIA_DEBUG_LOG("PictureHandlerClient::RequestPicture fileId: %{public}d", fileId);
49 std::string uri = PhotoColumn::PHOTO_REQUEST_PICTURE;
50 MediaFileUtils::UriAppendKeyValue(uri, MediaColumn::MEDIA_ID, std::to_string(fileId));
51 Uri requestUri(uri);
52 int32_t fd = UserFileClient::OpenFile(requestUri, MEDIA_FILEMODE_READONLY);
53 if (fd < 0) {
54 MEDIA_DEBUG_LOG("PictureHandlerClient::RequestPicture picture not exist");
55 return nullptr;
56 }
57 std::shared_ptr<Media::Picture> picture = nullptr;
58 int32_t ret = ReadPicture(fd, fileId, picture, errCode);
59 if (ret != E_OK) {
60 MEDIA_DEBUG_LOG("PictureHandlerClient::RequestPicture failed to ReadPicture");
61 errCode = E_ERR;
62 close(fd);
63 return nullptr;
64 }
65 FinishRequestPicture(fileId);
66 close(fd);
67 return picture;
68 }
69
FinishRequestPicture(const int32_t & fileId)70 void PictureHandlerClient::FinishRequestPicture(const int32_t &fileId)
71 {
72 MEDIA_DEBUG_LOG("PictureHandlerClient::FinishRequestPicture fileId: %{public}d", fileId);
73 std::string uri = PAH_FINISH_REQUEST_PICTURE;
74 MediaLibraryNapiUtils::UriAppendKeyValue(uri, API_VERSION, std::to_string(MEDIA_API_VERSION_V10));
75 Uri finishRequestPictureUri(uri);
76
77 DataShare::DataShareValuesBucket valuesBucket;
78 valuesBucket.Put(PhotoColumn::MEDIA_ID, fileId);
79 UserFileClient::Insert(finishRequestPictureUri, valuesBucket);
80 }
81
82 // 获取消息总长度
ReadMessageLength(const int32_t & fd)83 std::optional<uint32_t> PictureHandlerClient::ReadMessageLength(const int32_t &fd)
84 {
85 uint32_t msgLen = 0;
86 void *msgLenAddr = mmap(nullptr, UINT32_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
87 if (msgLenAddr == MAP_FAILED) {
88 MEDIA_ERR_LOG("Failed to map memory to read message length");
89 return std::nullopt;
90 }
91 msgLen = *((uint32_t*)msgLenAddr);
92 munmap(msgLenAddr, UINT32_LEN);
93 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadMessageLength msgLen: %{public}d", msgLen);
94 return msgLen;
95 }
96
97 // 获取消息
ReadMessage(uint8_t * addr,const uint32_t & msgLen)98 std::optional<std::pair<uint32_t, uint8_t*>> PictureHandlerClient::ReadMessage(uint8_t *addr, const uint32_t &msgLen)
99 {
100 uint32_t dataSize = *reinterpret_cast<const uint32_t*>(addr + UINT32_LEN);
101 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadMessage dataSize: %{public}d", dataSize);
102 if (dataSize == 0) {
103 MEDIA_ERR_LOG("PictureHandlerClient::ReadMessage picture is not exists");
104 munmap(addr, msgLen);
105 return std::nullopt;
106 }
107
108 uint8_t *pictureParcelData = static_cast<uint8_t *>(malloc(dataSize));
109 if (pictureParcelData == nullptr) {
110 MEDIA_ERR_LOG("Failed to allocate memory for pictureParcelData.");
111 munmap(addr, msgLen);
112 return std::nullopt;
113 }
114
115 if (memcpy_s((void*)pictureParcelData, dataSize, addr + UINT32_LEN * COUNT_THREE_TIMES, dataSize)) {
116 MEDIA_ERR_LOG("Failed to memcpy_s pictureParcelData.");
117 free(pictureParcelData);
118 munmap(addr, msgLen);
119 return std::nullopt;
120 }
121 return std::make_pair(dataSize, pictureParcelData);
122 }
123
124 // 解析图片
ParsePicture(MessageParcel & pictureParcel,uint8_t * addr,const uint32_t & msgLen,int32_t & err)125 std::unique_ptr<Media::Picture> PictureHandlerClient::ParsePicture(MessageParcel &pictureParcel, uint8_t *addr,
126 const uint32_t &msgLen, int32_t &err)
127 {
128 MEDIA_DEBUG_LOG("PictureHandlerClient::ParsePicture read mainPixelMap");
129 std::shared_ptr<PixelMap> mainPixelMap = ReadPixelMap(pictureParcel, err);
130 if (mainPixelMap == nullptr) {
131 MEDIA_ERR_LOG("PictureHandlerService::ParsePicture mainPixelMap is nullptr!");
132 return nullptr;
133 }
134 std::unique_ptr<Media::Picture> picturePtr = Picture::Create(mainPixelMap);
135 if (picturePtr == nullptr) {
136 MEDIA_ERR_LOG("PictureHandlerService::ParsePicture picturePtr is nullptr!");
137 munmap(addr, msgLen);
138 return nullptr;
139 }
140 ReadExifMetadata(pictureParcel, picturePtr);
141 bool ret = ReadMaintenanceData(pictureParcel, picturePtr, err);
142 if (!ret) {
143 MEDIA_ERR_LOG("Failed to ReadMaintenanceData");
144 }
145 return picturePtr;
146 }
147
ReadPicture(const int32_t & fd,const int32_t & fileId,std::shared_ptr<Media::Picture> & picture,int32_t & err)148 int32_t PictureHandlerClient::ReadPicture(const int32_t &fd, const int32_t &fileId,
149 std::shared_ptr<Media::Picture> &picture, int32_t &err)
150 {
151 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPicture fd: %{public}d", fd);
152 // 获取消息总长度
153 auto msgLenOpt = ReadMessageLength(fd);
154 if (!msgLenOpt.has_value()) {
155 return E_ERR;
156 }
157 uint32_t msgLen = msgLenOpt.value();
158 // 获取消息
159 uint8_t *addr = (uint8_t*)mmap(nullptr, msgLen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
160 if (addr == MAP_FAILED) {
161 MEDIA_ERR_LOG("Failed to map memory to read message content");
162 return E_ERR;
163 }
164 auto msgDataOpt = ReadMessage(addr, msgLen);
165 if (!msgDataOpt.has_value()) {
166 munmap(addr, msgLen);
167 return E_ERR;
168 }
169 auto [dataSize, pictureParcelData] = msgDataOpt.value();
170
171 // 读取auxiliaryPictureSize
172 uint32_t auxiliaryPictureSize = *reinterpret_cast<const uint32_t*>(addr + UINT32_LEN * COUNT_TWICE);
173 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPicture auxiliaryPictureSize: %{public}d", auxiliaryPictureSize);
174
175 // 解析图片
176 MessageParcel pictureParcel;
177 pictureParcel.ParseFrom(reinterpret_cast<uintptr_t>(pictureParcelData), dataSize);
178 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPicture read mainPixelMap");
179 std::unique_ptr<Media::Picture> picturePtr = ParsePicture(pictureParcel, addr, msgLen, err);
180 if (picturePtr == nullptr) {
181 munmap(addr, msgLen);
182 return E_ERR;
183 }
184 for (size_t i = 1; i <= auxiliaryPictureSize; i++) {
185 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPicture read auxiliaryPicture, index:%{public}zu", i);
186 bool ret = ReadAuxiliaryPicture(pictureParcel, picturePtr, err);
187 if (!ret) {
188 munmap(addr, msgLen);
189 return E_ERR;
190 }
191 }
192 picture.reset(picturePtr.get());
193 picturePtr.release();
194 munmap(addr, msgLen);
195 return E_OK;
196 }
197
ReadPixelMap(MessageParcel & data,int32_t & err)198 std::shared_ptr<PixelMap> PictureHandlerClient::ReadPixelMap(MessageParcel &data, int32_t &err)
199 {
200 ImageInfo imageInfo;
201 ReadImageInfo(data, imageInfo);
202
203 bool isYuv = data.ReadBool();
204 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPixelMap isYuv:%{public}d", isYuv);
205 YUVDataInfo yuvInfo;
206 if (isYuv) {
207 ReadYuvDataInfo(data, yuvInfo);
208 }
209
210 bool editable = data.ReadBool();
211 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPixelMap editable:%{public}d", editable);
212
213 std::unique_ptr<PixelMap> pixelMap;
214 if (isYuv) {
215 #ifdef EXT_PIXEL
216 pixelMap = std::make_unique<PixelYuvExt>();
217 #else
218 pixelMap = std::make_unique<PixelYuv>();
219 #endif
220 } else {
221 pixelMap = std::make_unique<PixelMap>();
222 }
223 pixelMap->SetImageInfo(imageInfo);
224 pixelMap->SetImageYUVInfo(yuvInfo);
225
226 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadPixelMap read surface buffer");
227 bool ret = ReadSurfaceBuffer(data, pixelMap, err);
228 if (!ret) {
229 MEDIA_ERR_LOG("Failed to ReadSurfaceBuffer");
230 return nullptr;
231 }
232 return pixelMap;
233 }
234
ReadAuxiliaryPicture(MessageParcel & data,std::unique_ptr<Media::Picture> & picture,int32_t & err)235 bool PictureHandlerClient::ReadAuxiliaryPicture(MessageParcel &data, std::unique_ptr<Media::Picture> &picture,
236 int32_t &err)
237 {
238 AuxiliaryPictureInfo auxiliaryPictureInfo;
239 ReadAuxiliaryPictureInfo(data, auxiliaryPictureInfo);
240 std::shared_ptr<PixelMap> pixelMap = ReadPixelMap(data, err);
241 if (pixelMap == nullptr) {
242 MEDIA_ERR_LOG("PictureHandlerService::ReadAuxiliaryPicture pixelMap is nullptr!");
243 return false;
244 }
245 std::unique_ptr<AuxiliaryPicture> uptr = AuxiliaryPicture::Create(pixelMap,
246 auxiliaryPictureInfo.auxiliaryPictureType, auxiliaryPictureInfo.size);
247 std::shared_ptr<AuxiliaryPicture> auxiliaryPicture;
248 auxiliaryPicture.reset(uptr.get());
249 uptr.release();
250
251 auxiliaryPicture->SetAuxiliaryPictureInfo(auxiliaryPictureInfo);
252
253 int32_t metadataSize = 0;
254 if (data.ReadInt32(metadataSize) && metadataSize >= 0 && metadataSize < MAX_VALUE) {
255 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPicture metadataSize: %{public}d", metadataSize);
256 for (int i = 0; i < metadataSize; i++) {
257 MetadataType type = static_cast<MetadataType>(data.ReadInt32());
258 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPicture type: %{public}d", type);
259 std::shared_ptr<ImageMetadata> metadataPtr(nullptr);
260 metadataPtr.reset(ExifMetadata::Unmarshalling(data));
261 auxiliaryPicture->SetMetadata(type, metadataPtr);
262 }
263 } else {
264 MEDIA_ERR_LOG("PictureHandlerClient::ReadAuxiliaryPicture metadataSize failed");
265 }
266 picture->SetAuxiliaryPicture(auxiliaryPicture);
267 MEDIA_DEBUG_LOG("PictureHandler::ReadAuxiliaryPicture end");
268 return true;
269 }
270
ReadAuxiliaryPictureInfo(MessageParcel & data,AuxiliaryPictureInfo & auxiliaryPictureInfo)271 bool PictureHandlerClient::ReadAuxiliaryPictureInfo(MessageParcel &data, AuxiliaryPictureInfo &auxiliaryPictureInfo)
272 {
273 auxiliaryPictureInfo.auxiliaryPictureType = static_cast<AuxiliaryPictureType>(data.ReadInt32());
274 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPictureInfo auxiliaryPictureType: %{public}d",
275 auxiliaryPictureInfo.auxiliaryPictureType);
276
277 auxiliaryPictureInfo.colorSpace = static_cast<ColorSpace>(data.ReadInt32());
278 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPictureInfo colorSpace: %{public}d",
279 auxiliaryPictureInfo.colorSpace);
280
281 auxiliaryPictureInfo.pixelFormat = static_cast<PixelFormat>(data.ReadInt32());
282 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPictureInfo pixelFormat: %{public}d",
283 auxiliaryPictureInfo.pixelFormat);
284
285 auxiliaryPictureInfo.rowStride = data.ReadInt32();
286 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPictureInfo rowStride: %{public}d",
287 auxiliaryPictureInfo.rowStride);
288
289 auxiliaryPictureInfo.size.height = data.ReadInt32();
290 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPictureInfo height: %{public}d",
291 auxiliaryPictureInfo.size.height);
292
293 auxiliaryPictureInfo.size.width = data.ReadInt32();
294 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadAuxiliaryPictureInfo width: %{public}d",
295 auxiliaryPictureInfo.size.width);
296
297 return true;
298 }
299
ReadImageInfo(MessageParcel & data,ImageInfo & imageInfo)300 bool PictureHandlerClient::ReadImageInfo(MessageParcel &data, ImageInfo &imageInfo)
301 {
302 imageInfo.size.width = data.ReadInt32();
303 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadImageInfo width: %{public}d", imageInfo.size.width);
304 imageInfo.size.height = data.ReadInt32();
305 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadImageInfo height: %{public}d", imageInfo.size.height);
306 imageInfo.pixelFormat = static_cast<PixelFormat>(data.ReadInt32());
307 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadImageInfo pixelFormat: %{public}d", imageInfo.pixelFormat);
308 imageInfo.colorSpace = static_cast<ColorSpace>(data.ReadInt32());
309 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadImageInfo colorSpace: %{public}d", imageInfo.colorSpace);
310 imageInfo.alphaType = static_cast<AlphaType>(data.ReadInt32());
311 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadImageInfo alphaType: %{public}d", imageInfo.alphaType);
312 imageInfo.baseDensity = data.ReadInt32();
313 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadImageInfo baseDensity: %{public}d", imageInfo.baseDensity);
314 return true;
315 }
316
ReadYuvDataInfo(MessageParcel & data,YUVDataInfo & info)317 bool PictureHandlerClient::ReadYuvDataInfo(MessageParcel &data, YUVDataInfo &info)
318 {
319 info.imageSize.width = data.ReadInt32();
320 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo width: %{public}d", info.imageSize.width);
321 info.imageSize.height = data.ReadInt32();
322 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo height: %{public}d", info.imageSize.height);
323 info.yWidth = data.ReadUint32();
324 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo yWidth: %{public}d", info.yWidth);
325 info.yHeight = data.ReadUint32();
326 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo yHeight: %{public}d", info.yHeight);
327 info.uvWidth = data.ReadUint32();
328 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo uvWidth: %{public}d", info.uvWidth);
329 info.uvHeight = data.ReadUint32();
330 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo uvHeight: %{public}d", info.uvHeight);
331 info.yStride = data.ReadUint32();
332 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo yStride: %{public}d", info.yStride);
333 info.uStride = data.ReadUint32();
334 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo uStride: %{public}d", info.uStride);
335 info.vStride = data.ReadUint32();
336 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo vStride: %{public}d", info.vStride);
337 info.uvStride = data.ReadUint32();
338 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo uvStride: %{public}d", info.uvStride);
339 info.yOffset = data.ReadUint32();
340 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo yOffset: %{public}d", info.yOffset);
341 info.uOffset = data.ReadUint32();
342 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo uOffset: %{public}d", info.uOffset);
343 info.vOffset = data.ReadUint32();
344 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo vOffset: %{public}d", info.vOffset);
345 info.uvOffset = data.ReadUint32();
346 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadYuvDataInfo uvOffset: %{public}d", info.uvOffset);
347 return true;
348 }
349
ReadSurfaceBuffer(MessageParcel & data,std::unique_ptr<PixelMap> & pixelMap,int32_t & err)350 bool PictureHandlerClient::ReadSurfaceBuffer(MessageParcel &data, std::unique_ptr<PixelMap> &pixelMap, int32_t &err)
351 {
352 bool hasBufferHandle = data.ReadBool();
353 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadSurfaceBuffer hasBufferHandle: %{public}d", hasBufferHandle);
354 if (!hasBufferHandle) {
355 return false;
356 }
357 sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create();
358 bool ret = ReadBufferHandle(data, surfaceBuffer, err);
359 if (!ret) {
360 return false;
361 }
362 void* nativeBuffer = surfaceBuffer.GetRefPtr();
363 OHOS::RefBase *ref = reinterpret_cast<OHOS::RefBase *>(nativeBuffer);
364 ref->IncStrongRef(ref);
365 pixelMap->SetPixelsAddr(static_cast<uint8_t*>(surfaceBuffer->GetVirAddr()), nativeBuffer,
366 pixelMap->GetByteCount(), AllocatorType::DMA_ALLOC, nullptr);
367 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadSurfaceBuffer end");
368 return true;
369 }
370
CreateBufferHandle(MessageParcel & data,const uint32_t & reserveFds,const uint32_t & reserveInts)371 BufferHandle* PictureHandlerClient::CreateBufferHandle(MessageParcel &data, const uint32_t &reserveFds,
372 const uint32_t &reserveInts)
373 {
374 size_t handleSize = sizeof(BufferHandle) + (sizeof(int32_t) * (reserveFds + reserveInts));
375 BufferHandle *handle = static_cast<BufferHandle *>(malloc(handleSize));
376 if (handle == nullptr) {
377 MEDIA_ERR_LOG("PictureHandlerClient::CreateBufferHandle malloc BufferHandle failed");
378 return nullptr;
379 }
380 memset_s(handle, handleSize, 0, handleSize);
381
382 handle->reserveFds = reserveFds;
383 handle->reserveInts = reserveInts;
384 handle->width = data.ReadInt32();
385 MEDIA_DEBUG_LOG("PictureHandlerClient::CreateBufferHandle width: %{public}d", handle->width);
386 handle->stride = data.ReadInt32();
387 MEDIA_DEBUG_LOG("PictureHandlerClient::CreateBufferHandle stride: %{public}d", handle->stride);
388 handle->height = data.ReadInt32();
389 MEDIA_DEBUG_LOG("PictureHandlerClient::CreateBufferHandle height: %{public}d", handle->height);
390 handle->size = data.ReadInt32();
391 MEDIA_DEBUG_LOG("PictureHandlerClient::CreateBufferHandle size: %{public}d", handle->size);
392 handle->format = data.ReadInt32();
393 MEDIA_DEBUG_LOG("PictureHandlerClient::CreateBufferHandle format: %{public}d", handle->format);
394 handle->usage = data.ReadUint64();
395 handle->phyAddr = data.ReadUint64();
396 return handle;
397 }
398
ReadBufferHandle(MessageParcel & data,sptr<SurfaceBuffer> & surfaceBuffer,int32_t & err)399 bool PictureHandlerClient::ReadBufferHandle(MessageParcel &data, sptr<SurfaceBuffer> &surfaceBuffer, int32_t &err)
400 {
401 uint32_t reserveFds = 0;
402 bool readReserveFdsRet = data.ReadUint32(reserveFds);
403 if (reserveFds < 0 || reserveFds > static_cast<uint32_t>(MAX_VALUE)) {
404 return false;
405 }
406 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadBufferHandle reserveFds: %{public}d", reserveFds);
407 uint32_t reserveInts = 0;
408 bool reserveIntsRet = data.ReadUint32(reserveInts);
409 if (reserveInts < 0 || reserveInts > static_cast<uint32_t>(MAX_VALUE)) {
410 return false;
411 }
412 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadBufferHandle reserveInts: %{public}d", reserveInts);
413 BufferHandle *handle = CreateBufferHandle(data, reserveFds, reserveInts);
414 if (handle == nullptr) {
415 return false;
416 }
417 int32_t fd = RequestBufferHandlerFd(data.ReadInt32());
418 if (fd < 0) {
419 MEDIA_ERR_LOG("PictureHandlerClient::ReadBufferHandle fd: %{public}d", fd);
420 err = E_ERR;
421 close(fd);
422 return false;
423 }
424 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadBufferHandle fd: %{public}d", fd);
425 handle->fd = dup(fd);
426 close(fd);
427 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadBufferHandle handle->fd: %{public}d", handle->fd);
428 if (readReserveFdsRet) {
429 for (uint32_t i = 0; i < reserveFds; i++) {
430 int32_t reserveFd = RequestBufferHandlerFd(data.ReadInt32());
431 if (reserveFd < 0) {
432 err = E_ERR;
433 MEDIA_ERR_LOG("PictureHandlerClient::ReadBufferHandle reserveFd: %{public}d", reserveFd);
434 return false;
435 }
436 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadBufferHandle reserve[%{public}d]: %{public}d", i, reserveFd);
437 handle->reserve[i] = dup(reserveFd);
438 close(reserveFd);
439 }
440 }
441 if (reserveIntsRet) {
442 for (uint32_t j = 0; j < handle->reserveInts; j++) {
443 handle->reserve[reserveFds + j] = data.ReadInt32();
444 }
445 }
446 surfaceBuffer->SetBufferHandle(handle);
447 return true;
448 }
449
ReadExifMetadata(MessageParcel & data,std::unique_ptr<Media::Picture> & picture)450 bool PictureHandlerClient::ReadExifMetadata(MessageParcel &data, std::unique_ptr<Media::Picture> &picture)
451 {
452 bool hasExifMetadata = data.ReadBool();
453 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadExifMetadata hasExifMetadata:%{public}d", hasExifMetadata);
454 if (!hasExifMetadata) {
455 return true;
456 }
457 ExifMetadata *exifMetadataPtr = ExifMetadata::Unmarshalling(data);
458 auto exifMetadata = std::shared_ptr<ExifMetadata>(exifMetadataPtr);
459 picture->SetExifMetadata(exifMetadata);
460 return true;
461 }
462
ReadMaintenanceData(MessageParcel & data,std::unique_ptr<Media::Picture> & picture,int32_t & err)463 bool PictureHandlerClient::ReadMaintenanceData(MessageParcel &data, std::unique_ptr<Media::Picture> &picture,
464 int32_t &err)
465 {
466 bool hasMaintenanceData = data.ReadBool();
467 MEDIA_DEBUG_LOG("PictureHandlerClient::ReadMaintenanceData hasMaintenanceData:%{public}d", hasMaintenanceData);
468 if (!hasMaintenanceData) {
469 return true;
470 }
471 sptr<SurfaceBuffer> surfaceBuffer = SurfaceBuffer::Create();
472 bool ret = ReadBufferHandle(data, surfaceBuffer, err);
473 if (!ret) {
474 return false;
475 }
476 return picture->SetMaintenanceData(surfaceBuffer);
477 }
478
RequestBufferHandlerFd(const int32_t & fd)479 int32_t PictureHandlerClient::RequestBufferHandlerFd(const int32_t &fd)
480 {
481 std::string uri = PhotoColumn::PHOTO_REQUEST_PICTURE_BUFFER;
482 MediaFileUtils::UriAppendKeyValue(uri, "fd", std::to_string(fd));
483 MEDIA_DEBUG_LOG("PictureHandlerClient::RequestBufferHandlerFd uri: %{public}s", uri.c_str());
484 Uri requestUri(uri);
485 return UserFileClient::OpenFile(requestUri, MEDIA_FILEMODE_READONLY);
486 }
487 }
488 }