-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_transcoding.py
executable file
·180 lines (154 loc) · 5.29 KB
/
update_transcoding.py
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
#!/usr/bin/env python3
import os
import re
import sys
import dbus
import json
import tempfile
import time
from urllib.parse import urlparse
from http.client import HTTPConnection, HTTPSConnection
from base64 import b64encode
CONF_DIR = "/opt/transcoder/config"
UNIT_REGEXP = re.compile("^transcode@(.+)\.target$")
# Fetch json via http/s with optional basicauth
def fetch(url, user=None, password=None):
u = urlparse(url)
headers = {}
if u.scheme == "https":
c = HTTPSConnection(u.netloc)
elif u.scheme == "http":
c = HTTPConnection(u.netloc)
else:
print(f"Unknown scheme in url: {u.scheme}")
if user is not None and password is not None:
token = b64encode(f"{user}:{password}".encode()).decode("ascii")
headers["Authorization"] = f"Basic {token}"
c.request("GET", u.path, headers=headers)
res = c.getresponse()
if res.status != 200:
print(f"Fetch failed: {res.status} {res.reason}")
return
data = None
try:
data = json.loads(res.read().decode())
except json.decoder.JSONDecodeError as e:
print("Invalid JSON format: ", e)
return data
# get streams we are responsible for
def filter_streams(data, name):
streams = data["streams"]
ret = {}
for stream in streams:
if "transcoding" in stream and "worker" in stream["transcoding"] and stream["transcoding"]["worker"] == name:
ret[stream["key"]] = stream
return ret
def template_config(stream):
res = f"""\
stream_key={stream["key"]}
transcoding_source={stream["source"]}
transcoding_sink={stream["transcoding"]["sink"]}
artwork_base={stream["artwork"]["base"]}
"""
if "options" in stream:
for (key, value) in stream["options"].items():
res += f"{key}={value}"
return res
def gen_config(stream):
path = CONF_DIR
conf = template_config(stream)
path = os.path.join(CONF_DIR, stream["key"])
# read and compare original config
existing = ""
try:
with open(path, "r") as f:
existing = f.read()
except FileNotFoundError:
pass
# replace atomically if changed
if existing != conf:
with tempfile.NamedTemporaryFile("w", delete=False) as f:
f.write(conf)
tmp = f.name
os.replace(tmp, path)
return True
return False
def get_running():
proxy = bus.get_object("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager/Devices/eth0")
def get_unitname(stream):
return f"transcode@{stream['key']}.target"
def restart_task(stream, mng):
unitname = get_unitname(stream)
print("restart/enable", unitname)
try:
mng.RestartUnit(unitname, "replace",
dbus_interface="org.freedesktop.systemd1.Manager")
mng.EnableUnitFiles([unitname], False, True,
dbus_interface="org.freedesktop.systemd1.Manager")
except dbus.exceptions.DBusException as e:
print("Failed to restart/enable", unitname, e)
return False
return True
def stop_task(stream, mng):
unitname = get_unitname(stream)
print("stop/disable", unitname)
try:
mng.DisableUnitFiles([unitname], False,
dbus_interface="org.freedesktop.systemd1.Manager")
mng.StopUnit(unitname, "replace",
dbus_interface="org.freedesktop.systemd1.Manager")
except dbus.exceptions.DBusException as e:
print("Failed to stop/disable", unitname, e)
return False
return True
# bring transcoding to desired state
def sync_tasks(streams):
bus = dbus.SystemBus()
# get transcode units
mng = bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
units = mng.ListUnits(dbus_interface="org.freedesktop.systemd1.Manager")
old_streams = {}
for unit in units:
match = UNIT_REGEXP.match(unit[0])
if match is not None:
key = match[1]
if key in streams:
streams[key]["unit"] = unit
else:
old_streams[key] = {"key": key, "unit": unit}
# place configs
os.makedirs(CONF_DIR, exist_ok=True)
for key in streams:
stream = streams[key]
didChange = gen_config(stream)
if didChange:
res = restart_task(stream, mng)
# remove config file to allow retry
if not res:
os.unlink(f"{CONF_DIR}/{key}")
# stop old and remove configs
for key in old_streams:
stream = old_streams[key]
res = stop_task(stream, mng)
# remove config file when service is gone
if res:
try:
os.unlink(f"{CONF_DIR}/{key}")
except FileNotFoundError:
pass
def main():
import argparse
parser = argparse.ArgumentParser(description="Fetch stream info and adapt local transcoding tasks")
parser.add_argument("url", help="streaming info url")
parser.add_argument("name", help="our transcoder name")
parser.add_argument("-u", "--user", help="user for http basic-auth")
parser.add_argument("-p", "--password", help="password for http basic-auth")
args = parser.parse_args()
data = fetch(args.url, args.user, args.password)
if data is None:
sys.exit(1)
streams = filter_streams(data, args.name)
sync_tasks(streams)
if __name__ == "__main__":
main()