Index: apps/app_mixmonitor.c =================================================================== --- apps/app_mixmonitor.c (revision 402904) +++ apps/app_mixmonitor.c (working copy) @@ -287,13 +287,45 @@ ast_free(mixmonitor); } } + +static void mixmonitor_save_prep(struct mixmonitor *mixmonitor, char *filename, struct ast_filestream **fs, unsigned int *oflags, int *errflag, char **ext) +{ + /* Initialize the file if not already done so */ + char *last_slash = NULL; + if (!ast_strlen_zero(filename)) { + if (!*fs && !*errflag && !mixmonitor->mixmonitor_ds->fs_quit) { + *oflags = O_CREAT | O_WRONLY; + *oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC; + + last_slash = strrchr(filename, '/'); + + if ((*ext = strrchr(filename, '.')) && (*ext > last_slash)) { + **ext = '\0'; + *ext = *ext + 1; + } else { + *ext = "raw"; + } + + if (!(*fs = ast_writefile(filename, *ext, NULL, *oflags, 0, 0666))) { + ast_log(LOG_ERROR, "Cannot open %s.%s\n", filename, *ext); + pbx_builtin_setvar_helper(mixmonitor->autochan->chan, "MIXMONITOR_STATUS", "ERROR"); + *errflag = 1; + } else if (ast_truncstream(*fs) < 0) { + ast_log(LOG_ERROR, "Cannot open %s.%s\n", filename, *ext); + pbx_builtin_setvar_helper(mixmonitor->autochan->chan, "MIXMONITOR_STATUS", "ERROR"); + *errflag = 1; + } + else { + pbx_builtin_setvar_helper(mixmonitor->autochan->chan, "MIXMONITOR_STATUS", "OK"); + } + } + } +} + static void *mixmonitor_thread(void *obj) { struct mixmonitor *mixmonitor = obj; struct ast_filestream **fs = NULL; - unsigned int oflags; - char *ext; - char *last_slash; int errflag = 0; ast_verb(2, "Begin MixMonitor Recording %s\n", mixmonitor->name); @@ -320,29 +352,17 @@ if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) || (mixmonitor->autochan->chan && ast_bridged_channel(mixmonitor->autochan->chan))) { ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); - /* Initialize the file if not already done so */ - if (!*fs && !errflag && !mixmonitor->mixmonitor_ds->fs_quit) { - oflags = O_CREAT | O_WRONLY; - oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC; - last_slash = strrchr(mixmonitor->filename, '/'); - if ((ext = strrchr(mixmonitor->filename, '.')) && (ext > last_slash)) - *(ext++) = '\0'; - else - ext = "raw"; - - if (!(*fs = ast_writefile(mixmonitor->filename, ext, NULL, oflags, 0, 0666))) { - ast_log(LOG_ERROR, "Cannot open %s.%s\n", mixmonitor->filename, ext); - errflag = 1; - } - } - /* Write out the frame(s) */ if (*fs) { struct ast_frame *cur; for (cur = fr; cur; cur = AST_LIST_NEXT(cur, frame_list)) { - ast_writestream(*fs, cur); + if(ast_writestream(*fs, cur) < 0){ + pbx_builtin_setvar_helper(mixmonitor->autochan->chan, "MIXMONITOR_STATUS", "ERROR"); + ast_log(LOG_ERROR, "Cannot write frame %s.%s\n", mixmonitor->filename, mixmonitor->autochan->chan->name); + errflag = 1; + } } } ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); @@ -423,6 +443,10 @@ struct mixmonitor *mixmonitor; char postprocess2[1024] = ""; size_t len; + struct ast_filestream **fs = NULL; + unsigned int oflags; + char *ext; + int errflag = 0; len = sizeof(*mixmonitor) + strlen(chan->name) + strlen(filename) + 2; @@ -490,6 +514,12 @@ return -1; } + fs = &mixmonitor->mixmonitor_ds->fs; + + ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); + mixmonitor_save_prep(mixmonitor, mixmonitor->filename, fs, &oflags, &errflag, &ext); + ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); + return ast_pthread_create_detached_background(&thread, NULL, mixmonitor_thread, mixmonitor); }