From 91aa44bce03a9d13a2b96a40fadf34732d0e3e04 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Wed, 20 Feb 2019 20:24:02 -0800 Subject: binding: mediaplayer: add functionality to switch loop playback types Allow client to request between playlist and track looping Bug-AGL: SPEC-2181 Change-Id: I05097426e827a891e14811dad1763944d3f2fd89 Signed-off-by: Matt Ranostay --- README.md | 2 +- binding/afm-mediaplayer-binding.c | 50 ++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d27ffc8..af304c0 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ the parameter of *value* (i.e. *{"value": "play"}*) | rewind | seek backward (in milliseconds) within current track | {"value": "rewind", "position": 2000} | | pick-track | select media item in playlist via index number | {"value": "pick-track", "index": 4} | | volume | set volume 0-100% for media stream | {"value": "volume, "volume": 40} | -| loop | loop playlist | {"value": "loop", "state": "true"} | +| loop | loop media (e.g off, playlist, track) | {"value": "loop", "state": "off"} | ### playlist JSON Response diff --git a/binding/afm-mediaplayer-binding.c b/binding/afm-mediaplayer-binding.c index 98887c1..233ccdc 100644 --- a/binding/afm-mediaplayer-binding.c +++ b/binding/afm-mediaplayer-binding.c @@ -42,10 +42,23 @@ static GList *current_track = NULL; static json_object *populate_json_metadata(void); +enum { + LOOP_OFF, + LOOP_PLAYLIST, + LOOP_TRACK, + LOOP_NUM_TYPES, +}; + +static const char * const LOOP_STATES[LOOP_NUM_TYPES] = { + "off", + "playlist", + "track", +}; + typedef struct _CustomData { GstElement *playbin, *fake_sink, *alsa_sink; gboolean playing; - gboolean loop; + int loop_state; gboolean one_time; long int volume; gint64 position; @@ -62,6 +75,23 @@ CustomData data = { .duration = GST_CLOCK_TIME_NONE, }; + +static int find_loop_state_idx(const char *state) +{ + int idx; + + if (!state) + return 0; + + for (idx = 0; idx < LOOP_NUM_TYPES; idx++) { + if (!g_strcmp0(LOOP_STATES[idx], state)) + return idx; + } + + /* default to 'off' state if invalid */ + return 0; +} + static void mediaplayer_set_role_state(afb_api_t api, int state) { json_object *jsonData = json_object_new_object(), *response; @@ -527,11 +557,10 @@ static void gstreamer_controls(afb_req_t request) break; } - case LOOP_CMD: { - const char *state = afb_req_value(request, "state"); - data.loop = !strcasecmp(state, "true") ? TRUE : FALSE; + case LOOP_CMD: + data.loop_state = + find_loop_state_idx(afb_req_value(request, "state")); break; - } case STOP_CMD: mediaplayer_set_role_state(api, GST_STATE_NULL); AFB_DEBUG("GSTREAMER playbin.state = GST_STATE_NULL"); @@ -771,10 +800,15 @@ static gboolean handle_message(GstBus *bus, GstMessage *msg, CustomData *data) data->position = GST_CLOCK_TIME_NONE; data->duration = GST_CLOCK_TIME_NONE; - ret = seek_track(NEXT_CMD); + if (data->loop_state == LOOP_TRACK) + ret = seek_stream("0", SEEK_CMD); + else + ret = seek_track(NEXT_CMD); if (ret < 0) { - if (!data->loop) { + int loop_playlist = data->loop_state == LOOP_PLAYLIST; + + if (!loop_playlist) { mediaplayer_set_role_state(data->api, GST_STATE_NULL); data->one_time = TRUE; } @@ -782,7 +816,7 @@ static gboolean handle_message(GstBus *bus, GstMessage *msg, CustomData *data) current_track = playlist; if (current_track != NULL) - set_media_uri(current_track->data, data->loop); + set_media_uri(current_track->data, loop_playlist); } pthread_mutex_unlock(&mutex); -- cgit 1.2.3-korg