• 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 "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_RETURN_RET_ELOG(captureSession == nullptr || !captureSession->IsSessionCommited(),
70         CameraErrorCode::SESSION_NOT_CONFIG, "DepthDataOutput Failed to Start!, session not config");
71     CHECK_RETURN_RET_ELOG(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     CHECK_PRINT_ELOG(itemStream == nullptr, "DepthDataOutput::Start itemStream is nullptr");
77     if (itemStream) {
78         errCode = itemStream->Start();
79         CHECK_PRINT_ELOG(errCode != CAMERA_OK, "DepthDataOutput Failed to Start!, errCode: %{public}d", errCode);
80     }
81     return ServiceToCameraError(errCode);
82 }
83 
Stop()84 int32_t DepthDataOutput::Stop()
85 {
86     std::lock_guard<std::mutex> lock(asyncOpMutex_);
87     MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::Stop");
88     CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
89         "DepthDataOutput Failed to Stop!, GetStream is nullptr");
90     auto stream = GetStream();
91     sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
92     int32_t errCode = CAMERA_UNKNOWN_ERROR;
93     CHECK_PRINT_ELOG(itemStream == nullptr, "DepthDataOutput::Stop itemStream is nullptr");
94     if (itemStream) {
95         errCode = itemStream->Stop();
96         CHECK_PRINT_ELOG(errCode != CAMERA_OK, "DepthDataOutput Failed to Stop!, errCode: %{public}d", errCode);
97     }
98     return ServiceToCameraError(errCode);
99 }
100 
SetDataAccuracy(int32_t dataAccuracy)101 int32_t DepthDataOutput::SetDataAccuracy(int32_t dataAccuracy)
102 {
103     CAMERA_SYNC_TRACE;
104     std::lock_guard<std::mutex> lock(asyncOpMutex_);
105     MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::SetDataAccuracy");
106     CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
107         "DepthDataOutput Failed to SetDataAccuracy!, GetStream is nullptr");
108     auto stream = GetStream();
109     sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
110     int32_t errCode = CAMERA_UNKNOWN_ERROR;
111     if (itemStream) {
112         errCode = itemStream->SetDataAccuracy(dataAccuracy);
113         CHECK_PRINT_ELOG(errCode != CAMERA_OK,
114             "DepthDataOutput Failed to SetDataAccuracy!, errCode: %{public}d", errCode);
115     } else {
116         MEDIA_ERR_LOG("DepthDataOutput::SetDataAccuracy itemStream is nullptr");
117     }
118     return ServiceToCameraError(errCode);
119 }
120 
CreateStream()121 int32_t DepthDataOutput::CreateStream()
122 {
123     MEDIA_INFO_LOG("DepthDataOutput::CreateStream enter");
124     return CameraErrorCode::SUCCESS;
125 }
126 
Release()127 int32_t DepthDataOutput::Release()
128 {
129     {
130         std::lock_guard<std::mutex> lock(outputCallbackMutex_);
131         svcCallback_ = nullptr;
132         appCallback_ = nullptr;
133     }
134     std::lock_guard<std::mutex> lock(asyncOpMutex_);
135     MEDIA_DEBUG_LOG("Enter Into DepthDataOutput::Release");
136     CHECK_RETURN_RET_ELOG(GetStream() == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
137         "DepthDataOutput Failed to Release!, GetStream is nullptr");
138     auto stream = GetStream();
139     sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
140     int32_t errCode = CAMERA_UNKNOWN_ERROR;
141     if (itemStream) {
142         errCode = itemStream->Release();
143         CHECK_PRINT_ELOG(errCode != CAMERA_OK, "Failed to release DepthDataOutput!, errCode: %{public}d", errCode);
144     } else {
145         MEDIA_ERR_LOG("DepthDataOutput::Release() itemStream is nullptr");
146     }
147     CaptureOutput::Release();
148     return ServiceToCameraError(errCode);
149 }
150 
SetCallback(std::shared_ptr<DepthDataStateCallback> callback)151 void DepthDataOutput::SetCallback(std::shared_ptr<DepthDataStateCallback> callback)
152 {
153     std::lock_guard<std::mutex> lock(outputCallbackMutex_);
154     appCallback_ = callback;
155     if (appCallback_ != nullptr) {
156         if (svcCallback_ == nullptr) {
157             svcCallback_ = new (std::nothrow) DepthDataOutputCallbackImpl(this);
158             if (svcCallback_ == nullptr) {
159                 MEDIA_ERR_LOG("new DepthDataOutputCallbackImpl Failed to register callback");
160                 appCallback_ = nullptr;
161                 return;
162             }
163         }
164         CHECK_RETURN_ELOG(GetStream() == nullptr, "DepthDataOutput Failed to SetCallback!, GetStream is nullptr");
165         auto stream = GetStream();
166         sptr<IStreamDepthData> itemStream = static_cast<IStreamDepthData*>(stream.GetRefPtr());
167         int32_t errorCode = CAMERA_OK;
168         if (itemStream) {
169             errorCode = itemStream->SetCallback(svcCallback_);
170         } else {
171             MEDIA_ERR_LOG("DepthDataOutput::SetCallback itemStream is nullptr");
172         }
173         if (errorCode != CAMERA_OK) {
174             MEDIA_ERR_LOG("DepthDataOutput::SetCallback Failed to register callback, errorCode: %{public}d", errorCode);
175             svcCallback_ = nullptr;
176             appCallback_ = nullptr;
177         }
178     }
179     return;
180 }
181 
GetApplicationCallback()182 std::shared_ptr<DepthDataStateCallback> DepthDataOutput::GetApplicationCallback()
183 {
184     std::lock_guard<std::mutex> lock(outputCallbackMutex_);
185     return appCallback_;
186 }
187 
CameraServerDied(pid_t pid)188 void DepthDataOutput::CameraServerDied(pid_t pid)
189 {
190     MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
191     std::lock_guard<std::mutex> lock(outputCallbackMutex_);
192     if (appCallback_ != nullptr) {
193         MEDIA_DEBUG_LOG("appCallback not nullptr");
194         int32_t serviceErrorType = ServiceToCameraError(CAMERA_INVALID_STATE);
195         appCallback_->OnDepthDataError(serviceErrorType);
196     }
197 }
198 } // namespace CameraStandard
199 } // namespace OHOS
200