Skip to content

Commit 58dfcaf

Browse files
committed
Update audioplayers impl:
Emit error if set source fails to set ready state Implement new release method Return NULL in align with audioplayers API
1 parent d4c44ce commit 58dfcaf

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/plugins/audioplayers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ const char* audio_player_subscribe_channel_name(const struct audio_player *self)
4747
///Returns `true` if player uses `channel`, otherwise returns `false
4848
bool audio_player_set_subscription_status(struct audio_player *self, const char *channel, bool value);
4949

50+
void audio_player_release(struct audio_player *self);
51+
5052
#endif // AUDIOPLAYERS_H_

src/plugins/audioplayers/player.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ gboolean audio_player_on_bus_message(GstBus *bus, GstMessage *message, struct au
210210
case GST_MESSAGE_ASYNC_DONE:
211211
if (!data->is_seek_completed) {
212212
audio_player_on_seek_completed(data);
213+
data->is_seek_completed = true;
213214
}
214215
break;
215216
default:
@@ -291,13 +292,13 @@ void audio_player_on_media_state_change(struct audio_player *self, GstObject *sr
291292
if (streq(GST_OBJECT_NAME(src), "playbin")) {
292293
LOG_DEBUG("%s: on_media_state_change(old_state=%d, new_state=%d)\n", self->player_id, *old_state, *new_state);
293294
if (*new_state == GST_STATE_READY) {
294-
self->is_initialized = false;
295-
296295
// Need to set to pause state, in order to make player functional
297296
GstStateChangeReturn ret = gst_element_set_state(self->playbin, GST_STATE_PAUSED);
298297
if (ret == GST_STATE_CHANGE_FAILURE) {
299298
LOG_ERROR("Unable to set the pipeline to the paused state.\n");
300299
}
300+
301+
self->is_initialized = false;
301302
} else if (*old_state == GST_STATE_PAUSED && *new_state == GST_STATE_PLAYING) {
302303
audio_player_on_position_update(self);
303304
audio_player_on_duration_update(self);
@@ -558,7 +559,10 @@ void audio_player_set_source_url(struct audio_player *self, char *url) {
558559
if (strlen(self->url) != 0) {
559560
g_object_set(self->playbin, "uri", self->url, NULL);
560561
if (self->playbin->current_state != GST_STATE_READY) {
561-
gst_element_set_state(self->playbin, GST_STATE_READY);
562+
if (gst_element_set_state(self->playbin, GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
563+
//This should not happen generally
564+
LOG_ERROR("Could not set player into ready state.\n");
565+
}
562566
}
563567
}
564568
} else {
@@ -582,3 +586,19 @@ bool audio_player_set_subscription_status(struct audio_player *self, const char
582586
return false;
583587
}
584588
}
589+
590+
void audio_player_release(struct audio_player *self) {
591+
self->is_initialized = false;
592+
self->is_playing = false;
593+
if (self->url != NULL) {
594+
free(self->url);
595+
self->url = NULL;
596+
}
597+
598+
GstState playbinState;
599+
gst_element_get_state(self->playbin, &playbinState, NULL, GST_CLOCK_TIME_NONE);
600+
601+
if (playbinState > GST_STATE_NULL) {
602+
gst_element_set_state(self->playbin, GST_STATE_NULL);
603+
}
604+
}

src/plugins/audioplayers/plugin.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
3131
struct std_value *args, *tmp;
3232
const char *method;
3333
char *player_id, *mode;
34-
int result = 1;
34+
struct std_value result = STDNULL;
3535
int ok;
3636

3737
(void) responsehandle;
@@ -75,8 +75,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
7575
audio_player_pause(player);
7676
audio_player_set_position(player, 0);
7777
} else if (streq(method, "release")) {
78-
audio_player_pause(player);
79-
audio_player_set_position(player, 0);
78+
audio_player_release(player);
8079
} else if (streq(method, "seek")) {
8180
tmp = stdmap_get_str(args, "position");
8281
if (tmp == NULL || !STDVALUE_IS_INT(*tmp)) {
@@ -109,7 +108,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
109108

110109
audio_player_set_source_url(player, url);
111110
} else if (streq(method, "getDuration")) {
112-
result = audio_player_get_duration(player);
111+
result = STDINT64(audio_player_get_duration(player));
113112
} else if (streq(method, "setVolume")) {
114113
tmp = stdmap_get_str(args, "volume");
115114
if (tmp != NULL && STDVALUE_IS_FLOAT(*tmp)) {
@@ -118,7 +117,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
118117
return platch_respond_illegal_arg_std(responsehandle, "Expected `arg['volume']` to be a float.");
119118
}
120119
} else if (streq(method, "getCurrentPosition")) {
121-
result = audio_player_get_position(player);
120+
result = STDINT64(audio_player_get_position(player));
122121
} else if (streq(method, "setPlaybackRate")) {
123122
tmp = stdmap_get_str(args, "playbackRate");
124123
if (tmp != NULL && STDVALUE_IS_FLOAT(*tmp)) {
@@ -191,7 +190,7 @@ static int on_local_method_call(char *channel, struct platch_obj *object, Flutte
191190
return platch_respond_not_implemented(responsehandle);
192191
}
193192

194-
return platch_respond_success_std(responsehandle, &STDINT64(result));
193+
return platch_respond_success_std(responsehandle, &result);
195194
}
196195

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

0 commit comments

Comments
 (0)