From a3770523c0b365a97ac47306be19b8ad3b035f4b Mon Sep 17 00:00:00 2001 From: Jan Schmidt Date: Thu, 17 Nov 2022 03:49:12 +1100 Subject: [PATCH] adaptivedemux2/downloadrequest: add a helper to retrieve the Age header Add a method to look at HTTP response headers and parse and return any Age header, provided by caching proxy servers if the data was provided from a cache. Part-of: --- .../ext/adaptivedemux2/downloadrequest.c | 48 +++++++++++++++++++ .../ext/adaptivedemux2/downloadrequest.h | 2 + 2 files changed, 50 insertions(+) diff --git a/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.c b/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.c index 878dac1c68..5725db0f4c 100644 --- a/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.c +++ b/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.c @@ -476,6 +476,54 @@ download_request_get_caps (DownloadRequest * request) return caps; } +static GstClockTime +_get_age_header (GstStructure * headers) +{ + const GstStructure *response_headers; + const gchar *http_age; + const GValue *val; + + val = gst_structure_get_value (headers, "response-headers"); + if (!val) { + return 0; + } + + response_headers = gst_value_get_structure (val); + http_age = gst_structure_get_string (response_headers, "Date"); + if (!http_age) { + return 0; + } + + return atoi (http_age) * GST_SECOND; +} + +/* Return the age of the download from the Age header, + * or 0 if there was none */ +GstClockTime +download_request_get_age (DownloadRequest * request) +{ + DownloadRequestPrivate *priv = DOWNLOAD_REQUEST_PRIVATE (request); + GstClockTime age = 0; + + g_return_val_if_fail (request != NULL, age); + + if (request->state != DOWNLOAD_REQUEST_STATE_LOADING + && request->state != DOWNLOAD_REQUEST_STATE_COMPLETE) + return age; + + g_rec_mutex_lock (&priv->lock); + + if (request->headers != NULL) { + /* We have headers for the download, see if there was an Age + * header in the response */ + GstClockTime age = _get_age_header (request->headers); + GST_LOG ("Got cached data with age %" GST_TIMEP_FORMAT, &age); + } + g_rec_mutex_unlock (&priv->lock); + + return age; +} + void download_request_add_buffer (DownloadRequest * request, GstBuffer * buffer) { diff --git a/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.h b/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.h index 8fbaa000b7..280417b6da 100644 --- a/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.h +++ b/subprojects/gst-plugins-good/ext/adaptivedemux2/downloadrequest.h @@ -84,6 +84,8 @@ void download_request_set_caps (DownloadRequest * request, GstCaps * caps); GstCaps * download_request_get_caps (DownloadRequest * request); +GstClockTime download_request_get_age (DownloadRequest *request); + void download_request_add_buffer (DownloadRequest *request, GstBuffer *buffer); GstBuffer * download_request_take_buffer (DownloadRequest *request); GstBuffer * download_request_take_buffer_range (DownloadRequest *request, gint64 range_start, gint64 range_end);