Unload g726 file formats in the order they were loaded (bug 1460)
[asterisk/asterisk.git] / formats / format_pcm_alaw.c
index ef17680..db6b0c3 100755 (executable)
@@ -17,6 +17,7 @@
 #include <asterisk/logger.h>
 #include <asterisk/sched.h>
 #include <asterisk/module.h>
+#include <netinet/in.h>
 #include <arpa/inet.h>
 #include <stdlib.h>
 #include <sys/time.h>
@@ -53,7 +54,7 @@ struct ast_filestream {
 };
 
 
-static pthread_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
+static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "alaw";
@@ -61,6 +62,7 @@ static char *desc = "Raw aLaw 8khz PCM Audio support";
 static char *exts = "alaw|al";
 
 
+#if 0
 /* Returns time in msec since system boot. */
 static unsigned long get_time(void)
 {
@@ -75,6 +77,7 @@ static unsigned long get_time(void)
        }
        return cur * 1000 / sysconf( _SC_CLK_TCK );
 }
+#endif
 
 static struct ast_filestream *pcm_open(int fd)
 {
@@ -84,7 +87,7 @@ static struct ast_filestream *pcm_open(int fd)
        struct ast_filestream *tmp;
        if ((tmp = malloc(sizeof(struct ast_filestream)))) {
                memset(tmp, 0, sizeof(struct ast_filestream));
-               if (pthread_mutex_lock(&pcm_lock)) {
+               if (ast_mutex_lock(&pcm_lock)) {
                        ast_log(LOG_WARNING, "Unable to lock pcm list\n");
                        free(tmp);
                        return NULL;
@@ -100,7 +103,7 @@ static struct ast_filestream *pcm_open(int fd)
                tmp->start_time = get_time();
 #endif
                glistcnt++;
-               pthread_mutex_unlock(&pcm_lock);
+               ast_mutex_unlock(&pcm_lock);
                ast_update_use_count();
        }
        return tmp;
@@ -114,7 +117,7 @@ static struct ast_filestream *pcm_rewrite(int fd, char *comment)
        struct ast_filestream *tmp;
        if ((tmp = malloc(sizeof(struct ast_filestream)))) {
                memset(tmp, 0, sizeof(struct ast_filestream));
-               if (pthread_mutex_lock(&pcm_lock)) {
+               if (ast_mutex_lock(&pcm_lock)) {
                        ast_log(LOG_WARNING, "Unable to lock pcm list\n");
                        free(tmp);
                        return NULL;
@@ -124,7 +127,7 @@ static struct ast_filestream *pcm_rewrite(int fd, char *comment)
                tmp->start_time = get_time();
 #endif
                glistcnt++;
-               pthread_mutex_unlock(&pcm_lock);
+               ast_mutex_unlock(&pcm_lock);
                ast_update_use_count();
        } else
                ast_log(LOG_WARNING, "Out of memory\n");
@@ -133,12 +136,12 @@ static struct ast_filestream *pcm_rewrite(int fd, char *comment)
 
 static void pcm_close(struct ast_filestream *s)
 {
-       if (pthread_mutex_lock(&pcm_lock)) {
+       if (ast_mutex_lock(&pcm_lock)) {
                ast_log(LOG_WARNING, "Unable to lock pcm list\n");
                return;
        }
        glistcnt--;
-       pthread_mutex_unlock(&pcm_lock);
+       ast_mutex_unlock(&pcm_lock);
        ast_update_use_count();
        close(s->fd);
        free(s);
@@ -237,18 +240,21 @@ static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
 
 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
 {
-       off_t offset,min,cur,max;
+       off_t offset=0,min,cur,max;
 
        min = 0;
        cur = lseek(fs->fd, 0, SEEK_CUR);
        max = lseek(fs->fd, 0, SEEK_END);
-       if(whence == SEEK_SET)
+       if (whence == SEEK_SET)
                offset = sample_offset;
-       if(whence == SEEK_CUR)
+       else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
                offset = sample_offset + cur;
-       if(whence == SEEK_END)
+       else if (whence == SEEK_END)
                offset = max - sample_offset;
-       offset = (offset > max)?max:offset;
+       if (whence != SEEK_FORCECUR) {
+               offset = (offset > max)?max:offset;
+       }
+       // Always protect against seeking past begining
        offset = (offset < min)?min:offset;
        return lseek(fs->fd, offset, SEEK_SET);
 }
@@ -293,12 +299,12 @@ int unload_module()
 int usecount()
 {
        int res;
-       if (pthread_mutex_lock(&pcm_lock)) {
+       if (ast_mutex_lock(&pcm_lock)) {
                ast_log(LOG_WARNING, "Unable to lock pcm list\n");
                return -1;
        }
        res = glistcnt;
-       pthread_mutex_unlock(&pcm_lock);
+       ast_mutex_unlock(&pcm_lock);
        return res;
 }