Skip to content

Commit a0665ee

Browse files
committed
add setSocketTimeout method and add speed parameter to Media3 sources
1 parent 7ee3871 commit a0665ee

File tree

20 files changed

+96
-27
lines changed

20 files changed

+96
-27
lines changed

common/src/main/java/com/pedro/common/socket/base/StreamSocket.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,39 @@ import javax.net.ssl.TrustManager
2828
* Created by pedro on 22/9/24.
2929
*/
3030
abstract class StreamSocket {
31-
protected val timeout = 5000L
31+
protected var timeout = DEFAULT_TIMEOUT
3232
abstract suspend fun connect()
3333
abstract suspend fun close()
3434
abstract fun isConnected(): Boolean
3535
abstract fun isReachable(): Boolean
3636

3737
companion object {
38+
const val DEFAULT_TIMEOUT = 5000L
3839
fun createTcpSocket(
3940
type: SocketType,
40-
host: String, port: Int, secured: Boolean, certificates: TrustManager? = null
41+
host: String, port: Int, secured: Boolean,
42+
timeout: Long,
43+
certificates: TrustManager? = null
4144
): TcpStreamSocket {
4245
return when (type) {
4346
SocketType.KTOR -> TcpStreamSocketKtor(host, port, secured, certificates)
4447
SocketType.JAVA -> TcpStreamSocketJava(host, port, secured, certificates)
48+
}.apply {
49+
this.timeout = timeout
4550
}
4651
}
4752

4853
fun createUdpSocket(
4954
type: SocketType,
50-
host: String, port: Int, sourcePort: Int? = null, receiveSize: Int? = null, udpType: UdpType = UdpType.UNICAST
55+
host: String, port: Int, timeout: Long,
56+
sourcePort: Int? = null, receiveSize: Int? = null,
57+
udpType: UdpType = UdpType.UNICAST
5158
): UdpStreamSocket {
5259
return when (type) {
5360
SocketType.KTOR -> UdpStreamSocketKtor(host, port, sourcePort, receiveSize, udpType)
5461
SocketType.JAVA -> UdpStreamSocketJava(host, port, sourcePort, receiveSize, udpType)
62+
}.apply {
63+
this.timeout = timeout
5564
}
5665
}
5766
}

extra-sources/src/main/java/com/pedro/extrasources/Media3AudioSource.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.net.Uri
55
import androidx.annotation.OptIn
66
import androidx.media3.common.MediaItem
7+
import androidx.media3.common.PlaybackParameters
78
import androidx.media3.common.Player
89
import androidx.media3.common.util.UnstableApi
910
import androidx.media3.exoplayer.ExoPlayer
@@ -17,7 +18,8 @@ import com.pedro.extrasources.extractor.Media3Extractor
1718
@OptIn(UnstableApi::class)
1819
class Media3AudioSource(
1920
private val context: Context,
20-
private val path: Uri
21+
private val path: Uri,
22+
private val speed: Float = 1f
2123
): AudioSource() {
2224

2325
private var player: ExoPlayer? = null
@@ -32,7 +34,7 @@ class Media3AudioSource(
3234
try {
3335
mediaExtractor.initialize(context, path)
3436
mediaExtractor.selectTrack(MediaFrame.Type.AUDIO)
35-
} catch (e: Exception) {
37+
} catch (_: Exception) {
3638
throw IllegalArgumentException("Audio file track not found")
3739
}
3840
val audioInfo = mediaExtractor.getAudioInfo()
@@ -46,6 +48,7 @@ class Media3AudioSource(
4648
player = ExoPlayer.Builder(context, TracksRenderersFactory(context, MediaFrame.Type.AUDIO, processor)).build().also { exoPlayer ->
4749
val mediaItem = MediaItem.fromUri(path)
4850
exoPlayer.setMediaItem(mediaItem)
51+
exoPlayer.playbackParameters = PlaybackParameters(speed)
4952
exoPlayer.prepare()
5053
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
5154
}

extra-sources/src/main/java/com/pedro/extrasources/Media3VideoSource.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.net.Uri
66
import android.view.Surface
77
import androidx.annotation.OptIn
88
import androidx.media3.common.MediaItem
9+
import androidx.media3.common.PlaybackParameters
910
import androidx.media3.common.Player
1011
import androidx.media3.common.util.UnstableApi
1112
import androidx.media3.exoplayer.ExoPlayer
@@ -18,7 +19,8 @@ import com.pedro.extrasources.extractor.Media3Extractor
1819
@OptIn(UnstableApi::class)
1920
class Media3VideoSource(
2021
private val context: Context,
21-
private val path: Uri
22+
private val path: Uri,
23+
private val speed: Float = 1f
2224
): VideoSource() {
2325

2426
private var player: ExoPlayer? = null
@@ -29,14 +31,15 @@ class Media3VideoSource(
2931
try {
3032
mediaExtractor.initialize(context, path)
3133
mediaExtractor.selectTrack(MediaFrame.Type.VIDEO)
32-
} catch (e: Exception) {
34+
} catch (_: Exception) {
3335
throw IllegalArgumentException("Video file track not found")
3436
}
3537
mediaExtractor.release()
3638
player = ExoPlayer.Builder(context, TracksRenderersFactory(context, MediaFrame.Type.VIDEO)).build().also { exoPlayer ->
3739
exoPlayer.setVideoSurface(surface)
3840
val mediaItem = MediaItem.fromUri(path)
3941
exoPlayer.setMediaItem(mediaItem)
42+
exoPlayer.playbackParameters = PlaybackParameters(speed)
4043
exoPlayer.prepare()
4144
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
4245
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ class GenericStreamClient(
4343
udpClient.setSocketType(type)
4444
}
4545

46+
/**
47+
* Set timeout ms for connection, write and read in sockets by default 5000ms
48+
*/
49+
override fun setSocketTimeout(timeout: Long) {
50+
rtmpClient.setSocketTimeout(timeout)
51+
rtspClient.setSocketTimeout(timeout)
52+
srtClient.setSocketTimeout(timeout)
53+
udpClient.setSocketTimeout(timeout)
54+
}
55+
4656
/**
4757
* Set stream delay in millis.
4858
* This will create a cache and wait the delay to start send packets in real time

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ class RtmpStreamClient(
175175
rtmpClient.socketType = type
176176
}
177177

178+
/**
179+
* Set timeout ms for connection, write and read in sockets by default 5000ms
180+
*/
181+
override fun setSocketTimeout(timeout: Long) {
182+
rtmpClient.socketTimeout = timeout
183+
}
184+
178185
fun setCustomAmfObject(amfObject: Map<String, Any>) {
179186
rtmpClient.setCustomAmfObject(amfObject)
180187
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,11 @@ class RtspStreamClient(
149149
override fun setSocketType(type: SocketType) {
150150
rtspClient.socketType = type
151151
}
152+
153+
/**
154+
* Set timeout ms for connection, write and read in sockets by default 5000ms
155+
*/
156+
override fun setSocketTimeout(timeout: Long) {
157+
rtspClient.socketTimeout = timeout
158+
}
152159
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ class SrtStreamClient(
148148
srtClient.socketType = type
149149
}
150150

151+
/**
152+
* Set timeout ms for connection, write and read in sockets by default 5000ms
153+
*/
154+
override fun setSocketTimeout(timeout: Long) {
155+
srtClient.socketTimeout = timeout
156+
}
157+
151158
/**
152159
* Set a custom Mpeg2TsService with specified parameters
153160
* Must be called before connecting to the server

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ abstract class StreamBaseClient {
7979
* Set if you want use java.io or ktor socket
8080
*/
8181
abstract fun setSocketType(type: SocketType)
82+
/**
83+
* Set timeout ms for connection, write and read in sockets by default 5000ms
84+
*/
85+
abstract fun setSocketTimeout(timeout: Long)
8286
/**
8387
* Set stream delay in millis.
8488
* This will create a cache and wait the delay to start send packets in real time

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,11 @@ class UdpStreamClient(
132132
override fun setSocketType(type: SocketType) {
133133
udpClient.socketType = type
134134
}
135+
136+
/**
137+
* Set timeout ms for connection, write and read in sockets by default 5000ms
138+
*/
139+
override fun setSocketTimeout(timeout: Long) {
140+
udpClient.socketTimeout = timeout
141+
}
135142
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.pedro.common.clone
2828
import com.pedro.common.frame.MediaFrame
2929
import com.pedro.common.onMainThread
3030
import com.pedro.common.socket.base.SocketType
31+
import com.pedro.common.socket.base.StreamSocket
3132
import com.pedro.common.toMediaFrameInfo
3233
import com.pedro.common.validMessage
3334
import com.pedro.rtmp.amf.AmfVersion
@@ -106,6 +107,7 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
106107
val bytesSend: Long
107108
get() = rtmpSender.bytesSend
108109
var socketType = SocketType.KTOR
110+
var socketTimeout = StreamSocket.DEFAULT_TIMEOUT
109111

110112
/**
111113
* Add certificates for TLS connection
@@ -336,7 +338,7 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
336338
val socket = if (tunneled) {
337339
TcpTunneledSocket(commandsManager.host, commandsManager.port, tlsEnabled)
338340
} else {
339-
TcpSocket(socketType, commandsManager.host, commandsManager.port, tlsEnabled, certificates)
341+
TcpSocket(socketType, commandsManager.host, commandsManager.port, tlsEnabled, socketTimeout, certificates)
340342
}
341343
this.socket = socket
342344
socket.connect()

0 commit comments

Comments
 (0)