1 /*
2 * Copyright (c) 2023-2023 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 "video_output_impl.h"
17 #include "camera_log.h"
18 #include "camera_util.h"
19
20 using namespace std;
21 using namespace OHOS;
22 using namespace OHOS::CameraStandard;
23 const std::unordered_map<CameraFormat, Camera_Format> g_fwToNdkCameraFormat = {
24 {CameraFormat::CAMERA_FORMAT_RGBA_8888, Camera_Format::CAMERA_FORMAT_RGBA_8888},
25 {CameraFormat::CAMERA_FORMAT_YUV_420_SP, Camera_Format::CAMERA_FORMAT_YUV_420_SP},
26 {CameraFormat::CAMERA_FORMAT_JPEG, Camera_Format::CAMERA_FORMAT_JPEG},
27 {CameraFormat::CAMERA_FORMAT_YCBCR_P010, Camera_Format::CAMERA_FORMAT_YCBCR_P010},
28 {CameraFormat::CAMERA_FORMAT_YCRCB_P010, Camera_Format::CAMERA_FORMAT_YCRCB_P010}
29 };
30
31 class InnerVideoOutputCallback : public VideoStateCallback {
32 public:
InnerVideoOutputCallback(Camera_VideoOutput * videoOutput,VideoOutput_Callbacks * callback)33 InnerVideoOutputCallback(Camera_VideoOutput* videoOutput, VideoOutput_Callbacks* callback)
34 : videoOutput_(videoOutput), callback_(*callback) {}
35 ~InnerVideoOutputCallback() = default;
36
OnFrameStarted() const37 void OnFrameStarted() const override
38 {
39 MEDIA_DEBUG_LOG("OnFrameStarted is called!");
40 CHECK_EXECUTE(videoOutput_ != nullptr && callback_.onFrameStart != nullptr,
41 callback_.onFrameStart(videoOutput_));
42 }
43
OnFrameEnded(const int32_t frameCount) const44 void OnFrameEnded(const int32_t frameCount) const override
45 {
46 MEDIA_DEBUG_LOG("OnFrameEnded is called! frame count: %{public}d", frameCount);
47 CHECK_EXECUTE(videoOutput_ != nullptr && callback_.onFrameEnd != nullptr,
48 callback_.onFrameEnd(videoOutput_, frameCount));
49 }
50
OnError(const int32_t errorCode) const51 void OnError(const int32_t errorCode) const override
52 {
53 MEDIA_DEBUG_LOG("OnError is called!, errorCode: %{public}d", errorCode);
54 CHECK_EXECUTE(videoOutput_ != nullptr && callback_.onError != nullptr,
55 callback_.onError(videoOutput_, FrameworkToNdkCameraError(errorCode)));
56 }
57
OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const58 void OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const override
59 {
60 // empty impl
61 MEDIA_DEBUG_LOG("OnDeferredVideoEnhancementInfo is called!");
62 }
63
64 private:
65 Camera_VideoOutput* videoOutput_;
66 VideoOutput_Callbacks callback_;
67 };
68
Camera_VideoOutput(sptr<VideoOutput> & innerVideoOutput)69 Camera_VideoOutput::Camera_VideoOutput(sptr<VideoOutput> &innerVideoOutput) : innerVideoOutput_(innerVideoOutput)
70 {
71 MEDIA_DEBUG_LOG("Camera_VideoOutput Constructor is called");
72 }
73
~Camera_VideoOutput()74 Camera_VideoOutput::~Camera_VideoOutput()
75 {
76 MEDIA_DEBUG_LOG("~Camera_VideoOutput is called");
77 if (innerVideoOutput_) {
78 innerVideoOutput_ = nullptr;
79 }
80 }
81
RegisterCallback(VideoOutput_Callbacks * callback)82 Camera_ErrorCode Camera_VideoOutput::RegisterCallback(VideoOutput_Callbacks* callback)
83 {
84 shared_ptr<InnerVideoOutputCallback> innerCallback =
85 make_shared<InnerVideoOutputCallback>(this, callback);
86 innerVideoOutput_->SetCallback(innerCallback);
87 return CAMERA_OK;
88 }
89
UnregisterCallback(VideoOutput_Callbacks * callback)90 Camera_ErrorCode Camera_VideoOutput::UnregisterCallback(VideoOutput_Callbacks* callback)
91 {
92 innerVideoOutput_->SetCallback(nullptr);
93 return CAMERA_OK;
94 }
95
Start()96 Camera_ErrorCode Camera_VideoOutput::Start()
97 {
98 int32_t ret = innerVideoOutput_->Start();
99 return FrameworkToNdkCameraError(ret);
100 }
101
Stop()102 Camera_ErrorCode Camera_VideoOutput::Stop()
103 {
104 int32_t ret = innerVideoOutput_->Stop();
105 return FrameworkToNdkCameraError(ret);
106 }
107
Release()108 Camera_ErrorCode Camera_VideoOutput::Release()
109 {
110 int32_t ret = innerVideoOutput_->Release();
111 return FrameworkToNdkCameraError(ret);
112 }
113
GetInnerVideoOutput()114 sptr<VideoOutput> Camera_VideoOutput::GetInnerVideoOutput()
115 {
116 return innerVideoOutput_;
117 }
118
GetVideoProfile(Camera_VideoProfile ** profile)119 Camera_ErrorCode Camera_VideoOutput::GetVideoProfile(Camera_VideoProfile** profile)
120 {
121 auto videoOutputProfile = innerVideoOutput_->GetVideoProfile();
122 CHECK_ERROR_RETURN_RET_LOG(videoOutputProfile == nullptr, CAMERA_SERVICE_FATAL_ERROR,
123 "Camera_VideoOutput::GetVideoProfile failed to get video profile!");
124
125 CameraFormat cameraFormat = videoOutputProfile->GetCameraFormat();
126 auto itr = g_fwToNdkCameraFormat.find(cameraFormat);
127 CHECK_ERROR_RETURN_RET_LOG(itr == g_fwToNdkCameraFormat.end(), CAMERA_SERVICE_FATAL_ERROR,
128 "Camera_VideoOutput::GetVideoProfile unsupported camera format %{public}d", cameraFormat);
129
130 auto frames = videoOutputProfile->GetFrameRates();
131 CHECK_ERROR_RETURN_RET_LOG(frames.size() <= 1, CAMERA_SERVICE_FATAL_ERROR,
132 "Camera_VideoOutput::GetVideoProfile the current video profile is not configured correctly!");
133
134 Camera_VideoProfile* newProfile = new Camera_VideoProfile;
135 CHECK_ERROR_RETURN_RET_LOG(newProfile == nullptr, CAMERA_SERVICE_FATAL_ERROR,
136 "Camera_VideoOutput::GetVideoProfile failed to allocate memory for video profile!");
137
138 newProfile->format = itr->second;
139 newProfile->range.min = static_cast<uint32_t>(frames[0]);
140 newProfile->range.max = static_cast<uint32_t>(frames[1]);
141 newProfile->size.width = videoOutputProfile->GetSize().width;
142 newProfile->size.height = videoOutputProfile->GetSize().height;
143
144 *profile = newProfile;
145 return CAMERA_OK;
146 }
147
GetSupportedFrameRates(Camera_FrameRateRange ** frameRateRange,uint32_t * size)148 Camera_ErrorCode Camera_VideoOutput::GetSupportedFrameRates(Camera_FrameRateRange** frameRateRange, uint32_t* size)
149 {
150 std::vector<std::vector<int32_t>> frameRate = innerVideoOutput_->GetSupportedFrameRates();
151 if (frameRate.size() == 0) {
152 *size = 0;
153 return CAMERA_OK;
154 }
155
156 Camera_FrameRateRange* newframeRateRange = new Camera_FrameRateRange[frameRate.size()];
157 CHECK_ERROR_RETURN_RET_LOG(newframeRateRange == nullptr, CAMERA_SERVICE_FATAL_ERROR,
158 "Failed to allocate memory for Camera_FrameRateRange!");
159
160 for (size_t index = 0; index < frameRate.size(); ++index) {
161 if (frameRate[index].size() <= 1) {
162 MEDIA_ERR_LOG("invalid frameRate size!");
163 delete[] newframeRateRange;
164 newframeRateRange = nullptr;
165 return CAMERA_SERVICE_FATAL_ERROR;
166 }
167 newframeRateRange[index].min = static_cast<uint32_t>(frameRate[index][0]);
168 newframeRateRange[index].max = static_cast<uint32_t>(frameRate[index][1]);
169 }
170
171 *frameRateRange = newframeRateRange;
172 *size = frameRate.size();
173 return CAMERA_OK;
174 }
175
DeleteFrameRates(Camera_FrameRateRange * frameRateRange)176 Camera_ErrorCode Camera_VideoOutput::DeleteFrameRates(Camera_FrameRateRange* frameRateRange)
177 {
178 if (frameRateRange != nullptr) {
179 delete[] frameRateRange;
180 frameRateRange = nullptr;
181 }
182
183 return CAMERA_OK;
184 }
185
SetFrameRate(int32_t minFps,int32_t maxFps)186 Camera_ErrorCode Camera_VideoOutput::SetFrameRate(int32_t minFps, int32_t maxFps)
187 {
188 int32_t ret = innerVideoOutput_->SetFrameRate(minFps, maxFps);
189 return FrameworkToNdkCameraError(ret);
190 }
191
GetActiveFrameRate(Camera_FrameRateRange * frameRateRange)192 Camera_ErrorCode Camera_VideoOutput::GetActiveFrameRate(Camera_FrameRateRange* frameRateRange)
193 {
194 std::vector<int32_t> activeFrameRate = innerVideoOutput_->GetFrameRateRange();
195 CHECK_ERROR_RETURN_RET_LOG(activeFrameRate.size() <= 1, CAMERA_SERVICE_FATAL_ERROR,
196 "invalid activeFrameRate size!");
197
198 frameRateRange->min = static_cast<uint32_t>(activeFrameRate[0]);
199 frameRateRange->max = static_cast<uint32_t>(activeFrameRate[1]);
200
201 return CAMERA_OK;
202 }
203
IsMirrorSupported(bool * isSupported)204 Camera_ErrorCode Camera_VideoOutput::IsMirrorSupported(bool* isSupported)
205 {
206 CHECK_ERROR_RETURN_RET_LOG(innerVideoOutput_ == nullptr, CAMERA_SERVICE_FATAL_ERROR,
207 "innerVideoOutput_ is nullptr");
208 CHECK_ERROR_RETURN_RET_LOG(isSupported == nullptr, CAMERA_SERVICE_FATAL_ERROR,
209 "isSupported is nullptr");
210 *isSupported = innerVideoOutput_->IsMirrorSupported();
211 return CAMERA_OK;
212 }
213
EnableMirror(bool mirrorMode)214 Camera_ErrorCode Camera_VideoOutput::EnableMirror(bool mirrorMode)
215 {
216 CHECK_ERROR_RETURN_RET_LOG(innerVideoOutput_ == nullptr, CAMERA_SERVICE_FATAL_ERROR,
217 "innerVideoOutput_ is nullptr");
218 int32_t ret = innerVideoOutput_->enableMirror(mirrorMode);
219 return FrameworkToNdkCameraError(ret);
220 }
221
GetVideoRotation(int32_t imageRotation,Camera_ImageRotation * cameraImageRotation)222 Camera_ErrorCode Camera_VideoOutput::GetVideoRotation(int32_t imageRotation, Camera_ImageRotation* cameraImageRotation)
223 {
224 CHECK_ERROR_RETURN_RET_LOG(cameraImageRotation == nullptr, CAMERA_SERVICE_FATAL_ERROR,
225 "GetCameraImageRotation failed");
226 CHECK_ERROR_RETURN_RET_LOG(innerVideoOutput_ == nullptr, CAMERA_SERVICE_FATAL_ERROR,
227 "innerVideoOutput_ is nullptr");
228 int32_t cameraOutputRotation = innerVideoOutput_->GetVideoRotation(imageRotation);
229 CHECK_ERROR_RETURN_RET_LOG(cameraOutputRotation == CAMERA_SERVICE_FATAL_ERROR, CAMERA_SERVICE_FATAL_ERROR,
230 "Camera_VideoOutput::GetVideoRotation camera service fatal error! ret: %{public}d", cameraOutputRotation);
231 *cameraImageRotation = static_cast<Camera_ImageRotation>(cameraOutputRotation);
232 return CAMERA_OK;
233 }