changeset 773:22c82f3c0411 trunk

[svn] - reduce connection latency to http stream. - remove unnecessary functions. - remove redundant dialog which had been shown when madplug failed to open audio output. - some cleanups.
author yaz
date Fri, 02 Mar 2007 20:28:55 -0800
parents 18dd1dd2d232
children b02927277ccb
files ChangeLog src/madplug/decoder.c src/madplug/input.c src/madplug/plugin.c src/madplug/plugin.h src/madplug/replaygain.c src/madplug/replaygain.h
diffstat 7 files changed, 198 insertions(+), 179 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Mar 02 20:17:50 2007 -0800
+++ b/ChangeLog	Fri Mar 02 20:28:55 2007 -0800
@@ -1,3 +1,11 @@
+2007-03-03 04:17:50 +0000  Yoshiki Yazawa <yaz@cc.rim.or.jp>
+  revision [1642]
+  - include strings.h
+  
+  trunk/src/vorbis/vorbis.c |    1 +
+  1 file changed, 1 insertion(+)
+
+
 2007-03-02 17:36:54 +0000  William Pitcock <nenolod@sacredspiral.co.uk>
   revision [1640]
   - some UI improvements from ccr.
--- a/src/madplug/decoder.c	Fri Mar 02 20:17:50 2007 -0800
+++ b/src/madplug/decoder.c	Fri Mar 02 20:28:55 2007 -0800
@@ -186,11 +186,11 @@
 
     info->bitrate = 0;
     info->pos = mad_timer_zero;
-    info->duration.seconds = 0; // should be cleared before loop, if we use it as break condition.
+    info->duration = mad_timer_zero; // should be cleared before loop, if we use it as break condition.
 
 #ifdef DEBUG
     g_message("f: scan_file");
-    g_message("scan_file frames = %d\n", info->frames);
+    g_message("scan_file frames = %d", info->frames);
 #endif                          /* DEBUG */
 
     while (1) {
@@ -207,8 +207,12 @@
         len = input_get_data(info, buffer + remainder,
                              BUFFER_SIZE - remainder);
 
-        if (len <= 0)
+        if (len <= 0) {
+#ifdef DEBUG
+            g_message("scan_file: len <= 0 abort.");
+#endif
             break;
+        }
 
         mad_stream_buffer(&stream, buffer, len + remainder);
 
@@ -276,7 +280,7 @@
                     frame.header = header;
                     if (mad_frame_decode(&frame, &stream) == -1) {
 #ifdef DEBUG
-                        printf("xing frame decode failed\n");
+                        g_message("xing frame decode failed");
 #endif
                         goto no_xing;
                     }
@@ -323,7 +327,15 @@
         no_xing:
             if (fast && info->frames >= N_AVERAGE_FRAMES) {
                 float frame_size = ((double) data_used) / N_AVERAGE_FRAMES;
-                info->frames = (info->size - tagsize) / frame_size;
+#ifdef DEBUG
+                g_message("info->frames = %d info->size = %d tagsize = %d frame_size = %lf",
+                          info->frames, info->size, tagsize, frame_size);
+#endif
+                if(info->size != 0)
+                    info->frames = (info->size - tagsize) / frame_size;
+#ifdef DEBUG
+                g_message("info->frames = %d", info->frames);
+#endif
                 if(info->tuple->length == -1) {
                     info->duration.seconds /= N_AVERAGE_FRAMES;
                     info->duration.fraction /= N_AVERAGE_FRAMES;
@@ -362,8 +374,7 @@
 #ifdef DEBUG
     g_message("e: scan_file");
 #endif                          /* DEBUG */
-//    return info->frames != 0;
-    return (info->frames != 0 || info->remote == TRUE); // suspicious
+    return (info->frames != 0 || info->remote == TRUE);
 }
 
 gpointer decode_loop(gpointer arg)
@@ -393,17 +404,20 @@
     mad_synth_init(&synth);
 
     if(!info->playback){
-        g_print("decode: playback == NULL\n");
+#ifdef DEBUG
+        g_message("decode: playback == NULL");
+#endif
         return NULL;
     }
 
+#ifdef DEBUG
+    g_message("decode: fmt = %d freq = %d channels = %d", info->fmt, info->freq, info->channels);
+#endif
     if (!info->playback->output->open_audio(info->fmt, info->freq, info->channels)) {
         g_mutex_lock(pb_mutex);
         info->playback->error = TRUE;
         info->playback->eof = 1;
         g_mutex_unlock(pb_mutex);        
-        audmad_error("failed to open audio output: %s",
-                      info->playback->output->description);
         g_message("failed to open audio output: %s",
                   info->playback->output->description);
         return NULL;
@@ -413,13 +427,15 @@
     if (info->title)
         g_free(info->title);
     info->title = xmms_get_titlestring(audmad_config.title_override == TRUE ?
-        audmad_config.id3_format : xmms_get_gentitle_format(), info->tuple);
+                                       audmad_config.id3_format : xmms_get_gentitle_format(), info->tuple);
     
     tlen = (gint) mad_timer_count(info->duration, MAD_UNITS_MILLISECONDS),
-    
-    mad_plugin->set_info(info->title,
-                        tlen == 0 ? -1 : tlen,
-                        info->bitrate, info->freq, info->channels);
+        mad_plugin->set_info(info->title,
+                             tlen == 0 ? -1 : tlen,
+                             info->bitrate, info->freq, info->channels);
+#ifdef DEBUG
+    g_message("decode: tlen = %d\n", tlen);
+#endif
 
     /* main loop */
     do {
@@ -437,6 +453,9 @@
         }
         len = input_get_data(info, buffer + remainder,
                              BUFFER_SIZE - remainder);
+
+        input_process_remote_metadata(info);
+
         if (len <= 0) {
 #ifdef DEBUG
             g_message("finished decoding");
@@ -532,7 +551,7 @@
 
             info->bitrate = frame.header.bitrate;
 
-            if (!audmad_config.show_avg_vbr_bitrate && info->vbr && (iteration % 40 == 0)) {
+            if (!info->remote && !audmad_config.show_avg_vbr_bitrate && info->vbr && (iteration % 40 == 0)) {
                 mad_plugin->set_info(info->title,
                                      tlen == 0 ? -1 : tlen,
                                      info->bitrate, info->freq, info->channels);
--- a/src/madplug/input.c	Fri Mar 02 20:17:50 2007 -0800
+++ b/src/madplug/input.c	Fri Mar 02 20:28:55 2007 -0800
@@ -66,14 +66,6 @@
 
 extern gboolean scan_file(struct mad_info_t *info, gboolean fast);
 
-// new VFS handles url.
-static void input_parse_url(struct mad_info_t *info)
-{
-    info->filename = g_strdup(info->url);
-    return;
-}
-
-
 /**
  * init the mad_info_t struct.
  */
@@ -108,9 +100,16 @@
     info->mp3gain_undo_str = 0;
     info->mp3gain_minmax_str = 0;
 
+    // from input_read_replaygain()
+    info->has_replaygain = FALSE;
+    info->replaygain_album_scale = -1;
+    info->replaygain_track_scale = -1;
+    info->mp3gain_undo = -77;
+    info->mp3gain_minmax = -77;
+
     info->tuple = NULL;
 
-    input_parse_url(info);
+    info->filename = g_strdup(url);
 
     info->infile = vfs_fopen(info->filename, "rb");
     if (info->infile == NULL) {
@@ -332,6 +331,20 @@
     return rtn;
 }
 
+
+static void input_alloc_tag(struct mad_info_t *info)
+{
+    TitleInput *title_input;
+
+    if (info->tuple == NULL) {
+        title_input = bmp_title_input_new();
+        info->tuple = title_input;
+        info->tuple->length = -1; //will be refferd in decoder.c
+    }
+    else
+        title_input = info->tuple;
+}
+
 /**
  * read the ID3 tag 
  */
@@ -350,16 +363,27 @@
     else
         title_input = info->tuple;
 
-    if(info->infile)
-      info->id3file = id3_file_vfsopen(info->infile, ID3_FILE_MODE_READONLY);
+#define REUSE_FD 1
+#if REUSE_FD
+    if(info->infile) {
+        info->id3file = id3_file_vfsopen(info->infile, ID3_FILE_MODE_READONLY);
+    }
     else
+#endif
       info->id3file = id3_file_open(info->filename, ID3_FILE_MODE_READONLY);
+
     if (!info->id3file) {
+#ifdef DEBUG
+        g_message("read_tag: no id3file");
+#endif
         return;
     }
 
     info->tag = id3_file_tag(info->id3file);
     if (!info->tag) {
+#ifdef DEBUG
+        g_message("read_tag: no tag");
+#endif
         return;
     }
 
@@ -418,11 +442,15 @@
 
 }
 
-static void input_process_remote_metadata(struct mad_info_t *info)
+void input_process_remote_metadata(struct mad_info_t *info)
 {
     if(info->remote && mad_timer_count(info->duration, MAD_UNITS_SECONDS) <= 0){
         gchar *tmp = NULL;
-
+#ifdef DEBUG
+#ifdef DEBUG_INTENSIVELY
+        g_message("process_remote_meta");
+#endif
+#endif
         g_free(info->title);
         info->title = NULL;
         g_free(info->tuple->track_name);
@@ -450,7 +478,7 @@
         else if (info->tuple->album_name)
             tmp = g_strdup(info->tuple->album_name);
         else
-            tmp = g_strdup(""); // really?
+            tmp = g_strdup(g_basename(info->filename));
 
         mad_plugin->set_info(tmp,
                              -1, // indicate the stream is unseekable
@@ -467,14 +495,21 @@
 gboolean input_get_info(struct mad_info_t *info, gboolean fast_scan)
 {
 #ifdef DEBUG
-    g_message("f: input_get_info: %s, fast_scan = %s", info->title, fast_scan ? "TRUE" : "FALSE");
+    g_message("f: input_get_info: %s, fast_scan = %s", info->filename, fast_scan ? "TRUE" : "FALSE");
 #endif                          /* DEBUG */
 
-    input_read_tag(info);
-    input_read_replaygain(info);
+    input_alloc_tag(info);
+
+    if(!info->remote) { // reduce startup delay
+        input_read_tag(info);
+        read_replaygain(info);
+    }
 
     /* scan mp3 file, decoding headers */
     if (scan_file(info, fast_scan) == FALSE) {
+#ifdef DEBUG
+        g_message("input_get_info: scan_file failed");
+#endif
         return FALSE;
     }
 
@@ -482,8 +517,6 @@
     vfs_fseek(info->infile, 0, SEEK_SET);
     info->offset = 0;
 
-    input_process_remote_metadata(info);
-
     /* use the filename for the title as a last resort */
     if (!info->title) {
         char *pos = strrchr(info->filename, DIR_SEPARATOR);
@@ -526,8 +559,6 @@
 		    info->playback->eof = TRUE;
     }
 
-    input_process_remote_metadata(info);
-    
 #ifdef DEBUG
 #ifdef DEBUG_INTENSIVELY
     g_message ("e: input_get_data: size=%d offset=%d", len, info->offset);
--- a/src/madplug/plugin.c	Fri Mar 02 20:17:50 2007 -0800
+++ b/src/madplug/plugin.c	Fri Mar 02 20:28:55 2007 -0800
@@ -386,14 +386,8 @@
 #endif                          /* DEBUG */
 
     if (input_init(&myinfo, url) == FALSE) {
+#ifdef DEBUG
         g_message("error initialising input");
-        return;
-    }
-
-    // don't try to get from stopped stream.
-    if(myinfo.remote && (!myinfo.playback || !myinfo.playback->playing)){
-#ifdef DEBUG
-        g_message("get_song_info: remote and not playing.");
 #endif
         return;
     }
@@ -486,7 +480,6 @@
 {
     TitleInput *tuple = NULL;
     gchar *string = NULL;
-    VFSFile *file;
 
     struct id3_file *id3file = NULL;
     struct id3_tag *tag = NULL;
@@ -527,7 +520,7 @@
             tuple->length = -1;
             tuple->mtime = 0; // this indicates streaming
 #ifdef DEBUG
-            g_message("get_song_tuple remote: tuple");
+            g_message("get_song_tuple: remote: tuple");
 #endif
             return tuple;
         }
@@ -537,147 +530,95 @@
         return NULL;
     }
 
-    if ((file = vfs_fopen(filename, "rb")) != NULL) {
-
-        tuple = bmp_title_input_new();
-        id3file = id3_file_open(filename, ID3_FILE_MODE_READONLY);
-
-        if (id3file) {
-            tag = id3_file_tag(id3file);
-
-            if (tag) {
-                tuple->performer =
-                    input_id3_get_string(tag, ID3_FRAME_ARTIST);
-                tuple->album_name =
-                    input_id3_get_string(tag, ID3_FRAME_ALBUM);
-                tuple->track_name =
-                    input_id3_get_string(tag, ID3_FRAME_TITLE);
+    tuple = bmp_title_input_new();
 
-                // year
-                string = NULL;
-                string = input_id3_get_string(tag, ID3_FRAME_YEAR); //TDRC
-                if (!string)
-                    string = input_id3_get_string(tag, "TYER");
-
-                if (string) {
-                    tuple->year = atoi(string);
-                    g_free(string);
-                    string = NULL;
-                }
-
-                tuple->file_name = g_path_get_basename(filename);
-                tuple->file_path = g_path_get_dirname(filename);
-                tuple->file_ext = extname(filename);
+    id3file = id3_file_open(filename, ID3_FILE_MODE_READONLY);
+    if (id3file) {
 
-                // length
-                tuple->length = -1;
-                string = input_id3_get_string(tag, "TLEN");
-                if (string) {
-                    tuple->length = atoi(string);
-#ifdef DEBUG
-                    g_message("get_song_tuple: TLEN = %d", tuple->length);
-#endif	
-                    g_free(string);
-                    string = NULL;
-                }
-                else {
-                    char *dummy = NULL;
-                    int length = 0;
-                    audmad_get_song_info(filename, &dummy, &length);
-                    tuple->length = length;
-                    g_free(dummy);
-                }
+        tag = id3_file_tag(id3file);
+        if (tag) {
+            tuple->performer =
+                input_id3_get_string(tag, ID3_FRAME_ARTIST);
+            tuple->album_name =
+                input_id3_get_string(tag, ID3_FRAME_ALBUM);
+            tuple->track_name =
+                input_id3_get_string(tag, ID3_FRAME_TITLE);
 
-                // track number
-                string = input_id3_get_string(tag, ID3_FRAME_TRACK);
-                if (string) {
-                    tuple->track_number = atoi(string);
-                    g_free(string);
-                    string = NULL;
-                }
-                // genre
-                tuple->genre = input_id3_get_string(tag, ID3_FRAME_GENRE);
-#ifdef DEBUG
-                g_message("genre = %s", tuple->genre);
-#endif
-                // comment
-                tuple->comment =
-                    input_id3_get_string(tag, ID3_FRAME_COMMENT);
+            // year
+            string = NULL;
+            string = input_id3_get_string(tag, ID3_FRAME_YEAR); //TDRC
+            if (!string)
+                string = input_id3_get_string(tag, "TYER");
 
+            if (string) {
+                tuple->year = atoi(string);
+                g_free(string);
+                string = NULL;
             }
-            id3_file_close(id3file);
-        }
-        else { // no id3tag
+
             tuple->file_name = g_path_get_basename(filename);
             tuple->file_path = g_path_get_dirname(filename);
             tuple->file_ext = extname(filename);
+
             // length
-            {
+            tuple->length = -1;
+            string = input_id3_get_string(tag, "TLEN");
+            if (string) {
+                tuple->length = atoi(string);
+#ifdef DEBUG
+                g_message("get_song_tuple: TLEN = %d", tuple->length);
+#endif	
+                g_free(string);
+                string = NULL;
+            }
+            else {
                 char *dummy = NULL;
                 int length = 0;
-                if(tuple->length == -1) {
-                    audmad_get_song_info(filename, &dummy, &length);
-                    tuple->length = length;
-                }
+                audmad_get_song_info(filename, &dummy, &length);
+                tuple->length = length;
                 g_free(dummy);
             }
+
+            // track number
+            string = input_id3_get_string(tag, ID3_FRAME_TRACK);
+            if (string) {
+                tuple->track_number = atoi(string);
+                g_free(string);
+                string = NULL;
+            }
+            // genre
+            tuple->genre = input_id3_get_string(tag, ID3_FRAME_GENRE);
+#ifdef DEBUG
+            g_message("genre = %s", tuple->genre);
+#endif
+            // comment
+            tuple->comment =
+                input_id3_get_string(tag, ID3_FRAME_COMMENT);
+
         }
-        vfs_fclose(file);
+        id3_file_close(id3file);
+    }
+    else { // no id3tag
+        tuple->file_name = g_path_get_basename(filename);
+        tuple->file_path = g_path_get_dirname(filename);
+        tuple->file_ext = extname(filename);
+        // length
+        {
+            char *dummy = NULL;
+            int length = 0;
+            if(tuple->length == -1) {
+                audmad_get_song_info(filename, &dummy, &length);
+                tuple->length = length;
+            }
+            g_free(dummy);
+        }
     }
 #ifdef DEBUG
     g_message("e: mad: audmad_get_song_tuple");
 #endif
     return tuple;
-
 }
 
-// probably this function is nolonger needed.
-#if 0
-/**
- * Retrieve meta-information about URL.
- * For local files this means ID3 tag etc.
- */
-gboolean mad_get_info(struct mad_info_t * info, gboolean fast_scan)
-{
-    TitleInput *tuple = NULL;
-
-#ifdef DEBUG
-    g_message("f: mad_get_info: %s", info->filename);
-#endif
-    if (info->remote) {
-        return TRUE;
-    }
-
-    tuple = audmad_get_song_tuple(info->filename);
-    
-    info->title = xmms_get_titlestring(audmad_config.title_override == TRUE ?
-	audmad_config.id3_format : xmms_get_gentitle_format(), tuple);
-
-    /* scan mp3 file, decoding headers unless fast_scan is set */
-    if (scan_file(info, fast_scan) == FALSE)
-        return FALSE;
-
-    /* reset the input file to the start */
-    vfs_rewind(info->infile);
-    info->offset = 0;
-
-    /* use the filename for the title as a last resort */
-    if (!info->title)
-    {
-        char *pos = strrchr(info->filename, '/');
-        if (pos)
-            info->title = g_strdup(pos + 1);
-        else
-            info->title = g_strdup(info->filename);
-     }
-
-#ifdef DEBUG
-    g_message("e: mad_get_info");
-#endif
-    return TRUE;
-}
-#endif
-
 
 static gchar *fmts[] = { "mp3", "mp2", "mpg", NULL };
 
--- a/src/madplug/plugin.h	Fri Mar 02 20:17:50 2007 -0800
+++ b/src/madplug/plugin.h	Fri Mar 02 20:28:55 2007 -0800
@@ -123,19 +123,21 @@
     gboolean show_avg_vbr_bitrate;
 };
 
+// global variables
+InputPlugin *mad_plugin;
+struct audmad_config_t audmad_config;
+
 // gcond
 extern GMutex *mad_mutex;
 extern GMutex *pb_mutex;
 extern GCond *mad_cond;
 
+// prototypes
 void audmad_config_compute(struct audmad_config_t *config);
-// compute scale values from "_db" strings
 gboolean audmad_is_remote(gchar *url);
-
-extern gpointer decode_loop(gpointer arg);
-extern void audmad_error(gchar * fmt, ...);
-extern void audmad_configure();
-extern InputPlugin *mad_plugin;
-extern struct audmad_config_t audmad_config;
+void input_process_remote_metadata(struct mad_info_t *info);
+gpointer decode_loop(gpointer arg);
+void audmad_error(gchar * fmt, ...);
+void audmad_configure(void);
 
 #endif                          /* !AUD_MAD_H */
--- a/src/madplug/replaygain.c	Fri Mar 02 20:17:50 2007 -0800
+++ b/src/madplug/replaygain.c	Fri Mar 02 20:28:55 2007 -0800
@@ -194,11 +194,15 @@
     return last_match + 1 - 8 + sizeof(struct APETagFooterStruct) - N;
 }
 
-void input_read_replaygain(struct mad_info_t *file_info)
+void read_replaygain(struct mad_info_t *file_info)
 {
     VFSFile *fp;
     glong curpos = 0;
 
+#ifdef DEBUG
+    g_message("f: read_replaygain");
+#endif
+
     file_info->has_replaygain = FALSE;
     file_info->replaygain_album_scale = -1;
     file_info->replaygain_track_scale = -1;
@@ -206,12 +210,18 @@
     file_info->mp3gain_minmax = -77;
 
     if (file_info->infile) {
+#ifdef DEBUG
+        g_message("replaygain: dup");
+#endif
         fp = vfs_dup(file_info->infile);
         curpos = vfs_ftell(fp);
     } else {
         if ((fp = vfs_fopen(file_info->filename, "rb")) == NULL)
             return;
         if (vfs_fseek(fp, 0L, SEEK_END) != 0) {
+#ifdef DEBUG
+            g_message("replaygain: seek error");
+#endif
             vfs_fclose(fp);
             return;
         }
@@ -241,12 +251,16 @@
                      offs, res);
             }
         }
+#ifdef DEBUG
+        else 
+            g_message("replaygain: not found");
+#endif
     }
 #ifdef DEBUG
     if (res == 0) {             // got APE tags, show the result
-        printf("RG album scale= %g, RG track scale = %g, in %s \n",
-               file_info->replaygain_album_scale,
-               file_info->replaygain_track_scale, file_info->filename);
+        g_message("RG album scale= %g, RG track scale = %g, in %s",
+		  file_info->replaygain_album_scale,
+		  file_info->replaygain_track_scale, file_info->filename);
     }
 #endif
 
@@ -257,5 +271,9 @@
     if (file_info->infile)
         vfs_fseek(fp, curpos, SEEK_SET);
 
-    vfs_fclose(fp);        
+    vfs_fclose(fp);
+
+#ifdef DEBUG
+    g_message("e: read_replaygain");
+#endif
 }
--- a/src/madplug/replaygain.h	Fri Mar 02 20:17:50 2007 -0800
+++ b/src/madplug/replaygain.h	Fri Mar 02 20:28:55 2007 -0800
@@ -39,6 +39,6 @@
 };
 
 /* prototypes */
-void input_read_replaygain(struct mad_info_t *file_info);
+void read_replaygain(struct mad_info_t *file_info);
 
 #endif