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/preview_output.h"
17 #include "camera_util.h"
18 #include "hstream_repeat_callback_stub.h"
19 #include "camera_log.h"
20
21 namespace OHOS {
22 namespace CameraStandard {
PreviewOutput(sptr<IStreamRepeat> & streamRepeat)23 PreviewOutput::PreviewOutput(sptr<IStreamRepeat> &streamRepeat)
24 : CaptureOutput(CAPTURE_OUTPUT_TYPE_PREVIEW, StreamType::REPEAT, streamRepeat) {
25 }
26
~PreviewOutput()27 PreviewOutput::~PreviewOutput()
28 {
29 svcCallback_ = nullptr;
30 appCallback_ = nullptr;
31 }
32
Release()33 int32_t PreviewOutput::Release()
34 {
35 std::lock_guard<std::mutex> lock(asyncOpMutex_);
36 if (GetStream() == nullptr) {
37 MEDIA_ERR_LOG("PreviewOutput Failed to Release!, GetStream is nullptr");
38 return CameraErrorCode::SERVICE_FATL_ERROR;
39 }
40 auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
41 int32_t errCode = CAMERA_UNKNOWN_ERROR;
42 if (itemStream) {
43 errCode = itemStream->Release();
44 if (errCode != CAMERA_OK) {
45 MEDIA_ERR_LOG("Failed to release PreviewOutput!, errCode: %{public}d", errCode);
46 }
47 } else {
48 MEDIA_ERR_LOG("PreviewOutput::Release() itemStream is nullptr");
49 }
50 svcCallback_ = nullptr;
51 appCallback_ = nullptr;
52 CaptureOutput::Release();
53 return ServiceToCameraError(errCode);
54 }
55
56 class HStreamRepeatCallbackImpl : public HStreamRepeatCallbackStub {
57 public:
58 sptr<PreviewOutput> previewOutput_ = nullptr;
HStreamRepeatCallbackImpl()59 HStreamRepeatCallbackImpl() : previewOutput_(nullptr) {
60 }
61
HStreamRepeatCallbackImpl(const sptr<PreviewOutput> & previewOutput)62 explicit HStreamRepeatCallbackImpl(const sptr<PreviewOutput>& previewOutput) : previewOutput_(previewOutput) {
63 }
64
~HStreamRepeatCallbackImpl()65 ~HStreamRepeatCallbackImpl()
66 {
67 previewOutput_ = nullptr;
68 }
69
OnFrameStarted()70 int32_t OnFrameStarted() override
71 {
72 CAMERA_SYNC_TRACE;
73 if (previewOutput_ != nullptr && previewOutput_->GetApplicationCallback() != nullptr) {
74 previewOutput_->GetApplicationCallback()->OnFrameStarted();
75 } else {
76 MEDIA_INFO_LOG("Discarding HStreamRepeatCallbackImpl::OnFrameStarted callback in preview");
77 }
78 return CAMERA_OK;
79 }
80
OnFrameEnded(int32_t frameCount)81 int32_t OnFrameEnded(int32_t frameCount) override
82 {
83 CAMERA_SYNC_TRACE;
84 if (previewOutput_ != nullptr && previewOutput_->GetApplicationCallback() != nullptr) {
85 previewOutput_->GetApplicationCallback()->OnFrameEnded(frameCount);
86 } else {
87 MEDIA_INFO_LOG("Discarding HStreamRepeatCallbackImpl::OnFrameEnded callback in preview");
88 }
89 return CAMERA_OK;
90 }
91
OnFrameError(int32_t errorCode)92 int32_t OnFrameError(int32_t errorCode) override
93 {
94 if (previewOutput_ != nullptr && previewOutput_->GetApplicationCallback() != nullptr) {
95 previewOutput_->GetApplicationCallback()->OnError(errorCode);
96 } else {
97 MEDIA_INFO_LOG("Discarding HStreamRepeatCallbackImpl::OnFrameError callback in preview");
98 }
99 return CAMERA_OK;
100 }
101 };
102
AddDeferredSurface(sptr<Surface> surface)103 void PreviewOutput::AddDeferredSurface(sptr<Surface> surface)
104 {
105 if (surface == nullptr) {
106 MEDIA_ERR_LOG("PreviewOutput::AddDeferredSurface surface is null");
107 return;
108 }
109 auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
110 if (!itemStream) {
111 MEDIA_ERR_LOG("PreviewOutput::AddDeferredSurface itemStream is nullptr");
112 return;
113 }
114 itemStream->AddDeferredSurface(surface->GetProducer());
115 }
116
Start()117 int32_t PreviewOutput::Start()
118 {
119 std::lock_guard<std::mutex> lock(asyncOpMutex_);
120 CaptureSession* captureSession = GetSession();
121 if (captureSession == nullptr || !captureSession->IsSessionCommited()) {
122 MEDIA_ERR_LOG("PreviewOutput Failed to Start!, session not config");
123 return CameraErrorCode::SESSION_NOT_CONFIG;
124 }
125 if (GetStream() == nullptr) {
126 MEDIA_ERR_LOG("PreviewOutput Failed to Start!, GetStream is nullptr");
127 return CameraErrorCode::SERVICE_FATL_ERROR;
128 }
129 auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
130 int32_t errCode = CAMERA_UNKNOWN_ERROR;
131 if (itemStream) {
132 errCode = itemStream->Start();
133 if (errCode != CAMERA_OK) {
134 MEDIA_ERR_LOG("PreviewOutput Failed to Start!, errCode: %{public}d", errCode);
135 }
136 } else {
137 MEDIA_ERR_LOG("PreviewOutput::Start itemStream is nullptr");
138 }
139 return ServiceToCameraError(errCode);
140 }
141
Stop()142 int32_t PreviewOutput::Stop()
143 {
144 std::lock_guard<std::mutex> lock(asyncOpMutex_);
145 if (GetStream() == nullptr) {
146 MEDIA_ERR_LOG("PreviewOutput Failed to Stop!, GetStream is nullptr");
147 return CameraErrorCode::SERVICE_FATL_ERROR;
148 }
149 auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
150 int32_t errCode = CAMERA_UNKNOWN_ERROR;
151 if (itemStream) {
152 errCode = itemStream->Stop();
153 if (errCode != CAMERA_OK) {
154 MEDIA_ERR_LOG("PreviewOutput Failed to Stop!, errCode: %{public}d", errCode);
155 }
156 } else {
157 MEDIA_ERR_LOG("PreviewOutput::Stop itemStream is nullptr");
158 }
159 return ServiceToCameraError(errCode);
160 }
161
SetCallback(std::shared_ptr<PreviewStateCallback> callback)162 void PreviewOutput::SetCallback(std::shared_ptr<PreviewStateCallback> callback)
163 {
164 appCallback_ = callback;
165 if (appCallback_ != nullptr) {
166 if (svcCallback_ == nullptr) {
167 svcCallback_ = new(std::nothrow) HStreamRepeatCallbackImpl(this);
168 if (svcCallback_ == nullptr) {
169 MEDIA_ERR_LOG("PreviewOutput::SetCallback: new HStreamRepeatCallbackImpl Failed to register callback");
170 appCallback_ = nullptr;
171 return;
172 }
173 }
174 if (GetStream() == nullptr) {
175 MEDIA_ERR_LOG("PreviewOutput Failed to SetCallback!, GetStream is nullptr");
176 return;
177 }
178 auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
179 int32_t errorCode = CAMERA_OK;
180 if (itemStream) {
181 errorCode = itemStream->SetCallback(svcCallback_);
182 } else {
183 MEDIA_ERR_LOG("PreviewOutput::SetCallback itemStream is nullptr");
184 }
185 if (errorCode != CAMERA_OK) {
186 MEDIA_ERR_LOG("PreviewOutput::SetCallback: Failed to register callback, errorCode: %{public}d", errorCode);
187 svcCallback_ = nullptr;
188 appCallback_ = nullptr;
189 }
190 }
191 return;
192 }
193
GetApplicationCallback()194 std::shared_ptr<PreviewStateCallback> PreviewOutput::GetApplicationCallback()
195 {
196 return appCallback_;
197 }
198 } // CameraStandard
199 } // OHOS
200
201