[xiph-commits] r9204 - icecast/branches/kh/icecast/src

karl at motherfish-iii.xiph.org karl at motherfish-iii.xiph.org
Mon May 2 07:05:22 PDT 2005


Author: karl
Date: 2005-05-02 07:05:15 -0700 (Mon, 02 May 2005)
New Revision: 9204

Modified:
   icecast/branches/kh/icecast/src/admin.c
   icecast/branches/kh/icecast/src/client.c
   icecast/branches/kh/icecast/src/client.h
   icecast/branches/kh/icecast/src/compat.h
   icecast/branches/kh/icecast/src/connection.c
   icecast/branches/kh/icecast/src/format.h
   icecast/branches/kh/icecast/src/format_mp3.c
   icecast/branches/kh/icecast/src/format_ogg.c
   icecast/branches/kh/icecast/src/source.c
   icecast/branches/kh/icecast/src/source.h
   icecast/branches/kh/icecast/src/stats.c
Log:
check in for various small bits. stats update (both the thread and added
per-mount bytes sent/received). Added client read function to remove some
code duplication.  The default metadata interval for new clients should be
the same as the input if there is one.


Modified: icecast/branches/kh/icecast/src/admin.c
===================================================================
--- icecast/branches/kh/icecast/src/admin.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/admin.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -1044,14 +1044,6 @@
 
         DEBUG2("Metadata on mountpoint %s changed to \"%s\"", 
                 source->mount, value);
-        stats_event(source->mount, "title", value);
-        /* At this point, we assume that the metadata passed in
-           is encoded in UTF-8 */
-        logging_playlist(source->mount, value, source->listeners);
-        /* If we get an update on the mountpoint, force a
-         * yp touch */
-        yp_touch (source->mount);
-
         thread_mutex_unlock (&source->lock);
         html_success(client, "Metadata update successful");
     }

Modified: icecast/branches/kh/icecast/src/client.c
===================================================================
--- icecast/branches/kh/icecast/src/client.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/client.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -121,6 +121,25 @@
     free(client);
 }
 
+
+/* helper function for reading data from a client */
+int client_read_bytes (client_t *client, void *buf, unsigned len)
+{
+    int bytes = sock_read_bytes (client->con->sock, buf, len);
+    if (bytes > 0)
+        return bytes;
+
+    if (bytes < 0)
+    {
+        if (sock_recoverable (sock_error()))
+            return -1;
+        WARN0 ("source connection has died");
+    }
+    client->con->error = 1;
+    return -1;
+}
+
+
 void client_send_302(client_t *client, char *location) {
     int bytes;
     bytes = sock_write(client->con->sock, "HTTP/1.0 302 Temporarily Moved\r\n"
@@ -149,7 +168,6 @@
     bytes = sock_write(client->con->sock, "HTTP/1.0 404 File Not Found\r\n"
             "Content-Type: text/html\r\n\r\n"
             "<b>%s</b>\r\n", message);
-    if(bytes > 0) client->con->sent_bytes = bytes;
     client->respcode = 404;
     client_destroy(client);
 }

Modified: icecast/branches/kh/icecast/src/client.h
===================================================================
--- icecast/branches/kh/icecast/src/client.h	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/client.h	2005-05-02 14:05:15 UTC (rev 9204)
@@ -86,6 +86,7 @@
 void client_send_400(client_t *client, char *message);
 void client_send_302(client_t *client, char *location);
 int client_send_bytes (client_t *client, const void *buf, unsigned len);
+int client_read_bytes (client_t *client, void *buf, unsigned len);
 void client_set_queue (client_t *client, refbuf_t *refbuf);
 void client_as_slave (client_t *client);
 

Modified: icecast/branches/kh/icecast/src/compat.h
===================================================================
--- icecast/branches/kh/icecast/src/compat.h	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/compat.h	2005-05-02 14:05:15 UTC (rev 9204)
@@ -35,8 +35,10 @@
 
 #ifdef _WIN32
 #define FORMAT_INT64      "%I64d"
+#define FORMAT_UINT64     "%I64u"
 #else
 #define FORMAT_INT64      "%lld"
+#define FORMAT_UINT64     "%llu"
 #endif
 
 #endif /* __COMPAT_H__ */

Modified: icecast/branches/kh/icecast/src/connection.c
===================================================================
--- icecast/branches/kh/icecast/src/connection.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/connection.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -736,7 +736,13 @@
         return;
     }
 
-    stats_event_inc(NULL, "stats");
+    client->respcode = 200;
+    if (sock_write (client->con->sock, "HTTP/1.0 200 OK\r\n\r\n") < 0)
+    {
+        client_destroy (client);
+        ERROR0 ("failed to write header");
+        return;
+    }
 
     thread_create("Stats Connection", stats_connection, (void *)client, THREAD_DETACHED);
 }

Modified: icecast/branches/kh/icecast/src/format.h
===================================================================
--- icecast/branches/kh/icecast/src/format.h	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/format.h	2005-05-02 14:05:15 UTC (rev 9204)
@@ -40,6 +40,8 @@
     char *mount;
 
     char *contenttype;
+    uint64_t read_bytes;
+    uint64_t sent_bytes;
 
     refbuf_t *(*get_buffer)(struct source_tag *);
     int (*write_buf_to_client)(struct source_tag *source, client_t *client);
@@ -47,7 +49,7 @@
     int (*create_client_data)(struct source_tag *source, client_t *client);
     void (*set_tag)(struct _format_plugin_tag *plugin, char *tag, char *value);
     void (*free_plugin)(struct _format_plugin_tag *self);
-    void (*apply_settings)(struct source_tag *source, struct _mount_proxy *mount);
+    void (*apply_settings)(client_t *client, struct _format_plugin_tag *format, struct _mount_proxy *mount);
 
     /* for internal state management */
     void *_state;

Modified: icecast/branches/kh/icecast/src/format_mp3.c
===================================================================
--- icecast/branches/kh/icecast/src/format_mp3.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/format_mp3.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -62,7 +62,7 @@
 static int format_mp3_write_buf_to_client(source_t *source, client_t *client);
 static void write_mp3_to_file (struct source_tag *source, refbuf_t *refbuf);
 static void mp3_set_tag (format_plugin_t *plugin, char *tag, char *value);
-static void format_mp3_apply_settings(struct source_tag *source, struct _mount_proxy *mount);
+static void format_mp3_apply_settings(client_t *client, format_plugin_t *format, mount_proxy *mount);
 
 
 typedef struct {
@@ -115,6 +115,7 @@
         {
             state->offset = 0;
             plugin->get_buffer = mp3_get_filter_meta;
+            state->interval = state->inline_metadata_interval;
         }
     }
     source->format = plugin;
@@ -193,12 +194,22 @@
 }
 
 
-static void format_mp3_apply_settings (source_t *source, mount_proxy *mount)
+static void format_mp3_apply_settings (client_t *client, format_plugin_t *format, mount_proxy *mount)
 {
-    mp3_state *source_mp3 = source->format->_state;
+    mp3_state *source_mp3 = format->_state;
 
-    source_mp3->interval = -1;
-    if (mount->mp3_meta_interval >= 0)
+    if (mount->mp3_meta_interval < 0)
+    {
+        char *metadata = httpp_getvar (client->parser, "icy-metaint");
+        source_mp3->interval = -1;
+        if (metadata)
+        {
+            int interval = atoi (metadata);
+            if (interval > 0)
+                source_mp3->interval = interval;
+        }
+    }
+    else
         source_mp3->interval = mount->mp3_meta_interval;
     DEBUG2 ("mp3 interval %d, %d", mount->mp3_meta_interval, source_mp3->interval);
 }
@@ -414,18 +425,18 @@
     int bytes;
     refbuf_t *refbuf;
     mp3_state *source_mp3 = source->format->_state;
+    format_plugin_t *format = source->format;
 
     if ((refbuf = refbuf_new (2048)) == NULL)
         return NULL;
-    bytes = sock_read_bytes (source->client->con->sock, refbuf->data, 2048);
 
-    if (bytes == 0)
+    bytes = client_read_bytes (source->client, refbuf->data, 2048);
+    if (bytes < 0)
     {
-        INFO1 ("End of stream %s", source->mount);
-        source->running = 0;
         refbuf_release (refbuf);
         return NULL;
     }
+    format->read_bytes += bytes;
     if (source_mp3->update_metadata)
     {
         mp3_set_title (source);
@@ -441,9 +452,6 @@
     }
     refbuf_release (refbuf);
 
-    if (!sock_recoverable (sock_error()))
-        source->running = 0;
-
     return NULL;
 }
 
@@ -457,6 +465,7 @@
     refbuf_t *refbuf;
     format_plugin_t *plugin = source->format;
     mp3_state *source_mp3 = plugin->_state;
+    format_plugin_t *format = source->format;
     unsigned char *src;
     unsigned int bytes, mp3_block;
     int ret;
@@ -464,29 +473,18 @@
     refbuf = refbuf_new (2048);
     src = refbuf->data;
 
-    ret = sock_read_bytes (source->client->con->sock, refbuf->data, 2048);
-
-    if (ret == 0)
+    ret = client_read_bytes (source->client, refbuf->data, 2048);
+    if (ret < 0) 
     {
-        INFO1 ("End of stream %s", source->mount);
-        source->running = 0;
         refbuf_release (refbuf);
         return NULL;
     }
+    format->read_bytes += ret;
     if (source_mp3->update_metadata)
     {
         mp3_set_title (source);
         source_mp3->update_metadata = 0;
     }
-    if (ret < 0)
-    {
-        refbuf_release (refbuf);
-        if (sock_recoverable (sock_error()))
-            return NULL; /* go back to waiting */
-        INFO0 ("Error on connection from source");
-        source->running = 0;
-        return NULL;
-    }
     /* fill the buffer with the read data */
     bytes = (unsigned int)ret;
     refbuf->len = 0;

Modified: icecast/branches/kh/icecast/src/format_ogg.c
===================================================================
--- icecast/branches/kh/icecast/src/format_ogg.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/format_ogg.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -372,6 +372,7 @@
 static refbuf_t *ogg_get_buffer (source_t *source)
 {
     ogg_state_t *ogg_info = source->format->_state;
+    format_plugin_t *format = source->format;
     char *data = NULL;
     int bytes;
 
@@ -418,23 +419,13 @@
         /* we need more data to continue getting pages */
         data = ogg_sync_buffer (&ogg_info->oy, 4096);
 
-        bytes = sock_read_bytes (source->client->con->sock, data, 4096);
+        bytes = client_read_bytes (source->client, data, 4096);
         if (bytes < 0)
         {
-            if (sock_recoverable (sock_error()))
-                return NULL;
-            WARN0 ("source connection has died");
             ogg_sync_wrote (&ogg_info->oy, 0);
-            source->running = 0;
             return NULL;
         }
-        if (bytes == 0)
-        {
-            INFO1 ("End of Stream %s", source->mount);
-            ogg_sync_wrote (&ogg_info->oy, 0);
-            source->running = 0;
-            return NULL;
-        }
+        format->read_bytes += bytes;
         ogg_sync_wrote (&ogg_info->oy, bytes);
     }
 }

Modified: icecast/branches/kh/icecast/src/source.c
===================================================================
--- icecast/branches/kh/icecast/src/source.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/source.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -259,6 +259,7 @@
     source->yp_public = 0;
     source->yp_prevent = 0;
     source->hidden = 0;
+    source->client_stats_update = 0;
     util_dict_free (source->audio_info);
     source->audio_info = NULL;
 
@@ -500,6 +501,14 @@
             source_update_settings (config, source);
             config_release_config ();
         }
+        if (current >= source->client_stats_update)
+        {
+            stats_event_args (source->mount, "total_bytes_read",
+                    FORMAT_UINT64, source->format->read_bytes);
+            stats_event_args (source->mount, "total_bytes_sent",
+                    FORMAT_UINT64, source->format->sent_bytes);
+            source->client_stats_update = current + 5;
+        }
         if (fds < 0)
         {
             if (! sock_recoverable (sock_error()))
@@ -527,6 +536,12 @@
         }
         source->last_read = current;
         refbuf = source->format->get_buffer (source);
+        if (source->client->con && source->client->con->error)
+        {
+            INFO1 ("End of Stream %s", source->mount);
+            source->running = 0;
+            continue;
+        }
         if (refbuf)
         {
             /* append buffer to the in-flight data queue,  */
@@ -611,6 +626,7 @@
 
         total_written += bytes;
     }
+    source->format->sent_bytes += total_written;
 
     /* the refbuf referenced at head (last in queue) may be marked for deletion
      * if so, check to see if this client is still referring to it */
@@ -1089,7 +1105,7 @@
         source->on_disconnect = strdup(mountinfo->on_disconnect);
     }
     if (source->format && source->format->apply_settings)
-        source->format->apply_settings (source, mountinfo);
+        source->format->apply_settings (source->client, source->format, mountinfo);
 }
 
 
@@ -1246,7 +1262,6 @@
             source_free_source (source);
             return NULL;
         }
-        source->client->con->sent_bytes += bytes;
     }
     stats_event_inc(NULL, "source_client_connections");
     stats_event (source->mount, "listeners", "0");

Modified: icecast/branches/kh/icecast/src/source.h
===================================================================
--- icecast/branches/kh/icecast/src/source.h	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/source.h	2005-05-02 14:05:15 UTC (rev 9204)
@@ -23,6 +23,7 @@
 typedef struct source_tag
 {
     client_t *client;
+    time_t client_stats_update;
     
     char *mount;
 

Modified: icecast/branches/kh/icecast/src/stats.c
===================================================================
--- icecast/branches/kh/icecast/src/stats.c	2005-05-02 07:05:30 UTC (rev 9203)
+++ icecast/branches/kh/icecast/src/stats.c	2005-05-02 14:05:15 UTC (rev 9204)
@@ -784,9 +784,11 @@
     stats_event_t *event;
 
     INFO0 ("stats client starting");
+
     /* increment the thread count */
     thread_mutex_lock(&_stats_mutex);
     _stats_threads++;
+    stats_event_args (NULL, "stats", "%d", _stats_threads);
     thread_mutex_unlock(&_stats_mutex);
 
     thread_mutex_create("stats local event", &local_event_mutex);
@@ -815,6 +817,7 @@
     thread_mutex_lock(&_stats_mutex);
     _unregister_listener (&local_event_queue);
     _stats_threads--;
+    stats_event_args (NULL, "stats", "%d", _stats_threads);
     thread_mutex_unlock(&_stats_mutex);
 
     thread_mutex_destroy(&local_event_mutex);



More information about the commits mailing list