vdm10-python 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- metzler/__init__.py +93 -0
- metzler/__main__.py +18 -0
- metzler/cli.py +262 -0
- metzler/config.py +42 -0
- metzler/discovery.py +525 -0
- metzler/media/__init__.py +1 -0
- metzler/media/g711.py +59 -0
- metzler/media/intercom.py +340 -0
- metzler/media/local.py +219 -0
- metzler/media/rtp.py +106 -0
- metzler/media/rtsp.py +73 -0
- metzler/media/rtsp_puller.py +128 -0
- metzler/media/rtsp_server.py +254 -0
- metzler/media/srtp.py +123 -0
- metzler/provider.py +500 -0
- metzler/py.typed +0 -0
- metzler/sip/__init__.py +1 -0
- metzler/sip/digest.py +45 -0
- metzler/sip/message.py +81 -0
- metzler/sip/sdp.py +89 -0
- metzler/station/__init__.py +6 -0
- metzler/station/bridge.py +284 -0
- metzler/station/call.py +52 -0
- metzler/station/core.py +574 -0
- metzler/station/door_unlock.py +42 -0
- metzler/station/ha_answer.py +92 -0
- metzler/station/ha_media.py +95 -0
- metzler/station/inbound.py +90 -0
- metzler/station/local_answer.py +97 -0
- metzler/station/outbound.py +137 -0
- metzler/station/registration.py +167 -0
- metzler/station/station.py +33 -0
- metzler/station/teardown.py +87 -0
- vdm10_python-0.1.0.dist-info/METADATA +86 -0
- vdm10_python-0.1.0.dist-info/RECORD +38 -0
- vdm10_python-0.1.0.dist-info/WHEEL +4 -0
- vdm10_python-0.1.0.dist-info/entry_points.txt +4 -0
- vdm10_python-0.1.0.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"""Media adapters for intercom (RTSP) calls: to the local headset, or to a SIP peer."""
|
|
2
|
+
|
|
3
|
+
import collections
|
|
4
|
+
import logging
|
|
5
|
+
import random
|
|
6
|
+
import shutil
|
|
7
|
+
import socket
|
|
8
|
+
import struct
|
|
9
|
+
import subprocess
|
|
10
|
+
import threading
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
from metzler.media.g711 import pcm_to_ulaw, ulaw_to_pcm
|
|
14
|
+
from metzler.media.rtp import bind_rtp, is_rtp
|
|
15
|
+
from metzler.media.rtsp_puller import RtspPuller
|
|
16
|
+
|
|
17
|
+
log = logging.getLogger("indoor")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class IntercomMedia:
|
|
21
|
+
"""local-mode media for intercom calls: serves our mic over the shared RtspServer
|
|
22
|
+
and pulls the caller's stream. Owns aplay/arecord/ffplay like LocalMediaBridge."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, o, server, caller_ip):
|
|
25
|
+
self.o, self.server, self.caller_ip = o, server, caller_ip
|
|
26
|
+
self.aplay = self.arecord = self.ffplay = None
|
|
27
|
+
self.puller = None
|
|
28
|
+
self.door_a = self.door_v = None # for log compatibility
|
|
29
|
+
self.rx = {True: 0, False: 0}
|
|
30
|
+
self.tx = 0
|
|
31
|
+
self._lock = threading.Lock()
|
|
32
|
+
self._next = 0.0
|
|
33
|
+
|
|
34
|
+
def _spawn(self, cmd):
|
|
35
|
+
try:
|
|
36
|
+
return subprocess.Popen(
|
|
37
|
+
cmd,
|
|
38
|
+
stdin=subprocess.PIPE if cmd[0] == "aplay" else None,
|
|
39
|
+
stdout=subprocess.PIPE if cmd[0] != "aplay" else None,
|
|
40
|
+
stderr=subprocess.PIPE,
|
|
41
|
+
)
|
|
42
|
+
except FileNotFoundError:
|
|
43
|
+
log.warning("%s not found - disabled", cmd[0])
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
def start(self):
|
|
47
|
+
o = self.o
|
|
48
|
+
if o.local_audio:
|
|
49
|
+
self.aplay = self._spawn(["aplay", "-q", "-D", o.alsa_device, "-f", "S16_LE", "-r", "8000", "-c", "1"])
|
|
50
|
+
if o.local_mic:
|
|
51
|
+
self.arecord = self._spawn(["arecord", "-q", "-D", o.alsa_device, "-f", "S16_LE", "-r", "8000", "-c", "1"])
|
|
52
|
+
if o.video_player and shutil.which("ffplay"):
|
|
53
|
+
self.ffplay = subprocess.Popen(
|
|
54
|
+
["ffplay", "-f", "h264", "-fflags", "nobuffer", "-probesize", "32", "-"],
|
|
55
|
+
stdin=subprocess.PIPE,
|
|
56
|
+
stderr=subprocess.DEVNULL,
|
|
57
|
+
)
|
|
58
|
+
self.server.mic = self.mic_frame
|
|
59
|
+
self.puller = RtspPuller(o, self.caller_ip, self.on_audio, self.on_video)
|
|
60
|
+
self.puller.start()
|
|
61
|
+
|
|
62
|
+
def mic_frame(self):
|
|
63
|
+
"""called by RtspServer.stream - one 20ms ulaw frame"""
|
|
64
|
+
if self.arecord and self.arecord.stdout:
|
|
65
|
+
try:
|
|
66
|
+
pcm = self.arecord.stdout.read(320)
|
|
67
|
+
if pcm and len(pcm) == 320:
|
|
68
|
+
self.tx += 1
|
|
69
|
+
return pcm_to_ulaw(pcm)
|
|
70
|
+
except (OSError, ValueError):
|
|
71
|
+
pass
|
|
72
|
+
return b"\xff" * 160
|
|
73
|
+
|
|
74
|
+
def on_audio(self, ulaw):
|
|
75
|
+
self.rx[True] += 1
|
|
76
|
+
if self.aplay and self.aplay.stdin:
|
|
77
|
+
try:
|
|
78
|
+
self.aplay.stdin.write(ulaw_to_pcm(ulaw))
|
|
79
|
+
except (BrokenPipeError, OSError, ValueError):
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
def on_video(self, frame):
|
|
83
|
+
self.rx[False] += 1
|
|
84
|
+
if self.ffplay and self.ffplay.stdin:
|
|
85
|
+
try:
|
|
86
|
+
self.ffplay.stdin.write(frame[12:])
|
|
87
|
+
except (BrokenPipeError, OSError, ValueError):
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
def stop(self):
|
|
91
|
+
log.info(
|
|
92
|
+
"intercom media stopped: RTSP rx audio=%d video=%d, tx audio=%d", self.rx[True], self.rx[False], self.tx
|
|
93
|
+
)
|
|
94
|
+
if self.puller:
|
|
95
|
+
self.puller.stop()
|
|
96
|
+
if self.server and self.server.mic == self.mic_frame:
|
|
97
|
+
self.server.mic = None
|
|
98
|
+
for p in (self.aplay, self.arecord, self.ffplay):
|
|
99
|
+
if not p:
|
|
100
|
+
continue
|
|
101
|
+
for f in (p.stdin, p.stdout, p.stderr):
|
|
102
|
+
try:
|
|
103
|
+
f and f.close()
|
|
104
|
+
except (OSError, ValueError):
|
|
105
|
+
pass
|
|
106
|
+
p.terminate()
|
|
107
|
+
try:
|
|
108
|
+
p.wait(timeout=1)
|
|
109
|
+
except subprocess.TimeoutExpired:
|
|
110
|
+
p.kill()
|
|
111
|
+
p.wait()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class IntercomVideoLeg:
|
|
115
|
+
"""provider-facing video channel for door calls: repacks each pulled
|
|
116
|
+
H264 RTP frame as PT96 with our seq/ssrc (source timestamp kept) and
|
|
117
|
+
protects it when the provider leg is SRTP. offer_sdp/_media_block use
|
|
118
|
+
the .rtp_port/.srtp attributes like any other media object."""
|
|
119
|
+
|
|
120
|
+
def __init__(self):
|
|
121
|
+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
122
|
+
bind_rtp(self.sock)
|
|
123
|
+
self.rtp_port = self.sock.getsockname()[1]
|
|
124
|
+
self.peer = None # provider video addr from answer SDP
|
|
125
|
+
self.srtp = None # SrtpSession bound via bind_srtp
|
|
126
|
+
self.seq = random.getrandbits(15)
|
|
127
|
+
self.ssrc = random.getrandbits(32)
|
|
128
|
+
self.tx = 0
|
|
129
|
+
|
|
130
|
+
def forward(self, frame):
|
|
131
|
+
"""door's pulled RTP frame -> provider leg (PT96, our seq/ssrc)"""
|
|
132
|
+
if not self.peer or len(frame) < 12:
|
|
133
|
+
return
|
|
134
|
+
pkt = (
|
|
135
|
+
bytes([frame[0], (frame[1] & 0x80) | 96])
|
|
136
|
+
+ struct.pack(">H", self.seq)
|
|
137
|
+
+ frame[4:8]
|
|
138
|
+
+ struct.pack(">I", self.ssrc)
|
|
139
|
+
+ frame[12:]
|
|
140
|
+
)
|
|
141
|
+
self.seq = (self.seq + 1) & 0xFFFF
|
|
142
|
+
if self.srtp:
|
|
143
|
+
pkt = self.srtp.tx.protect(pkt)
|
|
144
|
+
try:
|
|
145
|
+
self.sock.sendto(pkt, self.peer)
|
|
146
|
+
self.tx += 1
|
|
147
|
+
except OSError:
|
|
148
|
+
pass
|
|
149
|
+
|
|
150
|
+
def close(self):
|
|
151
|
+
try:
|
|
152
|
+
self.sock.close()
|
|
153
|
+
except OSError:
|
|
154
|
+
pass
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class SipIntercomMedia:
|
|
158
|
+
"""provider-mode media for intercom calls (header-only SDP): pulls the
|
|
159
|
+
caller's rtsp://<caller>:554 PCMU and forwards it as RTP (PT0) to the SIP
|
|
160
|
+
peer; incoming SIP peer RTP feeds RtspServer.mic so the caller's pull
|
|
161
|
+
hears the peer. Replaces the ALSA headset with a UDP RTP socket.
|
|
162
|
+
With video=True (door calls only) also forwards the pulled H264 stream
|
|
163
|
+
as a PT96 RTP leg to the provider peer."""
|
|
164
|
+
|
|
165
|
+
def __init__(self, o, server, caller_ip, video=False):
|
|
166
|
+
self.o, self.server, self.caller_ip = o, server, caller_ip
|
|
167
|
+
self.video = IntercomVideoLeg() if video else None
|
|
168
|
+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
169
|
+
bind_rtp(self.sock)
|
|
170
|
+
self.rtp_port = self.sock.getsockname()[1]
|
|
171
|
+
self.peer = None # provider RTP (ip, port)
|
|
172
|
+
self.srtp = None # SrtpSession when the leg is encrypted
|
|
173
|
+
self.rxq = collections.deque() # peer PCMU payloads -> caller pull
|
|
174
|
+
self.puller = None
|
|
175
|
+
self.running = True
|
|
176
|
+
self.seq = random.getrandbits(15)
|
|
177
|
+
self.ts = random.getrandbits(32)
|
|
178
|
+
self.ssrc = random.getrandbits(32)
|
|
179
|
+
self.rx = {True: 0, False: 0}
|
|
180
|
+
self.tx = 0
|
|
181
|
+
self.door_a = self.door_v = None # for log compatibility
|
|
182
|
+
self.on_dtmf = None # set by the station: digit sink
|
|
183
|
+
self.dtmf_pt = None # telephone-event PT from the answer
|
|
184
|
+
self._dropped = 0 # diagnostic: non-PT0 packets logged
|
|
185
|
+
self._srtp_ok = False # first authenticated peer packet seen
|
|
186
|
+
self._plain = 0 # pre-auth plain-RTP packets accepted
|
|
187
|
+
self._last_dtmf = -1
|
|
188
|
+
self._last_dtmf_ts = 0.0
|
|
189
|
+
|
|
190
|
+
def start(self):
|
|
191
|
+
self.server.mic = self.mic_frame
|
|
192
|
+
if self.puller: # re-entry (a racing leg won after early
|
|
193
|
+
return # media already started): peers re-latched
|
|
194
|
+
self.puller = RtspPuller(self.o, self.caller_ip, self.on_audio, self.on_video)
|
|
195
|
+
self.puller.start()
|
|
196
|
+
threading.Thread(target=self._recv_loop, daemon=True).start()
|
|
197
|
+
|
|
198
|
+
def set_provider(self, addr):
|
|
199
|
+
self.peer = addr
|
|
200
|
+
|
|
201
|
+
def on_audio(self, ulaw):
|
|
202
|
+
"""caller's PCMU frame (320B/40ms) -> provider RTP"""
|
|
203
|
+
self.rx[True] += 1
|
|
204
|
+
if not self.peer:
|
|
205
|
+
return
|
|
206
|
+
rtp = struct.pack(">BBHII", 0x80, 0, self.seq & 0xFFFF, self.ts & 0xFFFFFFFF, self.ssrc) + ulaw
|
|
207
|
+
if self.srtp:
|
|
208
|
+
rtp = self.srtp.tx.protect(rtp)
|
|
209
|
+
try:
|
|
210
|
+
self.sock.sendto(rtp, self.peer)
|
|
211
|
+
self.seq = (self.seq + 1) & 0xFFFF
|
|
212
|
+
self.ts = (self.ts + len(ulaw)) & 0xFFFFFFFF
|
|
213
|
+
except OSError:
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
def on_video(self, frame):
|
|
217
|
+
self.rx[False] += 1
|
|
218
|
+
if self.video: # door call: forward the door's H264
|
|
219
|
+
self.video.forward(frame)
|
|
220
|
+
|
|
221
|
+
def _recv_loop(self):
|
|
222
|
+
self.sock.settimeout(0.5)
|
|
223
|
+
while self.running:
|
|
224
|
+
try:
|
|
225
|
+
data, addr = self.sock.recvfrom(65535)
|
|
226
|
+
except TimeoutError:
|
|
227
|
+
continue
|
|
228
|
+
except OSError:
|
|
229
|
+
return
|
|
230
|
+
if self.peer is None:
|
|
231
|
+
self.peer = addr # latch (symmetric RTP)
|
|
232
|
+
payload = self._pcmu_payload(data)
|
|
233
|
+
if payload is None:
|
|
234
|
+
continue
|
|
235
|
+
self.rxq.append(payload)
|
|
236
|
+
while len(self.rxq) > 50: # ~1s jitter bound
|
|
237
|
+
self.rxq.popleft()
|
|
238
|
+
|
|
239
|
+
def _pcmu_payload(self, data):
|
|
240
|
+
"""peer packet -> PCMU payload; PT101 telephone-event digits are
|
|
241
|
+
tapped into on_dtmf first. None for RTCP, a bad SRTP tag (after the
|
|
242
|
+
first authenticated packet), or any PT other than PT0 (the only codec
|
|
243
|
+
we offer). Pre-key early media arrives unencrypted and is accepted as
|
|
244
|
+
plain RTP until the first SRTP packet authenticates."""
|
|
245
|
+
if not is_rtp(data):
|
|
246
|
+
return None
|
|
247
|
+
raw = data
|
|
248
|
+
data = self._unwrap(data)
|
|
249
|
+
if data is None or len(data) < 13:
|
|
250
|
+
if self._dropped < 20:
|
|
251
|
+
self._dropped += 1
|
|
252
|
+
log.info("peer RTP dropped: bad SRTP/short, %dB, PT %d", len(raw), raw[1] & 0x7F)
|
|
253
|
+
return None
|
|
254
|
+
pt = data[1] & 0x7F
|
|
255
|
+
if self.dtmf_pt is not None and pt == self.dtmf_pt:
|
|
256
|
+
d = self._dtmf_event(data)
|
|
257
|
+
if d and self.on_dtmf:
|
|
258
|
+
self.on_dtmf(d)
|
|
259
|
+
return None
|
|
260
|
+
if pt != 0:
|
|
261
|
+
if self._dropped < 20:
|
|
262
|
+
self._dropped += 1
|
|
263
|
+
log.info(
|
|
264
|
+
"peer RTP dropped: PT %d, %dB, payload %s (dtmf_pt=%s)",
|
|
265
|
+
pt,
|
|
266
|
+
len(data),
|
|
267
|
+
data[12:20].hex(),
|
|
268
|
+
self.dtmf_pt,
|
|
269
|
+
)
|
|
270
|
+
return None
|
|
271
|
+
return data[12:]
|
|
272
|
+
|
|
273
|
+
def _unwrap(self, data):
|
|
274
|
+
"""SRTP-unprotect. Early media sent before the peer's SRTP engine
|
|
275
|
+
armed arrives unencrypted: pass it through until the first packet
|
|
276
|
+
authenticates, then strict (None on auth failure)"""
|
|
277
|
+
if not (self.srtp and self.srtp.rx):
|
|
278
|
+
return data
|
|
279
|
+
out = self.srtp.rx.unprotect(data)
|
|
280
|
+
if out is not None:
|
|
281
|
+
self._srtp_ok = True
|
|
282
|
+
return out
|
|
283
|
+
if self._srtp_ok:
|
|
284
|
+
return None
|
|
285
|
+
self._plain += 1
|
|
286
|
+
log.debug("peer RTP: plain %dB PT %d accepted pre-SRTP-auth", len(data), data[1] & 0x7F)
|
|
287
|
+
if self._plain == 50:
|
|
288
|
+
log.info("peer keeps sending unencrypted RTP on an SRTP-negotiated leg")
|
|
289
|
+
return data
|
|
290
|
+
|
|
291
|
+
DTMF_CHARS = "0123456789*#ABCD"
|
|
292
|
+
|
|
293
|
+
def _dtmf_event(self, pkt):
|
|
294
|
+
"""RFC4733 telephone-event: emit the digit once on the end-bit
|
|
295
|
+
packet; the end packet's own retransmits (~400ms) are suppressed"""
|
|
296
|
+
ev, flags = pkt[12], pkt[13]
|
|
297
|
+
if not flags & 0x80 or ev >= len(self.DTMF_CHARS): # E bit = top bit of byte 1
|
|
298
|
+
return None
|
|
299
|
+
now = time.monotonic()
|
|
300
|
+
if ev == self._last_dtmf and now - self._last_dtmf_ts < 0.4:
|
|
301
|
+
return None
|
|
302
|
+
self._last_dtmf, self._last_dtmf_ts = ev, now
|
|
303
|
+
return self.DTMF_CHARS[ev]
|
|
304
|
+
|
|
305
|
+
def mic_frame(self):
|
|
306
|
+
"""called by RtspServer.stream - one 160B/20ms ulaw frame of PEER audio.
|
|
307
|
+
Shed to ~8 frames (160ms) first: the queue fills while early media
|
|
308
|
+
arrives before the caller's pull runs, and serving oldest-first would
|
|
309
|
+
lock that backlog in as a constant delay for the whole call (the
|
|
310
|
+
fill and drain rates match once the call is up)."""
|
|
311
|
+
while len(self.rxq) > 8:
|
|
312
|
+
self.rxq.popleft()
|
|
313
|
+
if not self.rxq:
|
|
314
|
+
return b"\xff" * 160
|
|
315
|
+
p = self.rxq.popleft()
|
|
316
|
+
self.tx += 1
|
|
317
|
+
if len(p) <= 160:
|
|
318
|
+
return p.ljust(160, b"\xff")
|
|
319
|
+
self.rxq.appendleft(p[160:])
|
|
320
|
+
return p[:160]
|
|
321
|
+
|
|
322
|
+
def stop(self):
|
|
323
|
+
log.info(
|
|
324
|
+
"sip intercom media stopped: RTSP rx audio=%d video=%d, tx audio=%d video=%d",
|
|
325
|
+
self.rx[True],
|
|
326
|
+
self.rx[False],
|
|
327
|
+
self.tx,
|
|
328
|
+
self.video.tx if self.video else 0,
|
|
329
|
+
)
|
|
330
|
+
if self.video:
|
|
331
|
+
self.video.close()
|
|
332
|
+
self.running = False
|
|
333
|
+
if self.puller:
|
|
334
|
+
self.puller.stop()
|
|
335
|
+
if self.server and self.server.mic == self.mic_frame:
|
|
336
|
+
self.server.mic = None
|
|
337
|
+
try:
|
|
338
|
+
self.sock.close()
|
|
339
|
+
except OSError:
|
|
340
|
+
pass
|
metzler/media/local.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""Local headset mode for door calls: RTP <-> aplay/arecord (+ optional ffplay video)."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import math
|
|
5
|
+
import random
|
|
6
|
+
import select
|
|
7
|
+
import shutil
|
|
8
|
+
import socket
|
|
9
|
+
import struct
|
|
10
|
+
import subprocess
|
|
11
|
+
import threading
|
|
12
|
+
import time
|
|
13
|
+
|
|
14
|
+
from metzler.media.g711 import alaw_to_pcm, pcm_to_ulaw, ulaw_encode_sample, ulaw_to_pcm
|
|
15
|
+
from metzler.media.rtp import bind_rtp
|
|
16
|
+
|
|
17
|
+
log = logging.getLogger("indoor")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _feed(proc, data):
|
|
21
|
+
"""write to a player's stdin; a player that went away is not an error"""
|
|
22
|
+
if proc and proc.stdin:
|
|
23
|
+
try:
|
|
24
|
+
proc.stdin.write(data)
|
|
25
|
+
except (BrokenPipeError, OSError, ValueError):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class LocalMediaBridge(threading.Thread):
|
|
30
|
+
"""Door RTP <-> local speaker/mic via aplay/arecord; optional video pipe to ffplay."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, o, door_a, door_v, fixed=False):
|
|
33
|
+
"""fixed=True: hicore 'talkback' mode - audio RTP on the fixed port (9654) and RTCP on
|
|
34
|
+
port+1, video (PT 96) arrives on the same socket; used when the offer has no m= lines"""
|
|
35
|
+
super().__init__(daemon=True)
|
|
36
|
+
self.o = o
|
|
37
|
+
self.fixed = fixed
|
|
38
|
+
self.audio_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
39
|
+
self.video_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
40
|
+
self.rtcp_sock = None
|
|
41
|
+
if fixed:
|
|
42
|
+
self.audio_sock.bind((o.local_ip, o.talkback_port))
|
|
43
|
+
try:
|
|
44
|
+
self.rtcp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
45
|
+
self.rtcp_sock.bind((o.local_ip, o.talkback_port + 1))
|
|
46
|
+
except OSError:
|
|
47
|
+
self.rtcp_sock = None
|
|
48
|
+
bind_rtp(self.video_sock)
|
|
49
|
+
else:
|
|
50
|
+
bind_rtp(self.audio_sock)
|
|
51
|
+
bind_rtp(self.video_sock)
|
|
52
|
+
self.audio_port = self.audio_sock.getsockname()[1]
|
|
53
|
+
self.video_port = self.video_sock.getsockname()[1]
|
|
54
|
+
self.pt_seen = set()
|
|
55
|
+
self.door_a = door_a # (ip, port) from door SDP
|
|
56
|
+
self.door_v = door_v
|
|
57
|
+
self.running = True
|
|
58
|
+
self.aplay = self.arecord = self.ffplay = None
|
|
59
|
+
self.rx = {True: 0, False: 0} # packets received: audio / video
|
|
60
|
+
self.tx = 0 # audio packets sent
|
|
61
|
+
self.seq = random.getrandbits(16)
|
|
62
|
+
self.ts = random.getrandbits(32)
|
|
63
|
+
self.ssrc = random.getrandbits(32)
|
|
64
|
+
|
|
65
|
+
def _spawn(self, cmd):
|
|
66
|
+
try:
|
|
67
|
+
return subprocess.Popen(
|
|
68
|
+
cmd,
|
|
69
|
+
stdin=subprocess.PIPE if cmd[0] == "aplay" else None,
|
|
70
|
+
stdout=subprocess.PIPE if cmd[0] != "aplay" else None,
|
|
71
|
+
stderr=subprocess.PIPE,
|
|
72
|
+
)
|
|
73
|
+
except FileNotFoundError:
|
|
74
|
+
log.warning("%s not found — %s disabled", cmd[0], "playback" if cmd[0] == "aplay" else "capture")
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
def start(self):
|
|
78
|
+
o = self.o
|
|
79
|
+
dev = o.alsa_device
|
|
80
|
+
if o.local_audio:
|
|
81
|
+
self.aplay = self._spawn(["aplay", "-q", "-D", dev, "-f", "S16_LE", "-r", "8000", "-c", "1"])
|
|
82
|
+
if o.local_mic:
|
|
83
|
+
self.arecord = self._spawn(["arecord", "-q", "-D", dev, "-f", "S16_LE", "-r", "8000", "-c", "1"])
|
|
84
|
+
if o.video_player and shutil.which("ffplay"):
|
|
85
|
+
self.ffplay = subprocess.Popen(
|
|
86
|
+
["ffplay", "-f", "h264", "-fflags", "nobuffer", "-probesize", "32", "-"],
|
|
87
|
+
stdin=subprocess.PIPE,
|
|
88
|
+
stderr=subprocess.DEVNULL,
|
|
89
|
+
)
|
|
90
|
+
threading.Thread(target=self.rx_loop, daemon=True).start()
|
|
91
|
+
threading.Thread(target=self.tx_loop, daemon=True).start()
|
|
92
|
+
time.sleep(0.4)
|
|
93
|
+
for name, p in (("aplay (speaker)", self.aplay), ("arecord (microphone)", self.arecord)):
|
|
94
|
+
if p and p.poll() is not None:
|
|
95
|
+
log.warning(
|
|
96
|
+
"%s exited at once (code %s): %s",
|
|
97
|
+
name,
|
|
98
|
+
p.returncode,
|
|
99
|
+
(p.stderr.read() or b"").decode(errors="replace").strip() or "no error text",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
def rx_loop(self):
|
|
103
|
+
"""door -> speaker (+ video -> ffplay)"""
|
|
104
|
+
while self.running:
|
|
105
|
+
socks = [self.audio_sock, self.video_sock] + ([self.rtcp_sock] if self.rtcp_sock else [])
|
|
106
|
+
try:
|
|
107
|
+
r, _, _ = select.select(socks, [], [], 0.5)
|
|
108
|
+
except (OSError, ValueError):
|
|
109
|
+
return
|
|
110
|
+
for s in r:
|
|
111
|
+
try:
|
|
112
|
+
data, addr = s.recvfrom(65535)
|
|
113
|
+
except OSError:
|
|
114
|
+
return
|
|
115
|
+
if s is not self.rtcp_sock and len(data) >= 13:
|
|
116
|
+
self.on_packet(s, data, addr)
|
|
117
|
+
|
|
118
|
+
def on_packet(self, sock, data, addr):
|
|
119
|
+
"""one RTP packet from the door: classify audio/video, then play it"""
|
|
120
|
+
pt = data[1] & 0x7F
|
|
121
|
+
# fixed (talkback) mode: audio and video share the socket, tell them apart by PT
|
|
122
|
+
is_audio = pt in (0, 2, 8, 9, 98) if self.fixed else sock is self.audio_sock
|
|
123
|
+
self.rx[is_audio] += 1
|
|
124
|
+
if pt not in self.pt_seen:
|
|
125
|
+
self.pt_seen.add(pt)
|
|
126
|
+
log.info(
|
|
127
|
+
"first RTP packet with payload type %d from %s:%d (%d bytes) on the %s socket",
|
|
128
|
+
pt,
|
|
129
|
+
addr[0],
|
|
130
|
+
addr[1],
|
|
131
|
+
len(data),
|
|
132
|
+
"audio" if sock is self.audio_sock else "video",
|
|
133
|
+
)
|
|
134
|
+
if is_audio:
|
|
135
|
+
self._play_audio(pt, data[12:], addr)
|
|
136
|
+
else:
|
|
137
|
+
self._play_video(data[12:], addr)
|
|
138
|
+
|
|
139
|
+
def _play_audio(self, pt, payload, addr):
|
|
140
|
+
if self.door_a is None or (self.fixed and self.rx[True] == 1 and addr != self.door_a):
|
|
141
|
+
if self.door_a:
|
|
142
|
+
log.info("RTP peer is %s:%d (not %s:%d), following it", *addr, *self.door_a)
|
|
143
|
+
self.door_a = addr
|
|
144
|
+
decode = {0: ulaw_to_pcm, 8: alaw_to_pcm}.get(pt)
|
|
145
|
+
if decode is None:
|
|
146
|
+
if self.rx[True] == 1:
|
|
147
|
+
log.warning("audio payload type %d is not decoded (only PCMU/PCMA)", pt)
|
|
148
|
+
return
|
|
149
|
+
_feed(self.aplay, decode(payload))
|
|
150
|
+
|
|
151
|
+
def _play_video(self, payload, addr):
|
|
152
|
+
if self.door_v is None and not self.fixed:
|
|
153
|
+
self.door_v = addr
|
|
154
|
+
_feed(self.ffplay, payload)
|
|
155
|
+
|
|
156
|
+
def _mic_frame(self):
|
|
157
|
+
if self.arecord and self.arecord.stdout:
|
|
158
|
+
try:
|
|
159
|
+
pcm = self.arecord.stdout.read(320) # 160 samples = 20 ms
|
|
160
|
+
if pcm:
|
|
161
|
+
return pcm_to_ulaw(pcm)
|
|
162
|
+
except (OSError, ValueError):
|
|
163
|
+
pass
|
|
164
|
+
if self.o.local_tone:
|
|
165
|
+
f = self.o.local_tone
|
|
166
|
+
return bytes(
|
|
167
|
+
ulaw_encode_sample(int(8000 * math.sin(2 * math.pi * f * (self.ts + n) / 8000))) for n in range(160)
|
|
168
|
+
)
|
|
169
|
+
return b"\xff" * 160 # ulaw silence
|
|
170
|
+
|
|
171
|
+
def tx_loop(self):
|
|
172
|
+
"""mic/tone -> door"""
|
|
173
|
+
while self.running:
|
|
174
|
+
if not self.door_a:
|
|
175
|
+
time.sleep(0.05)
|
|
176
|
+
continue
|
|
177
|
+
payload = self._mic_frame()
|
|
178
|
+
hdr = struct.pack(">BBHII", 0x80, 0, self.seq & 0xFFFF, self.ts & 0xFFFFFFFF, self.ssrc)
|
|
179
|
+
try:
|
|
180
|
+
self.audio_sock.sendto(hdr + payload, self.door_a)
|
|
181
|
+
self.tx += 1
|
|
182
|
+
if self.tx == 1:
|
|
183
|
+
log.info("first RTP audio packet sent to %s:%d", *self.door_a)
|
|
184
|
+
except OSError:
|
|
185
|
+
pass
|
|
186
|
+
self.seq += 1
|
|
187
|
+
self.ts += 160
|
|
188
|
+
time.sleep(0.02)
|
|
189
|
+
|
|
190
|
+
def stop(self):
|
|
191
|
+
self.running = False
|
|
192
|
+
log.info(
|
|
193
|
+
"media stopped: RTP rx audio=%d video=%d, tx audio=%d (peer %s)",
|
|
194
|
+
self.rx[True],
|
|
195
|
+
self.rx[False],
|
|
196
|
+
self.tx,
|
|
197
|
+
self.door_a,
|
|
198
|
+
)
|
|
199
|
+
for p in (self.aplay, self.arecord, self.ffplay):
|
|
200
|
+
if not p:
|
|
201
|
+
continue
|
|
202
|
+
# aplay/arecord restart blocked pipe I/O after SIGTERM: close the pipes first
|
|
203
|
+
# or they linger and keep the headset busy
|
|
204
|
+
for f in (p.stdin, p.stdout, p.stderr):
|
|
205
|
+
try:
|
|
206
|
+
f and f.close()
|
|
207
|
+
except (OSError, ValueError):
|
|
208
|
+
pass
|
|
209
|
+
p.terminate()
|
|
210
|
+
try:
|
|
211
|
+
p.wait(timeout=1)
|
|
212
|
+
except subprocess.TimeoutExpired:
|
|
213
|
+
p.kill()
|
|
214
|
+
p.wait()
|
|
215
|
+
for s in (self.audio_sock, self.video_sock):
|
|
216
|
+
try:
|
|
217
|
+
s.close()
|
|
218
|
+
except OSError:
|
|
219
|
+
pass
|
metzler/media/rtp.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""RTP plumbing: port allocation inside the firewall range, RTP/RTCP detection and
|
|
2
|
+
the two-sided UDP relay used for door calls."""
|
|
3
|
+
|
|
4
|
+
import random
|
|
5
|
+
import select
|
|
6
|
+
import socket
|
|
7
|
+
import threading
|
|
8
|
+
|
|
9
|
+
RTP_RANGE = None # (lo, hi) from --rtp-port-min/max: a fixed range so one firewall rule covers all RTP
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def set_rtp_range(lo, hi):
|
|
13
|
+
global RTP_RANGE
|
|
14
|
+
RTP_RANGE = (lo, hi)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def bind_rtp(sock):
|
|
18
|
+
"""bind an RTP socket inside the configured port range (any port if unset)"""
|
|
19
|
+
if not RTP_RANGE:
|
|
20
|
+
return sock.bind(("0.0.0.0", 0))
|
|
21
|
+
ports = list(range(RTP_RANGE[0], RTP_RANGE[1] + 1))
|
|
22
|
+
random.shuffle(ports)
|
|
23
|
+
for p in ports:
|
|
24
|
+
try:
|
|
25
|
+
return sock.bind(("0.0.0.0", p))
|
|
26
|
+
except OSError:
|
|
27
|
+
continue
|
|
28
|
+
raise OSError(f"no free RTP port in {RTP_RANGE[0]}-{RTP_RANGE[1]}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def is_rtp(pkt):
|
|
32
|
+
"""RTP (not RTCP: PT 72..76 once the marker bit is masked off)"""
|
|
33
|
+
return len(pkt) >= 12 and (pkt[0] >> 6) == 2 and not 72 <= (pkt[1] & 0x7F) <= 76
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class RtpRelay(threading.Thread):
|
|
37
|
+
"""Bidirectional UDP relay between door-side and provider-side endpoints."""
|
|
38
|
+
|
|
39
|
+
def __init__(self):
|
|
40
|
+
super().__init__(daemon=True)
|
|
41
|
+
self.sock_a = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # door side
|
|
42
|
+
self.sock_b = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # pbx side
|
|
43
|
+
bind_rtp(self.sock_a)
|
|
44
|
+
bind_rtp(self.sock_b)
|
|
45
|
+
self.port_a = self.sock_a.getsockname()[1]
|
|
46
|
+
self.port_b = self.sock_b.getsockname()[1]
|
|
47
|
+
self.peer_a = None
|
|
48
|
+
self.peer_b = None
|
|
49
|
+
self.srtp = None # SrtpSession on the provider (b) side
|
|
50
|
+
self.running = True
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def rtp_port(self):
|
|
54
|
+
"""the provider-facing port — what offer_sdp advertises in m= lines"""
|
|
55
|
+
return self.port_b
|
|
56
|
+
|
|
57
|
+
def set_peer(self, a=None, b=None):
|
|
58
|
+
if a:
|
|
59
|
+
self.peer_a = a
|
|
60
|
+
if b:
|
|
61
|
+
self.peer_b = b
|
|
62
|
+
|
|
63
|
+
def _to_b(self, data):
|
|
64
|
+
if self.srtp and is_rtp(data):
|
|
65
|
+
data = self.srtp.tx.protect(data)
|
|
66
|
+
if self.peer_b:
|
|
67
|
+
self.sock_b.sendto(data, self.peer_b)
|
|
68
|
+
|
|
69
|
+
def _to_a(self, data):
|
|
70
|
+
if self.srtp and self.srtp.rx and is_rtp(data):
|
|
71
|
+
data = self.srtp.rx.unprotect(data)
|
|
72
|
+
if data is None:
|
|
73
|
+
return # bad auth tag
|
|
74
|
+
if self.peer_a:
|
|
75
|
+
self.sock_a.sendto(data, self.peer_a)
|
|
76
|
+
|
|
77
|
+
def run(self):
|
|
78
|
+
socks = [self.sock_a, self.sock_b]
|
|
79
|
+
while self.running:
|
|
80
|
+
r, _, _ = select.select(socks, [], [], 0.5)
|
|
81
|
+
for s in r:
|
|
82
|
+
try:
|
|
83
|
+
data, addr = s.recvfrom(65535)
|
|
84
|
+
except OSError:
|
|
85
|
+
return
|
|
86
|
+
self._on_datagram(s, data, addr)
|
|
87
|
+
|
|
88
|
+
def _on_datagram(self, sock, data, addr):
|
|
89
|
+
"""forward to the other side; the first sender on a side becomes its
|
|
90
|
+
peer (symmetric RTP, covers NAT'ed providers)"""
|
|
91
|
+
if sock is self.sock_a:
|
|
92
|
+
if self.peer_a is None:
|
|
93
|
+
self.peer_a = addr
|
|
94
|
+
self._to_b(data)
|
|
95
|
+
else:
|
|
96
|
+
if self.peer_b is None:
|
|
97
|
+
self.peer_b = addr
|
|
98
|
+
self._to_a(data)
|
|
99
|
+
|
|
100
|
+
def stop(self):
|
|
101
|
+
self.running = False
|
|
102
|
+
for s in (self.sock_a, self.sock_b):
|
|
103
|
+
try:
|
|
104
|
+
s.close()
|
|
105
|
+
except OSError:
|
|
106
|
+
pass
|