Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.

2 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/demo/network-computer.git
HTTPS
https://gitstable.com/git/demo/network-computer.git
SSH
ssh://git@gitstable.com:2222/demo/network-computer.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

GitStable demo README: say this is a copy and where the original is cde82c2 14 hours, 3 minutes ago History
nc-stream · 17.7 KB · 441 lines
1#!/usr/bin/env python3
2"""Live streams into the desk: a phone (the SRT Stream app), OBS, ffmpeg...
3MediaMTX receives them (nc-streams.service); this is the one command for
4people, voice and scripts.
5
6 nc-stream on | off open or close the door (UDP 8890, SRT with a passphrase) [root]
7 nc-stream key the passphrase, and the address to put in the sender
8 nc-stream list what is live: name, video/audio, how long, how much
9 nc-stream look NAME [OUT.jpg] a picture of the stream as it is now (for AI, or a person)
10 nc-stream listen NAME [SECONDS] [OUT.wav] what it says: a clip of its sound (16 kHz mono)
11 nc-stream watch NAME open it in the desk's browser
12 nc-stream record NAME on|off keep it on the computer's disk (48 h), in 10-minute files
13 nc-stream recordings [NAME] what is kept
14 nc-stream relay-set TO keep where TO is (Twitch, YouTube...): its rtmp[s]:// address
15 with the stream key, read from the input, never an argument
16 nc-stream relay NAME TO send the stream on to TO, as it comes (no re-encoding)
17 nc-stream relay-stop TO | relays
18 nc-stream program NAME on|off what viewers get: the stream, or a slate while it is not there
19 (starting, reconnecting): published as NAME-program
20 nc-stream end NAME "thanks for watching" for 10 s, then the program and its relays stop
21 nc-stream ears on|off listen on live streams for "hey desk" (on this computer; on)
22 nc-stream eyes NAME|all on|off look when the picture changes and say what is there (off)
23 nc-stream say TEXT say it on the phone (its talkback)
24 nc-stream notice TEXT show it on the phone's screen
25
26The passphrase is in /etc/nc/streams.env (root and the desk user). Relay
27addresses, with their stream keys, are the desk user's own:
28~/.config/nc/stream-relays/TO.url (0600), never printed; relays run as that
29user and start again if either end drops. Everything but SRT publishing is on
30this computer only.
31"""
32import json, os, pathlib, re, secrets, subprocess, sys, time, urllib.parse, urllib.request
33
34ENV = "/etc/nc/streams.env"
35API = "http://127.0.0.1:9997/v3"
36RTSP = "rtsp://127.0.0.1:8554"
37PORT = 8890
38
39
40def info(key):
41 try:
42 for line in open("/etc/nc/info"):
43 if line.startswith(key + "="):
44 return line.split("=", 1)[1].strip().strip('"')
45 except OSError:
46 pass
47 return ""
48
49
50def desk_user():
51 try:
52 for line in open("/etc/nc/env"):
53 if line.startswith("NC_DESK_USER="):
54 return line.split("=", 1)[1].strip().strip('"')
55 except OSError:
56 pass
57 return os.environ.get("USER", "")
58
59
60def die(msg, code=1):
61 print(msg, file=sys.stderr)
62 sys.exit(code)
63
64
65def need_root():
66 if os.geteuid() != 0:
67 die("this needs root (ask voice, or ssh in as root)")
68
69
70def passphrase():
71 try:
72 for line in open(ENV):
73 if line.startswith("MTX_PATHDEFAULTS_SRTPUBLISHPASSPHRASE="):
74 return line.split("=", 1)[1].strip()
75 except PermissionError:
76 die(f"{ENV} is for root and the desk user")
77 except OSError:
78 pass
79 return ""
80
81
82def api(path, method="GET", body=None):
83 req = urllib.request.Request(API + path, method=method,
84 data=json.dumps(body).encode() if body is not None else None,
85 headers={"Content-Type": "application/json"})
86 try:
87 with urllib.request.urlopen(req, timeout=5) as r:
88 data = r.read()
89 return json.loads(data) if data else {}
90 except urllib.error.HTTPError as e:
91 raise RuntimeError(f"{e.code} {e.read().decode(errors='replace')[:200]}")
92 except OSError:
93 die("streams are off (nc-stream on)")
94
95
96def sender_url(name="phone"):
97 host = info("NC_DOMAIN") or info("NC_PUBLIC_IP") or "this-desk"
98 return f"srt://{host}:{PORT}?streamid=publish:{name}&passphrase=<passphrase>"
99
100
101def cmd_on():
102 need_root()
103 key = passphrase()
104 if len(key) < 10:
105 key = secrets.token_urlsafe(18) # 24 characters; SRT takes 10 to 79
106 user = desk_user()
107 pathlib.Path(ENV).write_text(f"MTX_PATHDEFAULTS_SRTPUBLISHPASSPHRASE={key}\n")
108 os.chmod(ENV, 0o640)
109 if user:
110 subprocess.run(["chgrp", user, ENV], check=False)
111 subprocess.run(["ufw", "allow", f"{PORT}/udp"], capture_output=True)
112 r = subprocess.run(["systemctl", "enable", "--now", "nc-streams"], capture_output=True, text=True)
113 if r.returncode:
114 die(r.stderr.strip())
115 print(f"streams on: {sender_url()}\n(nc-stream key shows the passphrase)")
116
117
118def cmd_off():
119 need_root()
120 for f in relay_dir().glob("*.url") if relay_dir().exists() else []:
121 cmd_relay_stop(f.stem, quiet=True)
122 subprocess.run(as_user(["sh", "-c", 'for p in "$XDG_RUNTIME_DIR"/nc-program/*.pid; do [ -f "$p" ] && kill $(cat "$p") 2>/dev/null; rm -f "$p"; done']),
123 capture_output=True)
124 subprocess.run(["systemctl", "disable", "--now", "nc-streams"], capture_output=True)
125 subprocess.run(["ufw", "delete", "allow", f"{PORT}/udp"], capture_output=True)
126 print("streams off: UDP 8890 closed")
127
128
129def cmd_key():
130 key = passphrase()
131 if not key:
132 die("no passphrase yet: nc-stream on")
133 host = info("NC_DOMAIN") or info("NC_PUBLIC_IP")
134 print(f"host {host}\nport {PORT}\nstream name any, e.g. phone\npassphrase {key}\n"
135 f"address srt://{host}:{PORT}?streamid=publish:phone&passphrase={key}")
136
137
138def live():
139 return api("/paths/list").get("items", [])
140
141
142def cmd_list(as_json=False):
143 items = live()
144 out = []
145 for p in items:
146 tracks = p.get("tracks") or []
147 since = p.get("readyTime") or ""
148 out.append({"name": p["name"], "live": bool(p.get("ready")), "tracks": tracks,
149 "since": since, "received_mb": round((p.get("bytesReceived") or 0) / 1e6, 1),
150 "watchers": len(p.get("readers") or [])})
151 if as_json:
152 print(json.dumps(out))
153 return
154 if not out:
155 print("nothing is live")
156 for s in out:
157 print(f"{s['name']:<16} {'live' if s['live'] else 'waiting':<8} {','.join(s['tracks']):<14} "
158 f"{s['received_mb']:>8} MB since {s['since'][:19].replace('T', ' ')}")
159
160
161def ensure_live(name):
162 for p in live():
163 if p["name"] == name and p.get("ready"):
164 return p
165 names = [p["name"] for p in live() if p.get("ready")]
166 die(f"no live stream {name!r}" + (f" (live: {', '.join(names)})" if names else " (nothing is live)"))
167
168
169def safe(name):
170 if not re.fullmatch(r"[A-Za-z0-9._~-]{1,64}", name or ""):
171 die(f"odd stream name {name!r}")
172 return name
173
174
175def cmd_look(name, out=None):
176 ensure_live(safe(name))
177 out = out or f"/tmp/nc-stream-{name}-{int(time.time())}.jpg"
178 r = subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-rtsp_transport", "tcp",
179 "-i", f"{RTSP}/{name}", "-frames:v", "1", "-vf", "scale='min(1280,iw)':-2", "-q:v", "3", out],
180 capture_output=True, text=True, timeout=30)
181 if r.returncode or not os.path.exists(out):
182 die(f"no picture from {name}: {r.stderr.strip()[:200]}")
183 print(out)
184
185
186def cmd_listen(name, seconds="8", out=None):
187 ensure_live(safe(name))
188 s = max(1, min(int(seconds), 120))
189 out = out or f"/tmp/nc-stream-{name}-{int(time.time())}.wav"
190 r = subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-rtsp_transport", "tcp",
191 "-i", f"{RTSP}/{name}", "-t", str(s), "-vn", "-ac", "1", "-ar", "16000", out],
192 capture_output=True, text=True, timeout=s + 30)
193 if r.returncode or not os.path.exists(out):
194 die(f"no sound from {name}: {r.stderr.strip()[:200]}")
195 print(out)
196
197
198def cmd_watch(name):
199 safe(name)
200 if program_running(name): # what viewers see, slates and all
201 name = f"{name}-program"
202 ensure_live(name)
203 url = f"http://127.0.0.1:8888/{name}/"
204 run = ["xdg-open", url] if os.geteuid() else ["nc-as-user", "xdg-open", url]
205 subprocess.Popen(run, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
206 print(f"opened {url}")
207
208
209def cmd_record(name, onoff):
210 safe(name)
211 on = onoff == "on"
212 try:
213 api(f"/config/paths/patch/{name}", "PATCH", {"record": on})
214 except RuntimeError:
215 api(f"/config/paths/add/{name}", "POST", {"record": on}) # not configured yet: add it
216 print(f"{name}: {'recording, 10-minute files kept 48 h' if on else 'not recording'}")
217
218
219def cmd_recordings(name=None):
220 root = pathlib.Path("/var/lib/nc-streams/recordings")
221 dirs = [root / name] if name else sorted(p for p in root.glob("*") if p.is_dir())
222 for d in dirs:
223 files = sorted(d.glob("*.mp4"))
224 if files:
225 mb = sum(f.stat().st_size for f in files) / 1e6
226 print(f"{d.name}: {len(files)} file(s), {mb:.0f} MB, newest {files[-1].name}")
227 if not any(root.glob("*/*.mp4")):
228 print("no recordings")
229
230
231def relay_dir():
232 """The desk user's relay addresses (root acts for them: voice)."""
233 home = os.path.expanduser("~") if os.geteuid() else f"/home/{desk_user()}"
234 return pathlib.Path(home) / ".config/nc/stream-relays"
235
236
237def as_user(cmd):
238 return cmd if os.geteuid() else ["nc-as-user", *cmd]
239
240
241def cmd_relay_set(to):
242 if os.geteuid() == 0:
243 die("relay-set is the desk user's (their stream keys): run it as them")
244 if sys.stdin.isatty(): # typed or pasted: not shown (the screen may be what is streamed)
245 import getpass
246 url = getpass.getpass(f"{to}: the rtmp:// or rtmps:// address with the stream key (not shown): ").strip()
247 else:
248 url = sys.stdin.readline().strip()
249 if not re.match(r"^rtmps?://\S+$", url):
250 die("give the rtmp:// or rtmps:// address with the stream key, e.g. rtmp://live.twitch.tv/app/<key>")
251 d = relay_dir()
252 d.mkdir(parents=True, exist_ok=True)
253 os.chmod(d, 0o700)
254 f = d / f"{safe(to)}.url"
255 f.write_text(url + "\n")
256 os.chmod(f, 0o600)
257 host = re.sub(r"^(rtmps?://[^/]+).*", r"\1", url)
258 print(f"kept {to}: {host}/… (nc-stream relay NAME {to})")
259
260
261def program_running(name):
262 run = f'p="$XDG_RUNTIME_DIR/nc-program/{name}.pid"; [ -f "$p" ] && kill -0 $(cat "$p") 2>/dev/null && echo yes'
263 return subprocess.run(as_user(["sh", "-c", run]), capture_output=True, text=True).stdout.strip() == "yes"
264
265
266def cmd_program(name, onoff="on"):
267 safe(name)
268 if onoff == "off":
269 run = f'p="$XDG_RUNTIME_DIR/nc-program/{name}.pid"; [ -f "$p" ] && kill $(cat "$p") 2>/dev/null; rm -f "$p"'
270 subprocess.run(as_user(["sh", "-c", run]), capture_output=True)
271 print(f"program {name}: off")
272 return
273 if program_running(name):
274 print(f"program {name}: already on ({name}-program)")
275 return
276 run = (f'd="$XDG_RUNTIME_DIR/nc-program"; mkdir -p "$d"; rm -f "$d/{name}.end"; '
277 f'setsid nc-program {name} > "$d/{name}.log" 2>&1 & echo $! > "$d/{name}.pid"')
278 subprocess.run(as_user(["sh", "-c", run]), capture_output=True)
279 for _ in range(30): # published within a few seconds
280 if any(p["name"] == f"{name}-program" and p.get("ready") for p in live()):
281 print(f"program {name}: on ({name}-program: the stream, or a slate while it is not there)")
282 return
283 time.sleep(0.5)
284 die(f"program {name} did not start (see $XDG_RUNTIME_DIR/nc-program/{name}.log)")
285
286
287def cmd_end(name):
288 safe(name)
289 if not program_running(name):
290 die(f"no program for {name}: nothing to end (nc-stream relay-stop TO stops a relay)")
291 # the goodbye, then the relays of it stop once it has been seen
292 run = (f'touch "$XDG_RUNTIME_DIR/nc-program/{name}.end"; '
293 f'(sleep 13; for f in "$XDG_RUNTIME_DIR"/nc-relays/*.from; do [ -f "$f" ] || continue; '
294 f'[ "$(cat "$f")" = "{name}-program" ] && nc-stream relay-stop "$(basename "$f" .from)"; done) '
295 f'>/dev/null 2>&1 &')
296 subprocess.run(as_user(["sh", "-c", run]), capture_output=True)
297 print(f"{name}: ending (\"thanks for watching\" for 10 s, then its relays stop)")
298
299
300def cmd_relay(name, to):
301 safe(name), safe(to)
302 f = relay_dir() / f"{to}.url"
303 if not f.exists():
304 die(f"no address for {to} yet: nc-stream relay-set {to} (then paste its rtmp address with the key)")
305 # viewers get the program: the stream, or a slate while it is not there
306 cmd_program(name, "on")
307 src = f"{name}-program"
308 cmd_relay_stop(to, quiet=True)
309 # as it comes, no re-encoding (almost free); again every 5 s if either end drops
310 loop = (f'while :; do ffmpeg -hide_banner -loglevel warning -rtsp_transport tcp -i {RTSP}/{src} '
311 f'-c copy -f flv "$(cat {f})"; sleep 5; done')
312 run = f'mkdir -p "$XDG_RUNTIME_DIR/nc-relays"; setsid sh -c \'{loop}\' >/dev/null 2>&1 & echo $! > "$XDG_RUNTIME_DIR/nc-relays/{to}.pid"; echo {src} > "$XDG_RUNTIME_DIR/nc-relays/{to}.from"'
313 r = subprocess.run(as_user(["sh", "-c", run]), capture_output=True, text=True)
314 if r.returncode:
315 die(r.stderr.strip())
316 print(f"relaying {name} to {to} (nc-stream relay-stop {to} ends it)")
317
318
319def cmd_relay_stop(to, quiet=False):
320 safe(to)
321 run = (f'p="$XDG_RUNTIME_DIR/nc-relays/{to}.pid"; [ -f "$p" ] && kill -- -$(cat "$p") 2>/dev/null; '
322 f'rm -f "$p" "$XDG_RUNTIME_DIR/nc-relays/{to}.from"')
323 subprocess.run(as_user(["sh", "-c", run]), capture_output=True)
324 if not quiet:
325 print(f"relay to {to} stopped")
326
327
328def cmd_relays():
329 run = ('for f in "$XDG_RUNTIME_DIR"/nc-relays/*.pid; do [ -f "$f" ] || continue; t=$(basename "$f" .pid); '
330 'kill -0 $(cat "$f") 2>/dev/null && s=running || s=stopped; echo "$t $s $(cat "${f%.pid}.from" 2>/dev/null)"; done')
331 out = subprocess.run(as_user(["sh", "-c", run]), capture_output=True, text=True).stdout.split("\n")
332 kept = sorted(p.stem for p in relay_dir().glob("*.url")) if relay_dir().exists() else []
333 live = {l.split()[0]: l.split()[1:] for l in out if l.strip()}
334 if not kept:
335 print("no relays kept (nc-stream relay-set TO)")
336 for t in kept:
337 st, frm = (live.get(t) + [""])[:2] if t in live else ("idle", "")
338 print(f"{t:<12} {st:<8} {('from ' + frm) if frm else ''}")
339
340
341def ai_settings_path():
342 home = os.path.expanduser("~") if os.geteuid() else f"/home/{desk_user()}"
343 return pathlib.Path(home) / ".config/nc/stream-ai.env"
344
345
346def set_ai(key, value):
347 p = ai_settings_path()
348 p.parent.mkdir(parents=True, exist_ok=True)
349 lines = p.read_text().splitlines() if p.exists() else [
350 "# Live streams and AI (nc-stream ears|eyes). WAKE: listen for \"hey desk\" on this",
351 "# computer. LOOKS: off, all, or stream names: look when the picture changes, at most",
352 "# LOOKS_PER_HOUR, LOOK_GAP seconds apart, and say what is there.",
353 "WAKE=on", "LOOKS=off", "LOOK_GAP=60", "LOOKS_PER_HOUR=30"]
354 out, done = [], False
355 for line in lines:
356 if re.match(rf"\s*{key}\s*=", line):
357 out.append(f"{key}={value}")
358 done = True
359 else:
360 out.append(line)
361 if not done:
362 out.append(f"{key}={value}")
363 p.write_text("\n".join(out) + "\n")
364 if os.geteuid() == 0:
365 subprocess.run(["chown", f"{desk_user()}:{desk_user()}", str(p)], capture_output=True)
366
367
368def get_ai(key, default):
369 p = ai_settings_path()
370 for line in (p.read_text().splitlines() if p.exists() else []):
371 m = re.match(rf"\s*{key}\s*=\s*(.*?)\s*$", line)
372 if m:
373 return m.group(1)
374 return default
375
376
377def cmd_ears(onoff):
378 set_ai("WAKE", "on" if onoff == "on" else "off")
379 print('ears on: "hey desk" is heard on live streams (here; only what follows it goes to speech to text)'
380 if onoff == "on" else "ears off")
381
382
383def cmd_eyes(name, onoff):
384 cur = {n for n in get_ai("LOOKS", "off").split(",") if n and n != "off"}
385 if name == "all":
386 cur = {"all"} if onoff == "on" else set()
387 else:
388 safe(name)
389 cur = (cur | {name}) if onoff == "on" else (cur - {name})
390 set_ai("LOOKS", ",".join(sorted(cur)) or "off")
391 print(f"eyes: {','.join(sorted(cur)) or 'off'} (at most {get_ai('LOOKS_PER_HOUR', '30')} looks an hour, "
392 f"{get_ai('LOOK_GAP', '60')} s apart)")
393
394
395def talkback(path, **form):
396 data = urllib.parse.urlencode(form).encode()
397 try:
398 with urllib.request.urlopen(urllib.request.Request(f"http://127.0.0.1:8078{path}", data=data), timeout=60) as r:
399 return json.loads(r.read())
400 except OSError as e:
401 die(f"talkback is not answering ({e}): is nc-voice running?")
402
403
404def cmd_say(*words):
405 t = talkback("/api/voice", text=" ".join(words))
406 print(f"said on the phone (#{t.get('id')})")
407
408
409def cmd_notice(*words):
410 t = talkback("/api/notice", text=" ".join(words), seconds="5")
411 print(f"shown on the phone (#{t.get('id')})")
412
413
414def main():
415 a = sys.argv[1:]
416 if not a:
417 print(__doc__)
418 return
419 c, rest = a[0], a[1:]
420 table = {
421 "on": (cmd_on, 0), "off": (cmd_off, 0), "key": (cmd_key, 0), "look": (cmd_look, 1),
422 "listen": (cmd_listen, 1), "watch": (cmd_watch, 1), "record": (cmd_record, 2),
423 "recordings": (cmd_recordings, 0), "relay-set": (cmd_relay_set, 1), "relay": (cmd_relay, 2),
424 "relay-stop": (cmd_relay_stop, 1), "relays": (cmd_relays, 0), "ears": (cmd_ears, 1),
425 "eyes": (cmd_eyes, 2), "say": (cmd_say, 1), "notice": (cmd_notice, 1), "program": (cmd_program, 1),
426 "end": (cmd_end, 1),
427 }
428 if c == "list":
429 return cmd_list("--json" in rest)
430 if c not in table or len(rest) < table[c][1]:
431 die(__doc__, 2)
432 try:
433 table[c][0](*rest)
434 except RuntimeError as e:
435 die(str(e))
436 except subprocess.TimeoutExpired:
437 die("the stream did not answer in time")
438
439
440if __name__ == "__main__":
441 main()