From 74e624267ed51cb95f2008fc9a39be8cd99d4078 Mon Sep 17 00:00:00 2001 From: shogo4405 Date: Sun, 18 Jul 2021 18:56:35 +0900 Subject: [PATCH] Support Authentication --- README.md | 2 +- .../java/com/haishinkit/net/NetSocketImpl.kt | 1 - .../com/haishinkit/rtmp/RtmpConnection.kt | 5 ++- .../java/com/haishinkit/rtmp/RtmpSocket.kt | 33 +++++++++---------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 37107d8ee..b4f3007a2 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ ## Features ### RTMP -- [ ] Authentication +- [x] Authentication - [x] Publish (H264/AAC) - [x] Playback - [ ] Action Message Format diff --git a/haishinkit/src/main/java/com/haishinkit/net/NetSocketImpl.kt b/haishinkit/src/main/java/com/haishinkit/net/NetSocketImpl.kt index 0f5c3e3a3..b810c2b49 100644 --- a/haishinkit/src/main/java/com/haishinkit/net/NetSocketImpl.kt +++ b/haishinkit/src/main/java/com/haishinkit/net/NetSocketImpl.kt @@ -61,7 +61,6 @@ internal class NetSocketImpl : NetSocket, CoroutineScope { outputStream = null socket?.close() outputQueue.clear() - inputBuffer.clear() } override fun doOutput(buffer: ByteBuffer) { diff --git a/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpConnection.kt b/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpConnection.kt index 1ed5791db..23b7461e7 100644 --- a/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpConnection.kt +++ b/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpConnection.kt @@ -181,8 +181,12 @@ open class RtmpConnection : EventDispatcher(null) { field = value } private var arguments: MutableList = mutableListOf() + private val authenticator: RtmpAuthenticator by lazy { + RtmpAuthenticator(this) + } init { + addEventListener(Event.RTMP_STATUS, authenticator) addEventListener(Event.RTMP_STATUS, EventListener(this)) } @@ -316,7 +320,6 @@ open class RtmpConnection : EventDispatcher(null) { buffer.position(rollback) throw e } - if (buffer.hasRemaining()) { listen(buffer) } diff --git a/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpSocket.kt b/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpSocket.kt index 36d256c9b..10afea880 100644 --- a/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpSocket.kt +++ b/haishinkit/src/main/java/com/haishinkit/rtmp/RtmpSocket.kt @@ -26,19 +26,15 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { var isConnected = false private set var timeout: Int - get() = socket.timeout + get() = socket?.timeout ?: 0 set(value) { - socket.timeout = value + socket?.timeout = value } val totalBytesIn: Long - get() = socket.totalBytesIn.get() + get() = socket?.totalBytesIn?.get() ?: 0 val totalBytesOut: Long - get() = socket.totalBytesOut.get() - private val socket: NetSocket by lazy { - val socket = NetSocketImpl() - socket.listener = this - socket - } + get() = socket?.totalBytesOut?.get() ?: 0 + private var socket: NetSocket? = null private val handshake: RtmpHandshake by lazy { RtmpHandshake() } @@ -49,15 +45,18 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { } fun connect(dstName: String, dstPort: Int, isSecure: Boolean) { - socket.connect(dstName, dstPort, isSecure) + socket?.listener = null + socket = NetSocketImpl() + socket?.listener = this + socket?.connect(dstName, dstPort, isSecure) } fun doOutput(buffer: ByteBuffer) { - socket.doOutput(buffer) + socket?.doOutput(buffer) } fun createByteBuffer(capacity: Int): ByteBuffer { - return socket.createByteBuffer(capacity) + return socket?.createByteBuffer(capacity) ?: ByteBuffer.allocate(capacity) } fun close(disconnected: Boolean) { @@ -71,7 +70,7 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { } } readyState = ReadyState.Closing - socket.close(disconnected) + socket?.close(disconnected) data?.let { connection.dispatchEventWith(Event.RTMP_STATUS, false, it) } @@ -90,7 +89,7 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { chunkSizeS = RtmpChunk.DEFAULT_SIZE handshake.clear() readyState = ReadyState.VersionSent - socket.doOutput(handshake.c0C1Packet) + socket?.doOutput(handshake.c0C1Packet) } override fun onInput(buffer: ByteBuffer) { @@ -100,7 +99,7 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { return } handshake.s0S1Packet = buffer - socket.doOutput(handshake.c2Packet) + socket?.doOutput(handshake.c2Packet) buffer.position(RtmpHandshake.SIGNAL_SIZE + 1) readyState = ReadyState.AckSent if (buffer.limit() - buffer.position() == RtmpHandshake.SIGNAL_SIZE) { @@ -128,7 +127,7 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { } catch (e: BufferUnderflowException) { if (VERBOSE) Log.d(TAG, "", e) } catch (e: IllegalArgumentException) { - Log.w(TAG, "", e) + if (VERBOSE) Log.w(TAG, "", e) throw e } else -> {} @@ -140,7 +139,7 @@ internal class RtmpSocket(val connection: RtmpConnection) : NetSocket.Listener { } companion object { - private const val VERBOSE = false + private const val VERBOSE = true private var TAG = RtmpSocket::class.java.simpleName } }