]> 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:36 +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 3b2195a30b8fa8d5b6fec81c3ac582b9c8705153..e6ecd794f03e41fa208ed618f334289008bf7c0f 100644 (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 fff6ed07bbc817b7c96d22ab61b1287c2b54f657..a88d132d9ab4b0c5fd76f2ca6407ea8d86474db7 100644 (file)
@@ -40,7 +40,7 @@
 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 ? */
@@ -65,15 +65,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 33f9639537af0b413a4dea3b19a6766cb1bfde91..366f85d8dc59f0070de138a880e84089070941b4 100644 (file)
@@ -117,22 +117,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 91dc855f17391e3e9cf9d6162d6a6175053da802..324371c33bc7c92af4ecf82b20bb9b548a027c1e 100644 (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 b737c97e5fec59e611d782941793e4eb89192344..70600b47f3a2033351d50ab98622bb5c7f837070 100644 (file)
@@ -53,19 +53,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 586e2d8e548f659d7e0657f21315a6e608484545..97bca21f0d2188aecc8b4b24809fd659babd834f 100644 (file)
@@ -67,7 +67,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;
@@ -85,15 +85,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 9230129e7ed490ded0f28402ccc5d01baf1e51b4..8860d8831113eb5c4f8e97c5bfb6cba089cc5327 100644 (file)
@@ -59,7 +59,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;
@@ -77,15 +77,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 8b41ab2287409cab03d9c655c5ea2dbb406014c7..326a04ebdffa96ca5a7e65cac1d284ef286e6f89 100644 (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 4891f7ed5b22829603e1c739bcc3e9fbb28accb4..b4fd3b36ab1ff1459bb6bd1b1851b4927c5d11a3 100644 (file)
@@ -78,21 +78,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 e15e20f465ed59245e6707ad5f9489aff1e21ab5..d39096a21248abf27f84067c25efe96fa1519892 100644 (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 298992c3ca0467371e67f84c2b236e070c6d75b9..83967507274f80c8496e6cd21fc922ff1828b2b5 100644 (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 5a5cde777081d86f21c8d1c4a8f3223ad6ae68b0..20985ef90146f1ef3da3efe779afd0e221962d9d 100644 (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 c3da4ab9419e379eb6ffc5e7f0b486cb6fe7bfea..82379f641468a31c2870f0dc93cf52ca2371ff2a 100644 (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 ce8a8bf0ab5be958a244cda9ee49721ab73a4421..b4e1f34f2a55f908d866845110311f72727d2e00 100644 (file)
@@ -369,7 +369,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;
@@ -391,16 +391,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 8d7d87f65795dc29797d62a50d4d383932057b8b..cd1cc6a2a554a1a5438286c71a5152bfb5a29593 100644 (file)
@@ -419,18 +419,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;
                }