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