diff options
author | Alfred E. Heggestad <aeh@db.org> | 2016-09-04 10:49:41 +0200 |
---|---|---|
committer | Alfred E. Heggestad <aeh@db.org> | 2016-09-04 10:49:41 +0200 |
commit | 8abcfeb1486a093a07360c63ea65958e3daaa57d (patch) | |
tree | e1bdfe78788ce83ff94f08d8ebc537d69bd8c561 /src | |
parent | 1939f2d3ec202c27b43cf6c2350d0b1cd2f6933c (diff) |
play: make it re-entrant, add struct player
Diffstat (limited to 'src')
-rw-r--r-- | src/baresip.c | 12 | ||||
-rw-r--r-- | src/main.c | 14 | ||||
-rw-r--r-- | src/play.c | 64 | ||||
-rw-r--r-- | src/ua.c | 3 |
4 files changed, 68 insertions, 25 deletions
diff --git a/src/baresip.c b/src/baresip.c index 5605136..6c68944 100644 --- a/src/baresip.c +++ b/src/baresip.c @@ -16,6 +16,7 @@ static struct baresip { struct network *net; struct contacts contacts; struct commands commands; + struct player *player; } baresip; @@ -44,12 +45,17 @@ int baresip_init(struct config *cfg, bool prefer_ipv6) if (err) return err; + err = play_init(&baresip.player); + if (err) + return err; + return 0; } void baresip_close(void) { + baresip.player = mem_deref(baresip.player); cmd_close(&baresip.commands); contact_close(&baresip.contacts); @@ -73,3 +79,9 @@ struct commands *baresip_commands(void) { return &baresip.commands; } + + +struct player *baresip_player(void) +{ + return baresip.player; +} @@ -158,12 +158,6 @@ int main(int argc, char *argv[]) goto out; } - /* Set audio path preferring the one given in -p argument (if any) */ - if (audio_path) - play_set_path(audio_path); - else if (str_isset(conf_config()->audio.audio_path)) - play_set_path(conf_config()->audio.audio_path); - /* * Initialise the top-level baresip struct, must be * done AFTER configuration is complete. @@ -174,6 +168,14 @@ int main(int argc, char *argv[]) goto out; } + /* Set audio path preferring the one given in -p argument (if any) */ + if (audio_path) + play_set_path(baresip_player(), audio_path); + else if (str_isset(conf_config()->audio.audio_path)) { + play_set_path(baresip_player(), + conf_config()->audio.audio_path); + } + /* NOTE: must be done after all arguments are processed */ if (modc) { size_t i; @@ -29,8 +29,13 @@ struct play { #ifndef PREFIX #define PREFIX "/usr" #endif -static char play_path[256] = PREFIX "/share/baresip"; -static struct list playl; +static const char default_play_path[256] = PREFIX "/share/baresip"; + + +struct player { + struct list playl; + char play_path[256]; +}; static void tmr_polling(void *arg); @@ -39,6 +44,7 @@ static void tmr_polling(void *arg); static void tmr_stop(void *arg) { struct play *play = arg; + debug("play: player complete.\n"); mem_deref(play); } @@ -203,6 +209,7 @@ static int aufile_load(struct mbuf *mb, const char *filename, * Play a tone from a PCM buffer * * @param playp Pointer to allocated player object + * @param player Audio-file player * @param tone PCM buffer to play * @param srate Sampling rate * @param ch Number of channels @@ -210,7 +217,8 @@ static int aufile_load(struct mbuf *mb, const char *filename, * * @return 0 if success, otherwise errorcode */ -int play_tone(struct play **playp, struct mbuf *tone, uint32_t srate, +int play_tone(struct play **playp, struct player *player, + struct mbuf *tone, uint32_t srate, uint8_t ch, int repeat) { struct auplay_prm wprm; @@ -218,6 +226,8 @@ int play_tone(struct play **playp, struct mbuf *tone, uint32_t srate, struct config *cfg; int err; + if (!player) + return EINVAL; if (playp && *playp) return EALREADY; @@ -246,7 +256,7 @@ int play_tone(struct play **playp, struct mbuf *tone, uint32_t srate, if (err) goto out; - list_append(&playl, &play->le, play); + list_append(&player->playl, &play->le, play); tmr_start(&play->tmr, 1000, tmr_polling, play); out: @@ -266,12 +276,14 @@ int play_tone(struct play **playp, struct mbuf *tone, uint32_t srate, * Play an audio file in WAV format * * @param playp Pointer to allocated player object + * @param player Audio-file player * @param filename Name of WAV file to play * @param repeat Number of times to repeat * * @return 0 if success, otherwise errorcode */ -int play_file(struct play **playp, const char *filename, int repeat) +int play_file(struct play **playp, struct player *player, + const char *filename, int repeat) { struct mbuf *mb; char path[512]; @@ -279,11 +291,13 @@ int play_file(struct play **playp, const char *filename, int repeat) uint8_t ch = 0; int err; + if (!player) + return EINVAL; if (playp && *playp) return EALREADY; if (re_snprintf(path, sizeof(path), "%s/%s", - play_path, filename) < 0) + player->play_path, filename) < 0) return ENOMEM; mb = mbuf_alloc(1024); @@ -296,7 +310,7 @@ int play_file(struct play **playp, const char *filename, int repeat) goto out; } - err = play_tone(playp, mb, srate, ch, repeat); + err = play_tone(playp, player, mb, srate, ch, repeat); out: mem_deref(mb); @@ -305,22 +319,40 @@ int play_file(struct play **playp, const char *filename, int repeat) } -void play_init(void) +static void player_destructor(void *data) { - list_init(&playl); + struct player *player = data; + + list_flush(&player->playl); } -/** - * Close all active audio players - */ -void play_close(void) +int play_init(struct player **playerp) { - list_flush(&playl); + struct player *player; + + if (!playerp) + return EINVAL; + + player = mem_zalloc(sizeof(*player), player_destructor); + if (!player) + return ENOMEM; + + list_init(&player->playl); + + str_ncpy(player->play_path, default_play_path, + sizeof(player->play_path)); + + *playerp = player; + + return 0; } -void play_set_path(const char *path) +void play_set_path(struct player *player, const char *path) { - str_ncpy(play_path, path, sizeof(play_path)); + if (!player) + return; + + str_ncpy(player->play_path, path, sizeof(player->play_path)); } @@ -1341,8 +1341,6 @@ int ua_init(const char *software, bool udp, bool tcp, bool tls, uag.cfg = &cfg->sip; bsize = cfg->sip.trans_bsize; - play_init(); - uag.use_udp = udp; uag.use_tcp = tcp; uag.use_tls = tls; @@ -1396,7 +1394,6 @@ int ua_init(const char *software, bool udp, bool tcp, bool tls, void ua_close(void) { cmd_unregister(baresip_commands(), cmdv); - play_close(); ui_reset(); uag.evsock = mem_deref(uag.evsock); |