static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) { hls_dbg("mongoose callback to proxy_live\n"); if (ev == MG_EVENT_LOG) { hls_dbg("%s\n", (const char *)mg_get_request_info(conn)->ev_data); } if (ev == MG_NEW_REQUEST) { hls_info("mongoose proxy get queue from hls\n"); mg_get_info_from_hls(conn, hls_ctx_g); } // Returning NULL marks request as not handled, signalling mongoose to // proceed with handling it. return NULL; }
int hls_which_live_src(char *url) { /* TODO now just for LETV(CCTV1 & CCTV3...) */ int ret = 0; if (!strncmp(url, LETV_SRC, strlen(LETV_SRC))) { ret = LETV; hls_dbg("Live TV source is [channel => LETV]\n"); } return ret; }
//int curl_http_header(char *url_live, http_payload_t *http_payload, int *flag) int curl_http_header(char *url_live, http_payload_t *host_url, http_payload_t *http_payload, int *flag) { CURL *curl_handle; char *redirect_url; //int response_code; long response_code; //http_payload_t *chunk = http_payload; http_payload_t *chunk = host_url; /* FIXME may cause memory lost * if http_payload->memory already has a memory, the free job must * give to the pointer which it give to */ chunk->memory = malloc(1); /* will be grown as needed by the realloc above */ chunk->size = 0; /* no data at this point */ //hls_dbg("[curl get request]===>%s\n", url_live); /* FIXME * QQTV's source do not support HTTP HEAD/ request, so we must request HTTP GET/ * but in curl_http_header(), we do not send http body out */ //http_payload_t body_duty; //body_duty.memory = malloc(1); //body_duty.size = 0; http_payload_t *chunk1 = http_payload; chunk1->memory = malloc(1); chunk1->size = 0; //curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */ curl_handle = curl_easy_init(); /* specify URL to get */ curl_easy_setopt(curl_handle, CURLOPT_URL, url_live); /* do not download body */ //curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1); /* FIXME we need get the rederection url, so not do it here */ //curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); /* we pass our 'chunk' struct to the callback function */ //curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&body_duty); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)chunk1); /* add http GET/ range context */ curl_easy_setopt(curl_handle, CURLOPT_RANGE,"0-"); /* some servers don't like requests that are made without a user-agent * field, so we provide one */ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); //curl_easy_setopt(curl_handle, CURLOPT_FORBID_REUSE, 1); /* some servers don't like requests that are made without a user-agent field, so we provide one */ //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); /* this fun will not return until the http payload is all download */ curl_easy_perform(curl_handle); /* get http response code */ curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &response_code); //hls_dbg("response_code = %d\n", response_code); /* for QQTV souct, release the body_duty.memory, do not send data out */ //free(body_duty.memory); if (response_code == HLS_REDIRECT) { /* get redirect_url 2013-01-19 */ curl_easy_getinfo(curl_handle, CURLINFO_REDIRECT_URL, &redirect_url); hls_dbg("redirect_url >>> %s\n", redirect_url); chunk->memory = realloc(chunk->memory, strlen(redirect_url) + 1); if (chunk->memory == NULL) { /* out of memory! */ hls_err("not enough memory (realloc returned NULL)\n"); exit(EXIT_FAILURE); } memcpy(&(chunk->memory[chunk->size]), redirect_url, strlen(redirect_url)); chunk->size += strlen(redirect_url); chunk->memory[chunk->size] = 0; *flag = HLS_REDIRECT; } //else if (response_code != 200) //*flag = 0; /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); /* * Now, our chunk.memory points to a memory block that is chunk.size * bytes big and contains the remote file. * * Do something nice with it! * * You should be aware of the fact that at this point we might have an * allocated data block, and nothing has yet deallocated that data. So when * you're done with it, you should free() it as a nice application. */ //hls_dbg("%lu bytes retrieved\n", (long)chunk->size); /* FIXME * modified by juguofeng * do not free it here only in this project */ //if(chunk->memory) // free(chunk->memory); /* we're done with libcurl, so clean it up */ //curl_global_cleanup(); return 0; }