Skip to content

Commit

Permalink
Refactor VideoActivity and implement 40 MHz bandwidth mode (#35)
Browse files Browse the repository at this point in the history
* Refactor VideoActivity

* Implement 40MHz bandwidth support
  • Loading branch information
vertexodessa authored Jan 18, 2025
1 parent d9cc605 commit 93412d5
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 291 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ audio:
outputVolume: 30
```
## List of potential improvements:
* 40 MHz bandwidth
* 40 MHz bandwidth [x]
* support stream over ipv6
* adaptive link

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public interface SettingsChanged {
void onChannelSettingChanged(final int channel);
void onBandwidthSettingChanged(final int bw);
}
808 changes: 536 additions & 272 deletions app/src/main/java/com/openipc/pixelpilot/VideoActivity.java

Large diffs are not rendered by default.

34 changes: 31 additions & 3 deletions app/src/main/java/com/openipc/pixelpilot/WfbLinkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ public class WfbLinkManager extends BroadcastReceiver {
private final ActivityVideoBinding binding;
private final Context context;
private int wifiChannel;
private Bandwidth bandWidth;

public enum Bandwidth {
BANDWIDTH_20(20),
BANDWIDTH_40(40);

private final int value;

Bandwidth(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public WfbLinkManager(Context context, ActivityVideoBinding binding, WfbNgLink wfbNgLink) {
this.binding = binding;
Expand All @@ -39,6 +55,19 @@ public void refreshKey() {
public void setChannel(int channel) {
wifiChannel = channel;
}
public void setBandwidth(int bw) {
switch(bw)
{
case 20:
bandWidth = Bandwidth.BANDWIDTH_20;
break;
case 40:
bandWidth = Bandwidth.BANDWIDTH_40;
break;
default:
break;
}
}

@Override
public synchronized void onReceive(Context context, Intent intent) {
Expand Down Expand Up @@ -160,8 +189,7 @@ public synchronized void stopAdapter(UsbDevice dev) {
}
}

public synchronized void startAdapters(int channel) {
wifiChannel = channel;
public synchronized void startAdapters() {
if (wfbLink.isRunning()) {
return;
}
Expand All @@ -177,7 +205,7 @@ public synchronized boolean startAdapter(UsbDevice dev) {
String text = "Starting wfb-ng channel " + wifiChannel + " with " + String.format(
"[%04X", dev.getVendorId()) + ":" + String.format("%04X]", dev.getProductId());
binding.tvMessage.setText(text);
wfbLink.start(wifiChannel, dev);
wfbLink.start(wifiChannel, bandWidth.getValue(), dev);
return true;
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@
<item>173</item>
<item>177</item>
</string-array>
<string-array name="bandwidths">
<item>20</item>
<item>40</item>
</string-array>
</resources>
24 changes: 13 additions & 11 deletions app/wfbngrtl8812/src/main/cpp/WfbngLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void WfbngLink::initAgg() {
epoch, mavlink_channel_id_f);
}

int WfbngLink::run(JNIEnv *env, jobject context, jint wifiChannel, jint fd) {
int WfbngLink::run(JNIEnv* env, jobject context, jint wifiChannel, jint bw, jint fd) {
int r;
libusb_context *ctx = NULL;

Expand Down Expand Up @@ -121,11 +121,15 @@ int WfbngLink::run(JNIEnv *env, jobject context, jint wifiChannel, jint fd) {
4, 0, antenna, rssi, noise, freq, 0, 0, NULL);
}
};
rtl_devices.at(fd)->Init(packetProcessor, SelectedChannel{
.Channel = static_cast<uint8_t>(wifiChannel),
.ChannelOffset = 0,
.ChannelWidth = CHANNEL_WIDTH_20,
});

auto bandWidth = (bw == 20 ? CHANNEL_WIDTH_20 : CHANNEL_WIDTH_40);
rtl_devices.at(fd)->Init(packetProcessor,
SelectedChannel{
.Channel = static_cast<uint8_t>(wifiChannel),
.ChannelOffset = 0,
.ChannelWidth = bandWidth,
});

} catch (const std::runtime_error &error) {
__android_log_print(ANDROID_LOG_ERROR, TAG,
"runtime_error: %s", error.what());
Expand Down Expand Up @@ -193,11 +197,9 @@ Java_com_openipc_wfbngrtl8812_WfbNgLink_nativeInitialize(JNIEnv *env, jclass cla
return jptr(p);
}

extern "C" JNIEXPORT void JNICALL
Java_com_openipc_wfbngrtl8812_WfbNgLink_nativeRun(JNIEnv *env, jclass clazz, jlong wfbngLinkN,
jobject androidContext, jint wifiChannel,
jint fd) {
native(wfbngLinkN)->run(env, androidContext, wifiChannel, fd);
extern "C" JNIEXPORT void JNICALL Java_com_openipc_wfbngrtl8812_WfbNgLink_nativeRun(
JNIEnv* env, jclass clazz, jlong wfbngLinkN, jobject androidContext, jint wifiChannel, jint bw, jint fd) {
native(wfbngLinkN)->run(env, androidContext, wifiChannel, bw, fd);
}

extern "C" JNIEXPORT void JNICALL
Expand Down
2 changes: 1 addition & 1 deletion app/wfbngrtl8812/src/main/cpp/WfbngLink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WfbngLink {
public:
WfbngLink(JNIEnv *env, jobject context);

int run(JNIEnv *env, jobject androidContext, jint wifiChannel, jint fd);
int run(JNIEnv* env, jobject androidContext, jint wifiChannel, jint bw, jint fd);

void initAgg();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void run() {
// Native cpp methods.
public static native long nativeInitialize(Context context);

public static native void nativeRun(long nativeInstance, Context context, int wifiChannel, int fd);
public static native void nativeRun(long nativeInstance, Context context, int wifiChannel, int bandWidth, int fd);

public static native void nativeStop(long nativeInstance, Context context, int fd);

Expand All @@ -61,12 +61,12 @@ public void refreshKey() {
nativeRefreshKey(nativeWfbngLink);
}

public synchronized void start(int wifiChannel, UsbDevice usbDevice) {
public synchronized void start(int wifiChannel, int bandWidth, UsbDevice usbDevice) {
Log.d(TAG, "wfb-ng monitoring on " + usbDevice.getDeviceName() + " using wifi channel " + wifiChannel);
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
int fd = usbDeviceConnection.getFileDescriptor();
Thread t = new Thread(() -> nativeRun(nativeWfbngLink, context, wifiChannel, fd));
Thread t = new Thread(() -> nativeRun(nativeWfbngLink, context, wifiChannel, bandWidth, fd));
t.setName("wfb-" + usbDevice.getDeviceName().split("/dev/bus/usb/")[1]);
linkThreads.put(usbDevice, t);
linkConns.put(usbDevice, usbDeviceConnection);
Expand Down

0 comments on commit 93412d5

Please sign in to comment.