-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplay.py
More file actions
199 lines (172 loc) · 7.29 KB
/
play.py
File metadata and controls
199 lines (172 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright (C) 2018 Alexander Seiler
#
#
# This file is part of script.module.srgssr.
#
# script.module.srgssr is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# script.module.srgssr is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with script.module.srgssr.
# If not, see <http://www.gnu.org/licenses/>.
import json
import xbmc
import xbmcgui
import xbmcplugin
import inputstreamhelper
import utils
class Player:
"""Handles playback logic for the SRGSSR plugin."""
def __init__(self, srgssr_instance):
self.srgssr = srgssr_instance
self.handle = srgssr_instance.handle
def play_video(self, media_id_or_urn):
"""
Gets the stream information starts to play it.
Keyword arguments:
media_id_or_urn -- the urn or id of the media to play
"""
if media_id_or_urn.startswith("urn:"):
urn = media_id_or_urn
media_id = media_id_or_urn.split(":")[-1]
else:
# TODO: Could fail for livestreams
media_type = "video"
urn = f"urn:{self.srgssr.bu}:{media_type}:{media_id_or_urn}"
media_id = media_id_or_urn
self.srgssr.log("play_video, urn = " + urn + ", media_id = " + media_id)
detail_url = (
"https://il.srgssr.ch/integrationlayer/2.0/mediaComposition/byUrn/" + urn
)
json_response = json.loads(self.srgssr.open_url(detail_url))
title = utils.try_get(json_response, ["episode", "title"], str, urn)
chapter_list = utils.try_get(
json_response, "chapterList", data_type=list, default=[]
)
if not chapter_list:
self.srgssr.log("play_video: no stream URL found (chapterList empty).")
return
first_chapter = utils.try_get(chapter_list, 0, data_type=dict, default={})
chapter = next(
(e for e in chapter_list if e.get("id") == media_id), first_chapter
)
resource_list = utils.try_get(
chapter, "resourceList", data_type=list, default=[]
)
if not resource_list:
self.srgssr.log("play_video: no stream URL found. (resourceList empty)")
return
stream_urls = {
"SD": "",
"HD": "",
}
mf_type = "hls"
drm = False
for resource in resource_list:
if utils.try_get(resource, "drmList", data_type=list, default=[]):
drm = True
break
if utils.try_get(resource, "protocol") == "HLS":
for key in ("SD", "HD"):
if utils.try_get(resource, "quality") == key:
stream_urls[key] = utils.try_get(resource, "url")
if drm:
self.play_drm(urn, title, resource_list)
return
if not stream_urls["SD"] and not stream_urls["HD"]:
self.srgssr.log("play_video: no stream URL found.")
return
stream_url = (
stream_urls["HD"]
if (stream_urls["HD"] and self.srgssr.prefer_hd) or not stream_urls["SD"]
else stream_urls["SD"]
)
self.srgssr.log(f"play_video, stream_url = {stream_url}")
auth_url = self.srgssr.get_auth_url(stream_url)
start_time = end_time = None
if utils.try_get(json_response, "segmentUrn"):
segment_list = utils.try_get(
chapter, "segmentList", data_type=list, default=[]
)
for segment in segment_list:
if (
utils.try_get(segment, "id") == media_id
or utils.try_get(segment, "urn") == urn
):
start_time = utils.try_get(
segment, "markIn", data_type=int, default=None
)
if start_time:
start_time = start_time // 1000
end_time = utils.try_get(
segment, "markOut", data_type=int, default=None
)
if end_time:
end_time = end_time // 1000
break
self.srgssr.log(f"play_video, auth_url = {auth_url}")
play_item = xbmcgui.ListItem(title, path=auth_url)
if self.srgssr.get_kodi_major_version() >= 20 and start_time and end_time:
info_tag = play_item.getVideoInfoTag()
duration = end_time - start_time
info_tag.setResumePoint(start_time, duration)
info_tag.addVideoStream(xbmc.VideoStreamDetail(duration=duration))
info_tag.setDuration(duration)
subs = self.srgssr.get_subtitles(stream_url, urn)
if subs:
play_item.setSubtitles(subs)
play_item.setProperty("inputstream", "inputstream.adaptive")
play_item.setProperty("inputstream.adaptive.manifest_type", mf_type)
play_item.setProperty("IsPlayable", "true")
xbmcplugin.setResolvedUrl(self.handle, True, play_item)
def play_drm(self, urn, title, resource_list):
self.srgssr.log(f"play_drm: urn = {urn}")
preferred_quality = "HD" if self.srgssr.prefer_hd else "SD"
resource_data = {
"url": "",
"lic_url": "",
}
for resource in resource_list:
url = utils.try_get(resource, "url")
if not url:
continue
quality = utils.try_get(resource, "quality")
lic_url = ""
if utils.try_get(resource, "protocol") == "DASH":
drmlist = utils.try_get(resource, "drmList", data_type=list, default=[])
for item in drmlist:
if utils.try_get(item, "type") == "WIDEVINE":
lic_url = utils.try_get(item, "licenseUrl")
resource_data["url"] = url
resource_data["lic_url"] = lic_url
if resource_data["lic_url"] and quality == preferred_quality:
break
if not resource_data["url"] or not resource_data["lic_url"]:
self.srgssr.log("play_drm: No stream found")
return
manifest_type = "mpd"
drm = "com.widevine.alpha"
helper = inputstreamhelper.Helper(manifest_type, drm=drm)
if not helper.check_inputstream():
self.srgssr.log("play_drm: Unable to setup drm")
return
play_item = xbmcgui.ListItem(
title, path=self.srgssr.get_auth_url(resource_data["url"])
)
ia = "inputstream.adaptive"
play_item.setProperty("inputstream", ia)
lic_key = (
f"{resource_data['lic_url']}|"
"Content-Type=application/octet-stream|R{SSM}|"
)
play_item.setProperty(f"{ia}.manifest_type", manifest_type)
play_item.setProperty(f"{ia}.license_type", drm)
play_item.setProperty(f"{ia}.license_key", lic_key)
xbmcplugin.setResolvedUrl(self.handle, True, play_item)