Version 0.3.0 from FTP
[asterisk/asterisk.git] / formats / format_mp3.c
index 85abdbc..e702e55 100755 (executable)
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -46,7 +47,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t mp3_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t mp3_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "mp3";
@@ -62,7 +63,7 @@ static struct ast_filestream *mp3_open(int fd)
           and be sure it's a valid file.  */
        struct ast_filestream *tmp;
        if ((tmp = malloc(sizeof(struct ast_filestream)))) {
-               if (pthread_mutex_lock(&mp3_lock)) {
+               if (ast_pthread_mutex_lock(&mp3_lock)) {
                        ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
                        free(tmp);
                        return NULL;
@@ -76,7 +77,7 @@ static struct ast_filestream *mp3_open(int fd)
                tmp->last.tv_sec = 0;
                tmp->adj = 0;
                glistcnt++;
-               pthread_mutex_unlock(&mp3_lock);
+               ast_pthread_mutex_unlock(&mp3_lock);
                ast_update_use_count();
        }
        return tmp;
@@ -89,7 +90,7 @@ static struct ast_filestream *mp3_rewrite(int fd, char *comment)
           and be sure it's a valid file.  */
        struct ast_filestream *tmp;
        if ((tmp = malloc(sizeof(struct ast_filestream)))) {
-               if (pthread_mutex_lock(&mp3_lock)) {
+               if (ast_pthread_mutex_lock(&mp3_lock)) {
                        ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
                        free(tmp);
                        return NULL;
@@ -99,7 +100,7 @@ static struct ast_filestream *mp3_rewrite(int fd, char *comment)
                tmp->fd = fd;
                tmp->owner = NULL;
                glistcnt++;
-               pthread_mutex_unlock(&mp3_lock);
+               ast_pthread_mutex_unlock(&mp3_lock);
                ast_update_use_count();
        } else
                ast_log(LOG_WARNING, "Out of memory\n");
@@ -114,7 +115,7 @@ static struct ast_frame *mp3_read(struct ast_filestream *s)
 static void mp3_close(struct ast_filestream *s)
 {
        struct ast_filestream *tmp, *tmpl = NULL;
-       if (pthread_mutex_lock(&mp3_lock)) {
+       if (ast_pthread_mutex_lock(&mp3_lock)) {
                ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
                return;
        }
@@ -137,7 +138,7 @@ static void mp3_close(struct ast_filestream *s)
                        ast_sched_del(s->owner->sched, s->owner->streamid);
                s->owner->streamid = -1;
        }
-       pthread_mutex_unlock(&mp3_lock);
+       ast_pthread_mutex_unlock(&mp3_lock);
        ast_update_use_count();
        if (!tmp) 
                ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
@@ -187,10 +188,11 @@ static int ast_read_callback(void *data)
                gettimeofday(&tv, NULL);
                ms = ((tv.tv_usec - s->last.tv_usec) / 1000 + (tv.tv_sec - s->last.tv_sec) * 1000);
                /* If we're within 2 milliseconds, that's close enough */
-               if ((ms - delay) * (ms - delay) > 4)
+               if ((ms - delay) > 0 )
                        s->adj -= (ms - delay);
+               s->adj -= 2;
        }
-       s->fr.timelen = delay;
+       s->fr.samples = delay * 8;
 #if 0
        ast_log(LOG_DEBUG, "delay is %d, adjusting by %d, as last was %d\n", delay, s->adj, ms);
 #endif
@@ -216,6 +218,11 @@ static int mp3_apply(struct ast_channel *c, struct ast_filestream *s)
 {
        /* Select our owner for this stream, and get the ball rolling. */
        s->owner = c;
+       return 0;
+}
+
+static int mp3_play(struct ast_filestream *s)
+{
        ast_read_callback(s);
        return 0;
 }
@@ -238,6 +245,21 @@ static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
        return 0;
 }
 
+static int mp3_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+       return -1;
+}
+
+static int mp3_trunc(struct ast_filestream *fs)
+{
+       return -1;
+}
+
+static long mp3_tell(struct ast_filestream *fs)
+{
+       return -1;
+}
+
 static char *mp3_getcomment(struct ast_filestream *s)
 {
        return NULL;
@@ -249,7 +271,11 @@ int load_module()
                                                                mp3_open,
                                                                mp3_rewrite,
                                                                mp3_apply,
+                                                               mp3_play,
                                                                mp3_write,
+                                                               mp3_seek,
+                                                               mp3_trunc,
+                                                               mp3_tell,
                                                                mp3_read,
                                                                mp3_close,
                                                                mp3_getcomment);
@@ -260,31 +286,31 @@ int load_module()
 int unload_module()
 {
        struct ast_filestream *tmp, *tmpl;
-       if (pthread_mutex_lock(&mp3_lock)) {
+       if (ast_pthread_mutex_lock(&mp3_lock)) {
                ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
                return -1;
        }
        tmp = glist;
        while(tmp) {
                if (tmp->owner)
-                       ast_softhangup(tmp->owner);
+                       ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
                tmpl = tmp;
                tmp = tmp->next;
                free(tmpl);
        }
-       pthread_mutex_unlock(&mp3_lock);
+       ast_pthread_mutex_unlock(&mp3_lock);
        return ast_format_unregister(name);
 }      
 
 int usecount()
 {
        int res;
-       if (pthread_mutex_lock(&mp3_lock)) {
+       if (ast_pthread_mutex_lock(&mp3_lock)) {
                ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
                return -1;
        }
        res = glistcnt;
-       pthread_mutex_unlock(&mp3_lock);
+       ast_pthread_mutex_unlock(&mp3_lock);
        return res;
 }
 
@@ -293,3 +319,8 @@ char *description()
        return desc;
 }
 
+
+char *key()
+{
+       return ASTERISK_GPL_KEY;
+}