Skip to content

Commit 79b49c7

Browse files
authored
Lot of improvements over a long weekend. See commit description for details. (#55)
* Decrease buffered queue size * Reapply "Fix BufferedPacketQueue + test" This reverts commit a66ffa3. * Update Devourer and WFB * Many stability fixes Many fixes related to adaptive link and non-adaptive link usage. Fixed crashes on application minimization Adaptive link now starts faster Channel change is possible without restart * [MINOR] Remove unneeded declaration
1 parent 17d521d commit 79b49c7

9 files changed

Lines changed: 221 additions & 55 deletions

File tree

app/src/main/java/com/openipc/pixelpilot/VideoActivity.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ protected void onCreate(Bundle savedInstanceState) {
245245
// WFB-NG Setup
246246
initializeWfbNg();
247247

248+
// Options like tx power must be initialized explicitly
249+
initDefaultOptions();
250+
248251
// Video Player(s) Setup
249252
initializeVideoPlayers();
250253

@@ -674,9 +677,7 @@ private void setupAdaptiveLinkSubMenu(PopupMenu popup) {
674677

675678
SharedPreferences prefs = getSharedPreferences("general", MODE_PRIVATE);
676679
boolean adaptiveEnabled = prefs.getBoolean("adaptive_link_enabled", true);
677-
int adaptiveTxPower = prefs.getInt("adaptive_tx_power", 30);
678-
wfbLink.nativeSetAdaptiveLinkEnabled(adaptiveEnabled);
679-
wfbLink.nativeSetTxPower(adaptiveTxPower);
680+
int adaptiveTxPower = prefs.getInt("adaptive_tx_power", 20);
680681

681682
// Adaptive link Enable option
682683
MenuItem adaptiveEnable = adaptiveMenu.add("Enable");
@@ -718,6 +719,14 @@ private void setupAdaptiveLinkSubMenu(PopupMenu popup) {
718719
}
719720
}
720721

722+
void initDefaultOptions(){
723+
SharedPreferences prefs = getSharedPreferences("general", MODE_PRIVATE);
724+
boolean adaptiveEnabled = prefs.getBoolean("adaptive_link_enabled", true);
725+
int adaptiveTxPower = prefs.getInt("adaptive_tx_power", 20);
726+
wfbLink.nativeSetAdaptiveLinkEnabled(adaptiveEnabled);
727+
wfbLink.nativeSetTxPower(adaptiveTxPower);
728+
}
729+
721730
/**
722731
* Submenu for recording options, including start/stop DVR and toggling fMP4.
723732
*/
@@ -1190,11 +1199,11 @@ protected void onResume() {
11901199
wfbLinkManager.refreshAdapters();
11911200

11921201
wfbLinkManager.startAdapters();
1202+
videoPlayer.start();
1203+
videoPlayer.startAudio();
11931204

11941205
osdManager.restoreOSDConfig();
11951206

1196-
videoPlayer.start();
1197-
videoPlayer.startAudio();
11981207

11991208
super.onResume();
12001209
}

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
#if defined(__ANDROID__) || defined(__ANDROID_API__)
12
#include <android/log.h>
3+
#else
4+
#include <cassert>
5+
#include <cstdio>
6+
#endif
27
#include <algorithm>
38
#include <cstdarg>
49
#include <cstddef>
510
#include <cstdint>
611
#include <limits>
12+
713
#include <unordered_map>
814
#include <vector>
915

1016
// Define logging tag and maximum buffer size
1117
#define BUFFERED_QUEUE_LOG_TAG "BufferedPacketQueue"
12-
// Considering the packet rate about 1000 packets per second, 100 packets should be enough
13-
constexpr size_t MAX_BUFFER_SIZE = 50;
18+
// Considering the packet rate about 100 packets per second, 10 packets should be enough
19+
constexpr size_t MAX_BUFFER_SIZE = 5;
1420
// Number of monotonically increasing packets
15-
constexpr size_t MONOTONIC_THRESHOLD = 40;
21+
constexpr size_t MONOTONIC_THRESHOLD = 3;
1622

1723
// Type definition for sequence numbers
18-
using SeqType = uint16_t;
24+
using SeqType = uint16_t;
25+
using SignedSeq = std::make_signed_t<SeqType>;
1926

2027
/**
2128
* @brief BufferedPacketQueue class handles packet processing with sequence numbers,
@@ -104,7 +111,10 @@ class BufferedPacketQueue
104111
* @param currPacketIdx Sequence index of the incoming packet.
105112
* @return True if it's the next expected packet; otherwise, false.
106113
*/
107-
bool isNextExpectedPacket(SeqType currPacketIdx) const { return currPacketIdx == mLastPacketIdx + 1; }
114+
bool isNextExpectedPacket(SeqType currPacketIdx) const
115+
{
116+
return currPacketIdx == static_cast<SeqType>(mLastPacketIdx + 1);
117+
}
108118

109119
/**
110120
* @brief Processes an in-order packet by invoking the callback and updating state.
@@ -170,8 +180,8 @@ class BufferedPacketQueue
170180

171181
if (isDuplicatePacket(currPacketIdx))
172182
{
173-
logWarning("Duplicate packet received with Sequence=%u. Ignoring.", currPacketIdx);
174-
return;
183+
logWarning("Duplicate packet received with Sequence=%u. ", currPacketIdx);
184+
// return;
175185
}
176186

177187
bufferPacket(currPacketIdx, data, data_length);
@@ -312,11 +322,20 @@ class BufferedPacketQueue
312322
*/
313323
void logDebug(const char* format, ...) const
314324
{
325+
#if defined(__ANDROID__) || defined(__ANDROID_API__)
315326
return;
316327
va_list args;
317328
va_start(args, format);
318329
__android_log_vprint(ANDROID_LOG_DEBUG, BUFFERED_QUEUE_LOG_TAG, format, args);
319330
va_end(args);
331+
#else
332+
// Fallback to standard output for non-Android platforms
333+
va_list args;
334+
va_start(args, format);
335+
vfprintf(stderr, format, args);
336+
va_end(args);
337+
fprintf(stderr, "\n");
338+
#endif
320339
}
321340

322341
/**
@@ -326,9 +345,18 @@ class BufferedPacketQueue
326345
*/
327346
void logWarning(const char* format, ...) const
328347
{
348+
#if defined(__ANDROID__) || defined(__ANDROID_API__)
329349
va_list args;
330350
va_start(args, format);
331351
__android_log_vprint(ANDROID_LOG_WARN, BUFFERED_QUEUE_LOG_TAG, format, args);
332352
va_end(args);
353+
#else
354+
// Fallback to standard output for non-Android platforms
355+
va_list args;
356+
va_start(args, format);
357+
vfprintf(stderr, format, args);
358+
va_end(args);
359+
fprintf(stderr, "\n");
360+
#endif
333361
}
334362
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "BufferedPacketQueue.h" // the class under test
2+
#include <gtest/gtest.h>
3+
#include <cstdint>
4+
#include <vector>
5+
6+
// ---------- Test fixture ----------------------------------------------------
7+
class BufferedPacketQueueTest : public ::testing::Test
8+
{
9+
protected:
10+
BufferedPacketQueue q;
11+
std::vector<uint16_t> delivered;
12+
13+
void SetUp() override { delivered.clear(); }
14+
15+
/* Helper: feed one packet and record what the queue actually delivers. */
16+
void feed(uint16_t seq)
17+
{
18+
uint16_t dummy = seq;
19+
20+
auto cb = [this](const uint8_t* seq, std::size_t)
21+
{
22+
std::cout << "Delivered packet with sequence: " << *(uint16_t*) seq << std::endl;
23+
delivered.push_back(*(uint16_t*) seq);
24+
};
25+
26+
q.processPacket(seq, (uint8_t*) &dummy, 2, cb);
27+
}
28+
};
29+
30+
// ---------- The reproduction test ------------------------------------------
31+
TEST_F(BufferedPacketQueueTest, WrapAroundDelivers)
32+
{
33+
feed(65534);
34+
feed(65535);
35+
ASSERT_EQ(delivered, (std::vector<uint16_t>{65534, 65535}));
36+
37+
for (uint16_t s = 0; s < 25; ++s) feed(s);
38+
39+
std::vector<uint16_t> expected = {65534, 65535};
40+
for (uint16_t s = 0; s < 25; ++s) expected.push_back(s);
41+
42+
ASSERT_EQ(delivered, expected) << "Overflow flush should deliver the entire block in one shot";
43+
}
44+
45+
TEST_F(BufferedPacketQueueTest, ReorderedDeliversInOrder)
46+
{
47+
feed(65533);
48+
feed(65535);
49+
feed(65534);
50+
ASSERT_EQ(delivered, (std::vector<uint16_t>{65533, 65534, 65535}));
51+
52+
for (uint16_t s = 0; s < 25; ++s) feed(s);
53+
54+
std::vector<uint16_t> expected = {65533, 65534, 65535};
55+
for (uint16_t s = 0; s < 25; ++s) expected.push_back(s);
56+
57+
ASSERT_EQ(delivered, expected) << "Overflow flush should deliver the entire block in one shot";
58+
}
59+
60+
// ---------- gtest boilerplate main -----------------------------------------
61+
int main(int argc, char** argv)
62+
{
63+
::testing::InitGoogleTest(&argc, argv);
64+
return RUN_ALL_TESTS();
65+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# CMakeLists.txt — build + run the unit test
2+
#
3+
# Requires CMake ≥ 3.14 (for FetchContent) and a C++17 toolchain.
4+
5+
cmake_minimum_required(VERSION 3.14)
6+
project(BufferedPacketQueueTests LANGUAGES CXX)
7+
8+
# ---------- Toolchain basics -------------------------------------------------
9+
set(CMAKE_CXX_STANDARD 17)
10+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11+
set(CMAKE_CXX_EXTENSIONS OFF)
12+
13+
# ---------- GoogleTest (fetched at configure time) ---------------------------
14+
include(FetchContent)
15+
16+
FetchContent_Declare(
17+
googletest
18+
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
19+
)
20+
# Keep GoogleTest from messing with CRT flags on MSVC
21+
set(gtest_force_shared_crt OFF CACHE BOOL "" FORCE)
22+
FetchContent_MakeAvailable(googletest)
23+
24+
enable_testing()
25+
26+
# ---------- Test executable --------------------------------------------------
27+
add_executable(queue_test
28+
BufferedPacketqueue_test.cpp
29+
)
30+
31+
target_include_directories(queue_test PUBLIC
32+
${CMAKE_CURRENT_SOURCE_DIR}
33+
${CMAKE_CURRENT_SOURCE_DIR}/../
34+
)
35+
target_link_libraries(queue_test
36+
GTest::gtest_main
37+
)
38+
39+
# Discover and register the test with CTest
40+
include(GoogleTest)
41+
gtest_discover_tests(queue_test)

app/wfbngrtl8812/src/main/cpp/SignalQualityCalculator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ SignalQualityCalculator::SignalQuality SignalQualityCalculator::calculate_signal
9696

9797
// __android_log_print(ANDROID_LOG_ERROR, "DEBUG", "FEC RECOVERED %d, FEC_LOST %d", p_recovered, p_lost);
9898

99-
float quality = avg_rssi - static_cast<float>(p_recovered) * 12.f - static_cast<float>(p_lost) * 40.f;
99+
float quality = avg_rssi; // - static_cast<float>(p_recovered) * 12.f - static_cast<float>(p_lost) * 40.f;
100100
quality = std::max(-1024.f, std::min(1024.f, quality));
101101

102102
ret.quality = quality;

0 commit comments

Comments
 (0)