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 "output/depth_data_output.h"
17
18 #include <cstdint>
19 #include <limits>
20 #include <memory>
21 #include <utility>
22 #include <variant>
23
24 #include "camera_error_code.h"
25 #include "camera_log.h"
26 #include "camera_output_capability.h"
27 #include "camera_util.h"
28 #include "session/capture_session.h"
29
30 namespace OHOS {
31 namespace CameraStandard {
32
OnDepthDataError(int32_t errorCode)33 int32_t DepthDataOutputCallbackImpl::OnDepthDataError(int32_t errorCode)
34 {
35 auto item = depthDataOutput_.promote();
36 if (item != nullptr) {
37 auto callback = item->GetApplicationCallback();
38 if (callback != nullptr) {
39 callback->OnDepthDataError(errorCode);
40 } else {
41 MEDIA_INFO_LOG("Discarding DepthDataOutputCallbackImpl::OnDepthDataError callback in depthoutput");
42 }
43 } else {
44 MEDIA_INFO_LOG("DepthDataOutputCallbackImpl::OnDepthDataError DepthDataOutput is nullptr");
45 }
46 return CAMERA_OK;
47 }
48
DepthDataOutput(sptr<IBufferProducer> bufferProducer)49 DepthDataOutput::DepthDataOutput(sptr<IBufferProducer> bufferProducer)
50 : CaptureOutput(CAPTURE_OUTPUT_TYPE_DEPTH_DATA, StreamType::DEPTH, bufferProducer, nullptr)
51 {
52 DepthDataFormat_ = 0;
53 DepthDataSize_.height = 0;
54 DepthDataSize_.width = 0;
55 }
56
~DepthDataOutput()57 DepthDataOutput::~DepthDataOutput()
58 {
59 MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::~DepthDataOutput()");
60 }
61
Start()62 int32_t DepthDataOutput::Start()
63 {
64 CAMERA_SYNC_TRACE;
65 SetDataAccuracy(GetDepthProfile()->GetDataAccuracy());
66 std::lock_guard<std::mutex> lock(asyncOpMutex_);
67 MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::Start");
68 auto captureSession = GetSession();
69 CHECK_ERROR_RETURN_RET_LOG(captureSession == nullptr || !captureSession->IsSessionCommited(),
70 CameraErrorCode::SESSION_NOT_CONFIG, "DepthDataOutput Failed to Start!, session not config");
71 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
72 "DepthDataOutput Failed to Start!, GetStream is nullptr");
73 auto stream = GetStream();
74 sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
75 int32_t errCode = CAMERA_UNKNOWN_ERROR;
76 if (itemStream) {
77 errCode = itemStream->Start();
78 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "DepthDataOutput Failed to Start!, errCode: %{public}d", errCode);
79 } else {
80 MEDIA_ERR_LOG("DepthDataOutput::Start itemStream is nullptr");
81 }
82 return ServiceToCameraError(errCode);
83 }
84
Stop()85 int32_t DepthDataOutput::Stop()
86 {
87 std::lock_guard<std::mutex> lock(asyncOpMutex_);
88 MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::Stop");
89 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
90 "DepthDataOutput Failed to Stop!, GetStream is nullptr");
91 auto stream = GetStream();
92 sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
93 int32_t errCode = CAMERA_UNKNOWN_ERROR;
94 if (itemStream) {
95 errCode = itemStream->Stop();
96 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "DepthDataOutput Failed to Stop!, errCode: %{public}d", errCode);
97 } else {
98 MEDIA_ERR_LOG("DepthDataOutput::Stop itemStream is nullptr");
99 }
100 return ServiceToCameraError(errCode);
101 }
102
SetDataAccuracy(int32_t dataAccuracy)103 int32_t DepthDataOutput::SetDataAccuracy(int32_t dataAccuracy)
104 {
105 CAMERA_SYNC_TRACE;
106 std::lock_guard<std::mutex> lock(asyncOpMutex_);
107 MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::SetDataAccuracy");
108 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
109 "DepthDataOutput Failed to SetDataAccuracy!, GetStream is nullptr");
110 auto stream = GetStream();
111 sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
112 int32_t errCode = CAMERA_UNKNOWN_ERROR;
113 if (itemStream) {
114 errCode = itemStream->SetDataAccuracy(dataAccuracy);
115 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK,
116 "DepthDataOutput Failed to SetDataAccuracy!, errCode: %{public}d", errCode);
117 } else {
118 MEDIA_ERR_LOG("DepthDataOutput::SetDataAccuracy itemStream is nullptr");
119 }
120 return ServiceToCameraError(errCode);
121 }
122
CreateStream()123 int32_t DepthDataOutput::CreateStream()
124 {
125 MEDIA_INFO_LOG("DepthDataOutput::CreateStream enter");
126 return CameraErrorCode::SUCCESS;
127 }
128
Release()129 int32_t DepthDataOutput::Release()
130 {
131 {
132 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
133 svcCallback_ = nullptr;
134 appCallback_ = nullptr;
135 }
136 std::lock_guard<std::mutex> lock(asyncOpMutex_);
137 MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::Release");
138 CHECK_ERROR_RETURN_RET_LOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
139 "DepthDataOutput Failed to Release!, GetStream is nullptr");
140 auto stream = GetStream();
141 sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
142 int32_t errCode = CAMERA_UNKNOWN_ERROR;
143 if (itemStream) {
144 errCode = itemStream->Release();
145 CHECK_ERROR_PRINT_LOG(errCode != CAMERA_OK, "Failed to release DepthDataOutput!, errCode: %{public}d", errCode);
146 } else {
147 MEDIA_ERR_LOG("DepthDataOutput::Release() itemStream is nullptr");
148 }
149 CaptureOutput::Release();
150 return ServiceToCameraError(errCode);
151 }
152
SetCallback(std::shared_ptr<DepthDataStateCallback> callback)153 void DepthDataOutput::SetCallback(std::shared_ptr<DepthDataStateCallback> callback)
154 {
155 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
156 appCallback_ = callback;
157 if (appCallback_ != nullptr) {
158 if (svcCallback_ == nullptr) {
159 svcCallback_ = new (std::nothrow) DepthDataOutputCallbackImpl(this);
160 if (svcCallback_ == nullptr) {
161 MEDIA_ERR_LOG("new DepthDataOutputCallbackImpl Failed to register callback");
162 appCallback_ = nullptr;
163 return;
164 }
165 }
166 if (GetStream() == nullptr) {
167 MEDIA_ERR_LOG("DepthDataOutput Failed to SetCallback!, GetStream is nullptr");
168 return;
169 }
170 auto stream = GetStream();
171 sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
172 int32_t errorCode = CAMERA_OK;
173 if (itemStream) {
174 errorCode = itemStream->SetCallback(svcCallback_);
175 } else {
176 MEDIA_ERR_LOG("DepthDataOutput::SetCallback itemStream is nullptr");
177 }
178 if (errorCode != CAMERA_OK) {
179 MEDIA_ERR_LOG("DepthDataOutput::SetCallback Failed to register callback, errorCode: %{public}d", errorCode);
180 svcCallback_ = nullptr;
181 appCallback_ = nullptr;
182 }
183 }
184 return;
185 }
186
GetApplicationCallback()187 std::shared_ptr<DepthDataStateCallback> DepthDataOutput::GetApplicationCallback()
188 {
189 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
190 return appCallback_;
191 }
192
CameraServerDied(pid_t pid)193 void DepthDataOutput::CameraServerDied(pid_t pid)
194 {
195 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
196 std::lock_guard<std::mutex> lock(outputCallbackMutex_);
197 if (appCallback_ != nullptr) {
198 MEDIA_DEBUG_LOG("appCallback not nullptr");
199 int32_t serviceErrorType = ServiceToCameraError(CAMERA_INVALID_STATE);
200 appCallback_->OnDepthDataError(serviceErrorType);
201 }
202 }
203 } // namespace CameraStandard
204 } // namespace OHOS
205