Store intermediate playlist in a tempfile no longer! Put it into the new and (the...
authorvolpol <volpol@packet-gain.de>
Sat, 7 Sep 2013 10:01:59 +0000 (10:01 +0000)
committervolpol <volpol@packet-gain.de>
Sat, 7 Sep 2013 10:01:59 +0000 (10:01 +0000)
Some cleanup? or maybe not.
memfile could use some more love in the future :)

CMakeLists.txt
curly.c
curly.h
distclean.sh [deleted file]
itemq.c

index df3eb277649c7786fa4ac88a467eb063701a3fac..71683fba37aba6afddc4e5efd52e29b87b393969 100644 (file)
@@ -1,9 +1,17 @@
 cmake_minimum_required(VERSION 2.6)
 project(HLS)
+set( CMAKE_C_FLAGS "-W -Wall -Wextra" )
 include_directories(${HLS_SOURCE_DIR})
 add_definitions(-DUSE_TCP)
-add_executable(hls curly.c itemq.c m3u8.c)
+#add_definitions(-DDEBUG)
+add_executable(hls curly.c itemq.c m3u8.c memfile.c)
 include(FindPkgConfig)
 pkg_check_modules(CURL REQUIRED libcurl)
 include_directories(${CURL_INCLUDE_DIR})
 target_link_libraries(hls ${CURL_LIBRARIES} )
+
+# add distclean make target
+add_custom_target( distclean
+    make clean
+    COMMAND rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake Makefile
+)
diff --git a/curly.c b/curly.c
index 81020107d15f3096d07c31ca3494a442bbfbca20..4f4d0672cf5b5439f71d0cefb2db387c758067c6 100644 (file)
--- a/curly.c
+++ b/curly.c
@@ -7,6 +7,7 @@
 #include <time.h>
 #include "log.h"
 
+#include "memfile.h"
 #ifdef USE_TCP
 
 #include <sys/types.h>
@@ -21,7 +22,6 @@ static int flag_conn;
 #endif
 
 static unsigned bufsize;
-static unsigned delay;
 
 #define BSIZE 4096
 
@@ -76,14 +76,7 @@ size_t fetch_send(void *ptr, size_t size, size_t nmemb, void *stream) {
                }
        }
 
-       if (!flag_conn)
-               usleep(delay);
-       //either limit speed further or burst at max speed filling clien't buffer
-       //send will block when it's full thus slowing download down
-/*
-       else
-               if (sent==bufsize) usleep(delay/2); //burst to fill client's buffer
-*/
+    (void)stream;
        return bytesize;
 
 }
@@ -207,7 +200,7 @@ int curly_stream(const char *from_url, unsigned long duration) {
        //but try to take just as long
        time_t st = time(NULL);
 
-       while (conn < 0 && time(NULL)-st<duration) {
+    while (conn < 0 && (unsigned long)(time(NULL)-st) < duration) {
                conn = accept(sock, NULL, NULL);
                usleep(1);
        }
@@ -264,16 +257,38 @@ OUT:
        return err;
 }
 
+
+#ifdef USE_STREAM
+
 int curly_refresh_m3u8(const char *from_url, const char *to_file) {
+
+#else
+
+size_t mwrite_wrapper(void *ptr, size_t size, size_t nmemb, void *stream){
+    WHOAMI;
+    return (mwrite(ptr, size, nmemb, (MFILE*)stream) * size);
+}
+
+int curly_refresh_m3u8(const char *from_url, MFILE *f) {
+
+#endif
+
        CURL *ch = NULL;
        CURLcode cc;
+
+#ifdef USE_STREAM
        FILE *f;
+#endif
+
        int err = -1;
        WHOAMI;
+
+#ifdef USE_STREAM
        f = fopen(to_file, "w");
 
        if (!f)
                goto OUT;
+#endif
 
        /*
         if (sch)
@@ -289,21 +304,44 @@ int curly_refresh_m3u8(const char *from_url, const char *to_file) {
        cc = curl_easy_setopt(ch, CURLOPT_URL, from_url);
        if (CURLE_OK != cc)
                goto OUT;
-       cc = curl_easy_setopt(ch, CURLOPT_WRITEDATA, f);
-       if (CURLE_OK != cc)
-               goto OUT;
-       cc = curl_easy_perform(ch);
+
+
+    WHOAMI;
+    cc = curl_easy_setopt(ch, CURLOPT_WRITEDATA, f);
+    if (CURLE_OK != cc)
+        goto OUT;
+
+
+#ifndef USE_STREAM
+    WHOAMI;
+    cc = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, mwrite_wrapper);
+    if (CURLE_OK != cc)
+        goto OUT;
+#endif
+
+#ifdef DEBUG
+    curl_easy_setopt(sch, CURLOPT_VERBOSE, 7L);
+#endif
+
+
+    cc = curl_easy_perform(ch);
        if (CURLE_OK != cc)
                goto OUT;
 
        err = 0;
-       OUT: if (f)
-               fclose(f);
 
+
+OUT:
+
+#ifdef USE_STREAM
+    if (f)
+               fclose(f);
+#endif
        //if (ch && ch!=sch)    curl_easy_cleanup(ch);
 
        if (ch)
                curl_easy_cleanup(ch);
+
        return err;
 }
 
diff --git a/curly.h b/curly.h
index 4dc3920c6163e5c10f309e5902510d308fd27af1..de3a7ac153ade837ac86c859d619ba1beec4fa34 100644 (file)
--- a/curly.h
+++ b/curly.h
@@ -7,10 +7,14 @@
 
 #ifndef CURLY_H_
 #define CURLY_H_
-
+#include "memfile.h"
 int curly_init(void);
 void curly_cleanup(void);
+#ifdef USE_STREAM
 int curly_refresh_m3u8(const char *from_url, const char *to_file);
+#else
+int curly_refresh_m3u8(const char *from_url, MFILE *f);
+#endif
 int curly_stream(const char *from_url, unsigned long duration);
 int curly_stream_init(const char *ip, unsigned short port);
 void curly_stream_cleanup(void);
diff --git a/distclean.sh b/distclean.sh
deleted file mode 100755 (executable)
index ce72c19..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-
-CLEAN="CMakeFiles CMakeCache.txt cmake_install.cmake Makefile"
-
-rm -rf $CLEAN
-
-
diff --git a/itemq.c b/itemq.c
index 809e802f0e0f104593b4a0c1c34925f0da69640c..35ddd82516f47ff203e5dc7578a1bcf341fd37e1 100644 (file)
--- a/itemq.c
+++ b/itemq.c
@@ -8,6 +8,7 @@
 #include "curly.h"
 #include "m3u8.h"
 #include "log.h"
+#include "memfile.h"
 
 #define PLS "/tmp/iq.m3u8"
 #define QVC_MASTER_M3U8 "http://live1.qvc.jp/iPhone/QVC_PC.m3u8"
@@ -66,7 +67,7 @@ static void print_list(void) {
 
 }
 
-struct item *find_hash(int hash) {
+struct item *find_hash(unsigned int hash) {
 
        struct item *n = head;
 
@@ -170,10 +171,15 @@ void emulate_stream(void) {
        curr->done = 1;
 
 }
-
+#ifdef USE_STREAM
 int reload_m3u8(const char *from_file, const char *urlbase) {
+#else
+int reload_m3u8(MFILE *f, const char *urlbase) {
+#endif
 
+#ifdef USE_STREAM
        FILE *f;
+#endif
        char buf[1024];
        struct item *n;
        struct item *p;
@@ -193,12 +199,16 @@ int reload_m3u8(const char *from_file, const char *urlbase) {
        plist = pid = bw = 0;
 
        DPRINT("Reloading M3U8\n");
-       f = fopen(PLS, "r");
+#ifdef USE_STREAM
+    f = fopen(from_file, "r");
+#endif
        if (!f)
                return -1;
-
+#ifdef USE_STREAM
        while (fgets(buf, sizeof buf, f)) {
-
+#else
+        while (mgets(buf, sizeof buf, f)) {
+#endif
                if (buf[strlen(buf) - 1] == '\n')
                        buf[strlen(buf) - 1] = 0;
                if (buf[strlen(buf) - 1] == '\r')
@@ -269,7 +279,9 @@ int reload_m3u8(const char *from_file, const char *urlbase) {
                        }
                }
        }
+#ifdef USE_STREAM
        fclose(f);
+#endif
        return 0;
 }
 
@@ -302,7 +314,6 @@ static void reset_base(char *base, char *url){
        //TODO risky dice
        *(strrchr(base, '/')+1) = 0;
 }
-
 int main(int argc, char *argv[]) {
 
        time_t last, now;
@@ -312,8 +323,6 @@ int main(int argc, char *argv[]) {
        char url[1024];
        char base[1024];
 
-       //url = QVC_MASTER_M3U8;
-
        if (argc>1)
                strcpy(url, argv[1]);
        else
@@ -324,7 +333,7 @@ int main(int argc, char *argv[]) {
        reset_base(base, url);
 
        curly_init();
-       curly_stream_init("0.0.0.0",1234);
+    curly_stream_init("0.0.0.0",1234);
 
 
        last = now = 0;
@@ -337,11 +346,22 @@ int main(int argc, char *argv[]) {
                now = time(NULL);
                if (live && now-last>=refresh_period) {
                        DPRINT("Refreshing M3U8 after %d real seconds\n",(int)(last?now-last:0));
+#ifdef USE_STREAM
                        refreshed = (0 == curly_refresh_m3u8(url, PLS));
+#else
+            MFILE *f = mopen(NULL, 0, "w+");
+
+            refreshed = (0 == curly_refresh_m3u8(url, f));
+#endif
                        if (refreshed) {
                                last = now = time(NULL);
                                purge_prepare();
+#ifdef USE_STREAM
                                if (0 == reload_m3u8(PLS, base)) {
+#else
+                if (0 == reload_m3u8(f, base)) {
+                    mclose(f);
+#endif
                                        refreshed = 0;
                                        purge_run();
                                        print_list();