-
-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1594 from pedroSG94/feature/packtizer-async
Feature/packtizer async
- Loading branch information
Showing
58 changed files
with
752 additions
and
1,103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<manifest> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
common/src/main/java/com/pedro/common/base/BaseSender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.pedro.common.base | ||
|
||
import android.util.Log | ||
import com.pedro.common.BitrateManager | ||
import com.pedro.common.ConnectChecker | ||
import com.pedro.common.frame.MediaFrame | ||
import com.pedro.common.trySend | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.cancelAndJoin | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import java.nio.ByteBuffer | ||
import java.util.concurrent.BlockingQueue | ||
import java.util.concurrent.LinkedBlockingQueue | ||
|
||
abstract class BaseSender( | ||
protected val connectChecker: ConnectChecker, | ||
protected val TAG: String | ||
) { | ||
|
||
@Volatile | ||
protected var running = false | ||
private var cacheSize = 200 | ||
@Volatile | ||
protected var queue: BlockingQueue<MediaFrame> = LinkedBlockingQueue(cacheSize) | ||
protected var audioFramesSent: Long = 0 | ||
protected var videoFramesSent: Long = 0 | ||
var droppedAudioFrames: Long = 0 | ||
protected set | ||
var droppedVideoFrames: Long = 0 | ||
protected set | ||
protected val bitrateManager: BitrateManager = BitrateManager(connectChecker) | ||
protected var isEnableLogs = true | ||
private var job: Job? = null | ||
protected val scope = CoroutineScope(Dispatchers.IO) | ||
@Volatile | ||
protected var bytesSend = 0L | ||
|
||
abstract fun setVideoInfo(sps: ByteBuffer, pps: ByteBuffer?, vps: ByteBuffer?) | ||
abstract fun setAudioInfo(sampleRate: Int, isStereo: Boolean) | ||
protected abstract suspend fun onRun() | ||
protected abstract suspend fun stopImp(clear: Boolean = true) | ||
|
||
fun sendMediaFrame(mediaFrame: MediaFrame) { | ||
if (running && !queue.trySend(mediaFrame)) { | ||
when (mediaFrame.type) { | ||
MediaFrame.Type.VIDEO -> { | ||
Log.i(TAG, "Video frame discarded") | ||
droppedVideoFrames++ | ||
} | ||
MediaFrame.Type.AUDIO -> { | ||
Log.i(TAG, "Audio frame discarded") | ||
droppedAudioFrames++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun start() { | ||
bitrateManager.reset() | ||
queue.clear() | ||
running = true | ||
scope.launch { | ||
val bitrateTask = async { | ||
while (scope.isActive && running) { | ||
//bytes to bits | ||
bitrateManager.calculateBitrate(bytesSend * 8) | ||
bytesSend = 0 | ||
delay(timeMillis = 1000) | ||
} | ||
} | ||
onRun() | ||
} | ||
} | ||
|
||
suspend fun stop(clear: Boolean = true) { | ||
running = false | ||
stopImp(clear) | ||
resetSentAudioFrames() | ||
resetSentVideoFrames() | ||
resetDroppedAudioFrames() | ||
resetDroppedVideoFrames() | ||
job?.cancelAndJoin() | ||
job = null | ||
queue.clear() | ||
} | ||
|
||
@Throws(IllegalArgumentException::class) | ||
fun hasCongestion(percentUsed: Float = 20f): Boolean { | ||
if (percentUsed < 0 || percentUsed > 100) throw IllegalArgumentException("the value must be in range 0 to 100") | ||
val size = queue.size.toFloat() | ||
val remaining = queue.remainingCapacity().toFloat() | ||
val capacity = size + remaining | ||
return size >= capacity * (percentUsed / 100f) | ||
} | ||
|
||
fun resizeCache(newSize: Int) { | ||
if (newSize < queue.size - queue.remainingCapacity()) { | ||
throw RuntimeException("Can't fit current cache inside new cache size") | ||
} | ||
val tempQueue: BlockingQueue<MediaFrame> = LinkedBlockingQueue(newSize) | ||
queue.drainTo(tempQueue) | ||
queue = tempQueue | ||
} | ||
|
||
fun getCacheSize(): Int = cacheSize | ||
|
||
fun getItemsInCache(): Int = queue.size | ||
|
||
fun clearCache() { | ||
queue.clear() | ||
} | ||
|
||
fun getSentAudioFrames(): Long = audioFramesSent | ||
|
||
fun getSentVideoFrames(): Long = videoFramesSent | ||
|
||
fun resetSentAudioFrames() { | ||
audioFramesSent = 0 | ||
} | ||
|
||
fun resetSentVideoFrames() { | ||
videoFramesSent = 0 | ||
} | ||
|
||
fun resetDroppedAudioFrames() { | ||
droppedAudioFrames = 0 | ||
} | ||
|
||
fun resetDroppedVideoFrames() { | ||
droppedVideoFrames = 0 | ||
} | ||
|
||
fun setLogs(enable: Boolean) { | ||
isEnableLogs = enable | ||
} | ||
|
||
fun setBitrateExponentialFactor(factor: Float) { | ||
bitrateManager.exponentialFactor = factor | ||
} | ||
|
||
fun getBitrateExponentialFactor() = bitrateManager.exponentialFactor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.pedro.common.frame | ||
|
||
import java.nio.ByteBuffer | ||
|
||
data class MediaFrame( | ||
val data: ByteBuffer, | ||
val info: Info, | ||
val type: Type | ||
) { | ||
data class Info( | ||
val offset: Int, | ||
val size: Int, | ||
val timestamp: Long, | ||
val isKeyFrame: Boolean | ||
) | ||
|
||
enum class Type { | ||
VIDEO, AUDIO | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.