Skip to content

Commit 74599b5

Browse files
authored
Merge pull request #2041 from pedroSG94/fixing-srt-handshake
fix srt handshake flags and support add passphrase in url
2 parents 5d8a5f7 + b4b8d69 commit 74599b5

5 files changed

Lines changed: 19 additions & 7 deletions

File tree

srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class CommandsManager {
7373
return encryptor?.type ?: EncryptionType.NONE
7474
}
7575

76+
fun encryptionEnabled() = encryptor != null
77+
7678
fun loadStartTs() {
7779
startTS = TimeUtils.getCurrentTimeMicro()
7880
}

srt/src/main/java/com/pedro/srt/srt/SrtClient.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class SrtClient(private val connectChecker: ConnectChecker) {
140140
*/
141141
fun setPassphrase(passphrase: String, type: EncryptionType) {
142142
if (!isStreaming) {
143-
if (passphrase.length < 10 || passphrase.length > 79) {
143+
if (passphrase.length !in 10..79) {
144144
throw IllegalArgumentException("passphrase must between 10 and 79 length")
145145
}
146146
commandsManager.setPassphrase(passphrase, type)
@@ -213,6 +213,15 @@ class SrtClient(private val connectChecker: ConnectChecker) {
213213
val port = urlParser.port ?: 8888
214214
val path = urlParser.getQuery("streamid") ?: urlParser.getFullPath()
215215
latency = urlParser.getQuery("latency")?.toIntOrNull() ?: latency
216+
val passphrase = urlParser.getQuery("passphrase") ?: ""
217+
if (passphrase.isNotEmpty() && passphrase.length in 10..79) {
218+
val encryptionType = when (urlParser.getQuery("pbkeylen")?.toIntOrNull()) {
219+
192 -> EncryptionType.AES192
220+
256 -> EncryptionType.AES256
221+
else -> EncryptionType.AES128
222+
}
223+
commandsManager.setPassphrase(passphrase, encryptionType)
224+
}
216225
if (path.isEmpty()) {
217226
isStreaming = false
218227
onMainThread {
@@ -232,7 +241,7 @@ class SrtClient(private val connectChecker: ConnectChecker) {
232241

233242
commandsManager.writeHandshake(socket, response.copy(
234243
encryption = commandsManager.getEncryptType(),
235-
extensionField = ExtensionField.calculateValue(response.extensionField),
244+
extensionField = ExtensionField.calculateValue(response.extensionField, commandsManager.encryptionEnabled()),
236245
handshakeType = HandshakeType.CONCLUSION,
237246
handshakeExtension = HandshakeExtension(
238247
flags = ExtensionContentFlag.TSBPDSND.value or ExtensionContentFlag.TSBPDRCV.value or

srt/src/main/java/com/pedro/srt/srt/packets/control/handshake/ExtensionField.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ enum class ExtensionField(val value: Int) {
2727
companion object {
2828
infix fun from(value: Int): ExtensionField = entries.firstOrNull { it.value == value } ?: throw IOException("unknown extension field: $value")
2929

30-
fun calculateValue(value: Int): Int {
30+
fun calculateValue(value: Int, encrypted: Boolean): Int {
3131
val hsV5enabled = value and HS_V5_FLAG.value != 0
32-
val extensionField = HS_REQ.value or CONFIG.value
32+
val extensionField = if (encrypted) HS_REQ.value or KM_REQ.value or CONFIG.value else HS_REQ.value or CONFIG.value
3333
return if (hsV5enabled) HS_V5_FLAG.value or KM_REQ.value or extensionField else extensionField
3434
}
3535
}

srt/src/main/java/com/pedro/srt/utils/EncryptionUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class EncryptionUtil(val type: EncryptionType, passphrase: String) {
4040
private val cipherType = CipherType.CTR
4141
private val salt: ByteArray
4242
private val keyLength: Int = when (type) {
43-
EncryptionType.NONE -> 0
43+
EncryptionType.NONE,
4444
EncryptionType.AES128 -> 16
4545
EncryptionType.AES192 -> 24
4646
EncryptionType.AES256 -> 32

srt/src/test/java/com/pedro/srt/srt/control/HandshakeTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class HandshakeTest {
3434

3535
@Test
3636
fun `test extension field calculation`() {
37-
assertEquals(131079, ExtensionField.calculateValue(150039))
38-
assertEquals(5, ExtensionField.calculateValue(18967))
37+
assertEquals(131079, ExtensionField.calculateValue(150039, false))
38+
assertEquals(5, ExtensionField.calculateValue(18967, false))
39+
assertEquals(7, ExtensionField.calculateValue(18967, true))
3940
}
4041

4142
@Test

0 commit comments

Comments
 (0)