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     burstSeqId_ = -1;
44     imageFormat_ = 0;
45 }
46 
CameraPhotoProxy(BufferHandle * bufferHandle,int32_t format,int32_t photoWidth,int32_t photoHeight,bool isHighQuality,int32_t captureId)47 CameraPhotoProxy::CameraPhotoProxy(BufferHandle* bufferHandle, int32_t format,
48     int32_t photoWidth, int32_t photoHeight, bool isHighQuality, int32_t captureId)
49 {
50     MEDIA_INFO_LOG("CameraPhotoProxy");
51     bufferHandle_ = bufferHandle;
52     format_ = format;
53     photoWidth_ = photoWidth;
54     photoHeight_ = photoHeight;
55     fileSize_ = 0;
56     isHighQuality_ = isHighQuality;
57     deferredProcType_ = 0;
58     isDeferredPhoto_ = 0;
59     longitude_ = 0.0;
60     latitude_ = 0.0;
61     captureId_ = captureId;
62     burstSeqId_ = -1;
63     imageFormat_ = 0;
64     MEDIA_INFO_LOG("format = %{public}d, width = %{public}d, height = %{public}d",
65         format_, photoWidth, photoHeight);
66 }
67 
CameraPhotoProxy(BufferHandle * bufferHandle,int32_t format,int32_t photoWidth,int32_t photoHeight,bool isHighQuality,int32_t captureId,int32_t burstSeqId)68 CameraPhotoProxy::CameraPhotoProxy(BufferHandle* bufferHandle, int32_t format,
69     int32_t photoWidth, int32_t photoHeight, bool isHighQuality, int32_t captureId, int32_t burstSeqId)
70 {
71     MEDIA_INFO_LOG("CameraPhotoProxy");
72     bufferHandle_ = bufferHandle;
73     format_ = format;
74     photoWidth_ = photoWidth;
75     photoHeight_ = photoHeight;
76     fileSize_ = 0;
77     isHighQuality_ = isHighQuality;
78     deferredProcType_ = 0;
79     isDeferredPhoto_ = 0;
80     longitude_ = 0.0;
81     latitude_ = 0.0;
82     captureId_ = captureId;
83     burstSeqId_ = burstSeqId;
84     imageFormat_ = 0;
85     MEDIA_INFO_LOG("format = %{public}d, width = %{public}d, height = %{public}d",
86         format_, photoWidth, photoHeight);
87 }
88 
~CameraPhotoProxy()89 CameraPhotoProxy::~CameraPhotoProxy()
90 {
91     std::lock_guard<std::mutex> lock(mutex_);
92     MEDIA_INFO_LOG("~CameraPhotoProxy");
93     fileSize_ = 0;
94 }
95 
ReadFromParcel(MessageParcel & parcel)96 void CameraPhotoProxy::ReadFromParcel(MessageParcel &parcel)
97 {
98     std::lock_guard<std::mutex> lock(mutex_);
99     photoId_ = parcel.ReadString();
100     deferredProcType_ = parcel.ReadInt32();
101     isDeferredPhoto_ = parcel.ReadInt32();
102     format_ = parcel.ReadInt32();
103     photoWidth_ = parcel.ReadInt32();
104     photoHeight_ = parcel.ReadInt32();
105     isHighQuality_ = parcel.ReadBool();
106     fileSize_ = parcel.ReadUint64();
107     latitude_ = parcel.ReadDouble();
108     longitude_ = parcel.ReadDouble();
109     captureId_ = parcel.ReadInt32();
110     burstSeqId_ = parcel.ReadInt32();
111     imageFormat_ = parcel.ReadInt32();
112     bufferHandle_ = ReadBufferHandle(parcel);
113     MEDIA_INFO_LOG("PhotoProxy::ReadFromParcel");
114 }
115 
CameraFreeBufferHandle()116 int32_t CameraPhotoProxy::CameraFreeBufferHandle()
117 {
118     MEDIA_ERR_LOG("CameraFreeBufferHandle start");
119     std::lock_guard<std::mutex> lock(mutex_);
120     CHECK_ERROR_RETURN_RET_LOG(bufferHandle_ == nullptr, 0, "CameraFreeBufferHandle with nullptr handle");
121     if (bufferHandle_->fd >= 0) {
122         close(bufferHandle_->fd);
123         bufferHandle_->fd = -1;
124     }
125     const uint32_t reserveFds = bufferHandle_->reserveFds;
126     for (uint32_t i = 0; i < reserveFds; i++) {
127         if (bufferHandle_->reserve[i] >= 0) {
128             close(bufferHandle_->reserve[i]);
129             bufferHandle_->reserve[i] = -1;
130         }
131     }
132     free(bufferHandle_);
133     return 0;
134 }
135 
WriteToParcel(MessageParcel & parcel)136 void CameraPhotoProxy::WriteToParcel(MessageParcel &parcel)
137 {
138     std::lock_guard<std::mutex> lock(mutex_);
139     parcel.WriteString(photoId_);
140     parcel.WriteInt32(deferredProcType_);
141     parcel.WriteInt32(isDeferredPhoto_);
142     parcel.WriteInt32(format_);
143     parcel.WriteInt32(photoWidth_);
144     parcel.WriteInt32(photoHeight_);
145     parcel.WriteBool(isHighQuality_);
146     parcel.WriteUint64(fileSize_);
147     parcel.WriteDouble(latitude_);
148     parcel.WriteDouble(longitude_);
149     parcel.WriteInt32(captureId_);
150     parcel.WriteInt32(burstSeqId_);
151     parcel.WriteInt32(imageFormat_);
152     if (bufferHandle_) {
153         MEDIA_DEBUG_LOG("PhotoProxy::WriteToParcel %{public}d", bufferHandle_->fd);
154         bool ret = WriteBufferHandle(parcel, *bufferHandle_);
155         if (ret == false) {
156             MEDIA_ERR_LOG("Failure, Reason: WriteBufferHandle return false");
157         }
158     } else {
159         MEDIA_ERR_LOG("PhotoProxy::WriteToParcel without bufferHandle_");
160     }
161     MEDIA_INFO_LOG("PhotoProxy::WriteToParcel");
162 }
163 
SetDeferredAttrs(std::string photoId,int32_t deferredProcType,uint64_t fileSize,int32_t imageFormat)164 void CameraPhotoProxy::SetDeferredAttrs(std::string photoId, int32_t deferredProcType,
165     uint64_t fileSize, int32_t imageFormat)
166 {
167     std::lock_guard<std::mutex> lock(mutex_);
168     isDeferredPhoto_ = 1;
169     photoId_ = photoId;
170     deferredProcType_ = deferredProcType;
171     fileSize_ = fileSize;
172     imageFormat_ = imageFormat;
173 }
174 
SetLocation(double latitude,double longitude)175 void CameraPhotoProxy::SetLocation(double latitude, double longitude)
176 {
177     std::lock_guard<std::mutex> lock(mutex_);
178     latitude_ = latitude;
179     longitude_ = longitude;
180 }
181 } // namespace CameraStandard
182 } // namespace OHOS
183