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 <unistd.h>
17 #include <sys/mman.h>
18 #include <iomanip>
19
20 #include <buffer_handle_parcel.h>
21 #include "camera_log.h"
22 #include "datetime_ex.h"
23 #include "camera_server_photo_proxy.h"
24 #include "format.h"
25 #include "photo_proxy.h"
26
27 namespace OHOS {
28 namespace CameraStandard {
29
CameraServerPhotoProxy()30 CameraServerPhotoProxy::CameraServerPhotoProxy()
31 {
32 format_ = 0;
33 photoId_ = "";
34 deferredProcType_ = 0;
35 photoWidth_ = 0;
36 photoHeight_ = 0;
37 bufferHandle_ = nullptr;
38 fileDataAddr_ = nullptr;
39 fileSize_ = 0;
40 isMmaped_ = false;
41 isDeferredPhoto_ = 0;
42 isHighQuality_ = false;
43 mode_ = 0;
44 longitude_ = 0.0;
45 latitude_ = 0.0;
46 captureId_ = 0;
47 burstSeqId_ = -1;
48 burstKey_ = "";
49 isCoverPhoto_ = false;
50 imageFormat_ = 0;
51 }
52
~CameraServerPhotoProxy()53 CameraServerPhotoProxy::~CameraServerPhotoProxy()
54 {
55 std::lock_guard<std::mutex> lock(mutex_);
56 MEDIA_INFO_LOG("~CameraServerPhotoProxy");
57 CameraFreeBufferHandle(const_cast<BufferHandle*>(bufferHandle_));
58 fileDataAddr_ = nullptr;
59 fileSize_ = 0;
60 }
61
CameraFreeBufferHandle(BufferHandle * handle)62 int32_t CameraServerPhotoProxy::CameraFreeBufferHandle(BufferHandle *handle)
63 {
64 CHECK_ERROR_RETURN_RET_LOG(handle == nullptr, 0, "CameraFreeBufferHandle with nullptr handle");
65 if (handle->fd >= 0) {
66 close(handle->fd);
67 handle->fd = -1;
68 }
69 const uint32_t reserveFds = handle->reserveFds;
70 for (uint32_t i = 0; i < reserveFds; i++) {
71 if (handle->reserve[i] >= 0) {
72 close(handle->reserve[i]);
73 handle->reserve[i] = -1;
74 }
75 }
76 free(handle);
77 return 0;
78 }
79
SetDisplayName(std::string displayName)80 void CameraServerPhotoProxy::SetDisplayName(std::string displayName)
81 {
82 displayName_ = displayName;
83 }
84
ReadFromParcel(MessageParcel & parcel)85 void CameraServerPhotoProxy::ReadFromParcel(MessageParcel &parcel)
86 {
87 std::lock_guard<std::mutex> lock(mutex_);
88 photoId_ = parcel.ReadString();
89 deferredProcType_ = parcel.ReadInt32();
90 isDeferredPhoto_ = parcel.ReadInt32();
91 format_ = parcel.ReadInt32();
92 photoWidth_ = parcel.ReadInt32();
93 photoHeight_ = parcel.ReadInt32();
94 isHighQuality_ = parcel.ReadBool();
95 fileSize_ = parcel.ReadUint64();
96 latitude_ = parcel.ReadDouble();
97 longitude_ = parcel.ReadDouble();
98 captureId_ = parcel.ReadInt32();
99 burstSeqId_ = parcel.ReadInt32();
100 imageFormat_ = parcel.ReadInt32();
101 bufferHandle_ = ReadBufferHandle(parcel);
102 MEDIA_INFO_LOG("CameraServerPhotoProxy::ReadFromParcel");
103 }
104
GetCaptureId()105 int32_t CameraServerPhotoProxy::GetCaptureId()
106 {
107 MEDIA_INFO_LOG("CameraServerPhotoProxy::GetCaptureId captureId:%{public}d", captureId_);
108 std::lock_guard<std::mutex> lock(mutex_);
109 return captureId_;
110 }
111
GetBurstSeqId()112 int32_t CameraServerPhotoProxy::GetBurstSeqId()
113 {
114 MEDIA_INFO_LOG("CameraServerPhotoProxy::GetBurstSeqId burstSeqId:%{public}d", burstSeqId_);
115 std::lock_guard<std::mutex> lock(mutex_);
116 return burstSeqId_;
117 }
118
GetPhotoId()119 std::string CameraServerPhotoProxy::GetPhotoId()
120 {
121 MEDIA_INFO_LOG("CameraServerPhotoProxy::GetPhotoId photoId:%{public}s", photoId_.c_str());
122 std::lock_guard<std::mutex> lock(mutex_);
123 return photoId_;
124 }
125
GetDeferredProcType()126 Media::DeferredProcType CameraServerPhotoProxy::GetDeferredProcType()
127 {
128 MEDIA_DEBUG_LOG("CameraServerPhotoProxy::GetDeferredProcType");
129 std::lock_guard<std::mutex> lock(mutex_);
130 if (deferredProcType_ == 0) {
131 return Media::DeferredProcType::BACKGROUND;
132 } else {
133 return Media::DeferredProcType::OFFLINE;
134 }
135 }
136
GetFileDataAddr()137 void* CameraServerPhotoProxy::GetFileDataAddr()
138 {
139 MEDIA_INFO_LOG("CameraServerPhotoProxy::GetFileDataAddr");
140 std::lock_guard<std::mutex> lock(mutex_);
141 CHECK_ERROR_RETURN_RET_LOG(
142 bufferHandle_ == nullptr, fileDataAddr_, "CameraServerPhotoProxy::GetFileDataAddr bufferHandle_ is nullptr");
143 if (!isMmaped_) {
144 MEDIA_INFO_LOG("CameraServerPhotoProxy::GetFileDataAddr mmap");
145 fileDataAddr_ = mmap(nullptr, bufferHandle_->size, PROT_READ, MAP_SHARED, bufferHandle_->fd, 0);
146 CHECK_ERROR_RETURN_RET_LOG(
147 fileDataAddr_ == MAP_FAILED, fileDataAddr_, "CameraServerPhotoProxy::GetFileDataAddr mmap failed");
148 isMmaped_ = true;
149 } else {
150 MEDIA_ERR_LOG("CameraServerPhotoProxy::GetFileDataAddr mmap failed");
151 }
152 return fileDataAddr_;
153 }
154
GetFileSize()155 size_t CameraServerPhotoProxy::GetFileSize()
156 {
157 MEDIA_INFO_LOG("CameraServerPhotoProxy::GetFileSize");
158 std::lock_guard<std::mutex> lock(mutex_);
159 return fileSize_;
160 }
161
GetWidth()162 int32_t CameraServerPhotoProxy::GetWidth()
163 {
164 return photoWidth_;
165 }
166
GetHeight()167 int32_t CameraServerPhotoProxy::GetHeight()
168 {
169 return photoHeight_;
170 }
171
GetFormat()172 PhotoFormat CameraServerPhotoProxy::GetFormat()
173 {
174 auto iter = formatMap.find(imageFormat_);
175 if (iter != formatMap.end()) {
176 return iter->second;
177 }
178 return Media::PhotoFormat::RGBA;
179 }
180
GetPhotoQuality()181 PhotoQuality CameraServerPhotoProxy::GetPhotoQuality()
182 {
183 return isHighQuality_ ? Media::PhotoQuality::HIGH : Media::PhotoQuality::LOW;
184 }
185
Release()186 void CameraServerPhotoProxy::Release()
187 {
188 MEDIA_INFO_LOG("CameraServerPhotoProxy release enter");
189 if (isMmaped_ && bufferHandle_ != nullptr) {
190 munmap(fileDataAddr_, bufferHandle_->size);
191 } else {
192 MEDIA_ERR_LOG("CameraServerPhotoProxy munmap failed");
193 }
194 }
195
GetTitle()196 std::string CameraServerPhotoProxy::GetTitle()
197 {
198 return displayName_;
199 }
200
GetExtension()201 std::string CameraServerPhotoProxy::GetExtension()
202 {
203 return (GetFormat() == PhotoFormat::HEIF) ? suffixHeif : suffixJpeg;
204 }
GetLatitude()205 double CameraServerPhotoProxy::GetLatitude()
206 {
207 return latitude_;
208 }
GetLongitude()209 double CameraServerPhotoProxy::GetLongitude()
210 {
211 return longitude_;
212 }
GetShootingMode()213 int32_t CameraServerPhotoProxy::GetShootingMode()
214 {
215 auto iter = modeMap.find(mode_);
216 if (iter != modeMap.end()) {
217 return iter->second;
218 }
219 return 0;
220 }
SetShootingMode(int32_t mode)221 void CameraServerPhotoProxy::SetShootingMode(int32_t mode)
222 {
223 mode_ = mode;
224 }
225
GetBurstKey()226 std::string CameraServerPhotoProxy::GetBurstKey()
227 {
228 MEDIA_DEBUG_LOG("CameraServerPhotoProxy GetBurstKey");
229 return burstKey_;
230 }
231
IsCoverPhoto()232 bool CameraServerPhotoProxy::IsCoverPhoto()
233 {
234 MEDIA_DEBUG_LOG("CameraServerPhotoProxy IsCoverPhoto");
235 return isCoverPhoto_;
236 }
237
SetBurstInfo(std::string burstKey,bool isCoverPhoto)238 void CameraServerPhotoProxy::SetBurstInfo(std::string burstKey, bool isCoverPhoto)
239 {
240 MEDIA_INFO_LOG("CameraServerPhotoProxy SetBurstInfo");
241 burstKey_ = burstKey;
242 isCoverPhoto_ = isCoverPhoto;
243 isHighQuality_ = true; // tell media lib disable deferred photo
244 }
245 } // namespace CameraStandard
246 } // namespace OHOS
247