Skip to content

Commit 21d902a

Browse files
committed
Refactoring event channel subscription to send events correctly
1 parent c5ed317 commit 21d902a

File tree

3 files changed

+128
-56
lines changed

3 files changed

+128
-56
lines changed

include/plugins/audioplayers.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,13 @@ void audio_player_set_source_url(struct audio_player *self, char *url);
3838

3939
bool audio_player_is_id(struct audio_player *self, char *id);
4040

41+
const char* audio_player_subscribe_channel_name(const struct audio_player *self);
42+
43+
///Asks to subscribe to channel events
44+
///
45+
///`value` - Indicates whether to subscribe or unsubscribe
46+
///
47+
///Returns `true` if player uses `channel`, otherwise returns `false
48+
bool audio_player_set_subscription_status(struct audio_player *self, const char *channel, bool value);
49+
4150
#endif // AUDIOPLAYERS_H_

src/plugins/audioplayers/player.c

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
#define _GNU_SOURCE
2+
13
#include <stdio.h>
4+
#include <stdbool.h>
25

36
#include "gst/gst.h"
47
#include "gst/gstelementfactory.h"
58
#include "gst/gstmessage.h"
69
#include "gst/gstsegment.h"
710
#include "platformchannel.h"
8-
#include "pluginregistry.h"
911

1012
#include <flutter-pi.h>
1113
#include <plugins/audioplayers.h>
@@ -30,7 +32,9 @@ struct audio_player {
3032

3133
char *url;
3234
char *player_id;
33-
char event_channel_name[128];
35+
char *event_channel_name;
36+
37+
_Atomic bool event_subscribed;
3438
};
3539

3640
// Private Class functions
@@ -75,22 +79,6 @@ static void audio_player_source_setup(GstElement *playbin, GstElement *source, G
7579
}
7680
}
7781

78-
static int on_receive_event_ch(char *channel, struct platch_obj *object, FlutterPlatformMessageResponseHandle *responsehandle) {
79-
//TODO: Do we need any more setup to send events to dart code?
80-
(void)channel;
81-
if (strcmp(object->method, "listen") == 0) {
82-
LOG_DEBUG("%s: listen()\n", channel);
83-
platch_respond_success_std(responsehandle, NULL);
84-
} else if (strcmp(object->method, "cancel") == 0) {
85-
LOG_DEBUG("%s: cancel()\n", channel);
86-
platch_respond_success_std(responsehandle, NULL);
87-
} else {
88-
return platch_respond_not_implemented(responsehandle);
89-
}
90-
91-
return 0;
92-
}
93-
9482
struct audio_player *audio_player_new(char *player_id, char *channel) {
9583
GPollFD fd;
9684
sd_event_source *busfd_event_source;
@@ -101,11 +89,13 @@ struct audio_player *audio_player_new(char *player_id, char *channel) {
10189
}
10290

10391
self->url = NULL;
92+
self->source = NULL;
10493
self->is_initialized = false;
10594
self->is_playing = false;
10695
self->is_looping = false;
10796
self->is_seek_completed = false;
10897
self->playback_rate = 1.0;
98+
self->event_subscribed = false;
10999

110100
gst_init(NULL, NULL);
111101
self->playbin = gst_element_factory_make("playbin", NULL);
@@ -154,23 +144,10 @@ struct audio_player *audio_player_new(char *player_id, char *channel) {
154144

155145
// audioplayers player event channel clang:
156146
// <local>/events/<player_id>
157-
int ok = snprintf(
158-
self->event_channel_name,
159-
sizeof(self->event_channel_name),
160-
"%s/events/%s",
161-
channel,
162-
player_id
163-
);
164-
DEBUG_ASSERT_MSG(ok < sizeof(self->event_channel_name), "event channel name overflow");
165-
166-
// set a receiver on the videoEvents event channel
167-
ok = plugin_registry_set_receiver(
168-
self->event_channel_name,
169-
kStandardMethodCall,
170-
on_receive_event_ch
171-
);
147+
asprintf(&self->event_channel_name, "%s/events/%s", channel, player_id);
148+
DEBUG_ASSERT_MSG(self->event_channel_name != NULL, "event channel name OEM");
172149

173-
if (ok != 0) {
150+
if (self->event_channel_name == NULL) {
174151
goto deinit_player_id;
175152
}
176153

@@ -293,6 +270,10 @@ void audio_player_set_playback(struct audio_player *self, int64_t seekTo, double
293270
}
294271

295272
void audio_player_on_media_error(struct audio_player *self, GError *error, gchar *debug) {
273+
if (!self->event_subscribed) {
274+
return;
275+
}
276+
296277
char error_code[16] = {0};
297278
snprintf(error_code, sizeof(error_code), "%d", error->code);
298279
// clang-format off
@@ -335,6 +316,10 @@ void audio_player_on_media_state_change(struct audio_player *self, GstObject *sr
335316
}
336317

337318
void audio_player_on_prepared(struct audio_player *self, bool value) {
319+
if (!self->event_subscribed) {
320+
return;
321+
}
322+
338323
// clang-format off
339324
platch_send_success_event_std(
340325
self->event_channel_name,
@@ -347,6 +332,10 @@ void audio_player_on_prepared(struct audio_player *self, bool value) {
347332
}
348333

349334
void audio_player_on_position_update(struct audio_player *self) {
335+
if (!self->event_subscribed) {
336+
return;
337+
}
338+
350339
// clang-format off
351340
platch_send_success_event_std(
352341
self->event_channel_name,
@@ -359,6 +348,9 @@ void audio_player_on_position_update(struct audio_player *self) {
359348
}
360349

361350
void audio_player_on_duration_update(struct audio_player *self) {
351+
if (!self->event_subscribed) {
352+
return;
353+
}
362354
// clang-format off
363355
platch_send_success_event_std(
364356
self->event_channel_name,
@@ -371,28 +363,32 @@ void audio_player_on_duration_update(struct audio_player *self) {
371363
}
372364
void audio_player_on_seek_completed(struct audio_player *self) {
373365
audio_player_on_position_update(self);
374-
// clang-format off
375-
platch_send_success_event_std(
376-
self->event_channel_name,
377-
&STDMAP2(
378-
STDSTRING("event"), STDSTRING("audio.onSeekComplete"),
379-
STDSTRING("value"), STDBOOL(true)
380-
)
381-
);
382-
// clang-format on
366+
367+
if (self->event_subscribed) {
368+
// clang-format off
369+
platch_send_success_event_std(
370+
self->event_channel_name,
371+
&STDMAP2(
372+
STDSTRING("event"), STDSTRING("audio.onSeekComplete"),
373+
STDSTRING("value"), STDBOOL(true)
374+
)
375+
);
376+
// clang-format on
377+
}
383378
self->is_seek_completed = true;
384379
}
385380
void audio_player_on_playback_ended(struct audio_player *self) {
386-
// clang-format off
387-
platch_send_success_event_std(
388-
self->event_channel_name,
389-
&STDMAP2(
390-
STDSTRING("event"), STDSTRING("audio.onComplete"),
391-
STDSTRING("value"), STDBOOL(true)
392-
)
393-
);
394-
395-
// clang-format on
381+
if (self->event_subscribed) {
382+
// clang-format off
383+
platch_send_success_event_std(
384+
self->event_channel_name,
385+
&STDMAP2(
386+
STDSTRING("event"), STDSTRING("audio.onComplete"),
387+
STDSTRING("value"), STDBOOL(true)
388+
)
389+
);
390+
// clang-format on
391+
}
396392

397393
if (audio_player_get_looping(self)) {
398394
audio_player_play(self);
@@ -482,14 +478,15 @@ void audio_player_destroy(struct audio_player *self) {
482478
self->url = NULL;
483479
}
484480

485-
486481
if (self->player_id != NULL) {
487482
free(self->player_id);
488483
self->player_id = NULL;
489484
}
490485

491-
plugin_registry_remove_receiver(self->event_channel_name);
492-
self->event_channel_name[0] = 0;
486+
if (self->event_channel_name != NULL) {
487+
free(self->event_channel_name);
488+
self->event_channel_name = NULL;;
489+
}
493490

494491
free(self);
495492
}
@@ -572,3 +569,16 @@ void audio_player_set_source_url(struct audio_player *self, char *url) {
572569
bool audio_player_is_id(struct audio_player *self, char *player_id) {
573570
return strcmp(self->player_id, player_id) == 0;
574571
}
572+
573+
const char* audio_player_subscribe_channel_name(const struct audio_player *self) {
574+
return self->event_channel_name;
575+
}
576+
577+
bool audio_player_set_subscription_status(struct audio_player *self, const char *channel, bool value) {
578+
if (strcmp(self->event_channel_name, channel) == 0) {
579+
self->event_subscribed = value;
580+
return true;
581+
} else {
582+
return false;
583+
}
584+
}

src/plugins/audioplayers/plugin.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,38 @@ static int on_global_method_call(char *channel, struct platch_obj *object, Flutt
195195
return platch_respond_success_std(responsehandle, &STDBOOL(true));
196196
}
197197

198+
static int on_receive_event_ch(char *channel, struct platch_obj *object, FlutterPlatformMessageResponseHandle *responsehandle) {
199+
struct audio_player *player;
200+
201+
if (strcmp(object->method, "listen") == 0) {
202+
LOG_DEBUG("%s: listen()\n", channel);
203+
204+
for_each_pointer_in_cpset(&plugin.players, player) {
205+
if (audio_player_set_subscription_status(player, channel, true)) {
206+
return platch_respond_success_std(responsehandle, NULL);
207+
}
208+
}
209+
210+
LOG_ERROR("%s: player not found\n", channel);
211+
return platch_respond_not_implemented(responsehandle);
212+
} else if (strcmp(object->method, "cancel") == 0) {
213+
LOG_DEBUG("%s: cancel()\n", channel);
214+
215+
for_each_pointer_in_cpset(&plugin.players, player) {
216+
if (audio_player_set_subscription_status(player, channel, false)) {
217+
return platch_respond_success_std(responsehandle, NULL);
218+
}
219+
}
220+
221+
LOG_ERROR("%s: player not found\n", channel);
222+
return platch_respond_not_implemented(responsehandle);
223+
} else {
224+
return platch_respond_not_implemented(responsehandle);
225+
}
226+
227+
return 0;
228+
}
229+
198230
enum plugin_init_result audioplayers_plugin_init(struct flutterpi *flutterpi, void **userdata_out) {
199231
(void) userdata_out;
200232
int ok;
@@ -249,12 +281,33 @@ static struct audio_player *audioplayers_linux_plugin_get_player(char *player_id
249281
}
250282
}
251283

284+
LOG_DEBUG("Create player(id=%s)\n", player_id);
252285
player = audio_player_new(player_id, AUDIOPLAYERS_LOCAL_CHANNEL);
286+
287+
if (player == NULL) {
288+
LOG_ERROR("player(id=%s) cannot be created", player_id);
289+
return NULL;
290+
}
291+
292+
const char* event_channel = audio_player_subscribe_channel_name(player);
293+
// set a receiver on the videoEvents event channel
294+
int ok = plugin_registry_set_receiver(
295+
event_channel,
296+
kStandardMethodCall,
297+
on_receive_event_ch
298+
);
299+
if (ok != 0) {
300+
LOG_ERROR("Cannot set player receiver for event channel: %s\n", event_channel);
301+
audio_player_destroy(player);
302+
return NULL;
303+
}
304+
253305
cpset_put_locked(&plugin.players, player);
254306
return player;
255307
}
256308

257309
static void audioplayers_linux_plugin_dispose_player(struct audio_player *player) {
310+
plugin_registry_remove_receiver(audio_player_subscribe_channel_name(player));
258311
audio_player_destroy(player);
259312
cpset_remove_locked(&plugin.players, player);
260313
}

0 commit comments

Comments
 (0)