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