Skip to content

Commit 40c33ec

Browse files
committed
Refactoring of FEC management class
1 parent c7936de commit 40c33ec

File tree

3 files changed

+82
-75
lines changed

3 files changed

+82
-75
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
#include <chrono>
5+
#include <android/log.h>
6+
7+
class FecChangeController
8+
{
9+
static constexpr const char *TAG = "FecChangeController";
10+
public:
11+
/// \brief Query the current (possibly decayed) fec_change value.
12+
/// Call this as often as you like; the class handles its own timing.
13+
int value()
14+
{
15+
if (!mEnabled)
16+
return 0;
17+
18+
std::lock_guard<std::mutex> lock(mx_);
19+
decayLocked_();
20+
return val_;
21+
}
22+
23+
/// \brief Raise fec_change. If newValue <= current, the call is ignored.
24+
/// A successful bump resets the 5-second “hold” timer.
25+
void bump(int newValue)
26+
{
27+
std::lock_guard<std::mutex> lock(mx_);
28+
if (newValue > val_) {
29+
__android_log_print(ANDROID_LOG_ERROR, TAG, "bumping FEC: %d", newValue);
30+
31+
val_ = newValue;
32+
lastChange_ = Clock::now();
33+
}
34+
}
35+
36+
void setEnabled(bool use){
37+
mEnabled = use;
38+
}
39+
40+
private:
41+
using Clock = std::chrono::steady_clock;
42+
static constexpr std::chrono::seconds kTick{1}; // length of one hold/decay window
43+
44+
void decayLocked_()
45+
{
46+
if (val_ == 0) return;
47+
48+
auto now = Clock::now();
49+
auto elapsed = now - lastChange_;
50+
51+
// Still inside the mandatory 5-second hold? Do nothing.
52+
if (elapsed < kTick) return;
53+
54+
// How many *full* ticks have passed since lastChange_?
55+
auto ticks = std::chrono::duration_cast<std::chrono::seconds>(elapsed).count() / kTick.count();
56+
if (ticks == 0) return; // safety net (shouldn’t hit)
57+
58+
int decayed = val_ - static_cast<int>(ticks);
59+
if (decayed < 0) decayed = 0;
60+
61+
// Commit the decay and anchor lastChange_ on the most recent tick boundary
62+
if (decayed != val_) {
63+
val_ = decayed;
64+
lastChange_ += kTick * ticks;
65+
}
66+
}
67+
68+
int val_ {0};
69+
Clock::time_point lastChange_ {Clock::now()};
70+
std::mutex mx_;
71+
bool mEnabled = true;
72+
};
73+

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

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void WfbngLink::initAgg() {
6565
video_channel_id_be = htobe32(video_channel_id_f);
6666
auto udsName = std::string("my_socket");
6767

68-
video_aggregator = std::make_unique<AggregatorUNIX>(udsName, keyPath, epoch, video_channel_id_f, 0);
68+
video_aggregator = std::make_unique<AggregatorUDPv4>(client_addr, 5600, keyPath, epoch, video_channel_id_f, 0);
6969

7070
int mavlink_client_port = 14550;
7171
uint8_t mavlink_radio_port = 0x10;
@@ -392,76 +392,6 @@ extern "C" JNIEXPORT void JNICALL Java_com_openipc_wfbngrtl8812_WfbNgLink_native
392392
native(wfbngLinkN)->initAgg();
393393
}
394394

395-
396-
class FecChangeController
397-
{
398-
public:
399-
/// \brief Query the current (possibly decayed) fec_change value.
400-
/// Call this as often as you like; the class handles its own timing.
401-
int value()
402-
{
403-
if (!mEnabled)
404-
return 0;
405-
406-
std::lock_guard<std::mutex> lock(mx_);
407-
decayLocked_();
408-
return val_;
409-
}
410-
411-
/// \brief Raise fec_change. If newValue <= current, the call is ignored.
412-
/// A successful bump resets the 5-second “hold” timer.
413-
void bump(int newValue)
414-
{
415-
std::lock_guard<std::mutex> lock(mx_);
416-
if (newValue > val_) {
417-
__android_log_print(ANDROID_LOG_ERROR, TAG, "bumping FEC: %d", newValue);
418-
419-
val_ = newValue;
420-
lastChange_ = Clock::now();
421-
}
422-
}
423-
424-
void setEnabled(bool use){
425-
mEnabled = use;
426-
}
427-
428-
private:
429-
using Clock = std::chrono::steady_clock;
430-
static constexpr std::chrono::seconds kTick{1}; // length of one hold/decay window
431-
432-
void decayLocked_()
433-
{
434-
if (val_ == 0) return;
435-
436-
auto now = Clock::now();
437-
auto elapsed = now - lastChange_;
438-
439-
// Still inside the mandatory 5-second hold? Do nothing.
440-
if (elapsed < kTick) return;
441-
442-
// How many *full* ticks have passed since lastChange_?
443-
auto ticks = std::chrono::duration_cast<std::chrono::seconds>(elapsed).count() / kTick.count();
444-
if (ticks == 0) return; // safety net (shouldn’t hit)
445-
446-
int decayed = val_ - static_cast<int>(ticks);
447-
if (decayed < 0) decayed = 0;
448-
449-
// Commit the decay and anchor lastChange_ on the most recent tick boundary
450-
if (decayed != val_) {
451-
val_ = decayed;
452-
lastChange_ += kTick * ticks;
453-
}
454-
}
455-
456-
int val_ {0};
457-
Clock::time_point lastChange_ {Clock::now()};
458-
std::mutex mx_;
459-
bool mEnabled = true;
460-
};
461-
462-
463-
FecChangeController fec;
464-
465395
// Modified start_link_quality_thread: use adaptive_link_enabled and adaptive_tx_power
466396
void WfbngLink::start_link_quality_thread(int fd) {
467397
auto thread_func = [this, fd]() {
@@ -567,7 +497,7 @@ void WfbngLink::start_link_quality_thread(int fd) {
567497
len = strlen(message + sizeof(len));
568498
len = htonl(len);
569499
memcpy(message, &len, sizeof(len));
570-
__android_log_print(ANDROID_LOG_ERROR, TAG, "message %s", message + 4);
500+
__android_log_print(ANDROID_LOG_ERROR, TAG, " message %s", message + 4);
571501
ssize_t sent = sendto(sockfd,
572502
message,
573503
strlen(message + sizeof(len)) + sizeof(len),
@@ -633,5 +563,6 @@ extern "C" JNIEXPORT void JNICALL Java_com_openipc_wfbngrtl8812_WfbNgLink_native
633563
jclass clazz,
634564
jlong wfbngLinkN,
635565
jint use) {
636-
fec.setEnabled(use);
566+
WfbngLink *link = native(wfbngLinkN);
567+
link->fec.setEnabled(use);
637568
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#ifndef FPV_VR_WFBNG_LINK_H
22
#define FPV_VR_WFBNG_LINK_H
33

4+
#include "FecChangeController.h"
45
#include "TxFrame.h"
6+
#include "SignalQualityCalculator.h"
7+
58
extern "C" {
69
#include "wfb-ng/src/fec.h"
710
}
811

9-
#include "SignalQualityCalculator.h"
1012
#include "devourer/src/WiFiDriver.h"
1113
#include "wfb-ng/src/rx.hpp"
1214
#include <jni.h>
@@ -28,7 +30,7 @@ class WfbngLink {
2830
void stop(JNIEnv *env, jobject androidContext, jint fd);
2931

3032
std::mutex agg_mutex;
31-
std::unique_ptr<AggregatorUNIX> video_aggregator;
33+
std::unique_ptr<AggregatorUDPv4> video_aggregator;
3234
std::unique_ptr<AggregatorUDPv4> mavlink_aggregator;
3335
std::unique_ptr<AggregatorUDPv4> udp_aggregator;
3436

@@ -43,6 +45,7 @@ class WfbngLink {
4345
std::map<int, std::unique_ptr<Rtl8812aDevice>> rtl_devices;
4446
std::unique_ptr<std::thread> link_quality_thread{nullptr};
4547
bool should_clear_stats{false};
48+
FecChangeController fec;
4649

4750
void init_thread(std::unique_ptr<std::thread> &thread,
4851
const std::function<std::unique_ptr<std::thread>()> &init_func) {

0 commit comments

Comments
 (0)