diff src/http.c @ 126:5dcaf3785ebe

fix process terminate problem. add dynamic Content-Length header.
author Naoya OYAMA <naoya.oyama@gmail.com>
date Sun, 03 Oct 2010 21:55:37 +0900
parents e413158cae13
children 5a380559a61e
line wrap: on
line diff
--- a/src/http.c	Sun Oct 03 11:35:19 2010 +0900
+++ b/src/http.c	Sun Oct 03 21:55:37 2010 +0900
@@ -78,6 +78,8 @@
     } detail;
 };
 
+static off_t get_streaming_length (void);
+
     static inline void
 set_info_file (struct File_Info *info, const size_t length,
         const char *content_type)
@@ -118,8 +120,7 @@
     if (!strcmp (filename, STREAM_LOCATION)) {
         log_verbose ("http_get_info, stream location found.\n");
         info->is_readable = 1;
-        info->file_length = 100*1024*1024;
-        info->file_length = info->file_length - (info->file_length % LENGTH_PACKET);
+        info->file_length = get_streaming_length();
         info->last_modified = time(NULL);
         info->is_directory = 0;
         info->content_type = ixmlCloneDOMString ("video/mpeg");
@@ -190,8 +191,7 @@
 #endif
 
     info->is_readable = 1;
-    info->file_length = 100*1024*1024;
-    info->file_length = info->file_length - (info->file_length % LENGTH_PACKET);
+    info->file_length = get_streaming_length();
     info->last_modified = time(NULL);
     info->is_directory = 0;
 
@@ -316,8 +316,7 @@
     file->fullpath = strdup (fullpath);
     file->pos = 0;
     file->type = FILE_STREAM;
-    file->detail.stream.len = 100*1024*1024; //$B%U%!%$%k%5%$%:(B($B;DO?2h;~4V(Bx$B%S%C%H%l!<%H(B)
-    file->detail.stream.len = file->detail.stream.len - (file->detail.stream.len % LENGTH_PACKET);
+    file->detail.stream.len = get_streaming_length(); //$B%U%!%$%k%5%$%:(B($B;DO?2h;~4V(Bx$B%S%C%H%l!<%H(B)
     file->detail.stream.qbuf = stream_dequeue(file->detail.stream.p_queue);
     if ( file->detail.stream.qbuf == NULL ) {
         log_error ("get_file_stream(): stream_dequeue error.\n");
@@ -424,7 +423,7 @@
             }
             if ( file->detail.stream.qbuf == NULL ) {
                 log_verbose ("http_read stream_dequeue error NULL\n");
-                return -1;
+                return 0;
             }
             len = (size_t) MIN (buflen, file->detail.stream.qbuf->size - file->pos);
             memcpy (buf, file->detail.stream.qbuf->data + file->pos, (size_t) len);
@@ -600,3 +599,22 @@
     http_seek,
     http_close
 };
+
+// TS_BITRATE$B$O$d$C$D$1;E;v2a$.$k5$$,$7$^$9(B
+#define TS_BITRATE (10*1000*1000/8)
+static off_t
+get_streaming_length (void)
+{
+    off_t length = 0;
+    extern thread_data *gp_tdata;
+    thread_data *tdata = gp_tdata;
+    time_t cur_time;
+
+    time(&cur_time);
+    length = ((tdata->start_time+tdata->recsec) -cur_time) * (off_t)TS_BITRATE;
+    if ( length < 0 ) {
+        length = 0;
+    }
+    length = length - (length % LENGTH_PACKET);
+    return length;
+}