diff --git a/README.md b/README.md index 62c550a..78ec6db 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ allprojects { } } dependencies { - implementation 'com.github.pedroSG94.RTSP-Server:1.0.7' + implementation 'com.github.pedroSG94.RTSP-Server:1.0.8' implementation 'com.github.pedroSG94.rtmp-rtsp-stream-client-java:rtplibrary:2.0.2' } diff --git a/app/build.gradle b/app/build.gradle index 5a4d66a..e3b59e6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.pedro.sample" minSdkVersion 16 targetSdkVersion 30 - versionCode 107 - versionName "1.0.7" + versionCode 108 + versionName "1.0.8" } buildTypes { release { diff --git a/rtspserver/build.gradle b/rtspserver/build.gradle index fd2386a..555d75d 100644 --- a/rtspserver/build.gradle +++ b/rtspserver/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 30 - versionCode 107 - versionName "1.0.7" + versionCode 108 + versionName "1.0.8" } buildTypes { diff --git a/rtspserver/src/main/java/com/pedro/rtspserver/RtspServer.kt b/rtspserver/src/main/java/com/pedro/rtspserver/RtspServer.kt index 555e740..222a2b4 100644 --- a/rtspserver/src/main/java/com/pedro/rtspserver/RtspServer.kt +++ b/rtspserver/src/main/java/com/pedro/rtspserver/RtspServer.kt @@ -3,6 +3,7 @@ package com.pedro.rtspserver import android.media.MediaCodec import android.util.Log import com.pedro.rtsp.utils.ConnectCheckerRtsp +import com.pedro.rtsp.utils.RtpConstants import java.io.* import java.net.* import java.nio.ByteBuffer @@ -28,6 +29,7 @@ class RtspServer(private val connectCheckerRtsp: ConnectCheckerRtsp, var sampleRate = 32000 var isStereo = true private val clients = mutableListOf() + private var isOnlyAudio = false private var thread: Thread? = null fun startServer() { @@ -38,8 +40,8 @@ class RtspServer(private val connectCheckerRtsp: ConnectCheckerRtsp, Log.i(TAG, "Server started $serverIp:$port") try { val client = - ServerClient(server!!.accept(), serverIp, port, connectCheckerRtsp, sps!!, pps!!, vps, sampleRate, - isStereo, this) + ServerClient(server!!.accept(), serverIp, port, connectCheckerRtsp, sps, pps, vps, sampleRate, + isStereo, isOnlyAudio, this) client.start() synchronized(clients) { clients.add(client) @@ -76,6 +78,17 @@ class RtspServer(private val connectCheckerRtsp: ConnectCheckerRtsp, } } + fun setOnlyAudio(onlyAudio: Boolean) { + if (onlyAudio) { + RtpConstants.trackAudio = 0 + RtpConstants.trackVideo = 1 + } else { + RtpConstants.trackVideo = 0 + RtpConstants.trackAudio = 1 + } + this.isOnlyAudio = onlyAudio + } + fun setLogs(enable: Boolean) { synchronized(clients) { clients.forEach { it.rtspSender.setLogs(enable) } @@ -145,6 +158,7 @@ class RtspServer(private val connectCheckerRtsp: ConnectCheckerRtsp, override fun onDisconnected(client: ServerClient) { synchronized(clients) { + client.stopClient() clients.remove(client) } } diff --git a/rtspserver/src/main/java/com/pedro/rtspserver/RtspServerOnlyAudio.kt b/rtspserver/src/main/java/com/pedro/rtspserver/RtspServerOnlyAudio.kt new file mode 100644 index 0000000..9ae5f52 --- /dev/null +++ b/rtspserver/src/main/java/com/pedro/rtspserver/RtspServerOnlyAudio.kt @@ -0,0 +1,89 @@ +package com.pedro.rtspserver + +import android.media.MediaCodec +import com.pedro.rtplibrary.base.OnlyAudioBase +import com.pedro.rtsp.utils.ConnectCheckerRtsp +import java.nio.ByteBuffer + +/** + * Created by pedro on 17/04/21. + */ +class RtspServerOnlyAudio(connectCheckerRtsp: ConnectCheckerRtsp, port: Int) : OnlyAudioBase() { + + private val rtspServer = RtspServer(connectCheckerRtsp, port) + + init { + rtspServer.setOnlyAudio(true) + } + + fun getNumClients(): Int = rtspServer.getNumClients() + + fun getEndPointConnection(): String = "rtsp://${rtspServer.serverIp}:${rtspServer.port}/" + + override fun setAuthorization(user: String, password: String) { //not developed + } + + fun startStream() { + super.startStream("") + rtspServer.startServer() + } + + override fun prepareAudioRtp(isStereo: Boolean, sampleRate: Int) { + rtspServer.isStereo = isStereo + rtspServer.sampleRate = sampleRate + } + + override fun startStreamRtp(url: String) { //unused + } + + override fun stopStreamRtp() { + rtspServer.stopServer() + } + + override fun getAacDataRtp(aacBuffer: ByteBuffer, info: MediaCodec.BufferInfo) { + rtspServer.sendAudio(aacBuffer, info) + } + + override fun setLogs(enable: Boolean) { + rtspServer.setLogs(enable) + } + + /** + * Unused functions + */ + @Throws(RuntimeException::class) + override fun resizeCache(newSize: Int) { + } + + override fun shouldRetry(reason: String?): Boolean = false + + override fun reConnect(delay: Long) { + } + + override fun hasCongestion(): Boolean = rtspServer.hasCongestion() + + override fun setReTries(reTries: Int) { + } + + override fun getCacheSize(): Int = 0 + + override fun getSentAudioFrames(): Long = 0 + + override fun getSentVideoFrames(): Long = 0 + + override fun getDroppedAudioFrames(): Long = 0 + + override fun getDroppedVideoFrames(): Long = 0 + + override fun resetSentAudioFrames() { + } + + override fun resetSentVideoFrames() { + } + + override fun resetDroppedAudioFrames() { + } + + override fun resetDroppedVideoFrames() { + } +} \ No newline at end of file diff --git a/rtspserver/src/main/java/com/pedro/rtspserver/ServerClient.kt b/rtspserver/src/main/java/com/pedro/rtspserver/ServerClient.kt index 9d1c4a2..08d43f7 100644 --- a/rtspserver/src/main/java/com/pedro/rtspserver/ServerClient.kt +++ b/rtspserver/src/main/java/com/pedro/rtspserver/ServerClient.kt @@ -14,9 +14,9 @@ import java.net.SocketException import java.nio.ByteBuffer class ServerClient(private val socket: Socket, serverIp: String, serverPort: Int, - connectCheckerRtsp: ConnectCheckerRtsp, sps: ByteBuffer, - pps: ByteBuffer, vps: ByteBuffer?, private val sampleRate: Int, - isStereo: Boolean, private val listener: ClientListener) : Thread() { + connectCheckerRtsp: ConnectCheckerRtsp, sps: ByteBuffer?, + pps: ByteBuffer?, vps: ByteBuffer?, private val sampleRate: Int, + isStereo: Boolean, isOnlyAudio: Boolean, private val listener: ClientListener) : Thread() { private val TAG = "Client" private var cSeq = 0 @@ -32,6 +32,7 @@ class ServerClient(private val socket: Socket, serverIp: String, serverPort: Int var canSend = false init { + commandsManager.isOnlyAudio = isOnlyAudio commandsManager.isStereo = isStereo commandsManager.sampleRate = sampleRate commandsManager.setVideoInfo(sps, pps, vps) @@ -58,11 +59,15 @@ class ServerClient(private val socket: Socket, serverIp: String, serverPort: Int Log.i(TAG, "Protocol ${commandsManager.protocol}") rtspSender.setSocketsInfo(commandsManager.protocol, commandsManager.videoClientPorts, commandsManager.audioClientPorts) - rtspSender.setVideoInfo(commandsManager.sps!!, commandsManager.pps!!, commandsManager.vps) + if (!commandsManager.isOnlyAudio) { + rtspSender.setVideoInfo(commandsManager.sps!!, commandsManager.pps!!, commandsManager.vps) + } rtspSender.setAudioInfo(sampleRate) rtspSender.setDataStream(socket.getOutputStream(), commandsManager.clientIp!!) if (commandsManager.protocol == Protocol.UDP) { - rtspSender.setVideoPorts(commandsManager.videoPorts[0], commandsManager.videoPorts[1]) + if (!commandsManager.isOnlyAudio) { + rtspSender.setVideoPorts(commandsManager.videoPorts[0], commandsManager.videoPorts[1]) + } rtspSender.setAudioPorts(commandsManager.audioPorts[0], commandsManager.audioPorts[1]) } rtspSender.start() diff --git a/rtspserver/src/main/java/com/pedro/rtspserver/ServerCommandManager.kt b/rtspserver/src/main/java/com/pedro/rtspserver/ServerCommandManager.kt index 076f337..726dc3d 100644 --- a/rtspserver/src/main/java/com/pedro/rtspserver/ServerCommandManager.kt +++ b/rtspserver/src/main/java/com/pedro/rtspserver/ServerCommandManager.kt @@ -135,7 +135,11 @@ class ServerCommandManager(private val serverIp: String, private val serverPort: private fun createBody(): String { val audioBody = SdpBody.createAacBody(RtpConstants.trackAudio, sampleRate, isStereo) - val videoBody = if (vps == null) SdpBody.createH264Body(RtpConstants.trackVideo, encodeToString(sps!!)!!, encodeToString(pps!!)!!) else SdpBody.createH265Body(RtpConstants.trackVideo, encodeToString(sps!!)!!, encodeToString(pps!!)!!, encodeToString(vps!!)!!) + var videoBody = "" + if (!isOnlyAudio) { + videoBody = if (vps == null) SdpBody.createH264Body(RtpConstants.trackVideo, encodeToString(sps!!)!!, encodeToString(pps!!)!!) + else SdpBody.createH265Body(RtpConstants.trackVideo, encodeToString(sps!!)!!, encodeToString(pps!!)!!, encodeToString(vps!!)!!) + } return "v=0\r\no=- 0 0 IN IP4 $serverIp\r\ns=Unnamed\r\ni=N/A\r\nc=IN IP4 $clientIp\r\nt=0 0\r\na=recvonly\r\n$videoBody$audioBody\r\n" }