Skip to content

Commit

Permalink
feat(lib): add an API to parse ffmpeg like url
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Jun 25, 2024
1 parent c3dab76 commit 574806b
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.thibaultbee.srtdroid.models

import io.github.thibaultbee.srtdroid.enums.SockOpt
import io.github.thibaultbee.srtdroid.enums.Transtype
import org.junit.Assert.assertEquals
import org.junit.Test

class SrtUrlTest {
@Test
fun srtUrlWithBoolean() {
var srtUrl = SrtUrl("srt://host:9000?tsbpd=0")
assertEquals(false, srtUrl.enableTimestampBasedPacketDelivery)
srtUrl = SrtUrl("srt://host:9000?tsbpd=1")
assertEquals(true, srtUrl.enableTimestampBasedPacketDelivery)
}

@Test
fun srtUrlWithNumber() {
val srtUrl = SrtUrl("srt://host:9000?snddropdelay=2000")
assertEquals(2, srtUrl.senderDropDelayInMs)
}

@Test
fun srtUrlWithString() {
val srtUrl = SrtUrl("srt://host:9000?streamid=abcde")
assertEquals("abcde", srtUrl.streamId)
}

@Test
fun srtUrlWithTranstype() {
var srtUrl = SrtUrl("srt://host:9000?transtype=live")
assertEquals(Transtype.LIVE, srtUrl.transtype)
srtUrl = SrtUrl("srt://host:9000?transtype=file")
assertEquals(Transtype.FILE, srtUrl.transtype)
}

@Test
fun srtUrlWithMultipleParameters() {
val srtUrl =
SrtUrl("srt://host:9000?transtype=live&srt_streamid=abcde&rcvlatency=2000&passphrase=1234")
assertEquals(Transtype.LIVE, srtUrl.transtype)
assertEquals("abcde", srtUrl.streamId)
assertEquals(2, srtUrl.receiverLatencyInMs)
assertEquals("1234", srtUrl.passphrase)
}

@Test
fun srtUrlApplyToSocket() {
val srtUrl = SrtUrl(hostname = "srt://host", port = 900, connectTimeoutInMs = 1234)
val socket = Socket()
srtUrl.preApplyTo(socket)
assertEquals(1234, socket.getSockFlag(SockOpt.CONNTIMEO))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ package io.github.thibaultbee.srtdroid.enums
*
* **See Also:** [srt_transtype](https://github.com/Haivision/srt/blob/master/docs/API/API-socket-options.md#srt_transtype)
*/
enum class Transtype {
enum class Transtype(val displayName: String) {
/**
* Live mode.
*/
LIVE,
LIVE("live"),

/**
* File mode.
*/
FILE,
FILE("file"),

/**
* Invalid mode.
*/
INVALID
INVALID("invalid");

companion object {
fun entryOf(displayName: String) = entries.firstOrNull { it.displayName == displayName } ?: INVALID
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.thibaultbee.srtdroid.extensions

fun Int.toBoolean(): Boolean {
return this != 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.github.thibaultbee.srtdroid.extensions

import io.github.thibaultbee.srtdroid.models.Socket
import io.github.thibaultbee.srtdroid.models.SrtUrl
import java.net.BindException
import java.net.ConnectException
import java.net.SocketException

/**
* Binds the socket to a local address.
*
* **See Also:** [srt_bind](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_bind)
*
* @param url the URL to bind to in FFmpeg format srt://hostname:port[?options]
*
* @throws BindException if bind has failed
*/
fun Socket.bind(url: String) = bind(SrtUrl(url))

/**
* Binds the socket to a local address.
*
* **See Also:** [srt_bind](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_bind)
*
* @param srtUrl the URL to bind to in FFmpeg format srt://hostname:port[?options]
*
* @throws BindException if bind has failed
*/
fun Socket.bind(srtUrl: SrtUrl) {
if (srtUrl.mode != null) {
require(srtUrl.mode != SrtUrl.Mode.CALLER) { "Bind is only for `listener` or `rendezvous` mode but ${srtUrl.mode.value}" }
}

srtUrl.preApplyTo(this)
srtUrl.preBindApplyTo(this)
bind(srtUrl.hostname, srtUrl.port)
srtUrl.postApplyTo(this)
}

/**
* Connects a socket to an URL.
*
* **See Also:** [srt_connect](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_connect)
*
* @param url the URL to connect to in FFmpeg format srt://hostname:port[?options]
* @throws ConnectException if connection has failed
*/
fun Socket.connect(url: String) = connect(SrtUrl(url))

/**
* Connects a socket to an URL.
*
* **See Also:** [srt_connect](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_connect)
*
* @param srtUrl the URL to connect to in FFmpeg format srt://hostname:port[?options]
* @throws ConnectException if connection has failed
*/
fun Socket.connect(srtUrl: SrtUrl) {
if (srtUrl.mode != null) {
require(srtUrl.mode != SrtUrl.Mode.LISTENER) { "Connect is only for `caller` or `rendezvous` mode but ${srtUrl.mode.value}" }
}

srtUrl.preApplyTo(this)
connect(srtUrl.hostname, srtUrl.port)
srtUrl.postApplyTo(this)
}

/**
* Performs a rendezvous connection.
*
* **See Also:** [srt_rendezvous](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_rendezvous)
*
* @param url the URL to rendezvous to in FFmpeg format srt://hostname:port[?options]
* @throws SocketException if rendezvous connection has failed
*/
fun Socket.rendezVous(url: String) = rendezVous(SrtUrl(url))

/**
* Performs a rendezvous connection.
*
* **See Also:** [srt_rendezvous](https://github.com/Haivision/srt/blob/master/docs/API/API-functions.md#srt_rendezvous)
*
* @param srtUrl the URL to rendezvous to in FFmpeg format srt://hostname:port[?options]
* @throws SocketException if rendezvous connection has failed
*/
fun Socket.rendezVous(
srtUrl: SrtUrl
) {
if (srtUrl.mode != null) {
require(srtUrl.mode == SrtUrl.Mode.RENDEZ_VOUS) { "Connect is only for `caller` or `rendezvous` mode but ${srtUrl.mode.value}" }
}

srtUrl.preApplyTo(this)
rendezVous(srtUrl.hostname, srtUrl.hostname, srtUrl.port)
srtUrl.postApplyTo(this)
}
Loading

0 comments on commit 574806b

Please sign in to comment.