Skip to content

Commit 2a8cdfc

Browse files
authored
Merge pull request #2125 from pedroSG94/feature/fixing-errors
fixing common and rtsp errors
2 parents 707a4a0 + 2662daf commit 2a8cdfc

80 files changed

Lines changed: 598 additions & 338 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/src/main/java/com/pedro/streamer/utils/FilterMenu.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class FilterMenu(private val context: Context) {
225225
BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)
226226
)
227227
imageObjectFilterRender.setScale(50f, 50f)
228-
imageObjectFilterRender.setPosition(TranslateTo.RIGHT)
228+
imageObjectFilterRender.setPosition(TranslateTo.CENTER)
229229
spriteGestureController.setBaseObjectFilterRender(imageObjectFilterRender) //Optional
230230
spriteGestureController.setPreventMoveOutside(false) //Optional
231231
return true

common/src/main/java/com/pedro/common/BitBuffer.kt

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,13 @@ class BitBuffer(val buffer: ByteBuffer) {
7171
}
7272
}
7373

74-
fun readUVLC(): Int {
74+
fun readUVLC(): Long {
7575
var leadingZeros = 0
76-
var value = 0
77-
var currentIndex = bufferPosition / 8
78-
var currentBit = 7 - bufferPosition % 8
79-
80-
while (buffer[currentIndex].toInt() and (1 shl currentBit) == 0) {
76+
while (!getBool()) {
8177
leadingZeros++
82-
if (currentBit == 0) {
83-
currentIndex++
84-
currentBit = 7
85-
} else {
86-
currentBit--
87-
}
88-
}
89-
90-
repeat((0 until leadingZeros + 1).count()) {
91-
if (currentBit == 0) {
92-
currentIndex++
93-
currentBit = 7
94-
} else {
95-
currentBit--
96-
}
97-
98-
value = (value shl 1) or ((buffer[currentIndex].toInt() ushr currentBit) and 1)
78+
if (leadingZeros >= 32) return (1L shl 32) - 1
9979
}
100-
bufferPosition += 2 * leadingZeros + 1
101-
return value
80+
return getLong(leadingZeros) + (1L shl leadingZeros) - 1
10281
}
10382

10483
fun resetPosition() {

common/src/main/java/com/pedro/common/Extensions.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import androidx.annotation.RequiresApi
3131
import com.pedro.common.frame.MediaFrame
3232
import kotlinx.coroutines.Dispatchers
3333
import kotlinx.coroutines.withContext
34+
import kotlinx.io.IOException
3435
import java.io.InputStream
3536
import java.io.OutputStream
3637
import java.io.UnsupportedEncodingException
@@ -53,9 +54,9 @@ import kotlin.coroutines.Continuation
5354
@Suppress("DEPRECATION")
5455
fun MediaCodec.BufferInfo.isKeyframe(): Boolean {
5556
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
56-
this.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME
57+
this.flags and MediaCodec.BUFFER_FLAG_KEY_FRAME != 0
5758
} else {
58-
this.flags == MediaCodec.BUFFER_FLAG_SYNC_FRAME
59+
this.flags and MediaCodec.BUFFER_FLAG_SYNC_FRAME != 0
5960
}
6061
}
6162

@@ -74,6 +75,7 @@ fun ByteBuffer.toByteArray(
7475
}
7576

7677
fun ByteBuffer.getStartCodeSize(): Int {
78+
if (this.remaining() < 4) return 0
7779
var startCodeSize = 0
7880
if (this.get(0).toInt() == 0x00 && this.get(1).toInt() == 0x00
7981
&& this.get(2).toInt() == 0x00 && this.get(3).toInt() == 0x01) {
@@ -123,16 +125,16 @@ fun ExecutorService.secureSubmit(timeout: Long = 5000, code: () -> Unit) {
123125
try {
124126
if (isTerminated || isShutdown) return
125127
submit { code() }.get(timeout, TimeUnit.MILLISECONDS)
126-
} catch (ignored: InterruptedException) {}
128+
} catch (_: Exception) {}
127129
}
128130

129131
fun String.getMd5Hash(): String {
130132
val md: MessageDigest
131133
try {
132134
md = MessageDigest.getInstance("MD5")
133135
return md.digest(toByteArray()).bytesToHex()
134-
} catch (ignore: NoSuchAlgorithmException) {
135-
} catch (ignore: UnsupportedEncodingException) {
136+
} catch (_: NoSuchAlgorithmException) {
137+
} catch (_: UnsupportedEncodingException) {
136138
}
137139
return ""
138140
}
@@ -236,29 +238,34 @@ fun BigInteger.toByteArray(length: Int): ByteArray {
236238
}
237239
}
238240

241+
@Throws(IOException::class)
239242
fun InputStream.readUntil(byteArray: ByteArray) {
240243
var bytesRead = 0
241244
while (bytesRead < byteArray.size) {
242245
val result = read(byteArray, bytesRead, byteArray.size - bytesRead)
243-
if (result != -1) bytesRead += result
246+
if (result == -1) throw IOException("End of stream")
247+
bytesRead += result
244248
}
245249
}
246250

251+
@Throws(IOException::class)
247252
fun InputStream.readUInt32(): Int {
248253
val data = ByteArray(4)
249-
read(data)
254+
readUntil(data)
250255
return data.toUInt32()
251256
}
252257

258+
@Throws(IOException::class)
253259
fun InputStream.readUInt24(): Int {
254260
val data = ByteArray(3)
255-
read(data)
261+
readUntil(data)
256262
return data.toUInt24()
257263
}
258264

265+
@Throws(IOException::class)
259266
fun InputStream.readUInt16(): Int {
260267
val data = ByteArray(2)
261-
read(data)
268+
readUntil(data)
262269
return data.toUInt16()
263270
}
264271

common/src/main/java/com/pedro/common/FpsUtils.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import kotlin.math.abs
77

88
object FpsUtils {
99
fun adaptFpsRange(expectedFps: Int, fpsRanges: List<IntArray>): IntArray {
10-
val expectedRange = intArrayOf(expectedFps, expectedFps)
1110
if (fpsRanges.isEmpty()) throw IllegalArgumentException("fpsRanges is empty")
12-
if (fpsRanges.contains(expectedRange)) return expectedRange
1311
val exactFps = fpsRanges.filter { it[1] == expectedFps }
1412
if (exactFps.isNotEmpty()) return exactFps.sortedBy { abs(expectedFps - it[0]) }[0]
1513
val higherFps = fpsRanges.filter { it[1] > expectedFps }

common/src/main/java/com/pedro/common/StreamBlockingQueue.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class StreamBlockingQueue(var capacity: Int) {
5555

5656
fun setCacheTime(cache: Long) {
5757
cacheTime = cache
58-
cacheQueue = PriorityBlockingQueue<MediaFrame>((cache / 5).toInt()) { p0, p1 ->
58+
if (cacheTime == 0L) return
59+
cacheQueue = PriorityBlockingQueue<MediaFrame>(maxOf(1, (cache / 5).toInt())) { p0, p1 ->
5960
p0.info.timestamp.compare(p1.info.timestamp)
6061
}
6162
}

common/src/main/java/com/pedro/common/UrlParser.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class UrlParser private constructor(
5757
fun getQuery(key: String): String? = getAllQueries()[key]
5858

5959
fun getAuthUser(): String? {
60-
val userInfo = auth?.split(":") ?: return null
60+
val userInfo = auth?.split(":", limit = 2) ?: return null
6161
return if (userInfo.size == 2) userInfo[0] else null
6262
}
6363

6464
fun getAuthPassword(): String? {
65-
val userInfo = auth?.split(":") ?: return null
65+
val userInfo = auth?.split(":", limit = 2) ?: return null
6666
return if (userInfo.size == 2) userInfo[1] else null
6767
}
6868

common/src/main/java/com/pedro/common/base/BaseSender.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ abstract class BaseSender(
2323

2424
@Volatile
2525
protected var running = false
26-
private var cacheSize = 400
2726

28-
protected val queue = StreamBlockingQueue(cacheSize)
27+
protected val queue = StreamBlockingQueue(400)
2928

3029
protected val audioFramesSent = AtomicLong(0)
3130
protected val videoFramesSent = AtomicLong(0)
@@ -92,21 +91,21 @@ abstract class BaseSender(
9291

9392
@Throws(IllegalArgumentException::class)
9493
fun hasCongestion(percentUsed: Float = 20f): Boolean {
95-
if (percentUsed < 0 || percentUsed > 100) throw IllegalArgumentException("the value must be in range 0 to 100")
94+
if (percentUsed !in 0.0..100.0) throw IllegalArgumentException("the value must be in range 0 to 100")
9695
val size = queue.getSize().toFloat()
9796
val remaining = queue.remainingCapacity().toFloat()
9897
val capacity = size + remaining
9998
return size >= capacity * (percentUsed / 100f)
10099
}
101100

102101
fun resizeCache(newSize: Int) {
103-
if (newSize < queue.getSize() - queue.remainingCapacity()) {
102+
if (newSize < queue.getSize()) {
104103
throw RuntimeException("Can't fit current cache inside new cache size")
105104
}
106105
queue.capacity = newSize
107106
}
108107

109-
fun getCacheSize(): Int = cacheSize
108+
fun getCacheSize(): Int = queue.capacity
110109

111110
fun getItemsInCache(): Int = queue.getSize()
112111

common/src/main/java/com/pedro/common/nal/NalReader.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ object NalReader {
3030
val start = offset + buffer.position()
3131
val limit = offset + buffer.limit()
3232
var payloadStart = -1
33+
var nalFound = false
3334

3435
var i = start
3536
while (i < limit - 2) {
@@ -40,6 +41,7 @@ object NalReader {
4041
duplicate.position(payloadStart - offset)
4142
duplicate.limit(previousPayloadEnd - offset)
4243
val nal = duplicate.slice()
44+
nalFound = true
4345
if (shouldKeepNal(nal, codec, shouldDiscardVideoInfo)) units.add(nal)
4446
}
4547
payloadStart = i + 3
@@ -53,9 +55,10 @@ object NalReader {
5355
duplicate.position(payloadStart - offset)
5456
duplicate.limit(limit - offset)
5557
val nal = duplicate.slice()
58+
nalFound = true
5659
if (shouldKeepNal(nal, codec, shouldDiscardVideoInfo)) units.add(nal)
5760
}
58-
if (units.isEmpty()) units.add(buffer)
61+
if (!nalFound) units.add(buffer)
5962
return units
6063
}
6164

common/src/main/java/com/pedro/common/socket/java/TcpStreamSocketJavaBase.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ package com.pedro.common.socket.java
22

33
import com.pedro.common.readUntil
44
import com.pedro.common.socket.base.TcpStreamSocket
5-
import java.io.BufferedReader
65
import java.io.ByteArrayInputStream
76
import java.io.ByteArrayOutputStream
8-
import java.io.InputStreamReader
97
import java.net.Socket
108

119
abstract class TcpStreamSocketJavaBase: TcpStreamSocket() {
1210

1311
private var socket = Socket()
1412
private var input = ByteArrayInputStream(byteArrayOf()).buffered()
1513
private var output = ByteArrayOutputStream().buffered()
16-
private var reader = InputStreamReader(input).buffered()
1714

1815
abstract fun onConnectSocket(timeout: Long): Socket
1916

2017
override suspend fun connect() {
2118
socket = onConnectSocket(timeout)
22-
reader = BufferedReader(InputStreamReader(socket.getInputStream()))
2319
output = socket.getOutputStream().buffered()
2420
input = socket.getInputStream().buffered()
2521
}
@@ -62,9 +58,22 @@ abstract class TcpStreamSocketJavaBase: TcpStreamSocket() {
6258
return data
6359
}
6460

65-
override suspend fun readLine(): String? = reader.readLine()
61+
override suspend fun readLine(): String? {
62+
var value = input.read()
63+
if (value == -1) return null
64+
val line = ByteArrayOutputStream()
65+
while (value != -1 && value != '\n'.code) {
66+
line.write(value)
67+
value = input.read()
68+
}
69+
var bytes = line.toByteArray()
70+
if (bytes.isNotEmpty() && bytes.last() == '\r'.code.toByte()) {
71+
bytes = bytes.copyOf(bytes.size - 1)
72+
}
73+
return String(bytes)
74+
}
6675

67-
override fun isConnected(): Boolean = socket.isConnected
76+
override fun isConnected(): Boolean = socket.isConnected && !socket.isClosed
6877

6978
override fun isReachable(): Boolean = socket.inetAddress?.isReachable(timeout.toInt()) ?: false
7079
}

common/src/main/java/com/pedro/common/socket/java/UdpStreamSocketJava.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class UdpStreamSocketJava(
1919
): UdpStreamSocket() {
2020

2121
private var socket: DatagramSocket? = null
22-
private val packetSize = receiveSize ?: SocketOptions.SO_RCVBUF
22+
private val packetSize = receiveSize ?: 65536
2323
private var remoteHost: String? = null
2424
private var remotePort: Int? = null
2525

0 commit comments

Comments
 (0)