From 8a83b37673485754d540f861adabb7f41c237458 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Sun, 6 Dec 2020 18:20:39 +0300 Subject: [PATCH] Videoplayer sample: format, drop unused imports --- .../src/videoPlayerMain/kotlin/DecoderWorker.kt | 13 ++++++------- .../videoplayer/src/videoPlayerMain/kotlin/Queue.kt | 2 +- .../src/videoPlayerMain/kotlin/SDLAudio.kt | 4 ++-- .../src/videoPlayerMain/kotlin/SDLInput.kt | 2 +- .../src/videoPlayerMain/kotlin/SDLVideo.kt | 5 +---- .../src/videoPlayerMain/kotlin/VideoPlayer.kt | 7 +++---- 6 files changed, 14 insertions(+), 19 deletions(-) diff --git a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/DecoderWorker.kt b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/DecoderWorker.kt index 339a1686f06..d7fd627e20b 100644 --- a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/DecoderWorker.kt +++ b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/DecoderWorker.kt @@ -153,7 +153,6 @@ private class VideoDecoder( videoQueue.push(VideoFrame(buffer, scaledVideoFrame.linesize[0], ts)) } } - } private fun SampleFormat.toAVSampleFormat(): AVSampleFormat? = when (this) { @@ -191,14 +190,14 @@ private class AudioDecoder( private val maxAudioFrames = 5 init { - with (resampledAudioFrame) { + with(resampledAudioFrame) { channels = output.channels sample_rate = output.sampleRate format = output.sampleFormat channel_layout = output.channelLayout.convert() } - with (audioCodecContext) { + with(audioCodecContext) { setResampleOpt("in_channel_layout", channel_layout.convert()) setResampleOpt("out_channel_layout", output.channelLayout) setResampleOpt("in_sample_rate", sample_rate) @@ -226,12 +225,12 @@ private class AudioDecoder( fun nextFrame(size: Int): AudioFrame? { val frame = audioQueue.peek() ?: return null val realSize = if (frame.position + size > frame.size) frame.size - frame.position else size - if (frame.position + realSize == frame.size) { - return audioQueue.pop() + return if (frame.position + realSize == frame.size) { + audioQueue.pop() } else { val result = AudioFrame(av_buffer_ref(frame.buffer)!!, frame.position, frame.size, frame.timeStamp) frame.position += realSize - return result + result } } @@ -241,7 +240,7 @@ private class AudioDecoder( if (frameFinished.value != 0) { // Put audio frame to decoder's queue. 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 buffer = av_buffer_alloc(audioFrameSize)!! val ts = av_frame_get_best_effort_timestamp(audioFrame.ptr) * diff --git a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/Queue.kt b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/Queue.kt index b8ec33e9563..9c80621d2c2 100644 --- a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/Queue.kt +++ b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/Queue.kt @@ -6,7 +6,7 @@ package sample.videoplayer class Queue(val maxSize: Int) { - private val array = kotlin.arrayOfNulls(maxSize) + private val array = arrayOfNulls(maxSize) private var head = 0 private var tail = 0 diff --git a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLAudio.kt b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLAudio.kt index b06bae5a673..11d42ec2181 100644 --- a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLAudio.kt +++ b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLAudio.kt @@ -23,7 +23,7 @@ private fun SampleFormat.toSDLFormat(): SDL_AudioFormat? = when (this) { SampleFormat.INVALID -> null } -class SDLAudio(val player: VideoPlayer) : DisposableContainer() { +class SDLAudio(private val player: VideoPlayer) : DisposableContainer() { private var state = State.STOPPED fun start(audio: AudioOutput) { @@ -67,7 +67,7 @@ class SDLAudio(val player: VideoPlayer) : DisposableContainer() { private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer?, length: Int) { // This handler will be invoked in the audio thread, so reinit runtime. - kotlin.native.initRuntimeIfNeeded() + initRuntimeIfNeeded() val decoder = DecoderWorker(Worker.fromCPointer(userdata)) var outPosition = 0 while (outPosition < length) { diff --git a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLInput.kt b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLInput.kt index 8b245c08a1f..18fee6ea0aa 100644 --- a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLInput.kt +++ b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLInput.kt @@ -8,7 +8,7 @@ package sample.videoplayer import kotlinx.cinterop.* import sdl.* -class SDLInput(val player: VideoPlayer) : DisposableContainer() { +class SDLInput(private val player: VideoPlayer) : DisposableContainer() { private val event = arena.alloc().ptr fun check() { diff --git a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLVideo.kt b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLVideo.kt index 33c1f04578c..96f970ada51 100644 --- a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLVideo.kt +++ b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/SDLVideo.kt @@ -5,9 +5,6 @@ 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 sdl.* @@ -72,7 +69,7 @@ class SDLRendererWindow(windowPos: Dimensions, videoSize: Dimensions) : Disposab SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), 0, videoSize.w, videoSize.h), ::SDL_DestroyTexture) private val rect = sdlDisposable("calloc(SDL_Rect)", - SDL_calloc(1, SDL_Rect.size.convert()), ::SDL_free) + SDL_calloc(1, sizeOf().convert()), ::SDL_free) .reinterpret() init { diff --git a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt index 75ca8e09c4a..9c4fd6f0779 100644 --- a/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt +++ b/kotlin-native/samples/videoplayer/src/videoPlayerMain/kotlin/VideoPlayer.kt @@ -6,7 +6,6 @@ package sample.videoplayer import ffmpeg.* -import kotlin.system.* import kotlinx.cinterop.* import platform.posix.* import kotlinx.cli.* @@ -32,12 +31,12 @@ enum class PlayMode { 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 video = disposable { SDLVideo() } private val audio = disposable { SDLAudio(this) } private val input = disposable { SDLInput(this) } - private val now = arena.alloc().ptr + private val now = arena.alloc().ptr private var state = State.STOPPED @@ -63,7 +62,7 @@ class VideoPlayer(val requestedSize: Dimensions?) : DisposableContainer() { } 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 }