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