Skip to content

Commit

Permalink
fix handshake ipaddress
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 8, 2023
1 parent 8f034fc commit 5327b3b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public void onClick(View view) {
if (srtCamera1.isRecording()
|| srtCamera1.prepareAudio() && srtCamera1.prepareVideo()) {
button.setText(R.string.stop_button);
srtCamera1.startStream(etUrl.getText().toString());
srtCamera1.startStream("srt://192.168.0.191:9999/srt://192.168.0.191:9999/app/stream");
} else {
Toast.makeText(this, "Error preparing stream, This device cant do it",
Toast.LENGTH_SHORT).show();
Expand Down
16 changes: 3 additions & 13 deletions srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CommandsManager {
var startTS = 0L //microSeconds
var audioDisabled = false
var videoDisabled = false
var host = ""
//Avoid write a packet in middle of other.
private val writeSync = Mutex(locked = false)

Expand All @@ -63,7 +64,7 @@ class CommandsManager {
suspend fun writeHandshake(socket: SrtSocket?, handshake: Handshake = Handshake()) {
writeSync.withLock {
handshake.initialPacketSequence = sequenceNumber
handshake.ipAddress = getIPAddress()
handshake.ipAddress = host
handshake.write(getTs(), 0)
Log.i(TAG, handshake.toString())
socket?.write(handshake)
Expand Down Expand Up @@ -144,25 +145,14 @@ class CommandsManager {
MTU = Constants.MTU
socketId = 0
startTS = 0L
host = ""
packetHandlingQueue.clear()
}

private fun generateInitialSequence(): Int {
return Random.nextInt(0, Int.MAX_VALUE)
}

private fun getIPAddress(): String {
val interfaces: List<NetworkInterface> = NetworkInterface.getNetworkInterfaces().toList()
val vpnInterfaces = interfaces.filter { it.displayName.contains("tun") }
val address: String by lazy { interfaces.findAddress().firstOrNull() ?: "0.0.0.0" }
return if (vpnInterfaces.isNotEmpty()) {
val vpnAddresses = vpnInterfaces.findAddress()
vpnAddresses.firstOrNull() ?: address
} else {
address
}
}

private fun List<NetworkInterface>.findAddress(): List<String?> = this.asSequence()
.map { addresses -> addresses.inetAddresses.asSequence() }
.flatten()
Expand Down
1 change: 1 addition & 0 deletions srt/src/main/java/com/pedro/srt/srt/SrtClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class SrtClient(private val connectCheckerSrt: ConnectCheckerSrt) {
val streamName =
if (srtMatcher.group(4).isNullOrEmpty()) "" else "/" + srtMatcher.group(4)
val path = "${srtMatcher.group(3)}$streamName".trim()
commandsManager.host = host

val error = runCatching {
socket = SrtSocket(host, port)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.pedro.srt.utils.readUInt32
import com.pedro.srt.utils.writeUInt16
import com.pedro.srt.utils.writeUInt32
import java.io.InputStream
import java.net.InetAddress

/**
* Created by pedro on 21/8/23.
Expand Down Expand Up @@ -103,9 +104,15 @@ data class Handshake(
}

private fun writeAddress() {
val numbers = ipAddress.split(".").map { it.trim().toInt() }
numbers.forEach {
buffer.writeUInt32(it)
val address = InetAddress.getByName(ipAddress)
val bytes = address.address.toList().chunked(4).map { it.reversed() }
bytes.forEach {
buffer.write(it.toByteArray())
}
if (bytes.size == 1) { //ipv4
buffer.writeUInt32(0)
buffer.writeUInt32(0)
buffer.writeUInt32(0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.pedro.srt.utils.writeUInt32
*
*/
data class HandshakeExtension(
private val version: String = "1.4.4",
private val version: String = "1.5.3",
private val flags: Int = ExtensionContentFlag.REXMITFLG.value or ExtensionContentFlag.CRYPT.value,
private val receiverDelay: Int = 120,
private val senderDelay: Int = 0,
Expand Down

0 comments on commit 5327b3b

Please sign in to comment.