Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/network-computer.git
HTTPS
https://gitstable.com/git/askrobots/network-computer.git
SSH
ssh://git@gitstable.com:2222/askrobots/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.

Dan Quellhorst Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353) a1d92f3 1 month, 3 weeks ago History
nc-program · 8.8 KB · 201 lines
1#!/usr/bin/env python3
2"""The program: what viewers get from a live stream, without a gap. The stream
3when it is live; a slate when it is not (starting, reconnecting); a goodbye
4when it ends. Published as NAME-program on the desk (relays send this).
5
6 nc-program NAME run it (nc-stream program NAME on|end starts and ends it)
7
8The stream is decoded, and either its frames or the slate's are encoded again
9(x264, 720p30, 3 Mb/s, a keyframe every 2 s: what YouTube and Twitch ask for)
10and published to rtsp://127.0.0.1:8554/NAME-program. `touch $XDG_RUNTIME_DIR/
11nc-program/NAME.end` (nc-stream end NAME) shows the goodbye for 10 s and stops.
12
13Slates: ~/.config/nc/slates/{starting,reconnecting,ending}.png if there (any
14size: fitted to 1280x720), else made here from plain text.
15"""
16import os, pathlib, subprocess, sys, threading, time, urllib.request, json, collections
17
18W, H, FPS, AR, CH = 1280, 720, 30, 48000, 2
19FRAME = W * H * 3 // 2 # yuv420p
20CHUNK = AR // FPS * CH * 2 # 1/30 s of s16le stereo
21RTSP = "rtsp://127.0.0.1:8554"
22HOME = pathlib.Path(os.path.expanduser("~"))
23RUN = pathlib.Path(os.environ.get("XDG_RUNTIME_DIR", "/tmp")) / "nc-program"
24FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
25FONT2 = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
26
27SLATES = {
28 "starting": ("Starting soon", "The stream begins in a moment."),
29 "reconnecting": ("Reconnecting…", "The camera's connection dropped. Back in a moment."),
30 "ending": ("Thanks for watching", "The stream is ending."),
31}
32
33
34def slate_frame(state):
35 """One yuv420p frame for a state: the person's image, or plain text on a dark gradient."""
36 own = HOME / ".config/nc/slates" / f"{state}.png"
37 fit = f"scale={W}:{H}:force_original_aspect_ratio=decrease,pad={W}:{H}:(ow-iw)/2:(oh-ih)/2:color=0x10141c"
38 if own.exists():
39 cmd = ["ffmpeg", "-loglevel", "error", "-i", str(own), "-vf", fit + ",format=yuv420p",
40 "-frames:v", "1", "-f", "rawvideo", "-"]
41 else:
42 title, sub = SLATES[state]
43 esc = lambda t: t.replace("\\", "\\\\").replace(":", "\\:").replace("'", "’")
44 vf = (f"drawtext=fontfile={FONT}:text='{esc(title)}':fontsize=72:fontcolor=white:"
45 f"x=(w-text_w)/2:y=(h/2)-90,"
46 f"drawtext=fontfile={FONT2}:text='{esc(sub)}':fontsize=32:fontcolor=0xb8c4d6:"
47 f"x=(w-text_w)/2:y=(h/2)+10,format=yuv420p")
48 cmd = ["ffmpeg", "-loglevel", "error", "-f", "lavfi", "-i",
49 f"gradients=s={W}x{H}:c0=0x0e1320:c1=0x23324d:x0=0:y0=0:x1={W}:y1={H}:d=1",
50 "-vf", vf, "-frames:v", "1", "-f", "rawvideo", "-"]
51 out = subprocess.run(cmd, capture_output=True, timeout=30).stdout
52 if len(out) != FRAME:
53 raise RuntimeError(f"slate {state}: {len(out)} bytes, not {FRAME}")
54 return out
55
56
57def live_tracks(name):
58 """The stream's tracks when it is live (e.g. ["H264", "MPEG-4 Audio"]), else None."""
59 try:
60 with urllib.request.urlopen(f"http://127.0.0.1:9997/v3/paths/get/{name}", timeout=2) as r:
61 p = json.loads(r.read())
62 return p.get("tracks") or [] if p.get("ready") else None
63 except (OSError, ValueError):
64 return None
65
66
67class Live:
68 """Decodes the stream into frames and sound; starts again when the stream comes back."""
69 def __init__(self, name):
70 self.name = name
71 self.frame, self.frame_at = None, 0.0
72 self.audio = collections.deque(maxlen=FPS * 2)
73 self.audio_at = 0.0
74 self.stop = False
75 threading.Thread(target=self.run, daemon=True).start()
76
77 def run(self):
78 fifo = RUN / f"{self.name}.live-audio"
79 while not self.stop:
80 tracks = live_tracks(self.name)
81 if tracks is None:
82 time.sleep(1)
83 continue
84 sound = any("Audio" in t or t in ("Opus", "G711", "LPCM", "MPEG-1/2 Audio", "AC-3") for t in tracks)
85 fit = f"scale={W}:{H}:force_original_aspect_ratio=decrease,pad={W}:{H}:(ow-iw)/2:(oh-ih)/2,fps={FPS},format=yuv420p"
86 cmd = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-rtsp_transport", "tcp", "-i", f"{RTSP}/{self.name}",
87 "-map", "0:v", "-vf", fit, "-f", "rawvideo", "pipe:1"]
88 if sound: # a stream without sound gets silence (an output with nothing in it would fail)
89 fifo.unlink(missing_ok=True)
90 os.mkfifo(fifo)
91 cmd += ["-map", "0:a", "-af", "aresample=async=1000", "-ac", str(CH), "-ar", str(AR), "-f", "s16le", "-y", str(fifo)]
92 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL)
93 if sound:
94 threading.Thread(target=self.read_audio, args=(fifo,), daemon=True).start()
95 try:
96 while not self.stop:
97 f = p.stdout.read(FRAME)
98 if len(f) < FRAME:
99 break
100 self.frame, self.frame_at = f, time.monotonic()
101 finally:
102 p.kill()
103 p.wait()
104 time.sleep(1)
105
106 def read_audio(self, fifo):
107 try:
108 with open(fifo, "rb") as a:
109 while not self.stop:
110 c = a.read(CHUNK)
111 if len(c) < CHUNK:
112 return
113 self.audio.append(c)
114 self.audio_at = time.monotonic()
115 except OSError:
116 pass
117
118
119def main():
120 name = sys.argv[1] if len(sys.argv) > 1 else sys.exit(__doc__)
121 RUN.mkdir(parents=True, exist_ok=True)
122 end_flag = RUN / f"{name}.end"
123 end_flag.unlink(missing_ok=True)
124 slates = {s: slate_frame(s) for s in SLATES}
125 silence = bytes(CHUNK)
126 out_audio = RUN / f"{name}.program-audio"
127 out_audio.unlink(missing_ok=True)
128 os.mkfifo(out_audio)
129 enc = subprocess.Popen(
130 ["ffmpeg", "-hide_banner", "-loglevel", "error",
131 "-thread_queue_size", "512", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-s", f"{W}x{H}", "-r", str(FPS), "-i", "pipe:0",
132 "-thread_queue_size", "512", "-f", "s16le", "-ar", str(AR), "-ac", str(CH), "-i", str(out_audio),
133 "-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency", "-b:v", "3000k", "-maxrate", "3500k",
134 "-bufsize", "6000k", "-g", str(FPS * 2), "-keyint_min", str(FPS * 2), "-sc_threshold", "0", "-pix_fmt", "yuv420p",
135 "-c:a", "aac", "-b:a", "128k", "-f", "rtsp", "-rtsp_transport", "tcp", f"{RTSP}/{name}-program"],
136 stdin=subprocess.PIPE)
137 # read-write: opening a pipe only to write waits for its reader, and ffmpeg
138 # reads its video first, which only comes once this is open (a deadlock)
139 audio_out = os.fdopen(os.open(out_audio, os.O_RDWR), "wb", buffering=0)
140 live = Live(name)
141 state, was_live, ending_at = "starting", False, None
142
143 def paced(write):
144 """Calls write() FPS times a second, on its own clock (sound and picture
145 each wait on their own pipe: one full never holds up the other)."""
146 t0, n = time.monotonic(), 0
147 while True:
148 n += 1
149 wait = t0 + n / FPS - time.monotonic()
150 if wait > 0:
151 time.sleep(wait)
152 try:
153 write()
154 except (BrokenPipeError, ValueError, OSError):
155 return
156
157 def sound():
158 fresh = state == "live" and live.audio_at and time.monotonic() - live.audio_at < 1.0 and live.audio
159 audio_out.write(live.audio.popleft() if fresh else silence)
160
161 def picture():
162 enc.stdin.write(live.frame if state == "live" and live.frame else slates[state])
163
164 threading.Thread(target=paced, args=(sound,), daemon=True).start()
165 threading.Thread(target=paced, args=(picture,), daemon=True).start()
166 print(f"program {name}-program: {state}", flush=True)
167 try:
168 while enc.poll() is None:
169 time.sleep(0.2)
170 now = time.monotonic()
171 if ending_at is None and end_flag.exists():
172 ending_at, state = now, "ending"
173 print(f"program {name}: ending", flush=True)
174 if ending_at is not None:
175 if now - ending_at > 10:
176 break
177 continue
178 fresh = live.frame_at and now - live.frame_at < 1.5
179 new = "live" if fresh else ("reconnecting" if was_live else "starting")
180 was_live = was_live or fresh
181 if new != state:
182 state = new
183 print(f"program {name}: {state}", flush=True)
184 finally:
185 live.stop = True
186 try:
187 audio_out.close()
188 enc.stdin.close()
189 except OSError:
190 pass
191 enc.terminate()
192 try:
193 enc.wait(timeout=5)
194 except subprocess.TimeoutExpired:
195 enc.kill()
196 out_audio.unlink(missing_ok=True)
197 end_flag.unlink(missing_ok=True)
198
199
200if __name__ == "__main__":
201 main()