• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
18 #include "camera_log.h"
19 #include "camera_output_capability.h"
20 #include "camera_util.h"
21 
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::CameraStandard;
25 const std::unordered_map<CameraFormat, Camera_Format> g_fwToNdkCameraFormat = {
26     {CameraFormat::CAMERA_FORMAT_RGBA_8888, Camera_Format::CAMERA_FORMAT_RGBA_8888},
27     {CameraFormat::CAMERA_FORMAT_YUV_420_SP, Camera_Format::CAMERA_FORMAT_YUV_420_SP},
28     {CameraFormat::CAMERA_FORMAT_JPEG, Camera_Format::CAMERA_FORMAT_JPEG},
29     {CameraFormat::CAMERA_FORMAT_YCBCR_P010, Camera_Format::CAMERA_FORMAT_YCBCR_P010},
30     {CameraFormat::CAMERA_FORMAT_YCRCB_P010, Camera_Format::CAMERA_FORMAT_YCRCB_P010}
31 };
32 namespace OHOS::CameraStandard {
33 class InnerPreviewOutputCallback : public PreviewStateCallback {
34 public:
InnerPreviewOutputCallback(Camera_PreviewOutput * previewOutput,PreviewOutput_Callbacks * callback)35     InnerPreviewOutputCallback(Camera_PreviewOutput* previewOutput, PreviewOutput_Callbacks* callback)
36         : previewOutput_(previewOutput), callback_(*callback)
37     {}
38     ~InnerPreviewOutputCallback() = default;
39 
OnFrameStarted() const40     void OnFrameStarted() const override
41     {
42         MEDIA_DEBUG_LOG("OnFrameStarted is called!");
43         CHECK_EXECUTE(previewOutput_ != nullptr && callback_.onFrameStart != nullptr,
44             callback_.onFrameStart(previewOutput_));
45     }
46 
OnFrameEnded(const int32_t frameCount) const47     void OnFrameEnded(const int32_t frameCount) const override
48     {
49         MEDIA_DEBUG_LOG("OnFrameEnded is called! frame count: %{public}d", frameCount);
50         CHECK_EXECUTE(previewOutput_ != nullptr && callback_.onFrameEnd != nullptr,
51             callback_.onFrameEnd(previewOutput_, frameCount));
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         CHECK_EXECUTE(previewOutput_ != nullptr && callback_.onError != nullptr,
58             callback_.onError(previewOutput_, FrameworkToNdkCameraError(errorCode)));
59     }
60 
OnSketchStatusDataChanged(const SketchStatusData & statusData) const61     void OnSketchStatusDataChanged(const SketchStatusData& statusData) const override {}
62 
63 private:
64     Camera_PreviewOutput* previewOutput_;
65     PreviewOutput_Callbacks callback_;
66 };
67 } // namespace OHOS::CameraStandard
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 = make_shared<InnerPreviewOutputCallback>(this, callback);
86     if (callbackMap_.SetMapValue(callback, innerCallback)) {
87         innerPreviewOutput_->SetCallback(innerCallback);
88     }
89     return CAMERA_OK;
90 }
91 
UnregisterCallback(PreviewOutput_Callbacks * callback)92 Camera_ErrorCode Camera_PreviewOutput::UnregisterCallback(PreviewOutput_Callbacks* callback)
93 {
94     auto innerCallback = callbackMap_.RemoveValue(callback);
95     if (innerCallback != nullptr) {
96         innerPreviewOutput_->RemoveCallback(innerCallback);
97     }
98     return CAMERA_OK;
99 }
100 
Start()101 Camera_ErrorCode Camera_PreviewOutput::Start()
102 {
103     int32_t ret = innerPreviewOutput_->Start();
104     return FrameworkToNdkCameraError(ret);
105 }
106 
Stop()107 Camera_ErrorCode Camera_PreviewOutput::Stop()
108 {
109     int32_t ret = innerPreviewOutput_->Stop();
110     return FrameworkToNdkCameraError(ret);
111 }
112 
Release()113 Camera_ErrorCode Camera_PreviewOutput::Release()
114 {
115     int32_t ret = innerPreviewOutput_->Release();
116     return FrameworkToNdkCameraError(ret);
117 }
118 
GetInnerPreviewOutput()119 sptr<PreviewOutput> Camera_PreviewOutput::GetInnerPreviewOutput()
120 {
121     return innerPreviewOutput_;
122 }
123 
GetActiveProfile(Camera_Profile ** profile)124 Camera_ErrorCode Camera_PreviewOutput::GetActiveProfile(Camera_Profile** profile)
125 {
126     auto previewOutputProfile = innerPreviewOutput_->GetPreviewProfile();
127     CHECK_ERROR_RETURN_RET_LOG(previewOutputProfile == nullptr, CAMERA_SERVICE_FATAL_ERROR,
128         "Camera_PreviewOutput::GetActiveProfile failed to get preview profile!");
129 
130     CameraFormat cameraFormat = previewOutputProfile->GetCameraFormat();
131     auto itr = g_fwToNdkCameraFormat.find(cameraFormat);
132     CHECK_ERROR_RETURN_RET_LOG(itr == g_fwToNdkCameraFormat.end(), CAMERA_SERVICE_FATAL_ERROR,
133         "Camera_PreviewOutput::GetActiveProfile Unsupported camera format %{public}d", cameraFormat);
134 
135     Camera_Profile* newProfile = new Camera_Profile;
136     CHECK_ERROR_RETURN_RET_LOG(newProfile == nullptr, CAMERA_SERVICE_FATAL_ERROR,
137         "Camera_PreviewOutput::GetActiveProfile failed to allocate memory for camera profile!");
138 
139     newProfile->format = itr->second;
140     newProfile->size.width = previewOutputProfile->GetSize().width;
141     newProfile->size.height = previewOutputProfile->GetSize().height;
142 
143     *profile = newProfile;
144     return CAMERA_OK;
145 }
146 
GetSupportedFrameRates(Camera_FrameRateRange ** frameRateRange,uint32_t * size)147 Camera_ErrorCode Camera_PreviewOutput::GetSupportedFrameRates(Camera_FrameRateRange** frameRateRange, uint32_t* size)
148 {
149     std::vector<std::vector<int32_t>> frameRate = innerPreviewOutput_->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_ERROR_RETURN_RET_LOG(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_PreviewOutput::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_PreviewOutput::SetFrameRate(int32_t minFps, int32_t maxFps)
186 {
187     int32_t ret = innerPreviewOutput_->SetFrameRate(minFps, maxFps);
188     return FrameworkToNdkCameraError(ret);
189 }
190 
GetActiveFrameRate(Camera_FrameRateRange * frameRateRange)191 Camera_ErrorCode Camera_PreviewOutput::GetActiveFrameRate(Camera_FrameRateRange* frameRateRange)
192 {
193     std::vector<int32_t> activeFrameRate = innerPreviewOutput_->GetFrameRateRange();
194     CHECK_ERROR_RETURN_RET_LOG(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 
GetPreviewRotation(int32_t imageRotation,Camera_ImageRotation * cameraImageRotation)203 Camera_ErrorCode Camera_PreviewOutput::GetPreviewRotation(int32_t imageRotation,
204     Camera_ImageRotation* cameraImageRotation)
205 {
206     CHECK_ERROR_RETURN_RET_LOG(cameraImageRotation == nullptr, CAMERA_SERVICE_FATAL_ERROR,
207         "GetCameraImageRotation failed");
208     int32_t cameraOutputRotation = innerPreviewOutput_->GetPreviewRotation(imageRotation);
209     CHECK_ERROR_RETURN_RET_LOG(cameraOutputRotation == CAMERA_SERVICE_FATAL_ERROR, CAMERA_SERVICE_FATAL_ERROR,
210         "Camera_PreviewOutput::GetPreviewRotation camera service fatal error! ret: %{public}d", cameraOutputRotation);
211     CHECK_ERROR_RETURN_RET_LOG(cameraOutputRotation == CAMERA_INVALID_ARGUMENT, CAMERA_INVALID_ARGUMENT,
212         "Camera_PreviewOutput::GetPreviewRotation camera invalid argument! ret: %{public}d", cameraOutputRotation);
213     *cameraImageRotation = static_cast<Camera_ImageRotation>(cameraOutputRotation);
214     return CAMERA_OK;
215 }
216 
SetPreviewRotation(int32_t imageRotation,bool isDisplayLocked)217 Camera_ErrorCode Camera_PreviewOutput::SetPreviewRotation(int32_t imageRotation, bool isDisplayLocked)
218 {
219     int32_t ret = innerPreviewOutput_->SetPreviewRotation(imageRotation, isDisplayLocked);
220     CHECK_ERROR_RETURN_RET_LOG(ret == CAMERA_SERVICE_FATAL_ERROR, CAMERA_SERVICE_FATAL_ERROR,
221         "Camera_PhotoOutput::SetPreviewRotation camera service fatal error! ret: %{public}d", ret);
222     CHECK_ERROR_RETURN_RET_LOG(ret == CAMERA_INVALID_ARGUMENT, CAMERA_INVALID_ARGUMENT,
223         "Camera_PreviewOutput::SetPreviewRotation camera invalid argument! ret: %{public}d", ret);
224     return CAMERA_OK;
225 }