Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import javax.net.ssl.TrustManager
*/
abstract class StreamSocket {
protected var timeout = DEFAULT_TIMEOUT
protected var hostVerification = false
abstract suspend fun connect()
abstract suspend fun close()
abstract fun isConnected(): Boolean
Expand All @@ -39,14 +40,15 @@ abstract class StreamSocket {
fun createTcpSocket(
type: SocketType,
host: String, port: Int, secured: Boolean,
timeout: Long,
timeout: Long, hostVerification: Boolean,
certificates: TrustManager? = null
): TcpStreamSocket {
return when (type) {
SocketType.KTOR -> TcpStreamSocketKtor(host, port, secured, certificates)
SocketType.JAVA -> TcpStreamSocketJava(host, port, secured, certificates)
}.apply {
this.timeout = timeout
this.hostVerification = hostVerification
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pedro.common.socket.java

import android.os.Build
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
Expand Down Expand Up @@ -30,6 +31,12 @@ class TcpStreamSocketJava(
val socketAddress: SocketAddress = InetSocketAddress(host, port)
socket.connect(socketAddress, timeout.toInt())
socket.soTimeout = timeout.toInt()
if (hostVerification && socket is SSLSocket && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
socket.sslParameters = socket.sslParameters.apply {
endpointIdentificationAlgorithm = "HTTPS"
}
socket.startHandshake()
}
return socket
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TcpStreamSocketKtor(
builder.tls(Dispatchers.IO) {
trustManager = certificate
random = SecureRandom()
if (hostVerification) serverName = host
}
} else builder
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ class RtmpStreamClient(
rtmpClient.socketType = type
}

/**
* If using TLS socket force to check host certificate
*/
fun setTlsHostVerification(enabled: Boolean) {
rtmpClient.tlsHostVerification = enabled
}

/**
* Set timeout ms for connection, write and read in sockets by default 5000ms
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ class RtspStreamClient(
rtspClient.socketType = type
}

/**
* If using TLS socket force to check host certificate
*/
fun setTlsHostVerification(enabled: Boolean) {
rtspClient.tlsHostVerification = enabled
}

/**
* Set timeout ms for connection, write and read in sockets by default 5000ms
*/
Expand Down
5 changes: 3 additions & 2 deletions rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
get() = rtmpSender.getSentVideoFrames()
val bytesSend: Long
get() = rtmpSender.getBytesSend()
var socketType = SocketType.KTOR
var socketType = SocketType.JAVA
var tlsHostVerification = false
var socketTimeout = StreamSocket.DEFAULT_TIMEOUT
var shouldFailOnRead = false
var shouldSendPings = false
Expand Down Expand Up @@ -352,7 +353,7 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
val socket = if (tunneled) {
TcpTunneledSocket(commandsManager.host, commandsManager.port, tlsEnabled)
} else {
TcpSocket(socketType, commandsManager.host, commandsManager.port, tlsEnabled, socketTimeout, certificates)
TcpSocket(socketType, commandsManager.host, commandsManager.port, tlsEnabled, socketTimeout, tlsHostVerification, certificates)
}
this.socket = socket
socket.connect()
Expand Down
4 changes: 2 additions & 2 deletions rtmp/src/main/java/com/pedro/rtmp/utils/socket/TcpSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import javax.net.ssl.TrustManager
class TcpSocket(
type: SocketType,
host: String, port: Int, secured: Boolean,
timeout: Long,
timeout: Long, hostVerification: Boolean,
certificates: TrustManager?
): RtmpSocket() {

private val socket = StreamSocket.createTcpSocket(type, host, port, secured, timeout, certificates)
private val socket = StreamSocket.createTcpSocket(type, host, port, secured, timeout, hostVerification, certificates)

override suspend fun flush(isPacket: Boolean) {
socket.flush()
Expand Down
5 changes: 3 additions & 2 deletions rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class RtspClient(private val connectChecker: ConnectChecker) {
get() = rtspSender.getSentVideoFrames()
val bytesSend: Long
get() = rtspSender.getBytesSend()
var socketType = SocketType.KTOR
var socketType = SocketType.JAVA
var tlsHostVerification = false
var socketTimeout = StreamSocket.DEFAULT_TIMEOUT

/**
Expand Down Expand Up @@ -246,7 +247,7 @@ class RtspClient(private val connectChecker: ConnectChecker) {
}
rtspSender.setVideoInfo(commandsManager.sps!!, commandsManager.pps, commandsManager.vps)
}
val socket = StreamSocket.createTcpSocket(socketType, host, port, tlsEnabled, socketTimeout, certificates)
val socket = StreamSocket.createTcpSocket(socketType, host, port, tlsEnabled, socketTimeout, tlsHostVerification, certificates)
this@RtspClient.socket = socket
socket.connect()
socket.write(commandsManager.createOptions())
Expand Down
2 changes: 1 addition & 1 deletion srt/src/main/java/com/pedro/srt/srt/SrtClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SrtClient(private val connectChecker: ConnectChecker) {
var packetsLost = 0
private set
private var latency = 120_000 //in micro
var socketType = SocketType.KTOR
var socketType = SocketType.JAVA
var socketTimeout = StreamSocket.DEFAULT_TIMEOUT

fun setVideoCodec(videoCodec: VideoCodec) {
Expand Down
2 changes: 1 addition & 1 deletion udp/src/main/java/com/pedro/udp/UdpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class UdpClient(private val connectChecker: ConnectChecker) {
get() = udpSender.getSentVideoFrames()
val bytesSend: Long
get() = udpSender.getBytesSend()
var socketType = SocketType.KTOR
var socketType = SocketType.JAVA
var socketTimeout = StreamSocket.DEFAULT_TIMEOUT

fun setVideoCodec(videoCodec: VideoCodec) {
Expand Down
Loading