rtpbench 0.3.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.
Files changed (144) hide show
  1. rtpbench/.dockerignore +4 -0
  2. rtpbench/Dockerfile +12 -0
  3. rtpbench/__init__.py +6 -0
  4. rtpbench/__main__.py +3 -0
  5. rtpbench/agent.py +746 -0
  6. rtpbench/analyze.py +520 -0
  7. rtpbench/bargein.py +272 -0
  8. rtpbench/bench.py +1069 -0
  9. rtpbench/caller.py +781 -0
  10. rtpbench/capture.py +33 -0
  11. rtpbench/carrier/compose.yaml +68 -0
  12. rtpbench/carrier/opensips.cfg +226 -0
  13. rtpbench/carrier/tls.cfg +25 -0
  14. rtpbench/carrier/tls.yaml +9 -0
  15. rtpbench/cases.py +1989 -0
  16. rtpbench/check.py +369 -0
  17. rtpbench/cli.py +616 -0
  18. rtpbench/clock.py +174 -0
  19. rtpbench/compose.yaml +55 -0
  20. rtpbench/corpus/README.md +55 -0
  21. rtpbench/corpus/cough-c1-0.wav +0 -0
  22. rtpbench/corpus/cough-c1-1.wav +0 -0
  23. rtpbench/corpus/cough-c2-0.wav +0 -0
  24. rtpbench/corpus/cough-c2-1.wav +0 -0
  25. rtpbench/corpus/cough-man-0.wav +0 -0
  26. rtpbench/corpus/cough-man-1.wav +0 -0
  27. rtpbench/corpus/cough-short-0.wav +0 -0
  28. rtpbench/corpus/cough-short-1.wav +0 -0
  29. rtpbench/corpus/cough-woman_three_times-0.wav +0 -0
  30. rtpbench/corpus/human-en-dr-0.wav +0 -0
  31. rtpbench/corpus/human-en-dr-1.wav +0 -0
  32. rtpbench/corpus/human-en-sc-0.wav +0 -0
  33. rtpbench/corpus/human-en-sc-1.wav +0 -0
  34. rtpbench/corpus/human-en-sc-2.wav +0 -0
  35. rtpbench/corpus/human-es-f012-0.wav +0 -0
  36. rtpbench/corpus/human-es-f012-1.wav +0 -0
  37. rtpbench/corpus/human-es-f012-2.wav +0 -0
  38. rtpbench/corpus/human-es-f016-0.wav +0 -0
  39. rtpbench/corpus/human-es-f016-1.wav +0 -0
  40. rtpbench/corpus/human-es-f016-2.wav +0 -0
  41. rtpbench/corpus/lines/en/interrupt-00.wav +0 -0
  42. rtpbench/corpus/lines/en/interrupt-01.wav +0 -0
  43. rtpbench/corpus/lines/en/interrupt-02.wav +0 -0
  44. rtpbench/corpus/lines/en/interrupt-03.wav +0 -0
  45. rtpbench/corpus/lines/en/prompt-00.wav +0 -0
  46. rtpbench/corpus/lines/en/prompt-01.wav +0 -0
  47. rtpbench/corpus/lines/en/prompt-02.wav +0 -0
  48. rtpbench/corpus/lines/en/prompt-03.wav +0 -0
  49. rtpbench/corpus/lines/en/prompt-04.wav +0 -0
  50. rtpbench/corpus/lines/en/prompt-05.wav +0 -0
  51. rtpbench/corpus/lines/en/prompt-06.wav +0 -0
  52. rtpbench/corpus/lines/en/prompt-07.wav +0 -0
  53. rtpbench/corpus/lines/en/prompt-08.wav +0 -0
  54. rtpbench/corpus/lines/en/prompt-09.wav +0 -0
  55. rtpbench/corpus/lines/en/prompt.wav +0 -0
  56. rtpbench/corpus/lines/en/transfer.wav +0 -0
  57. rtpbench/corpus/lines/en/turn-00.wav +0 -0
  58. rtpbench/corpus/lines/en/turn-01.wav +0 -0
  59. rtpbench/corpus/lines/en/turn-02.wav +0 -0
  60. rtpbench/corpus/lines/en/turn-03.wav +0 -0
  61. rtpbench/corpus/lines/en/turn-04.wav +0 -0
  62. rtpbench/corpus/lines/en/turn-05.wav +0 -0
  63. rtpbench/corpus/lines/en/turn-06.wav +0 -0
  64. rtpbench/corpus/lines/en/turn-07.wav +0 -0
  65. rtpbench/corpus/lines/en/turn-08.wav +0 -0
  66. rtpbench/corpus/lines/en/turn-09.wav +0 -0
  67. rtpbench/corpus/lines/en/turn-10.wav +0 -0
  68. rtpbench/corpus/lines/en/turn-11.wav +0 -0
  69. rtpbench/corpus/tts-ald-bc.wav +0 -0
  70. rtpbench/corpus/tts-ald-long.wav +0 -0
  71. rtpbench/corpus/tts-ald-s03.wav +0 -0
  72. rtpbench/corpus/tts-ald-s07.wav +0 -0
  73. rtpbench/corpus/tts-ald-s11.wav +0 -0
  74. rtpbench/corpus/tts-cori-bc.wav +0 -0
  75. rtpbench/corpus/tts-cori-long.wav +0 -0
  76. rtpbench/corpus/tts-cori-s01.wav +0 -0
  77. rtpbench/corpus/tts-cori-s05.wav +0 -0
  78. rtpbench/corpus/tts-cori-s09.wav +0 -0
  79. rtpbench/corpus/tts-davefx-bc.wav +0 -0
  80. rtpbench/corpus/tts-davefx-long.wav +0 -0
  81. rtpbench/corpus/tts-davefx-s00.wav +0 -0
  82. rtpbench/corpus/tts-davefx-s04.wav +0 -0
  83. rtpbench/corpus/tts-davefx-s08.wav +0 -0
  84. rtpbench/corpus/tts-libritts0-bc.wav +0 -0
  85. rtpbench/corpus/tts-libritts0-long.wav +0 -0
  86. rtpbench/corpus/tts-libritts0-s02.wav +0 -0
  87. rtpbench/corpus/tts-libritts0-s06.wav +0 -0
  88. rtpbench/corpus/tts-libritts0-s10.wav +0 -0
  89. rtpbench/corpus/tts-libritts3-bc.wav +0 -0
  90. rtpbench/corpus/tts-libritts3-long.wav +0 -0
  91. rtpbench/corpus/tts-libritts3-s03.wav +0 -0
  92. rtpbench/corpus/tts-libritts3-s07.wav +0 -0
  93. rtpbench/corpus/tts-libritts3-s11.wav +0 -0
  94. rtpbench/corpus/tts-ljspeech-bc.wav +0 -0
  95. rtpbench/corpus/tts-ljspeech-long.wav +0 -0
  96. rtpbench/corpus/tts-ljspeech-s00.wav +0 -0
  97. rtpbench/corpus/tts-ljspeech-s04.wav +0 -0
  98. rtpbench/corpus/tts-ljspeech-s08.wav +0 -0
  99. rtpbench/corpus/tts-sharvardf-bc.wav +0 -0
  100. rtpbench/corpus/tts-sharvardf-long.wav +0 -0
  101. rtpbench/corpus/tts-sharvardf-s02.wav +0 -0
  102. rtpbench/corpus/tts-sharvardf-s06.wav +0 -0
  103. rtpbench/corpus/tts-sharvardf-s10.wav +0 -0
  104. rtpbench/corpus/tts-sharvardm-bc.wav +0 -0
  105. rtpbench/corpus/tts-sharvardm-long.wav +0 -0
  106. rtpbench/corpus/tts-sharvardm-s01.wav +0 -0
  107. rtpbench/corpus/tts-sharvardm-s05.wav +0 -0
  108. rtpbench/corpus/tts-sharvardm-s09.wav +0 -0
  109. rtpbench/detect.py +145 -0
  110. rtpbench/g711.py +82 -0
  111. rtpbench/mock/compose.yaml +18 -0
  112. rtpbench/pcap.py +106 -0
  113. rtpbench/progress.py +184 -0
  114. rtpbench/realtime.py +1015 -0
  115. rtpbench/report.py +450 -0
  116. rtpbench/rtp.py +166 -0
  117. rtpbench/sip.py +653 -0
  118. rtpbench/speech.py +100 -0
  119. rtpbench/speechcal.py +367 -0
  120. rtpbench/srtp.py +191 -0
  121. rtpbench/targets/aivc/aivc.ini +25 -0
  122. rtpbench/targets/aivc/compose.yaml +72 -0
  123. rtpbench/targets/aivc/constraints.txt +52 -0
  124. rtpbench/targets/aivc/opensips.cfg +45 -0
  125. rtpbench/targets/ava/ai-agent.yaml +275 -0
  126. rtpbench/targets/ava/ari.conf +7 -0
  127. rtpbench/targets/ava/compose.yaml +128 -0
  128. rtpbench/targets/ava/constraints.txt +75 -0
  129. rtpbench/targets/ava/extensions.conf +10 -0
  130. rtpbench/targets/ava/http.conf +4 -0
  131. rtpbench/targets/ava/pjsip.conf +19 -0
  132. rtpbench/targets/ava/websocket_client.conf +15 -0
  133. rtpbench/targets/livekit/agent.py +111 -0
  134. rtpbench/targets/livekit/compose.yaml +86 -0
  135. rtpbench/targets/livekit/constraints.txt +72 -0
  136. rtpbench/tls.py +284 -0
  137. rtpbench/uri/advertise.yaml +7 -0
  138. rtpbench/uri/network.yaml +14 -0
  139. rtpbench/voice.py +18 -0
  140. rtpbench-0.3.0.dist-info/METADATA +123 -0
  141. rtpbench-0.3.0.dist-info/RECORD +144 -0
  142. rtpbench-0.3.0.dist-info/WHEEL +4 -0
  143. rtpbench-0.3.0.dist-info/entry_points.txt +2 -0
  144. rtpbench-0.3.0.dist-info/licenses/LICENSE +202 -0
rtpbench/report.py ADDED
@@ -0,0 +1,450 @@
1
+ """Static HTML report of a case run: one table per target, then a timeline per call with the
2
+ SIP messages, speech and RTP of both sides on one time axis (all from the caller's capture)."""
3
+
4
+ import json
5
+ from html import escape
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from rtpbench import analyze, cases, clock, sip, speechcal
10
+
11
+ W, LEFT, RIGHT = 1000, 118, 16 # SVG width, label column, right margin
12
+ LANE, AXIS = 26, 28
13
+ RTP_GAP_NS = 100_000_000 # RTP silent this long: a gap in the lane
14
+
15
+ CSS = """
16
+ :root { color-scheme: light dark; --surface: #fcfcfb; --page: #f9f9f7; --ink: #0b0b0b;
17
+ --ink2: #52514e; --muted: #898781; --grid: #e1e0d9; --axis: #c3c2b7;
18
+ --border: rgba(11,11,11,.10); --caller: #2a78d6; --agent: #eb6834;
19
+ --pass: #0ca30c; --fail: #d03b3b; --inconclusive: #fab219; --unsupported: #898781; }
20
+ @media (prefers-color-scheme: dark) { :root { --surface: #1a1a19; --page: #0d0d0d;
21
+ --ink: #f0efec; --ink2: #c3c2b7; --grid: #2c2c2a; --axis: #383835;
22
+ --border: rgba(255,255,255,.10); --caller: #3987e5; --agent: #d95926; } }
23
+ body { margin: 0 auto; max-width: 1100px; padding: 24px 16px 48px; background: var(--page);
24
+ color: var(--ink); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
25
+ h1 { font-size: 20px; margin: 0 0 4px; } h2 { font-size: 17px; margin: 32px 0 8px; }
26
+ h3 { font-size: 15px; margin: 28px 0 6px; } .meta, .note { color: var(--ink2); margin: 0 0 12px; }
27
+ .scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: 6px;
28
+ background: var(--surface); }
29
+ table { border-collapse: collapse; width: 100%; min-width: 640px; }
30
+ th, td { text-align: left; vertical-align: top; padding: 7px 10px;
31
+ border-bottom: 1px solid var(--grid); }
32
+ th { color: var(--ink2); font-weight: 600; font-size: 13px; }
33
+ tr:last-child td { border-bottom: 0; }
34
+ td.t { min-width: 330px; } .n { font-variant-numeric: tabular-nums; white-space: nowrap; }
35
+ .v { white-space: nowrap; }
36
+ .icon { font-weight: 700; } .pass .icon { color: var(--pass); }
37
+ .fail .icon { color: var(--fail); } .inconclusive .icon { color: var(--inconclusive); }
38
+ .unsupported .icon { color: var(--unsupported); }
39
+ a { color: inherit; } svg { display: block; width: 100%; min-width: 720px; height: auto; }
40
+ svg text { font: 11px system-ui, -apple-system, "Segoe UI", sans-serif; fill: var(--ink2); }
41
+ svg .lane { fill: var(--ink2); font-size: 12px; } svg .grid { stroke: var(--grid); }
42
+ svg .tick { fill: var(--muted); } svg .caller { fill: var(--caller); stroke: var(--caller); }
43
+ svg .agent { fill: var(--agent); stroke: var(--agent); }
44
+ svg .hit { fill: transparent; stroke: none; }
45
+ .legend { display: flex; gap: 16px; color: var(--ink2); margin: 8px 0; }
46
+ .legend i { display: inline-block; width: 12px; height: 12px; border-radius: 2px;
47
+ vertical-align: -1px; margin-right: 6px; }
48
+ details { margin: 8px 0 0; } summary { cursor: pointer; color: var(--ink2); }
49
+ @media (max-width: 640px) { /* a phone: tables as wide as their cells; each case a card, its
50
+ cells stacked under their names */
51
+ table { min-width: 0; } table.cases tr:first-child { display: none; }
52
+ table.cases tr { display: block; padding: 6px 0; border-bottom: 1px solid var(--grid); }
53
+ table.cases tr:last-child { border-bottom: 0; }
54
+ table.cases td { display: block; min-width: 0; border: 0; padding: 3px 12px; }
55
+ table.cases td.none { display: none; } table.cases td:first-child { font-weight: 600; }
56
+ table.cases .n { white-space: normal; }
57
+ table.cases td[data-label]::before { content: attr(data-label); display: block;
58
+ color: var(--ink2); font-size: 12px; font-weight: 600; } }
59
+ """
60
+ INDEX_CSS = """
61
+ .m td a { text-decoration: none; } .m td:first-child { white-space: nowrap; }
62
+ .sets { margin-top: 16px; }
63
+ """
64
+ ICON = {"pass": "✓", "fail": "✗", "inconclusive": "?", "unsupported": "⊘"}
65
+
66
+
67
+ def _verdict(v: str) -> str:
68
+ return f'<span class="v {v}"><span class="icon" aria-hidden="true">{ICON[v]}</span> {v}</span>'
69
+
70
+
71
+ def _id(run: str) -> str:
72
+ return run.replace("/", "-")
73
+
74
+
75
+ def _group_verdict(recs: list[dict[str, Any]]) -> str:
76
+ """The verdict, with how many repeats gave it, or the mix when it flips."""
77
+ text = cases.verdicts(recs)
78
+ if text.startswith("flips"):
79
+ icon = '<span class="icon" aria-hidden="true">↯</span>'
80
+ return f'<span class="v inconclusive">{icon} {escape(text)}</span>'
81
+ count = text.removeprefix(recs[0]["verdict"])
82
+ return _verdict(recs[0]["verdict"]) + (
83
+ f'<span class="n">{escape(count)}</span>' if count else ""
84
+ )
85
+
86
+
87
+ def _table(groups: list[list[dict[str, Any]]]) -> str:
88
+ """One profile's cases, repeats grouped."""
89
+ reference = groups[0][0]["behaviour"] is not None
90
+ head = ["case", "agent", "expected"] if reference else ["case"]
91
+ head += ["result", "timings, ms (true value within the error bar)", "failed checks"]
92
+ head += ["measured &minus; truth, ms"] if reference else []
93
+ rows = []
94
+ for recs in groups:
95
+ rec = recs[0]
96
+ cells = [f'<a href="#{_id(rec["run"])}">{escape(rec["case"])}</a>']
97
+ if reference:
98
+ cells += [escape(rec["behaviour"]), cases.expected(rec["behaviour"]) or ""]
99
+ timings = "<br>".join(
100
+ f'{escape(n)}: <span class="n">{escape(v)}</span>' for n, v in cases.timings(recs)
101
+ )
102
+ failed = "<br>".join(escape(f) for f in cases.failures(recs))
103
+ cells += [_group_verdict(recs), timings or "-", failed or "-"]
104
+ if reference:
105
+ cells.append(escape(", ".join(cases.errors(recs))) or "-")
106
+ timing = 4 if reference else 2
107
+ tds = []
108
+ for i, c in enumerate(cells):
109
+ kind = " ".join(k for k, on in (("t", i == timing), ("none", c in ("", "-"))) if on)
110
+ attrs = (f' class="{kind}"' if kind else "") + (f' data-label="{head[i]}"' if i else "")
111
+ tds.append(f"<td{attrs}>{c}</td>") # on a phone each cell shows its column's name
112
+ rows.append("<tr>" + "".join(tds) + "</tr>")
113
+ th = "".join(f"<th>{h}</th>" for h in head)
114
+ return f'<div class="scroll"><table class="cases"><tr>{th}</tr>{"".join(rows)}</table></div>'
115
+
116
+
117
+ def _runs(times: list[int]) -> list[tuple[int, int]]:
118
+ """Packet arrival times as [first, last] runs split at gaps over RTP_GAP_NS."""
119
+ out: list[list[int]] = []
120
+ for t in times:
121
+ if out and t - out[-1][1] <= RTP_GAP_NS:
122
+ out[-1][1] = t
123
+ else:
124
+ out.append([t, t])
125
+ return [(a, b) for a, b in out]
126
+
127
+
128
+ def _said(w: cases.Wire) -> list[tuple[int, int]]:
129
+ """The caller's labelled speech, as captured."""
130
+ pkts = [(t, p) for t, p in w.up if p.pt == w.log.get("pt")]
131
+ if not pkts:
132
+ return []
133
+ st, s0 = analyze.stream(pkts, w.log["pt"]), w.log["seq0"]
134
+ out = []
135
+ for line in w.log.get("said", []):
136
+ spans = [(st.index(s0, a), st.index(s0, b)) for a, b in line["speech"]]
137
+ if edges := speechcal._truth(st, spans):
138
+ out.append(edges)
139
+ return out
140
+
141
+
142
+ def _label(m: sip.Message) -> str:
143
+ return m.first.split()[1] + " " + m.cseq_method if m.status else m.method
144
+
145
+
146
+ def timeline(w: cases.Wire) -> str:
147
+ """SVG lanes: SIP, speech and RTP for each side, on one time axis from the first message."""
148
+ if not w.msgs:
149
+ return '<p class="note">No SIP messages of this call in the capture.</p>'
150
+ audio = [t for t, p in w.down if p.pt == w.log.get("pt")]
151
+ sent = [t for t, p in w.up if p.pt == w.log.get("pt")]
152
+ events = [t for t, p in w.up if p.pt != w.log.get("pt")]
153
+ t0 = w.msgs[0][0]
154
+ end = max([w.msgs[-1][0], *audio[-1:], *sent[-1:]])
155
+ span = max(end - t0, 1_000_000_000)
156
+ plot = W - LEFT - RIGHT
157
+
158
+ def x(t: int) -> float:
159
+ return LEFT + plot * (t - t0) / span
160
+
161
+ lanes = [
162
+ ("caller SIP", "caller"),
163
+ ("caller speech", "caller"),
164
+ ("caller RTP", "caller"),
165
+ ("agent SIP", "agent"),
166
+ ("agent speech", "agent"),
167
+ ("agent RTP", "agent"),
168
+ ]
169
+ height = LANE * len(lanes) + AXIS + 8
170
+ out = [f'<svg viewBox="0 0 {W} {height}" role="img" aria-label="call timeline">']
171
+ step = next(s for s in (0.5, 1, 2, 5, 10, 15, 30, 60) if span / 1e9 / s <= 12)
172
+ k = 0
173
+ while (sec := k * step) <= span / 1e9:
174
+ gx = x(t0 + round(sec * 1e9))
175
+ out.append(f'<line class="grid" x1="{gx:.1f}" x2="{gx:.1f}" y1="0" y2="{LANE * 6}"/>')
176
+ out.append(
177
+ f'<text class="tick" x="{gx:.1f}" y="{LANE * 6 + 16}" text-anchor="middle">'
178
+ f"{sec:g} s</text>"
179
+ )
180
+ k += 1
181
+ for i, (name, _) in enumerate(lanes):
182
+ out.append(f'<text class="lane" x="0" y="{i * LANE + 17}">{name}</text>')
183
+
184
+ def bar(lane: int, side: str, a: int, b: int, h: int, tip: str) -> None:
185
+ y = lane * LANE + (LANE - h) / 2
186
+ x0, x1 = x(a), max(x(b), x(a) + 2)
187
+ out.append(
188
+ f'<rect class="{side}" x="{x0:.1f}" y="{y:.1f}" width="{x1 - x0:.1f}" height="{h}" '
189
+ f'rx="2"><title>{escape(tip)}</title></rect>'
190
+ )
191
+
192
+ def secs(t: int) -> str:
193
+ return f"{(t - t0) / 1e9:.3f} s"
194
+
195
+ for a, b in _said(w):
196
+ bar(1, "caller", a, b, 12, f"caller speech {secs(a)} to {secs(b)} (labelled)")
197
+ for a, b in w.speech:
198
+ bar(4, "agent", a, b, 12, f"agent speech {secs(a)} to {secs(b)} (webrtcvad)")
199
+ for lane, side, times in ((2, "caller", sent), (5, "agent", audio)):
200
+ for a, b in _runs(times):
201
+ bar(lane, side, a, b, 4, f"{side} RTP {secs(a)} to {secs(b)}")
202
+ for a, b in _runs(events):
203
+ bar(2, "caller", a, b, 14, f"telephone-event {secs(a)} to {secs(b)}")
204
+ label_end = {True: 0.0, False: 0.0} # where each SIP lane's last label ends
205
+ for t, by_caller, m in w.msgs:
206
+ lane, side = (0, "caller") if by_caller else (3, "agent")
207
+ px, y = x(t), lane * LANE
208
+ tip = f"{secs(t)} {'→' if by_caller else '←'} {m.first} ({m.get('cseq')})"
209
+ out.append(
210
+ f'<line class="{side}" x1="{px:.1f}" x2="{px:.1f}" y1="{y + 14}" y2="{y + 24}" '
211
+ f'stroke-width="2"/><rect class="hit" x="{px - 4:.1f}" y="{y}" width="8" '
212
+ f'height="{LANE}"><title>{escape(tip)}</title></rect>'
213
+ )
214
+ text = _label(m)
215
+ wide = len(text) * 7.0 # 11px text, generous
216
+ right = px + 3 + wide > W # near the end: put it left of the tick
217
+ lo, hi = (px - 3 - wide, px) if right else (px + 3, px + 3 + wide)
218
+ if lo > label_end[by_caller] + 6: # label only where it fits
219
+ tx, anchor = (px - 3, "end") if right else (px + 3, "start")
220
+ out.append(
221
+ f'<text x="{tx:.1f}" y="{y + 12}" text-anchor="{anchor}">{escape(text)}</text>'
222
+ )
223
+ label_end[by_caller] = hi
224
+ out.append("</svg>")
225
+ return "".join(out)
226
+
227
+
228
+ def _messages(w: cases.Wire) -> str:
229
+ if not w.msgs:
230
+ return ""
231
+ t0 = w.msgs[0][0]
232
+ rows = "".join(
233
+ f'<tr><td class="n">{(t - t0) / 1e9:.3f}</td>'
234
+ f"<td>{'caller → agent' if c else 'agent → caller'}</td><td>{escape(m.first)}</td>"
235
+ f"<td>{escape(m.get('cseq'))}</td></tr>"
236
+ for t, c, m in w.msgs
237
+ )
238
+ head = "<tr><th>s</th><th>direction</th><th>message</th><th>CSeq</th></tr>"
239
+ return (
240
+ f"<details><summary>SIP messages ({len(w.msgs)})</summary>"
241
+ f'<div class="scroll"><table>{head}{rows}</table></div></details>'
242
+ )
243
+
244
+
245
+ def _markdown(md: str) -> str:
246
+ """A pooled summary (paragraphs and pipe tables, as `analyze._table` writes them)."""
247
+ out: list[str] = []
248
+ rows: list[list[str]] = []
249
+
250
+ def flush() -> None:
251
+ if rows:
252
+ th = "".join(f"<th>{escape(c)}</th>" for c in rows[0])
253
+ trs = "".join(
254
+ "<tr>" + "".join(f'<td class="n">{escape(c)}</td>' for c in r) + "</tr>"
255
+ for r in rows[1:]
256
+ )
257
+ out.append(f'<div class="scroll"><table><tr>{th}</tr>{trs}</table></div>')
258
+ rows.clear()
259
+
260
+ for line in md.splitlines():
261
+ if line.startswith("|"):
262
+ cells = [c.strip() for c in line.strip().strip("|").split("|")]
263
+ if not all(set(c) <= set("-: ") for c in cells): # the --- separator
264
+ rows.append(cells)
265
+ else:
266
+ flush()
267
+ if line.strip():
268
+ out.append(f'<p class="note">{escape(line)}</p>')
269
+ flush()
270
+ return "".join(out)
271
+
272
+
273
+ def render(out: Path, sections: list[tuple[str, str]] | None = None) -> str:
274
+ """The report for a results directory written by `rtpbench cases` or `rtpbench target`:
275
+ per profile, the cases with their repeats grouped; `sections` (title, markdown: turn latency
276
+ and barge-in against a target); then a timeline for the first repeat of each case, and for
277
+ every repeat whose verdict differs from it."""
278
+ from rtpbench.bench import PROFILES
279
+
280
+ records = json.loads((out / "results.json").read_text())
281
+ conds = json.loads((out / "conditions.json").read_text())
282
+ target = conds.get("target", "reference agent")
283
+ bars = {cases.Bar(*t["bar"]) for r in records for t in r["timings"]}
284
+ meta = ", ".join(
285
+ f"{k} {str(conds[k]).removeprefix('Python ')}"
286
+ for k in ("date", "image", "commit", "runs", "kernel", "python")
287
+ if k in conds
288
+ )
289
+ by_profile: dict[str, list[list[dict[str, Any]]]] = {}
290
+ for (profile, _, _), recs in cases.groups(records).items():
291
+ by_profile.setdefault(profile, []).append(recs)
292
+ parts = [
293
+ f"<h1>rtpbench conformance cases: {escape(target)}</h1>",
294
+ f'<p class="meta">{escape(meta)}</p>',
295
+ ]
296
+ for profile, groups in by_profile.items():
297
+ netem = PROFILES.get(profile, "")
298
+ parts += [
299
+ f"<h2>{escape(target)}, {escape(profile)}</h2>",
300
+ f'<p class="note">netem per direction: <code>{escape(netem or "none")}</code></p>',
301
+ _table(groups),
302
+ ]
303
+ parts += [
304
+ '<p class="note">True value = measured &minus; error; the error lies within:</p><ul>',
305
+ *(f"<li>[{b.lo:+.1f}, {b.hi:+.1f}] ms: {escape(b.source)}</li>" for b in sorted(bars)),
306
+ "</ul>",
307
+ f'<p class="note">{escape(clock.note(conds.get("clock_steps")))}</p>',
308
+ ]
309
+ for title, md in sections or []:
310
+ parts += [f"<h2>{escape(title)}, {escape(target)}</h2>", _markdown(md)]
311
+ parts += [
312
+ "<h2>Calls</h2>",
313
+ '<div class="legend"><span><i style="background:var(--caller)"></i>caller</span>'
314
+ '<span><i style="background:var(--agent)"></i>agent</span>'
315
+ "<span>tall bars: speech · thin: RTP flowing · ticks: SIP (hover for detail)</span></div>",
316
+ ]
317
+ for groups in by_profile.values():
318
+ for recs in groups:
319
+ for rec in [recs[0], *(r for r in recs[1:] if r["verdict"] != recs[0]["verdict"])]:
320
+ w = cases.load(out / rec["run"], rec["profile"])
321
+ title = " · ".join(
322
+ x
323
+ for x in (rec["case"], rec["behaviour"], rec["profile"], f"run {rec['repeat']}")
324
+ if x
325
+ )
326
+ parts += [
327
+ f'<h3 id="{_id(rec["run"])}">{escape(title)} {_verdict(rec["verdict"])}</h3>',
328
+ f'<div class="scroll">{timeline(w)}</div>',
329
+ _messages(w),
330
+ ]
331
+ body = "\n".join(parts)
332
+ return (
333
+ '<!doctype html><html lang="en"><meta charset="utf-8">'
334
+ '<meta name="viewport" content="width=device-width, initial-scale=1">'
335
+ f"<title>rtpbench cases: {escape(target)}</title><style>{CSS}</style>"
336
+ f"<body>{body}</body></html>\n"
337
+ )
338
+
339
+
340
+ def _cell(href: str, recs: list[dict[str, Any]]) -> str:
341
+ """One case in one set: the reference agent's calls with the expected verdict, or a target's
342
+ verdicts; linked to the case's first timeline."""
343
+ if recs[0]["behaviour"] is None:
344
+ text = _group_verdict(recs)
345
+ else:
346
+ ok = sum(r["verdict"] == cases.expected(r["behaviour"]) for r in recs)
347
+ v = "pass" if ok == len(recs) else "fail"
348
+ icon = f'<span class="icon" aria-hidden="true">{ICON[v]}</span>'
349
+ text = f'<span class="v {v}">{icon} <span class="n">{ok}/{len(recs)}</span></span>'
350
+ return f'<a href="{escape(href)}#{_id(recs[0]["run"])}">{text}</a>'
351
+
352
+
353
+ def index(root: Path, markdown: str | None = None) -> str:
354
+ """A page for every result set under `root` (a `rtpbench cases` or `target` directory): per
355
+ folder, each case's verdicts in each set, linked to its first call's timeline, then each
356
+ set's conditions and files; other runs with a summary.md (calibrations) by that summary.
357
+ `markdown`: where `root`'s Markdown files render (such as the repository's blob URL for it),
358
+ for their links; a static host serves them as plain text."""
359
+
360
+ def href(path: Path) -> str:
361
+ rel = path.relative_to(root).as_posix()
362
+ return f"{markdown.rstrip('/')}/{rel}" if markdown and path.suffix == ".md" else rel
363
+
364
+ sets: dict[Path, list[tuple[Path, dict[str, Any]]]] = {}
365
+ for f in sorted(root.rglob("results.json")):
366
+ conds = json.loads((f.parent / "conditions.json").read_text())
367
+ sets.setdefault(f.parent.parent, []).append((f.parent, conds))
368
+ others: dict[Path, list[Path]] = {}
369
+ for f in sorted(root.rglob("summary.md")):
370
+ if not any(f.is_relative_to(d) for group in sets.values() for d, _ in group):
371
+ others.setdefault(f.parent.parent, []).append(f)
372
+ order = {name: i for i, name in enumerate(cases.CASES)}
373
+ parts = [
374
+ "<h1>rtpbench results</h1>",
375
+ '<p class="meta">Reference columns: calls with the expected verdict over every behaviour '
376
+ "of a case (the good agent passes, each bad one fails, each decline is unsupported). "
377
+ "Target columns: verdicts over every profile and repeat. Each links to the case's first "
378
+ "call.</p>",
379
+ ]
380
+ for folder in sorted(sets.keys() | others.keys()):
381
+ rel = folder.relative_to(root).as_posix()
382
+ group = sorted(sets.get(folder, []), key=lambda s: ("target" in s[1], s[0].name))
383
+ readme = folder / "README.md"
384
+ parts.append(f"<h2>{escape(rel if rel != '.' else root.name)}</h2>")
385
+ if readme.exists():
386
+ parts.append(f'<p class="note"><a href="{escape(href(readme))}">README</a></p>')
387
+ if group:
388
+ by_case: dict[str, dict[Path, list[dict[str, Any]]]] = {}
389
+ for d, _ in group:
390
+ for rec in json.loads((d / "results.json").read_text()):
391
+ by_case.setdefault(rec["case"], {}).setdefault(d, []).append(rec)
392
+ th = "".join(
393
+ f'<th><a href="{escape(href(d / "report.html"))}">{escape(d.name)}</a></th>'
394
+ for d, _ in group
395
+ )
396
+ rows = [
397
+ f"<tr><td>{escape(case)}</td>"
398
+ + "".join(
399
+ f"<td>{_cell(href(d / 'report.html'), by_case[case][d])}</td>"
400
+ if d in by_case[case]
401
+ else "<td>-</td>"
402
+ for d, _ in group
403
+ )
404
+ + "</tr>"
405
+ for case in sorted(by_case, key=lambda c: (order.get(c, len(order)), c))
406
+ ]
407
+ parts.append(
408
+ f'<div class="scroll"><table class="m"><tr><th>case</th>{th}</tr>'
409
+ f"{''.join(rows)}</table></div>"
410
+ )
411
+ info = [
412
+ "<tr>"
413
+ + "".join(
414
+ f"<td>{c}</td>"
415
+ for c in (
416
+ escape(d.name),
417
+ escape(conds.get("target", "reference agent")),
418
+ escape(conds.get("date", "")[:16].replace("T", " ")),
419
+ escape(", ".join(conds.get("profiles", []))), # none with a URI
420
+ escape(str(conds.get("runs", ""))),
421
+ escape(conds.get("image") or conds.get("commit", "")),
422
+ " · ".join(
423
+ f'<a href="{escape(href(d / n))}">{n.split(".")[0]}</a>'
424
+ for n in ("report.html", "summary.md", "conditions.json")
425
+ if (d / n).exists()
426
+ ),
427
+ )
428
+ )
429
+ + "</tr>"
430
+ for d, conds in group
431
+ ]
432
+ head = "".join(
433
+ f"<th>{h}</th>"
434
+ for h in ("set", "agent", "date (UTC)", "profiles", "runs", "code", "files")
435
+ )
436
+ parts.append(
437
+ f'<div class="scroll sets"><table><tr>{head}</tr>{"".join(info)}</table></div>'
438
+ )
439
+ if summaries := others.get(folder):
440
+ links = ", ".join(
441
+ f'<a href="{escape(href(f))}">{escape(f.parent.name)}</a>' for f in summaries
442
+ )
443
+ parts.append(f'<p class="note">Other runs (summary): {links}</p>')
444
+ body = "\n".join(parts)
445
+ return (
446
+ '<!doctype html><html lang="en"><meta charset="utf-8">'
447
+ '<meta name="viewport" content="width=device-width, initial-scale=1">'
448
+ f"<title>rtpbench results</title><style>{CSS}{INDEX_CSS}</style>"
449
+ f"<body>{body}</body></html>\n"
450
+ )
rtpbench/rtp.py ADDED
@@ -0,0 +1,166 @@
1
+ """RTP packets and a 20 ms media clock with kernel receive timestamps."""
2
+
3
+ import socket
4
+ import struct
5
+ import threading
6
+ from collections.abc import Callable
7
+ from dataclasses import dataclass
8
+
9
+ from rtpbench.clock import CLOCK, Clock
10
+ from rtpbench.srtp import Context
11
+
12
+ FRAME = 160 # samples per 20 ms packet at 8 kHz
13
+ PTIME_NS = 20_000_000
14
+ SAMPLE_NS = 125_000
15
+ SO_TIMESTAMPNS = 35 # Linux value; Python does not export it
16
+ _HEADER = struct.Struct("!BBHII")
17
+
18
+
19
+ @dataclass(frozen=True, slots=True)
20
+ class Packet:
21
+ pt: int
22
+ seq: int
23
+ ts: int
24
+ ssrc: int
25
+ marker: bool
26
+ payload: bytes
27
+
28
+
29
+ def pack(pt: int, seq: int, ts: int, ssrc: int, payload: bytes, marker: bool = False) -> bytes:
30
+ return _HEADER.pack(0x80, pt | (0x80 if marker else 0), seq, ts, ssrc) + payload
31
+
32
+
33
+ def parse(data: bytes) -> Packet | None:
34
+ if len(data) < 12 or data[0] >> 6 != 2:
35
+ return None
36
+ b0, b1, seq, ts, ssrc = _HEADER.unpack_from(data)
37
+ start = 12 + 4 * (b0 & 0x0F)
38
+ if b0 & 0x10:
39
+ if len(data) < start + 4:
40
+ return None
41
+ start += 4 + 4 * struct.unpack_from("!H", data, start + 2)[0]
42
+ end = len(data) - (data[-1] if b0 & 0x20 else 0)
43
+ if start > end:
44
+ return None
45
+ return Packet(b1 & 0x7F, seq, ts, ssrc, bool(b1 & 0x80), data[start:end])
46
+
47
+
48
+ @dataclass(frozen=True, slots=True)
49
+ class Frame:
50
+ """A packet whose payload type, timestamp or marker the source sets itself."""
51
+
52
+ pt: int
53
+ payload: bytes
54
+ ts: int
55
+ marker: bool = False
56
+
57
+
58
+ class Sender(threading.Thread):
59
+ """Sends one packet per 20 ms tick on an absolute schedule from `start_ns` on `clock`.
60
+
61
+ `source(n, tick_ns)` returns tick n's payload (timestamp n * 160, marker on the first packet
62
+ after a pause), a full `Frame`, or None to send nothing. Seq counts packets sent (RFC 3550),
63
+ so with no pauses tick n carries seq `seq0 + n`. With `srtp`, packets go out protected.
64
+ """
65
+
66
+ def __init__(
67
+ self,
68
+ sock: socket.socket,
69
+ addr: tuple[str, int],
70
+ pt: int,
71
+ source: Callable[[int, int], bytes | Frame | None],
72
+ start_ns: int,
73
+ seq0: int,
74
+ ssrc: int,
75
+ clock: Clock = CLOCK,
76
+ srtp: Context | None = None,
77
+ ) -> None:
78
+ super().__init__(daemon=True)
79
+ self.sock, self.addr, self.pt, self.source, self.clock = sock, addr, pt, source, clock
80
+ self.srtp = srtp
81
+ self.start_ns, self.seq0, self.ssrc = start_ns, seq0, ssrc
82
+ self.stop = threading.Event()
83
+ self.ts0 = 0 # added to every timestamp
84
+ self.new: tuple[int, int, int] | None = None # switch to (ssrc, seq, ts) at the next tick
85
+ self.switched: int | None = None # the tick it did
86
+ self.send_errors: list[tuple[int, str]] = [] # (tick, error) of packets the host refused
87
+
88
+ def switch(self, ssrc: int, seq: int, ts: int) -> None:
89
+ """From the next packet on, be a new source (RFC 3550: new SSRC, its own random seq and
90
+ timestamp origin): the next packet has `seq`, timestamp `ts` and the marker."""
91
+ self.new = ssrc, seq, ts
92
+
93
+ def run(self) -> None:
94
+ n, sent, paused = 0, 0, True
95
+ while not self.stop.is_set():
96
+ tick = self.start_ns + n * PTIME_NS
97
+ out = self.source(n, tick)
98
+ # SRTP is protected before the tick, so encrypting does not delay the send; a switch
99
+ # set meanwhile (from another thread) has it packed again
100
+ early = None
101
+ if self.srtp and out is not None and self.new is None:
102
+ early = self._packet(out, n, sent, paused)
103
+ self.clock.sleep_until(tick)
104
+ if self.new is not None:
105
+ (self.ssrc, self.seq0, ts), self.new = self.new, None
106
+ self.ts0, sent, paused, self.switched = ts - n * FRAME, 0, True, n
107
+ early = None
108
+ if out is not None:
109
+ data = early or self._packet(out, n, sent, paused)
110
+ try:
111
+ self.sock.sendto(data, self.addr)
112
+ except OSError as e:
113
+ # refused by the host (a conntrack clash when both ends start at once
114
+ # gives EPERM): lost before the wire; its seq is spent, the stream goes on
115
+ self.send_errors.append((n, f"{type(e).__name__}: {e}"))
116
+ sent += 1
117
+ paused = out is None
118
+ n += 1
119
+
120
+ def _packet(self, out: bytes | Frame, n: int, sent: int, paused: bool) -> bytes:
121
+ """Tick n's packet, the `sent`th of this source."""
122
+ if not isinstance(out, Frame):
123
+ out = Frame(self.pt, out, n * FRAME, paused)
124
+ seq = (self.seq0 + sent) & 0xFFFF
125
+ ts = (out.ts + self.ts0) & 0xFFFFFFFF
126
+ packet = pack(out.pt, seq, ts, self.ssrc, out.payload, out.marker)
127
+ # its index counts on from seq0: the rollover counter starts at 0 (RFC 3711 3.3.1)
128
+ return self.srtp.protect(packet, self.seq0 + sent) if self.srtp else packet
129
+
130
+
131
+ class Receiver(threading.Thread):
132
+ """Calls `sink(arrival_ns, packet)` for each RTP packet, stamped by the kernel on arrival
133
+ (CLOCK_REALTIME) and put on `clock`; with `srtp`, each that it opens (it may change)."""
134
+
135
+ def __init__(
136
+ self,
137
+ sock: socket.socket,
138
+ sink: Callable[[int, Packet], None],
139
+ clock: Clock = CLOCK,
140
+ srtp: Context | None = None,
141
+ ) -> None:
142
+ super().__init__(daemon=True)
143
+ sock.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1)
144
+ sock.settimeout(0.2)
145
+ self.sock, self.sink, self.clock, self.srtp = sock, sink, clock, srtp
146
+ self.stop = threading.Event()
147
+
148
+ def run(self) -> None:
149
+ while not self.stop.is_set():
150
+ try:
151
+ data, anc, _flags, _addr = self.sock.recvmsg(2048, 64)
152
+ except TimeoutError:
153
+ continue
154
+ except OSError:
155
+ return
156
+ arrival = self.clock.now()
157
+ for level, kind, raw in anc:
158
+ if level == socket.SOL_SOCKET and kind == SO_TIMESTAMPNS:
159
+ sec, nsec = struct.unpack("qq", raw[:16])
160
+ arrival = self.clock.steady(sec * 1_000_000_000 + nsec)
161
+ if self.srtp:
162
+ if (opened := self.srtp.open(data)) is None:
163
+ continue
164
+ data = opened
165
+ if (pkt := parse(data)) is not None:
166
+ self.sink(arrival, pkt)