project(HLS)
set( CMAKE_C_FLAGS "-W -Wall -Wextra" )
include_directories(${HLS_SOURCE_DIR})
-add_definitions(-DUSE_TCP)
-#add_definitions(-DDEBUG)
-add_definitions(-DUSE_TC7200_WORKAROUND)
add_executable(hls curly.c itemq.c m3u8.c memfile.c)
include(FindPkgConfig)
pkg_check_modules(CURL REQUIRED libcurl)
--- /dev/null
+#define USE_TCP
+//#define USE_MEMFILE
+#define DEBUG
+//#define USE_TC7200_WORKAROUND
#include <unistd.h>
#include <time.h>
-#include "log.h"
+#include "config.h"
+
+#include "log.h"
#include "memfile.h"
+
#ifdef USE_TCP
#include <sys/types.h>
}
-#ifdef USE_STREAM
-
-int curly_refresh_m3u8(const char *from_url, const char *to_file) {
-
-#else
-
+#ifdef USE_MEMFILE
size_t mwrite_wrapper(void *ptr, size_t size, size_t nmemb, void *stream){
WHOAMI;
return (mwrite(ptr, size, nmemb, (MFILE*)stream) * size);
}
+#endif
-int curly_refresh_m3u8(const char *from_url, MFILE *f) {
+int curly_refresh_m3u8(const char *from_url, void *src) {
-#endif
CURL *ch = NULL;
CURLcode cc;
-#ifdef USE_STREAM
+#ifdef USE_MEMFILE
+ MFILE *f;
+#else
FILE *f;
#endif
int err = -1;
WHOAMI;
-#ifdef USE_STREAM
- f = fopen(to_file, "w");
-
+ f = src;
if (!f)
goto OUT;
-#endif
+
/*
if (sch)
goto OUT;
-#ifndef USE_STREAM
+#ifdef USE_MEMFILE
WHOAMI;
cc = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, mwrite_wrapper);
if (CURLE_OK != cc)
OUT:
-#ifdef USE_STREAM
- if (f)
- fclose(f);
-#endif
//if (ch && ch!=sch) curl_easy_cleanup(ch);
if (ch)
#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_refresh_m3u8(const char *from_url, void *src);
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);
#include <sys/types.h>
#include <time.h>
#include <signal.h>
+
+#include "config.h"
#include "curly.h"
#include "m3u8.h"
#include "log.h"
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
+int reload_m3u8(void *src, const char *urlbase) {
+
+#ifdef USE_MEMFILE
+ MFILE *f;
+#else
FILE *f;
#endif
+
char buf[1024];
struct item *n;
struct item *p;
WHOAMI;
-
+ f = src;
plist = pid = bw = 0;
DPRINT("Reloading M3U8\n");
-#ifdef USE_STREAM
- f = fopen(from_file, "r");
-#endif
if (!f)
return -1;
-#ifdef USE_STREAM
- while (fgets(buf, sizeof buf, f)) {
-#else
+
+#ifdef USE_MEMFILE
while (mgets(buf, sizeof buf, f)) {
+#else
+ while (fgets(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;
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
+#ifdef USE_MEMFILE
MFILE *f = mopen(NULL, 0, "w+");
-
refreshed = (0 == curly_refresh_m3u8(url, f));
+ //memfile actually doesn't reposition rpos in mwrite calls
+ //but it should at some point so let's stay future-fool
+ mrewind(f);
+#else
+ FILE *f = fopen(PLS, "w+");
+ refreshed = (0 == curly_refresh_m3u8(url, f));
+ rewind(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)) {
+
+#ifdef USE_MEMFILE
mclose(f);
+#else
+ fclose(f);
#endif
refreshed = 0;
purge_run(0);
#include <ctype.h>
+#include "config.h"
#include "m3u8.h"
#include "log.h"
#include <string.h>
#include <unistd.h>
+#include "config.h"
#include "memfile.h"
#include "log.h"
return tot;
}
+void mrewind(MFILE *m){
+ m->rpos = 0;
+}
+
void mdump(MFILE *m){
WHOAMI;
write(1, m->pm, m->length);
size_t mread(void *ptr, size_t size, size_t nmemb, MFILE *mf);
size_t mwrite(const void *ptr, size_t size, size_t nmemb, MFILE *mf);
+void mrewind(MFILE *m);
+
void mdump(MFILE *m);
+
#endif /* MEMFILE_H_ */