Some cleanup? or maybe not.
memfile could use some more love in the future :)
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
+)
#include <time.h>
#include "log.h"
+#include "memfile.h"
#ifdef USE_TCP
#include <sys/types.h>
#endif
static unsigned bufsize;
-static unsigned delay;
#define BSIZE 4096
}
}
- 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;
}
//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);
}
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)
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;
}
#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);
+++ /dev/null
-#!/bin/sh
-
-CLEAN="CMakeFiles CMakeCache.txt cmake_install.cmake Makefile"
-
-rm -rf $CLEAN
-
-
#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"
}
-struct item *find_hash(int hash) {
+struct item *find_hash(unsigned int hash) {
struct item *n = head;
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;
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')
}
}
}
+#ifdef USE_STREAM
fclose(f);
+#endif
return 0;
}
//TODO risky dice
*(strrchr(base, '/')+1) = 0;
}
-
int main(int argc, char *argv[]) {
time_t last, now;
char url[1024];
char base[1024];
- //url = QVC_MASTER_M3U8;
-
if (argc>1)
strcpy(url, argv[1]);
else
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;
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();