X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=input.cpp;h=f9f879b6c2a393f6671b42fb25e4462854e04711;hp=d36919407077c60aff7f34eabb5a74dc57f15aab;hb=6d062eab70fd6528219545846e6c19c1cad35a3d;hpb=1d995a9b2f2d56fb66192b8404c9f5591af9ff75 diff --git a/input.cpp b/input.cpp index d369194..f9f879b 100644 --- a/input.cpp +++ b/input.cpp @@ -15,6 +15,7 @@ #include #include "metacube.h" +#include "mutexlock.h" #include "input.h" #include "server.h" @@ -22,13 +23,42 @@ using namespace std; extern Server *servers; -Input::Input(const string &stream_id) +Input::Input(const string &stream_id, const string &url) : stream_id(stream_id), + url(url), has_metacube_header(false) { } -void Input::run(const string &url) +void Input::run() +{ + should_stop = false; + + // Joinable is already the default, but it's good to be certain. + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + pthread_create(&worker_thread, &attr, Input::do_work_thunk, this); +} + +void Input::stop() +{ + should_stop = true; + + if (pthread_join(worker_thread, NULL) == -1) { + perror("pthread_join"); + exit(1); + } +} + +void *Input::do_work_thunk(void *arg) +{ + Input *input = static_cast(arg); + input->do_work(); + return NULL; +} + +void Input::do_work() { CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); @@ -40,6 +70,10 @@ void Input::run(const string &url) size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata) { Input *input = static_cast(userdata); + if (input->should_stop) { + return 0; + } + size_t bytes = size * nmemb; input->curl_callback(ptr, bytes); return bytes;