remove unused variable
[asterisk/asterisk.git] / formats / format_pcm_alaw.c
old mode 100755 (executable)
new mode 100644 (file)
index c9efba1..ffbb17a
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2006, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
  * at the top of the source tree.
  */
 
-/*
+/*! \file
  *
- * Flat, binary, alaw PCM file format.
- * 
+ * \brief Flat, binary, alaw PCM file format.
+ * \arg File name extensions: alaw, al
+ * \ingroup formats
  */
  
 #include <unistd.h>
@@ -44,6 +45,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/sched.h"
 #include "asterisk/module.h"
 #include "asterisk/endian.h"
+#include "asterisk/alaw.h"
 
 #define BUF_SIZE 160           /* 160 samples */
 
@@ -72,6 +74,8 @@ static char *name = "alaw";
 static char *desc = "Raw aLaw 8khz PCM Audio support";
 static char *exts = "alaw|al";
 
+static char alaw_silence[BUF_SIZE];
+
 
 #if 0
 /* Returns time in msec since system boot. */
@@ -247,24 +251,52 @@ 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=0,min,cur,max;
+       long cur, max, offset = 0;
+       int ret = -1; /* assume error */
 
-       min = 0;
        cur = ftell(fs->f);
        fseek(fs->f, 0, SEEK_END);
        max = ftell(fs->f);
-       if (whence == SEEK_SET)
+
+       switch (whence) {
+       case SEEK_SET:
                offset = sample_offset;
-       else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
-               offset = sample_offset + cur;
-       else if (whence == SEEK_END)
+               break;
+       case SEEK_END:
                offset = max - sample_offset;
-       if (whence != SEEK_FORCECUR) {
-               offset = (offset > max)?max:offset;
+               break;
+       case SEEK_CUR:
+       case SEEK_FORCECUR:
+               offset = cur + sample_offset;
+               break;
+       default:
+               ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
+               offset = sample_offset;
+       }
+
+       if (offset < 0) {
+               offset = 0;
+               ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", offset);
        }
-       /* Always protect against seeking past begining */
-       offset = (offset < min)?min:offset;
-       return fseek(fs->f, offset, SEEK_SET);
+       if (whence == SEEK_FORCECUR && offset > max) {
+               size_t left = offset - max;
+
+               while (left) {
+                       size_t written = fwrite(alaw_silence, sizeof(alaw_silence[0]),
+                                    (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
+                       if (written == -1)
+                               break; /* error */
+                       left -= written * sizeof(alaw_silence[0]);
+               }
+               ret = 0; /* success */
+       } else {
+                if (offset > max) {
+                        ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", offset, max);
+                        offset = max;
+               }
+                ret = fseek(fs->f, offset, SEEK_SET);
+       }
+       return ret;
 }
 
 static int pcm_trunc(struct ast_filestream *fs)
@@ -287,16 +319,21 @@ static char *pcm_getcomment(struct ast_filestream *s)
 
 int load_module()
 {
+       int index;
+
+       for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
+               alaw_silence[index] = AST_LIN2A(0);
+
        return ast_format_register(name, exts, AST_FORMAT_ALAW,
-                                                               pcm_open,
-                                                               pcm_rewrite,
-                                                               pcm_write,
-                                                               pcm_seek,
-                                                               pcm_trunc,
-                                                               pcm_tell,
-                                                               pcm_read,
-                                                               pcm_close,
-                                                               pcm_getcomment);
+                                  pcm_open,
+                                  pcm_rewrite,
+                                  pcm_write,
+                                  pcm_seek,
+                                  pcm_trunc,
+                                  pcm_tell,
+                                  pcm_read,
+                                  pcm_close,
+                                  pcm_getcomment);
 }
 
 int unload_module()