core/audio: Rework how tracks are loaded
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
fc6e3ff464
commit
4986bdad13
71
core/audio.c
71
core/audio.c
|
@ -6,6 +6,10 @@
|
|||
#include <core/playlist.h>
|
||||
#include <core/settings.h>
|
||||
|
||||
#define LOAD_PLAYING (1 << 0) /* Begin playback after loading */
|
||||
#define LOAD_HISTORY (1 << 1) /* Add the track to the history */
|
||||
#define LOAD_DEFAULT (LOAD_PLAYING | LOAD_HISTORY)
|
||||
|
||||
static const char *SETTINGS_TRACK = "core.audio.cur";
|
||||
static const char *SETTINGS_VOLUME = "core.audio.volume";
|
||||
|
||||
|
@ -31,49 +35,31 @@ static bool __audio_change_state(GstState state)
|
|||
return gst_element_set_state(audio_pipeline, state) != GST_STATE_CHANGE_FAILURE;
|
||||
}
|
||||
|
||||
static void __audio_gst_load(struct track *track, GstState state)
|
||||
static struct track *__audio_load(struct track *track, unsigned int flags)
|
||||
{
|
||||
gchar *path = track_path(track);
|
||||
struct track *prev = audio_track;
|
||||
gchar *path;
|
||||
|
||||
if (!track)
|
||||
return NULL;
|
||||
|
||||
audio_track = track;
|
||||
path = track_path(track);
|
||||
gst_element_set_state(audio_pipeline, GST_STATE_READY);
|
||||
g_object_set(G_OBJECT(audio_source), "location", path, NULL);
|
||||
gst_element_set_state(audio_pipeline, flags & LOAD_PLAYING ?
|
||||
GST_STATE_PLAYING : GST_STATE_PAUSED);
|
||||
|
||||
__audio_change_state(state);
|
||||
|
||||
playlist_played(prev);
|
||||
playlist_selected(track);
|
||||
if (flags & LOAD_HISTORY)
|
||||
playlist_add(playlist_lookup(PL_SYSTEM, "History"), track);
|
||||
if (audio_cb)
|
||||
audio_cb->audio_cb_load(track);
|
||||
|
||||
audio_save();
|
||||
g_free(path);
|
||||
}
|
||||
|
||||
/* Load a track, but don't add it to the history. */
|
||||
static struct track *__audio_do_load(struct track *track, GstState state)
|
||||
{
|
||||
struct track *prev = audio_track;
|
||||
|
||||
if (track) {
|
||||
__audio_gst_load(track, state);
|
||||
playlist_selected(track);
|
||||
}
|
||||
|
||||
playlist_played(prev);
|
||||
return audio_track;
|
||||
}
|
||||
|
||||
static struct track *__audio_load(struct track *track, GstState state)
|
||||
{
|
||||
struct track *ret = __audio_do_load(track, state);
|
||||
|
||||
if (ret == track)
|
||||
playlist_add(playlist_lookup(PL_SYSTEM, "History"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct track *__audio_next(GstState state)
|
||||
{
|
||||
return __audio_load(playlist_next(), state);
|
||||
return track;
|
||||
}
|
||||
|
||||
static void __audio_pad_added(GstElement *element, GstPad *pad, gpointer data)
|
||||
|
@ -109,12 +95,12 @@ static bool __audio_init_idle(void *data)
|
|||
|
||||
if (settings_has(SETTINGS_TRACK)) {
|
||||
track = settings_get(SETTINGS_TRACK);
|
||||
__audio_load(track_get(track), GST_STATE_PAUSED);
|
||||
__audio_load(track_get(track), LOAD_HISTORY);
|
||||
} else if (file_open(&audio_file, OPEN_READ)) {
|
||||
file_readf(&audio_file, "%u", &track);
|
||||
file_close(&audio_file);
|
||||
file_remove(&audio_file);
|
||||
__audio_load(track_get(track), GST_STATE_PAUSED);
|
||||
__audio_load(track_get(track), LOAD_HISTORY);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -179,7 +165,7 @@ bool audio_load(struct track *track)
|
|||
{
|
||||
if (track == audio_track)
|
||||
return false;
|
||||
return __audio_load(track, GST_STATE_PLAYING) == track;
|
||||
return __audio_load(track, LOAD_DEFAULT) != NULL;
|
||||
}
|
||||
|
||||
struct track *audio_cur_track()
|
||||
|
@ -264,12 +250,12 @@ gint64 audio_duration()
|
|||
|
||||
struct track *audio_next()
|
||||
{
|
||||
return __audio_next(GST_STATE_PLAYING);
|
||||
return __audio_load(playlist_next(), LOAD_DEFAULT);
|
||||
}
|
||||
|
||||
struct track *audio_prev()
|
||||
{
|
||||
return __audio_do_load(playlist_prev(), GST_STATE_PLAYING);
|
||||
return __audio_load(playlist_prev(), LOAD_PLAYING);
|
||||
}
|
||||
|
||||
void audio_state_changed(GstMessage *message)
|
||||
|
@ -294,12 +280,10 @@ struct track *audio_eos()
|
|||
track_played(audio_track);
|
||||
|
||||
/* Check pause count and pick the next track */
|
||||
if (audio_pause_count >= 0) {
|
||||
if (audio_pause_count >= 0)
|
||||
audio_pause_after(audio_pause_count - 1);
|
||||
if (audio_pause_count == -1)
|
||||
return __audio_next(GST_STATE_PAUSED);
|
||||
}
|
||||
return __audio_next(GST_STATE_PLAYING);
|
||||
return __audio_load(playlist_next(), audio_pause_count != -1 ?
|
||||
LOAD_DEFAULT : LOAD_HISTORY);
|
||||
}
|
||||
|
||||
void audio_error(GstMessage *error)
|
||||
|
@ -316,7 +300,8 @@ void audio_error(GstMessage *error)
|
|||
if (debug)
|
||||
g_print("Debug details: %s\n", debug);
|
||||
|
||||
__audio_next(audio_cur_state());
|
||||
__audio_load(playlist_next(), audio_cur_state() == GST_STATE_PLAYING ?
|
||||
LOAD_DEFAULT : LOAD_HISTORY);
|
||||
|
||||
g_error_free(err);
|
||||
g_free(debug);
|
||||
|
|
Loading…
Reference in New Issue