-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): add an API to parse ffmpeg like url
- Loading branch information
1 parent
c3dab76
commit 574806b
Showing
5 changed files
with
511 additions
and
4 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
srtdroid/src/androidTest/java/io/github/thibaultbee/srtdroid/models/SrtUrlTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
srtdroid/src/main/java/io/github/thibaultbee/srtdroid/extensions/IntExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
96 changes: 96 additions & 0 deletions
96
srtdroid/src/main/java/io/github/thibaultbee/srtdroid/extensions/SocketExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.