Fix potential memory leak in video player sample.
This commit is contained in:
@@ -316,7 +316,7 @@ private class Decoder(
|
|||||||
fun audioVideoSynced() = (audio?.isSynced() ?: true) || done()
|
fun audioVideoSynced() = (audio?.isSynced() ?: true) || done()
|
||||||
}
|
}
|
||||||
|
|
||||||
class DecoderWorker(val worker: Worker) : Disposable {
|
inline class DecoderWorker(val worker: Worker) : Disposable {
|
||||||
// This class must have no other state, but this worker object.
|
// This class must have no other state, but this worker object.
|
||||||
// All the real state must be stored on the worker's side.
|
// All the real state must be stored on the worker's side.
|
||||||
constructor() : this(Worker.start())
|
constructor() : this(Worker.start())
|
||||||
|
|||||||
@@ -65,15 +65,10 @@ class SDLAudio(val player: VideoPlayer) : DisposableContainer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only set in the audio thread
|
|
||||||
@ThreadLocal
|
|
||||||
private var decoder: DecoderWorker? = null
|
|
||||||
|
|
||||||
private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer<Uint8Var>?, length: Int) {
|
private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer<Uint8Var>?, length: Int) {
|
||||||
// This handler will be invoked in the audio thread, so reinit runtime.
|
// This handler will be invoked in the audio thread, so reinit runtime.
|
||||||
kotlin.native.initRuntimeIfNeeded()
|
kotlin.native.initRuntimeIfNeeded()
|
||||||
val decoder = decoder ?:
|
val decoder = DecoderWorker(Worker.fromCPointer(userdata))
|
||||||
DecoderWorker(Worker.fromCPointer(userdata)).also { decoder = it }
|
|
||||||
var outPosition = 0
|
var outPosition = 0
|
||||||
while (outPosition < length) {
|
while (outPosition < length) {
|
||||||
val frame = decoder.nextAudioFrame(length - outPosition)
|
val frame = decoder.nextAudioFrame(length - outPosition)
|
||||||
@@ -88,4 +83,4 @@ private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer<Uint8Var>?
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user