Skip to content

Commit

Permalink
Update audioplayers impl:
Browse files Browse the repository at this point in the history
Emit error if set source fails to set ready state
Implement new release method
Return NULL in align with audioplayers API
  • Loading branch information
DoumanAsh committed Oct 22, 2023
1 parent d4c44ce commit 58dfcaf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/plugins/audioplayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ const char* audio_player_subscribe_channel_name(const struct audio_player *self)
///Returns `true` if player uses `channel`, otherwise returns `false
bool audio_player_set_subscription_status(struct audio_player *self, const char *channel, bool value);

void audio_player_release(struct audio_player *self);

#endif // AUDIOPLAYERS_H_
26 changes: 23 additions & 3 deletions src/plugins/audioplayers/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ gboolean audio_player_on_bus_message(GstBus *bus, GstMessage *message, struct au
case GST_MESSAGE_ASYNC_DONE:
if (!data->is_seek_completed) {
audio_player_on_seek_completed(data);
data->is_seek_completed = true;
}
break;
default:
Expand Down Expand Up @@ -291,13 +292,13 @@ void audio_player_on_media_state_change(struct audio_player *self, GstObject *sr
if (streq(GST_OBJECT_NAME(src), "playbin")) {
LOG_DEBUG("%s: on_media_state_change(old_state=%d, new_state=%d)\n", self->player_id, *old_state, *new_state);
if (*new_state == GST_STATE_READY) {
self->is_initialized = false;

// Need to set to pause state, in order to make player functional
GstStateChangeReturn ret = gst_element_set_state(self->playbin, GST_STATE_PAUSED);
if (ret == GST_STATE_CHANGE_FAILURE) {
LOG_ERROR("Unable to set the pipeline to the paused state.\n");
}

self->is_initialized = false;
} else if (*old_state == GST_STATE_PAUSED && *new_state == GST_STATE_PLAYING) {
audio_player_on_position_update(self);
audio_player_on_duration_update(self);
Expand Down Expand Up @@ -558,7 +559,10 @@ void audio_player_set_source_url(struct audio_player *self, char *url) {
if (strlen(self->url) != 0) {
g_object_set(self->playbin, "uri", self->url, NULL);
if (self->playbin->current_state != GST_STATE_READY) {
gst_element_set_state(self->playbin, GST_STATE_READY);
if (gst_element_set_state(self->playbin, GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
//This should not happen generally
LOG_ERROR("Could not set player into ready state.\n");
}
}
}
} else {
Expand All @@ -582,3 +586,19 @@ bool audio_player_set_subscription_status(struct audio_player *self, const char
return false;
}
}

void audio_player_release(struct audio_player *self) {
self->is_initialized = false;
self->is_playing = false;
if (self->url != NULL) {
free(self->url);
self->url = NULL;
}

GstState playbinState;
gst_element_get_state(self->playbin, &playbinState, NULL, GST_CLOCK_TIME_NONE);

if (playbinState > GST_STATE_NULL) {
gst_element_set_state(self->playbin, GST_STATE_NULL);
}
}
11 changes: 5 additions & 6 deletions src/plugins/audioplayers/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
struct std_value *args, *tmp;
const char *method;
char *player_id, *mode;
int result = 1;
struct std_value result = STDNULL;
int ok;

(void) responsehandle;
Expand Down Expand Up @@ -75,8 +75,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
audio_player_pause(player);
audio_player_set_position(player, 0);
} else if (streq(method, "release")) {
audio_player_pause(player);
audio_player_set_position(player, 0);
audio_player_release(player);
} else if (streq(method, "seek")) {
tmp = stdmap_get_str(args, "position");
if (tmp == NULL || !STDVALUE_IS_INT(*tmp)) {
Expand Down Expand Up @@ -109,7 +108,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte

audio_player_set_source_url(player, url);
} else if (streq(method, "getDuration")) {
result = audio_player_get_duration(player);
result = STDINT64(audio_player_get_duration(player));
} else if (streq(method, "setVolume")) {
tmp = stdmap_get_str(args, "volume");
if (tmp != NULL && STDVALUE_IS_FLOAT(*tmp)) {
Expand All @@ -118,7 +117,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
return platch_respond_illegal_arg_std(responsehandle, "Expected `arg['volume']` to be a float.");
}
} else if (streq(method, "getCurrentPosition")) {
result = audio_player_get_position(player);
result = STDINT64(audio_player_get_position(player));
} else if (streq(method, "setPlaybackRate")) {
tmp = stdmap_get_str(args, "playbackRate");
if (tmp != NULL && STDVALUE_IS_FLOAT(*tmp)) {
Expand Down Expand Up @@ -191,7 +190,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
return platch_respond_not_implemented(responsehandle);
}

return platch_respond_success_std(responsehandle, &STDINT64(result));
return platch_respond_success_std(responsehandle, &result);
}

static int on_global_method_call(char *channel, struct platch_obj *object, FlutterPlatformMessageResponseHandle *responsehandle) {
Expand Down

0 comments on commit 58dfcaf

Please sign in to comment.