First attempt at stream capture (only while a client is connected for svn/trunk
authorvolpol <volpol@packet-gain.de>
Thu, 20 Mar 2014 17:53:35 +0000 (17:53 +0000)
committervolpol <volpol@packet-gain.de>
Thu, 20 Mar 2014 17:53:35 +0000 (17:53 +0000)
now)

curly.c
curly.h
itemq.c

diff --git a/curly.c b/curly.c
index 80789ae01e6702bf37991fa10962ceb3dc1568f7..a3539f73645404f131d218edd4720d42583b3241 100644 (file)
--- a/curly.c
+++ b/curly.c
 static int sock;
 static int conn;
 static int flag_conn;
-
 #endif
 
+static FILE *capf = NULL;
+
 static unsigned bufsize;
 
 #define BSIZE 4096
 
+int curly_stop_capture(void){
+    FILE *tmp = capf;
+    capf = NULL;
+    puts("Finishing capture");
+    if (tmp) fclose(tmp);
+    return 0;
+
+}
+
+int curly_start_capture(void){
+
+    time_t t;
+    char fname[256];
+
+    if (capf) curly_stop_capture();
+
+    t = time(NULL);
+    strftime(fname, sizeof fname, "QVC_%Y%m%d_%H%M.ts", localtime(&t));
+
+    capf = fopen(fname, "w");
+
+    if (capf)
+        printf ("Starting capture to %s\n", fname);
+    else
+        puts ("Tough luck!");
+
+    return (NULL != capf) ? 0 : -1;
+
+}
+
 
 #ifdef USE_TCP
 
@@ -52,6 +83,8 @@ static void simple_http(int sock) {
     DPRINT ("simple http response sent\n");
 }
 
+//TODO capture even in absence of the client?
+
 size_t fetch_send(void *ptr, size_t size, size_t nmemb, void *stream) {
     size_t bytesize = size * nmemb;
 
@@ -70,6 +103,7 @@ size_t fetch_send(void *ptr, size_t size, size_t nmemb, void *stream) {
 
     if (flag_conn) {
         sent = send(conn, ptr, bytesize, MSG_NOSIGNAL);
+        if (capf) fwrite(ptr, size, nmemb, capf);
         if (sent <= 0) {
             DPRINT("Pious Teardown!\n");
             shutdown(conn, SHUT_RDWR);
@@ -77,6 +111,7 @@ size_t fetch_send(void *ptr, size_t size, size_t nmemb, void *stream) {
             conn = -1;
             flag_conn = 0;
         }
+        usleep(1500);
     }
 
     (void)stream;
diff --git a/curly.h b/curly.h
index 568a9d645ac2a405f71f0eed01099839308d0768..c000a48d42b370da658c03bccd5f9f1e62da5a86 100644 (file)
--- a/curly.h
+++ b/curly.h
@@ -17,4 +17,7 @@ void curly_stream_cleanup(void);
 
 int curly_is_connected(void);
 
+int curly_stop_capture(void);
+int curly_start_capture(void);
+
 #endif /* CURLY_H_ */
diff --git a/itemq.c b/itemq.c
index 4754b3949df7f12f6cc234ba9a049fd922341740..1f2f0cfefa1ebe79aa9eeafa87d3d9da46204a49 100644 (file)
--- a/itemq.c
+++ b/itemq.c
 #define QVC_800_M3U8 "http://live1.qvc.jp/iPhone/800.m3u8"
 
 struct item;
-static volatile sig_atomic_t in_game;
+static volatile sig_atomic_t in_game, capt_req;
 static struct item *head, *tail, *curr;
 static int live;
+static int capturing;
 
 
 static void handle_signal(int sig){
     printf("Caught signal %d\n",sig);
     switch (sig){
+    case SIGUSR1:
+        capt_req = 1;
+        break;
     case SIGTERM:
     case SIGINT:
         if (!in_game) { puts("Hurry up!\n"); exit(-1);  }
@@ -326,6 +330,19 @@ static void reset_base(char *base, char *url){
     *(strrchr(base, '/')+1) = 0;
 }
 
+static void cap_isr(void){
+    if (capt_req){
+        if (capturing) {
+            curly_stop_capture();
+            capturing = 0;
+        } else {
+            capturing = !curly_start_capture();
+        }
+        capt_req = 0;
+        //return IRQF_HANDLED ;)
+    }
+}
+
 int main(int argc, char *argv[]) {
 
     time_t last, now;
@@ -351,9 +368,11 @@ int main(int argc, char *argv[]) {
     last = now = 0;
     head = tail = curr = NULL;
     live = in_game = 1;
+    capturing = capt_req = 0;
 
     signal(SIGTERM, handle_signal);
     signal(SIGINT,  handle_signal);
+    signal(SIGUSR1, handle_signal);
 
     while (in_game) {
         now = time(NULL);
@@ -399,6 +418,7 @@ int main(int argc, char *argv[]) {
             }
         }
 
+        cap_isr();
         emulate_stream();