Fix for formats so they give better output on failure conditions. (#6141)
authorMatthew Fredrickson <creslin@digium.com>
Fri, 20 Jan 2006 21:36:33 +0000 (21:36 +0000)
committerMatthew Fredrickson <creslin@digium.com>
Fri, 20 Jan 2006 21:36:33 +0000 (21:36 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8357 65c4cc65-6c06-0410-ace0-fbb531ad65f3

formats/format_pcm.c
formats/format_pcm_alaw.c

index 289038c..da72a55 100644 (file)
@@ -177,6 +177,7 @@ 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)
 {
        long cur, max, offset = 0;
+       int ret = -1;   /* assume error */
 
        cur = ftell(fs->f);
        fseek(fs->f, 0, SEEK_END);
@@ -193,29 +194,33 @@ static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
        case SEEK_FORCECUR:
                offset = cur + sample_offset;
                break;
+       default:
+               ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
+               offset = sample_offset;
        }
-
-       switch (whence) {
-       case SEEK_FORCECUR:
+       if (offset < 0) {
+               ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", offset);
+               offset = 0;
+       }
+       if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
+               size_t left = offset - max;
+
+               while (left) {
+                       size_t written = fwrite(ulaw_silence, sizeof(ulaw_silence[0]),
+                                    (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
+                       if (written == -1)
+                               break;  /* error */
+                       left -= written * sizeof(ulaw_silence[0]);
+               }
+               ret = 0; /* successful */
+       } else {
                if (offset > max) {
-                       size_t left = offset - max;
-                       size_t res;
-
-                       while (left) {
-                               res = fwrite(ulaw_silence, sizeof(ulaw_silence[0]),
-                                            (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
-                               if (res == -1)
-                                       return res;
-                               left -= res * sizeof(ulaw_silence[0]);
-                       }
-                       return offset;
+                       ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", offset, max);
+                       offset = max;
                }
-               /* fall through */
-       default:
-               offset = (offset > max) ? max : offset;
-               offset = (offset < 0) ? 0 : offset;
-               return fseek(fs->f, offset, SEEK_SET);
+               ret = fseek(fs->f, offset, SEEK_SET);
        }
+       return ret;
 }
 
 static int pcm_trunc(struct ast_filestream *fs)
index 13d7c17..ffbb17a 100644 (file)
@@ -252,6 +252,7 @@ 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)
 {
        long cur, max, offset = 0;
+       int ret = -1; /* assume error */
 
        cur = ftell(fs->f);
        fseek(fs->f, 0, SEEK_END);
@@ -268,29 +269,34 @@ static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
        case SEEK_FORCECUR:
                offset = cur + sample_offset;
                break;
+       default:
+               ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
+               offset = sample_offset;
        }
 
-       switch (whence) {
-       case SEEK_FORCECUR:
-               if (offset > max) {
-                       size_t left = offset - max;
-                       size_t res;
-
-                       while (left) {
-                               res = fwrite(alaw_silence, sizeof(alaw_silence[0]),
-                                            (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
-                               if (res == -1)
-                                       return res;
-                               left -= res * sizeof(alaw_silence[0]);
-                       }
-                       return offset;
+       if (offset < 0) {
+               offset = 0;
+               ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", offset);
+       }
+       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]);
                }
-               /* fall through */
-       default:
-               offset = (offset > max) ? max : offset;
-               offset = (offset < 0) ? 0 : offset;
-               return fseek(fs->f, offset, SEEK_SET);
+               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)