Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.peerless2012.ass.AssTexType
import io.github.peerless2012.ass.AssVideoColorSpace
import java.util.concurrent.ExecutorCompletionService
import java.util.concurrent.Executors
import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.TimeUnit

/**
Expand Down Expand Up @@ -67,7 +68,11 @@ class AssExecutor(private val render: AssRender) {
task.type = type
task.videoColorSpace = videoColorSpace
// execute render task
executor.execute(task)
try {
executor.execute(task)
} catch (_: RejectedExecutionException) {
// executor already shut down (e.g. view detached), drop this frame
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AssSubtitleCanvasView : View, AssSubtitleRender {
xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)
}

// Written on the main thread, read from the player thread in requestRender.
@Volatile
private var assExecutor: AssExecutor? = null

private var assFrame: AssFrame? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class AssSubtitleTextureView : TextureView, AssSubtitleRender, TextureView.Surfa
fun onSurfaceDestroyed()
}

@Volatile
private var renderThread: AssRenderThread? = null

override fun requestRender(timestampNanos: Long) {
Expand Down Expand Up @@ -107,7 +108,10 @@ class AssSubtitleTextureView : TextureView, AssSubtitleRender, TextureView.Surfa
private val MSG_SURFACE_SIZE_CHANGED = 3
private val MSG_RELEASE = 4

private lateinit var handler: Handler
// Null until the looper is ready. Calls may arrive from other threads
// (e.g. the player thread) before start() finishes, so they must be ignored.
@Volatile
private var handler: Handler? = null

private var eglDisplay: EGLDisplay = EGL14.EGL_NO_DISPLAY
private var eglContext: EGLContext = EGL14.EGL_NO_CONTEXT
Expand All @@ -116,23 +120,28 @@ class AssSubtitleTextureView : TextureView, AssSubtitleRender, TextureView.Surfa

override fun start() {
super.start()
handler = Handler(looper, this)
handler.sendEmptyMessage(MSG_INIT)
// getLooper() blocks until the looper is ready
val h = Handler(looper, this)
// Queue MSG_INIT first, then publish the handler, so any MSG_DRAW
// from other threads is always queued after MSG_INIT.
h.sendEmptyMessage(MSG_INIT)
handler = h
}

fun requestRender(timestampNanos: Long) {
handler.removeMessages(MSG_DRAW)
handler.obtainMessage(MSG_DRAW, timestampNanos).sendToTarget()
val h = handler ?: return
h.removeMessages(MSG_DRAW)
h.obtainMessage(MSG_DRAW, timestampNanos).sendToTarget()
}

fun onSurfaceSizeChanged(width: Int, height: Int) {
this.width = width
this.height = height
handler.sendEmptyMessage(MSG_SURFACE_SIZE_CHANGED)
handler?.sendEmptyMessage(MSG_SURFACE_SIZE_CHANGED)
}

fun release() {
handler.sendEmptyMessage(MSG_RELEASE)
handler?.sendEmptyMessage(MSG_RELEASE)
}

override fun handleMessage(msg: Message): Boolean {
Expand Down
Loading