1 /*
2 * Copyright (c) 2024-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 <cstdint>
17 #include <unistd.h>
18 #include <sys/mman.h>
19 #include <iomanip>
20
21 #include <buffer_handle_parcel.h>
22 #include "camera_log.h"
23 #include "camera_photo_proxy.h"
24
25 namespace OHOS {
26 namespace CameraStandard {
27
CameraPhotoProxy()28 CameraPhotoProxy::CameraPhotoProxy()
29 {
30 MEDIA_INFO_LOG("CameraPhotoProxy no args");
31 format_ = 0;
32 photoId_ = "";
33 deferredProcType_ = 0;
34 photoWidth_ = 0;
35 photoHeight_ = 0;
36 isHighQuality_ = false;
37 bufferHandle_ = nullptr;
38 fileSize_ = 0;
39 isDeferredPhoto_ = 0;
40 longitude_ = 0.0;
41 latitude_ = 0.0;
42 captureId_ = 0;
43 imageFormat_ = 0;
44 }
45
CameraPhotoProxy(BufferHandle * bufferHandle,int32_t format,int32_t photoWidth,int32_t photoHeight,bool isHighQuality,int32_t captureId)46 CameraPhotoProxy::CameraPhotoProxy(BufferHandle* bufferHandle, int32_t format,
47 int32_t photoWidth, int32_t photoHeight, bool isHighQuality, int32_t captureId)
48 {
49 MEDIA_INFO_LOG("CameraPhotoProxy");
50 bufferHandle_ = bufferHandle;
51 format_ = format;
52 photoWidth_ = photoWidth;
53 photoHeight_ = photoHeight;
54 fileSize_ = 0;
55 isHighQuality_ = isHighQuality;
56 deferredProcType_ = 0;
57 isDeferredPhoto_ = 0;
58 longitude_ = 0.0;
59 latitude_ = 0.0;
60 captureId_ = captureId;
61 imageFormat_ = 0;
62 MEDIA_INFO_LOG("format = %{public}d, width = %{public}d, height = %{public}d",
63 format_, photoWidth, photoHeight);
64 }
65
~CameraPhotoProxy()66 CameraPhotoProxy::~CameraPhotoProxy()
67 {
68 std::lock_guard<std::mutex> lock(mutex_);
69 MEDIA_INFO_LOG("~CameraPhotoProxy");
70 fileSize_ = 0;
71 }
72
ReadFromParcel(MessageParcel & parcel)73 void CameraPhotoProxy::ReadFromParcel(MessageParcel &parcel)
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 photoId_ = parcel.ReadString();
77 deferredProcType_ = parcel.ReadInt32();
78 isDeferredPhoto_ = parcel.ReadInt32();
79 format_ = parcel.ReadInt32();
80 photoWidth_ = parcel.ReadInt32();
81 photoHeight_ = parcel.ReadInt32();
82 isHighQuality_ = parcel.ReadBool();
83 fileSize_ = parcel.ReadUint64();
84 latitude_ = parcel.ReadDouble();
85 longitude_ = parcel.ReadDouble();
86 captureId_ = parcel.ReadInt32();
87 imageFormat_ = parcel.ReadInt32();
88 bufferHandle_ = ReadBufferHandle(parcel);
89 MEDIA_INFO_LOG("PhotoProxy::ReadFromParcel");
90 }
91
CameraFreeBufferHandle()92 int32_t CameraPhotoProxy::CameraFreeBufferHandle()
93 {
94 MEDIA_ERR_LOG("CameraFreeBufferHandle start");
95 std::lock_guard<std::mutex> lock(mutex_);
96 CHECK_ERROR_RETURN_RET_LOG(bufferHandle_ == nullptr, 0, "CameraFreeBufferHandle with nullptr handle");
97 if (bufferHandle_->fd >= 0) {
98 close(bufferHandle_->fd);
99 bufferHandle_->fd = -1;
100 }
101 const uint32_t reserveFds = bufferHandle_->reserveFds;
102 for (uint32_t i = 0; i < reserveFds; i++) {
103 if (bufferHandle_->reserve[i] >= 0) {
104 close(bufferHandle_->reserve[i]);
105 bufferHandle_->reserve[i] = -1;
106 }
107 }
108 free(bufferHandle_);
109 return 0;
110 }
111
WriteToParcel(MessageParcel & parcel)112 void CameraPhotoProxy::WriteToParcel(MessageParcel &parcel)
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 parcel.WriteString(photoId_);
116 parcel.WriteInt32(deferredProcType_);
117 parcel.WriteInt32(isDeferredPhoto_);
118 parcel.WriteInt32(format_);
119 parcel.WriteInt32(photoWidth_);
120 parcel.WriteInt32(photoHeight_);
121 parcel.WriteBool(isHighQuality_);
122 parcel.WriteUint64(fileSize_);
123 parcel.WriteDouble(latitude_);
124 parcel.WriteDouble(longitude_);
125 parcel.WriteInt32(captureId_);
126 parcel.WriteInt32(imageFormat_);
127 if (bufferHandle_) {
128 MEDIA_DEBUG_LOG("PhotoProxy::WriteToParcel %{public}d", bufferHandle_->fd);
129 bool ret = WriteBufferHandle(parcel, *bufferHandle_);
130 if (ret == false) {
131 MEDIA_ERR_LOG("Failure, Reason: WriteBufferHandle return false");
132 }
133 } else {
134 MEDIA_ERR_LOG("PhotoProxy::WriteToParcel without bufferHandle_");
135 }
136 MEDIA_INFO_LOG("PhotoProxy::WriteToParcel");
137 }
138
SetDeferredAttrs(std::string photoId,int32_t deferredProcType,uint64_t fileSize,int32_t imageFormat)139 void CameraPhotoProxy::SetDeferredAttrs(std::string photoId, int32_t deferredProcType,
140 uint64_t fileSize, int32_t imageFormat)
141 {
142 std::lock_guard<std::mutex> lock(mutex_);
143 isDeferredPhoto_ = 1;
144 photoId_ = photoId;
145 deferredProcType_ = deferredProcType;
146 fileSize_ = fileSize;
147 imageFormat_ = imageFormat;
148 }
149
SetLocation(double latitude,double longitude)150 void CameraPhotoProxy::SetLocation(double latitude, double longitude)
151 {
152 std::lock_guard<std::mutex> lock(mutex_);
153 latitude_ = latitude;
154 longitude_ = longitude;
155 }
156 } // namespace CameraStandard
157 } // namespace OHOS
158