oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "fmt"
5 "io"
6 "sort"
7 "strconv"
8 "strings"
9
10 "amadan.net/oficina/calls/protocol"
11)
12
13// The per-peer lines (spec §15.29, 2026-10-28): one `calls: quality peer`
14// line per report per remote peer, flat, so that `carlos logs` prints all
15// of it. This file reads them into what each side said about each other
16// side, and prints three things the head lines could never say: the path
17// each side took and the candidates behind it, and so whose network made a
18// relayed call relayed; each direction's camera and screen, as the sender
19// sent them and as the receiver got them; and the encoders and decoders.
20
21// side is what one person's client said about one remote peer, over every
22// window it reported.
23type side struct {
24 windows, relayWindows int
25 paths map[string]int
26 regions map[string]bool
27 // local is how often the selected pair's local end was the relay:
28 // this side's own leg went through it.
29 localRelay int
30 // The candidates, at their most, and whether a kind of pair ever
31 // worked. measured is false for a client before §15.29.
32 measured bool
33 host, srflx, prflx, relay float64
34 rHost, rSrflx, rPrflx, rRelay float64
35 directOK, localDirectOK, udpOK bool
36 rttMax, jitterMax float64
37 rttAvgSum float64
38 rttAvgN int
39 concealedMax, bufferMax float64
40 out, in map[string]*stream // "Camera", "Screen"
41 // §15.30: seconds this side asked the other for each lower level, and
42 // sent the other less because it asked (reduced, minimal, audio).
43 asked, sent [protocol.VideoLevelCount]float64
44}
45
46// stream is one video stream in one direction, from one side's account.
47type stream struct {
48 windows int
49 sizes map[string]int
50 fpsSum, kbpsSum float64
51 fpsN, kbpsN int
52 freezes, freezeSeconds float64
53 dropped float64
54 limits map[string]int
55 limitedBW, limitedCPU float64
56 coders, codecs, modes map[string]bool
57 stalls, rebuilds, other float64
58}
59
60func newStream() *stream {
61 return &stream{sizes: map[string]int{}, limits: map[string]int{}, coders: map[string]bool{}, codecs: map[string]bool{}, modes: map[string]bool{}}
62}
63
64func (c *call) addPeerLine(l line) {
65 if l.Remote == "" {
66 return
67 }
68 f := l.Fields
69 key := [2]string{l.Peer, l.Remote}
70 s := c.sides[key]
71 if s == nil {
72 s = &side{paths: map[string]int{}, regions: map[string]bool{}, out: map[string]*stream{}, in: map[string]*stream{}}
73 c.sides[key] = s
74 }
75 s.windows++
76 num := func(k string) (float64, bool) {
77 v, err := strconv.ParseFloat(f[k], 64)
78 return v, err == nil
79 }
80 n := func(k string) float64 { v, _ := num(k); return v }
81 if loc, rem := f["pathLocal"], f["pathRemote"]; loc != "" || rem != "" {
82 s.paths[fmt.Sprintf("%s→%s %s", orDash(loc), orDash(rem), orDash(f["pathProtocol"]))]++
83 if loc == "relay" || rem == "relay" {
84 s.relayWindows++
85 }
86 if loc == "relay" {
87 s.localRelay++
88 }
89 }
90 if r := f["relayRegion"]; r != "" {
91 s.regions[r] = true
92 }
93 if _, ok := f["udpOk"]; ok {
94 s.measured = true
95 s.host, s.srflx, s.prflx, s.relay = max(s.host, n("candHost")), max(s.srflx, n("candSrflx")), max(s.prflx, n("candPrflx")), max(s.relay, n("candRelay"))
96 s.rHost, s.rSrflx = max(s.rHost, n("candRemoteHost")), max(s.rSrflx, n("candRemoteSrflx"))
97 s.rPrflx, s.rRelay = max(s.rPrflx, n("candRemotePrflx")), max(s.rRelay, n("candRemoteRelay"))
98 s.directOK = s.directOK || f["directOk"] == "true"
99 s.localDirectOK = s.localDirectOK || f["localDirectOk"] == "true"
100 s.udpOK = s.udpOK || f["udpOk"] == "true"
101 }
102 s.rttMax = max(s.rttMax, n("rttMax"), n("rtt"))
103 s.jitterMax = max(s.jitterMax, n("jitterMax"))
104 if v, ok := num("rttAvg"); ok {
105 s.rttAvgSum += v
106 s.rttAvgN++
107 }
108 for i, name := range [protocol.VideoLevelCount]string{"Reduced", "Minimal", "Audio"} {
109 s.asked[i] += n("asked" + name)
110 s.sent[i] += n("sent" + name)
111 }
112 s.concealedMax = max(s.concealedMax, n("inAudioConcealedPct"))
113 s.bufferMax = max(s.bufferMax, n("inAudioJitterBufferMs"))
114 for _, kind := range []string{"Camera", "Screen"} {
115 for dir, m := range map[string]map[string]*stream{"out": s.out, "in": s.in} {
116 pre := dir + kind
117 present := false
118 for k := range f {
119 if strings.HasPrefix(k, pre) {
120 present = true
121 break
122 }
123 }
124 if !present {
125 continue
126 }
127 st := m[kind]
128 if st == nil {
129 st = newStream()
130 m[kind] = st
131 }
132 st.windows++
133 if w, h := n(pre+"Width"), n(pre+"Height"); w > 0 && h > 0 {
134 st.sizes[fmt.Sprintf("%g×%g", w, h)]++
135 }
136 if v, ok := num(pre + "Fps"); ok {
137 st.fpsSum += v
138 st.fpsN++
139 }
140 if v, ok := num(pre + "Kbps"); ok {
141 st.kbpsSum += v
142 st.kbpsN++
143 }
144 st.freezes += n(pre + "Freezes")
145 st.freezeSeconds += n(pre + "FreezeSeconds")
146 st.dropped += n(pre + "Dropped")
147 if lim := f[pre+"Limit"]; lim != "" {
148 st.limits[lim]++
149 }
150 st.limitedBW += n(pre + "LimitedBandwidthSeconds")
151 st.limitedCPU += n(pre + "LimitedCpuSeconds")
152 for _, k := range []string{pre + "Encoder", pre + "Decoder"} {
153 if v := f[k]; v != "" {
154 st.coders[v] = true
155 }
156 }
157 if v := f[pre+"Codec"]; v != "" {
158 st.codecs[v] = true
159 }
160 if v := f[pre+"Mode"]; v != "" {
161 st.modes[v] = true
162 }
163 st.stalls += n(pre + "Stalls")
164 st.rebuilds += n(pre+"Rebuilds") + n(pre+"Reshares")
165 st.other += n(pre + "Failures")
166 }
167 }
168}
169
170func set(m map[string]bool) string {
171 var out []string
172 for k := range m {
173 out = append(out, k)
174 }
175 sort.Strings(out)
176 return strings.Join(out, "/")
177}
178
179func most(m map[string]int) string {
180 best, n := "", 0
181 for k, v := range m {
182 if v > n || (v == n && k < best) {
183 best, n = k, v
184 }
185 }
186 return best
187}
188
189// dominantLimit is what held the encoder back over the call: by the
190// seconds the clients counted, where they did (§15.29), and otherwise the
191// reason most windows named.
192func (st *stream) dominantLimit() string {
193 switch {
194 case st.limitedBW > 0 || st.limitedCPU > 0:
195 if st.limitedBW >= st.limitedCPU {
196 return fmt.Sprintf("bandwidth (%.0f s; cpu %.0f s)", st.limitedBW, st.limitedCPU)
197 }
198 return fmt.Sprintf("cpu (%.0f s; bandwidth %.0f s)", st.limitedCPU, st.limitedBW)
199 case len(st.limits) > 0:
200 return most(st.limits) + " (most windows)"
201 }
202 return ""
203}
204
205func (st *stream) words(sent bool) string {
206 var parts []string
207 if sz := most(st.sizes); sz != "" {
208 parts = append(parts, sz)
209 }
210 if st.fpsN > 0 {
211 parts = append(parts, fmt.Sprintf("%.1f fps", st.fpsSum/float64(st.fpsN)))
212 }
213 if st.kbpsN > 0 {
214 parts = append(parts, fmt.Sprintf("%.0f kbps", st.kbpsSum/float64(st.kbpsN)))
215 }
216 if sent {
217 if lim := st.dominantLimit(); lim != "" {
218 parts = append(parts, "limited by "+lim)
219 }
220 if m := set(st.modes); m != "" {
221 parts = append(parts, "mode "+m)
222 }
223 if c := set(st.coders); c != "" {
224 parts = append(parts, "encoder "+c)
225 }
226 if st.rebuilds+st.other > 0 {
227 parts = append(parts, fmt.Sprintf("%g rebuilds/shares again, %g gave up", st.rebuilds, st.other))
228 }
229 } else {
230 parts = append(parts, fmt.Sprintf("freezes %g (%.1f s)", st.freezes, st.freezeSeconds))
231 if st.dropped > 0 {
232 parts = append(parts, fmt.Sprintf("dropped %g", st.dropped))
233 }
234 if st.stalls > 0 {
235 parts = append(parts, fmt.Sprintf("stalls %g", st.stalls))
236 }
237 if c := set(st.coders); c != "" {
238 parts = append(parts, "decoder "+c)
239 }
240 }
241 if c := set(st.codecs); c != "" {
242 parts = append(parts, c)
243 }
244 return strings.Join(parts, ", ")
245}
246
247// candidates is a side's own summary in words.
248func (s *side) candidates() string {
249 if !s.measured {
250 return "not reported (a client from before 2026-10-28)"
251 }
252 yes := func(b bool) string {
253 if b {
254 return "yes"
255 }
256 return "no"
257 }
258 return fmt.Sprintf("gathered host %g, srflx %g, relay %g; offered host %g, srflx %g, relay %g; direct pair worked %s, own network reached out %s, UDP %s",
259 s.host, s.srflx, s.relay, s.rHost, s.rSrflx, s.rRelay, yes(s.directOK), yes(s.localDirectOK), yes(s.udpOK))
260}
261
262// whyRelayed is the verdict for a pair, a and b being the two sides'
263// accounts (either may be nil, for a client that sent no peer lines). The
264// strongest evidence first: a side that found no public address of its own
265// cannot have been reached directly; a side whose own leg was the relay is
266// the one that needed it; a pair where both found their addresses and
267// nothing direct worked is a NAT or a firewall that maps each destination
268// apart.
269func whyRelayed(an, bn string, a, b *side) string {
270 noSrflx := func(s *side) bool { return s != nil && s.measured && s.srflx == 0 && s.prflx == 0 }
271 onlyRelay := func(s *side) bool { return s != nil && s.measured && s.host == 0 && s.srflx == 0 && s.relay > 0 }
272 ownLeg := func(s *side) bool { return s != nil && s.localRelay > 0 }
273 var said []string
274 for _, x := range []struct {
275 name string
276 s *side
277 }{{an, a}, {bn, b}} {
278 switch {
279 case onlyRelay(x.s):
280 said = append(said, x.name+" gathered only relay candidates (a relay-only policy, or a network that exposes nothing)")
281 case noSrflx(x.s):
282 said = append(said, x.name+" found no public address (no srflx candidate: UDP to the outside blocked)")
283 case x.s != nil && x.s.measured && !x.s.udpOK:
284 said = append(said, x.name+" had no pair work over UDP")
285 }
286 }
287 switch {
288 case (noSrflx(a) || onlyRelay(a)) && !(noSrflx(b) || onlyRelay(b)):
289 said = append(said, "→ "+an+"'s network forced the relay")
290 case (noSrflx(b) || onlyRelay(b)) && !(noSrflx(a) || onlyRelay(a)):
291 said = append(said, "→ "+bn+"'s network forced the relay")
292 case noSrflx(a) && noSrflx(b):
293 said = append(said, "→ both networks forced it")
294 case ownLeg(a) && !ownLeg(b):
295 said = append(said, "→ "+an+"'s own leg went through the relay: "+an+"'s network is the likelier cause")
296 case ownLeg(b) && !ownLeg(a):
297 said = append(said, "→ "+bn+"'s own leg went through the relay: "+bn+"'s network is the likelier cause")
298 case a != nil && a.measured && b != nil && b.measured && !a.directOK && !b.directOK:
299 said = append(said, "→ both found public addresses and no direct pair worked: a NAT or firewall on one side maps each destination apart")
300 case (a == nil || !a.measured) && (b == nil || !b.measured):
301 said = append(said, "→ unknown: neither side reported its candidates")
302 default:
303 said = append(said, "→ not clear from the candidates")
304 }
305 return strings.Join(said, "; ")
306}
307
308// printSides prints the paths, the relay verdicts and the media, when
309// there are peer lines to print them from.
310func (c *call) printSides(out io.Writer) {
311 if len(c.sides) == 0 {
312 return
313 }
314 keys := make([][2]string, 0, len(c.sides))
315 for k := range c.sides {
316 keys = append(keys, k)
317 }
318 sort.Slice(keys, func(i, j int) bool { return keys[i][0]+keys[i][1] < keys[j][0]+keys[j][1] })
319
320 fmt.Fprintln(out, " Paths (each side's own account)")
321 for _, k := range keys {
322 s := c.sides[k]
323 relay := fmt.Sprintf("relay %.0f%%", 100*float64(s.relayWindows)/float64(s.windows))
324 if r := set(s.regions); r != "" {
325 relay += " via " + r
326 }
327 fmt.Fprintf(out, " %s → %s path %s, %s", short(k[0]), short(k[1]), orDash(most(s.paths)), relay)
328 if s.rttAvgN > 0 {
329 fmt.Fprintf(out, ", RTT avg %.0f ms", s.rttAvgSum/float64(s.rttAvgN))
330 }
331 fmt.Fprintf(out, ", RTT max %.0f ms", s.rttMax)
332 if s.jitterMax > 0 {
333 fmt.Fprintf(out, ", jitter max %.0f ms", s.jitterMax)
334 }
335 if s.concealedMax > 0 || s.bufferMax > 0 {
336 fmt.Fprintf(out, ", audio concealed up to %.1f%%, jitter buffer up to %.0f ms", s.concealedMax, s.bufferMax)
337 }
338 fmt.Fprintf(out, "\n %s's candidates: %s\n", short(k[0]), s.candidates())
339 }
340
341 // Each unordered pair once, when either side saw the relay.
342 var verdicts []string
343 done := map[[2]string]bool{}
344 for _, k := range keys {
345 pair := [2]string{min(k[0], k[1]), max(k[0], k[1])}
346 if done[pair] {
347 continue
348 }
349 done[pair] = true
350 a, b := c.sides[[2]string{pair[0], pair[1]}], c.sides[[2]string{pair[1], pair[0]}]
351 if (a == nil || a.relayWindows == 0) && (b == nil || b.relayWindows == 0) {
352 continue
353 }
354 verdicts = append(verdicts, fmt.Sprintf(" %s – %s %s", short(pair[0]), short(pair[1]), whyRelayed(short(pair[0]), short(pair[1]), a, b)))
355 }
356 if len(verdicts) > 0 {
357 fmt.Fprintln(out, " Why relayed")
358 for _, v := range verdicts {
359 fmt.Fprintln(out, v)
360 }
361 }
362
363 // Media, per direction: the sender's account of what it sent and the
364 // receiver's of what arrived.
365 dirs := map[[2]string]bool{}
366 for _, k := range keys {
367 dirs[k], dirs[[2]string{k[1], k[0]}] = true, true
368 }
369 ordered := make([][2]string, 0, len(dirs))
370 for k := range dirs {
371 ordered = append(ordered, k)
372 }
373 sort.Slice(ordered, func(i, j int) bool { return ordered[i][0]+ordered[i][1] < ordered[j][0]+ordered[j][1] })
374 var media []string
375 for _, k := range ordered {
376 from, to := k[0], k[1]
377 sent := c.sides[[2]string{from, to}]
378 got := c.sides[[2]string{to, from}]
379 for _, kind := range []string{"Camera", "Screen"} {
380 var o, i *stream
381 if sent != nil {
382 o = sent.out[kind]
383 }
384 if got != nil {
385 i = got.in[kind]
386 }
387 if o == nil && i == nil {
388 continue
389 }
390 line := fmt.Sprintf(" %s → %s %s", short(from), short(to), strings.ToLower(kind))
391 if o != nil {
392 line += " sent " + o.words(true)
393 }
394 if i != nil {
395 line += " received " + i.words(false)
396 }
397 media = append(media, line)
398 }
399 }
400 if len(media) > 0 {
401 fmt.Fprintln(out, " Media (sender → receiver)")
402 for _, m := range media {
403 fmt.Fprintln(out, m)
404 }
405 }
406
407 // §15.30: who asked whom for less video, and for how long; and the
408 // sender's own account of sending it.
409 var levels []string
410 for _, k := range keys {
411 s := c.sides[k]
412 if v := videoLevels(s.asked); v != "" {
413 levels = append(levels, fmt.Sprintf(" %s asked %s for %s", short(k[0]), short(k[1]), v))
414 }
415 if v := videoLevels(s.sent); v != "" {
416 levels = append(levels, fmt.Sprintf(" %s sent %s %s because they asked", short(k[0]), short(k[1]), v))
417 }
418 }
419 if len(levels) > 0 {
420 fmt.Fprintln(out, " Video levels")
421 for _, l := range levels {
422 fmt.Fprintln(out, l)
423 }
424 }
425}
426