Repository navigation
Expose kernel TCP RTT for Java RTMP sockets #2211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ | |
| /build | ||
| .DS_Store | ||
| /.kotlin | ||
| /.claude | ||
| /.claude | ||
| /common/.cxx/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| cmake_minimum_required(VERSION 3.22.1) | ||
|
|
||
| project(rootencoder_common) | ||
|
|
||
| add_library( | ||
| rootencoder_common | ||
| SHARED | ||
| tcp_info.cpp | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include <cstddef> | ||
| #include <jni.h> | ||
| #include <netinet/in.h> | ||
| #include <netinet/tcp.h> | ||
| #include <sys/socket.h> | ||
|
|
||
| extern "C" JNIEXPORT jlong JNICALL | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this file performs one read-only query against the active TCP socket. It does not send packets or change the connection. it's like a small JNI adapter: Kotlin socket --> file descriptor --> kernel TCP_INFO --> smoothed RTT |
||
| Java_com_pedro_common_socket_java_TcpInfo_nativeGetRttMicros( | ||
| JNIEnv*, jobject, jint fd) { | ||
| tcp_info info{}; | ||
| socklen_t length = sizeof(info); | ||
|
|
||
| if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &length) != 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| constexpr size_t requiredLength = | ||
| offsetof(tcp_info, tcpi_rtt) + sizeof(info.tcpi_rtt); | ||
| if (length < requiredLength) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (info.tcpi_state != TCP_ESTABLISHED) { | ||
| return -1; | ||
| } | ||
|
|
||
| return static_cast<jlong>(info.tcpi_rtt); | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
common/src/main/java/com/pedro/common/socket/base/TcpStreamSocket.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
common/src/main/java/com/pedro/common/socket/java/TcpInfo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.pedro.common.socket.java | ||
|
|
||
| import androidx.annotation.Keep | ||
|
|
||
| @Keep | ||
| internal object TcpInfo { | ||
|
|
||
| private val loaded by lazy { | ||
| runCatching { System.loadLibrary("rootencoder_common") }.isSuccess | ||
| } | ||
|
|
||
| fun getRttMicros(fd: Int): Long? { | ||
| if (!loaded) return null | ||
| return runCatching { nativeGetRttMicros(fd) } | ||
| .getOrNull() | ||
| ?.takeIf { it > 0 } | ||
| } | ||
|
|
||
| private external fun nativeGetRttMicros(fd: Int): Long | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kotlin/Java cannot call Linux’s getsockopt(TCP_INFO) directly. We need the small C++ JNI bridge in tcp_info.cpp, and Android’s normal Kotlin compiler cannot compile C++.
This is the first native code and CMake/NDK toolchain maintained by RootEncoder. Every published protocol artifact depends on
common, whose release AAR now contains the JNI library for four ABIs (about 15 KB total uncompressed, or roughly 3–4 KB per ABI).commoncurrently owns the socket and descriptor lifetime, so keeping the shim there avoids exposing descriptors or adding a provider API and leaves room for RTSP-over-TCP reuse.But if keeping common native-free is preferred, I can instead split this into an optional native artifact with an integration point in common.