From 22d4b6e50cd8bd2af1921fb73838325921090677 Mon Sep 17 00:00:00 2001 From: Joshua Strobl Date: Thu, 8 Jul 2021 19:58:54 +0300 Subject: [PATCH] Fixed progress and duration reporting. Leveraging duration from ID3 data when possible, otherwise fall back to GStreamer. Fixed glib warnings on providing empty string in koto_album_add_track when the provided track has no genres. --- src/indexer/album.c | 4 ++++ src/koto-utils.c | 13 +++++++++++++ src/playback/engine.c | 24 +++++++++++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/indexer/album.c b/src/indexer/album.c index 2865a10..4f681fa 100644 --- a/src/indexer/album.c +++ b/src/indexer/album.c @@ -223,6 +223,10 @@ void koto_album_add_track( for (current_genre_list = track_genres; current_genre_list != NULL; current_genre_list = current_genre_list->next) { // Iterate over each item in the track genres gchar * track_genre = current_genre_list->data; // Get this genre + if (g_strcmp0(track_genre, "") == 0) { + continue; + } + if (koto_utils_string_contains_substring(existing_genres_as_string, track_genre)) { // Genres list contains this genre continue; } diff --git a/src/koto-utils.c b/src/koto-utils.c index 5391bf0..8b22649 100644 --- a/src/koto-utils.c +++ b/src/koto-utils.c @@ -150,6 +150,10 @@ gchar * koto_utils_replace_string_all( gchar * find, gchar * repl ) { + if (!koto_utils_is_string_valid(str)) { // Not a valid string + return g_strdup(""); + } + gchar ** split = g_strsplit(str, find, -1); // Split on find guint split_len = g_strv_length(split); @@ -177,6 +181,10 @@ GList * koto_utils_string_to_string_list( gchar * sep ) { GList * list = NULL; + if (!koto_utils_is_string_valid(s)) { // Provided string is not valid + return list; + } + gchar ** separated_strings = g_strsplit(s, sep, -1); // Split on separator for the string for (guint i = 0; i < g_strv_length(separated_strings); i++) { // Iterate over each item @@ -191,6 +199,11 @@ GList * koto_utils_string_to_string_list( gchar * koto_utils_unquote_string(gchar * s) { gchar * new_s = NULL; + if (!koto_utils_is_string_valid(s)) { // Not a valid string + new_s = g_strdup(""); + return new_s; + } + if (g_str_has_prefix(s, "'") && g_str_has_suffix(s, "'")) { // Begins and ends with ' new_s = g_utf8_substring(s, 1, g_utf8_strlen(s, -1) - 1); // Start at 1 and end at n-1 } else { diff --git a/src/playback/engine.c b/src/playback/engine.c index c7c0ad2..abe3e42 100644 --- a/src/playback/engine.c +++ b/src/playback/engine.c @@ -214,7 +214,7 @@ static void koto_playback_engine_init(KotoPlaybackEngine * self) { self->monitor = gst_bus_new(); // Get the bus for the playbin self->duration_query = gst_query_new_duration(GST_FORMAT_TIME); // Create our re-usable query for querying the duration - self->position_query = gst_query_new_position(GST_FORMAT_TIME); // Create our re-usable query for querying the position + self->position_query = gst_query_new_position(GST_FORMAT_DEFAULT); // Create our re-usable query for querying the position if (GST_IS_BUS(self->monitor)) { gst_bus_add_watch(self->monitor, koto_playback_engine_monitor_changed, self); @@ -336,10 +336,16 @@ KotoTrack * koto_playback_engine_get_current_track(KotoPlaybackEngine * self) { } gint64 koto_playback_engine_get_duration(KotoPlaybackEngine * self) { + guint64 track_duration = koto_track_get_duration(self->current_track); // Get the current track's duration + + if (track_duration != 0) { // Have a duration stored + return (gint64) track_duration; // Trust what we got from the ID3 data + } + gint64 duration = 0; if (gst_element_query(self->player, self->duration_query)) { // Able to query our duration - gst_query_parse_duration(self->duration_query, NULL, &duration); // Get the duration + gst_query_parse_duration(self->duration_query, NULL, &duration); // Get the duration") duration = duration / GST_SECOND; // Divide by NS to get seconds } @@ -357,7 +363,7 @@ gdouble koto_playback_engine_get_progress(KotoPlaybackEngine * self) { return 0.0; } - progress = gstprog / GST_SECOND; // Divide by GST_SECOND then again by 100. + progress = gstprog / 100000; } return progress; @@ -388,7 +394,6 @@ gboolean koto_playback_engine_monitor_changed( KotoPlaybackEngine * self = user_data; switch (GST_MESSAGE_TYPE(msg)) { - case GST_MESSAGE_ASYNC_DONE: case GST_MESSAGE_DURATION_CHANGED: { // Duration changed koto_playback_engine_tick_duration(self); koto_playback_engine_tick_track(self); @@ -450,7 +455,16 @@ void koto_playback_engine_set_position( KotoPlaybackEngine * self, int position ) { - gst_element_seek_simple(self->playbin, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, position * GST_SECOND); + gst_element_seek( + self->playbin, + 1.0, + GST_FORMAT_TIME, + GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, + GST_SEEK_TYPE_SET, + position * GST_SECOND, + GST_SEEK_TYPE_NONE, + -1 + ); } void koto_playback_engine_set_track_repeat(