Use FILE * instead of fd for files to support buffering
[asterisk/asterisk.git] / formats / format_ogg_vorbis.c
index e8cb733..b39a774 100755 (executable)
@@ -54,7 +54,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 struct ast_filestream {
        void *reserved[AST_RESERVED_POINTERS];
 
-       int fd;
+       FILE *f;
 
        /* structures for handling the Ogg container */
        ogg_sync_state   oy;
@@ -92,10 +92,10 @@ static char *exts = "ogg";
 
 /*!
  * \brief Create a new OGG/Vorbis filestream and set it up for reading.
- * \param fd Descriptor that points to on disk storage of the OGG/Vorbis data.
+ * \param f File that points to on disk storage of the OGG/Vorbis data.
  * \return The new filestream.
  */
-static struct ast_filestream *ogg_vorbis_open(int fd)
+static struct ast_filestream *ogg_vorbis_open(FILE *f)
 {
        int i;
        int bytes;
@@ -109,12 +109,12 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
                memset(tmp, 0, sizeof(struct ast_filestream));
 
                tmp->writing = 0;
-               tmp->fd = fd;
+               tmp->f = f;
 
                ogg_sync_init(&tmp->oy);
 
                buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
-               bytes = read(tmp->fd, buffer, BLOCK_SIZE);
+               bytes = fread(buffer, 1, BLOCK_SIZE, f);
                ogg_sync_wrote(&tmp->oy, bytes);
 
                result = ogg_sync_pageout(&tmp->oy, &tmp->og);
@@ -124,7 +124,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
                        } else {
                                ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
                        }
-                       close(fd);
+                       fclose(f);
                        ogg_sync_clear(&tmp->oy);
                        free(tmp);
                        return NULL;
@@ -136,7 +136,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
 
                if(ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { 
                        ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
-                       close(fd);
+                       fclose(f);
                        ogg_stream_clear(&tmp->os);
                        vorbis_comment_clear(&tmp->vc);
                        vorbis_info_clear(&tmp->vi);
@@ -147,7 +147,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
                
                if(ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { 
                        ast_log(LOG_ERROR, "Error reading initial header packet.\n");
-                       close(fd);
+                       fclose(f);
                        ogg_stream_clear(&tmp->os);
                        vorbis_comment_clear(&tmp->vc);
                        vorbis_info_clear(&tmp->vi);
@@ -158,7 +158,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
                
                if(vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { 
                        ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
-                       close(fd);
+                       fclose(f);
                        ogg_stream_clear(&tmp->os);
                        vorbis_comment_clear(&tmp->vc);
                        vorbis_info_clear(&tmp->vi);
@@ -181,7 +181,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
                                                        break;
                                                if(result < 0) {
                                                        ast_log(LOG_ERROR, "Corrupt secondary header.  Exiting.\n");
-                                                       close(fd);
+                                                       fclose(f);
                                                        ogg_stream_clear(&tmp->os);
                                                        vorbis_comment_clear(&tmp->vc);
                                                        vorbis_info_clear(&tmp->vi);
@@ -196,10 +196,10 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
                        }
 
                        buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
-                       bytes = read(tmp->fd, buffer, BLOCK_SIZE);
+                       bytes = fread(buffer, 1, BLOCK_SIZE, f);
                        if(bytes == 0 && i < 2) {
                                ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
-                               close(fd);
+                               fclose(f);
                                ogg_stream_clear(&tmp->os);
                                vorbis_comment_clear(&tmp->vc);
                                vorbis_info_clear(&tmp->vi);
@@ -231,7 +231,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
 
                if(tmp->vi.rate != 8000) {
                        ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
-                       close(fd);
+                       fclose(f);
                        ogg_stream_clear(&tmp->os);
                        vorbis_block_clear(&tmp->vb);
                        vorbis_dsp_clear(&tmp->vd);
@@ -247,7 +247,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
 
                if(ast_mutex_lock(&ogg_vorbis_lock)) {
                        ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
-                       close(fd);
+                       fclose(f);
                        ogg_stream_clear(&tmp->os);
                        vorbis_block_clear(&tmp->vb);
                        vorbis_dsp_clear(&tmp->vd);
@@ -266,11 +266,11 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
 
 /*!
  * \brief Create a new OGG/Vorbis filestream and set it up for writing.
- * \param fd File descriptor that points to on-disk storage.
+ * \param f File pointer that points to on-disk storage.
  * \param comment Comment that should be embedded in the OGG/Vorbis file.
  * \return A new filestream.
  */
-static struct ast_filestream *ogg_vorbis_rewrite(int fd, const char *comment)
+static struct ast_filestream *ogg_vorbis_rewrite(FILE *f, const char *comment)
 {
        ogg_packet header;
        ogg_packet header_comm;
@@ -282,7 +282,7 @@ static struct ast_filestream *ogg_vorbis_rewrite(int fd, const char *comment)
                memset(tmp, 0, sizeof(struct ast_filestream));
 
                tmp->writing = 1;
-               tmp->fd = fd;
+               tmp->f = f;
 
                vorbis_info_init(&tmp->vi);
 
@@ -310,15 +310,15 @@ static struct ast_filestream *ogg_vorbis_rewrite(int fd, const char *comment)
                while(!tmp->eos) {
                        if(ogg_stream_flush(&tmp->os, &tmp->og) == 0)
                                break;
-                       write(tmp->fd, tmp->og.header, tmp->og.header_len);
-                       write(tmp->fd, tmp->og.body, tmp->og.body_len);
+                       fwrite(tmp->og.header, 1, tmp->og.header_len, tmp->f);
+                       fwrite(tmp->og.body, 1, tmp->og.body_len, tmp->f);
                        if(ogg_page_eos(&tmp->og))
                                tmp->eos = 1;
                }
 
                if(ast_mutex_lock(&ogg_vorbis_lock)) {
                        ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
-                       close(fd);
+                       fclose(f);
                        ogg_stream_clear(&tmp->os);
                        vorbis_block_clear(&tmp->vb);
                        vorbis_dsp_clear(&tmp->vd);
@@ -350,8 +350,8 @@ static void write_stream(struct ast_filestream *s)
                                if(ogg_stream_pageout(&s->os, &s->og) == 0) {
                                        break;
                                }
-                               write(s->fd, s->og.header, s->og.header_len);
-                               write(s->fd, s->og.body, s->og.body_len);
+                               fwrite(s->og.header, 1, s->og.header_len, s->f);
+                               fwrite(s->og.body, 1, s->og.body_len, s->f);
                                if(ogg_page_eos(&s->og)) {
                                        s->eos = 1;
                                }
@@ -434,7 +434,7 @@ static void ogg_vorbis_close(struct ast_filestream *s)
                ogg_sync_clear(&s->oy);
        }
        
-       close(s->fd);
+       fclose(s->f);
        free(s);
 }
 
@@ -503,7 +503,7 @@ static int read_samples(struct ast_filestream *s, float ***pcm)
                        /* get a buffer from OGG to read the data into */
                        buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
                        /* read more data from the file descriptor */
-                       bytes = read(s->fd, buffer, BLOCK_SIZE);
+                       bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
                        /* Tell OGG how many bytes we actually read into the buffer */
                        ogg_sync_wrote(&s->oy, bytes);
                        if(bytes == 0) {