From 765400557771e503dd02d699e6a7a5cbccb50b29 Mon Sep 17 00:00:00 2001 From: pedro Date: Fri, 3 Nov 2023 11:21:19 +0100 Subject: [PATCH] move BitrateManager and TimeUtils to common module --- common/build.gradle | 12 ++- .../pedro/common/ExampleInstrumentedTest.kt | 24 ------ .../java/com/pedro/common}/BitrateManager.kt | 3 +- .../main/java/com/pedro/common/Extensions.kt | 14 ++++ .../main/java/com/pedro/common}/TimeUtils.kt | 2 +- .../com/pedro/common}/BitrateManagerTest.kt | 12 +-- .../java/com/pedro/common/ExampleUnitTest.kt | 17 ---- .../pedro/common/util}/MainDispatcherRule.kt | 2 +- .../test/java/com/pedro/common/util}/Utils.kt | 2 +- .../java/com/pedro/rtmp/amf/v0/AmfDate.kt | 2 +- .../com/pedro/rtmp/rtmp/CommandsManager.kt | 2 +- .../java/com/pedro/rtmp/rtmp/Handshake.kt | 2 +- .../java/com/pedro/rtmp/rtmp/RtmpClient.kt | 4 +- .../java/com/pedro/rtmp/rtmp/RtmpSender.kt | 4 +- .../com/pedro/rtmp/utils/BitrateManager.kt | 42 ---------- .../java/com/pedro/rtmp/utils/TimeUtils.kt | 29 ------- .../main/java/com/pedro/rtmp/utils/Utils.kt | 6 -- .../rtmp/utils/socket/TcpTunneledSocket.kt | 2 +- .../java/com/pedro/rtmp/MainDispatcherRule.kt | 23 ------ .../com/pedro/rtsp/rtcp/BaseSenderReport.kt | 2 +- .../java/com/pedro/rtsp/rtsp/RtspClient.kt | 2 +- .../java/com/pedro/rtsp/rtsp/RtspSender.kt | 4 +- .../rtsp/rtsp/commands/CommandsManager.kt | 2 +- .../com/pedro/rtsp/utils/BitrateManager.kt | 42 ---------- .../java/com/pedro/rtsp/utils/Extensions.kt | 6 -- .../com/pedro/rtsp/rtcp/RtcpReportTest.kt | 2 +- .../pedro/rtsp/utils/BitrateManagerTest.kt | 81 ------------------- .../com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt | 2 +- .../java/com/pedro/srt/srt/CommandsManager.kt | 2 +- .../main/java/com/pedro/srt/srt/SrtClient.kt | 2 +- .../main/java/com/pedro/srt/srt/SrtSender.kt | 4 +- .../java/com/pedro/srt/utils/Extensions.kt | 6 -- .../java/com/pedro/srt/utils/TimeUtils.kt | 29 ------- .../java/com/pedro/srt/MainDispatcherRule.kt | 23 ------ .../java/com/pedro/srt/mpeg2ts/PesTest.kt | 2 +- .../java/com/pedro/srt/mpeg2ts/PsiTest.kt | 2 +- .../com/pedro/srt/utils/BitrateManagerTest.kt | 80 ------------------ 37 files changed, 50 insertions(+), 447 deletions(-) delete mode 100644 common/src/androidTest/java/com/pedro/common/ExampleInstrumentedTest.kt rename {srt/src/main/java/com/pedro/srt/utils => common/src/main/java/com/pedro/common}/BitrateManager.kt (94%) create mode 100644 common/src/main/java/com/pedro/common/Extensions.kt rename {rtsp/src/main/java/com/pedro/rtsp/utils => common/src/main/java/com/pedro/common}/TimeUtils.kt (96%) rename {rtmp/src/test/java/com/pedro/rtmp/utils => common/src/test/java/com/pedro/common}/BitrateManagerTest.kt (87%) delete mode 100644 common/src/test/java/com/pedro/common/ExampleUnitTest.kt rename {rtsp/src/test/java/com/pedro/rtsp => common/src/test/java/com/pedro/common/util}/MainDispatcherRule.kt (95%) rename {rtmp/src/test/java/com/pedro/rtmp => common/src/test/java/com/pedro/common/util}/Utils.kt (97%) delete mode 100644 rtmp/src/main/java/com/pedro/rtmp/utils/BitrateManager.kt delete mode 100644 rtmp/src/main/java/com/pedro/rtmp/utils/TimeUtils.kt delete mode 100644 rtmp/src/test/java/com/pedro/rtmp/MainDispatcherRule.kt delete mode 100644 rtsp/src/main/java/com/pedro/rtsp/utils/BitrateManager.kt delete mode 100644 rtsp/src/test/java/com/pedro/rtsp/utils/BitrateManagerTest.kt delete mode 100644 srt/src/main/java/com/pedro/srt/utils/TimeUtils.kt delete mode 100644 srt/src/test/java/com/pedro/srt/MainDispatcherRule.kt delete mode 100644 srt/src/test/java/com/pedro/srt/utils/BitrateManagerTest.kt diff --git a/common/build.gradle b/common/build.gradle index b59f863d3..3eee9fc07 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -5,26 +5,24 @@ plugins { android { namespace 'com.pedro.common' - compileSdk 33 + compileSdk 34 defaultConfig { minSdk 16 - - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + targetSdkVersion 34 } buildTypes { release { minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { - jvmTarget = '1.8' + jvmTarget = '17' } } diff --git a/common/src/androidTest/java/com/pedro/common/ExampleInstrumentedTest.kt b/common/src/androidTest/java/com/pedro/common/ExampleInstrumentedTest.kt deleted file mode 100644 index cf5230ce3..000000000 --- a/common/src/androidTest/java/com/pedro/common/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.pedro.common - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.pedro.common.test", appContext.packageName) - } -} \ No newline at end of file diff --git a/srt/src/main/java/com/pedro/srt/utils/BitrateManager.kt b/common/src/main/java/com/pedro/common/BitrateManager.kt similarity index 94% rename from srt/src/main/java/com/pedro/srt/utils/BitrateManager.kt rename to common/src/main/java/com/pedro/common/BitrateManager.kt index 9b7c82ddb..09cffefa3 100644 --- a/srt/src/main/java/com/pedro/srt/utils/BitrateManager.kt +++ b/common/src/main/java/com/pedro/common/BitrateManager.kt @@ -14,9 +14,8 @@ * limitations under the License. */ -package com.pedro.srt.utils +package com.pedro.common -import com.pedro.common.ConnectChecker /** * Created by pedro on 8/04/21. diff --git a/common/src/main/java/com/pedro/common/Extensions.kt b/common/src/main/java/com/pedro/common/Extensions.kt new file mode 100644 index 000000000..49cd5d2b5 --- /dev/null +++ b/common/src/main/java/com/pedro/common/Extensions.kt @@ -0,0 +1,14 @@ +package com.pedro.common + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +/** + * Created by pedro on 3/11/23. + */ + +suspend fun onMainThread(code: () -> Unit) { + withContext(Dispatchers.Main) { + code() + } +} \ No newline at end of file diff --git a/rtsp/src/main/java/com/pedro/rtsp/utils/TimeUtils.kt b/common/src/main/java/com/pedro/common/TimeUtils.kt similarity index 96% rename from rtsp/src/main/java/com/pedro/rtsp/utils/TimeUtils.kt rename to common/src/main/java/com/pedro/common/TimeUtils.kt index 4fe685099..122fb5053 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/utils/TimeUtils.kt +++ b/common/src/main/java/com/pedro/common/TimeUtils.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.rtsp.utils +package com.pedro.common /** * Created by pedro on 30/8/23. diff --git a/rtmp/src/test/java/com/pedro/rtmp/utils/BitrateManagerTest.kt b/common/src/test/java/com/pedro/common/BitrateManagerTest.kt similarity index 87% rename from rtmp/src/test/java/com/pedro/rtmp/utils/BitrateManagerTest.kt rename to common/src/test/java/com/pedro/common/BitrateManagerTest.kt index 031b5c8ca..fc9ad6c35 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/utils/BitrateManagerTest.kt +++ b/common/src/test/java/com/pedro/common/BitrateManagerTest.kt @@ -14,10 +14,10 @@ * limitations under the License. */ -package com.pedro.rtmp.utils +package com.pedro.common -import com.pedro.rtmp.MainDispatcherRule -import com.pedro.rtmp.Utils +import com.pedro.common.util.MainDispatcherRule +import com.pedro.common.util.Utils import kotlinx.coroutines.test.runTest import org.junit.After import org.junit.Assert.assertTrue @@ -42,7 +42,7 @@ class BitrateManagerTest { @get:Rule val mainDispatcherRule = MainDispatcherRule() @Mock - private lateinit var connectCheckerRtmp: ConnectCheckerRtmp + private lateinit var connectChecker: ConnectChecker private val timeUtilsMocked = Mockito.mockStatic(TimeUtils::class.java) private var fakeTime = 7502849023L @@ -59,7 +59,7 @@ class BitrateManagerTest { @Test fun `WHEN set multiple values THEN return total of values each second`() = runTest { Utils.useStatics(listOf(timeUtilsMocked)) { - val bitrateManager = BitrateManager(connectCheckerRtmp) + val bitrateManager = BitrateManager(connectChecker) val fakeValues = arrayOf(100L, 200L, 300L, 400L, 500L) var expectedResult = 0L fakeValues.forEach { @@ -71,7 +71,7 @@ class BitrateManagerTest { bitrateManager.calculateBitrate(value) expectedResult += value val resultValue = argumentCaptor() - verify(connectCheckerRtmp, times(1)).onNewBitrateRtmp(resultValue.capture()) + verify(connectChecker, times(1)).onNewBitrate(resultValue.capture()) val marginError = 20 assertTrue(expectedResult - marginError <= resultValue.firstValue && resultValue.firstValue <= expectedResult + marginError) } diff --git a/common/src/test/java/com/pedro/common/ExampleUnitTest.kt b/common/src/test/java/com/pedro/common/ExampleUnitTest.kt deleted file mode 100644 index 6162ac624..000000000 --- a/common/src/test/java/com/pedro/common/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.pedro.common - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} \ No newline at end of file diff --git a/rtsp/src/test/java/com/pedro/rtsp/MainDispatcherRule.kt b/common/src/test/java/com/pedro/common/util/MainDispatcherRule.kt similarity index 95% rename from rtsp/src/test/java/com/pedro/rtsp/MainDispatcherRule.kt rename to common/src/test/java/com/pedro/common/util/MainDispatcherRule.kt index 6de44ca54..c1be3f64a 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/MainDispatcherRule.kt +++ b/common/src/test/java/com/pedro/common/util/MainDispatcherRule.kt @@ -1,4 +1,4 @@ -package com.pedro.rtsp +package com.pedro.common.util import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi diff --git a/rtmp/src/test/java/com/pedro/rtmp/Utils.kt b/common/src/test/java/com/pedro/common/util/Utils.kt similarity index 97% rename from rtmp/src/test/java/com/pedro/rtmp/Utils.kt rename to common/src/test/java/com/pedro/common/util/Utils.kt index 9a10e2fcc..c63f1b7e1 100644 --- a/rtmp/src/test/java/com/pedro/rtmp/Utils.kt +++ b/common/src/test/java/com/pedro/common/util/Utils.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.pedro.rtmp +package com.pedro.common.util import org.mockito.MockedStatic diff --git a/rtmp/src/main/java/com/pedro/rtmp/amf/v0/AmfDate.kt b/rtmp/src/main/java/com/pedro/rtmp/amf/v0/AmfDate.kt index d49aae73e..69ac66ff2 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/amf/v0/AmfDate.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/amf/v0/AmfDate.kt @@ -16,7 +16,7 @@ package com.pedro.rtmp.amf.v0 -import com.pedro.rtmp.utils.TimeUtils +import com.pedro.common.TimeUtils import com.pedro.rtmp.utils.readUntil import java.io.IOException import java.io.InputStream diff --git a/rtmp/src/main/java/com/pedro/rtmp/rtmp/CommandsManager.kt b/rtmp/src/main/java/com/pedro/rtmp/rtmp/CommandsManager.kt index 004d0d822..0631dd704 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/rtmp/CommandsManager.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/rtmp/CommandsManager.kt @@ -17,6 +17,7 @@ package com.pedro.rtmp.rtmp import android.util.Log +import com.pedro.common.TimeUtils import com.pedro.rtmp.amf.v0.* import com.pedro.rtmp.flv.FlvPacket import com.pedro.rtmp.rtmp.message.* @@ -25,7 +26,6 @@ import com.pedro.rtmp.rtmp.message.control.Type import com.pedro.rtmp.rtmp.message.control.UserControl import com.pedro.rtmp.utils.CommandSessionHistory import com.pedro.rtmp.utils.RtmpConfig -import com.pedro.rtmp.utils.TimeUtils import com.pedro.rtmp.utils.socket.RtmpSocket import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock diff --git a/rtmp/src/main/java/com/pedro/rtmp/rtmp/Handshake.kt b/rtmp/src/main/java/com/pedro/rtmp/rtmp/Handshake.kt index fec7ad636..69f862356 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/rtmp/Handshake.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/rtmp/Handshake.kt @@ -17,7 +17,7 @@ package com.pedro.rtmp.rtmp import android.util.Log -import com.pedro.rtmp.utils.TimeUtils +import com.pedro.common.TimeUtils import com.pedro.rtmp.utils.readUntil import com.pedro.rtmp.utils.socket.RtmpSocket import java.io.IOException diff --git a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt index 7c4c913e0..6ccb7af35 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt @@ -19,6 +19,8 @@ package com.pedro.rtmp.rtmp import android.media.MediaCodec import android.util.Log import com.pedro.common.ConnectChecker +import com.pedro.common.TimeUtils +import com.pedro.common.onMainThread import com.pedro.rtmp.amf.AmfVersion import com.pedro.rtmp.flv.video.ProfileIop import com.pedro.rtmp.rtmp.message.* @@ -27,8 +29,6 @@ import com.pedro.rtmp.rtmp.message.control.Type import com.pedro.rtmp.rtmp.message.control.UserControl import com.pedro.rtmp.utils.AuthUtil import com.pedro.rtmp.utils.RtmpConfig -import com.pedro.rtmp.utils.TimeUtils -import com.pedro.rtmp.utils.onMainThread import com.pedro.rtmp.utils.socket.RtmpSocket import com.pedro.rtmp.utils.socket.TcpSocket import com.pedro.rtmp.utils.socket.TcpTunneledSocket diff --git a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt index 1f72f5086..91423ade9 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpSender.kt @@ -18,15 +18,15 @@ package com.pedro.rtmp.rtmp import android.media.MediaCodec import android.util.Log +import com.pedro.common.BitrateManager import com.pedro.common.ConnectChecker +import com.pedro.common.onMainThread import com.pedro.rtmp.flv.FlvPacket import com.pedro.rtmp.flv.FlvType import com.pedro.rtmp.flv.audio.AacPacket import com.pedro.rtmp.flv.video.H264Packet import com.pedro.rtmp.flv.video.H265Packet import com.pedro.rtmp.flv.video.ProfileIop -import com.pedro.rtmp.utils.BitrateManager -import com.pedro.rtmp.utils.onMainThread import com.pedro.rtmp.utils.socket.RtmpSocket import com.pedro.rtmp.utils.trySend import kotlinx.coroutines.CoroutineScope diff --git a/rtmp/src/main/java/com/pedro/rtmp/utils/BitrateManager.kt b/rtmp/src/main/java/com/pedro/rtmp/utils/BitrateManager.kt deleted file mode 100644 index a7a889e42..000000000 --- a/rtmp/src/main/java/com/pedro/rtmp/utils/BitrateManager.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2021 pedroSG94. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pedro.rtmp.utils - -import com.pedro.common.ConnectChecker - -/** - * Created by pedro on 8/04/21. - * - * Calculate video and audio bitrate per second - */ -open class BitrateManager(private val connectChecker: ConnectChecker) { - - private var bitrate: Long = 0 - private var timeStamp = TimeUtils.getCurrentTimeMillis() - - suspend fun calculateBitrate(size: Long) { - bitrate += size - val timeDiff = TimeUtils.getCurrentTimeMillis() - timeStamp - if (timeDiff >= 1000) { - onMainThread { - } - connectChecker.onNewBitrate((bitrate / (timeDiff / 1000f)).toLong()) - timeStamp = TimeUtils.getCurrentTimeMillis() - bitrate = 0 - } - } -} \ No newline at end of file diff --git a/rtmp/src/main/java/com/pedro/rtmp/utils/TimeUtils.kt b/rtmp/src/main/java/com/pedro/rtmp/utils/TimeUtils.kt deleted file mode 100644 index 9aaeea3ca..000000000 --- a/rtmp/src/main/java/com/pedro/rtmp/utils/TimeUtils.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2023 pedroSG94. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pedro.rtmp.utils - -/** - * Created by pedro on 30/8/23. - */ -object TimeUtils { - - @JvmStatic - fun getCurrentTimeMillis(): Long = System.currentTimeMillis() - - @JvmStatic - fun getCurrentTimeNano(): Long = System.nanoTime() -} \ No newline at end of file diff --git a/rtmp/src/main/java/com/pedro/rtmp/utils/Utils.kt b/rtmp/src/main/java/com/pedro/rtmp/utils/Utils.kt index 057e5e3b5..f7f678661 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/utils/Utils.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/utils/Utils.kt @@ -36,12 +36,6 @@ inline infix fun BlockingQueue.trySend(item: T): Boolean { } } -suspend fun onMainThread(code: () -> Unit) { - withContext(Dispatchers.Main) { - code() - } -} - fun InputStream.readUntil(byteArray: ByteArray) { var bytesRead = 0 while (bytesRead < byteArray.size) { diff --git a/rtmp/src/main/java/com/pedro/rtmp/utils/socket/TcpTunneledSocket.kt b/rtmp/src/main/java/com/pedro/rtmp/utils/socket/TcpTunneledSocket.kt index fba10a870..aaf5e7368 100644 --- a/rtmp/src/main/java/com/pedro/rtmp/utils/socket/TcpTunneledSocket.kt +++ b/rtmp/src/main/java/com/pedro/rtmp/utils/socket/TcpTunneledSocket.kt @@ -17,7 +17,7 @@ package com.pedro.rtmp.utils.socket import android.util.Log -import com.pedro.rtmp.utils.TimeUtils +import com.pedro.common.TimeUtils import java.io.* import java.net.HttpURLConnection import java.net.SocketTimeoutException diff --git a/rtmp/src/test/java/com/pedro/rtmp/MainDispatcherRule.kt b/rtmp/src/test/java/com/pedro/rtmp/MainDispatcherRule.kt deleted file mode 100644 index aef4ba6e4..000000000 --- a/rtmp/src/test/java/com/pedro/rtmp/MainDispatcherRule.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.pedro.rtmp - -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestDispatcher -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain -import org.junit.rules.TestWatcher -import org.junit.runner.Description - -@OptIn(ExperimentalCoroutinesApi::class) -class MainDispatcherRule( - private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher() -) : TestWatcher() { - override fun starting(description: Description) { - Dispatchers.setMain(testDispatcher) - } - - override fun finished(description: Description) { - Dispatchers.resetMain() - } -} \ No newline at end of file diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt b/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt index 8e81ba373..6f06cd8a9 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtcp/BaseSenderReport.kt @@ -16,10 +16,10 @@ package com.pedro.rtsp.rtcp +import com.pedro.common.TimeUtils import com.pedro.rtsp.rtsp.Protocol import com.pedro.rtsp.rtsp.RtpFrame import com.pedro.rtsp.utils.RtpConstants -import com.pedro.rtsp.utils.TimeUtils import com.pedro.rtsp.utils.setLong import java.io.IOException import java.io.OutputStream diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt index 37640b74f..3fd4ef2f5 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt @@ -19,11 +19,11 @@ package com.pedro.rtsp.rtsp import android.media.MediaCodec import android.util.Log import com.pedro.common.ConnectChecker +import com.pedro.common.onMainThread import com.pedro.rtsp.rtsp.commands.CommandsManager import com.pedro.rtsp.rtsp.commands.Method import com.pedro.rtsp.utils.CreateSSLSocket.createSSlSocket import com.pedro.rtsp.utils.RtpConstants -import com.pedro.rtsp.utils.onMainThread import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt index 3e296e328..c61c1a42c 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspSender.kt @@ -18,14 +18,14 @@ package com.pedro.rtsp.rtsp import android.media.MediaCodec import android.util.Log +import com.pedro.common.BitrateManager import com.pedro.common.ConnectChecker +import com.pedro.common.onMainThread import com.pedro.rtsp.rtcp.BaseSenderReport import com.pedro.rtsp.rtp.packets.* import com.pedro.rtsp.rtp.sockets.BaseRtpSocket import com.pedro.rtsp.rtp.sockets.RtpSocketTcp -import com.pedro.rtsp.utils.BitrateManager import com.pedro.rtsp.utils.RtpConstants -import com.pedro.rtsp.utils.onMainThread import com.pedro.rtsp.utils.trySend import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers diff --git a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt index 994c8a4b3..47069da64 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/rtsp/commands/CommandsManager.kt @@ -18,6 +18,7 @@ package com.pedro.rtsp.rtsp.commands import android.util.Base64 import android.util.Log +import com.pedro.common.TimeUtils import com.pedro.rtsp.BuildConfig import com.pedro.rtsp.rtsp.Protocol import com.pedro.rtsp.rtsp.VideoCodec @@ -26,7 +27,6 @@ import com.pedro.rtsp.rtsp.commands.SdpBody.createH264Body import com.pedro.rtsp.rtsp.commands.SdpBody.createH265Body import com.pedro.rtsp.utils.AuthUtil.getMd5Hash import com.pedro.rtsp.utils.RtpConstants -import com.pedro.rtsp.utils.TimeUtils import com.pedro.rtsp.utils.encodeToString import com.pedro.rtsp.utils.getData import java.io.BufferedReader diff --git a/rtsp/src/main/java/com/pedro/rtsp/utils/BitrateManager.kt b/rtsp/src/main/java/com/pedro/rtsp/utils/BitrateManager.kt deleted file mode 100644 index 2484ac809..000000000 --- a/rtsp/src/main/java/com/pedro/rtsp/utils/BitrateManager.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2021 pedroSG94. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pedro.rtsp.utils - -import com.pedro.common.ConnectChecker - -/** - * Created by pedro on 10/07/19. - * - * Calculate video and audio bitrate per second - */ -class BitrateManager(private val connectChecker: ConnectChecker) { - - private var bitrate: Long = 0 - private var timeStamp = TimeUtils.getCurrentTimeMillis() - - suspend fun calculateBitrate(size: Long) { - bitrate += size - val timeDiff = TimeUtils.getCurrentTimeMillis() - timeStamp - if (timeDiff >= 1000) { - onMainThread { - connectChecker.onNewBitrate((bitrate / (timeDiff / 1000f)).toLong()) - } - timeStamp = TimeUtils.getCurrentTimeMillis() - bitrate = 0 - } - } -} \ No newline at end of file diff --git a/rtsp/src/main/java/com/pedro/rtsp/utils/Extensions.kt b/rtsp/src/main/java/com/pedro/rtsp/utils/Extensions.kt index 8a85b79ea..9c8fe38c1 100644 --- a/rtsp/src/main/java/com/pedro/rtsp/utils/Extensions.kt +++ b/rtsp/src/main/java/com/pedro/rtsp/utils/Extensions.kt @@ -63,10 +63,4 @@ fun ByteBuffer.getVideoStartCodeSize(): Int { startCodeSize = 3 } return startCodeSize -} - -suspend fun onMainThread(code: () -> Unit) { - withContext(Dispatchers.Main) { - code() - } } \ No newline at end of file diff --git a/rtsp/src/test/java/com/pedro/rtsp/rtcp/RtcpReportTest.kt b/rtsp/src/test/java/com/pedro/rtsp/rtcp/RtcpReportTest.kt index dc03b8d80..a6fa8e99c 100644 --- a/rtsp/src/test/java/com/pedro/rtsp/rtcp/RtcpReportTest.kt +++ b/rtsp/src/test/java/com/pedro/rtsp/rtcp/RtcpReportTest.kt @@ -1,10 +1,10 @@ package com.pedro.rtsp.rtcp +import com.pedro.common.TimeUtils import com.pedro.rtsp.Utils import com.pedro.rtsp.rtsp.Protocol import com.pedro.rtsp.rtsp.RtpFrame import com.pedro.rtsp.utils.RtpConstants -import com.pedro.rtsp.utils.TimeUtils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.test.runTest import kotlinx.coroutines.withContext diff --git a/rtsp/src/test/java/com/pedro/rtsp/utils/BitrateManagerTest.kt b/rtsp/src/test/java/com/pedro/rtsp/utils/BitrateManagerTest.kt deleted file mode 100644 index d54cd9e1e..000000000 --- a/rtsp/src/test/java/com/pedro/rtsp/utils/BitrateManagerTest.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2021 pedroSG94. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pedro.rtsp.utils - -import com.pedro.rtsp.MainDispatcherRule -import com.pedro.rtsp.Utils -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain -import org.junit.After -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Mock -import org.mockito.Mockito -import org.mockito.junit.MockitoJUnitRunner -import org.mockito.kotlin.argumentCaptor -import org.mockito.kotlin.times -import org.mockito.kotlin.verify - - -/** - * Created by pedro on 14/4/22. - */ -@RunWith(MockitoJUnitRunner::class) -class BitrateManagerTest { - - @get:Rule - val mainDispatcherRule = MainDispatcherRule() - @Mock - private lateinit var connectCheckerRtsp: ConnectCheckerRtsp - private val timeUtilsMocked = Mockito.mockStatic(TimeUtils::class.java) - private var fakeTime = 7502849023L - - @Before - fun setup() { - timeUtilsMocked.`when`(TimeUtils::getCurrentTimeMillis).then { fakeTime } - } - - @After - fun teardown() { - fakeTime = 7502849023L - } - - @Test - fun `WHEN set multiple values THEN return total of values each second`() = runTest { - Utils.useStatics(listOf(timeUtilsMocked)) { - val bitrateManager = BitrateManager(connectCheckerRtsp) - val fakeValues = arrayOf(100L, 200L, 300L, 400L, 500L) - var expectedResult = 0L - fakeValues.forEach { - bitrateManager.calculateBitrate(it) - expectedResult += it - } - fakeTime += 1000 - val value = 100L - bitrateManager.calculateBitrate(value) - expectedResult += value - val resultValue = argumentCaptor() - verify(connectCheckerRtsp, times(1)).onNewBitrateRtsp(resultValue.capture()) - val marginError = 20 - assertTrue(expectedResult - marginError <= resultValue.firstValue && resultValue.firstValue <= expectedResult + marginError) - } - } -} \ No newline at end of file diff --git a/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt b/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt index 7d9d06ec8..485e6dc58 100644 --- a/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt +++ b/srt/src/main/java/com/pedro/srt/mpeg2ts/MpegTsPacketizer.kt @@ -16,8 +16,8 @@ package com.pedro.srt.mpeg2ts +import com.pedro.common.TimeUtils import com.pedro.srt.mpeg2ts.psi.Psi -import com.pedro.srt.utils.TimeUtils import com.pedro.srt.utils.toByteArray import com.pedro.srt.utils.toInt import java.nio.ByteBuffer diff --git a/srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt b/srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt index c7e4d307f..d9bbf4508 100644 --- a/srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt +++ b/srt/src/main/java/com/pedro/srt/srt/CommandsManager.kt @@ -17,6 +17,7 @@ package com.pedro.srt.srt import android.util.Log +import com.pedro.common.TimeUtils import com.pedro.srt.mpeg2ts.MpegTsPacket import com.pedro.srt.srt.packets.DataPacket import com.pedro.srt.srt.packets.SrtPacket @@ -25,7 +26,6 @@ import com.pedro.srt.srt.packets.control.Shutdown import com.pedro.srt.srt.packets.control.handshake.Handshake import com.pedro.srt.utils.Constants import com.pedro.srt.utils.SrtSocket -import com.pedro.srt.utils.TimeUtils import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import java.io.IOException diff --git a/srt/src/main/java/com/pedro/srt/srt/SrtClient.kt b/srt/src/main/java/com/pedro/srt/srt/SrtClient.kt index d9bd44c30..004b7f76b 100644 --- a/srt/src/main/java/com/pedro/srt/srt/SrtClient.kt +++ b/srt/src/main/java/com/pedro/srt/srt/SrtClient.kt @@ -19,6 +19,7 @@ package com.pedro.srt.srt import android.media.MediaCodec import android.util.Log import com.pedro.common.ConnectChecker +import com.pedro.common.onMainThread import com.pedro.srt.mpeg2ts.Codec import com.pedro.srt.srt.packets.ControlPacket import com.pedro.srt.srt.packets.DataPacket @@ -37,7 +38,6 @@ import com.pedro.srt.srt.packets.control.handshake.HandshakeType import com.pedro.srt.srt.packets.control.handshake.extension.ExtensionContentFlag import com.pedro.srt.srt.packets.control.handshake.extension.HandshakeExtension import com.pedro.srt.utils.SrtSocket -import com.pedro.srt.utils.onMainThread import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job diff --git a/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt b/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt index cbddcf00e..f2544484d 100644 --- a/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt +++ b/srt/src/main/java/com/pedro/srt/srt/SrtSender.kt @@ -18,7 +18,9 @@ package com.pedro.srt.srt import android.media.MediaCodec import android.util.Log +import com.pedro.common.BitrateManager import com.pedro.common.ConnectChecker +import com.pedro.common.onMainThread import com.pedro.srt.mpeg2ts.Codec import com.pedro.srt.mpeg2ts.MpegTsPacket import com.pedro.srt.mpeg2ts.MpegTsPacketizer @@ -31,9 +33,7 @@ import com.pedro.srt.mpeg2ts.psi.TableToSend import com.pedro.srt.mpeg2ts.service.Mpeg2TsService import com.pedro.srt.srt.packets.SrtPacket import com.pedro.srt.srt.packets.data.PacketPosition -import com.pedro.srt.utils.BitrateManager import com.pedro.srt.utils.SrtSocket -import com.pedro.srt.utils.onMainThread import com.pedro.srt.utils.trySend import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers diff --git a/srt/src/main/java/com/pedro/srt/utils/Extensions.kt b/srt/src/main/java/com/pedro/srt/utils/Extensions.kt index 51c290293..11b6c21b4 100644 --- a/srt/src/main/java/com/pedro/srt/utils/Extensions.kt +++ b/srt/src/main/java/com/pedro/srt/utils/Extensions.kt @@ -84,10 +84,4 @@ fun InputStream.readUntil(byteArray: ByteArray) { val result = read(byteArray, bytesRead, byteArray.size - bytesRead) if (result != -1) bytesRead += result } -} - -suspend fun onMainThread(code: () -> Unit) { - withContext(Dispatchers.Main) { - code() - } } \ No newline at end of file diff --git a/srt/src/main/java/com/pedro/srt/utils/TimeUtils.kt b/srt/src/main/java/com/pedro/srt/utils/TimeUtils.kt deleted file mode 100644 index 77b578473..000000000 --- a/srt/src/main/java/com/pedro/srt/utils/TimeUtils.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2023 pedroSG94. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pedro.srt.utils - -/** - * Created by pedro on 30/8/23. - */ -object TimeUtils { - - @JvmStatic - fun getCurrentTimeMicro(): Long = System.nanoTime() / 1000 - - @JvmStatic - fun getCurrentTimeMillis(): Long = System.currentTimeMillis() -} \ No newline at end of file diff --git a/srt/src/test/java/com/pedro/srt/MainDispatcherRule.kt b/srt/src/test/java/com/pedro/srt/MainDispatcherRule.kt deleted file mode 100644 index df3049c48..000000000 --- a/srt/src/test/java/com/pedro/srt/MainDispatcherRule.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.pedro.srt - -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestDispatcher -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain -import org.junit.rules.TestWatcher -import org.junit.runner.Description - -@OptIn(ExperimentalCoroutinesApi::class) -class MainDispatcherRule( - private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher() -) : TestWatcher() { - override fun starting(description: Description) { - Dispatchers.setMain(testDispatcher) - } - - override fun finished(description: Description) { - Dispatchers.resetMain() - } -} \ No newline at end of file diff --git a/srt/src/test/java/com/pedro/srt/mpeg2ts/PesTest.kt b/srt/src/test/java/com/pedro/srt/mpeg2ts/PesTest.kt index 9cda0bdc3..f564f3073 100644 --- a/srt/src/test/java/com/pedro/srt/mpeg2ts/PesTest.kt +++ b/srt/src/test/java/com/pedro/srt/mpeg2ts/PesTest.kt @@ -16,8 +16,8 @@ package com.pedro.srt.mpeg2ts +import com.pedro.common.TimeUtils import com.pedro.srt.Utils -import com.pedro.srt.utils.TimeUtils import kotlinx.coroutines.test.runTest import org.junit.Assert.assertArrayEquals import org.junit.Before diff --git a/srt/src/test/java/com/pedro/srt/mpeg2ts/PsiTest.kt b/srt/src/test/java/com/pedro/srt/mpeg2ts/PsiTest.kt index fa6320edc..c50562653 100644 --- a/srt/src/test/java/com/pedro/srt/mpeg2ts/PsiTest.kt +++ b/srt/src/test/java/com/pedro/srt/mpeg2ts/PsiTest.kt @@ -16,12 +16,12 @@ package com.pedro.srt.mpeg2ts +import com.pedro.common.TimeUtils import com.pedro.srt.Utils import com.pedro.srt.mpeg2ts.psi.Pat import com.pedro.srt.mpeg2ts.psi.Pmt import com.pedro.srt.mpeg2ts.psi.Sdt import com.pedro.srt.mpeg2ts.service.Mpeg2TsService -import com.pedro.srt.utils.TimeUtils import kotlinx.coroutines.test.runTest import org.junit.After import org.junit.Assert.assertArrayEquals diff --git a/srt/src/test/java/com/pedro/srt/utils/BitrateManagerTest.kt b/srt/src/test/java/com/pedro/srt/utils/BitrateManagerTest.kt deleted file mode 100644 index 66189e557..000000000 --- a/srt/src/test/java/com/pedro/srt/utils/BitrateManagerTest.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2021 pedroSG94. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.pedro.srt.utils - -import com.pedro.srt.MainDispatcherRule -import com.pedro.srt.Utils -import kotlinx.coroutines.delay -import kotlinx.coroutines.test.runTest -import org.junit.After -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith -import org.mockito.Mock -import org.mockito.Mockito -import org.mockito.junit.MockitoJUnitRunner -import org.mockito.kotlin.argumentCaptor -import org.mockito.kotlin.times -import org.mockito.kotlin.verify - - -/** - * Created by pedro on 9/9/23. - */ -@RunWith(MockitoJUnitRunner::class) -class BitrateManagerTest { - - @get:Rule - val mainDispatcherRule = MainDispatcherRule() - @Mock - private lateinit var connectCheckerSrt: ConnectCheckerSrt - private val timeUtilsMocked = Mockito.mockStatic(TimeUtils::class.java) - private var fakeTime = 7502849023L - - @Before - fun setup() { - timeUtilsMocked.`when`(TimeUtils::getCurrentTimeMillis).then { fakeTime } - } - - @After - fun teardown() { - fakeTime = 7502849023L - } - - @Test - fun `WHEN set multiple values THEN return total of values each second`() = runTest { - Utils.useStatics(listOf(timeUtilsMocked)) { - val bitrateManager = BitrateManager(connectCheckerSrt) - val fakeValues = arrayOf(100L, 200L, 300L, 400L, 500L) - var expectedResult = 0L - fakeValues.forEach { - bitrateManager.calculateBitrate(it) - expectedResult += it - } - fakeTime += 1000 - val value = 100L - bitrateManager.calculateBitrate(value) - expectedResult += value - val resultValue = argumentCaptor() - verify(connectCheckerSrt, times(1)).onNewBitrateSrt(resultValue.capture()) - val marginError = 20 - assertTrue(expectedResult - marginError <= resultValue.firstValue && resultValue.firstValue <= expectedResult + marginError) - } - } -} \ No newline at end of file