Skip to content

Commit c87783c

Browse files
committed
chore(sample): rtmpclient: improve error handle
1 parent 96ebac5 commit c87783c

File tree

1 file changed

+15
-11
lines changed
  • samples/rtmpclient-cli/src/main/kotlin

1 file changed

+15
-11
lines changed

samples/rtmpclient-cli/src/main/kotlin/Main.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import io.github.thibaultbee.krtmp.flv.decodeAllTagOnly
1111
import io.github.thibaultbee.krtmp.logger.IKrtmpLogger
1212
import io.github.thibaultbee.krtmp.logger.KrtmpLogger
1313
import io.github.thibaultbee.krtmp.rtmp.RtmpConnectionBuilder
14+
import io.github.thibaultbee.krtmp.rtmp.client.RtmpClientSettings
1415
import io.github.thibaultbee.krtmp.rtmp.connect
1516
import io.ktor.network.selector.SelectorManager
17+
import io.ktor.network.sockets.awaitClosed
1618
import kotlinx.coroutines.Dispatchers
1719
import kotlinx.io.files.Path
1820

@@ -44,14 +46,15 @@ class RTMPClientCli : SuspendingCliktCommand() {
4446
val client =
4547
try {
4648
// Connect the RTMP client
47-
builder.connect(rtmpUrlPath, {
48-
connectInfo = {
49-
// Configure the connect object here if needed
50-
// videoCodecs = listOf(VideoMediaType.AVC)
51-
}
52-
})
49+
builder.connect(
50+
rtmpUrlPath, RtmpClientSettings(
51+
connectInfo = {
52+
// Configure the connect object here if needed
53+
// videoCodecs = listOf(VideoMediaType.AVC)
54+
}
55+
))
5356
} catch (t: Throwable) {
54-
echo("Error connecting to connect server: ${t.message}")
57+
echo("Error connecting to the server: ${t.message}")
5558
throw t
5659
}
5760

@@ -61,7 +64,7 @@ class RTMPClientCli : SuspendingCliktCommand() {
6164
client.createStream()
6265
client.publish()
6366
} catch (t: Throwable) {
64-
echo("Error connecting to publish server: ${t.message}")
67+
echo("Error sending publish to the server: ${t.message}")
6568
client.close()
6669
throw t
6770
}
@@ -87,12 +90,13 @@ class RTMPClientCli : SuspendingCliktCommand() {
8790
try {
8891
echo("Closing connection to the server")
8992
client.deleteStream()
90-
client.close()
9193
} catch (t: Throwable) {
92-
echo("Error connecting to close connection to the server: ${t.message}")
93-
client.close()
94+
echo("Error sending delete stream to the server: ${t.message}")
9495
throw t
96+
} finally {
97+
client.close()
9598
}
99+
client.awaitClosed()
96100
}
97101

98102
private inner class EchoLogger : IKrtmpLogger {

0 commit comments

Comments
 (0)