@@ -31,6 +31,7 @@ import androidx.annotation.RequiresApi
3131import com.pedro.common.frame.MediaFrame
3232import kotlinx.coroutines.Dispatchers
3333import kotlinx.coroutines.withContext
34+ import kotlinx.io.IOException
3435import java.io.InputStream
3536import java.io.OutputStream
3637import java.io.UnsupportedEncodingException
@@ -53,9 +54,9 @@ import kotlin.coroutines.Continuation
5354@Suppress(" DEPRECATION" )
5455fun 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
7677fun 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
129131fun 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 )
239242fun 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 )
247252fun InputStream.readUInt32 (): Int {
248253 val data = ByteArray (4 )
249- read (data)
254+ readUntil (data)
250255 return data.toUInt32()
251256}
252257
258+ @Throws(IOException ::class )
253259fun InputStream.readUInt24 (): Int {
254260 val data = ByteArray (3 )
255- read (data)
261+ readUntil (data)
256262 return data.toUInt24()
257263}
258264
265+ @Throws(IOException ::class )
259266fun InputStream.readUInt16 (): Int {
260267 val data = ByteArray (2 )
261- read (data)
268+ readUntil (data)
262269 return data.toUInt16()
263270}
264271
0 commit comments