Put video export in a coroutine

hh
Laurent 5 years ago
parent f16defa86e
commit 7a64eb1e65
  1. 34
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/replayer/ReplayExportService.kt
  2. 2
      app/src/main/java/net/pokeranalytics/android/util/video/MMediaMuxer.kt

@ -3,11 +3,14 @@ package net.pokeranalytics.android.ui.modules.handhistory.replayer
import android.app.PendingIntent import android.app.PendingIntent
import android.app.Service import android.app.Service
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Binder import android.os.Binder
import android.os.IBinder import android.os.IBinder
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
@ -17,6 +20,7 @@ import net.pokeranalytics.android.util.extensions.findById
import net.pokeranalytics.android.util.video.MMediaMuxer import net.pokeranalytics.android.util.video.MMediaMuxer
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
import kotlin.coroutines.CoroutineContext
class ReplayExportService : Service() { class ReplayExportService : Service() {
@ -25,6 +29,9 @@ class ReplayExportService : Service() {
private val binder = LocalBinder() private val binder = LocalBinder()
private val coroutineContext: CoroutineContext
get() = Dispatchers.Main
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {
return binder return binder
} }
@ -37,19 +44,18 @@ class ReplayExportService : Service() {
fun export(handHistoryId: String) { fun export(handHistoryId: String) {
this@ReplayExportService.handHistoryId = handHistoryId this@ReplayExportService.handHistoryId = handHistoryId
export() startExport()
} }
private fun export() { private fun startExport() {
val realm = Realm.getDefaultInstance() GlobalScope.launch(coroutineContext) {
realm.findById<HandHistory>(this.handHistoryId)?.let { hh -> val c = GlobalScope.async {
startExport(hh)
} ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
} val realm = Realm.getDefaultInstance()
val handHistory = realm.findById<HandHistory>(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId")
private fun startExport(handHistory: HandHistory) { val context = this@ReplayExportService
val animator = ReplayerAnimator(handHistory, true) val animator = ReplayerAnimator(handHistory, true)
@ -59,12 +65,12 @@ class ReplayExportService : Service() {
val height = square val height = square
animator.setDimension(width, height) animator.setDimension(width, height)
TableDrawer.configurePaints(this, animator) TableDrawer.configurePaints(context, animator)
val muxer = MMediaMuxer() val muxer = MMediaMuxer()
muxer.Init(null, width.toInt(), height.toInt(), "hhVideo", "YES!") muxer.Init(null, width.toInt(), height.toInt(), "hhVideo", "YES!")
animator.frames(this) { bitmap, count -> animator.frames(context) { bitmap, count ->
try { try {
val byteArray = bitmap.toByteArray() val byteArray = bitmap.toByteArray()
@ -75,10 +81,16 @@ class ReplayExportService : Service() {
} }
realm.close()
muxer.CreateVideo { path -> muxer.CreateVideo { path ->
notifyUser(path) notifyUser(path)
} }
}
c.await()
}
} }
private fun notifyUser(path: String) { private fun notifyUser(path: String) {

@ -51,7 +51,7 @@ class MMediaMuxer {
ShowProgressBar() ShowProgressBar()
} }
private val aHandler = Handler() // private val aHandler = Handler()
fun AddFrame(byteFrame: ByteArray) { fun AddFrame(byteFrame: ByteArray) {
CheckDataListState() CheckDataListState()

Loading…
Cancel
Save