Skip to content

Commit

Permalink
support rtsp and srt in background example
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 1, 2023
1 parent 8cfe688 commit 19425a3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ class BackgroundActivity : AppCompatActivity(), SurfaceHolder.Callback {

binding.bStartStop.setOnClickListener {
if (service?.isStreaming() != true) {
if (service?.prepare() == true) {
service?.startStream(binding.etRtpUrl.text.toString())
service?.stopPreview()
val endpoint = binding.etRtpUrl.text.toString()
if (service?.prepare(endpoint) == true) {
startPreview()
service?.startStream(endpoint)
binding.bStartStop.setText(R.string.stop_button)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package com.pedro.streamer.backgroundexample

import com.pedro.rtmp.utils.ConnectCheckerRtmp
import com.pedro.rtsp.utils.ConnectCheckerRtsp
import com.pedro.srt.utils.ConnectCheckerSrt

/**
* (Only working in kotlin)
* Mix both connect interfaces to support RTMP and RTSP in service with same code.
*/
interface ConnectCheckerRtp: ConnectCheckerRtmp, ConnectCheckerRtsp {
interface ConnectCheckerRtp: ConnectCheckerRtmp, ConnectCheckerRtsp, ConnectCheckerSrt {

/**
* Commons
Expand Down Expand Up @@ -103,4 +104,35 @@ interface ConnectCheckerRtp: ConnectCheckerRtmp, ConnectCheckerRtsp {
override fun onAuthSuccessRtsp() {
onAuthSuccessRtp()
}

/**
* SRT
*/
override fun onConnectionStartedSrt(srtUrl: String) {
onConnectionStartedRtp(srtUrl)
}

override fun onConnectionSuccessSrt() {
onConnectionSuccessRtp()
}

override fun onConnectionFailedSrt(reason: String) {
onConnectionFailedRtp(reason)
}

override fun onNewBitrateSrt(bitrate: Long) {
onNewBitrateRtp(bitrate)
}

override fun onDisconnectSrt() {
onDisconnectRtp()
}

override fun onAuthErrorSrt() {
onAuthErrorRtp()
}

override fun onAuthSuccessSrt() {
onAuthSuccessRtp()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import androidx.core.app.NotificationCompat
import androidx.lifecycle.MutableLiveData
import com.pedro.library.base.Camera2Base
import com.pedro.library.rtmp.RtmpCamera2
import com.pedro.library.rtsp.RtspCamera2
import com.pedro.library.srt.SrtCamera2
import com.pedro.library.view.OpenGlView
import com.pedro.streamer.R

Expand Down Expand Up @@ -101,7 +103,14 @@ class RtpService : Service() {
notificationManager?.notify(notifyId, notification)
}

fun prepare(): Boolean {
fun prepare(endpoint: String): Boolean {
if (endpoint.startsWith("rtmp")) {
camera2Base = RtmpCamera2(this, true, connectCheckerRtp)
} else if (endpoint.startsWith("rtsp")){
camera2Base = RtspCamera2(this, true, connectCheckerRtp)
} else {
camera2Base = SrtCamera2(this, true, connectCheckerRtp)
}
return camera2Base?.prepareVideo() ?: false && camera2Base?.prepareAudio() ?: false
}

Expand Down

0 comments on commit 19425a3

Please sign in to comment.