Skip to content

Commit

Permalink
Add support for live streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
e2iplayer committed Jan 7, 2017
1 parent 6e9d004 commit 0586430
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 62 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes
CFLAGS+=-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual
CFLAGS+=-Wsign-compare -Iincludes -g
CFLAGS+=-DPREFIX='"$(PREFIX)"'
LDFLAGS+=-lcurl -lavformat -lavutil -lavcodec -lswresample
LDFLAGS+=-lpthread -lcurl -lavformat -lavutil -lavcodec -lswresample

OSNAME=$(shell uname -s | sed -e 's/[-_].*//g' | tr A-Z a-z)
ifeq ("$(OSNAME)", "darwin")
Expand Down
85 changes: 73 additions & 12 deletions src/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <assert.h>
#include "msg.h"
#include "curl.h"
#include "hls.h"
Expand Down Expand Up @@ -31,45 +32,105 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
return realsize;
}

size_t get_data_from_url(char *url, char **str, uint8_t **bin, int type)

void * init_http_session(void)
{
CURL *c;
c = curl_easy_init();
return c;
}

long get_data_from_url_with_session(void **session, char **url, char **out, size_t *size, int type, bool update_url)
{
assert(session && *session);
assert(url && *url);
assert(size);

CURL *c = (CURL *)(*session);
CURLcode res;
long http_code = 0;
char *e_url = NULL;

url[strcspn(url, "\r")] = '\0';
(*url)[strcspn(*url, "\r")] = '\0';

struct MemoryStruct chunk;

chunk.memory = malloc(1);
chunk.size = 0;

c = curl_easy_init();
curl_easy_setopt(c, CURLOPT_URL, url);
curl_easy_setopt(c, CURLOPT_URL, *url);
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(c, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(c, CURLOPT_USERAGENT, USER_AGENT);

res = curl_easy_perform(c);

curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &http_code);
if (update_url && CURLE_OK == curl_easy_getinfo(c, CURLINFO_EFFECTIVE_URL, &e_url))
{
if (0 != strcmp(*url, e_url))
{
free(*url);
*url = strdup(e_url);
}
}

if (res != CURLE_OK) {
MSG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
if (type == STRING) {
*str = strdup(chunk.memory);
*out = strdup(chunk.memory);
} else if (type == BINKEY) {
*bin = malloc(KEYLEN);
*bin = memcpy(*bin, chunk.memory, KEYLEN);
*out = malloc(KEYLEN);
*out = memcpy(*out, chunk.memory, KEYLEN);
} else if (type == BINARY) {
*bin = malloc(chunk.size);
*bin = memcpy(*bin, chunk.memory, chunk.size);
*out = malloc(chunk.size);
*out = memcpy(*out, chunk.memory, chunk.size);
}
}

curl_easy_cleanup(c);
*size = chunk.size;

if (chunk.memory) {
free(chunk.memory);
}

return chunk.size;
return http_code;
}

void clean_http_session(void *session)
{
curl_easy_cleanup((CURL *)session);
}

long get_data_from_url_ext(char **url, char **out, size_t *size, int type, bool update_url)
{
CURL *c = (CURL *)init_http_session();
long http_code = get_data_from_url_with_session(&c, url, out, size, type, update_url);
clean_http_session(c);
return http_code;
}

size_t get_data_from_url(char **url, char **str, uint8_t **bin, int type, bool update_url)
{
CURL *c = (CURL *)init_http_session();
size_t size;
char *out = NULL;
get_data_from_url_with_session(&c, url, &out, &size, type, update_url);

switch (type){
case STRING:
*str = out;
break;
case BINARY:
case BINKEY:
*bin = (uint8_t *)out;
break;
default:
break;
}

clean_http_session(c);

return size;
}
9 changes: 8 additions & 1 deletion src/curl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __HLS_DownLoad__curl__
#define __HLS_DownLoad__curl__

#include <stdbool.h>

#define STRING 0x0001
#define BINKEY 0x0002
#define BINARY 0x0003
Expand All @@ -10,6 +12,11 @@
"Mobile/10A5355d Safari/8536.25"


size_t get_data_from_url(char *url, char **str, uint8_t **bin, int type);
size_t get_data_from_url(char **url, char **str, uint8_t **bin, int type, bool update_url);

void * init_http_session(void);
long get_data_from_url_with_session(void **session, char **url, char **out, size_t *size, int type, bool update_url);
long get_data_from_url_ext(char **url, char **out, size_t *size, int type, bool update_url);
void clean_http_session(void *session);

#endif /* defined(__HLS_DownLoad__curl__) */
Loading

0 comments on commit 0586430

Please sign in to comment.