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 CHECK_RETURN(!innerVideoOutput_);
78 innerVideoOutput_ = nullptr;
79 }
80
RegisterCallback(VideoOutput_Callbacks * callback)81 Camera_ErrorCode Camera_VideoOutput::RegisterCallback(VideoOutput_Callbacks* callback)
82 {
83 shared_ptr<InnerVideoOutputCallback> innerCallback =
84 make_shared<InnerVideoOutputCallback>(this, callback);
85 innerVideoOutput_->SetCallback(innerCallback);
86 return CAMERA_OK;
87 }
88
UnregisterCallback(VideoOutput_Callbacks * callback)89 Camera_ErrorCode Camera_VideoOutput::UnregisterCallback(VideoOutput_Callbacks* callback)
90 {
91 innerVideoOutput_->SetCallback(nullptr);
92 return CAMERA_OK;
93 }
94
Start()95 Camera_ErrorCode Camera_VideoOutput::Start()
96 {
97 int32_t ret = innerVideoOutput_->Start();
98 return FrameworkToNdkCameraError(ret);
99 }
100
Stop()101 Camera_ErrorCode Camera_VideoOutput::Stop()
102 {
103 int32_t ret = innerVideoOutput_->Stop();
104 return FrameworkToNdkCameraError(ret);
105 }
106
Release()107 Camera_ErrorCode Camera_VideoOutput::Release()
108 {
109 int32_t ret = innerVideoOutput_->Release();
110 return FrameworkToNdkCameraError(ret);
111 }
112
GetInnerVideoOutput()113 sptr<VideoOutput> Camera_VideoOutput::GetInnerVideoOutput()
114 {
115 return innerVideoOutput_;
116 }
117
GetVideoProfile(Camera_VideoProfile ** profile)118 Camera_ErrorCode Camera_VideoOutput::GetVideoProfile(Camera_VideoProfile** profile)
119 {
120 auto videoOutputProfile = innerVideoOutput_->GetVideoProfile();
121 CHECK_RETURN_RET_ELOG(videoOutputProfile == nullptr, CAMERA_SERVICE_FATAL_ERROR,
122 "Camera_VideoOutput::GetVideoProfile failed to get video profile!");
123
124 CameraFormat cameraFormat = videoOutputProfile->GetCameraFormat();
125 auto itr = g_fwToNdkCameraFormat.find(cameraFormat);
126 CHECK_RETURN_RET_ELOG(itr == g_fwToNdkCameraFormat.end(), CAMERA_SERVICE_FATAL_ERROR,
127 "Camera_VideoOutput::GetVideoProfile unsupported camera format %{public}d", cameraFormat);
128
129 auto frames = videoOutputProfile->GetFrameRates();
130 CHECK_RETURN_RET_ELOG(frames.size() <= 1, CAMERA_SERVICE_FATAL_ERROR,
131 "Camera_VideoOutput::GetVideoProfile the current video profile is not configured correctly!");
132
133 Camera_VideoProfile* newProfile = new Camera_VideoProfile;
134 CHECK_RETURN_RET_ELOG(newProfile == nullptr, CAMERA_SERVICE_FATAL_ERROR,
135 "Camera_VideoOutput::GetVideoProfile failed to allocate memory for video profile!");
136
137 newProfile->format = itr->second;
138 newProfile->range.min = static_cast<uint32_t>(frames[0]);
139 newProfile->range.max = static_cast<uint32_t>(frames[1]);
140 newProfile->size.width = videoOutputProfile->GetSize().width;
141 newProfile->size.height = videoOutputProfile->GetSize().height;
142
143 *profile = newProfile;
144 return CAMERA_OK;
145 }
146
GetSupportedFrameRates(Camera_FrameRateRange ** frameRateRange,uint32_t * size)147 Camera_ErrorCode Camera_VideoOutput::GetSupportedFrameRates(Camera_FrameRateRange** frameRateRange, uint32_t* size)
148 {
149 std::vector<std::vector<int32_t>> frameRate = innerVideoOutput_->GetSupportedFrameRates();
150 if (frameRate.size() == 0) {
151 *size = 0;
152 return CAMERA_OK;
153 }
154
155 Camera_FrameRateRange* newframeRateRange = new Camera_FrameRateRange[frameRate.size()];
156 CHECK_RETURN_RET_ELOG(newframeRateRange == nullptr, CAMERA_SERVICE_FATAL_ERROR,
157 "Failed to allocate memory for Camera_FrameRateRange!");
158
159 for (size_t index = 0; index < frameRate.size(); ++index) {
160 if (frameRate[index].size() <= 1) {
161 MEDIA_ERR_LOG("invalid frameRate size!");
162 delete[] newframeRateRange;
163 newframeRateRange = nullptr;
164 return CAMERA_SERVICE_FATAL_ERROR;
165 }
166 newframeRateRange[index].min = static_cast<uint32_t>(frameRate[index][0]);
167 newframeRateRange[index].max = static_cast<uint32_t>(frameRate[index][1]);
168 }
169
170 *frameRateRange = newframeRateRange;
171 *size = frameRate.size();
172 return CAMERA_OK;
173 }
174
DeleteFrameRates(Camera_FrameRateRange * frameRateRange)175 Camera_ErrorCode Camera_VideoOutput::DeleteFrameRates(Camera_FrameRateRange* frameRateRange)
176 {
177 if (frameRateRange != nullptr) {
178 delete[] frameRateRange;
179 frameRateRange = nullptr;
180 }
181
182 return CAMERA_OK;
183 }
184
SetFrameRate(int32_t minFps,int32_t maxFps)185 Camera_ErrorCode Camera_VideoOutput::SetFrameRate(int32_t minFps, int32_t maxFps)
186 {
187 int32_t ret = innerVideoOutput_->SetFrameRate(minFps, maxFps);
188 return FrameworkToNdkCameraError(ret);
189 }
190
GetActiveFrameRate(Camera_FrameRateRange * frameRateRange)191 Camera_ErrorCode Camera_VideoOutput::GetActiveFrameRate(Camera_FrameRateRange* frameRateRange)
192 {
193 std::vector<int32_t> activeFrameRate = innerVideoOutput_->GetFrameRateRange();
194 CHECK_RETURN_RET_ELOG(activeFrameRate.size() <= 1, CAMERA_SERVICE_FATAL_ERROR,
195 "invalid activeFrameRate size!");
196
197 frameRateRange->min = static_cast<uint32_t>(activeFrameRate[0]);
198 frameRateRange->max = static_cast<uint32_t>(activeFrameRate[1]);
199
200 return CAMERA_OK;
201 }
202
IsMirrorSupported(bool * isSupported)203 Camera_ErrorCode Camera_VideoOutput::IsMirrorSupported(bool* isSupported)
204 {
205 CHECK_RETURN_RET_ELOG(innerVideoOutput_ == nullptr, CAMERA_SERVICE_FATAL_ERROR,
206 "innerVideoOutput_ is nullptr");
207 CHECK_RETURN_RET_ELOG(isSupported == nullptr, CAMERA_SERVICE_FATAL_ERROR,
208 "isSupported is nullptr");
209 *isSupported = innerVideoOutput_->IsMirrorSupported();
210 return CAMERA_OK;
211 }
212
EnableMirror(bool mirrorMode)213 Camera_ErrorCode Camera_VideoOutput::EnableMirror(bool mirrorMode)
214 {
215 CHECK_RETURN_RET_ELOG(innerVideoOutput_ == nullptr, CAMERA_SERVICE_FATAL_ERROR,
216 "innerVideoOutput_ is nullptr");
217 int32_t ret = innerVideoOutput_->enableMirror(mirrorMode);
218 return FrameworkToNdkCameraError(ret);
219 }
220
GetVideoRotation(int32_t imageRotation,Camera_ImageRotation * cameraImageRotation)221 Camera_ErrorCode Camera_VideoOutput::GetVideoRotation(int32_t imageRotation, Camera_ImageRotation* cameraImageRotation)
222 {
223 CHECK_RETURN_RET_ELOG(cameraImageRotation == nullptr, CAMERA_SERVICE_FATAL_ERROR,
224 "GetCameraImageRotation failed");
225 CHECK_RETURN_RET_ELOG(innerVideoOutput_ == nullptr, CAMERA_SERVICE_FATAL_ERROR,
226 "innerVideoOutput_ is nullptr");
227 int32_t cameraOutputRotation = innerVideoOutput_->GetVideoRotation(imageRotation);
228 CHECK_RETURN_RET_ELOG(cameraOutputRotation == CAMERA_SERVICE_FATAL_ERROR, CAMERA_SERVICE_FATAL_ERROR,
229 "Camera_VideoOutput::GetVideoRotation camera service fatal error! ret: %{public}d", cameraOutputRotation);
230 *cameraImageRotation = static_cast<Camera_ImageRotation>(cameraOutputRotation);
231 return CAMERA_OK;
232 }