Videoplayer sample: format, drop unused imports
This commit is contained in:
committed by
Stanislav Erokhin
parent
0e0c8e274d
commit
8a83b37673
@@ -153,7 +153,6 @@ private class VideoDecoder(
|
|||||||
videoQueue.push(VideoFrame(buffer, scaledVideoFrame.linesize[0], ts))
|
videoQueue.push(VideoFrame(buffer, scaledVideoFrame.linesize[0], ts))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun SampleFormat.toAVSampleFormat(): AVSampleFormat? = when (this) {
|
private fun SampleFormat.toAVSampleFormat(): AVSampleFormat? = when (this) {
|
||||||
@@ -191,14 +190,14 @@ private class AudioDecoder(
|
|||||||
private val maxAudioFrames = 5
|
private val maxAudioFrames = 5
|
||||||
|
|
||||||
init {
|
init {
|
||||||
with (resampledAudioFrame) {
|
with(resampledAudioFrame) {
|
||||||
channels = output.channels
|
channels = output.channels
|
||||||
sample_rate = output.sampleRate
|
sample_rate = output.sampleRate
|
||||||
format = output.sampleFormat
|
format = output.sampleFormat
|
||||||
channel_layout = output.channelLayout.convert()
|
channel_layout = output.channelLayout.convert()
|
||||||
}
|
}
|
||||||
|
|
||||||
with (audioCodecContext) {
|
with(audioCodecContext) {
|
||||||
setResampleOpt("in_channel_layout", channel_layout.convert())
|
setResampleOpt("in_channel_layout", channel_layout.convert())
|
||||||
setResampleOpt("out_channel_layout", output.channelLayout)
|
setResampleOpt("out_channel_layout", output.channelLayout)
|
||||||
setResampleOpt("in_sample_rate", sample_rate)
|
setResampleOpt("in_sample_rate", sample_rate)
|
||||||
@@ -226,12 +225,12 @@ private class AudioDecoder(
|
|||||||
fun nextFrame(size: Int): AudioFrame? {
|
fun nextFrame(size: Int): AudioFrame? {
|
||||||
val frame = audioQueue.peek() ?: return null
|
val frame = audioQueue.peek() ?: return null
|
||||||
val realSize = if (frame.position + size > frame.size) frame.size - frame.position else size
|
val realSize = if (frame.position + size > frame.size) frame.size - frame.position else size
|
||||||
if (frame.position + realSize == frame.size) {
|
return if (frame.position + realSize == frame.size) {
|
||||||
return audioQueue.pop()
|
audioQueue.pop()
|
||||||
} else {
|
} else {
|
||||||
val result = AudioFrame(av_buffer_ref(frame.buffer)!!, frame.position, frame.size, frame.timeStamp)
|
val result = AudioFrame(av_buffer_ref(frame.buffer)!!, frame.position, frame.size, frame.timeStamp)
|
||||||
frame.position += realSize
|
frame.position += realSize
|
||||||
return result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +240,7 @@ private class AudioDecoder(
|
|||||||
if (frameFinished.value != 0) {
|
if (frameFinished.value != 0) {
|
||||||
// Put audio frame to decoder's queue.
|
// Put audio frame to decoder's queue.
|
||||||
swr_convert_frame(resampleContext, resampledAudioFrame.ptr, audioFrame.ptr).checkAVError()
|
swr_convert_frame(resampleContext, resampledAudioFrame.ptr, audioFrame.ptr).checkAVError()
|
||||||
with (resampledAudioFrame) {
|
with(resampledAudioFrame) {
|
||||||
val audioFrameSize = av_samples_get_buffer_size(null, channels, nb_samples, format, 1)
|
val audioFrameSize = av_samples_get_buffer_size(null, channels, nb_samples, format, 1)
|
||||||
val buffer = av_buffer_alloc(audioFrameSize)!!
|
val buffer = av_buffer_alloc(audioFrameSize)!!
|
||||||
val ts = av_frame_get_best_effort_timestamp(audioFrame.ptr) *
|
val ts = av_frame_get_best_effort_timestamp(audioFrame.ptr) *
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
package sample.videoplayer
|
package sample.videoplayer
|
||||||
|
|
||||||
class Queue<T>(val maxSize: Int) {
|
class Queue<T>(val maxSize: Int) {
|
||||||
private val array = kotlin.arrayOfNulls<Any>(maxSize)
|
private val array = arrayOfNulls<Any>(maxSize)
|
||||||
private var head = 0
|
private var head = 0
|
||||||
private var tail = 0
|
private var tail = 0
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ private fun SampleFormat.toSDLFormat(): SDL_AudioFormat? = when (this) {
|
|||||||
SampleFormat.INVALID -> null
|
SampleFormat.INVALID -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
class SDLAudio(val player: VideoPlayer) : DisposableContainer() {
|
class SDLAudio(private val player: VideoPlayer) : DisposableContainer() {
|
||||||
private var state = State.STOPPED
|
private var state = State.STOPPED
|
||||||
|
|
||||||
fun start(audio: AudioOutput) {
|
fun start(audio: AudioOutput) {
|
||||||
@@ -67,7 +67,7 @@ class SDLAudio(val player: VideoPlayer) : DisposableContainer() {
|
|||||||
|
|
||||||
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()
|
initRuntimeIfNeeded()
|
||||||
val decoder = DecoderWorker(Worker.fromCPointer(userdata))
|
val decoder = DecoderWorker(Worker.fromCPointer(userdata))
|
||||||
var outPosition = 0
|
var outPosition = 0
|
||||||
while (outPosition < length) {
|
while (outPosition < length) {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ package sample.videoplayer
|
|||||||
import kotlinx.cinterop.*
|
import kotlinx.cinterop.*
|
||||||
import sdl.*
|
import sdl.*
|
||||||
|
|
||||||
class SDLInput(val player: VideoPlayer) : DisposableContainer() {
|
class SDLInput(private val player: VideoPlayer) : DisposableContainer() {
|
||||||
private val event = arena.alloc<SDL_Event>().ptr
|
private val event = arena.alloc<SDL_Event>().ptr
|
||||||
|
|
||||||
fun check() {
|
fun check() {
|
||||||
|
|||||||
@@ -5,9 +5,6 @@
|
|||||||
|
|
||||||
package sample.videoplayer
|
package sample.videoplayer
|
||||||
|
|
||||||
import ffmpeg.AV_PIX_FMT_NONE
|
|
||||||
import ffmpeg.AV_PIX_FMT_RGB24
|
|
||||||
import ffmpeg.AV_PIX_FMT_RGB32
|
|
||||||
import kotlinx.cinterop.*
|
import kotlinx.cinterop.*
|
||||||
import sdl.*
|
import sdl.*
|
||||||
|
|
||||||
@@ -72,7 +69,7 @@ class SDLRendererWindow(windowPos: Dimensions, videoSize: Dimensions) : Disposab
|
|||||||
SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), 0, videoSize.w, videoSize.h),
|
SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), 0, videoSize.w, videoSize.h),
|
||||||
::SDL_DestroyTexture)
|
::SDL_DestroyTexture)
|
||||||
private val rect = sdlDisposable("calloc(SDL_Rect)",
|
private val rect = sdlDisposable("calloc(SDL_Rect)",
|
||||||
SDL_calloc(1, SDL_Rect.size.convert()), ::SDL_free)
|
SDL_calloc(1, sizeOf<SDL_Rect>().convert()), ::SDL_free)
|
||||||
.reinterpret<SDL_Rect>()
|
.reinterpret<SDL_Rect>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
package sample.videoplayer
|
package sample.videoplayer
|
||||||
|
|
||||||
import ffmpeg.*
|
import ffmpeg.*
|
||||||
import kotlin.system.*
|
|
||||||
import kotlinx.cinterop.*
|
import kotlinx.cinterop.*
|
||||||
import platform.posix.*
|
import platform.posix.*
|
||||||
import kotlinx.cli.*
|
import kotlinx.cli.*
|
||||||
@@ -32,12 +31,12 @@ enum class PlayMode {
|
|||||||
val useAudio: Boolean get() = this != VIDEO
|
val useAudio: Boolean get() = this != VIDEO
|
||||||
}
|
}
|
||||||
|
|
||||||
class VideoPlayer(val requestedSize: Dimensions?) : DisposableContainer() {
|
class VideoPlayer(private val requestedSize: Dimensions?) : DisposableContainer() {
|
||||||
private val decoder = disposable { DecoderWorker() }
|
private val decoder = disposable { DecoderWorker() }
|
||||||
private val video = disposable { SDLVideo() }
|
private val video = disposable { SDLVideo() }
|
||||||
private val audio = disposable { SDLAudio(this) }
|
private val audio = disposable { SDLAudio(this) }
|
||||||
private val input = disposable { SDLInput(this) }
|
private val input = disposable { SDLInput(this) }
|
||||||
private val now = arena.alloc<platform.posix.timespec>().ptr
|
private val now = arena.alloc<timespec>().ptr
|
||||||
|
|
||||||
private var state = State.STOPPED
|
private var state = State.STOPPED
|
||||||
|
|
||||||
@@ -63,7 +62,7 @@ class VideoPlayer(val requestedSize: Dimensions?) : DisposableContainer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getTime(): Double {
|
private fun getTime(): Double {
|
||||||
clock_gettime(platform.posix.CLOCK_MONOTONIC, now)
|
clock_gettime(CLOCK_MONOTONIC, now)
|
||||||
return now.pointed.tv_sec + now.pointed.tv_nsec / 1e9
|
return now.pointed.tv_sec + now.pointed.tv_nsec / 1e9
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user