@@ -14,6 +14,7 @@ import kotlinx.coroutines.delay
1414import kotlinx.coroutines.isActive
1515import kotlinx.coroutines.launch
1616import java.nio.ByteBuffer
17+ import java.util.concurrent.atomic.AtomicLong
1718
1819abstract class BaseSender (
1920 protected val connectChecker : ConnectChecker ,
@@ -23,23 +24,21 @@ abstract class BaseSender(
2324 @Volatile
2425 protected var running = false
2526 private var cacheSize = 400
26- @Volatile
27- protected var queue = StreamBlockingQueue (cacheSize)
28- protected var audioFramesSent : Long = 0
29- protected var videoFramesSent : Long = 0
30- var droppedAudioFrames : Long = 0
31- protected set
32- var droppedVideoFrames: Long = 0
33- protected set
27+
28+ protected val queue = StreamBlockingQueue (cacheSize)
29+
30+ protected val audioFramesSent = AtomicLong ( 0 )
31+ protected val videoFramesSent = AtomicLong ( 0 )
32+ private val droppedAudioFrames = AtomicLong ( 0 )
33+ private val droppedVideoFrames = AtomicLong ( 0 )
34+
3435 private val bitrateManager: BitrateManager = BitrateManager (connectChecker)
3536 protected var isEnableLogs = true
3637 private var job: Job ? = null
3738 protected val scope = CoroutineScope (Dispatchers .IO )
38- @Volatile
39- var bytesSend = 0L
40- protected set
41- @Volatile
42- protected var bytesSendPerSecond = 0L
39+
40+ protected val bytesSend = AtomicLong (0 )
41+ protected val bytesSendPerSecond = AtomicLong (0 )
4342
4443 abstract fun setVideoInfo (sps : ByteBuffer , pps : ByteBuffer ? , vps : ByteBuffer ? )
4544 abstract fun setAudioInfo (sampleRate : Int , isStereo : Boolean )
@@ -51,11 +50,11 @@ abstract class BaseSender(
5150 when (mediaFrame.type) {
5251 MediaFrame .Type .VIDEO -> {
5352 Log .i(TAG , " Video frame discarded" )
54- droppedVideoFrames++
53+ droppedVideoFrames.incrementAndGet()
5554 }
5655 MediaFrame .Type .AUDIO -> {
5756 Log .i(TAG , " Audio frame discarded" )
58- droppedAudioFrames++
57+ droppedAudioFrames.incrementAndGet()
5958 }
6059 }
6160 }
@@ -69,8 +68,8 @@ abstract class BaseSender(
6968 val bitrateTask = async {
7069 while (scope.isActive && running) {
7170 // bytes to bits
72- bitrateManager.calculateBitrate(bytesSendPerSecond * 8 )
73- bytesSendPerSecond = 0
71+ bitrateManager.calculateBitrate(bytesSendPerSecond.get() * 8 )
72+ bytesSendPerSecond.set( 0 )
7473 delay(timeMillis = 1000 )
7574 }
7675 }
@@ -104,9 +103,7 @@ abstract class BaseSender(
104103 if (newSize < queue.getSize() - queue.remainingCapacity()) {
105104 throw RuntimeException (" Can't fit current cache inside new cache size" )
106105 }
107- val tempQueue = StreamBlockingQueue (newSize)
108- queue.drainTo(tempQueue)
109- queue = tempQueue
106+ queue.size = newSize
110107 }
111108
112109 fun getCacheSize (): Int = cacheSize
@@ -117,24 +114,30 @@ abstract class BaseSender(
117114 queue.clear()
118115 }
119116
120- fun getSentAudioFrames (): Long = audioFramesSent
117+ fun getSentAudioFrames (): Long = audioFramesSent.get()
118+
119+ fun getSentVideoFrames (): Long = videoFramesSent.get()
120+
121+ fun getDroppedAudioFrames (): Long = droppedAudioFrames.get()
122+
123+ fun getDroppedVideoFrames (): Long = droppedVideoFrames.get()
121124
122- fun getSentVideoFrames (): Long = videoFramesSent
125+ fun getBytesSend (): Long = bytesSend.get()
123126
124127 fun resetSentAudioFrames () {
125- audioFramesSent = 0
128+ audioFramesSent.set( 0 )
126129 }
127130
128131 fun resetSentVideoFrames () {
129- videoFramesSent = 0
132+ videoFramesSent.set( 0 )
130133 }
131134
132135 fun resetDroppedAudioFrames () {
133- droppedAudioFrames = 0
136+ droppedAudioFrames.set( 0 )
134137 }
135138
136139 fun resetDroppedVideoFrames () {
137- droppedVideoFrames = 0
140+ droppedVideoFrames.set( 0 )
138141 }
139142
140143 fun setLogs (enable : Boolean ) {
@@ -152,6 +155,6 @@ abstract class BaseSender(
152155 }
153156
154157 fun resetBytesSend () {
155- bytesSend = 0
158+ bytesSend.set( 0 )
156159 }
157160}
0 commit comments