From 805421ac73fda11f338177fb57a928b2eb4553d7 Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Wed, 27 Nov 2024 13:13:11 +0100 Subject: [PATCH 1/3] revert ktor to version 2.3.13 --- .../pedro/streamer/rotation/CameraFragment.kt | 15 +++++++++-- .../pedro/common/socket/TcpStreamSocket.kt | 25 ++++++++----------- .../pedro/common/socket/TcpStreamSocketImp.kt | 7 +++++- .../pedro/common/socket/UdpStreamSocket.kt | 5 ++-- gradle/libs.versions.toml | 2 +- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt index 3e2df4b77..de9f8e361 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt @@ -35,13 +35,17 @@ import com.pedro.library.generic.GenericStream import com.pedro.encoder.input.sources.video.Camera1Source import com.pedro.encoder.input.sources.video.Camera2Source import com.pedro.extrasources.CameraXSource +import com.pedro.library.rtmp.RtmpStream import com.pedro.library.util.BitrateAdapter import com.pedro.streamer.R import com.pedro.streamer.utils.PathUtils import com.pedro.streamer.utils.toast +import java.security.cert.X509Certificate import java.text.SimpleDateFormat import java.util.Date import java.util.Locale +import javax.net.ssl.TrustManager +import javax.net.ssl.X509TrustManager /** * Example code to stream using StreamBase. This is the recommend way to use the library. @@ -70,14 +74,21 @@ import java.util.Locale @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class CameraFragment: Fragment(), ConnectChecker { + class AcceptAllCertificates: X509TrustManager { + override fun checkClientTrusted(chain: Array?, authType: String?) {} + override fun checkServerTrusted(chain: Array?, authType: String?) {} + override fun getAcceptedIssuers(): Array? = null + } + companion object { fun getInstance(): CameraFragment = CameraFragment() } - val genericStream: GenericStream by lazy { - GenericStream(requireContext(), this).apply { + val genericStream: RtmpStream by lazy { + RtmpStream(requireContext(), this).apply { getGlInterface().autoHandleOrientation = true getStreamClient().setBitrateExponentialFactor(0.5f) + getStreamClient().addCertificates(AcceptAllCertificates()) } } private lateinit var surfaceView: SurfaceView diff --git a/common/src/main/java/com/pedro/common/socket/TcpStreamSocket.kt b/common/src/main/java/com/pedro/common/socket/TcpStreamSocket.kt index 13bf2d625..ab13ba63a 100644 --- a/common/src/main/java/com/pedro/common/socket/TcpStreamSocket.kt +++ b/common/src/main/java/com/pedro/common/socket/TcpStreamSocket.kt @@ -21,14 +21,11 @@ package com.pedro.common.socket import io.ktor.network.selector.SelectorManager import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.ByteWriteChannel -import io.ktor.utils.io.readByte import io.ktor.utils.io.readFully import io.ktor.utils.io.readUTF8Line -import io.ktor.utils.io.writeByte import io.ktor.utils.io.writeFully import io.ktor.utils.io.writeStringUtf8 import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withTimeout import java.net.ConnectException /** @@ -36,7 +33,7 @@ import java.net.ConnectException */ abstract class TcpStreamSocket: StreamSocket { - private val timeout = 5000L + protected val timeout = 5000L protected var input: ByteReadChannel? = null protected var output: ByteWriteChannel? = null protected var selectorManager = SelectorManager(Dispatchers.IO) @@ -45,16 +42,16 @@ abstract class TcpStreamSocket: StreamSocket { output?.flush() } - suspend fun write(b: Int) = withTimeout(timeout) { + suspend fun write(b: Int) { output?.writeByte(b.toByte()) } - suspend fun write(b: ByteArray) = withTimeout(timeout) { + suspend fun write(b: ByteArray) { output?.writeFully(b) } - suspend fun write(b: ByteArray, offset: Int, size: Int) = withTimeout(timeout) { - output?.writeFully(b, offset, offset + size) + suspend fun write(b: ByteArray, offset: Int, size: Int) { + output?.writeFully(b, offset, size) } suspend fun writeUInt16(b: Int) { @@ -73,13 +70,13 @@ abstract class TcpStreamSocket: StreamSocket { writeUInt32(Integer.reverseBytes(b)) } - suspend fun write(string: String) = withTimeout(timeout) { + suspend fun write(string: String) { output?.writeStringUtf8(string) } - suspend fun read(): Int = withTimeout(timeout) { + suspend fun read(): Int { val input = input ?: throw ConnectException("Read with socket closed, broken pipe") - input.readByte().toInt() + return input.readByte().toInt() } suspend fun readUInt16(): Int { @@ -104,13 +101,13 @@ abstract class TcpStreamSocket: StreamSocket { return Integer.reverseBytes(readUInt32()) } - suspend fun readUntil(b: ByteArray) = withTimeout(timeout) { + suspend fun readUntil(b: ByteArray) { val input = input ?: throw ConnectException("Read with socket closed, broken pipe") input.readFully(b) } - suspend fun readLine(): String? = withTimeout(timeout) { + suspend fun readLine(): String? { val input = input ?: throw ConnectException("Read with socket closed, broken pipe") - input.readUTF8Line() + return input.readUTF8Line() } } \ No newline at end of file diff --git a/common/src/main/java/com/pedro/common/socket/TcpStreamSocketImp.kt b/common/src/main/java/com/pedro/common/socket/TcpStreamSocketImp.kt index e426abdc4..d31b35ca3 100644 --- a/common/src/main/java/com/pedro/common/socket/TcpStreamSocketImp.kt +++ b/common/src/main/java/com/pedro/common/socket/TcpStreamSocketImp.kt @@ -47,7 +47,12 @@ class TcpStreamSocketImp( override suspend fun connect() { selectorManager = SelectorManager(Dispatchers.IO) - val builder = aSocket(selectorManager).tcp().connect(remoteAddress = InetSocketAddress(host, port)) + val builder = aSocket(selectorManager).tcp().connect( + remoteAddress = InetSocketAddress(host, port), + configure = { + if (!secured) socketTimeout = timeout + } + ) val socket = if (secured) { builder.tls(Dispatchers.Default) { trustManager = certificate diff --git a/common/src/main/java/com/pedro/common/socket/UdpStreamSocket.kt b/common/src/main/java/com/pedro/common/socket/UdpStreamSocket.kt index ce8cb1b41..e74194cc7 100644 --- a/common/src/main/java/com/pedro/common/socket/UdpStreamSocket.kt +++ b/common/src/main/java/com/pedro/common/socket/UdpStreamSocket.kt @@ -25,10 +25,9 @@ import io.ktor.network.sockets.InetSocketAddress import io.ktor.network.sockets.aSocket import io.ktor.network.sockets.isClosed import io.ktor.utils.io.core.ByteReadPacket -import io.ktor.utils.io.core.remaining +import io.ktor.utils.io.core.readBytes import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import kotlinx.io.readByteArray import java.net.ConnectException import java.net.InetAddress @@ -78,7 +77,7 @@ class UdpStreamSocket( val socket = socket ?: throw ConnectException("Read with socket closed, broken pipe") val packet = socket.receive().packet val length = packet.remaining.toInt() - return packet.readByteArray().sliceArray(0 until length) + return packet.readBytes().sliceArray(0 until length) } suspend fun writePacket(bytes: ByteArray) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8e3ca57cc..ee57df29a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,7 +19,7 @@ annotation = "1.9.1" coroutines = "1.9.0" junit = "4.13.2" mockito = "5.4.0" -ktor = "3.0.1" +ktor = "2.3.13" uvcandroid = "1.0.7" media3 = "1.5.0" From 51bc2754dcab85b48f5868a71ebce05d0e504bad Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Wed, 27 Nov 2024 13:19:36 +0100 Subject: [PATCH 2/3] remove autoaccept cerftificates --- .../com/pedro/streamer/rotation/CameraFragment.kt | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt index de9f8e361..d7ed34223 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt @@ -30,22 +30,18 @@ import android.widget.TextView import androidx.annotation.RequiresApi import androidx.fragment.app.Fragment import com.pedro.common.ConnectChecker -import com.pedro.library.base.recording.RecordController -import com.pedro.library.generic.GenericStream import com.pedro.encoder.input.sources.video.Camera1Source import com.pedro.encoder.input.sources.video.Camera2Source import com.pedro.extrasources.CameraXSource +import com.pedro.library.base.recording.RecordController import com.pedro.library.rtmp.RtmpStream import com.pedro.library.util.BitrateAdapter import com.pedro.streamer.R import com.pedro.streamer.utils.PathUtils import com.pedro.streamer.utils.toast -import java.security.cert.X509Certificate import java.text.SimpleDateFormat import java.util.Date import java.util.Locale -import javax.net.ssl.TrustManager -import javax.net.ssl.X509TrustManager /** * Example code to stream using StreamBase. This is the recommend way to use the library. @@ -74,12 +70,6 @@ import javax.net.ssl.X509TrustManager @RequiresApi(Build.VERSION_CODES.LOLLIPOP) class CameraFragment: Fragment(), ConnectChecker { - class AcceptAllCertificates: X509TrustManager { - override fun checkClientTrusted(chain: Array?, authType: String?) {} - override fun checkServerTrusted(chain: Array?, authType: String?) {} - override fun getAcceptedIssuers(): Array? = null - } - companion object { fun getInstance(): CameraFragment = CameraFragment() } @@ -88,7 +78,6 @@ class CameraFragment: Fragment(), ConnectChecker { RtmpStream(requireContext(), this).apply { getGlInterface().autoHandleOrientation = true getStreamClient().setBitrateExponentialFactor(0.5f) - getStreamClient().addCertificates(AcceptAllCertificates()) } } private lateinit var surfaceView: SurfaceView From 4c7603fb6924899dc79bd42ebee2dc4078e9dca9 Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Wed, 27 Nov 2024 13:21:34 +0100 Subject: [PATCH 3/3] remove test --- .../main/java/com/pedro/streamer/rotation/CameraFragment.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt index d7ed34223..a8ead22f3 100644 --- a/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt +++ b/app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt @@ -34,7 +34,7 @@ import com.pedro.encoder.input.sources.video.Camera1Source import com.pedro.encoder.input.sources.video.Camera2Source import com.pedro.extrasources.CameraXSource import com.pedro.library.base.recording.RecordController -import com.pedro.library.rtmp.RtmpStream +import com.pedro.library.generic.GenericStream import com.pedro.library.util.BitrateAdapter import com.pedro.streamer.R import com.pedro.streamer.utils.PathUtils @@ -74,8 +74,8 @@ class CameraFragment: Fragment(), ConnectChecker { fun getInstance(): CameraFragment = CameraFragment() } - val genericStream: RtmpStream by lazy { - RtmpStream(requireContext(), this).apply { + val genericStream: GenericStream by lazy { + GenericStream(requireContext(), this).apply { getGlInterface().autoHandleOrientation = true getStreamClient().setBitrateExponentialFactor(0.5f) }