diff --git a/src/plugins/audioplayers.h b/src/plugins/audioplayers.h index bfa99df5..64c9e907 100644 --- a/src/plugins/audioplayers.h +++ b/src/plugins/audioplayers.h @@ -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_ diff --git a/src/plugins/audioplayers/player.c b/src/plugins/audioplayers/player.c index 11a1e933..71cb91ba 100644 --- a/src/plugins/audioplayers/player.c +++ b/src/plugins/audioplayers/player.c @@ -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: @@ -288,16 +289,16 @@ void audio_player_on_media_error(struct audio_player *self, GError *error, gchar void audio_player_on_media_state_change(struct audio_player *self, GstObject *src, GstState *old_state, GstState *new_state) { (void) old_state; - if (streq(GST_OBJECT_NAME(src), "playbin")) { + if (src == GST_OBJECT(self->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); @@ -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 { @@ -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); + } +} diff --git a/src/plugins/audioplayers/plugin.c b/src/plugins/audioplayers/plugin.c index 1f72a82d..e80d2d77 100644 --- a/src/plugins/audioplayers/plugin.c +++ b/src/plugins/audioplayers/plugin.c @@ -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; @@ -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)) { @@ -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)) { @@ -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)) { @@ -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) {