• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "hcamera_service_stub.h"
17 #include <cinttypes>
18 #include "camera_log.h"
19 #include "metadata_utils.h"
20 #include "remote_request_code.h"
21 #include "input/camera_death_recipient.h"
22 #include "hcamera_service.h"
23 #include "input/i_standard_camera_listener.h"
24 #include "ipc_skeleton.h"
25 #include "xcollie/xcollie.h"
26 #include "xcollie/xcollie_define.h"
27 
28 namespace OHOS {
29 namespace CameraStandard {
HCameraServiceStub()30 HCameraServiceStub::HCameraServiceStub()
31 {
32     RegisterMethod();
33     deathRecipientMap_.Clear();
34     cameraListenerMap_.Clear();
35     MEDIA_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create",
36         (POINTER_MASK & reinterpret_cast<uintptr_t>(this)));
37 }
38 
~HCameraServiceStub()39 HCameraServiceStub::~HCameraServiceStub()
40 {
41     MEDIA_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy",
42         (POINTER_MASK & reinterpret_cast<uintptr_t>(this)));
43 }
44 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)45 int HCameraServiceStub::OnRemoteRequest(
46     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
47 {
48     DisableJeMalloc();
49     if (data.ReadInterfaceToken() != GetDescriptor()) {
50         return -1;
51     }
52     const int TIME_OUT_SECONDS = 10;
53     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(
54         "CameraServiceStub", TIME_OUT_SECONDS, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
55     int32_t ret = CheckRequestCode(code, data, reply, option);
56     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
57     return ret;
58 }
59 
RegisterMethod()60 void HCameraServiceStub::RegisterMethod()
61 {
62     methodFactory[CAMERA_SERVICE_MUTE_CAMERA] = &HCameraServiceStub::HandleMuteCamera;
63     methodFactory[CAMERA_SERVICE_SET_MUTE_CALLBACK] = &HCameraServiceStub::HandleSetMuteCallback;
64     methodFactory[CAMERA_SERVICE_IS_CAMERA_MUTED] = &HCameraServiceStub::HandleIsCameraMuted;
65     methodFactory[CAMERA_SERVICE_CREATE_DEVICE] = &HCameraServiceStub::HandleCreateCameraDevice;
66     methodFactory[CAMERA_SERVICE_SET_CALLBACK] = &HCameraServiceStub::HandleSetCallback;
67     methodFactory[CAMERA_SERVICE_GET_CAMERAS] = &HCameraServiceStub::HandleGetCameras;
68     methodFactory[CAMERA_SERVICE_CREATE_CAPTURE_SESSION] = &HCameraServiceStub::HandleCreateCaptureSession;
69     methodFactory[CAMERA_SERVICE_CREATE_PHOTO_OUTPUT] = &HCameraServiceStub::HandleCreatePhotoOutput;
70     methodFactory[CAMERA_SERVICE_CREATE_PREVIEW_OUTPUT] = &HCameraServiceStub::HandleCreatePreviewOutput;
71     methodFactory[CAMERA_SERVICE_CREATE_DEFERRED_PREVIEW_OUTPUT] =
72         &HCameraServiceStub::HandleCreateDeferredPreviewOutput;
73     methodFactory[CAMERA_SERVICE_CREATE_METADATA_OUTPUT] = &HCameraServiceStub::HandleCreateMetadataOutput;
74     methodFactory[CAMERA_SERVICE_CREATE_VIDEO_OUTPUT] = &HCameraServiceStub::HandleCreateVideoOutput;
75     methodFactory[CAMERA_SERVICE_SET_LISTENER_OBJ] = &HCameraServiceStub::SetListenerObject;
76 }
77 
CheckRequestCode(const uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)78 int32_t HCameraServiceStub::CheckRequestCode(
79     const uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
80 {
81     typedef std::map<uint32_t, HandleMethod>::const_iterator Iterator;
82     Iterator iter = methodFactory.find(code);
83     if (methodFactory.end() == iter) {
84         MEDIA_ERR_LOG("HCameraServiceStub request code %{public}u not handled", code);
85         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
86     }
87     HandleMethod method = iter->second;
88     return (this->*method)(data, reply);
89 }
90 
HandleGetCameras(MessageParcel & data,MessageParcel & reply)91 int HCameraServiceStub::HandleGetCameras(MessageParcel &data, MessageParcel &reply)
92 {
93     std::vector<std::string> cameraIds;
94     std::vector<std::shared_ptr<OHOS::Camera::CameraMetadata>> cameraAbilityList;
95 
96     int errCode = GetCameras(cameraIds, cameraAbilityList);
97     if (!reply.WriteStringVector(cameraIds)) {
98         MEDIA_ERR_LOG("HCameraServiceStub HandleGetCameras WriteStringVector failed");
99         return IPC_STUB_WRITE_PARCEL_ERR;
100     }
101 
102     int count = static_cast<int>(cameraAbilityList.size());
103     if (!reply.WriteInt32(count)) {
104         MEDIA_ERR_LOG("HCameraServiceStub HandleGetCameras Write vector size failed");
105         return IPC_STUB_WRITE_PARCEL_ERR;
106     }
107 
108     for (auto cameraAbility : cameraAbilityList) {
109         if (!(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(cameraAbility, reply))) {
110             MEDIA_ERR_LOG("HCameraServiceStub HandleGetCameras write ability failed");
111             return IPC_STUB_WRITE_PARCEL_ERR;
112         }
113     }
114 
115     return errCode;
116 }
117 
HandleCreateCameraDevice(MessageParcel & data,MessageParcel & reply)118 int HCameraServiceStub::HandleCreateCameraDevice(MessageParcel &data, MessageParcel &reply)
119 {
120     std::string cameraId = data.ReadString();
121     sptr<ICameraDeviceService> device = nullptr;
122 
123     int errCode = CreateCameraDevice(cameraId, device);
124     if (errCode != ERR_NONE) {
125         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateCameraDevice Create camera device failed : %{public}d", errCode);
126         return errCode;
127     }
128 
129     if (!reply.WriteRemoteObject(device->AsObject())) {
130         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateCameraDevice Write CameraDevice obj failed");
131         return IPC_STUB_WRITE_PARCEL_ERR;
132     }
133 
134     return errCode;
135 }
136 
HandleMuteCamera(MessageParcel & data,MessageParcel & reply)137 int HCameraServiceStub::HandleMuteCamera(MessageParcel &data, MessageParcel &reply)
138 {
139     bool muteMode = data.ReadBool();
140     MEDIA_DEBUG_LOG("HCameraServiceStub HandleMuteCamera read muteMode : %{public}d", muteMode);
141 
142     int32_t ret = MuteCamera(muteMode);
143     MEDIA_INFO_LOG("HCameraServiceStub HandleMuteCamera MuteCamera result: %{public}d", ret);
144     return ret;
145 }
146 
HandleIsCameraMuted(MessageParcel & data,MessageParcel & reply)147 int HCameraServiceStub::HandleIsCameraMuted(MessageParcel &data, MessageParcel &reply)
148 {
149     bool isMuted = false;
150     int32_t ret = IsCameraMuted(isMuted);
151     MEDIA_INFO_LOG("HCameraServiceStub HandleIsCameraMuted result: %{public}d, isMuted: %{public}d", ret, isMuted);
152     if (!reply.WriteBool(isMuted)) {
153         MEDIA_ERR_LOG("HCameraServiceStub HandleIsCameraMuted Write isMuted failed");
154         return IPC_STUB_WRITE_PARCEL_ERR;
155     }
156     return ret;
157 }
158 
HandleSetCallback(MessageParcel & data,MessageParcel & reply)159 int HCameraServiceStub::HandleSetCallback(MessageParcel &data, MessageParcel &reply)
160 {
161     auto remoteObject = data.ReadRemoteObject();
162     if (remoteObject == nullptr) {
163         MEDIA_ERR_LOG("HCameraServiceStub HandleSetCallback CameraServiceCallback is null");
164         return IPC_STUB_INVALID_DATA_ERR;
165     }
166 
167     auto callback = iface_cast<ICameraServiceCallback>(remoteObject);
168 
169     return SetCallback(callback);
170 }
171 
HandleSetMuteCallback(MessageParcel & data,MessageParcel & reply)172 int HCameraServiceStub::HandleSetMuteCallback(MessageParcel &data, MessageParcel &reply)
173 {
174     auto remoteObject = data.ReadRemoteObject();
175     if (remoteObject == nullptr) {
176         MEDIA_ERR_LOG("HCameraServiceStub HandleSetMuteCallback CameraMuteServiceCallback is null");
177         return IPC_STUB_INVALID_DATA_ERR;
178     }
179 
180     auto callback = iface_cast<ICameraMuteServiceCallback>(remoteObject);
181 
182     return SetMuteCallback(callback);
183 }
184 
HandleCreateCaptureSession(MessageParcel & data,MessageParcel & reply)185 int HCameraServiceStub::HandleCreateCaptureSession(MessageParcel &data, MessageParcel &reply)
186 {
187     sptr<ICaptureSession> session = nullptr;
188 
189     int errCode = CreateCaptureSession(session);
190     if (errCode != ERR_NONE) {
191         MEDIA_ERR_LOG("HandleCreateCaptureSession CreateCaptureSession failed : %{public}d", errCode);
192         return errCode;
193     }
194 
195     if (!reply.WriteRemoteObject(session->AsObject())) {
196         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateCaptureSession Write CaptureSession obj failed");
197         return IPC_STUB_WRITE_PARCEL_ERR;
198     }
199 
200     return errCode;
201 }
202 
HandleCreatePhotoOutput(MessageParcel & data,MessageParcel & reply)203 int HCameraServiceStub::HandleCreatePhotoOutput(MessageParcel &data, MessageParcel &reply)
204 {
205     sptr<IStreamCapture> photoOutput = nullptr;
206     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
207 
208     if (remoteObj == nullptr) {
209         MEDIA_ERR_LOG("HCameraServiceStub HandleCreatePhotoOutput BufferProducer is null");
210         return IPC_STUB_INVALID_DATA_ERR;
211     }
212 
213     sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
214     int32_t format = data.ReadInt32();
215     int32_t width = data.ReadInt32();
216     int32_t height = data.ReadInt32();
217     int errCode = CreatePhotoOutput(producer, format, width, height, photoOutput);
218     if (errCode != ERR_NONE) {
219         MEDIA_ERR_LOG("HCameraServiceStub::HandleCreatePhotoOutput Create photo output failed : %{public}d", errCode);
220         return errCode;
221     }
222 
223     if (!reply.WriteRemoteObject(photoOutput->AsObject())) {
224         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateCameraDevice Write photoOutput obj failed");
225         return IPC_STUB_WRITE_PARCEL_ERR;
226     }
227 
228     return errCode;
229 }
230 
HandleCreatePreviewOutput(MessageParcel & data,MessageParcel & reply)231 int HCameraServiceStub::HandleCreatePreviewOutput(MessageParcel &data, MessageParcel &reply)
232 {
233     sptr<IStreamRepeat> previewOutput = nullptr;
234 
235     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
236     if (remoteObj == nullptr) {
237         MEDIA_ERR_LOG("HCameraServiceStub HandleCreatePreviewOutput BufferProducer is null");
238         return IPC_STUB_INVALID_DATA_ERR;
239     }
240     int32_t format = data.ReadInt32();
241     int32_t width = data.ReadInt32();
242     int32_t height = data.ReadInt32();
243     MEDIA_INFO_LOG("CreatePreviewOutput, format: %{public}d, width: %{public}d, height: %{public}d",
244                    format, width, height);
245     sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
246     int errCode = CreatePreviewOutput(producer, format, width, height, previewOutput);
247     if (errCode != ERR_NONE) {
248         MEDIA_ERR_LOG("HandleCreatePreviewOutput CreatePreviewOutput failed : %{public}d", errCode);
249         return errCode;
250     }
251     if (!reply.WriteRemoteObject(previewOutput->AsObject())) {
252         MEDIA_ERR_LOG("HCameraServiceStub HandleCreatePreviewOutput Write previewOutput obj failed");
253         return IPC_STUB_WRITE_PARCEL_ERR;
254     }
255     return errCode;
256 }
257 
HandleCreateDeferredPreviewOutput(MessageParcel & data,MessageParcel & reply)258 int HCameraServiceStub::HandleCreateDeferredPreviewOutput(MessageParcel &data, MessageParcel &reply)
259 {
260     sptr<IStreamRepeat> previewOutput = nullptr;
261 
262     int32_t format = data.ReadInt32();
263     int32_t width = data.ReadInt32();
264     int32_t height = data.ReadInt32();
265     MEDIA_INFO_LOG("CreatePreviewOutput, format: %{public}d, width: %{public}d, height: %{public}d",
266                    format, width, height);
267 
268     int errCode = CreateDeferredPreviewOutput(format, width, height, previewOutput);
269     if (errCode != ERR_NONE) {
270         MEDIA_ERR_LOG("HandleCreatePreviewOutput CreatePreviewOutput failed : %{public}d", errCode);
271         return errCode;
272     }
273     if (!reply.WriteRemoteObject(previewOutput->AsObject())) {
274         MEDIA_ERR_LOG("HCameraServiceStub HandleCreatePreviewOutput Write previewOutput obj failed");
275         return IPC_STUB_WRITE_PARCEL_ERR;
276     }
277     return errCode;
278 }
279 
HandleCreateMetadataOutput(MessageParcel & data,MessageParcel & reply)280 int HCameraServiceStub::HandleCreateMetadataOutput(MessageParcel &data, MessageParcel &reply)
281 {
282     sptr<IStreamMetadata> metadataOutput = nullptr;
283     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
284 
285     if (remoteObj == nullptr) {
286         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateMetadataOutput BufferProducer is null");
287         return IPC_STUB_INVALID_DATA_ERR;
288     }
289     sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
290     int32_t format = data.ReadInt32();
291     int errCode = CreateMetadataOutput(producer, format, metadataOutput);
292     if (errCode != ERR_NONE) {
293         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateMetadataOutput CreateMetadataOutput failed : %{public}d",
294                       errCode);
295         return errCode;
296     }
297     if (!reply.WriteRemoteObject(metadataOutput->AsObject())) {
298         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateMetadataOutput Write metadataOutput obj failed");
299         return IPC_STUB_WRITE_PARCEL_ERR;
300     }
301     return errCode;
302 }
303 
HandleCreateVideoOutput(MessageParcel & data,MessageParcel & reply)304 int HCameraServiceStub::HandleCreateVideoOutput(MessageParcel &data, MessageParcel &reply)
305 {
306     sptr<IStreamRepeat> videoOutput = nullptr;
307     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
308 
309     if (remoteObj == nullptr) {
310         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateVideoOutput BufferProducer is null");
311         return IPC_STUB_INVALID_DATA_ERR;
312     }
313 
314     sptr<OHOS::IBufferProducer> producer = iface_cast<OHOS::IBufferProducer>(remoteObj);
315     int32_t format = data.ReadInt32();
316     int32_t width = data.ReadInt32();
317     int32_t height = data.ReadInt32();
318     int errCode = CreateVideoOutput(producer, format, width, height, videoOutput);
319     if (errCode != ERR_NONE) {
320         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateVideoOutput CreateVideoOutput failed : %{public}d", errCode);
321         return errCode;
322     }
323     if (!reply.WriteRemoteObject(videoOutput->AsObject())) {
324         MEDIA_ERR_LOG("HCameraServiceStub HandleCreateVideoOutput Write videoOutput obj failed");
325         return IPC_STUB_WRITE_PARCEL_ERR;
326     }
327 
328     return errCode;
329 }
330 
UnSetCallback(pid_t pid)331 int32_t HCameraServiceStub::UnSetCallback(pid_t pid)
332 {
333     return CAMERA_OK;
334 }
335 
CloseCameraForDestory(pid_t pid)336 int32_t HCameraServiceStub::CloseCameraForDestory(pid_t pid)
337 {
338     return CAMERA_OK;
339 }
340 
DestroyStubForPid(pid_t pid)341 int HCameraServiceStub::DestroyStubForPid(pid_t pid)
342 {
343     sptr<CameraDeathRecipient> deathRecipient = nullptr;
344     sptr<IStandardCameraListener> cameraListener = nullptr;
345     if (deathRecipientMap_.Find(pid, deathRecipient)) {
346         if (deathRecipient != nullptr) {
347             deathRecipient->SetNotifyCb(nullptr);
348         }
349         deathRecipientMap_.Erase(pid);
350     }
351     if (cameraListenerMap_.Find(pid, cameraListener)) {
352         if (cameraListener != nullptr && cameraListener->AsObject() != nullptr && deathRecipient != nullptr) {
353             (void)cameraListener->AsObject()->RemoveDeathRecipient(deathRecipient);
354         }
355         cameraListenerMap_.Erase(pid);
356     }
357     HCaptureSession::DestroyStubObjectForPid(pid);
358     CloseCameraForDestory(pid);
359     UnSetCallback(pid);
360     return CAMERA_OK;
361 }
362 
ClientDied(pid_t pid)363 void HCameraServiceStub::ClientDied(pid_t pid)
364 {
365     DisableJeMalloc();
366     MEDIA_ERR_LOG("client pid is dead, pid:%{public}d", pid);
367     (void)DestroyStubForPid(pid);
368 }
369 
SetListenerObject(const sptr<IRemoteObject> & object)370 int HCameraServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
371 {
372     int errCode = -1;
373     sptr<CameraDeathRecipient> deathRecipientTmp = nullptr;
374     sptr<IStandardCameraListener> cameraListenerTmp = nullptr;
375     pid_t pid = IPCSkeleton::GetCallingPid();
376     if (deathRecipientMap_.Find(pid, deathRecipientTmp)) {
377         deathRecipientMap_.Erase(pid);
378     }
379     if (cameraListenerMap_.Find(pid, cameraListenerTmp)) {
380         if (cameraListenerTmp != nullptr && cameraListenerTmp->AsObject() != nullptr && deathRecipientTmp != nullptr) {
381             (void)cameraListenerTmp->AsObject()->RemoveDeathRecipient(deathRecipientTmp);
382         }
383         cameraListenerMap_.Erase(pid);
384     }
385     CHECK_AND_RETURN_RET_LOG(object != nullptr, CAMERA_ALLOC_ERROR, "set listener object is nullptr");
386     sptr<IStandardCameraListener> cameraListener = iface_cast<IStandardCameraListener>(object);
387     CHECK_AND_RETURN_RET_LOG(cameraListener != nullptr, CAMERA_ALLOC_ERROR,
388         "failed to convert IStandardCameraListener");
389     sptr<CameraDeathRecipient> deathRecipient = new(std::nothrow) CameraDeathRecipient(pid);
390     CHECK_AND_RETURN_RET_LOG(deathRecipient != nullptr, CAMERA_ALLOC_ERROR, "failed to new CameraDeathRecipient");
391     deathRecipient->SetNotifyCb(std::bind(&HCameraServiceStub::ClientDied, this, std::placeholders::_1));
392     if (cameraListener->AsObject() != nullptr) {
393         (void)cameraListener->AsObject()->AddDeathRecipient(deathRecipient);
394     }
395     MEDIA_DEBUG_LOG("client pid pid:%{public}d", pid);
396     cameraListenerMap_.EnsureInsert(pid, cameraListener);
397     deathRecipientMap_.EnsureInsert(pid, deathRecipient);
398     return errCode;
399 }
400 
SetListenerObject(MessageParcel & data,MessageParcel & reply)401 int HCameraServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
402 {
403     int errCode = -1;
404     sptr<IRemoteObject> object = data.ReadRemoteObject();
405     (void)reply.WriteInt32(SetListenerObject(object));
406     return errCode;
407 }
408 } // namespace CameraStandard
409 } // namespace OHOS
410