From: Jaroslav Kysela Date: Mon, 11 Sep 2017 12:20:53 +0000 (+0200) Subject: htsp: add subscription entry also for DVR files, fixes #3854 X-Git-Tag: v4.2.4~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b96d9291303c6160d555b90a54702f30e7db46bd;p=thirdparty%2Ftvheadend.git htsp: add subscription entry also for DVR files, fixes #3854 --- diff --git a/src/htsp_server.c b/src/htsp_server.c index b1fe329d4..1db4cd1e8 100644 --- a/src/htsp_server.c +++ b/src/htsp_server.c @@ -240,6 +240,7 @@ typedef struct htsp_file { int hf_fd; // Our file descriptor char *hf_path; // For logging uint32_t hf_de_id; // Associated dvr entry + th_subscription_t *hf_subscription; } htsp_file_t; #define HTSP_DEFAULT_QUEUE_DEPTH 500000 @@ -728,6 +729,17 @@ htsp_file_open(htsp_connection_t *htsp, const char *path, int fd, dvr_entry_t *d hf->hf_id = ++htsp->htsp_file_id; hf->hf_path = strdup(path); hf->hf_de_id = de ? idnode_get_short_uuid(&de->de_id) : 0; + + if (de) { + const char *charset = de->de_config ? de->de_config->dvr_charset_id : NULL; + + hf->hf_subscription = + subscription_create_from_file("HTSP", charset, path, + htsp->htsp_peername, + htsp->htsp_granted_access->aa_representative, + htsp->htsp_clientname); + } + LIST_INSERT_HEAD(&htsp->htsp_files, hf, hf_link); htsmsg_t *rep = htsmsg_create_map(); @@ -765,12 +777,24 @@ static void htsp_file_destroy(htsp_file_t *hf) { tvhdebug(LS_HTSP, "Closed opened file %s", hf->hf_path); + LIST_REMOVE(hf, hf_link); + if (hf->hf_subscription) + subscription_unsubscribe(hf->hf_subscription, UNSUBSCRIBE_FINAL); free(hf->hf_path); close(hf->hf_fd); - LIST_REMOVE(hf, hf_link); free(hf); } +static void +htsp_file_update_stats(htsp_file_t *hf, size_t len) +{ + th_subscription_t *ts = hf->hf_subscription; + if (ts) { + subscription_add_bytes_in(ts, len); + subscription_add_bytes_out(ts, len); + } +} + /* ************************************************************************** * Output message generators * *************************************************************************/ @@ -2787,6 +2811,8 @@ htsp_method_file_read(htsp_connection_t *htsp, htsmsg_t *in) goto error; } + htsp_file_update_stats(hf, r); + rep = htsmsg_create_map(); htsmsg_add_bin(rep, "data", m, r); free(m); diff --git a/src/subscriptions.c b/src/subscriptions.c index 03da4b1e7..17a654bd0 100644 --- a/src/subscriptions.c +++ b/src/subscriptions.c @@ -41,6 +41,7 @@ #include "notify.h" #include "atomic.h" #include "input.h" +#include "intlconv.h" #include "dbus.h" struct th_subscription_list subscriptions; @@ -960,6 +961,39 @@ subscription_create_from_mux(profile_chain_t *prch, } #endif +/** + * + */ +th_subscription_t * +subscription_create_from_file(const char *name, + const char *charset, + const char *filename, + const char *hostname, + const char *username, + const char *client) +{ + th_subscription_t *ts; + char *str, *url; + + ts = subscription_create(NULL, 1, name, + SUBSCRIPTION_NONE, NULL, + hostname, username, client); + if (ts == NULL) + return NULL; + str = intlconv_to_utf8safestr(charset, filename, strlen(filename) * 3); + if (str == NULL) + str = intlconv_to_utf8safestr(intlconv_charset_id("ASCII", 1, 1), + filename, strlen(filename) * 3); + if (str == NULL) + str = strdup("error"); + url = malloc(strlen(str) + 7 + 1); + strcpy(url, "file://"); + strcat(url, str); + ts->ths_dvrfile = url; + free(str); + return ts; +} + /* ************************************************************************** * Status monitoring * *************************************************************************/ diff --git a/src/subscriptions.h b/src/subscriptions.h index 330f3bddb..a79962615 100644 --- a/src/subscriptions.h +++ b/src/subscriptions.h @@ -197,6 +197,14 @@ subscription_create_from_mux(struct profile_chain *prch, int *error); #endif +th_subscription_t * +subscription_create_from_file(const char *name, + const char *charset, + const char *filename, + const char *hostname, + const char *username, + const char *client); + th_subscription_t *subscription_create(struct profile_chain *prch, int weight, const char *name, int flags, streaming_ops_t *ops, diff --git a/src/webui/webui.c b/src/webui/webui.c index e9d03c9fc..ed74849bd 100644 --- a/src/webui/webui.c +++ b/src/webui/webui.c @@ -1694,32 +1694,19 @@ page_dvrfile_preop(http_connection_t *hc, off_t file_start, size_t content_len, void *opaque) { page_dvrfile_priv_t *priv = opaque; - char *str, *basename; dvr_entry_t *de; pthread_mutex_lock(&global_lock); priv->tcp_id = http_stream_preop(hc); priv->sub = NULL; if (priv->tcp_id && !hc->hc_no_output && content_len > 64*1024) { - priv->sub = subscription_create(NULL, 1, "HTTP", - SUBSCRIPTION_NONE, NULL, - hc->hc_peer_ipstr, hc->hc_username, - http_arg_get(&hc->hc_args, "User-Agent")); + priv->sub = subscription_create_from_file("HTTP", priv->charset, + priv->fname, hc->hc_peer_ipstr, + hc->hc_username, + http_arg_get(&hc->hc_args, "User-Agent")); if (priv->sub == NULL) { http_stream_postop(priv->tcp_id); priv->tcp_id = NULL; - } else { - str = intlconv_to_utf8safestr(priv->charset, priv->fname, strlen(priv->fname) * 3); - if (str == NULL) - str = intlconv_to_utf8safestr(intlconv_charset_id("ASCII", 1, 1), - priv->fname, strlen(priv->fname) * 3); - if (str == NULL) - str = strdup("error"); - basename = malloc(strlen(str) + 7 + 1); - strcpy(basename, "file://"); - strcat(basename, str); - priv->sub->ths_dvrfile = basename; - free(str); } } /* Play count + 1 when write access */