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.
This commit is contained in:
Joshua Strobl 2021-07-08 19:58:54 +03:00
parent 381cc9ce4c
commit 22d4b6e50c
3 changed files with 36 additions and 5 deletions

View file

@ -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 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 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 if (koto_utils_string_contains_substring(existing_genres_as_string, track_genre)) { // Genres list contains this genre
continue; continue;
} }

View file

@ -150,6 +150,10 @@ gchar * koto_utils_replace_string_all(
gchar * find, gchar * find,
gchar * repl 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 gchar ** split = g_strsplit(str, find, -1); // Split on find
guint split_len = g_strv_length(split); guint split_len = g_strv_length(split);
@ -177,6 +181,10 @@ GList * koto_utils_string_to_string_list(
gchar * sep gchar * sep
) { ) {
GList * list = NULL; 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 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 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 * koto_utils_unquote_string(gchar * s) {
gchar * new_s = NULL; 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 ' 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 new_s = g_utf8_substring(s, 1, g_utf8_strlen(s, -1) - 1); // Start at 1 and end at n-1
} else { } else {

View file

@ -214,7 +214,7 @@ static void koto_playback_engine_init(KotoPlaybackEngine * self) {
self->monitor = gst_bus_new(); // Get the bus for the playbin 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->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)) { if (GST_IS_BUS(self->monitor)) {
gst_bus_add_watch(self->monitor, koto_playback_engine_monitor_changed, self); 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) { 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; gint64 duration = 0;
if (gst_element_query(self->player, self->duration_query)) { // Able to query our duration 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 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; return 0.0;
} }
progress = gstprog / GST_SECOND; // Divide by GST_SECOND then again by 100. progress = gstprog / 100000;
} }
return progress; return progress;
@ -388,7 +394,6 @@ gboolean koto_playback_engine_monitor_changed(
KotoPlaybackEngine * self = user_data; KotoPlaybackEngine * self = user_data;
switch (GST_MESSAGE_TYPE(msg)) { switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_ASYNC_DONE:
case GST_MESSAGE_DURATION_CHANGED: { // Duration changed case GST_MESSAGE_DURATION_CHANGED: { // Duration changed
koto_playback_engine_tick_duration(self); koto_playback_engine_tick_duration(self);
koto_playback_engine_tick_track(self); koto_playback_engine_tick_track(self);
@ -450,7 +455,16 @@ void koto_playback_engine_set_position(
KotoPlaybackEngine * self, KotoPlaybackEngine * self,
int position 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( void koto_playback_engine_set_track_repeat(