]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
formats: Restore previous fread() behavior
authorSean Bright <sean.bright@gmail.com>
Tue, 5 Sep 2017 15:05:48 +0000 (11:05 -0400)
committerSean Bright <sean.bright@gmail.com>
Tue, 5 Sep 2017 15:10:14 +0000 (10:10 -0500)
Some formats are able to handle short reads while others are not, so
restore the previous behavior for the format modules so that we don't
have spurious errors when playing back files.

ASTERISK-27232 #close
Reported by: Jens T.

Change-Id: Iab7f52b25a394f277566c8a2a4b15a692280a300

15 files changed:
formats/format_g719.c
formats/format_g723.c
formats/format_g726.c
formats/format_g729.c
formats/format_gsm.c
formats/format_h263.c
formats/format_h264.c
formats/format_ilbc.c
formats/format_pcm.c
formats/format_siren14.c
formats/format_siren7.c
formats/format_sln.c
formats/format_vox.c
formats/format_wav.c
formats/format_wav_gsm.c

index bc374ee4501ad607de01ffe847c62580fb041e50..a8e7b12d4f491e85985151fc51da945323284611 100644 (file)
@@ -42,20 +42,15 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *g719read(struct ast_filestream *s, int *whennext)
 {
-       int res;
-       /* Send a frame from the file to the appropriate channel */
+       size_t res;
 
+       /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 22e39927a3e8f202f313ab8ff8e1d44fd8774f86..023e38baed1701952c34a06ccab9bfc689b08563 100644 (file)
@@ -42,7 +42,7 @@ ASTERISK_REGISTER_FILE()
 static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
 {
        unsigned short size;
-       int res;
+       size_t res;
        int delay;
        /* Read the delay for the next packet, and schedule again if necessary */
        /* XXX is this ignored ? */
@@ -67,15 +67,10 @@ static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
        /* Read the data into the buffer */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 4d5a91a8f6961586fbaaf9b8d8a1bb5f5f274472..45f0ad9393435ac56fb563ba204740ddc4a13420 100644 (file)
@@ -119,22 +119,17 @@ static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
 
 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
        struct g726_desc *fs = (struct g726_desc *)s->_private;
 
        /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
        s->fr.samples = 8 * FRAME_TIME;
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 48fee3a23108b13170d34a92933e7761c5e649ae..7d6d1cff0c09c0142afcb64d3eba9231d204052d 100644 (file)
@@ -48,20 +48,16 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
+
        /* Send a frame from the file to the appropriate channel */
        s->fr.samples = G729A_SAMPLES;
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res && res != 10) /* XXX what for ? */ {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 7e7c54da3849575ac467ea550c6fdfd5452a8ab5..30b12f1a776f431574b02e6112a2a5a9e0056ee8 100644 (file)
@@ -55,19 +55,14 @@ static const char gsm_silence[] = /* 33 */
 
 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
 
        AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), GSM_FRAME_SIZE, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), GSM_FRAME_SIZE, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index a3ca933462c09e6c4ae9533b7f9e2b9ee383d63d..e56cbe99a749c837b4f6d6d608bab973cee52c9c 100644 (file)
@@ -69,7 +69,7 @@ static int h263_open(struct ast_filestream *s)
 
 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
        uint32_t mark;
        unsigned short len;
        unsigned int ts;
@@ -87,15 +87,10 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
        }
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index b89e8ec81791727d8375040e05d4aab4d30ac1fb..47477926dd2e7ae48ce139707d8ad0117225ab4e 100644 (file)
@@ -61,7 +61,7 @@ static int h264_open(struct ast_filestream *s)
 
 static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
        int mark = 0;
        unsigned short len;
        unsigned int ts;
@@ -79,15 +79,10 @@ static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
        }
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 3103611c0ca8cc266b809f8a69e8fbb2fad08ae9..42a28d8d8954968b7e748ac0b6a70e45b7d8f714 100644 (file)
@@ -47,19 +47,15 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
+
        /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 705a03530f6e2da6817e580b7b2826d2cfc76450..c12ef6b1ac76f396df6181c98b8941744048b99f 100644 (file)
@@ -80,21 +80,15 @@ static int pcma_rewrite(struct ast_filestream *s, const char *comment)
 
 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
-       
-       /* Send a frame from the file to the appropriate channel */
+       size_t res;
 
+       /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 15cbe4cfd605e5083cec9c439783ff59e0417145..62c6cb2dedb325d7bd11af4f7ee8ae8dd15f4968 100644 (file)
@@ -42,20 +42,15 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *siren14read(struct ast_filestream *s, int *whennext)
 {
-       int res;
-       /* Send a frame from the file to the appropriate channel */
+       size_t res;
 
+       /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index ff0bca3b48821f32c526a1ed9adff18e8db02f91..3dd4d5ea807c555c76fc15cbaf17ef9b62d7953c 100644 (file)
@@ -42,20 +42,15 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *siren7read(struct ast_filestream *s, int *whennext)
 {
-       int res;
-       /* Send a frame from the file to the appropriate channel */
+       size_t res;
 
+       /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index f68becb0f3b6f739ca91ef7a228765de0ea8cd67..e73916e18c9f66ddacdb52f3b445e7cf7a9d6814 100644 (file)
@@ -36,20 +36,15 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, unsigned int buf_size)
 {
-       int res;
-       /* Send a frame from the file to the appropriate channel */
+       size_t res;
 
+       /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index fc840530f3ca271e4e25e2c8490a611315e2fd47..0775f8556db975bd1b8de7773763488543ac23d8 100644 (file)
@@ -42,20 +42,15 @@ ASTERISK_REGISTER_FILE()
 
 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
 
        /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index 85a22ed8a47355bd95fb4aa220c15ccfc9224013..fcdaf39e30e13f30cf9ab6a3bed0e18574a5ba79 100644 (file)
@@ -371,7 +371,7 @@ static void wav_close(struct ast_filestream *s)
 
 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
 {
-       int res;
+       size_t res;
        int samples;    /* actual samples read */
 #if __BYTE_ORDER == __BIG_ENDIAN
        int x;
@@ -393,16 +393,11 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
 /*     ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
 
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (feof(s->f)) {
-                       if (res) {
-                               ast_debug(3, "Incomplete frame data at end of %s file "
-                                                 "(expected %d bytes, read %d)\n",
-                                                 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
-                       }
-               } else {
-                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) == 0) {
+               if (res) {
+                       ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
+                                       strerror(errno));
                }
                return NULL;
        }
index e480179dd1d751e21bc4754b5ff9f1f1b237855f..3bdeab9e1c01b9887ff1eccc8b45822d7fe6de95 100644 (file)
@@ -421,18 +421,13 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
        } else {
                /* read and convert */
                unsigned char msdata[MSGSM_FRAME_SIZE];
-               int res;
-               
+               size_t res;
+
                if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
-                       if (feof(s->f)) {
-                               if (res) {
-                                       ast_debug(3, "Incomplete frame data at end of %s file "
-                                                         "(expected %d bytes, read %d)\n",
-                                                         ast_format_get_name(s->fr.subclass.format), MSGSM_FRAME_SIZE, res);
-                               }
-                       } else {
-                               ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
-                                               ast_format_get_name(s->fr.subclass.format), strerror(errno));
+                       if (res && res != 1) {
+                               ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
+                                               ast_format_get_name(s->fr.subclass.format), MSGSM_FRAME_SIZE, res,
+                                               strerror(errno));
                        }
                        return NULL;
                }