Skip to content

Commit 78d0a7c

Browse files
authored
Merge pull request #1917 from pedroSG94/feature/customamf0
Feature/customamf0
2 parents faf64a6 + fc1cbbe commit 78d0a7c

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

library/src/main/java/com/pedro/library/util/streamclient/RtmpStreamClient.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import javax.net.ssl.TrustManager
2424
* Created by pedro on 12/10/23.
2525
*/
2626
class RtmpStreamClient(
27-
private val rtmpClient: RtmpClient,
27+
private val rtmpClient: RtmpClient,
2828
private val streamClientListener: StreamClientListener?
2929
): StreamBaseClient() {
3030

@@ -174,4 +174,8 @@ class RtmpStreamClient(
174174
override fun setSocketType(type: SocketType) {
175175
rtmpClient.socketType = type
176176
}
177+
178+
fun setCustomAmfObject(amfObject: Map<String, Any>) {
179+
rtmpClient.setCustomAmfObject(amfObject)
180+
}
177181
}

rtmp/src/main/java/com/pedro/rtmp/amf/v0/AmfObject.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ open class AmfObject(private val properties: LinkedHashMap<AmfString, AmfData> =
8888
bodySize += data.getSize() + 1
8989
}
9090

91+
open fun setProperty(name: String, data: Any) {
92+
val newValue: AmfData = when (data) {
93+
is String -> AmfString(data)
94+
is Int -> AmfNumber(data.toDouble())
95+
is Long -> AmfNumber(data.toDouble())
96+
is Double -> AmfNumber(data)
97+
is Float -> AmfNumber(data.toDouble())
98+
is Boolean -> AmfBoolean(data)
99+
is AmfData -> data
100+
else -> throw IllegalArgumentException(
101+
"Unsupported value type: ${data::class.java.name}"
102+
)
103+
}
104+
105+
val existingEntry = properties.entries.find { it.key.value == name }
106+
107+
if (existingEntry != null) {
108+
properties[existingEntry.key] = newValue
109+
bodySize += newValue.getSize() - existingEntry.value.getSize()
110+
} else {
111+
val key = AmfString(name)
112+
properties[key] = newValue
113+
bodySize += key.getSize() + newValue.getSize() + 1
114+
}
115+
}
116+
91117
fun getProperties() = properties
92118

93119
@Throws(IOException::class)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ abstract class CommandsManager {
6161
var readChunkSize = RtmpConfig.DEFAULT_CHUNK_SIZE
6262
var audioDisabled = false
6363
var videoDisabled = false
64+
var customAmfObject: Map<String, Any> = emptyMap()
6465
private var bytesRead = 0
6566
private var acknowledgementSequence = 0
6667

rtmp/src/main/java/com/pedro/rtmp/rtmp/CommandsManagerAmf0.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class CommandsManagerAmf0: CommandsManager() {
5656
}
5757
}
5858
connectInfo.setProperty("objectEncoding", 0.0)
59+
60+
// Inject other custom AMF fields as-is
61+
customAmfObject.forEach { (key, value) ->
62+
connectInfo.setProperty(key, value)
63+
}
5964
connect.addData(connectInfo)
6065

6166
connect.writeHeader(socket)

rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
175175
}
176176
}
177177

178+
fun setCustomAmfObject(amfObject: Map<String, Any>) {
179+
commandsManager.customAmfObject = amfObject
180+
}
181+
178182
fun setAuthorization(user: String?, password: String?) {
179183
commandsManager.setAuth(user, password)
180184
}

0 commit comments

Comments
 (0)