• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "dcamera_client.h"
17 
18 #include "anonymous_string.h"
19 #include "dcamera_input_callback.h"
20 #include "dcamera_manager_callback.h"
21 #include "dcamera_photo_callback.h"
22 #include "dcamera_preview_callback.h"
23 #include "dcamera_session_callback.h"
24 #include "dcamera_utils_tools.h"
25 #include "dcamera_video_callback.h"
26 #include "distributed_camera_constants.h"
27 #include "distributed_camera_errno.h"
28 #include "distributed_hardware_log.h"
29 #include "metadata_utils.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
DCameraClient(const std::string & dhId)33 DCameraClient::DCameraClient(const std::string& dhId)
34 {
35     DHLOGI("DCameraClientCommon Constructor dhId: %s", GetAnonyString(dhId).c_str());
36     cameraId_ = dhId.substr(CAMERA_ID_PREFIX.size());
37     isInit_ = false;
38 }
39 
~DCameraClient()40 DCameraClient::~DCameraClient()
41 {
42     if (isInit_) {
43         UnInit();
44     }
45 }
46 
Init()47 int32_t DCameraClient::Init()
48 {
49     DHLOGI("DCameraClientCommon::Init cameraId: %s", GetAnonyString(cameraId_).c_str());
50     cameraManager_ = CameraStandard::CameraManager::GetInstance();
51     if (cameraManager_ == nullptr) {
52         DHLOGE("DCameraClientCommon::Init cameraManager getInstance failed");
53         return DCAMERA_BAD_VALUE;
54     }
55     cameraManager_->SetCallback(std::make_shared<DCameraManagerCallback>());
56 
57     std::vector<sptr<CameraStandard::CameraInfo>> cameraList = cameraManager_->GetCameras();
58     DHLOGI("DCameraClientCommon::Init camera size: %d", cameraList.size());
59     for (auto& info : cameraList) {
60         if (info->GetID() == cameraId_) {
61             DHLOGI("DCameraClientCommon::Init cameraInfo get id: %s", GetAnonyString(info->GetID()).c_str());
62             cameraInfo_ = info;
63             break;
64         }
65     }
66     if (cameraInfo_ == nullptr) {
67         DHLOGE("DCameraClientCommon::Init cameraInfo is null");
68         return DCAMERA_BAD_VALUE;
69     }
70 
71     isInit_ = true;
72     DHLOGI("DCameraClientCommon::Init %s success", GetAnonyString(cameraId_).c_str());
73     return DCAMERA_OK;
74 }
75 
UnInit()76 int32_t DCameraClient::UnInit()
77 {
78     DHLOGI("DCameraClientCommon::UnInit cameraId: %s", GetAnonyString(cameraId_).c_str());
79     if (cameraManager_ != nullptr) {
80         DHLOGI("DCameraClientCommon::UnInit unregister cameraManager callback");
81         cameraManager_->SetCallback(nullptr);
82     }
83 
84     isInit_ = false;
85     cameraInfo_ = nullptr;
86     cameraManager_ = nullptr;
87     DHLOGI("DCameraClientCommon::UnInit %s success", GetAnonyString(cameraId_).c_str());
88     return DCAMERA_OK;
89 }
90 
UpdateSettings(std::vector<std::shared_ptr<DCameraSettings>> & settings)91 int32_t DCameraClient::UpdateSettings(std::vector<std::shared_ptr<DCameraSettings>>& settings)
92 {
93     DHLOGI("DCameraClientCommon::UpdateCameraSettings cameraId: %s", GetAnonyString(cameraId_).c_str());
94     for (auto& setting : settings) {
95         switch (setting->type_) {
96             case UPDATE_METADATA: {
97                 DHLOGI("DCameraClientCommon::UpdateCameraSettings %s update metadata settings",
98                        GetAnonyString(cameraId_).c_str());
99                 std::string metadataStr = Base64Decode(setting->value_);
100                 int32_t ret = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetCameraSettings(metadataStr);
101                 if (ret != DCAMERA_OK) {
102                     DHLOGE("DCameraClientCommon::UpdateSettings %s update metadata settings failed, ret: %d",
103                            GetAnonyString(cameraId_).c_str(), ret);
104                     return ret;
105                 }
106                 break;
107             }
108             default: {
109                 DHLOGE("DCameraClientCommon::UpdateSettings unknown setting type");
110                 break;
111             }
112         }
113     }
114     DHLOGI("DCameraClientCommon::UpdateCameraSettings %s success", GetAnonyString(cameraId_).c_str());
115     return DCAMERA_OK;
116 }
117 
StartCapture(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)118 int32_t DCameraClient::StartCapture(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
119 {
120     DHLOGI("DCameraClientCommon::StartCapture cameraId: %s", GetAnonyString(cameraId_).c_str());
121     if ((photoOutput_ == nullptr) && (previewOutput_ == nullptr)) {
122         DHLOGI("DCameraClientCommon::StartCapture %s config capture session", GetAnonyString(cameraId_).c_str());
123         int32_t ret = ConfigCaptureSession(captureInfos);
124         if (ret != DCAMERA_OK) {
125             DHLOGE("DCameraClientCommon::StartCapture config capture session failed, cameraId: %s, ret: %d",
126                    GetAnonyString(cameraId_).c_str(), ret);
127             return ret;
128         }
129     }
130 
131     for (auto& info : captureInfos) {
132         if ((info->streamType_ == CONTINUOUS_FRAME) || (!info->isCapture_)) {
133             continue;
134         }
135         int32_t ret = StartCaptureInner(info);
136         if (ret != DCAMERA_OK) {
137             DHLOGE("DCameraClientCommon::StartCapture failed, cameraId: %s, ret: %d",
138                 GetAnonyString(cameraId_).c_str(), ret);
139             return ret;
140         }
141     }
142     DHLOGI("DCameraClientCommon::StartCapture %s success", GetAnonyString(cameraId_).c_str());
143     return DCAMERA_OK;
144 }
145 
StopCapture()146 int32_t DCameraClient::StopCapture()
147 {
148     DHLOGI("DCameraClientCommon::StopCapture cameraId: %s", GetAnonyString(cameraId_).c_str());
149     if (captureSession_ != nullptr) {
150         DHLOGI("DCameraClientCommon::StopCapture %s stop captureSession", GetAnonyString(cameraId_).c_str());
151         int32_t ret = captureSession_->Stop();
152         if (ret != DCAMERA_OK) {
153             DHLOGE("DCameraClientCommon::StopCapture captureSession stop failed, cameraId: %s, ret: %d",
154                    GetAnonyString(cameraId_).c_str(), ret);
155         }
156         DHLOGI("DCameraClientCommon::StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str());
157         captureSession_->Release();
158     }
159 
160     if (cameraInput_ != nullptr) {
161         DHLOGI("DCameraClientCommon::StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str());
162         cameraInput_->Release();
163     }
164 
165     photoOutput_ = nullptr;
166     previewOutput_ = nullptr;
167     cameraInput_ = nullptr;
168     captureSession_ = nullptr;
169     DHLOGI("DCameraClientCommon::StopCapture %s success", GetAnonyString(cameraId_).c_str());
170     return DCAMERA_OK;
171 }
172 
SetStateCallback(std::shared_ptr<StateCallback> & callback)173 int32_t DCameraClient::SetStateCallback(std::shared_ptr<StateCallback>& callback)
174 {
175     DHLOGI("DCameraClientCommon::SetStateCallback cameraId: %s", GetAnonyString(cameraId_).c_str());
176     if (callback == nullptr) {
177         DHLOGE("DCameraClientCommon::SetStateCallback %s unregistering state callback",
178             GetAnonyString(cameraId_).c_str());
179     }
180     stateCallback_ = callback;
181     return DCAMERA_OK;
182 }
183 
SetResultCallback(std::shared_ptr<ResultCallback> & callback)184 int32_t DCameraClient::SetResultCallback(std::shared_ptr<ResultCallback>& callback)
185 {
186     DHLOGI("DCameraClientCommon::SetResultCallback cameraId: %s", GetAnonyString(cameraId_).c_str());
187     if (callback == nullptr) {
188         DHLOGE("DCameraClientCommon::SetResultCallback %s unregistering result callback",
189             GetAnonyString(cameraId_).c_str());
190     }
191     resultCallback_ = callback;
192     return DCAMERA_OK;
193 }
194 
ConfigCaptureSession(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)195 int32_t DCameraClient::ConfigCaptureSession(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
196 {
197     DHLOGI("DCameraClientCommon::ConfigCaptureSession cameraId: %s", GetAnonyString(cameraId_).c_str());
198     cameraInput_ = cameraManager_->CreateCameraInput(cameraInfo_);
199     if (cameraInput_ == nullptr) {
200         DHLOGE("DCameraClientCommon::ConfigCaptureSession %s create cameraInput failed",
201             GetAnonyString(cameraId_).c_str());
202         return DCAMERA_BAD_VALUE;
203     }
204     std::shared_ptr<DCameraInputCallback> inputCallback = std::make_shared<DCameraInputCallback>(stateCallback_);
205     ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetErrorCallback(inputCallback);
206 
207     captureSession_ = cameraManager_->CreateCaptureSession();
208     if (captureSession_ == nullptr) {
209         DHLOGE("DCameraClientCommon::ConfigCaptureSession %s create captureSession failed",
210                GetAnonyString(cameraId_).c_str());
211         return DCAMERA_BAD_VALUE;
212     }
213     captureSession_->SetCallback(std::make_shared<DCameraSessionCallback>(stateCallback_));
214 
215     int32_t ret = CreateCaptureOutput(captureInfos);
216     if (ret != DCAMERA_OK) {
217         DHLOGE("DCameraClientCommon::ConfigCaptureSession create capture output failed, cameraId: %s, ret: %d",
218                GetAnonyString(cameraId_).c_str(), ret);
219         return ret;
220     }
221 
222     return ConfigCaptureSessionInner();
223 }
224 
ConfigCaptureSessionInner()225 int32_t DCameraClient::ConfigCaptureSessionInner()
226 {
227     int32_t ret = captureSession_->BeginConfig();
228     if (ret != DCAMERA_OK) {
229         DHLOGE("DCameraClientCommon::ConfigCaptureSession %s config captureSession failed, ret: %d",
230                GetAnonyString(cameraId_).c_str(), ret);
231         return ret;
232     }
233 
234     ret = captureSession_->AddInput(cameraInput_);
235     if (ret != DCAMERA_OK) {
236         DHLOGE("DCameraClientCommon::ConfigCaptureSession %s add cameraInput to captureSession failed, ret: %d",
237                GetAnonyString(cameraId_).c_str(), ret);
238         return ret;
239     }
240 
241     if (photoOutput_ != nullptr) {
242         ret = captureSession_->AddOutput(photoOutput_);
243         if (ret != DCAMERA_OK) {
244             DHLOGE("DCameraClientCommon::ConfigCaptureSession %s add photoOutput to captureSession failed, ret: %d",
245                    GetAnonyString(cameraId_).c_str(), ret);
246             return ret;
247         }
248     }
249 
250     if (previewOutput_ != nullptr) {
251         ret = captureSession_->AddOutput(previewOutput_);
252         if (ret != DCAMERA_OK) {
253             DHLOGE("DCameraClientCommon::ConfigCaptureSession %s add previewOutput to captureSession failed, ret: %d",
254                    GetAnonyString(cameraId_).c_str(), ret);
255             return ret;
256         }
257     }
258 
259     ret = captureSession_->CommitConfig();
260     if (ret != DCAMERA_OK) {
261         DHLOGE("DCameraClientCommon::ConfigCaptureSession %s commit captureSession failed, ret: %d",
262                GetAnonyString(cameraId_).c_str(), ret);
263         return ret;
264     }
265 
266     ret = captureSession_->Start();
267     if (ret != DCAMERA_OK) {
268         DHLOGE("DCameraClientCommon::ConfigCaptureSession %s start captureSession failed, ret: %d",
269                GetAnonyString(cameraId_).c_str(), ret);
270     }
271 
272     DHLOGI("DCameraClientCommon::ConfigCaptureSession %s success", GetAnonyString(cameraId_).c_str());
273     return DCAMERA_OK;
274 }
275 
CreateCaptureOutput(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)276 int32_t DCameraClient::CreateCaptureOutput(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
277 {
278     if (captureInfos.empty()) {
279         DHLOGE("DCameraClientCommon::CreateCaptureOutput no capture info, cameraId: %s",
280             GetAnonyString(cameraId_).c_str());
281         return DCAMERA_BAD_VALUE;
282     }
283 
284     for (auto& info : captureInfos) {
285         if (info->streamType_ == SNAPSHOT_FRAME) {
286             int32_t ret = CreatePhotoOutput(info);
287             if (ret != DCAMERA_OK) {
288                 DHLOGE("DCameraClientCommon::CreateCaptureOutput %s create photo output failed, ret: %d",
289                        GetAnonyString(cameraId_).c_str(), ret);
290                 return ret;
291             }
292         } else if (info->streamType_ == CONTINUOUS_FRAME) {
293             int32_t ret = CreateVideoOutput(info);
294             if (ret != DCAMERA_OK) {
295                 DHLOGE("DCameraClientCommon::CreateCaptureOutput %s create video output failed, ret: %d",
296                        GetAnonyString(cameraId_).c_str(), ret);
297                 return ret;
298             }
299         } else {
300             DHLOGE("DCameraClientCommon::CreateCaptureOutput unknown stream type");
301             return DCAMERA_BAD_VALUE;
302         }
303     }
304     return DCAMERA_OK;
305 }
306 
CreatePhotoOutput(std::shared_ptr<DCameraCaptureInfo> & info)307 int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
308 {
309     DHLOGI("DCameraClientCommon::CreatePhotoOutput camId: %s, w: %d, h: %d, f: %d, stream: %d, isCapture: %d",
310            GetAnonyString(cameraId_).c_str(), info->width_, info->height_,
311            camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888, info->streamType_, info->isCapture_);
312     photoSurface_ = Surface::CreateSurfaceAsConsumer();
313     photoSurface_->SetDefaultWidthAndHeight(info->width_, info->height_);
314     photoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888));
315     photoListener_ = std::make_shared<DCameraPhotoSurfaceListener>(photoSurface_, resultCallback_);
316     photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoListener_);
317     photoOutput_ = cameraManager_->CreatePhotoOutput(photoSurface_);
318     if (photoOutput_ == nullptr) {
319         DHLOGE("DCameraClientCommon::CreatePhotoOutput %s create photo output failed",
320             GetAnonyString(cameraId_).c_str());
321         return DCAMERA_BAD_VALUE;
322     }
323     std::shared_ptr<DCameraPhotoCallback> photoCallback = std::make_shared<DCameraPhotoCallback>(stateCallback_);
324     ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->SetCallback(photoCallback);
325     DHLOGI("DCameraClientCommon::CreatePhotoOutput %s success", GetAnonyString(cameraId_).c_str());
326     return DCAMERA_OK;
327 }
328 
CreateVideoOutput(std::shared_ptr<DCameraCaptureInfo> & info)329 int32_t DCameraClient::CreateVideoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
330 {
331     DHLOGI("DCameraClientCommon::CreatePreviewOutput camId: %s, w: %d, h: %d, f: %d, stream: %d, isCapture: %d",
332            GetAnonyString(cameraId_).c_str(), info->width_, info->height_,
333            camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888, info->streamType_, info->isCapture_);
334     videoSurface_ = Surface::CreateSurfaceAsConsumer();
335     videoSurface_->SetDefaultWidthAndHeight(info->width_, info->height_);
336     videoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888));
337     videoListener_ = std::make_shared<DCameraVideoSurfaceListener>(videoSurface_, resultCallback_);
338     videoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)videoListener_);
339     previewOutput_ = cameraManager_->CreateCustomPreviewOutput(videoSurface_, info->width_, info->height_);
340     if (previewOutput_ == nullptr) {
341         DHLOGE("DCameraClientCommon::CreatePreviewOutput %s create preview output failed",
342             GetAnonyString(cameraId_).c_str());
343         return DCAMERA_BAD_VALUE;
344     }
345     std::shared_ptr<DCameraPreviewCallback> previewCallback = std::make_shared<DCameraPreviewCallback>(stateCallback_);
346     ((sptr<CameraStandard::PreviewOutput> &)previewOutput_)->SetCallback(previewCallback);
347     DHLOGI("DCameraClientCommon::CreatePreviewOutput %s success", GetAnonyString(cameraId_).c_str());
348     return DCAMERA_OK;
349 }
350 
StartCaptureInner(std::shared_ptr<DCameraCaptureInfo> & info)351 int32_t DCameraClient::StartCaptureInner(std::shared_ptr<DCameraCaptureInfo>& info)
352 {
353     switch (info->streamType_) {
354         case CONTINUOUS_FRAME: {
355             return StartVideoOutput();
356         }
357         case SNAPSHOT_FRAME: {
358             return StartPhotoOutput(info);
359         }
360         default: {
361             DHLOGE("DCameraClientCommon::StartCaptureInner unknown stream type");
362             return DCAMERA_BAD_VALUE;
363         }
364     }
365 }
366 
StartPhotoOutput(std::shared_ptr<DCameraCaptureInfo> & info)367 int32_t DCameraClient::StartPhotoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
368 {
369     DHLOGI("DCameraClientCommon::StartPhotoOutput cameraId: %s", GetAnonyString(cameraId_).c_str());
370     if (photoOutput_ == nullptr) {
371         DHLOGE("DCameraClientCommon::StartPhotoOutput photoOutput is null");
372         return DCAMERA_BAD_VALUE;
373     }
374     int32_t ret = ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->Capture();
375     if (ret != DCAMERA_OK) {
376         DHLOGE("DCameraClientCommon::StartPhotoOutput %s photoOutput capture failed, ret: %d",
377             GetAnonyString(cameraId_).c_str(), ret);
378         return ret;
379     }
380     return DCAMERA_OK;
381 }
382 
StartVideoOutput()383 int32_t DCameraClient::StartVideoOutput()
384 {
385     DHLOGI("DCameraClientCommon::StartVideoOutput cameraId: %s", GetAnonyString(cameraId_).c_str());
386     if (videoOutput_ == nullptr) {
387         DHLOGE("DCameraClientCommon::StartVideoOutput videoOutput is null");
388         return DCAMERA_BAD_VALUE;
389     }
390     int32_t ret = ((sptr<CameraStandard::VideoOutput> &)videoOutput_)->Start();
391     if (ret != DCAMERA_OK) {
392         DHLOGE("DCameraClientCommon::StartVideoOutput %s videoOutput start failed, ret: %d",
393             GetAnonyString(cameraId_).c_str(), ret);
394         return ret;
395     }
396     return DCAMERA_OK;
397 }
398 } // namespace DistributedHardware
399 } // namespace OHOS