Skip to content

Commit

Permalink
add only audio mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro committed Apr 17, 2021
1 parent f5f2ae1 commit 6c244f1
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 13 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 @@ 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'
}
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions rtspserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 107
versionName "1.0.7"
versionCode 108
versionName "1.0.8"
}

buildTypes {
Expand Down
18 changes: 16 additions & 2 deletions rtspserver/src/main/java/com/pedro/rtspserver/RtspServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,7 @@ class RtspServer(private val connectCheckerRtsp: ConnectCheckerRtsp,
var sampleRate = 32000
var isStereo = true
private val clients = mutableListOf<ServerClient>()
private var isOnlyAudio = false
private var thread: Thread? = null

fun startServer() {
Expand All @@ -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)
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -145,6 +158,7 @@ class RtspServer(private val connectCheckerRtsp: ConnectCheckerRtsp,

override fun onDisconnected(client: ServerClient) {
synchronized(clients) {
client.stopClient()
clients.remove(client)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
15 changes: 10 additions & 5 deletions rtspserver/src/main/java/com/pedro/rtspserver/ServerClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down

0 comments on commit 6c244f1

Please sign in to comment.