Index: apps/app_mixmonitor.c =================================================================== --- apps/app_mixmonitor.c (revision 173242) +++ apps/app_mixmonitor.c (working copy) @@ -98,7 +98,7 @@ char *post_process; char *name; unsigned int flags; - struct ast_channel *chan; + struct mixmonitor_ds *mixmonitor_ds; }; enum { @@ -124,6 +124,40 @@ AST_APP_OPTION_ARG('W', MUXFLAG_VOLUME, OPT_ARG_VOLUME), }); +struct mixmonitor_ds { + struct ast_channel *chan; + unsigned int condition_boolean; + ast_cond_t condition; + ast_mutex_t lock; +}; + +static void mixmonitor_ds_destroy(void *data) +{ + struct mixmonitor_ds *mixmonitor_ds = data; + + ast_log(LOG_NOTICE, "DESTROY!\n"); + ast_mutex_lock(&mixmonitor_ds->lock); + mixmonitor_ds->chan = NULL; + mixmonitor_ds->condition_boolean = 1; + ast_cond_signal(&mixmonitor_ds->condition); + ast_mutex_unlock(&mixmonitor_ds->lock); +} + +static void mixmonitor_ds_chan_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) +{ + struct mixmonitor_ds *mixmonitor_ds = data; + + ast_mutex_lock(&mixmonitor_ds->lock); + mixmonitor_ds->chan = new_chan; + ast_mutex_unlock(&mixmonitor_ds->lock); +} + +static struct ast_datastore_info mixmonitor_ds_info = { + .type = "mixmonitor", + .destroy = mixmonitor_ds_destroy, + .chan_fixup = mixmonitor_ds_chan_fixup, +}; + static int startmon(struct ast_channel *chan, struct ast_audiohook *audiohook) { struct ast_channel *peer; @@ -165,8 +199,10 @@ if (!(fr = ast_audiohook_read_frame(&mixmonitor->audiohook, SAMPLES_PER_FRAME, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR))) continue; - - if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) || ast_bridged_channel(mixmonitor->chan)) { + + ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); + if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) || (mixmonitor->mixmonitor_ds->chan && ast_bridged_channel(mixmonitor->mixmonitor_ds->chan))) { + ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); /* Initialize the file if not already done so */ if (!fs && !errflag) { oflags = O_CREAT | O_WRONLY; @@ -186,6 +222,8 @@ /* Write out the frame */ if (fs) ast_writestream(fs, fr); + } else { + ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); } /* All done! free it. */ @@ -208,12 +246,47 @@ ast_safe_system(mixmonitor->post_process); } + ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); + if (!mixmonitor->mixmonitor_ds->condition_boolean) { + ast_cond_wait(&mixmonitor->mixmonitor_ds->condition, &mixmonitor->mixmonitor_ds->lock); + } + ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); + ast_log(LOG_NOTICE, "FREE!!!\n"); + ast_free(mixmonitor->mixmonitor_ds); free(mixmonitor); - return NULL; } +static int setup_mixmonitor_ds(struct mixmonitor *mixmonitor, struct ast_channel *chan) +{ + struct ast_datastore *datastore = NULL; + struct mixmonitor_ds *mixmonitor_ds; + + if (!(mixmonitor_ds = ast_calloc(1, sizeof(*mixmonitor_ds)))) { + return -1; + } + + ast_mutex_init(&mixmonitor_ds->lock); + ast_cond_init(&mixmonitor_ds->condition, NULL); + + if (!(datastore = ast_channel_datastore_alloc(&mixmonitor_ds_info, NULL))) { + ast_free(mixmonitor_ds); + return -1; + } + + /* No need to lock mixmonitor_ds since this is still operating in the channel's thread */ + mixmonitor_ds->chan = chan; + datastore->data = mixmonitor_ds; + + ast_channel_lock(chan); + ast_channel_datastore_add(chan, datastore); + ast_channel_unlock(chan); + + mixmonitor->mixmonitor_ds = mixmonitor_ds; + return 0; +} + static void launch_monitor_thread(struct ast_channel *chan, const char *filename, unsigned int flags, int readvol, int writevol, const char *post_process) { @@ -248,7 +321,9 @@ /* Copy over flags and channel name */ mixmonitor->flags = flags; - mixmonitor->chan = chan; + if (setup_mixmonitor_ds(mixmonitor, chan)) { + return; + } mixmonitor->name = (char *) mixmonitor + sizeof(*mixmonitor); strcpy(mixmonitor->name, chan->name); if (!ast_strlen_zero(postprocess2)) { @@ -285,7 +360,6 @@ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); ast_pthread_create_background(&thread, &attr, mixmonitor_thread, mixmonitor); pthread_attr_destroy(&attr); - } static int mixmonitor_exec(struct ast_channel *chan, void *data)