Skip to content

Commit 432e3ce

Browse files
authored
More fixes and improvements (#57)
1 parent 336b295 commit 432e3ce

File tree

8 files changed

+102
-12
lines changed

8 files changed

+102
-12
lines changed

app/mavlink/src/main/cpp/mavlink.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ void *listen(int mavlink_port) {
9696
memset(buffer, 0x00, sizeof(buffer));
9797
int ret = recv(fd, buffer, sizeof(buffer), 0);
9898
if (ret < 0) {
99-
continue;
99+
// Check for timeout vs real error
100+
if (errno == EAGAIN || errno == EWOULDBLOCK) {
101+
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Mavlink recv timeout");
102+
continue;
103+
} else {
104+
__android_log_print(ANDROID_LOG_ERROR, TAG, "Error receiving mavlink: %s", strerror(errno));
105+
return 0;
106+
}
100107
} else if (ret == 0) {
101108
// peer has done an orderly shutdown
102109
__android_log_print(ANDROID_LOG_ERROR, TAG, "Shutting down mavlink: ret=0");

app/videonative/src/main/cpp/BufferedPacketQueue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
// Define logging tag and maximum buffer size
1717
#define BUFFERED_QUEUE_LOG_TAG "BufferedPacketQueue"
1818
// Considering the packet rate about 100 packets per second, 10 packets should be enough
19-
constexpr size_t MAX_BUFFER_SIZE = 5;
19+
constexpr size_t MAX_BUFFER_SIZE = 15;
2020
// Number of monotonically increasing packets
21-
constexpr size_t MONOTONIC_THRESHOLD = 3;
21+
constexpr size_t MONOTONIC_THRESHOLD = 5;
2222

2323
// Type definition for sequence numbers
2424
using SeqType = uint16_t;

app/videonative/src/main/cpp/parser/ParseRTP.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ void RTPDecoder::parseRTPH264toNALU(const uint8_t* rtp_data, const size_t data_l
123123
}
124124
// MLOGD<<"Got h264 rtp data";
125125
const RTP::RTPPacketH264 rtpPacket(rtp_data, data_length);
126+
127+
if (rtpPacket.rtpPayloadSize == 0)
128+
{
129+
MLOGD << "RTP packet is empty";
130+
return;
131+
}
132+
126133
// MLOGD<<"RTP Header: "<<rtp_header->asString();
127134
if (!validateRTPPacket(rtpPacket.header))
128135
{

app/videonative/src/main/cpp/parser/RTP.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class RTPPacket
205205
// r.n we don't support padding
206206
// assert(header.padding == 0);
207207

208-
if(header.padding >0 )
208+
if (header.padding > 0)
209209
{
210210
rtpPayloadSize = 0;
211211
}

app/wfbngrtl8812/src/main/cpp/TxFrame.cpp

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,69 @@ uint32_t TxFrame::extractRxqOverflow(struct msghdr *msg) {
492492
return 0;
493493
}
494494

495+
int TxFrame::open_udp_socket_for_rx(int port, int buf_size) {
496+
int fd = ::socket(AF_INET, SOCK_DGRAM, 0);
497+
if (fd < 0) {
498+
throw std::runtime_error(string_format("Unable to open socket: %s", std::strerror(errno)));
499+
}
500+
501+
int optval = 1;
502+
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
503+
504+
// Set receive timeout to 500ms
505+
struct timeval tv;
506+
tv.tv_sec = 0;
507+
tv.tv_usec = 500000; // 500ms
508+
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
509+
::close(fd);
510+
throw std::runtime_error(string_format("Unable to set socket timeout: %s", std::strerror(errno)));
511+
}
512+
513+
if (buf_size) {
514+
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size)) < 0) {
515+
::close(fd);
516+
throw std::runtime_error(string_format("Unable to set requested buffer size: %s", std::strerror(errno)));
517+
}
518+
int actual_buf_size = 0;
519+
socklen_t optlen = sizeof(actual_buf_size);
520+
getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &actual_buf_size, &optlen);
521+
if (actual_buf_size < buf_size * 2) {
522+
// Linux doubles the value we set
523+
fprintf(stderr, "Warning: requested rx buffer size %d but got %d\n", buf_size, actual_buf_size / 2);
524+
}
525+
}
526+
527+
struct sockaddr_in saddr;
528+
std::memset(&saddr, 0, sizeof(saddr));
529+
saddr.sin_family = AF_INET;
530+
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
531+
saddr.sin_port = htons(static_cast<uint16_t>(port));
532+
533+
if (::bind(fd, reinterpret_cast<struct sockaddr *>(&saddr), sizeof(saddr)) < 0) {
534+
::close(fd);
535+
throw std::runtime_error(string_format("Unable to bind to port %d: %s", port, std::strerror(errno)));
536+
}
537+
538+
return fd;
539+
}
540+
495541
void TxFrame::dataSource(
496542
std::shared_ptr<Transmitter> &transmitter, std::vector<int> &rxFds, int fecTimeout, bool mirror, int logInterval) {
497543
int nfds = static_cast<int>(rxFds.size());
498544
if (nfds <= 0) {
499545
throw std::runtime_error("dataSource: no valid rx sockets");
500546
}
501547

548+
// Set timeout on all sockets
549+
for (int fd : rxFds) {
550+
struct timeval tv;
551+
tv.tv_sec = 0;
552+
tv.tv_usec = 500000; // 500ms
553+
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
554+
throw std::runtime_error(string_format("Unable to set socket timeout: %s", std::strerror(errno)));
555+
}
556+
}
557+
502558
std::vector<pollfd> fds(nfds);
503559
for (int i = 0; i < nfds; ++i) {
504560
fds[i].fd = rxFds[i];
@@ -650,7 +706,8 @@ void TxFrame::dataSource(
650706

651707
ssize_t rsize = ::recvmsg(pfd.fd, &msg, 0);
652708
if (rsize < 0) {
653-
if (errno != EWOULDBLOCK && errno != EAGAIN) {
709+
if (errno != EWOULDBLOCK && errno != EAGAIN && errno != ETIMEDOUT) {
710+
continue;
654711
throw std::runtime_error(string_format("Error receiving packet: %s", std::strerror(errno)));
655712
}
656713
break;
@@ -809,7 +866,7 @@ void TxFrame::run(Rtl8812aDevice *rtlDevice, TxArgs *arg) {
809866
// Attempt to create a UDP listening socket
810867
std::vector<int> rxFds;
811868
int bindPort = arg->udp_port;
812-
int udpFd = open_udp_socket_for_rx(bindPort, arg->rcv_buf);
869+
int udpFd = TxFrame::open_udp_socket_for_rx(bindPort, arg->rcv_buf);
813870

814871
if (arg->udp_port == 0) {
815872
// ephemeral port

app/wfbngrtl8812/src/main/cpp/TxFrame.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Transmitter {
102102

103103
/**
104104
* @brief Choose which output interface (antenna / socket / etc.) to use.
105-
* @param idx The interface index, or -1 for mirror mode.
105+
* @param idx The interface index, or -1 for "mirror" mode.
106106
*/
107107
virtual void selectOutput(int idx) = 0;
108108

@@ -206,7 +206,7 @@ using TxAntennaStat = std::unordered_map<uint64_t, TxAntennaItem>;
206206
* @class RawSocketTransmitter
207207
* @brief Transmitter that sends packets over raw AF_PACKET sockets.
208208
*
209-
* This transmitter can operate in single-output or mirror mode. In mirror mode,
209+
* This transmitter can operate in single-output or "mirror" mode. In mirror mode,
210210
* it sends each packet through all raw sockets. In single-output mode, only one is selected.
211211
*/
212212
class RawSocketTransmitter : public Transmitter {
@@ -391,4 +391,12 @@ class TxFrame {
391391

392392
private:
393393
bool shouldStop_ = false;
394+
395+
/**
396+
* @brief Create a UDP socket for receiving data
397+
* @param port UDP port to bind to
398+
* @param buf_size Receive buffer size
399+
* @return Socket file descriptor
400+
*/
401+
static int open_udp_socket_for_rx(int port, int buf_size);
394402
};

app/wfbngrtl8812/src/main/cpp/WfbngLink.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void WfbngLink::initAgg() {
5858
std::string client_addr = "127.0.0.1";
5959
uint64_t epoch = 0;
6060

61-
int video_client_port = 5600;
6261
uint8_t video_radio_port = 0;
6362
uint32_t video_channel_id_f = (link_id << 8) + video_radio_port;
6463
video_channel_id_be = htobe32(video_channel_id_f);
@@ -79,13 +78,14 @@ void WfbngLink::initAgg() {
7978
uint32_t udp_channel_id_f = (link_id << 8) + udp_radio_port;
8079
udp_channel_id_be = htobe32(udp_channel_id_f);
8180

82-
udp_aggregator = std::make_unique<AggregatorUDPv4>(client_addr, udp_client_port, keyPath, epoch, udp_channel_id_f, 0);
81+
udp_aggregator =
82+
std::make_unique<AggregatorUDPv4>(client_addr, udp_client_port, keyPath, epoch, udp_channel_id_f, 0);
8383
}
8484

8585
int WfbngLink::run(JNIEnv *env, jobject context, jint wifiChannel, jint bw, jint fd) {
8686
int r;
8787
libusb_context *ctx = NULL;
88-
std::shared_ptr<TxFrame> txFrame = std::make_shared<TxFrame>();
88+
txFrame = std::make_shared<TxFrame>();
8989

9090
r = libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY);
9191
r = libusb_init(&ctx);
@@ -214,7 +214,8 @@ int WfbngLink::run(JNIEnv *env, jobject context, jint wifiChannel, jint bw, jint
214214
Rtl8812aDevice *current_device = rtl_devices.at(fd).get();
215215
if (!usb_tx_thread) {
216216
init_thread(usb_tx_thread, [&]() {
217-
return std::make_unique<std::thread>([txFrame, current_device, args] {
217+
return std::make_unique<std::thread>([this, current_device, args] {
218+
setName(pthread_self(), "usb_transfer");
218219
txFrame->run(current_device, args.get());
219220
__android_log_print(ANDROID_LOG_DEBUG, TAG, "usb_transfer thread should terminate");
220221
});

app/wfbngrtl8812/src/main/cpp/WfbngLink.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef FPV_VR_WFBNG_LINK_H
22
#define FPV_VR_WFBNG_LINK_H
33

4+
#include "TxFrame.h"
45
extern "C" {
56
#include "wfb-ng/src/fec.h"
67
}
@@ -67,9 +68,18 @@ class WfbngLink {
6768
}
6869

6970
private:
71+
void stopDevice() {
72+
if (rtl_devices.find(current_fd) == rtl_devices.end()) return;
73+
auto dev = rtl_devices.at(current_fd).get();
74+
if (dev) {
75+
dev->should_stop = true;
76+
}
77+
}
78+
7079
const char *keyPath = "/data/user/0/com.openipc.pixelpilot/files/gs.key";
7180
std::recursive_mutex thread_mutex;
7281
std::unique_ptr<WiFiDriver> wifi_driver;
82+
std::shared_ptr<TxFrame> txFrame;
7383
uint32_t video_channel_id_be;
7484
uint32_t mavlink_channel_id_be;
7585
uint32_t udp_channel_id_be;

0 commit comments

Comments
 (0)