Skip to content

Commit

Permalink
Refactor and reorganize media player components; enhance logging with…
Browse files Browse the repository at this point in the history
… KotlinLogging
  • Loading branch information
kdroidFilter committed Jan 28, 2025
1 parent 8ae98fe commit 053bcb1
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 143 deletions.
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gst1JavaCore = "1.4.0"
gst1JavaSwing = "0.9.0"
kotlin = "2.1.10"
agp = "8.7.3"
kotlinLogging = "7.0.3"
kotlinx-coroutines = "1.10.1"
kotlinxBrowserWasmJs = "0.3"
kotlinxDatetime = "0.6.1"
Expand All @@ -14,6 +15,7 @@ compose = "1.7.3"
androidx-activityCompose = "1.10.0"
media3Exoplayer = "1.5.1"
jna = "5.16.0"
slf4jSimple = "2.0.16"


[libraries]
Expand All @@ -25,6 +27,7 @@ filekit-core = { module = "io.github.vinceglb:filekit-core", version.ref = "file
filekit-dialog-compose = { module = "io.github.vinceglb:filekit-dialog-compose", version.ref = "filekit" }
gst1-java-core = { module = "org.freedesktop.gstreamer:gst1-java-core", version.ref = "gst1JavaCore" }
gst1-java-swing = { module = "org.freedesktop.gstreamer:gst1-java-swing", version.ref = "gst1JavaSwing" }
kotlin-logging = { module = "io.github.oshai:kotlin-logging", version.ref = "kotlinLogging" }
kotlinx-browser-wasm-js = { module = "org.jetbrains.kotlinx:kotlinx-browser-wasm-js", version.ref = "kotlinxBrowserWasmJs" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
Expand All @@ -43,6 +46,7 @@ ktor-client-winhttp = { module = "io.ktor:ktor-client-winhttp", version.ref = "k
androidx-activityCompose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
jna = { module = "net.java.dev.jna:jna", version.ref = "jna" }
jna-platform = { module = "net.java.dev.jna:jna-platform", version.ref = "jna" }
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4jSimple" }

[plugins]

Expand Down
6 changes: 2 additions & 4 deletions mediaplayer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ kotlin {
implementation(libs.kotlinx.coroutines.test)
api(libs.filekit.core)
implementation(libs.kotlinx.datetime)

implementation(libs.kotlin.logging)
}

commonTest.dependencies {
Expand All @@ -81,6 +81,7 @@ kotlin {
implementation(libs.gst1.java.swing)
implementation(libs.jna)
implementation(libs.jna.platform)
implementation(libs.slf4j.simple)

compileOnly("org.openjfx:javafx-base:${javafxVersion}:${fxClassifier}")
compileOnly("org.openjfx:javafx-graphics:${javafxVersion}:${fxClassifier}")
Expand All @@ -93,9 +94,6 @@ kotlin {

wasmJsMain.dependencies {
implementation(libs.kotlinx.browser.wasm.js)
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-wasm-js:1.10.1")
// implementation(npm("audio-context", "1.0.3"))

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@ class AudioLevelProcessor : BaseAudioProcessor() {
var rightSum = 0.0
var sampleCount = 0

// Copier le buffer pour ne pas affecter la position originale
// Copy the buffer so as not to affect the original position
val buffer = inputBuffer.duplicate()

while (buffer.remaining() >= 2) {
// Lecture des échantillons 16-bit
// Reading 16-bit samples
val sample = buffer.short / Short.MAX_VALUE.toFloat()

if (channelCount >= 2) {
// Stéréo
// Stereo
if (sampleCount % 2 == 0) {
leftSum += abs(sample.toDouble())
} else {
rightSum += abs(sample.toDouble())
}
} else {
// Mono - même valeur pour les deux canaux
// Mono - same value for both channels
leftSum += abs(sample.toDouble())
rightSum += abs(sample.toDouble())
}
sampleCount++
}

// Calculer RMS et convertir en dB
// Calculate RMS and convert to dB
val samplesPerChannel = if (channelCount >= 2) sampleCount / 2 else sampleCount
val leftRms = if (samplesPerChannel > 0) sqrt(leftSum / samplesPerChannel) else 0.0
val rightRms = if (samplesPerChannel > 0) sqrt(rightSum / samplesPerChannel) else 0.0

// Convertir en pourcentage (0-100)
// Convert to percentage (0-100)
val leftLevel = convertToPercentage(leftRms)
val rightLevel = convertToPercentage(rightRms)

onAudioLevelUpdate?.invoke(leftLevel, rightLevel)

// Passer le buffer original tel quel
// Pass the original buffer as is
val output = replaceOutputBuffer(inputBuffer.remaining())
output.put(inputBuffer)
output.flip()
Expand All @@ -75,7 +75,7 @@ class AudioLevelProcessor : BaseAudioProcessor() {
private fun convertToPercentage(rms: Double): Float {
if (rms <= 0) return 0f
val db = 20 * log10(rms)
// Convertir de -60dB..0dB à 0..100%
// Convert from -60dB..0dB to 0..100%
return ((db + 60) / 60 * 100).toFloat().coerceIn(0f, 100f)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.media3.exoplayer.audio.AudioSink
import androidx.media3.exoplayer.audio.DefaultAudioSink
import com.kdroid.androidcontextprovider.ContextProvider
import io.github.kdroidfilter.composemediaplayer.util.formatTime
import io.github.kdroidfilter.composemediaplayer.util.logger
import io.github.vinceglb.filekit.AndroidFile
import io.github.vinceglb.filekit.PlatformFile
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -222,7 +223,7 @@ actual open class VideoPlayerState {
player.play()
_hasMedia = true // Set to true when media is loaded
} catch (e: Exception) {
println("Error opening media: ${e.message}")
logger.debug{"Error opening media: ${e.message}"}
_isPlaying = false
_hasMedia = false // Set to false on error
_error = VideoPlayerError.SourceError("Failed to load media: ${e.message}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.kdroidfilter.composemediaplayer.util

import io.github.oshai.kotlinlogging.KotlinLogging

internal val logger = KotlinLogging.logger {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.github.kdroidfilter.composemediaplayer.VideoMetadata
import io.github.kdroidfilter.composemediaplayer.VideoPlayerError
import io.github.kdroidfilter.composemediaplayer.util.DEFAULT_ASPECT_RATIO
import io.github.kdroidfilter.composemediaplayer.util.formatTime
import io.github.kdroidfilter.composemediaplayer.util.logger
import javafx.application.Platform
import javafx.scene.media.Media
import javafx.scene.media.MediaPlayer
Expand Down Expand Up @@ -98,7 +99,7 @@ class JavaFxVideoPlayerState : PlatformVideoPlayerState {

// Créer le nouveau MediaPlayer
val fileOrUrl = if (uri.startsWith("http")) uri else File(uri).toURI().toString()
println("Opening media: $fileOrUrl")
logger.debug { "Opening media: $fileOrUrl" }

val media = Media(fileOrUrl)
MediaPlayer(media).also { player ->
Expand Down Expand Up @@ -244,7 +245,7 @@ class JavaFxVideoPlayerState : PlatformVideoPlayerState {

private fun setupStatusListener(player: MediaPlayer) {
player.statusProperty().addListener { _, _, newStatus ->
println("Player status: $newStatus")
logger.debug { "Player status: $newStatus" }
when (newStatus) {
MediaPlayer.Status.PLAYING -> {
_isPlaying = true
Expand Down Expand Up @@ -283,7 +284,7 @@ class JavaFxVideoPlayerState : PlatformVideoPlayerState {

private fun setupEventHandlers(player: MediaPlayer) {
player.setOnReady {
println("Player ready")
logger.debug { "Player ready" }
_duration = player.totalDuration?.toSeconds() ?: 0.0
_isLoading = false

Expand Down Expand Up @@ -328,7 +329,7 @@ class JavaFxVideoPlayerState : PlatformVideoPlayerState {
// Finally clear the reference
mediaPlayer = null
resetStates()
println("Media stopped successfully")
logger.debug { "Media stopped successfully" }
} catch (e: Exception) {
handleError("Error stopping media", e)
}
Expand Down Expand Up @@ -366,7 +367,7 @@ class JavaFxVideoPlayerState : PlatformVideoPlayerState {
}

private fun handleError(message: String, error: Throwable?) {
println("$message: ${error?.message}")
logger.debug { "$message: ${error?.message}" }
error?.printStackTrace()
_isPlaying = false
_isLoading = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.sun.jna.ptr.FloatByReference
import com.sun.jna.ptr.LongByReference
import com.sun.jna.win32.StdCallLibrary

interface MediaPlayerLib : StdCallLibrary {
internal interface MediaPlayerLib : StdCallLibrary {
companion object {
// Events constants
const val MP_EVENT_MEDIAITEM_CREATED = 1
Expand Down
Loading

0 comments on commit 053bcb1

Please sign in to comment.