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