Skip to content

Commit

Permalink
fix getAppName parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 26, 2024
1 parent e7d23da commit dbd7a9e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 98 deletions.
18 changes: 6 additions & 12 deletions common/src/main/java/com/pedro/common/UrlParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,16 @@ class UrlParser private constructor(
}

fun getAppName(): String {
val fullPath = getFullPath()
val indexes = fullPath.getIndexes('/')
val app = path.ifEmpty { query ?: "" }
val indexes = app.getIndexes('/')
return when (indexes.size) {
0 -> fullPath
1 -> fullPath.substring(0, indexes[0])
else -> {
if (getAllQueries().isEmpty()) {
fullPath.substring(0, indexes[1])
} else {
fullPath.substring(0, indexes[0])
}
}
0 -> app
1 -> app.substring(0, indexes[0])
else -> app.substring(0, indexes[1])
}
}

fun getStreamName(): String = getFullPath().removePrefix(getAppName()).removePrefix("/")
fun getStreamName(): String = getFullPath().removePrefix(getAppName()).removePrefix("/").removePrefix("?")

fun getTcUrl(): String {
val port = if (port != null) ":$port" else ""
Expand Down
127 changes: 41 additions & 86 deletions common/src/test/java/com/pedro/common/UrlParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.pedro.common
import junit.framework.TestCase.assertEquals
import org.junit.Test
import java.net.URISyntaxException
import java.util.regex.Pattern

/**
* Created by pedro on 2/9/24.
Expand All @@ -18,9 +17,9 @@ class UrlParserTest {
assertEquals("rtmp", urlParser.scheme)
assertEquals("localhost", urlParser.host)
assertEquals(1935, urlParser.port)
assertEquals("live?test", urlParser.getAppName())
assertEquals("fake", urlParser.getStreamName())
assertEquals("rtmp://localhost:1935/live?test", urlParser.getTcUrl())
assertEquals("live", urlParser.getAppName())
assertEquals("test/fake", urlParser.getStreamName())
assertEquals("rtmp://localhost:1935/live", urlParser.getTcUrl())

val url0 = "rtmp://192.168.238.182:1935/live/100044?userId=100044&roomTitle=123123&roomCover=http://192.168.238.182/xxxx.png"
val urlParser0 = UrlParser.parse(url0, arrayOf("rtmp"))
Expand All @@ -36,9 +35,9 @@ class UrlParserTest {
assertEquals("rtmp", urlParser1.scheme)
assertEquals("localhost", urlParser1.host)
assertEquals(1935, urlParser1.port)
assertEquals("live?test", urlParser1.getAppName())
assertEquals("fake", urlParser1.getStreamName())
assertEquals("rtmp://localhost:1935/live?test", urlParser1.getTcUrl())
assertEquals("live", urlParser1.getAppName())
assertEquals("test/fake", urlParser1.getStreamName())
assertEquals("rtmp://localhost:1935/live", urlParser1.getTcUrl())
assertEquals("user", urlParser1.getAuthUser())
assertEquals("pass", urlParser1.getAuthPassword())

Expand Down Expand Up @@ -86,6 +85,24 @@ class UrlParserTest {
assertEquals("live/test", urlParser6.getAppName())
assertEquals("fake", urlParser6.getStreamName())
assertEquals("rtmp://192.168.0.1:1234/live/test", urlParser6.getTcUrl())

val url7 = "rtmp://192.168.0.1:1935/v2/pub/streamName?token=streamToken"
val urlParser7 = UrlParser.parse(url7, arrayOf("rtmp"))
assertEquals("rtmp", urlParser7.scheme)
assertEquals("192.168.0.1", urlParser7.host)
assertEquals(1935, urlParser7.port)
assertEquals("v2/pub", urlParser7.getAppName())
assertEquals("streamName?token=streamToken", urlParser7.getStreamName())
assertEquals("rtmp://192.168.0.1:1935/v2/pub", urlParser7.getTcUrl())

val url8 = "rtmp://192.168.0.1:1935/v2/pub/streamName?token"
val urlParser8 = UrlParser.parse(url8, arrayOf("rtmp"))
assertEquals("rtmp", urlParser8.scheme)
assertEquals("192.168.0.1", urlParser8.host)
assertEquals(1935, urlParser8.port)
assertEquals("v2/pub", urlParser8.getAppName())
assertEquals("streamName?token", urlParser8.getStreamName())
assertEquals("rtmp://192.168.0.1:1935/v2/pub", urlParser8.getTcUrl())
} catch (e: URISyntaxException) {
assert(false)
}
Expand Down Expand Up @@ -203,95 +220,33 @@ class UrlParserTest {

@Test
fun testUdpUrls() {
try {
val url = "udp://localhost:1935"
val urlParser = UrlParser.parse(url, arrayOf("udp"))
assertEquals("udp", urlParser.scheme)
assertEquals("localhost", urlParser.host)
assertEquals(1935, urlParser.port)

val url0 = "udp://localhost:1935/"
val urlParser0 = UrlParser.parse(url0, arrayOf("udp"))
assertEquals("udp", urlParser0.scheme)
assertEquals("localhost", urlParser0.host)
assertEquals(1935, urlParser0.port)
} catch (e: URISyntaxException) {
assert(false)
}
val url = "udp://localhost:1935"
val urlParser = UrlParser.parse(url, arrayOf("udp"))
assertEquals("udp", urlParser.scheme)
assertEquals("localhost", urlParser.host)
assertEquals(1935, urlParser.port)

val url0 = "udp://localhost:1935/"
val urlParser0 = UrlParser.parse(url0, arrayOf("udp"))
assertEquals("udp", urlParser0.scheme)
assertEquals("localhost", urlParser0.host)
assertEquals(1935, urlParser0.port)
}

@Test
fun testUrl2() {
try {
val urlParser = UrlParser.parse(
"srt://push.domain.com:1105?streamid=#!::h=push.domain.com,r=/live/stream,m=publish&live=asd",
arrayOf("srt")
)
println(urlParser.toString())
println(urlParser.getQuery("streamid"))
} catch (e: IllegalArgumentException) {

}
val urlParser = UrlParser.parse(
"srt://push.domain.com:1105?streamid=#!::h=push.domain.com,r=/live/stream,m=publish&live=asd",
arrayOf("srt")
)
assertEquals("#!::h=push.domain.com,r=/live/stream,m=publish", urlParser.getQuery("streamid"))
}

@Test
fun testUrl3() {
try {
val urlParser = UrlParser.parse(
UrlParser.parse(
"rtmp://localhost?live=adasdasd",
arrayOf("rtmp")
)
println(urlParser.toString())
} catch (e: IllegalArgumentException) {

}
}

private fun oldParser(url: String) {
val urlPattern: Pattern = Pattern.compile("^rtmpt?s?://([^/:]+)(?::(\\d+))*/([^/]+)/?([^*]*)$")
val tunneled: Boolean
val tlsEnabled: Boolean
val rtmpMatcher = urlPattern.matcher(url)
if (!rtmpMatcher.matches()) {
assert(false)
}
val schema = rtmpMatcher.group(0) ?: ""
tunneled = schema.startsWith("rtmpt")
tlsEnabled = schema.startsWith("rtmps") || schema.startsWith("rtmpts")

val host = rtmpMatcher.group(1) ?: ""
val portStr = rtmpMatcher.group(2)
val defaultPort = if (tlsEnabled) 443 else if (tunneled) 80 else 1935
val port = portStr?.toInt() ?: defaultPort
val appName = getAppName(rtmpMatcher.group(3) ?: "", rtmpMatcher.group(4) ?: "")
val streamName = getStreamName(rtmpMatcher.group(4) ?: "")
val tcUrl = getTcUrl((rtmpMatcher.group(0)
?: "").substring(0, (rtmpMatcher.group(0)
?: "").length - streamName.length))
println("OldParser(scheme='$schema', host='$host', port=$port, appName='$appName', streamName='$streamName', tcUrl='$tcUrl'")
}

private fun getAppName(app: String, name: String): String {
return if (!name.contains("/")) {
app
} else {
app + "/" + name.substring(0, name.indexOf("/"))
}
}

private fun getStreamName(name: String): String {
return if (!name.contains("/")) {
name
} else {
name.substring(name.indexOf("/") + 1)
}
}

private fun getTcUrl(url: String): String {
return if (url.endsWith("/")) {
url.substring(0, url.length - 1)
} else {
url
}
}
}

0 comments on commit dbd7a9e

Please sign in to comment.