Blocked revisions 72597 via svnmerge
[asterisk/asterisk.git] / main / file.c
index c259d5a..699df5a 100644 (file)
@@ -112,7 +112,7 @@ int ast_format_unregister(const char *name)
        AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
                if (!strcasecmp(name, tmp->name)) {
                        AST_RWLIST_REMOVE_CURRENT(&formats, list);
-                       free(tmp);
+                       ast_free(tmp);
                        res = 0;
                }
        }
@@ -137,6 +137,11 @@ int ast_stopstream(struct ast_channel *tmp)
                if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
                        ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
        }
+       /* Stop the video stream too */
+       if (tmp->vstream != NULL) {
+               ast_closestream(tmp->vstream);
+               tmp->vstream = NULL;
+       }
        return 0;
 }
 
@@ -150,8 +155,7 @@ int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
                        if (!fs->vfs && fs->filename) {
                                const char *type = ast_getformatname(f->subclass & ~0x1);
                                fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "Opened video output file\n");
+                               ast_debug(1, "Opened video output file\n");
                        }
                        if (fs->vfs)
                                return ast_writestream(fs->vfs, f);
@@ -367,7 +371,7 @@ static int ast_filehelper(const char *filename, const void *arg2, const char *fm
                                continue;
 
                        if ( stat(fn, &st) ) { /* file not existent */
-                               free(fn);
+                               ast_free(fn);
                                continue;
                        }
                        /* for 'OPEN' we need to be sure that the format matches
@@ -380,23 +384,23 @@ static int ast_filehelper(const char *filename, const void *arg2, const char *fm
 
                                if ( !(chan->writeformat & f->format) &&
                                     !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
-                                       free(fn);
+                                       ast_free(fn);
                                        continue;       /* not a supported format */
                                }
                                if ( (bfile = fopen(fn, "r")) == NULL) {
-                                       free(fn);
+                                       ast_free(fn);
                                        continue;       /* cannot open file */
                                }
                                s = get_filestream(f, bfile);
                                if (!s) {
                                        fclose(bfile);
-                                       free(fn);       /* cannot allocate descriptor */
+                                       ast_free(fn);   /* cannot allocate descriptor */
                                        continue;
                                }
                                if (open_wrapper(s)) {
                                        fclose(bfile);
-                                       free(fn);
-                                       free(s);
+                                       ast_free(fn);
+                                       ast_free(s);
                                        continue;       /* cannot run open on file */
                                }
                                /* ok this is good for OPEN */
@@ -405,11 +409,16 @@ static int ast_filehelper(const char *filename, const void *arg2, const char *fm
                                s->fmt = f;
                                s->trans = NULL;
                                s->filename = NULL;
-                               if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
+                               if (s->fmt->format < AST_FORMAT_MAX_AUDIO) {
+                                       if (chan->stream)
+                                               ast_closestream(chan->stream);
                                        chan->stream = s;
-                               else
+                               } else {
+                                       if (chan->vstream)
+                                               ast_closestream(chan->vstream);
                                        chan->vstream = s;
-                               free(fn);
+                               }
+                               ast_free(fn);
                                break;
                        }
                        switch (action) {
@@ -436,7 +445,7 @@ static int ast_filehelper(const char *filename, const void *arg2, const char *fm
                                                ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
                                                        action == ACTION_COPY ? "copy" : "rename",
                                                         fn, nfn, strerror(errno));
-                                       free(nfn);
+                                       ast_free(nfn);
                                }
                            }
                                break;
@@ -444,7 +453,7 @@ static int ast_filehelper(const char *filename, const void *arg2, const char *fm
                        default:
                                ast_log(LOG_WARNING, "Unknown helper %d\n", action);
                        }
-                       free(fn);
+                       ast_free(fn);
                }
        }
        AST_RWLIST_UNLOCK(&formats);
@@ -722,16 +731,16 @@ int ast_closestream(struct ast_filestream *f)
        }
 
        if (f->filename)
-               free(f->filename);
+               ast_free(f->filename);
        if (f->realfilename)
-               free(f->realfilename);
+               ast_free(f->realfilename);
        if (f->fmt->close)
                f->fmt->close(f);
        fclose(f->f);
        if (f->vfs)
                ast_closestream(f->vfs);
        ast_module_unref(f->fmt->module);
-       free(f);
+       ast_free(f);
        return 0;
 }
 
@@ -778,20 +787,18 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
        if (fs)
                vfs = ast_openvstream(chan, filename, preflang);
        if (vfs) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
+               ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
        }
        if (fs){
                if (ast_applystream(chan, fs))
                        return -1;
                if (vfs && ast_applystream(chan, vfs))
                        return -1;
-               if (ast_playstream(fs))
-                       return -1;
-               if (vfs && ast_playstream(vfs))
-                       return -1;
+               ast_playstream(fs);
+               if (vfs)
+                       ast_playstream(vfs);
                if (option_verbose > 2)
-                       ast_verbose(VERBOSE_PREFIX_3 "<%s> Playing '%s' (language '%s')\n", chan->name, filename, preflang ? preflang : "default");
+                       ast_verbose(VERBOSE_PREFIX_3 "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
 
                return 0;
        }
@@ -820,10 +827,10 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
                    open_wrapper(fs) ) {
                        ast_log(LOG_WARNING, "Unable to open %s\n", fn);
                        if (fs)
-                               free(fs);
+                               ast_free(fs);
                        if (bfile)
                                fclose(bfile);
-                       free(fn);
+                       ast_free(fn);
                        continue;
                }
                /* found it */
@@ -831,7 +838,7 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
                fs->fmt = f;
                fs->flags = flags;
                fs->mode = mode;
-               fs->filename = strdup(filename);
+               fs->filename = ast_strdup(filename);
                fs->vfs = NULL;
                break;
        }
@@ -909,7 +916,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                        strcpy(buf, record_cache_dir);
                        strcat(buf, "/");
                        strcat(buf, fn);
-                       free(fn);
+                       ast_free(fn);
                        fn = buf;
                        fd = open(fn, flags | myflags, mode);
                        if (fd > -1) {
@@ -933,18 +940,20 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                                        unlink(orig_fn);
                                }
                                if (fs)
-                                       free(fs);
+                                       ast_free(fs);
+                               fs = NULL;
+                               continue;
                        }
                        fs->trans = NULL;
                        fs->fmt = f;
                        fs->flags = flags;
                        fs->mode = mode;
                        if (orig_fn) {
-                               fs->realfilename = strdup(orig_fn);
-                               fs->filename = strdup(fn);
+                               fs->realfilename = ast_strdup(orig_fn);
+                               fs->filename = ast_strdup(fn);
                        } else {
                                fs->realfilename = NULL;
-                               fs->filename = strdup(filename);
+                               fs->filename = ast_strdup(filename);
                        }
                        fs->vfs = NULL;
                        /* If truncated, we'll be at the beginning; if not truncated, then append */
@@ -956,7 +965,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                }
                /* if buf != NULL then fn is already free and pointing to it */
                if (!buf)
-                       free(fn);
+                       ast_free(fn);
        }
 
        AST_RWLIST_UNLOCK(&formats);