• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "sensor_controller.h"
20 #include "securec.h"
21 
22 namespace OHOS::Camera {
SensorController()23 SensorController::SensorController() {}
24 
SensorController(std::string hardwareName)25 SensorController::SensorController(std::string hardwareName) : IController(hardwareName), buffCont_(0) {}
26 
~SensorController()27 SensorController::~SensorController() {}
28 
Init()29 RetCode SensorController::Init()
30 {
31     sensorVideo_ = std::make_shared<HosV4L2Dev>();
32     if (sensorVideo_ == nullptr) {
33         CAMERA_LOGE("%s Create HosV4L2Dev fail", __FUNCTION__);
34         return RC_ERROR;
35     }
36     return RC_OK;
37 }
38 
PowerUp()39 RetCode SensorController::PowerUp()
40 {
41     RetCode rc = RC_OK;
42     if (GetPowerOnState() == false) {
43         SetPowerOnState(true);
44         CAMERA_LOGI("%s Sensor Powerup", __FUNCTION__);
45         return rc;
46     }
47     return rc;
48 }
49 
PowerDown()50 RetCode SensorController::PowerDown()
51 {
52     RetCode rc = RC_OK;
53     if (GetPowerOnState() == true) {
54         SetPowerOnState(false);
55         sensorVideo_->StopStream(GetName());
56         sensorVideo_->ReleaseBuffers(GetName());
57         sensorVideo_->stop(GetName());
58         CAMERA_LOGI("%s Sensor PowerDown", __FUNCTION__);
59         return rc;
60     }
61     return rc;
62 }
63 
Configure(std::shared_ptr<Camera::CameraMetadata> meta)64 RetCode SensorController::Configure(std::shared_ptr<Camera::CameraMetadata> meta)
65 {
66     return SendSensorMetaData(meta);
67 };
68 
Start(int buffCont,DeviceFormat & format)69 RetCode SensorController::Start(int buffCont, DeviceFormat& format)
70 {
71     CAMERA_LOGI("%s Start", __FUNCTION__);
72     std::lock_guard<std::mutex> l(startSensorLock_);
73     RetCode rc = RC_OK;
74     if (startSensorState_ == false) {
75         buffCont_ = buffCont;
76         sensorVideo_->start(GetName());
77         sensorVideo_->ConfigSys(GetName(), CMD_V4L2_SET_FORMAT, format);
78         sensorVideo_->ConfigSys(GetName(), CMD_V4L2_SET_FPS, format);
79         sensorVideo_->ReqBuffers(GetName(), buffCont_, V4L2_MEMORY_MMAP);
80         startSensorState_ = true;
81     }
82     return rc;
83 };
84 
Stop()85 RetCode SensorController::Stop()
86 {
87     CAMERA_LOGI("%s Stop", __FUNCTION__);
88     std::lock_guard<std::mutex> l(startSensorLock_);
89     RetCode rc = RC_OK;
90     if (startSensorState_ == true) {
91         startSensorState_ = false;
92     }
93     return rc;
94 };
95 
SendFrameBuffer(std::shared_ptr<FrameSpec> buffer)96 RetCode SensorController::SendFrameBuffer(std::shared_ptr<FrameSpec> buffer)
97 {
98     if (buffCont_ >= 1) {
99         CAMERA_LOGI("%s buffCont_ %d", __FUNCTION__, buffCont_);
100         sensorVideo_->CreatBuffer(GetName(), buffer);
101         if (buffCont_ == 1) {
102             sensorVideo_->StartStream(GetName());
103         }
104         buffCont_--;
105     } else {
106         sensorVideo_->QueueBuffer(GetName(), buffer);
107     }
108     return RC_OK;
109 }
110 
SetNodeCallBack(const NodeBufferCb cb)111 void SensorController::SetNodeCallBack(const NodeBufferCb cb)
112 {
113     CAMERA_LOGI("SensorController SetNodeCallBack entry");
114     nodeBufferCb_ = cb;
115     sensorVideo_->SetCallback([&](std::shared_ptr<FrameSpec> buffer) {
116         BufferCallback(buffer);
117     });
118 }
119 
SetMetaDataCallBack(const MetaDataCb cb)120 void SensorController::SetMetaDataCallBack(const MetaDataCb cb)
121 {
122     metaDataCb_ = cb;
123 }
124 
BufferCallback(std::shared_ptr<FrameSpec> buffer)125 void SensorController::BufferCallback(std::shared_ptr<FrameSpec> buffer)
126 {
127     if (nodeBufferCb_ == nullptr) {
128         CAMERA_LOGE("nodeBufferCb_ is nullptr");
129         return;
130     }
131     nodeBufferCb_(buffer);
132 
133     const int ENTRY_CAPACITY = 30; // 30:entry capacity
134     const int DATA_CAPACITY = 2000; // 2000:data capacity
135     std::shared_ptr<Camera::CameraMetadata> meta =
136         std::make_shared<Camera::CameraMetadata>(ENTRY_CAPACITY, DATA_CAPACITY);
137     RetCode rc = GetAbilityMetaData(meta);
138     std::lock_guard<std::mutex> l(metaDataFlaglock_);
139     if (rc == RC_OK && metaDataFlag_ == true) {
140         if (metaDataCb_ == nullptr) {
141             CAMERA_LOGE("metaDataCb_ is nullptr");
142             return;
143         }
144         metaDataCb_(meta);
145         metaDataFlag_ = false;
146     } else {
147         if (rc == RC_ERROR) {
148             CAMERA_LOGE("%s GetAbilityMetaData error", __FUNCTION__);
149         } else {
150             CAMERA_LOGI("%s no send", __FUNCTION__);
151         }
152     }
153 }
154 
GetAbilityMetaData(std::shared_ptr<Camera::CameraMetadata> meta)155 RetCode SensorController::GetAbilityMetaData(std::shared_ptr<Camera::CameraMetadata> meta)
156 {
157     return GetSensorMetaData(meta);
158 }
159 
SetAbilityMetaDataTag(std::vector<int32_t> abilityMetaDataTag)160 void SensorController::SetAbilityMetaDataTag(std::vector<int32_t> abilityMetaDataTag)
161 {
162     std::lock_guard<std::mutex> l(metaDataSetlock_);
163     std::vector<int32_t>().swap(abilityMetaData_);
164     for (auto it = abilityMetaDataTag.cbegin(); it != abilityMetaDataTag.cend(); it++) {
165         switch (*it) {
166             case OHOS_SENSOR_EXPOSURE_TIME: {
167                 abilityMetaData_.push_back(*it);
168                 break;
169             }
170             case OHOS_SENSOR_COLOR_CORRECTION_GAINS: {
171                 abilityMetaData_.push_back((*it));
172                 break;
173             }
174             default:
175                 break;
176         }
177     }
178 }
179 
GetSensorMetaData(std::shared_ptr<Camera::CameraMetadata> meta)180 RetCode SensorController::GetSensorMetaData(std::shared_ptr<Camera::CameraMetadata> meta)
181 {
182     RetCode rc = GetAEMetaData(meta);
183     if (rc == RC_ERROR) {
184         CAMERA_LOGE("%s GetAEMetaData fail", __FUNCTION__);
185         return rc;
186     }
187     rc = GetAWBMetaData(meta);
188     if (rc == RC_ERROR) {
189         CAMERA_LOGE("%s GetAWBMetaData fail", __FUNCTION__);
190     }
191     return rc;
192 }
193 
GetAEMetaData(std::shared_ptr<Camera::CameraMetadata> meta)194 RetCode SensorController::GetAEMetaData(std::shared_ptr<Camera::CameraMetadata> meta)
195 {
196     static int64_t oldExpoTime = 0;
197     int64_t expoTime = 0;
198     RetCode rc = RC_ERROR;
199     std::lock_guard<std::mutex> l(metaDataSetlock_);
200     for (auto iter = abilityMetaData_.cbegin(); iter != abilityMetaData_.cend(); iter++) {
201         switch (*iter) {
202             case OHOS_SENSOR_EXPOSURE_TIME: {
203                 rc = sensorVideo_->QuerySetting(GetName(), CMD_AE_EXPO, (int*)&expoTime);
204                 CAMERA_LOGD("%s Get CMD_AE_EXPOTIME [%d]", __FUNCTION__, expoTime);
205                 if (rc == RC_ERROR) {
206                     CAMERA_LOGE("%s CMD_AE_EXPO QuerySetting fail", __FUNCTION__);
207                     return rc;
208                 }
209                 if (oldExpoTime != expoTime) {
210                     std::lock_guard<std::mutex> l(metaDataFlaglock_);
211                     metaDataFlag_ = true;
212                     oldExpoTime = expoTime;
213                 }
214                 meta->addEntry(OHOS_SENSOR_EXPOSURE_TIME, &expoTime, 1);
215                 break;
216             }
217             default:
218                 break;
219         }
220     }
221     return rc;
222 }
223 
GetAWBMetaData(std::shared_ptr<Camera::CameraMetadata> meta)224 RetCode SensorController::GetAWBMetaData(std::shared_ptr<Camera::CameraMetadata> meta)
225 {
226     static float oldColorGains[4] = {0};
227     float colorGains[4] = {0};
228     int value = 0;
229     RetCode rc = RC_ERROR;
230     std::lock_guard<std::mutex> l(metaDataSetlock_);
231     for (auto iter = abilityMetaData_.cbegin(); iter != abilityMetaData_.cend(); iter++) {
232         switch (*iter) {
233             case OHOS_SENSOR_COLOR_CORRECTION_GAINS: {
234                 rc = sensorVideo_->QuerySetting(GetName(), CMD_AWB_COLORGAINS, &value);
235                 if (rc == RC_ERROR) {
236                     CAMERA_LOGE("%s CMD_AWB_COLORGAINS QuerySetting fail", __FUNCTION__);
237                     return rc;
238                 }
239                 colorGains[0] = (float)value;
240                 int gainsSize = 4;
241                 if (!CheckNumequal(oldColorGains, colorGains, gainsSize)) {
242                     std::lock_guard<std::mutex> l(metaDataFlaglock_);
243                     metaDataFlag_ = true;
244                     (void)memcpy_s(oldColorGains, sizeof(oldColorGains) / sizeof(float), colorGains,
245                         gainsSize * sizeof(float));
246                 }
247                 meta->addEntry(OHOS_SENSOR_COLOR_CORRECTION_GAINS, &colorGains, 4); // 4:data size
248                 break;
249             }
250             default:
251                 break;
252         }
253     }
254     return rc;
255 }
256 
SendSensorMetaData(std::shared_ptr<Camera::CameraMetadata> meta)257 RetCode SensorController::SendSensorMetaData(std::shared_ptr<Camera::CameraMetadata> meta)
258 {
259     common_metadata_header_t *data = meta->get();
260     if (data == nullptr) {
261         CAMERA_LOGE("%s data is nullptr", __FUNCTION__);
262         return RC_ERROR;
263     }
264     RetCode rc = SendAEMetaData(data);
265     if (rc == RC_ERROR) {
266         CAMERA_LOGE("%s SendAEMetaData fail", __FUNCTION__);
267         return rc;
268     }
269     rc = SendAWBMetaData(data);
270     if (rc == RC_ERROR) {
271         CAMERA_LOGE("%s SendAWBMetaData fail", __FUNCTION__);
272     }
273     return rc;
274 }
275 
SendAEMetaData(common_metadata_header_t * data)276 RetCode SensorController::SendAEMetaData(common_metadata_header_t *data)
277 {
278     int32_t expo = 0;
279     RetCode rc = RC_OK;
280     camera_metadata_item_t entry;
281     int ret = Camera::FindCameraMetadataItem(data, OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &entry);
282     if (ret == 0) {
283         expo = *(entry.data.i32);
284         if (expo != 0) {
285             int32_t aemode = 1;
286             rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPO, (int*)&aemode);
287             rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPOTIME, (int*)&expo);
288             CAMERA_LOGD("%s Set CMD_AE_EXPO EXPOTIME[%d] EXPO[%d]", __FUNCTION__, expo, aemode);
289         } else {
290             int32_t aemode = 0;
291             rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPO, (int*)&aemode);
292             CAMERA_LOGD("%s Set CMD_AE_EXPOTIME [%d]", __FUNCTION__, aemode);
293         }
294         if (rc == RC_ERROR) {
295             CAMERA_LOGE("%s Send CMD_AE_EXPOTIME fail", __FUNCTION__);
296             return rc;
297         }
298     }
299     return rc;
300 }
301 
SendAWBMetaData(common_metadata_header_t * data)302 RetCode SensorController::SendAWBMetaData(common_metadata_header_t *data)
303 {
304     uint8_t awbMode = 0;
305     RetCode rc = RC_OK;
306     camera_metadata_item_t entry;
307     int ret = Camera::FindCameraMetadataItem(data, OHOS_CONTROL_AWB_MODE, &entry);
308     if (ret == 0) {
309         awbMode = *(entry.data.u8);
310         rc = sensorVideo_->UpdateSetting(GetName(), CMD_AWB_MODE, (int*)&awbMode);
311         CAMERA_LOGD("%s Set CMD_AWB_MODE [%d]", __FUNCTION__, awbMode);
312         if (rc == RC_ERROR) {
313             CAMERA_LOGE("%s Send CMD_AWB_MODE fail", __FUNCTION__);
314             return rc;
315         }
316     }
317     return rc;
318 }
319 
Flush(int32_t streamId)320 RetCode SensorController::Flush(int32_t streamId)
321 {
322     return sensorVideo_->Flush(GetName());
323 }
324 
SetMemoryType(uint8_t & memType)325 void SensorController::SetMemoryType(uint8_t &memType)
326 {
327     sensorVideo_->SetMemoryType(memType);
328     return;
329 }
330 } // namespace OHOS::Camera
331