Apollo 10.0
自动驾驶开放平台
apollo::drivers::video::SocketInput类 参考

Live Velodyne input from socket. 更多...

#include <socket_input.h>

apollo::drivers::video::SocketInput 的协作图:

Public 成员函数

 SocketInput ()
 constructor
 
virtual ~SocketInput ()
 destructor
 
void Init (uint32_t port)
 
int GetFramePacket (std::shared_ptr< CompressedImage > h265)
 Get one camera packet.
 

详细描述

Live Velodyne input from socket.

在文件 socket_input.h40 行定义.

构造及析构函数说明

◆ SocketInput()

apollo::drivers::video::SocketInput::SocketInput ( )

constructor

参数
private_nhprivate node handle for driver
udpportUDP port number to connect

在文件 socket_input.cc44 行定义.

44 : sockfd_(-1), port_(0) {
45 pkg_num_ = 0;
46 bytes_num_ = 0;
47 frame_id_ = 0;
48}

◆ ~SocketInput()

apollo::drivers::video::SocketInput::~SocketInput ( )
virtual

destructor

在文件 socket_input.cc51 行定义.

51 {
52 if (buf_) {
53 delete[] buf_;
54 }
55 if (pdu_) {
56 delete[] pdu_;
57 }
58 (void)close(sockfd_);
59}

成员函数说明

◆ GetFramePacket()

int apollo::drivers::video::SocketInput::GetFramePacket ( std::shared_ptr< CompressedImage h265)

Get one camera packet.

在文件 socket_input.cc112 行定义.

112 {
113 uint8_t *frame_data = &buf_[0];
114 uint8_t *pdu_data = &pdu_[0];
115 int total = 0;
116 int pkg_len = 0;
117 size_t frame_len = 0;
118 uint16_t pre_seq = 0;
119
120 do {
121 if (!InputAvailable(POLL_TIMEOUT)) {
122 return SOCKET_TIMEOUT;
123 }
124 // Receive packets that should now be available from the socket using
125 // a blocking read.
126 ssize_t pdu_len = recvfrom(sockfd_, pdu_data, H265_PDU_SIZE, 0, NULL, NULL);
127 if (pdu_len < 0) {
128 if (errno != EWOULDBLOCK) {
129 AERROR << "Failed to receive package from port: " << port_;
130 return RECEIVE_FAIL;
131 }
132 }
133
134 AINFO << "Received pdu length: " << pdu_len << " from port: " << port_;
135 HwPduPacket *pdu_pkg = reinterpret_cast<HwPduPacket *>(&pdu_[0]);
136 uint16_t local_seq = ntohs(pdu_pkg->rtp_header.seq);
137 AINFO << "Package seq number: " << local_seq;
138 if (local_seq - pre_seq != 1 && pre_seq > 1 && local_seq > 0) {
139 AERROR << "Error! port: " << port_
140 << ", package sequence is wrong. curent/pre " << local_seq << "/"
141 << pre_seq;
142 }
143 pre_seq = local_seq;
144
145 if (ntohl(pdu_pkg->header.magic0) == HW_CAMERA_MAGIC0 &&
146 ntohl(pdu_pkg->header.magic1) == HW_CAMERA_MAGIC1) {
147 // Receive camera frame head
148 if (total) {
149 AERROR << "Error! lost package for last frame, left bytes: " << total;
150 }
151 AINFO << "Received new frame from port: " << port_;
152
153 uint32_t frame_id = ntohl(pdu_pkg->header.frame_id);
154 if (frame_id - frame_id_ != 1 && frame_id_ > 1 && frame_id > 1) {
155 AERROR << "Error! port: " << port_
156 << ", lose Frame. pre_frame_id/frame_id " << frame_id_ << "/"
157 << frame_id;
158 }
159 frame_id_ = frame_id;
160
161 cyber::Time image_time(ntohl(pdu_pkg->header.ts_sec),
162 1000 * ntohl(pdu_pkg->header.ts_usec));
163 // AINFO << "image_time second: " << ntohl(pdu_pkg->header.ts_sec) <<
164 // " usec: " << ntohl(pdu_pkg->header.ts_usec);
165 uint64_t camera_timestamp = image_time.ToNanosecond();
166 h265Pb->mutable_header()->set_camera_timestamp(camera_timestamp);
167 h265Pb->set_measurement_time(image_time.ToSecond());
168 h265Pb->set_format("h265");
169 h265Pb->set_frame_type(static_cast<int>(pdu_pkg->header.frame_type));
170 AINFO << "Port: " << port_
171 << ", received frame size: " << ntohl(pdu_pkg->header.frame_size)
172 << ", frame type: " << static_cast<int>(pdu_pkg->header.frame_type)
173 << ", PhyNo: " << static_cast<int>(pdu_pkg->header.PhyNo)
174 << ", frame id: " << frame_id;
175
176 frame_len = ntohl(pdu_pkg->header.frame_size);
177 total = static_cast<int>(frame_len);
178 frame_data = &buf_[0];
179 continue;
180 }
181 // Receive camera frame data
182 if (total > 0) {
183 pkg_len = static_cast<int>(pdu_len - sizeof(RtpHeader));
184 memcpy(frame_data, pdu_data + sizeof(RtpHeader), pkg_len);
185 total -= pkg_len;
186 frame_data += pkg_len;
187 // AINFO << "receive pkg data " << pkg_len << "/" << total << "/"
188 // << frame_len;
189 }
190 if (total <= 0) {
191 total = 0;
192 // AINFO << "receive frame data " << pkg_len << "/" << total << "/"
193 // << frame_len;
194 if (frame_len > 0) {
195 h265Pb->set_data(buf_, frame_len);
196 break;
197 }
198 AERROR << "Error! frame info is wrong. frame length: " << frame_len;
199 }
200 } while (true);
201
202 return 0;
203}
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::string frame_id
Point cloud frame id
struct apollo::drivers::video::RtpHeader RtpHeader
struct apollo::drivers::video::HwPduPacket HwPduPacket
#define HW_CAMERA_MAGIC1
Definition input.h:42
#define HW_CAMERA_MAGIC0
Definition input.h:41

◆ Init()

void apollo::drivers::video::SocketInput::Init ( uint32_t  port)

在文件 socket_input.cc61 行定义.

61 {
62 if (sockfd_ != -1) {
63 (void)close(sockfd_);
64 }
65
66 sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
67 if (sockfd_ < 0) {
68 AERROR << "Failed to create socket fd.";
69 return;
70 }
71
72 // Connect to camera UDP port
73 AINFO << "Opening UDP socket on port: " << uint16_t(port);
74 port_ = port;
75 sockaddr_in my_addr;
76 memset(&my_addr, '\0', sizeof(my_addr));
77 my_addr.sin_family = AF_INET;
78 my_addr.sin_port = htons(uint16_t(port));
79 my_addr.sin_addr.s_addr = INADDR_ANY;
80 if (bind(sockfd_, reinterpret_cast<sockaddr *>(&my_addr),
81 sizeof(sockaddr_in)) < 0) {
82 AERROR << "Failed to bind socket on local address.";
83 }
84
85 if (fcntl(sockfd_, F_SETFL, O_NONBLOCK | FASYNC) < 0) {
86 AERROR << "Failed to enable non-blocking I/O.";
87 }
88
89 const int rbuf = 4 * 1024 * 1024;
90 if (setsockopt(sockfd_, SOL_SOCKET, SO_RCVBUF, &rbuf, sizeof(int)) < 0) {
91 AERROR << "Failed to enable socket receive buffer.";
92 }
93
94 int enable = 1;
95 if (setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
96 AERROR << "Failed to enable socket reuseable address.";
97 }
98
99 buf_ = new uint8_t[H265_FRAME_PACKAGE_SIZE];
100 if (!buf_) {
101 AERROR << "Failed to allocate H265 frame package buffer.";
102 }
103 pdu_ = new uint8_t[H265_PDU_SIZE];
104 if (!pdu_) {
105 AERROR << "Failed to allocate H265 PDU buffer.";
106 }
107 // _metric_controller->Init();
108 AINFO << "Camera socket fd: " << sockfd_ << ", port: " << port_;
109}

该类的文档由以下文件生成: