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 "preview_output_impl.h"
17 #include "camera_log.h"
18 #include "camera_output_capability.h"
19 #include "camera_util.h"
20
21 using namespace std;
22 using namespace OHOS;
23 using namespace OHOS::CameraStandard;
24 const std::unordered_map<CameraFormat, Camera_Format> g_fwToNdkCameraFormat = {
25 {CameraFormat::CAMERA_FORMAT_RGBA_8888, Camera_Format::CAMERA_FORMAT_RGBA_8888},
26 {CameraFormat::CAMERA_FORMAT_YUV_420_SP, Camera_Format::CAMERA_FORMAT_YUV_420_SP},
27 {CameraFormat::CAMERA_FORMAT_JPEG, Camera_Format::CAMERA_FORMAT_JPEG},
28 {CameraFormat::CAMERA_FORMAT_YCBCR_P010, Camera_Format::CAMERA_FORMAT_YCBCR_P010},
29 {CameraFormat::CAMERA_FORMAT_YCRCB_P010, Camera_Format::CAMERA_FORMAT_YCRCB_P010}
30 };
31
32 class InnerPreviewOutputCallback : public PreviewStateCallback {
33 public:
InnerPreviewOutputCallback(Camera_PreviewOutput * previewOutput,PreviewOutput_Callbacks * callback)34 InnerPreviewOutputCallback(Camera_PreviewOutput* previewOutput, PreviewOutput_Callbacks* callback)
35 : previewOutput_(previewOutput), callback_(*callback) {}
36 ~InnerPreviewOutputCallback() = default;
37
OnFrameStarted() const38 void OnFrameStarted() const override
39 {
40 MEDIA_DEBUG_LOG("OnFrameStarted is called!");
41 if (previewOutput_ != nullptr && callback_.onFrameStart != nullptr) {
42 callback_.onFrameStart(previewOutput_);
43 }
44 }
45
OnFrameEnded(const int32_t frameCount) const46 void OnFrameEnded(const int32_t frameCount) const override
47 {
48 MEDIA_DEBUG_LOG("OnFrameEnded is called! frame count: %{public}d", frameCount);
49 if (previewOutput_ != nullptr && callback_.onFrameEnd != nullptr) {
50 callback_.onFrameEnd(previewOutput_, frameCount);
51 }
52 }
53
OnError(const int32_t errorCode) const54 void OnError(const int32_t errorCode) const override
55 {
56 MEDIA_DEBUG_LOG("OnError is called!, errorCode: %{public}d", errorCode);
57 if (previewOutput_ != nullptr && callback_.onError != nullptr) {
58 callback_.onError(previewOutput_, FrameworkToNdkCameraError(errorCode));
59 }
60 }
61
OnSketchStatusDataChanged(const SketchStatusData & statusData) const62 void OnSketchStatusDataChanged(const SketchStatusData& statusData) const override {}
63
64 private:
65 Camera_PreviewOutput* previewOutput_;
66 PreviewOutput_Callbacks callback_;
67 };
68
Camera_PreviewOutput(sptr<PreviewOutput> & innerPreviewOutput)69 Camera_PreviewOutput::Camera_PreviewOutput(sptr<PreviewOutput> &innerPreviewOutput)
70 : innerPreviewOutput_(innerPreviewOutput)
71 {
72 MEDIA_DEBUG_LOG("Camera_PreviewOutput Constructor is called");
73 }
74
~Camera_PreviewOutput()75 Camera_PreviewOutput::~Camera_PreviewOutput()
76 {
77 MEDIA_DEBUG_LOG("~Camera_PreviewOutput is called");
78 if (innerPreviewOutput_) {
79 innerPreviewOutput_ = nullptr;
80 }
81 }
82
RegisterCallback(PreviewOutput_Callbacks * callback)83 Camera_ErrorCode Camera_PreviewOutput::RegisterCallback(PreviewOutput_Callbacks* callback)
84 {
85 shared_ptr<InnerPreviewOutputCallback> innerCallback =
86 make_shared<InnerPreviewOutputCallback>(this, callback);
87 innerPreviewOutput_->SetCallback(innerCallback);
88 return CAMERA_OK;
89 }
90
UnregisterCallback(PreviewOutput_Callbacks * callback)91 Camera_ErrorCode Camera_PreviewOutput::UnregisterCallback(PreviewOutput_Callbacks* callback)
92 {
93 innerPreviewOutput_->SetCallback(nullptr);
94 return CAMERA_OK;
95 }
96
Start()97 Camera_ErrorCode Camera_PreviewOutput::Start()
98 {
99 int32_t ret = innerPreviewOutput_->Start();
100 return FrameworkToNdkCameraError(ret);
101 }
102
Stop()103 Camera_ErrorCode Camera_PreviewOutput::Stop()
104 {
105 int32_t ret = innerPreviewOutput_->Stop();
106 return FrameworkToNdkCameraError(ret);
107 }
108
Release()109 Camera_ErrorCode Camera_PreviewOutput::Release()
110 {
111 int32_t ret = innerPreviewOutput_->Release();
112 return FrameworkToNdkCameraError(ret);
113 }
114
GetInnerPreviewOutput()115 sptr<PreviewOutput> Camera_PreviewOutput::GetInnerPreviewOutput()
116 {
117 return innerPreviewOutput_;
118 }
119
GetActiveProfile(Camera_Profile ** profile)120 Camera_ErrorCode Camera_PreviewOutput::GetActiveProfile(Camera_Profile** profile)
121 {
122 auto previewOutputProfile = innerPreviewOutput_->GetPreviewProfile();
123 CHECK_AND_RETURN_RET_LOG(previewOutputProfile != nullptr, CAMERA_SERVICE_FATAL_ERROR,
124 "Camera_PreviewOutput::GetActiveProfile failed to get preview profile!");
125
126 CameraFormat cameraFormat = previewOutputProfile->GetCameraFormat();
127 auto itr = g_fwToNdkCameraFormat.find(cameraFormat);
128 CHECK_AND_RETURN_RET_LOG(itr != g_fwToNdkCameraFormat.end(), CAMERA_SERVICE_FATAL_ERROR,
129 "Camera_PreviewOutput::GetActiveProfile Unsupported camera format %{public}d", cameraFormat);
130
131 Camera_Profile* newProfile = new Camera_Profile;
132 CHECK_AND_RETURN_RET_LOG(newProfile != nullptr, CAMERA_SERVICE_FATAL_ERROR,
133 "Camera_PreviewOutput::GetActiveProfile failed to allocate memory for camera profile!");
134
135 newProfile->format = itr->second;
136 newProfile->size.width = previewOutputProfile->GetSize().width;
137 newProfile->size.height = previewOutputProfile->GetSize().height;
138
139 *profile = newProfile;
140 return CAMERA_OK;
141 }
142
GetSupportedFrameRates(Camera_FrameRateRange ** frameRateRange,uint32_t * size)143 Camera_ErrorCode Camera_PreviewOutput::GetSupportedFrameRates(Camera_FrameRateRange** frameRateRange, uint32_t* size)
144 {
145 std::vector<std::vector<int32_t>> frameRate = innerPreviewOutput_->GetSupportedFrameRates();
146 if (frameRate.size() == 0) {
147 *size = 0;
148 return CAMERA_OK;
149 }
150
151 Camera_FrameRateRange* newframeRateRange = new Camera_FrameRateRange[frameRate.size()];
152 CHECK_AND_RETURN_RET_LOG(newframeRateRange != nullptr, CAMERA_SERVICE_FATAL_ERROR,
153 "Failed to allocate memory for Camera_FrameRateRange!");
154
155 for (size_t index = 0; index < frameRate.size(); ++index) {
156 if (frameRate[index].size() <= 1) {
157 MEDIA_ERR_LOG("invalid frameRate size!");
158 delete[] newframeRateRange;
159 newframeRateRange = nullptr;
160 return CAMERA_SERVICE_FATAL_ERROR;
161 }
162 newframeRateRange[index].min = static_cast<uint32_t>(frameRate[index][0]);
163 newframeRateRange[index].max = static_cast<uint32_t>(frameRate[index][1]);
164 }
165
166 *frameRateRange = newframeRateRange;
167 *size = frameRate.size();
168 return CAMERA_OK;
169 }
170
DeleteFrameRates(Camera_FrameRateRange * frameRateRange)171 Camera_ErrorCode Camera_PreviewOutput::DeleteFrameRates(Camera_FrameRateRange* frameRateRange)
172 {
173 if (frameRateRange != nullptr) {
174 delete[] frameRateRange;
175 frameRateRange = nullptr;
176 }
177
178 return CAMERA_OK;
179 }
180
SetFrameRate(int32_t minFps,int32_t maxFps)181 Camera_ErrorCode Camera_PreviewOutput::SetFrameRate(int32_t minFps, int32_t maxFps)
182 {
183 int32_t ret = innerPreviewOutput_->SetFrameRate(minFps, maxFps);
184 return FrameworkToNdkCameraError(ret);
185 }
186
GetActiveFrameRate(Camera_FrameRateRange * frameRateRange)187 Camera_ErrorCode Camera_PreviewOutput::GetActiveFrameRate(Camera_FrameRateRange* frameRateRange)
188 {
189 std::vector<int32_t> activeFrameRate = innerPreviewOutput_->GetFrameRateRange();
190 CHECK_AND_RETURN_RET_LOG(activeFrameRate.size() > 1, CAMERA_SERVICE_FATAL_ERROR,
191 "invalid activeFrameRate size!");
192
193 frameRateRange->min = static_cast<uint32_t>(activeFrameRate[0]);
194 frameRateRange->max = static_cast<uint32_t>(activeFrameRate[1]);
195
196 return CAMERA_OK;
197 }
GetPreviewRotation(int32_t imageRotation,Camera_ImageRotation * cameraImageRotation)198 Camera_ErrorCode Camera_PreviewOutput::GetPreviewRotation(int32_t imageRotation,
199 Camera_ImageRotation* cameraImageRotation)
200 {
201 CHECK_AND_RETURN_RET_LOG(cameraImageRotation != nullptr, CAMERA_SERVICE_FATAL_ERROR,
202 "GetCameraImageRotation failed");
203 int32_t cameraOutputRotation = innerPreviewOutput_->GetPreviewRotation(imageRotation);
204 CHECK_AND_RETURN_RET_LOG(cameraOutputRotation != CAMERA_INVALID_ARGUMENT, CAMERA_INVALID_ARGUMENT,
205 "Camera_PreviewOutput::GetPreviewRotation camera invalid argument! ret: %{public}d", cameraOutputRotation);
206 CHECK_AND_RETURN_RET_LOG(cameraOutputRotation != CAMERA_SERVICE_FATAL_ERROR, CAMERA_SERVICE_FATAL_ERROR,
207 "Camera_PreviewOutput::GetPreviewRotation camera service fatal error! ret: %{public}d", cameraOutputRotation);
208 *cameraImageRotation = static_cast<Camera_ImageRotation>(cameraOutputRotation);
209 return CAMERA_OK;
210 }
211
SetPreviewRotation(int32_t imageRotation,bool isDisplayLocked)212 Camera_ErrorCode Camera_PreviewOutput::SetPreviewRotation(int32_t imageRotation, bool isDisplayLocked)
213 {
214 int32_t ret = innerPreviewOutput_->SetPreviewRotation(imageRotation, isDisplayLocked);
215 CHECK_AND_RETURN_RET_LOG(ret != CAMERA_INVALID_ARGUMENT, CAMERA_INVALID_ARGUMENT,
216 "Camera_PreviewOutput::SetPreviewRotation camera invalid argument! ret: %{public}d", ret);
217 CHECK_AND_RETURN_RET_LOG(ret != CAMERA_SERVICE_FATAL_ERROR, CAMERA_SERVICE_FATAL_ERROR,
218 "Camera_PhotoOutput::SetPreviewRotation camera service fatal error! ret: %{public}d", ret);
219 return CAMERA_OK;
220 }