vss.c: Switch from fopen() to open()
authorAndre Noll <maan@systemlinux.org>
Tue, 13 Mar 2007 22:57:04 +0000 (23:57 +0100)
committerAndre Noll <maan@systemlinux.org>
Tue, 13 Mar 2007 22:57:04 +0000 (23:57 +0100)
vss.c

diff --git a/vss.c b/vss.c
index 93790a7..042db04 100644 (file)
--- a/vss.c
+++ b/vss.c
@@ -18,8 +18,9 @@
 
 /** \file vss.c the virtual streaming system
  *
- * This contains the audio sending part of para_server which is independent of
- * the current audio format, audio file selector and of the activated senders.
+ * This contains the audio streaming code of para_server which is independent
+ * of the current audio format, audio file selector and of the activated
+ * senders.
  */
 
 #include "server.h"
@@ -44,7 +45,7 @@ extern struct misc_meta_data *mmd;
 extern struct audio_file_selector selectors[];
 extern struct sender senders[];
 
-static FILE *audio_file = NULL;
+static int audio_file;
 static char *map;
 
 #if 1
@@ -225,10 +226,8 @@ static int get_audio_format(int omit)
        FOR_EACH_AUDIO_FORMAT(i) {
                if (i == omit)
                        continue;
-               rewind(audio_file);
                if (get_file_info(i) > 0)
                        return i;
-               rewind(audio_file);
        }
        return -E_AUDIO_FORMAT;
 }
@@ -238,14 +237,8 @@ static int get_audio_format(int omit)
  */
 static int update_mmd(void)
 {
-       int i, fd = fileno(audio_file);
-       struct stat file_status;
+       int i;
 
-       if (fstat(fd, &file_status) == -1)
-               return -E_FSTAT;
-       mmd->size = file_status.st_size;
-       mmd->mtime = file_status.st_mtime;
-       map = para_mmap(file_status.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        i = guess_audio_format(mmd->filename);
        if (i < 0 || get_file_info(i) < 0)
                i = get_audio_format(i);
@@ -256,7 +249,6 @@ static int update_mmd(void)
        mmd->current_chunk = 0;
        mmd->offset = 0;
        mmd->events++;
-       PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
        return 1;
 }
 
@@ -264,6 +256,7 @@ static void vss_get_audio_file(void)
 {
        char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
        int i;
+       struct stat file_status;
 
        if (!sl)
                goto err_out;
@@ -272,27 +265,35 @@ static void vss_get_audio_file(void)
                PARA_INFO_LOG("trying %s\n", sl[i]);
                if (strlen(sl[i]) >= _POSIX_PATH_MAX)
                        continue;
-               audio_file = fopen(sl[i], "r");
-               if (!audio_file)
+               audio_file = open(sl[i], O_RDONLY);
+               if (audio_file < 0)
+                       continue;
+               if (fstat(audio_file, &file_status) == -1) {
+                       close(audio_file);
                        continue;
+               }
+               mmd->size = file_status.st_size;
+               mmd->mtime = file_status.st_mtime;
+               map = para_mmap(file_status.st_size, PROT_READ, MAP_PRIVATE,
+                       audio_file, 0);
                strcpy(mmd->filename, sl[i]);
-               if (update_mmd() < 0) {
-                       fclose(audio_file);
-                       audio_file = NULL;
+               if (update_mmd() < 0) { /* invalid file */
+                       close(audio_file);
+                       munmap(map, mmd->size);
+                       map = NULL;
                        continue;
                }
                mmd->num_played++;
                if (selectors[mmd->selector_num].update_audio_file)
                        selectors[mmd->selector_num].update_audio_file(sl[i]);
-               PARA_DEBUG_LOG("%s", "success\n");
+               PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
                mmd->new_vss_status_flags &= (~VSS_NEXT);
                gettimeofday(&now, NULL);
                tv_add(&now, &announce_tv, &data_send_barrier);
-
                goto free;
        }
-       PARA_ERROR_LOG("%s", "no valid files found\n");
 err_out:
+       PARA_ERROR_LOG("%s", "no valid files found\n");
        mmd->new_vss_status_flags = VSS_NEXT;
 free:
        if (sl) {
@@ -348,7 +349,7 @@ static struct timeval *vss_compute_timeout(void)
        if (chk_barrier("data send", &now, &data_send_barrier,
                        &the_timeout, 1) < 0)
                return &the_timeout;
-       if (mmd->audio_format < 0 || !vss_playing() || !audio_file)
+       if (mmd->audio_format < 0 || !vss_playing() || !map)
                return NULL;
        vss_next_chunk_time(&next_chunk);
        if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
@@ -366,7 +367,7 @@ static void vss_eof(struct audio_format_handler *af)
        int i;
        char *tmp;
 
-       if (!af || !audio_file) {
+       if (!af || !map) {
                for (i = 0; senders[i].name; i++)
                        senders[i].shutdown_clients();
                return;
@@ -374,8 +375,8 @@ static void vss_eof(struct audio_format_handler *af)
        gettimeofday(&now, NULL);
        tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
        munmap(map, mmd->size);
-       fclose(audio_file);
-       audio_file = NULL;
+       map = NULL;
+       close(audio_file);
        mmd->audio_format = -1;
        af = NULL;
        mmd->chunks_sent = 0;
@@ -494,7 +495,7 @@ again:
                mmd->current_chunk = mmd->repos_request;
        }
        ret = vss_compute_timeout();
-       if (!ret && !audio_file && vss_playing() &&
+       if (!ret && !map && vss_playing() &&
                        !(mmd->new_vss_status_flags & VSS_NOMORE)) {
                PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
                vss_get_audio_file();
@@ -519,7 +520,7 @@ void vss_send_chunk(void)
        ssize_t pos, len;
        struct timeval now, due;
 
-       if (mmd->audio_format < 0 || !audio_file || !vss_playing())
+       if (mmd->audio_format < 0 || !map || !vss_playing())
                return;
        af = &afl[mmd->audio_format];
        gettimeofday(&now, NULL);