From: Jaroslav Kysela Date: Sun, 9 Dec 2018 16:34:13 +0000 (+0100) Subject: iptv http input: play with the locking, issue #5353 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f0524db407764aeaad4229aa7301babf05a6de79;p=thirdparty%2Ftvheadend.git iptv http input: play with the locking, issue #5353 --- diff --git a/src/input/mpegts/iptv/iptv_http.c b/src/input/mpegts/iptv/iptv_http.c index 43e0d8da7..9ad78b601 100644 --- a/src/input/mpegts/iptv/iptv_http.c +++ b/src/input/mpegts/iptv/iptv_http.c @@ -32,8 +32,11 @@ typedef struct http_priv { iptv_input_t *mi; iptv_mux_t *im; http_client_t *hc; + gtimer_t kick_timer; uint8_t shutdown; uint8_t started; + uint8_t flush; + uint8_t unpause; sbuf_t m3u_sbuf; sbuf_t key_sbuf; int m3u_header; @@ -61,35 +64,6 @@ typedef struct http_priv { static int iptv_http_complete_key ( http_client_t *hc ); -/* - * - */ -static int -iptv_http_safe_global_lock( http_priv_t *hp ) -{ - iptv_mux_t *im = hp->im; - int r; - - while (1) { - if (im->mm_active == NULL || hp->shutdown) - return 0; - r = tvh_mutex_trylock(&global_lock); - if (r == 0) - break; - if (r != EBUSY) - continue; - sched_yield(); - if (im->mm_active == NULL || hp->shutdown) - return 0; - r = tvh_mutex_trylock(&global_lock); - if (r == 0) - break; - if (r == EBUSY) - tvh_safe_usleep(10000); - } - return 1; -} - /* * */ @@ -204,6 +178,38 @@ iptv_http_data_aes128 ( http_priv_t *hp, sbuf_t *sb, int off ) AES_cbc_encrypt(in, out, size, &hp->hls_aes128.key, hp->hls_aes128.iv, AES_DECRYPT); } +/* + * + */ +static void +iptv_http_kick_cb( void *aux ) +{ + http_client_t *hc = aux; + http_priv_t *hp; + iptv_mux_t *im; + + if (hc == NULL) return; + hp = hc->hc_aux; + if (hp == NULL) return; + im = hp->im; + if (im == NULL) return; + if (hp->flush) { + hp->flush = 0; + if (!hp->started) { + iptv_input_mux_started(hp->mi, im); + } else { + iptv_input_recv_flush(im); + } + hp->started = 1; + } + + if (hp->unpause) { + hp->unpause = 0; + if (im->mm_active && !hp->shutdown) + mtimer_arm_rel(&im->im_pause_timer, iptv_input_unpause, im, sec2mono(1)); + } +} + /* * Connected */ @@ -248,15 +254,8 @@ iptv_http_header ( http_client_t *hc ) hp->m3u_header = 0; hp->off = 0; - if (iptv_http_safe_global_lock(hp)) { - if (!hp->started) { - iptv_input_mux_started(hp->mi, hp->im); - } else { - iptv_input_recv_flush(hp->im); - } - tvh_mutex_unlock(&global_lock); - hp->started = 1; - } + hp->flush = 1; + gtimer_arm_rel(&hp->kick_timer, iptv_http_kick_cb, hc, 0); return 0; } @@ -343,13 +342,11 @@ iptv_http_data if (iptv_input_recv_packets(im, len) == 1) pause = hc->hc_pause = 1; + if (pause) hp->unpause = 1; tvh_mutex_unlock(&iptv_lock); - if (pause && iptv_http_safe_global_lock(hp)) { - if (im->mm_active && !hp->shutdown) - mtimer_arm_rel(&im->im_pause_timer, iptv_input_unpause, im, sec2mono(1)); - tvh_mutex_unlock(&global_lock); - } + if (pause) + gtimer_arm_rel(&hp->kick_timer, iptv_http_kick_cb, hc, 0); return 0; } @@ -558,6 +555,7 @@ iptv_http_stop http_priv_t *hp = im->im_data; hp->shutdown = 1; + gtimer_disarm(&hp->kick_timer); tvh_mutex_unlock(&iptv_lock); http_client_close(hp->hc); tvh_mutex_lock(&iptv_lock);