Skip to content

Commit

Permalink
Changed: New InputStream.Adaptive parameters added.
Browse files Browse the repository at this point in the history
  • Loading branch information
basrieter committed Aug 4, 2024
1 parent b804ea1 commit d4eb92c
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 76 deletions.
3 changes: 2 additions & 1 deletion channels/channel.regionalnl/lokaal/chn_lokaal.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ def update_video_item(self, item):

if AddonSettings.use_adaptive_stream_add_on():
stream = item.add_stream(url, 0)
M3u8.set_input_stream_addon_input(stream, item.HttpHeaders)
M3u8.set_input_stream_addon_input(
stream, stream_headers=item.HttpHeaders, manifest_headers=item.HttpHeaders)
item.complete = True
else:
for s, b in M3u8.get_streams_from_m3u8(url, append_query_string=True):
Expand Down
4 changes: 3 additions & 1 deletion channels/channel.sbsnl/kijknl/chn_kijknl.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,9 @@ def update_graphql_item(self, item):
key_value=encryption_json,
key_headers={"Content-Type": "application/json", "authorization": "Basic {}".format(token)}
)
Mpd.set_input_stream_addon_input(stream, license_key=encryption_key, headers={"user-agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"})
Mpd.set_input_stream_addon_input(
stream, license_key=encryption_key,
stream_headers={"user-agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"})
item.complete = True

elif stream_type == "m3u8" and not drm:
Expand Down
110 changes: 78 additions & 32 deletions resources/lib/streams/adaptive.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import Dict, Optional
from urllib.parse import urlencode, quote

from resources.lib.addonsettings import AddonSettings
from resources.lib.helpers.htmlentityhelper import HtmlEntityHelper
Expand Down Expand Up @@ -49,33 +51,41 @@ def get_license_key(key_url, key_type="R", key_headers=None, key_value=None, jso

return "{0}|{1}|{2}|{3}".format(key_url, header.strip("&"), key_value, json_filter)

# noinspection PyUnusedLocal
# noinspection PyUnusedLocal,PyUnresolvedReferences
@staticmethod
def set_input_stream_addon_input(strm,
headers=None,
addon="inputstream.adaptive",
manifest_type=None,
license_key=None,
license_type=None,
max_bit_rate=None,
persist_storage=False,
service_certificate=None,
manifest_update=None):
""" Updates an existing stream with parameters for the inputstream adaptive add-on.
def set_input_stream_addon_input(strm: "MediaStream",
stream_headers: Optional[Dict[str, str]] = None,
stream_parameters: Optional[Dict[str, str]] = None,
addon: str = "inputstream.adaptive",
manifest_type: Optional[str] = None,

license_key: Optional[str] = None,
license_type: Optional[str] = None,
max_bit_rate: Optional[int] = 0,
persist_storage: bool = False,
service_certificate: Optional[str] = None,

manifest_params: Optional[Dict[str, str]] = None,
manifest_headers: Optional[Dict[str, str]] = None,
manifest_update_params: Optional[str] = None,
manifest_upd_params: Optional[Dict[str, str]] = None) -> "MediaStream":

:param strm: (MediaStream) the MediaStream to update
:param dict headers: Possible HTTP Headers
:param str addon: Adaptive add-on to use
:param str manifest_type: Type of manifest (hls/mpd)
:param str license_key: The value of the license key request
:param str license_type: The type of license key request used (see below)
:param int max_bit_rate: The maximum bitrate to use (optional)
:param bool persist_storage: Should we store certificates? And request server certificates?
:param str service_certificate: Use the specified server certificate
:param str manifest_update: How should the manifest be updated
""" Updates an existing stream with parameters for the inputstream adaptive add-on.
:returns: The updated stream
:rtype: MediaStream
:param strm: The MediaStream to update
:param stream_headers: Possible HTTP Headers for the stream.
:param stream_parameters: The stream parameters.
:param addon: Adaptive add-on to use
:param manifest_type: Type of manifest (hls/mpd)
:param license_key: The value of the license key request
:param license_type: The type of license key request used (see below)
:param max_bit_rate: The maximum bitrate to use (optional)
:param persist_storage: Should we store certificates? And request server certificates?
:param service_certificate: Use the specified server certificate
:param manifest_headers: The headers to add to the manifest request.
:param manifest_params: The parameters to asdd to the manifest request.
:param manifest_update_params: How should the manifest be updated ("full"). Deprecated in v21
:param manifest_upd_params: The request parameters for the manifest update requests.
Can be used like this:
Expand All @@ -86,6 +96,8 @@ def set_input_stream_addon_input(strm,
if maxBitRate is not set, the bitrate will be configured via the normal generic Retrospect
or channel settings.
https://github.com/xbmc/inputstream.adaptive/wiki/Integration
"""

if manifest_type is None:
Expand All @@ -101,24 +113,58 @@ def set_input_stream_addon_input(strm,

# See https://github.com/peak3d/inputstream.adaptive/blob/master/inputstream.adaptive/addon.xml.in
strm.add_property("inputstream.adaptive.manifest_type", manifest_type)

if license_key:
strm.add_property("inputstream.adaptive.license_key", license_key)
if license_type:
strm.add_property("inputstream.adaptive.license_type", license_type)

if max_bit_rate:
strm.add_property("inputstream.adaptive.max_bandwidth", max_bit_rate * 1000)
strm.add_property("inputstream.adaptive.max_bandwidth", str(max_bit_rate * 1000))
if persist_storage:
strm.add_property("inputstream.adaptive.license_flags", "persistent_storage")
if service_certificate is not None:
strm.add_property("inputstream.adaptive.server_certificate", service_certificate)
if manifest_update:
strm.add_property("inputstream.adaptive.manifest_update_parameter", manifest_update)

if headers:
header = ""
for k, v in headers.items():
header = "{0}&{1}={2}".format(header, k, HtmlEntityHelper.url_encode(v))
strm.add_property("inputstream.adaptive.stream_headers", header.strip("&"))
# Stream stuff
if stream_headers:
# On Kodi v19 or below: Specifies the HTTP headers to be used to download manifests
# and streams (audio/video/subtitles).
#
# On Kodi v20: Specifies the HTTP headers to be used to download manifests
# and streams (audio/video/subtitles).
# NOTE: Use this property to set headers to the manifests is a deprecated behaviour,
# use inputstream.adaptive.manifest_headers instead.
#
# From Kodi v21 or above:
# Specifies the HTTP headers to be used to download streams (audio/video/subtitles) only.
params = urlencode(stream_headers, quote_via=quote)
strm.add_property("inputstream.adaptive.stream_headers", params)

if stream_parameters and AddonSettings.is_min_version(AddonSettings.KodiNexus):
params = urlencode(stream_parameters, quote_via=quote)
strm.add_property("inputstream.adaptive.stream_params", params)

# Manifest stuff
if AddonSettings.is_min_version(AddonSettings.KodiNexus):
if manifest_params:
params = urlencode(manifest_params, quote_via=quote)
strm.add_property("inputstream.adaptive.manifest_params", params)

if manifest_headers:
params = urlencode(manifest_headers, quote_via=quote)
strm.add_property("inputstream.adaptive.manifest_headers", params)
elif stream_headers:
params = urlencode(stream_headers, quote_via=quote)
strm.add_property("inputstream.adaptive.manifest_headers", params)

if manifest_update_params and not AddonSettings.is_min_version(AddonSettings.KodiOmega):
# WARNING: PROPERTY DEPRECATED ON Kodi v21 AND REMOVED ON Kodi v22, please use manifest_upd_params instead.
strm.add_property("inputstream.adaptive.manifest_update_parameter", manifest_update_params)

if manifest_upd_params and AddonSettings.is_min_version(AddonSettings.KodiOmega):
params = urlencode(manifest_upd_params, quote_via=quote)
strm.add_property("inputstream.adaptive.manifest_upd_params", params)

return strm

Expand Down
53 changes: 34 additions & 19 deletions resources/lib/streams/m3u8.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import Optional, Dict

from resources.lib.urihandler import UriHandler
from resources.lib.logger import Logger
Expand Down Expand Up @@ -73,25 +74,35 @@ def get_subtitle(url, play_list_data=None, append_query_string=True, language=No
return sub

@staticmethod
def set_input_stream_addon_input(strm,
headers=None,
license_key=None, license_type=None,
max_bit_rate=None,
persist_storage=False,
service_certificate=None,
manifest_update=None):
""" Updates an existing stream with parameters for the inputstream adaptive add-on.
def set_input_stream_addon_input(strm: MediaStream,
stream_headers: Optional[Dict[str, str]] = None,
stream_parameters: Optional[Dict[str, str]] = None,

license_key: Optional[str] = None,
license_type: Optional[str] = None,
max_bit_rate: Optional[int] = 0,
persist_storage: bool = False,
service_certificate: Optional[str] = None,

:param MediaStream strm: The MediaStream to update
:param dict headers: Possible HTTP Headers
:param str license_key: The value of the license key request
:param str license_type: The type of license key request used (see below)
:param int max_bit_rate: The maximum bitrate to use (optional)
:param bool persist_storage: Should we store certificates? And request server certificates?
:param str service_certificate: Use the specified server certificate
manifest_params: Optional[Dict[str, str]] = None,
manifest_headers: Optional[Dict[str, str]] = None,
manifest_update_params: Optional[str] = None,
manifest_upd_params: Optional[Dict[str, str]] = None) -> MediaStream:

""" Updates an existing stream with parameters for the inputstream adaptive add-on.
:returns: The updated stream
:rtype: MediaStream
:param strm: The MediaStream to update
:param stream_headers: Possible HTTP Headers for the stream.
:param stream_parameters: The stream parameters.
:param license_key: The value of the license key request
:param license_type: The type of license key request used (see below)
:param max_bit_rate: The maximum bitrate to use (optional)
:param persist_storage: Should we store certificates? And request server certificates?
:param service_certificate: Use the specified server certificate
:param manifest_headers: The headers to add to the manifest request.
:param manifest_params: The parameters to asdd to the manifest request.
:param manifest_update_params: How should the manifest be updated ("full"). Deprecated in v21
:param manifest_upd_params: The request parameters for the manifest update requests.
Can be used like this:
Expand All @@ -105,14 +116,18 @@ def set_input_stream_addon_input(strm,
"""

return Adaptive.set_input_stream_addon_input(strm,
headers,
stream_headers=stream_headers,
stream_parameters=stream_parameters,
manifest_type="hls",
license_key=license_key,
license_type=license_type,
max_bit_rate=max_bit_rate,
persist_storage=persist_storage,
service_certificate=service_certificate,
manifest_update=manifest_update)
manifest_params=manifest_params,
manifest_headers=manifest_headers,
manifest_update_params=manifest_update_params,
manifest_upd_params=manifest_upd_params)

@staticmethod
def get_license_key(key_url, key_type="R", key_headers=None, key_value=None):
Expand Down
Loading

0 comments on commit d4eb92c

Please sign in to comment.