Skip to content

Commit

Permalink
Support Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Jul 18, 2021
1 parent 4845d47 commit 74e6242
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## Features
### RTMP
- [ ] Authentication
- [x] Authentication
- [x] Publish (H264/AAC)
- [x] Playback
- [ ] Action Message Format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ internal class NetSocketImpl : NetSocket, CoroutineScope {
outputStream = null
socket?.close()
outputQueue.clear()
inputBuffer.clear()
}

override fun doOutput(buffer: ByteBuffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ open class RtmpConnection : EventDispatcher(null) {
field = value
}
private var arguments: MutableList<Any?> = mutableListOf()
private val authenticator: RtmpAuthenticator by lazy {
RtmpAuthenticator(this)
}

init {
addEventListener(Event.RTMP_STATUS, authenticator)
addEventListener(Event.RTMP_STATUS, EventListener(this))
}

Expand Down Expand Up @@ -316,7 +320,6 @@ open class RtmpConnection : EventDispatcher(null) {
buffer.position(rollback)
throw e
}

if (buffer.hasRemaining()) {
listen(buffer)
}
Expand Down
33 changes: 16 additions & 17 deletions haishinkit/src/main/java/com/haishinkit/rtmp/RtmpSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 -> {}
Expand All @@ -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
}
}

0 comments on commit 74e6242

Please sign in to comment.