oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package protocol defines Calls’s client-neutral signaling contract.
2package protocol
3
4import "encoding/json"
5
6// PeerID is the server-issued identity of one participation: sixteen
7// random bytes in hex, bound to a room and a session, shown in rosters
8// so browsers can address each other. It is not a credential (spec §4.2).
9type PeerID string
10
11// Kind is a participation's authority, re-derived from standing on
12// every tick (spec §2.4).
13type Kind int
14
15// Reason says why a stream ended. Only "buffer" is followed by a
16// resume; the rest are terminal (spec §4.5).
17type Reason string
18
19const (
20 KindGuest Kind = iota
21 KindMember
22)
23
24func (k Kind) String() string {
25 if k == KindMember {
26 return "member"
27 }
28 return "guest"
29}
30
31const (
32 ReasonBuffer Reason = "buffer"
33 ReasonReplaced Reason = "replaced"
34 ReasonStanding Reason = "standing"
35 ReasonExpired Reason = "expired"
36 ReasonRemoved Reason = "removed"
37 ReasonEnded Reason = "ended"
38)
39
40// Upstream types a browser may send.
41const (
42 TypeJoin = "join"
43 TypeAdmit = "admit"
44 TypeLeave = "leave"
45 TypeOffer = "offer"
46 TypeAnswer = "answer"
47 TypeCandidate = "candidate"
48 TypeReset = "reset"
49 TypeChat = "chat" // slice 4
50 TypeShare = "share" // slice 4
51 // TypeMedia goes both ways: a peer reports its own MediaState
52 // upstream, and the hub fans the change out downstream as
53 // {type, from, name, payload: MediaState} (spec §15.19).
54 TypeMedia = "media"
55)
56
57// Downstream types the server emits.
58const (
59 TypeHello = "hello"
60 TypeRoster = "roster"
61 TypePeer = "peer"
62 TypeGone = "gone"
63 TypeKnock = "knock"
64 TypeKnockGone = "knock-gone"
65 TypeFull = "full"
66 // TypeEnded names the ending in §4.3's table, but nothing sends it:
67 // a meeting that ends closes every stream, and a closing stream
68 // carries its reason in lost, so the browser reads the ending as
69 // lost{reason: "ended"} (§4.5). The name is kept because the spec
70 // uses it.
71 TypeEnded = "ended"
72 TypeLost = "lost"
73 TypePair = "pair"
74 TypeGap = "gap"
75 TypeState = "state"
76)
77
78// Message is the one envelope, built by the server. Payload is opaque.
79type Message struct {
80 Type string `json:"type"`
81 From PeerID `json:"from,omitempty"`
82 Name string `json:"name,omitempty"`
83 To PeerID `json:"to,omitempty"`
84 Seq uint64 `json:"seq,omitempty"`
85 Gen uint64 `json:"gen,omitempty"`
86 Payload json.RawMessage `json:"payload,omitempty"`
87 Reason Reason `json:"reason,omitempty"`
88 Roster *Roster `json:"roster,omitempty"`
89 Boot string `json:"boot,omitempty"`
90 Peer PeerID `json:"peer,omitempty"`
91 Waiting bool `json:"waiting,omitempty"`
92 // Report is hello's report token (spec §15.26): the client's authority
93 // to send quality and problem reports, for its participation and ten
94 // minutes after. Sent back in the X-Calls-Report header, never logged.
95 Report string `json:"report,omitempty"`
96}
97
98// Roster is the authoritative snapshot a browser reconciles against
99// (spec §4.3). Waiting is filled only for a member recipient.
100type Roster struct {
101 Peers []RosterPeer `json:"peers"`
102 Waiting []RosterPeer `json:"waiting,omitempty"`
103}
104
105type RosterPeer struct {
106 Peer PeerID `json:"peer"`
107 Name string `json:"name"`
108 Kind string `json:"kind"`
109 Gen uint64 `json:"gen,omitempty"`
110 // Media is the peer's own last report of its microphone and camera,
111 // absent until it has made one — which is every client that predates
112 // §15.19, and every peer for the moment between admission and its
113 // first report. Absent means unknown, and a client shows unknown as
114 // neither muted nor camera-off. Never written through: a snapshot
115 // copies the pointer, so a change replaces it.
116 Media *MediaState `json:"media,omitempty"`
117 // Client is what the peer's participation was opened from (spec
118 // §15.28): "mac 0.4.12", "iphone 0.4.12" or "ipad 0.4.12" for an app
119 // that says (ClientHeader), "native" for an app that does not — every
120 // app from before §15.28, recognised by the Go HTTP client's own user
121 // agent — and absent for a browser, or anything else. Set once, at
122 // open, by the server; a peer cannot set another's.
123 Client string `json:"client,omitempty"`
124}
125
126// MediaState is what a peer says about its own outgoing media (spec
127// §15.19): whether its microphone is muted and whether its camera is
128// off. It is a claim about what the peer is choosing to send, not a
129// measurement of what arrives — the server never sees media.
130type MediaState struct {
131 Muted bool `json:"muted"`
132 CameraOff bool `json:"cameraOff"`
133 // AudioBlocked and NoSoundFrom are the receiving half (spec §15.20):
134 // what this peer can say about the sound reaching it, so that the
135 // person talking can be told. Both carry only bad news and are
136 // omitted when there is none, so an ordinary report is the two-key
137 // object §15.19 defined and a server from before §15.20 still takes
138 // it.
139 //
140 // AudioBlocked: the browser refused to play remote sound (an autoplay
141 // policy wanting a click first).
142 AudioBlocked bool `json:"audioBlocked,omitempty"`
143 // NoSoundFrom: the peers whose audio is arriving here as silence —
144 // connected, reporting themselves unmuted, and flat for several
145 // seconds. At most MaxNoSoundFrom, each a peer id, none repeated.
146 NoSoundFrom []PeerID `json:"noSoundFrom,omitempty"`
147 // ScreenStalled (2026-09-28): the peers whose shared screen has stopped
148 // moving here while they say they are sharing — no frame decoded for
149 // several seconds. The sharer reads its own id in it as a request to
150 // recover (a keyframe first). Bad news only, omitted when there is
151 // none, at most MaxScreenStalled, each a peer id, none repeated.
152 ScreenStalled []PeerID `json:"screenStalled,omitempty"`
153 // Hearing (spec §15.24, 2026-10-01): the peers whose audio this client
154 // is actually receiving and playing — the path from them is
155 // established, so they can be heard the moment they speak. Unlike the
156 // members above it is good news, and its presence is itself news: nil
157 // (the key absent) is a client that does not say, an old one, and an
158 // empty list is a client that says it hears nobody yet. So it is
159 // omitted only when nil (omitzero), and an empty list travels as [].
160 // At most MaxHearing, each a peer id, none repeated.
161 Hearing []PeerID `json:"hearing,omitzero"`
162 // Video (spec §15.30, 2026-10-28): what this client, as a receiver,
163 // asks each sender for — the level of video it can take from them over
164 // its own connection to them, chosen by its automatic watch or by the
165 // person's own "Video quality" setting. Only peers asked for less than
166 // normal are listed; nil (absent) is "normal from everyone", which is
167 // also what a client from before this section means. A sender that
168 // reads its own id here sends that receiver less video, by sender
169 // parameters alone; audio is never reduced. At most MaxVideo entries,
170 // each a peer id with one of the VideoLevels.
171 Video map[PeerID]VideoLevel `json:"video,omitempty"`
172}
173
174// VideoLevel is what a receiver asks one sender for (spec §15.30).
175type VideoLevel string
176
177const (
178 // VideoReduced: the camera at most 640×360, 15 fps, about 350 kbit/s;
179 // a shared screen at a lower frame rate, at full size.
180 VideoReduced VideoLevel = "reduced"
181 // VideoMinimal: the camera at most 320×180, 8 fps, about 120 kbit/s
182 // (paused instead while a screen is shared); a shared screen at a few
183 // frames a second, smaller if it must be.
184 VideoMinimal VideoLevel = "minimal"
185 // VideoAudio: no camera at all to this receiver. A shared screen is
186 // never paused for it: it goes as at minimal.
187 VideoAudio VideoLevel = "audio"
188)
189
190// VideoLevels are the values MediaState.Video may carry. Normal is never
191// sent: it is the absence of an entry.
192var VideoLevels = []VideoLevel{VideoReduced, VideoMinimal, VideoAudio}
193
194// MaxVideo bounds MediaState.Video, for the same reason as MaxNoSoundFrom.
195const MaxVideo = 15
196
197// MaxNoSoundFrom bounds MediaState.NoSoundFrom: a room holds at most
198// sixteen participations (§4.2), so fifteen others is every one there
199// could be.
200const MaxNoSoundFrom = 15
201
202// MaxScreenStalled bounds MediaState.ScreenStalled for the same reason.
203const MaxScreenStalled = 15
204
205// MaxHearing bounds MediaState.Hearing for the same reason.
206const MaxHearing = 15
207
208// Equal compares two reports field by field; the peer lists are compared
209// in order, which is fine because the hub only asks whether a report
210// changed, and a reordered list is a report worth passing on.
211func (m MediaState) Equal(o MediaState) bool {
212 if m.Muted != o.Muted || m.CameraOff != o.CameraOff || m.AudioBlocked != o.AudioBlocked {
213 return false
214 }
215 if (m.Hearing == nil) != (o.Hearing == nil) {
216 return false
217 }
218 return samePeers(m.NoSoundFrom, o.NoSoundFrom) && samePeers(m.ScreenStalled, o.ScreenStalled) &&
219 samePeers(m.Hearing, o.Hearing) && sameVideo(m.Video, o.Video)
220}
221
222// sameVideo compares two §15.30 asks as sets: an empty map and a nil one
223// both say "normal from everyone".
224func sameVideo(a, b map[PeerID]VideoLevel) bool {
225 if len(a) != len(b) {
226 return false
227 }
228 for k, v := range a {
229 if w, ok := b[k]; !ok || w != v {
230 return false
231 }
232 }
233 return true
234}
235
236func samePeers(a, b []PeerID) bool {
237 if len(a) != len(b) {
238 return false
239 }
240 for i := range a {
241 if a[i] != b[i] {
242 return false
243 }
244 }
245 return true
246}
247