• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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  *     http://www.apache.org/licenses/LICENSE-2.0
7  * Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS,
9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and
11  * limitations under the License.
12  */
13 
14 #include "v4l2_source_node_rk.h"
15 #include "metadata_controller.h"
16 #include <unistd.h>
17 
18 namespace OHOS::Camera {
V4L2SourceNodeRK(const std::string & name,const std::string & type,const std::string & cameraId)19 V4L2SourceNodeRK::V4L2SourceNodeRK(const std::string& name, const std::string& type, const std::string &cameraId)
20     : SourceNode(name, type, cameraId), NodeBase(name, type, cameraId)
21 {
22     CAMERA_LOGI("%s enter, type(%s)\n", name_.c_str(), type_.c_str());
23     RetCode rc = RC_OK;
24     deviceManager_ = IDeviceManager::GetInstance();
25     if (deviceManager_ == nullptr) {
26         CAMERA_LOGE("get device manager failed.");
27         return;
28     }
29     rc = GetDeviceController();
30     if (rc == RC_ERROR) {
31         CAMERA_LOGE("GetDeviceController failed.");
32         return;
33     }
34 }
35 
GetDeviceController()36 RetCode V4L2SourceNodeRK::GetDeviceController()
37 {
38     CameraId cameraId = CAMERA_FIRST;
39     sensorController_ = std::static_pointer_cast<SensorController>
40         (deviceManager_->GetController(cameraId, DM_M_SENSOR, DM_C_SENSOR));
41     if (sensorController_ == nullptr) {
42         CAMERA_LOGE("get device controller failed");
43         return RC_ERROR;
44     }
45     return RC_OK;
46 }
47 
Init(const int32_t streamId)48 RetCode V4L2SourceNodeRK::Init(const int32_t streamId)
49 {
50     return RC_OK;
51 }
52 
Start(const int32_t streamId)53 RetCode V4L2SourceNodeRK::Start(const int32_t streamId)
54 {
55     RetCode rc = RC_OK;
56     deviceManager_ = IDeviceManager::GetInstance();
57     if (deviceManager_ == nullptr) {
58         CAMERA_LOGE("get device manager failed.");
59         return RC_ERROR;
60     }
61     rc = GetDeviceController();
62     if (rc == RC_ERROR) {
63         CAMERA_LOGE("GetDeviceController failed.");
64         return RC_ERROR;
65     }
66     std::vector<std::shared_ptr<IPort>> outPorts = GetOutPorts();
67     for (const auto& it : outPorts) {
68         DeviceFormat format;
69         format.fmtdesc.pixelformat =  V4L2_PIX_FMT_NV12;
70         format.fmtdesc.width = it->format_.w_;
71         format.fmtdesc.height = it->format_.h_;
72         int bufCnt = it->format_.bufferCount_;
73         rc = sensorController_->Start(bufCnt, format);
74         if (rc == RC_ERROR) {
75             CAMERA_LOGE("start failed.");
76             return RC_ERROR;
77         }
78     }
79     rc = SourceNode::Start(streamId);
80     return rc;
81 }
82 
~V4L2SourceNodeRK()83 V4L2SourceNodeRK::~V4L2SourceNodeRK()
84 {
85     CAMERA_LOGV("%{public}s, v4l2 source node dtor.", __FUNCTION__);
86 }
87 
Flush(const int32_t streamId)88 RetCode V4L2SourceNodeRK::Flush(const int32_t streamId)
89 {
90     RetCode rc;
91 
92     if (sensorController_ != nullptr) {
93         rc = sensorController_->Flush(streamId);
94         CHECK_IF_NOT_EQUAL_RETURN_VALUE(rc, RC_OK, RC_ERROR);
95     }
96     rc = SourceNode::Flush(streamId);
97 
98     return rc;
99 }
100 
Stop(const int32_t streamId)101 RetCode V4L2SourceNodeRK::Stop(const int32_t streamId)
102 {
103     RetCode rc;
104 
105     if (sensorController_ != nullptr) {
106         rc = sensorController_->Stop();
107         CHECK_IF_NOT_EQUAL_RETURN_VALUE(rc, RC_OK, RC_ERROR);
108     }
109 
110     return SourceNode::Stop(streamId);
111 }
112 
SetCallback()113 RetCode V4L2SourceNodeRK::SetCallback()
114 {
115     MetadataController &metaDataController = MetadataController::GetInstance();
116     metaDataController.AddNodeCallback([this](const std::shared_ptr<CameraMetadata> &metadata) {
117         OnMetadataChanged(metadata);
118     });
119     return RC_OK;
120 }
121 
GetStreamId(const CaptureMeta & meta)122 int32_t V4L2SourceNodeRK::GetStreamId(const CaptureMeta &meta)
123 {
124     common_metadata_header_t *data = meta->get();
125     if (data == nullptr) {
126         CAMERA_LOGE("data is nullptr");
127         return RC_ERROR;
128     }
129     camera_metadata_item_t entry;
130     int32_t streamId = -1;
131     int rc = FindCameraMetadataItem(data, OHOS_CAMERA_STREAM_ID, &entry);
132     if (rc == 0) {
133         streamId = *entry.data.i32;
134     }
135     return streamId;
136 }
137 
OnMetadataChanged(const std::shared_ptr<CameraMetadata> & metadata)138 void V4L2SourceNodeRK::OnMetadataChanged(const std::shared_ptr<CameraMetadata>& metadata)
139 {
140     if (metadata == nullptr) {
141         CAMERA_LOGE("meta is nullptr");
142         return;
143     }
144     constexpr uint32_t DEVICE_STREAM_ID = 0;
145     if (sensorController_ != nullptr) {
146         if (GetStreamId(metadata) == DEVICE_STREAM_ID) {
147             sensorController_->Configure(metadata);
148         }
149     } else {
150         CAMERA_LOGE("V4L2SourceNodeRK sensorController_ is null");
151     }
152 }
153 
SetBufferCallback()154 void V4L2SourceNodeRK::SetBufferCallback()
155 {
156     sensorController_->SetNodeCallBack([&](std::shared_ptr<FrameSpec> frameSpec) {
157             OnPackBuffer(frameSpec);
158     });
159     return;
160 }
161 
ProvideBuffers(std::shared_ptr<FrameSpec> frameSpec)162 RetCode V4L2SourceNodeRK::ProvideBuffers(std::shared_ptr<FrameSpec> frameSpec)
163 {
164     CAMERA_LOGI("provide buffers enter.");
165     if (sensorController_->SendFrameBuffer(frameSpec) == RC_OK) {
166         CAMERA_LOGI("sendframebuffer success bufferpool id = %llu", frameSpec->bufferPoolId_);
167         return RC_OK;
168     }
169     return RC_ERROR;
170 }
171 REGISTERNODE(V4L2SourceNodeRK, {"v4l2_source_rk"})
172 } // namespace OHOS::Camera
173