Debian Codegraph @

doc/twrtp/twrtp-guide.t

raw
1 .\" This document is written in troff language, using -ms macros
2 .\" plus pic and tbl preprocessors. The original ThemWi version
3 .\" of this document was produced on a MicroVAX 4.3BSD system
4 .\" using Quasijarus troff; the present Osmocom-adapted version
5 .\" has been modified to format with groff, an implementation of
6 .\" troff that is available in most GNU+Linux distributions.
7 .nr LL 6.5i
8 .TL
9 Guide to ThemWi RTP endpoint library (Osmocom version)
10 .AU
11 Mychaela N. Falconia
12 .AI
13 Themyscira Wireless
14 .\" Any additional authors editing this document in the future,
15 .\" please add another stanza of this form:
16 .\"
17 .\" .AU
18 .\" Your name
19 .\" .AI
20 .\" Your "institution", e.g., sysmocom GmbH or Osmocom Community etc.
21 .\"
22 .hw hand-over
23 .hw time-stamp
24 .hw trans-port
25 .hw wrap-around
26 .NH 1
27 Introduction
28 .PP
29 This document describes the version of Themyscira Wireless RTP endpoint
30 library (\fBtwrtp\fP) that is integrated into \&\fClibosmo\-netif\fP.
31 This version of \fBtwrtp\fP is a derivative work based on the original
32 author's \&\fCtwrtp\-native\fP version, used by ThemWi network elements
33 in Osmocom+ThemWi hybrid networks, and this version of the manual
34 is a derivative work based on the original \fBtwrtp\fP guide document.
35 .NH 2
36 Principal function
37 .PP
38 ThemWi RTP endpoint library, consisting of \%\fC<osmocom/netif/twrtp.h>\fP
39 and \%\fC<osmocom/netif/twjit.h>\fP blocks in the present version,
40 is intended for interworking between an RTP stream and a fixed timing system
41 such as GSM Um interface TCH or T1/E1 TDM. Such interworking consists of two
42 fundamental elements:
43 .IP \(bu
44 In every fixed time quantum, the interworking element receives a unit of
45 speech or CSData media from the fixed source and emits an RTP packet
46 carrying that quantum of speech or CSData.
47 .IP \(bu
48 In the opposite direction, the fixed timing system requires a quantum of
49 speech or CSData to be fed to it on every tick without fail, yet the
50 interworking element has no control over when RTP packets may arrive
51 from the IP network. This direction of interworking requires a rather
52 complex element called a jitter buffer, an element whose design and
53 configuration always involves some trade-offs and compromises.
54 .NH 2
55 Domain of application
56 .PP
57 The present library is \fBnot\fP intended to be an all-purpose implementation
58 of IETF RFCs 3550 and 3551, supporting all possible RTP use cases as
59 envisioned by IETF. Instead it is intended to support RTP \fIas it is used\fP
60 in these two specific telecom network environments:
61 .IP \(bu
62 3GPP networks that use RTP according to TS\ 26.102 and TS\ 48.103;
63 .IP \(bu
64 The way RTP is used to transport G.711 PSTN traffic across the public
65 Internet in what may be colloquially referred to as IP-PSTN.
66 .LP
67 The two 3GPP specs referenced above prescribe a fixed packetization time
68 of 20\ ms for all codecs on AoIP interface. Furthermore, they stipulate
69 that:
70 .IP \(bu
71 In the case of compressed speech transport, each RTP packet carries
72 exactly one frame of the speech codec in use;
73 .IP \(bu
74 In the case of uncompressed G.711 speech or CSData transport, each RTP
75 packet carries exactly 160 payload octets (20\ ms worth) of what would
76 have been a 64\ kbit/s timeslot in T1/E1 transport.
77 .LP
78 This fixed-quantum property, namely the property that every RTP packet
79 carries exactly one fixed quantum of speech or CSData, where the duration
80 of this quantum is known at connection setup time and cannot suddenly
81 change from one packet to the next, is required by the present ThemWi
82 RTP endpoint library \(em this requirement constitutes a fundamental aspect
83 of its architectural design.
84 .PP
85 An RTP endpoint implementation library that imposes the just-described
86 requirement is sufficient for the purpose of building IP-based GSM networks
87 that follow 3GPP TS\ 48.103 (the requirements of that spec are in agreement
88 with the library constraint), and it is also sufficient for interfacing
89 to IP-PSTN by way of common commercial PSTN-via-SIP connectivity providers.
90 .PP
91 In the case of IP-PSTN, the author of the present library has experience
92 only with North American PSTN-via-SIP connectivity providers. In all
93 of our operational experience so far, these IP-PSTN connectivity providers
94 behave in ways that are fully compatible with the expectations of the
95 present RTP library, as long as the following conditions are met:
96 .IP \(bu
97 No attempt is made to use any codecs other than PCMU or PCMA:
98 don't include any other codecs in the SDP offer, and send only SDP answers
99 that select either PCMU or PCMA out of the received multi-codec offer.
100 .IP \(bu
101 No attempt is made to use any other \&\fCptime\fP besides the most common
102 industry standard of 20\ ms.
103 .LP
104 In all operational experience so far, incoming INVITE SDPs indicate either
105 \&\fCa=ptime:20\fP or \&\fCa=maxptime:20\fP, and when we indicate
106 \&\fCa=ptime:20\fP in all SDPs we send out, the IP-PSTN peer always sends us
107 20\ ms RTP packets, as opposed to some other packetization interval which
108 would break the fixed-quantum model assumed by the present RTP library.
109 .PP
110 However, it needs to be acknowledged that the present library is \fBnot\fP
111 suitable for general-purpose, IETF-style applications outside of
112 ``walled garden'' 3GPP networks or the semi-walled environment of
113 IP-PSTN with ``well-behaved'' entities: there are many behaviors that
114 are perfectly legal per the RFCs, but are not supported by the present
115 library. Having a peer send RTP with a packetization interval that is
116 different from what we asked for via \&\fCptime\fP attribute is one of those
117 behaviors that is allowed by IETF, but not supported by this library.
118 .NH 3
119 Expectation of continuous streaming
120 .PP
121 In addition to the just-described requirement for a fixed packetization
122 interval, the domain of application for \fBtwrtp\fP is subject to one more
123 constraint: our jitter buffer component (\fBtwjit\fP) is designed for
124 environments that implement continuous streaming, and may perform suboptimally
125 in those that do not.
126 .PP
127 Continuous streaming is an operational policy under which an RTP endpoint
128 \fIalways\fP emits an RTP packet in \fIevery\fP 20\ ms (or whatever other
129 packetization interval is used) time window, be it rain or shine, even if
130 it has no data to send because nothing was received on the air interface
131 (DTX pause on the radio link, reception errors, frame stealing) or because
132 E1 TRAU frame decoding failed, etc.
133 Continuous streaming may be implemented by sending an RTP packet with a
134 zero-length payload when the endpoint has nothing else to send in a given
135 quantum time window \(em this method allows any existing RTP payload format
136 standard to be operationally modified for continuous streaming.
137 There also exist Themyscira-defined enhanced RTP payload formats for GSM
138 speech codecs that not only mandate continuous streaming, but additionally
139 convey errored frame bits and the Time Alignment Flag in every 20\ ms frame
140 position, exactly like TRAU-UL frames in the world of TDM-based GSM.
141 .PP
142 The opposite of continuous streaming is the practice of intentional gaps.
143 Under this operational policy, an RTP endpoint may create intentional gaps
144 in the RTP stream it emits, simply by sending no RTP packets at all when
145 it deems that there are no useful data to be transmitted.
146 An intentional gap is distinguished from packet loss in that the sequence
147 number in the RTP header increments by one while the timestamp increments
148 by a greater than normal amount.
149 Unfortunately for \fBtwjit\fP, intentional gaps in RTP were the design intent
150 of IETF.
151 Even more unfortunately, this IETF-ism has been canonized
152 by 3GPP in TS\ 26.102 and TS\ 48.103 \(em hence those operators who prefer a
153 continuous streaming model now have to explicitly deviate from 3GPP
154 specifications.
155 .PP
156 In an Osmocom GSM network, 3GPP-compliant operation with intentional gaps
157 is the default \(em however, the operator can switch to continuous streaming
158 model
159 by setting \%\fCrtp\ continuous\-streaming\fP in OsmoBTS vty configuration.
160 .PP
161 Fortunately for \fBtwjit\fP, however, the situation is better in the world
162 of IP-PSTN, the other RTP environment for which the present library was
163 designed.
164 At least on North American IP-PSTN and at least when uncompressed PCMU or
165 PCMA codecs are used, all PSTN-via-SIP connectivity providers in our
166 operational experience so far always emit perfectly continuous RTP streams,
167 without any intentional gaps.
168 .PP
169 If an application uses \fBtwrtp\fP with \fBtwjit\fP to receive an RTP stream
170 that incurs intentional gaps, the resulting performance may be acceptable
171 or unacceptable depending on additional factors.
172 The following behaviors will occur in standard \fBtwjit\fP operation,
173 without underrun extension feature:
174 .IP \(bu
175 If RTP gaps are incurred only during frame erasure events (radio
176 errors or FACCH stealing) without DTX, the resulting \fBtwjit\fP performance
177 will most likely still be acceptable for speech applications.
178 All transmitted speech frames will still be delivered to the receiver,
179 but the frame erasure gap may lengthen or shorten depending on exact jitter
180 buffer conditions at the time of the intentional gap in the Tx stream \(em
181 in other words, a phase shift may be incurred.
182 .IP \(bu
183 If RTP stream gaps are enabled in conjunction with DTX, \fBtwjit\fP will not
184 be able to receive such a stream according to common expectations.
185 When RTP stream gaps are used together with DTX, the stream will typically
186 feature occasional single packets of comfort noise update, sent every
187 160, 240 or 480 ms depending on the codec, surrounded by gaps.
188 When \fBtwjit\fP receives such a stream and the flow-starting fill level
189 is set to 2 or greater (the default and usually necessary configuration),
190 all of these ``isolated island'' comfort noise update packets will be dropped
191 \(em a behavior counter to the way DTX is expected to work.
192 .LP
193 The present Osmocom-integrated version of \fBtwjit\fP includes an additional
194 feature designed to improve operation in the presence of intentional gaps:
195 it is the underrun extension feature described in \(sc2.3.5.1.
196 When this feature is enabled and configured correctly (matching the maximum
197 allowed length of an intentional gap per codec DTX rules), the resulting
198 system behavior becomes almost as good as it would be in the case of
199 continuous streaming: all transmitted RTP packets will be delivered
200 to the receiving application in the correct stream position, in the case of
201 intentional gaps that occur in \%\fBFLOWING\fP state.
202 However, if the transmitted RTP stream is in the state of DTX with
203 intentional gaps (only occasional ``isolated island'' comfort noise update
204 packets are emitted) when \fBtwjit\fP has to perform flow acquisition,
205 those comfort noise update packets will still be dropped.
206 .PP
207 The take-away is that \%\fCrtp\ continuous\-streaming\fP is still the best
208 mode of operation for networks that use \fBtwjit\fP on the receiving end
209 of RTP streams, especially if DTX is enabled.
210 Continuous streaming can also be seen as improved layer separation:
211 with \%\fCrtp\ continuous\-streaming\fP, RTP stream conditions that matter
212 to a receiving jitter buffer become completely independent and decoupled
213 from whatever conditions occur in radio and speech codec layers.
214 .NH 2
215 Configurable quantum duration and time scale
216 .LP
217 For every RTP stream it handles, the library needs to know two key
218 parameters:
219 .IP \(bu
220 The scale or ``clock rate'' used for RTP timestamps, i.e., how many
221 timestamp units equal one millisecond of physical time;
222 .IP \(bu
223 The ``quantum'' duration in milliseconds.
224 .PP
225 \fBQuantum\fP is the term used in this RTP endpoint library for the unit
226 of speech or CSData carried in one RTP packet. In Kantian philosophy terms,
227 a quantum of speech or CSData is the thing-in-itself (a single codec frame,
228 or a contiguous chunk of 160 PCM samples grabbed from an ISDN B channel),
229 whereas the RTP packet that carries said quantum is one particular transport
230 representation of that thing-in-itself.
231 .PP
232 In most applications of this library (all 3GPP codecs other than AMR-WB,
233 and all IP-PSTN applications in our experience so far), the time scale
234 is 8000 timestamp units per second (or 8 per millisecond, as it appears
235 in the actual APIs) and the time duration of a single quantum is 20\ ms,
236 hence one quantum equals 160 timestamp units.
237 Both parameters (RTP timestamp clock rate in kHz and the number of ms
238 per quantum) are configurable at the time of endpoint creation, allowing
239 RTP endpoints for AMR-WB, or perhaps G.711 or CSData applications with
240 different packetization times \(em but they cannot be changed later in
241 the lifetime of an allocated endpoint.
242 .PP
243 For ease of exposition, the rest of this document will assume that
244 one quantum equals 20\ ms in time or 160 RTP timestamp units. If these
245 numbers are different in your application, substitute accordingly.
246 .NH 1
247 Jitter buffer model
248 .PP
249 In the interworking direction from incoming RTP to the fixed timing system,
250 the latter will poll the RTP endpoint (or more precisely, the jitter buffer
251 portion thereof) for a quantum of media every 20\ ms, whenever that quantum
252 is required for transmission on GSM Um TCH or for TDM output etc.
253 The job of the jitter buffer is to match previously received RTP packets
254 to these fixed-timing output polls, while striving to meet these two
255 conflicting goals:
256 .IP \(bu
257 Any time that elapses between an RTP packet being received and its
258 payload being passed as a quantum to the fixed timing system constitutes
259 added latency \(em which needs to be minimized.
260 .IP \(bu
261 IP-based transport always involves some jitter: the time delta between
262 the receipt of one RTP packet and the arrival of its successor is very
263 unlikely to be exactly equal to 20\ ms every time. This jitter may be
264 already present in the RTP stream from its source if that source is
265 an IP-native BTS that does not pass through E1 Abis and thus exposes
266 the inherent jitter of GSM TDMA multiframe structure, but even if
267 the source is perfectly timed, some jitter will still be seen on the
268 receiving end. Depending on the actual amount of jitter seen in a
269 given deployment, it may be necessary to introduce some latency-adding
270 buffering in the receiving RTP endpoint \(em otherwise the function of
271 interworking to the fixed timing system at the destination will perform
272 poorly, as will be seen in the ensuing sections.
273 .LP
274 This chapter covers the design and operation of \fBtwjit\fP,
275 the jitter buffer component of ThemWi RTP endpoint library.
276 .NH 2
277 Flows and handovers
278 .LP
279 In \fBtwjit\fP terminology, a single RTP flow is a portion (or the whole)
280 of an RTP stream that exhibits the following two key properties:
281 .IP \(bu
282 All packets have the same SSRC;
283 .IP \(bu
284 The RTP timestamp increment from each packet to the next always equals
285 the fixed quantum duration expressed in timestamp units, i.e., 160
286 in most practical applications.
287 .LP
288 A handover in \fBtwjit\fP terminology is a point in the incoming RTP stream
289 at which either of the following events occurs:
290 .IP \(bu
291 An SSRC change is seen;
292 .IP \(bu
293 The RTP timestamp advances by an increment that is not an integral multiple
294 of the expected fixed quantum duration. In contrast, if an RTP timestamp
295 increment is seen that \fIis\fP an integral multiple of the quantum, but that
296 integral multiple is more than one, and there is enough buffering in the
297 system such that this event is seen before the jitter buffer underruns,
298 such events are \fBnot\fP treated as handovers: instead it is assumed to be
299 an occurrence of either packet loss or reordering.
300 .LP
301 A handover in \fBtwjit\fP is thus a transition from one flow to the next.
302 This term was adopted because such transitions are expected to occur
303 when an RTP stream belonging to a single call switches from one BSS endpoint
304 to another (in the same BSS or in a different one) upon radio handover events
305 in GSM and other cellular networks, but handovers in \fBtwjit\fP sense can also
306 occur in other applications that aren't GSM. For example, if an IP-PSTN peer
307 we are conversing with suddenly decides, for its own reasons known only to
308 itself, to change its SSRC or jump its RTP output timescale, our \fBtwjit\fP
309 instance will treat that event as a handover.
310 .NH 2
311 Examples of flows
312 .PP
313 The following drawing depicts the best case scenario of a TDM-native speech or
314 CSData stream being transported across an IP network in RTP:
315 .KS
316 .PS
317 # each time line in these drawings is 4i long
318 # let's represent each 20 ms interval as 0.4i in the drawing,
319 # i.e., each 1 ms is 0.020i long
320
321 Tx_line: line -> right 4i
322 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
323 "time" at Tx_line.end + 0.1,0 ljust
324
325 vdist = 0.8i
326 move to Tx_line - 0,vdist
327 Rx_line: line -> right 4i
328 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
329 "time" at Rx_line.end + 0.1,0 ljust
330 for x = 0.2i to 3.8i by 0.4i do {
331 line from Rx_line + x,0 down 0.1i
332 }
333
334 for x = 0.2i to 3.4i by 0.4i do {
335 arrow from Tx_line + x,0 down vdist right 0.3i
336 }
337 .PE
338 .ce
339 Figure 1: Ideal case
340 .KE
341 .PP
342 In the above figure and other similar drawings that follow, each
343 down-and-forward arrow represents an RTP packet: the beginning of each arrow
344 on the RTP Tx line is the point in time when that RTP packet is emitted
345 by the source endpoint, and the landing point of each arrow on the RTP Rx
346 line is the time point when the same packet is received at the destination
347 endpoint. The forward horizontal movement of each arrow in the figure is
348 the flight time of the corresponding RTP packet through the IP network.
349 Tick marks below the RTP Rx time axis represent fixed points in time
350 when the destination application polls its \fBtwjit\fP buffer because
351 the destination fixed timing system (GSM Um TCH, T1/E1 etc) requires a new
352 quantum of media.
353 .PP
354 Figure\ 1 depicts the ideal scenario: the source endpoint emits RTP packets
355 in a perfect 20\ ms cadence without built-in jitter, the flight time through
356 the IP network also remains constant from each packet to the next (no jitter
357 introduced by IP transport), and these packets arrive in a perfect cadence
358 at the receiving endpoint, exactly one packet before each \fBtwjit\fP polling
359 instant.
360 Let us now consider a more realistic scenario:
361 .KS
362 .PS
363 Tx_line: line -> right 4i
364 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
365 "time" at Tx_line.end + 0.1,0 ljust
366
367 vdist = 0.8i
368 move to Tx_line - 0,vdist
369 Rx_line: line -> right 4i
370 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
371 "time" at Rx_line.end + 0.1,0 ljust
372 for x = 0.2i to 3.8i by 0.4i do {
373 line from Rx_line + x,0 down 0.1i
374 }
375
376 Tx0: Tx_line + 0.04i,0
377
378 Land0: Rx_line + (0.04i + 0.02i * 26.0),0
379 Land1: Land0 + (0.02i * 19.992),0
380 Land2: Land1 + (0.02i * 20.522),0
381 Land3: Land2 + (0.02i * 19.509),0
382 Land4: Land3 + (0.02i * 20.211),0
383 Land5: Land4 + (0.02i * 19.741),0
384 Land6: Land5 + (0.02i * 25.245),0
385 Land7: Land6 + (0.02i * 14.776),0
386 Land8: Land7 + (0.02i * 20.007),0
387
388 arrow from Tx0 to Land0
389 arrow from Tx0 + (0.02i * 20),0 to Land1
390 arrow from Tx0 + (0.02i * 40),0 to Land2
391 arrow from Tx0 + (0.02i * 60),0 to Land3
392 arrow from Tx0 + (0.02i * 80),0 to Land4
393 arrow from Tx0 + (0.02i * 100),0 to Land5
394 arrow from Tx0 + (0.02i * 120),0 to Land6
395 arrow from Tx0 + (0.02i * 140),0 to Land7
396 arrow from Tx0 + (0.02i * 160),0 to Land8
397
398 "seq # 0x0630" at 1/2 <Tx_line.start, Rx_line.start> + 0.23i,0 rjust
399 "seq # 0x0638" at 1/2 <Tx_line.end, Rx_line.end> - 0.45i,0 ljust
400 .PE
401 .ce
402 Figure 2: IP-PSTN realistic scenario, good Internet connection
403 .KE
404 .PP
405 Figure\ 2 above is based on an actual IP-PSTN test call that was made on
406 2024-05-14 from the author's interconnection point with BulkVS to a phone
407 number on T-Mobile USA (2G). The figure was drawn using these time-of-arrival
408 delta numbers from the pcap of that call:
409 .TS
410 box center;
411 c|c|c
412 n|n|n.
413 From seq # To seq # ToA delta (ms)
414 _
415 0x0630 0x0631 19.992
416 0x0631 0x0632 20.522
417 0x0632 0x0633 19.509
418 0x0633 0x0634 20.211
419 0x0634 0x0635 19.741
420 0x0635 0x0636 25.245
421 0x0636 0x0637 14.776
422 0x0637 0x0638 20.007
423 .TE
424 .PP
425 This small excerpt from the pcap of one particular test call is a
426 representative example of this author's general experience with North American
427 IP-PSTN. Most of the time the \(*D between arrival times of two successive
428 RTP packets is within a few microseconds of the ideal 20\ ms value;
429 interarrival jitter spikes up to about 500\ \(*ms are fairly frequent
430 (seen a few times every second), but larger spikes (in the range of several
431 milliseconds) appear as rare outliers when I look at pcap files from
432 short test calls.
433 .PP
434 In order to draw packet flight diagrams that are intuitively understandable
435 by a human reader, we need to know the absolute flight time from the source
436 for each depicted packet. In actual operation this absolute flight time
437 is unknowable \(em the only available info is the observed cadence of
438 time-of-arrival deltas. (The absolute reception time of each packet according
439 to the local clock is known of course, but it is of no use without knowing
440 the absolute time at which those packets were emitted by the sender.)
441 These absolute flight times of packets are furthermore not needed for
442 actual operation of jitter buffers \(em the available ToA \(*D information
443 is sufficient for jitter buffer design and tuning \(em but they are needed
444 for more intuitive understanding by humans.
445 Therefore, when we draw packet flight diagrams, we have to factor in some
446 arbitrary, made-up number for the ``baseline'' packet flight time under
447 ideal conditions: the purely notional ``baseline'' number which, when added
448 to the actually observed jitter, equals what we assume to be the true
449 flight time of each individual packet.
450 In drawing Figure\ 2 above, I set this ``baseline'' delay to 26\ ms:
451 one half of the lowest round-trip time I observe now when I ping the
452 IPv4 address of the IP-PSTN node I was conversing with in that test call.
453 .PP
454 In drawing this figure, I also exercised a degree of freedom in choosing
455 the arbitrary (not known in advance in real operation) phase shift between
456 the arrival time of RTP packets and the receiving entity's fixed time base,
457 i.e., the position of fixed-time polling ticks below the RTP Rx time axis
458 relative to the times of packet arrival on the same axis.
459 The specific phase shift I chose in drawing this figure is one that
460 illustrates the effect of this amount of real-world jitter on \fBtwjit\fP
461 operation: RTP packet with sequence number 0x0636 arrives just after
462 the receiver's polling time instead of just before.
463 The significance of this effect will be seen when we examine \fBtwjit\fP
464 operation and tuning.
465 .PP
466 At this point a reader of this paper, seeing that most packets depicted
467 in Figure\ 2 exhibit interarrival jitter measured in \(*ms rather than ms,
468 with a single occurrence of 5\ ms jitter as a rare outlier, may accuse
469 this author of first-world privilege in terms of Internet connection quality.
470 So let us consider what the figure would look like with more substantial
471 jitter \(em but still below 20\ ms.
472 (Why below 20\ ms, you may ask? The answer will be seen shortly.)
473 Because such jitter does not occur in the wild where I live,
474 I used a made-up dataset for the following figure:
475 .KS
476 .PS
477 # Let's assume each packet flight time consists of a 24 ms fixed component
478 # and a random jitter component between 0 and 13 ms: total flight time
479 # thus ranges from 24 to 37 ms, random in this range.
480
481 Tx_line: line -> right 4i
482 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
483 "time" at Tx_line.end + 0.1,0 ljust
484
485 vdist = 0.8i
486 move to Tx_line - 0,vdist
487 Rx_line: line -> right 4i
488 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
489 "time" at Rx_line.end + 0.1,0 ljust
490 for x = 0.2i to 3.8i by 0.4i do {
491 line from Rx_line + x,0 down 0.1i
492 }
493
494 Pkt1: arrow from Tx_line + 0.06,0 down vdist right 0.02i * 24.791
495 Pkt2: arrow from Pkt1 + 0.4,0 down vdist right 0.02i * 34.899
496 Pkt3: arrow from Pkt2 + 0.4,0 down vdist right 0.02i * 26.518
497 Pkt4: arrow from Pkt3 + 0.4,0 down vdist right 0.02i * 29.739
498 Pkt5: arrow from Pkt4 + 0.4,0 down vdist right 0.02i * 27.318
499 Pkt6: arrow from Pkt5 + 0.4,0 down vdist right 0.02i * 31.820
500 Pkt7: arrow from Pkt6 + 0.4,0 down vdist right 0.02i * 26.468
501 Pkt8: arrow from Pkt7 + 0.4,0 down vdist right 0.02i * 36.795
502 Pkt9: arrow from Pkt8 + 0.4,0 down vdist right 0.02i * 24.051
503 .PE
504 .ce
505 Figure 3: 13\ ms of random jitter
506 .KE
507 .PP
508 In the above figure, each packet flight time was arbitrarily picked in the
509 range between 24 and 37 ms, i.e., a ``baseline'' delay of 24\ ms combined
510 with 13\ ms of jitter. (The actual flight times for the figure were
511 initially drawn from an RNG program, then slightly tweaked by hand to
512 get closer to the extremes of the jitter range to be illustrated.)
513 Unlike Figure\ 2 which represents a real life occurrence, Figure\ 3 is
514 completely made up \(em yet as will be seen later in this paper,
515 the same \fBtwjit\fP configuration that is optimal for Figure\ 2 will also
516 handle the conditions of Figure\ 3 just as well \(em and the jitter
517 is more visible here.
518 .PP
519 So far we've only considered cases of jitter below 20\ ms, i.e., jitter
520 magnitude less than the periodic interval between successive RTP packets.
521 A reader ought to ask now: what happens if the jitter exceeds 20\ ms? Before
522 we can answer the question of what happens in such cases, let us first
523 consider what it means for IP network-induced jitter to exceed the interval
524 between successive packets.
525 Assuming that successive RTP packets are emitted every 20\ ms by the sender,
526 for the receiver to experience interarrival jitter that exceeds 20\ ms,
527 the intervening IP network would have to ``bunch up'' packets: the receiving
528 end suddenly stops receiving packets when they are expected, then a slew of
529 massively delayed packets arrive all at once.
530 The following figure is a real life example of such occurrence:
531 .KS
532 .PS
533 Tx_line: line -> right 6i
534 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
535 "time" at Tx_line.end + 0.1,0 ljust
536
537 vdist = 0.8i
538 move to Tx_line - 0,vdist
539 Rx_line: line -> right 6i
540 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
541 "time" at Rx_line.end + 0.1,0 ljust
542 for x = 0.2i to 5.8i by 0.4i do {
543 line from Rx_line + x,0 down 0.1i
544 }
545
546 Tx0: Tx_line + 0.1i,0
547
548 Land0: Rx_line + (0.1i + 0.02i * 26.0),0
549 Land1: Land0 + (0.02i * 19.951),0
550 Land2: Land1 + (0.02i * 22.235),0
551 Land3: Land2 + (0.02i * 18.341),0
552 Land4: Land3 + (0.02i * 19.429),0
553 Land5: Land4 + (0.02i * 127.784),0
554 Land6: Land5 + (0.02i * 1.542),0
555 Land7: Land6 + (0.02i * 3.417),0
556 Land8: Land7 + (0.02i * 0.273),0
557 Land9: Land8 + (0.02i * 0.440),0
558 Land10: Land9 + (0.02i * 0.115),0
559 Land11: Land10 + (0.02i * 6.467),0
560 Land12: Land11 + (0.02i * 20.013),0
561 Land13: Land12 + (0.02i * 20.009),0
562
563 arrow from Tx0 to Land0
564 arrow from Tx0 + (0.02i * 20),0 to Land1
565 arrow from Tx0 + (0.02i * 40),0 to Land2
566 arrow from Tx0 + (0.02i * 60),0 to Land3
567 arrow from Tx0 + (0.02i * 80),0 to Land4
568 arrow from Tx0 + (0.02i * 100),0 to Land5
569 arrow from Tx0 + (0.02i * 120),0 to Land6
570 arrow from Tx0 + (0.02i * 140),0 to Land7
571 arrow from Tx0 + (0.02i * 160),0 to Land8
572 arrow from Tx0 + (0.02i * 180),0 to Land9
573 arrow from Tx0 + (0.02i * 200),0 to Land10
574 arrow from Tx0 + (0.02i * 220),0 to Land11
575 arrow from Tx0 + (0.02i * 240),0 to Land12
576 arrow from Tx0 + (0.02i * 260),0 to Land13
577
578 "seq # 0x0082" at 1/2 <Tx_line.start, Rx_line.start> + 0.28i,0 rjust
579 "seq # 0x008F" at 1/2 <Tx_line.end, Rx_line.end> - 0.40i,0 ljust
580 .PE
581 .ce
582 Figure 4: 6 packets bunched together by IP network bottleneck
583 .KE
584 .PP
585 The above figure is based on observed behavior during an experiment performed
586 by this author on 2024-03-31, involving a mobile Internet connection (LTE)
587 and a Hurricane Electric IPv6 tunnel.
588 A WireGuard tunnel was established between the author's laptop and a server;
589 the test laptop was connected to the Internet via T-Mobile LTE in this
590 experiment, while the server was one that has native IPv4 plus an IPv6 address
591 by way of HE 6-in-4 tunnel.
592 The WireGuard tunnel was set up using only IPv6 addresses on the outside,
593 i.e., the LTE leg saw only IPv6 in this experiment.
594 A test stream of RTP packets,
595 spaced 20\ ms apart on the sending end, was transmitted inside a WireGuard
596 tunnel from the test laptop to the test server; the path of each packet
597 was thus as follows:
598 .IP \(bu
599 WireGuard encapsulation in IPv6 on the sending end (laptop);
600 .IP \(bu
601 Transport across T-Mobile Internet service (LTE) in IPv6, going
602 to the IPv6 address of the Hurricane Electric tunnel;
603 .IP \(bu
604 Hurricane Electric PoP received each packet on IPv6 and re-emitted it in IPv4
605 wrapping, going to the IPv4 address of the author's server;
606 .IP \(bu
607 The receiving server decapsulated first 6-in-4, then WireGuard.
608 .LP
609 A similar experiment was performed addressing a different server, one that has
610 native IPv6 connectivity in addition to IPv4; the behavior seen in Figure\ 4
611 was not seen in that other experiment, leading to the conclusion that the IP
612 network bottleneck that occasionally ``bunches together'' a series of
613 consecutive RTP packets is an artifact of the HE 6-in-4 tunnel, rather than
614 an artifact of mobile Internet access via LTE.
615 .PP
616 Irrespective of the cause though, Figure\ 4 is a good illustration of what
617 happens when buffering delays at an IP network bottleneck significantly exceed
618 the spacing interval between successive RTP packets.
619 No packet loss occurred in this experiment, i.e., every packet emitted by
620 the sending end was \fIeventually\fP received; likewise, no reordering appeared
621 at the receiving end: packets were received in the same order in which they
622 were emitted by the sender.
623 However, if we look at the arrival times of these selected packets
624 on the receiving end, we see the following picture \(em the dataset from which
625 Figure\ 4 was drawn:
626 .TS
627 box center;
628 c|c|c
629 c|c|n.
630 From seq # To seq # ToA delta (ms)
631 _
632 0x0082 0x0083 19.951
633 0x0083 0x0084 22.235
634 0x0084 0x0085 18.341
635 0x0085 0x0086 19.429
636 0x0086 0x0087 127.784
637 0x0087 0x0088 1.542
638 0x0088 0x0089 3.417
639 0x0089 0x008A 0.273
640 0x008A 0x008B 0.440
641 0x008B 0x008C 0.115
642 0x008C 0x008D 6.467
643 0x008D 0x008E 20.013
644 0x008E 0x008F 20.009
645 .TE
646 .PP
647 Generally speaking, IP network behavior in this more adverse environment
648 (passing through a leg of consumer mobile Internet) is not much worse than the
649 ``luxurious'' IP-PSTN environment (server to server, business-grade Internet
650 connection on the non-datacenter end) of Figure\ 2: most of the time, ToA \(*D
651 from one packet to the next is only a few \(*ms away from the true 20\ ms
652 ideal, with occasional jitter spikes of a few ms.
653 However, occasionally a more obstinent bottleneck occurs in the IP network path
654 that blocks the flow for a much longer duration: in the example I presented
655 here, for just over 120\ ms.
656 During such suddenly induced pauses, RTP packets coming from the source every
657 20\ ms accumulate at the bottleneck, and when that bottleneck clears,
658 all queued-up packets are delivered directly back to back, arriving less than
659 1\ ms apart, essentially all at once.
660 .PP
661 In all examples we have considered so far, there has been no packet reordering:
662 despite variations in flight time that appear on the receiving end as jitter,
663 all RTP packets were received in the same order in which they were emitted
664 by the source endpoint.
665 Now let us consider what kind of scenarios can result in RTP packets arriving
666 out of order.
667 So far this author has not observed even one actual occurrence of packet
668 reordering \(em apparently it does not happen on IP networks that exist
669 in this part of the world, even on consumer LTE \(em hence the following
670 analysis will be strictly theoretical.
671 Given that the source endpoint steadily emits one packet at a time,
672 spaced every 20\ ms, how can these packets arrive in a reversed order? In
673 order for packets to arrive out of order, a later-sent packet has to
674 experience significantly shorter transit delay than an earlier-sent one,
675 such that the later-sent packet ``overtakes'' the earlier-sent one.
676 One situation where we can easily imagine such happening is the ``melee''
677 shown in Figure\ 4, the spot on the RTP Rx time axis where 6 different arrows,
678 representing 6 different RTP packets emitted 20\ ms apart, all arrive at
679 essentially the same point in time.
680 In our actual experience, such ``bunched together'' packets still arrive in
681 the correct order, even if the \(*D in the time of arrival between them is
682 only a few \(*ms \(em but we can easily imagine a different implementation
683 of the offending IP network element (the one where the bottleneck occurs)
684 that ``sprays'' buffered packets out of order when the congestion clears.
685 The following drawing is a rework of Figure\ 4, showing what this hypothesized
686 behavior would look like:
687 .KS
688 .PS
689 Tx_line: line -> right 6i
690 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
691 "time" at Tx_line.end + 0.1,0 ljust
692
693 vdist = 0.8i
694 move to Tx_line - 0,vdist
695 Rx_line: line -> right 6i
696 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
697 "time" at Rx_line.end + 0.1,0 ljust
698 for x = 0.2i to 5.8i by 0.4i do {
699 line from Rx_line + x,0 down 0.1i
700 }
701
702 Tx0: Tx_line + 0.1i,0
703
704 Land0: Rx_line + (0.1i + 0.02i * 26.0),0
705 Land1: Land0 + (0.02i * 19.951),0
706 Land2: Land1 + (0.02i * 22.235),0
707 Land3: Land2 + (0.02i * 18.341),0
708 Land4: Land3 + (0.02i * 19.429),0
709 Land5: Land4 + (0.02i * 127.784),0
710 Land6: Land5 + (0.02i * 1.542),0
711 Land7: Land6 + (0.02i * 3.417),0
712 Land8: Land7 + (0.02i * 0.273),0
713 Land9: Land8 + (0.02i * 0.440),0
714 Land10: Land9 + (0.02i * 0.115),0
715 Land11: Land10 + (0.02i * 6.467),0
716 Land12: Land11 + (0.02i * 20.013),0
717 Land13: Land12 + (0.02i * 20.009),0
718
719 arrow from Tx0 to Land0
720 arrow from Tx0 + (0.02i * 20),0 to Land1
721 arrow from Tx0 + (0.02i * 40),0 to Land2
722 arrow from Tx0 + (0.02i * 60),0 to Land3
723 arrow from Tx0 + (0.02i * 80),0 to Land4
724 arrow from Tx0 + (0.02i * 100),0 to Land10
725 arrow from Tx0 + (0.02i * 120),0 to Land9
726 arrow from Tx0 + (0.02i * 140),0 to Land8
727 arrow from Tx0 + (0.02i * 160),0 to Land7
728 arrow from Tx0 + (0.02i * 180),0 to Land6
729 arrow from Tx0 + (0.02i * 200),0 to Land5
730 arrow from Tx0 + (0.02i * 220),0 to Land11
731 arrow from Tx0 + (0.02i * 240),0 to Land12
732 arrow from Tx0 + (0.02i * 260),0 to Land13
733
734 "seq # 0x0082" at 1/2 <Tx_line.start, Rx_line.start> + 0.28i,0 rjust
735 "seq # 0x008F" at 1/2 <Tx_line.end, Rx_line.end> - 0.40i,0 ljust
736 .PE
737 .ce
738 Figure 5: Hypothetical reordering of the 6 ``bunched up'' packets of Figure 4
739 .KE
740 .PP
741 Figure\ 5 above (hypothetical scenario) differs from Figure\ 4 (actual
742 experience) in that the arrival times of 6 RTP packets 0x0087 through 0x008C
743 (RTP sequence numbers from the dataset of Figure\ 4) have been reversed:
744 0x008C is hypothesized to arrive when 0x0087 actually arrived, 0x0087 is
745 hypothesized to arrive when 0x008C actually arrived, and the 4 packets in
746 the middle are mirrored symmetrically.
747 Because all 6 of these packets were stuck waiting behind the same bottleneck
748 at the same time, the fictional scenario presented in Figure\ 5 is at least
749 plausible.
750 .PP
751 For the sake of completeness, let us consider a more fantastical (less likely
752 in reality) scenario of packet reordering.
753 Figure\ 6 below depicts the way beginning students of IP networking likely
754 imagine packet reordering:
755 .KS
756 .PS
757 Tx_line: line -> right 4i
758 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
759 "time" at Tx_line.end + 0.1,0 ljust
760
761 vdist = 0.8i
762 move to Tx_line - 0,vdist
763 Rx_line: line -> right 4i
764 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
765 "time" at Rx_line.end + 0.1,0 ljust
766 for x = 0.2i to 3.8i by 0.4i do {
767 line from Rx_line + x,0 down 0.1i
768 }
769
770 arrow from Tx_line + 0.2i,0 down vdist right 0.3i
771 arrow from Tx_line + 0.6i,0 down vdist right 1.3i
772 arrow from Tx_line + 1.0i,0 down vdist right 0.3i
773 arrow from Tx_line + 1.4i,0 down vdist right 0.3i
774 arrow from Tx_line + 1.8i,0 down vdist right 1.0i
775 arrow from Tx_line + 2.2i,0 down vdist right 0.3i
776 arrow from Tx_line + 2.6i,0 down vdist right 0.8i
777 arrow from Tx_line + 3.0i,0 down vdist right 0.3i
778 arrow from Tx_line + 3.4i,0 down vdist right 0.3i
779 .PE
780 .ce
781 Figure 6: Unlikely form of packet reordering
782 .KE
783 .PP
784 In order for the fictional scenario of Figure\ 6 to occur, the IP network
785 would have to behave in a way where some packets get stuck behind a
786 delay-inducing bottleneck, yet other packets, including those that directly
787 follow the unlucky ``stuck'' packet, are delivered without excessive delay,
788 arriving ahead of those that got stuck.
789 It is difficult to imagine what mechanisms could cause a real IP network
790 to behave in such manner \(em but if some real IP network somewhere does
791 indeed exhibit this theorized behavior, \fBtwjit\fP should be able to handle
792 it with appropriate tuning.
793 .PP
794 None of the examples we've examined so far include packet loss, only delay
795 and perhaps reordering. However, each of the presented figures can be
796 trivially modified to reflect packet loss: instead of a packet arriving late
797 or out of order (later than a subsequently-sent packet), it never arrives
798 at all.
799 Readers are invited to use their imagination: take any of the arrows that
800 represent individual RTP packets, and erase it.
801 .NH 2
802 Design of twjit
803 .PP
804 Having seen various scenarios of RTP flows, let us now consider what work
805 \fBtwjit\fP needs to do in order to convert a received RTP stream back to
806 fixed timing.
807 .PP
808 Each \fBtwjit\fP instance consists of two sub-buffers (subbufs for short)
809 and a global state variable that gives the overall state of the instance
810 across both subbufs.
811 Each subbuf holds a queue of received RTP packets that belong to a single
812 RTP flow as defined in \(sc2.1; two subbufs are needed in order to handle
813 handovers \(em if the incoming RTP stream does not exhibit any handover
814 events, a single subbuf is sufficient.
815 .NH 3
816 The major states of twjit
817 .LP
818 Each \fBtwjit\fP instance as a whole, across both subbufs, is a finite state
819 machine with 4 possible states:
820 .sp .3
821 .IP \fBEMPTY\fP 15
822 The \fBtwjit\fP instance is completely empty, neither subbuf holds any
823 packets or any meaningful state information.
824 .IP \fBHUNT\fP 15
825 Only one subbuf is active and valid in this state;
826 this subbuf is non-empty \(em some received packets are held \(em but it hasn't
827 started flowing out yet, as will be explained in the following section.
828 .IP \fBFLOWING\fP 15
829 Only one subbuf is active and valid in this state;
830 this subbuf is both flowing out and accepting new packets.
831 This state is the one that holds long-term during good reception of a
832 steady RTP flow.
833 .IP \fBHANDOVER\fP 15
834 Both subbufs are active and valid:
835 one is flowing out while the other receives new packets.
836 As indicated in the name, this state is entered from the \fBFLOWING\fP state
837 only when the received RTP stream exhibits a handover.
838 .sp .3
839 .LP
840 Possible transitions between these 4 fundamental states are as follows:
841 .PS
842 circlerad = 0.5
843 EMPTY: circle "\fBEMPTY\fP"
844 move right 0.65i
845 HUNT: circle "\fBHUNT\fP"
846 move same
847 FLOWING: circle "\fBFLOWING\fP"
848 move same
849 HANDOVER: circle "\fBHANDOVER\fP"
850 arrow from EMPTY.e to HUNT.w
851 arrow from HUNT.e to FLOWING.w
852 arc cw -> from FLOWING.s to EMPTY.s rad 2i
853 arc cw -> from FLOWING.e to HANDOVER.w rad 0.5i
854 arc cw -> from HANDOVER.w to FLOWING.e rad 0.5i
855 arc -> from HANDOVER.n to HUNT.n rad 2i
856 .PE
857 .PP
858 In order to understand the workings of \fBtwjit\fP,
859 let us first consider operation without handovers
860 (SSRC never changes, and the timestamp increment from each source-emitted packet
861 to the next always equals the samples-per-quantum constant) \(em in such
862 sans-handover operation, only \fBEMPTY\fP, \fBHUNT\fP and \fBFLOWING\fP states
863 are encountered \(em and then examine handover handling.
864 .PP
865 The previous description and state machine diagram apply to standard
866 \fBtwjit\fP as originally designed, either \&\fCtwrtp\-native\fP version
867 or the present Osmocom-integrated version \fIwithout\fP underrun extension
868 feature.
869 However, when \&\fCunderrun\-extension\fP feature is configured,
870 a fifth state named \%\fBUNDERRUN\fP appears, and the state transition
871 diagram changes to:
872 .PS
873 circlerad = 0.5
874 EMPTY: circle "\fBEMPTY\fP"
875 move right 0.65i
876 HUNT: circle "\fBHUNT\fP"
877 move same
878 FLOWING: circle "\fBFLOWING\fP"
879 move same
880 HANDOVER: circle "\fBHANDOVER\fP"
881 move to FLOWING.s; move down 0.65i
882 UNDERRUN: circle "\fBUNDERRUN\fP"
883 arrow from EMPTY.e to HUNT.w
884 arrow from HUNT.e to FLOWING.w
885 arc cw -> from FLOWING.e to HANDOVER.w rad 0.5i
886 arc cw -> from HANDOVER.w to FLOWING.e rad 0.5i
887 arc cw -> from FLOWING.s to UNDERRUN.n rad 0.5i
888 arc cw -> from UNDERRUN.n to FLOWING.s rad 0.5i
889 arc -> from HANDOVER.n to HUNT.n rad 2i
890 arrow from UNDERRUN to EMPTY chop
891 arrow from UNDERRUN to HUNT chop
892 .PE
893 .NH 3
894 Structure and operation of one subbuf
895 .PP
896 Each subbuf of \fBtwjit\fP
897 holds a queue of received RTP packets that belong to a single
898 RTP flow as defined in \(sc2.1.
899 In terms of memory allocation, the queue of each subbuf is implemented
900 as a linked list of Osmocom message buffers (\fBmsgb\fPs) \(em but this
901 implementation detail is really only a matter of memory allocation strategy,
902 and must \fBnot\fP be misconstrued to infer what kinds of packet sequences
903 are allowed to exist in one subbuf.
904 In sharp contrast with a naive interpretation of what a linked list can
905 presumably hold (any sequence of packets, without strict constraints on
906 timestamp increment or any other aspect), the logical structure of a single
907 \fBtwjit\fP subbuf is a chain of fixed slots, where each slot corresponds
908 to a given RTP timestamp and may be either empty or filled with a received
909 packet.
910 .PP
911 A good physical analogy for the logical structure of a \fBtwjit\fP subbuf
912 can be found in carrier tapes that hold electronic components in tape-and-reel
913 packaging.
914 There is a long tape made of plastic or thick paper with regularly spaced
915 wells, with each well intended to hold one piece of the reeled part.
916 Whether each given well in the tape holds a component or is empty, the spacing
917 between wells remains fixed, and a component cannot be inserted anywhere into
918 the tape except into a designated well.
919 .PP
920 The following drawing depicts a subbuf with both filled and empty slots:
921 .KS
922 .PS
923 boxwid = 0.5
924 boxht = 0.3
925
926 box; box; box; box
927
928 move to 1st box.n; line <- up " head" ljust
929 move to last box.n; line <- up " tail" ljust
930
931 "0" at 1st box.s below
932 "1" at 2nd box.s below
933 "2" at 3rd box.s below
934 "3" at last box.s below
935
936 boxwid = 0.2
937 boxht = 0.1
938
939 box filled 1 with .center at 1st box
940 box filled 1 with .center at 4th box
941
942 .PE
943 .ce
944 Figure 7: Basic principle of twjit subbuf
945 .KE
946 .PP
947 The subbuf depicted in Figure\ 7 has a total depth (to be defined shortly)
948 of 4 quantum units (please recall the definition of a quantum in \(sc1.3),
949 the tail slot is filled as always required for a non-empty subbuf,
950 the head slot is also filled in this example, but the other two slots
951 are empty.
952 (Such subbuf state may result from packet loss, and may also occur in cases
953 of packet reordering if the packets destined for empty slots 1 and 2 may yet
954 arrive.)
955 .PP
956 Every non-empty \fBtwjit\fP subbuf has a head slot, a tail slot and a total
957 depth.
958 The head slot is defined by the 32-bit RTP timestamp stored in \&\fChead_ts\fP
959 member of the subbuf structure, which is \&\fCstruct\ twjit_subbuf\fP inside
960 \&\fCtwjit.c\fP in the present version.
961 If the subbuf holds a received RTP packet whose timestamp equals
962 \&\fChead_ts\fP, that packet resides in the head slot; if no such packet is
963 held, then the head slot is empty.
964 However, packets with RTP timestamps earlier than \&\fChead_ts\fP \fBcannot\fP
965 exist in a subbuf!
966 .PP
967 Every received RTP packet held in a subbuf, as well as every empty slot that
968 can potentially be filled by a late-arriving out-of-order packet, can be viewed
969 as existing at a certain depth.
970 The head slot shall be regarded as depth 0, the following slot shall be
971 regarded as depth 1, and so forth.
972 Recall that per the fundamental design of \fBtwjit\fP, each subbuf can only
973 hold RTP packets belonging to a single flow as defined in \(sc2.1 \(em thus
974 if one quantum equals 160 timestamp units,
975 slot 1 can only hold an RTP packet whose timestamp equals
976 (\fChead_ts\fP\ +\ 160),
977 slot 2 can only hold an RTP packet whose timestamp equals
978 (\fChead_ts\fP\ +\ 320),
979 and so forth.
980 .PP
981 Of all received RTP packets held by the subbuf, whichever packet has the newest
982 RTP timestamp is regarded as the current tail of the subbuf, and its depth
983 is regarded as the current tail slot.
984 The total depth of a subbuf is defined as the depth of the tail packet plus 1;
985 in the example of Figure\ 7, the tail packet has depth 3 and the total depth
986 of the subbuf is 4.
987 The total depth of a subbuf is also called the fill level, by analogy with
988 fill level of a water tank.
989 .PP
990 When a new RTP packet is received, and that packet is deemed to belong to the
991 flow already being received (same SSRC, timestamp increment meets expectations),
992 the newly received packet is added to the current write subbuf.
993 The insertion depth of the new packet is calculated as the newly received
994 timestamp minus \&\fChead_ts\fP, divided by the number of timestamp units
995 per quantum.
996 If this insertion depth exceeds the current tail depth, which is the normal
997 case, the subbuf grows (the fill level increases) and the newly added packet
998 becomes the new tail.
999 As a result of this operation, the tail slot of a non-empty subbuf
1000 can never be empty!
1001 Alternatively, if the insertion depth falls somewhere before the current
1002 total depth of the subbuf, the fill level stays the same and the target slot
1003 \(em which is expected to be empty in this case \(em
1004 is filled with the newly received packet.
1005 If that target slot was already filled, the new packet is discarded and
1006 an error counter is incremented, indicating duplicate Rx packets.
1007 .PP
1008 When an active, flowing-out subbuf is polled for output at fixed times
1009 determined by TDM or GSM Um etc, the head slot is consumed, whether it is
1010 filled or empty.
1011 If the consumed head slot was filled, that buffered packet is delivered
1012 to the fixed timing system on the output of the jitter buffer.
1013 If that slot was empty, the application on the output of the jitter buffer
1014 receives a gap in the stream.
1015 Either way, when the previous head slot is consumed, \&\fChead_ts\fP is
1016 incremented by the samples-per-quantum constant, the following slot becomes
1017 the new head slot, and the total depth of the subbuf decreases by 1.
1018 .PP
1019 There also exists a special condition in which a subbuf is empty
1020 (does not hold any buffered packets, fill level equals 0),
1021 but is still considered active.
1022 This condition can occur only in \fBFLOWING\fP state, and is covered in
1023 the respective section.
1024 .NH 3
1025 EMPTY and HUNT states
1026 .PP
1027 Upon initialization or reset, each \fBtwjit\fP instance begins life in
1028 \fBEMPTY\fP state.
1029 As soon as the first valid RTP packet is received, one subbuf is initialized
1030 to hold this first packet; the total depth of this newly initialized subbuf
1031 is 1 and the slot occupied by the initial packet is both the head and the tail.
1032 The overall state of \fBtwjit\fP instance transitions to \fBHUNT\fP.
1033 .PP
1034 The purpose of \fBHUNT\fP state is to accumulate enough received packets,
1035 necessarily belonging to a single flow as defined in \(sc2.1, until a
1036 configured threshold is met for entry into \fBFLOWING\fP state.
1037 The critically important configuration parameter is the flow-starting
1038 fill level; it is the first number given on the \&\fCbuffer\-depth\fP vty
1039 configuration line.
1040 .PP
1041 The flow-starting fill level is the fill level (total depth of the sole
1042 active subbuf) required for transition from \fBHUNT\fP state into \fBFLOWING\fP
1043 state.
1044 This criterion is evaluated on every 20\ ms fixed timing tick when the
1045 application polls \fBtwjit\fP for a required quantum of media; as a result of
1046 this check, the \fBtwjit\fP instance either delivers its first output and
1047 transitions into \fBFLOWING\fP, or returns \fBNULL\fP (``sorry, I got nothing'')
1048 to the application and remains in \fBHUNT\fP state.
1049 .PP
1050 The significance of this threshold parameter, and guidelines for its tuning,
1051 are best understood by looking at examples of RTP flows shown in \(sc2.2.
1052 The minimum allowed setting for this parameter is 1; with this minimum setting,
1053 the first tick of the fixed timing system after reception of any RTP packet
1054 always causes transition into \%\fBFLOWING\fP state,
1055 and the packet received just prior to this transition-causing tick
1056 is delivered to the output on that tick.
1057 This setting produces the lowest possible buffer-added latency: this latency
1058 can be near-zero if the RTP packet arrived just prior to the fixed timing tick,
1059 or just under 20\ ms if it arrives just after the previous tick.
1060 .PP
1061 However, if we look at Figures 1 and 2 in \(sc2.2, we can see the one big
1062 problem with this lowest latency setting: the flow remains perfect under
1063 absolutely ideal conditions of Figure\ 1, but as soon as we enter real-world
1064 conditions as shown in Figure\ 2, we can easily encounter scenarios like
1065 the RTP packet with sequence number 0x0636 in that figure.
1066 If the RTP flow depicted in Figure\ 2 were to be received by a \fBtwjit\fP
1067 instance whose flow-starting fill level is set to 1, the buffer would
1068 experience an underrun on the tick of the fixed timing system that just barely
1069 missed the slightly delayed packet; the user would then experience an equivalent
1070 of packet loss (frame erasure) at a time when no actual packet loss occurred.
1071 .PP
1072 For this reason, the default and generally recommended setting for the
1073 flow-starting fill level parameter is 2.
1074 With this setting, two RTP packets with properly consecutive timestamps
1075 must be received in \fBHUNT\fP state before \fBtwjit\fP transitions into
1076 \fBFLOWING\fP state.
1077 The buffer-added latency will be anywhere between 20 and 40 ms,
1078 depending on the unpredictable phase alignment between arriving RTP packets
1079 and ticks of the fixed timing system on the output side of \fBtwjit\fP.
1080 As long as the jitter between flight times of different packets, or its
1081 observable manifestation as interarrival jitter, remains below 20\ ms
1082 (or below 16.9\ ms if the receiving element is OsmoBTS whose fixed time
1083 base includes the inherent jitter of GSM Um multiframe structure),
1084 there will not be an occurrence where the receiving system transforms jitter
1085 into an effective equivalent of packet loss.
1086 This amount of jitter tolerance is sufficient for most practical IP networks
1087 in this author's experience.
1088 .PP
1089 But what if the IP network regularly exhibits packet delay jitter that is
1090 significantly greater than 20\ ms?
1091 Suppose the network regularly exhibits conditions similar to the aberration
1092 depicted in Figure\ 4 \(em what then?
1093 In this case the administrator of jitter-buffer-equipped network elements
1094 has to make a trade-off between two different forms of degraded user experience:
1095 either increase the flow-starting fill level setting and thereby increase the
1096 experienced latency, or live with underruns (frame erasure effectively
1097 equivalent to packet loss) whenever the IP network stops flowing smoothly
1098 and decides to ``bunch up'' packets instead.
1099 As just one example, if the amount of ``bunching up'' exhibited by the IP
1100 network were exactly as depicted in Figure\ 4, and the desire were to eliminate
1101 packet-loss-equivalent effects at the expense of added latency,
1102 the required flow-starting fill level setting would be 7,
1103 producing added latency between 120 and 140 ms.
1104 .NH 4
1105 Reception of additional packets in HUNT state
1106 .PP
1107 Every time an additional RTP packet is received when the \fBtwjit\fP instance
1108 is already in \fBHUNT\fP state (after the first Rx packet that moved the
1109 state from \fBEMPTY\fP to \fBHUNT\fP),
1110 certain checks are made.
1111 The first fundamental requirement of \fBHUNT\fP state is that all queued
1112 packets belong to the same flow.
1113 If the newly received RTP packet has a different SSRC, or if it exhibits
1114 a timestamp increment that is numerically incompatible with being a member
1115 of the same flow (not an integral multiple of samples-per-quantum constant),
1116 all previously queued packets are discarded and the \fBHUNT\fP state is
1117 reinitialized anew with the just-received packet.
1118 This behavior is unavoidably necessary: the single subbuf of \fBHUNT\fP state
1119 holds packets belonging to \fIone\fP particular flow, and given the choice
1120 between a stale flow that appears to have just ended and the new flow that
1121 appears to have just begun, the new flow is clearly the correct choice.
1122 .PP
1123 Once the same-flow requirement is met and the newly received packet is not
1124 too old (packets whose RTP timestamps precede the current \&\fChead_ts\fP
1125 have to be discarded), the new packet is inserted into the sole active
1126 subbuf at its respective depth.
1127 Most of the time, this insertion will increase the total depth or fill level
1128 of this subbuf.
1129 At this point the new fill level is checked against the flow-starting
1130 fill level setting: if the flow-starting fill level has just been exceeded,
1131 packets are discarded from the head of the subbuf and \&\fChead_ts\fP advances
1132 forward until the remaining fill level is equal to or below the flow-starting
1133 threshold.
1134 .PP
1135 This trimming of the subbuf to the flow-starting fill level is necessary to
1136 ensure that the latency added by the jitter buffer will indeed be what the
1137 administrator intended to set via the tunable parameter, as opposed to
1138 potentially much higher added latency that could be caused by artifacts
1139 at flow starting time.
1140 Suppose that the IP network bunches up a significant number of packets
1141 when the sender begins transmitting them 20\ ms apart, then delivers that
1142 bunch all at once, and then begins to flow evenly:
1143 .KS
1144 .PS
1145 Tx_line: line -> right 6i
1146 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
1147 "time" at Tx_line.end + 0.1,0 ljust
1148
1149 vdist = 0.8i
1150 move to Tx_line - 0,vdist
1151 Rx_line: line -> right 6i
1152 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
1153 "time" at Rx_line.end + 0.1,0 ljust
1154 for x = 0.2i to 5.8i by 0.4i do {
1155 line from Rx_line + x,0 down 0.1i
1156 }
1157
1158 Tx0: Tx_line + 0.1i,0
1159
1160 # timing numbers are made up!
1161 Land0: Rx_line + (0.1i + 0.02i * 127.5),0
1162 Land1: Land0 + (0.02i * 1.542),0
1163 Land2: Land1 + (0.02i * 3.417),0
1164 Land3: Land2 + (0.02i * 0.273),0
1165 Land4: Land3 + (0.02i * 0.440),0
1166 Land5: Land4 + (0.02i * 0.115),0
1167 Land6: Land5 + (0.02i * 6.467),0
1168 Land7: Land6 + (0.02i * 20.013),0
1169 Land8: Land7 + (0.02i * 20.009),0
1170 Land9: Land8 + (0.02i * 19.992),0
1171 Land10: Land9 + (0.02i * 20.522),0
1172 Land11: Land10 + (0.02i * 19.509),0
1173 Land12: Land11 + (0.02i * 20.211),0
1174 Land13: Land12 + (0.02i * 19.741),0
1175
1176 arrow from Tx0 to Land0
1177 arrow from Tx0 + (0.02i * 20),0 to Land1
1178 arrow from Tx0 + (0.02i * 40),0 to Land2
1179 arrow from Tx0 + (0.02i * 60),0 to Land3
1180 arrow from Tx0 + (0.02i * 80),0 to Land4
1181 arrow from Tx0 + (0.02i * 100),0 to Land5
1182 arrow from Tx0 + (0.02i * 120),0 to Land6
1183 arrow from Tx0 + (0.02i * 140),0 to Land7
1184 arrow from Tx0 + (0.02i * 160),0 to Land8
1185 arrow from Tx0 + (0.02i * 180),0 to Land9
1186 arrow from Tx0 + (0.02i * 200),0 to Land10
1187 arrow from Tx0 + (0.02i * 220),0 to Land11
1188 arrow from Tx0 + (0.02i * 240),0 to Land12
1189 arrow from Tx0 + (0.02i * 260),0 to Land13
1190 .PE
1191 .ce
1192 Figure 8: Packets bunched together at the beginning of flow
1193 .KE
1194 .PP
1195 If the step of trimming the subbuf in \fBHUNT\fP state to the flow-starting
1196 fill level were omitted, then in the scenario depicted in Figure\ 8,
1197 the fill level on entry into \fBFLOWING\fP state would be 7 instead of 2
1198 or whatever flow-starting fill level is configured by the administrator,
1199 significantly increasing the latency experienced by the user.
1200 .PP
1201 If the flow-starting fill level is set to 1, the total depth
1202 of the sole active subbuf in \fBHUNT\fP state will never equal anything
1203 other than 1; if the flow-starting fill level is set to 2 (the default),
1204 the total depth of the subbuf in \fBHUNT\fP state will always equal either
1205 1 or 2, with both head and tail slots always filled.
1206 Empty slots in this sole active subbuf cannot exist when the flow-starting
1207 fill level is set to 1 or 2.
1208 However, if the flow-starting fill level is set to 3 or greater, empty
1209 slots in the subbuf in \fBHUNT\fP state become possible: both head and tail
1210 slots are still always filled in \fBHUNT\fP state, but with higher settings
1211 of the flow-starting fill level configuration parameter, it becomes possible
1212 to have empty slots in the middle, produced by packet loss or reordering.
1213 .NH 4
1214 Additional time delta guards
1215 .PP
1216 Let us once again consider the scenario depicted in Figure\ 8.
1217 The step of trimming the subbuf to the flow-starting fill level prevents
1218 induction of significantly increased latency by initial floods arriving
1219 while the \fBtwjit\fP instance is still in \fBHUNT\fP state
1220 \(em but suppose the transition from \fBHUNT\fP into \fBFLOWING\fP occurs
1221 while an initial flood, similar to that depicted in Figure\ 8,
1222 is still ongoing.
1223 As covered in the following section, once the overall state of \fBtwjit\fP
1224 instance is \fBFLOWING\fP, no more simple head trimming can occur: there is
1225 a much slower-acting standing queue thinning mechanism, but any extra latency
1226 that was induced at the start of the flow can only be dissipated very slowly,
1227 and with an unavoidable side effect of phase shifts in the delivered flow.
1228 .PP
1229 In order to produce better performance in IP network environments where
1230 scenarios like Figure\ 8 are expected,
1231 there is an additional check, optionally enabled per configuration,
1232 gating the transition from \fBHUNT\fP into \fBFLOWING\fP state:
1233 it is \&\fCstart\-min\-delta\fP vty setting.
1234 When this optional parameter is set, it specifies the minimum required
1235 time-of-arrival delta in milliseconds between the most recently received
1236 RTP packet and the one received just prior; this minimum ToA delta must hold
1237 in order for transition from \fBHUNT\fP into \fB\%FLOWING\fP to be allowed.
1238 .PP
1239 For the sake of symmetry, there is also an optional \&\fCstart\-max\-delta\fP
1240 vty setting.
1241 When this optional parameter is set, it specifies the maximum allowed
1242 ToA delta in \fBHUNT\fP and \fBHANDOVER\fP states:
1243 if the ToA delta between successively received packets exceeds this
1244 threshold, previously queued packets are discarded
1245 (regarded as remnants of a stale previous flow)
1246 and the hunt process begins anew with the latest received packet.
1247 .NH 3
1248 FLOWING state
1249 .PP
1250 When the global state of a given \fBtwjit\fP instance is \fBFLOWING\fP,
1251 there is only one active subbuf, just like in \fBHUNT\fP state.
1252 Newly received RTP packets that belong to the same flow (same SSRC,
1253 timestamp increments meet expectations) are likewise added to this subbuf
1254 just as they were during \fBHUNT\fP.
1255 However, the same subbuf is also flowing out: on every tick of the fixed
1256 timing system on the output side of \fBtwjit\fP,
1257 the head slot of the subbuf is consumed and \&\fChead_ts\fP advances
1258 accordingly.
1259 .PP
1260 Unlike \fBHUNT\fP state, \fBFLOWING\fP state allows the head slot of
1261 the sole active subbuf to be empty.
1262 This situation will occur if the received flow experiences a gap
1263 (packet loss, reordering or an intentional gap emitted by the RTP stream
1264 source), the last received packet before the gap is consumed,
1265 but there are still more packets at greater depth,
1266 such that the subbuf is not entirely empty.
1267 If the head slot remains empty on the fixed timing tick that consumes it,
1268 the application on the output side of \fBtwjit\fP receives a gap
1269 in the stream,
1270 but this event is \fBnot\fP regarded as an underrun.
1271 .NH 4
1272 Handling of underruns
1273 .PP
1274 It is possible for the flowing-out subbuf to be empty, but not incur
1275 an underrun just yet.
1276 Suppose the total depth (see \(sc2.3.2) of the flowing-out subbuf
1277 equals 1 at the time of an output poll: the head slot is also the tail slot,
1278 and the previously received RTP packet consumed on this tick is the very last
1279 one received till this moment.
1280 After this output poll tick, the subbuf is empty (total depth equals 0),
1281 but it is still valid in the sense that the overall state remains \fBFLOWING\fP
1282 (does not transition to \fBEMPTY\fP)
1283 and \&\fChead_ts\fP is still regarded as valid, equal to the timestamp
1284 of the last delivered packet plus 160.
1285 If another RTP packet, belonging to the same flow, is received before the
1286 next output poll tick, the flow continues without underrun or any other
1287 undesirable interruptions.
1288 This situation occurs all the time in normal operation when the flow-starting
1289 fill level configuration parameter is optimally tuned, adding just enough
1290 buffering latency to handle the amount of jitter that actually occurs,
1291 but no more.
1292 .PP
1293 A true underrun occurs on the next output poll tick after the one that
1294 leaves the flowing-out subbuf empty while still valid.
1295 At this point the overall state of the \fBtwjit\fP instance transitions to
1296 \fBEMPTY\fP.
1297 Any subsequently received RTP packet, whatever its SSRC and timestamp may be,
1298 causes a transition from \fBEMPTY\fP into \fBHUNT\fP, and the process of
1299 latching onto a flow begins anew.
1300 The application on the output of the jitter buffer will keep receiving
1301 \fBNULL\fP (``sorry, I got nothing'')
1302 starting with the output poll tick on which the underrun occurs
1303 and continuing until the new flow after the underrun (if there is one)
1304 reaches the flow-starting fill level.
1305 .PP
1306 For the benefit of network operations staff looking at stats counters
1307 logged upon call completion (see Chapter\ 3 regarding stats and analytics),
1308 the \&\fCunderruns\fP counter is incremented not at the point where the
1309 actual underrun occurs, but upon receipt of the next RTP packet
1310 (if there is one) that makes the transition from \fBEMPTY\fP into \fBHUNT\fP.
1311 This implementation detail results in not counting the final underrun
1312 that often occurs upon call teardown, instead counting only those underrun
1313 events that are true indications of problematic network conditions
1314 or insufficient jitter buffering.
1315 .PP
1316 The previous description applies to standard \fBtwjit\fP algorithm:
1317 either \&\fCtwrtp\-native\fP or the present \fBosmo_twjit\fP version
1318 without \&\fCunderrun\-extension\fP.
1319 If underrun extension feature is enabled, the behavior changes to that
1320 described in \(sc2.3.5.1.
1321 .NH 4
1322 Standing queue thinning mechanism
1323 .PP
1324 Suppose that the beginning of an RTP flow (acquisition in \fBHUNT\fP state,
1325 then transition into \fBFLOWING\fP) happens when the IP network path
1326 experiences a spike in latency, then later that spike subsides and
1327 latency along the IP network path returns to a lower baseline.
1328 This scenario may look as follows:
1329 .KS
1330 .PS
1331 Tx_line: line -> right 5i
1332 "RTP Tx:" at Tx_line.start - 0.1,0 rjust
1333 "time" at Tx_line.end + 0.1,0 ljust
1334
1335 vdist = 0.8i
1336 move to Tx_line - 0,vdist
1337 Rx_line: line -> right 5i
1338 "RTP Rx:" at Rx_line.start - 0.1,0 rjust
1339 "time" at Rx_line.end + 0.1,0 ljust
1340 for x = 0.2i to 4.8i by 0.4i do {
1341 line from Rx_line + x,0 down 0.1i
1342 }
1343
1344 Tx0: Tx_line + 0.1i,0
1345
1346 # timing numbers are made up!
1347 Land0: Rx_line + (0.1i + 0.02i * 63),0
1348 Land1: Land0 + (0.02i * 19.992),0
1349 Land2: Land1 + (0.02i * 20.522),0
1350 Land3: Land2 + (0.02i * 19.509),0
1351 Land4: Land3 + (0.02i * 20.211),0
1352 Land5: Land4 + (0.02i * 6.467),0
1353 Land6: Land5 + (0.02i * 1.542),0
1354 Land7: Land6 + (0.02i * 3.417),0
1355 Land8: Land7 + (0.02i * 20.273),0
1356 Land9: Land8 + (0.02i * 20.440),0
1357 Land10: Land9 + (0.02i * 19.501),0
1358 Land11: Land10 + (0.02i * 19.701),0
1359
1360 arrow from Tx0 to Land0
1361 arrow from Tx0 + (0.02i * 20),0 to Land1
1362 arrow from Tx0 + (0.02i * 40),0 to Land2
1363 arrow from Tx0 + (0.02i * 60),0 to Land3
1364 arrow from Tx0 + (0.02i * 80),0 to Land4
1365 arrow from Tx0 + (0.02i * 100),0 to Land5
1366 arrow from Tx0 + (0.02i * 120),0 to Land6
1367 arrow from Tx0 + (0.02i * 140),0 to Land7
1368 arrow from Tx0 + (0.02i * 160),0 to Land8
1369 arrow from Tx0 + (0.02i * 180),0 to Land9
1370 arrow from Tx0 + (0.02i * 200),0 to Land10
1371 arrow from Tx0 + (0.02i * 220),0 to Land11
1372 .PE
1373 .ce
1374 Figure 9: Period of high IP latency followed by lower latency
1375 .KE
1376 .PP
1377 If the flow-starting fill level is set to 2 (the default),
1378 the total depth of the active subbuf will alternate between 1 and 2
1379 during the initial high latency phase: increase to 2 on each RTP packet
1380 arrival, then go back down to 1 when the subsequent output poll tick
1381 consumes the packet in the head slot.
1382 However, following the transition from higher to lower IP network path
1383 latency while \fBtwjit\fP is in \fBFLOWING\fP state, if the arriving
1384 packets land where they do in Figure\ 9,
1385 the subsequent total depth of the same active subbuf will alternate
1386 between 3 and 4: rise to 4 when ``bunched up'' packets arrive,
1387 then go down to 3 on each output poll tick and go back up to 4
1388 as new RTP packets arrive in between those ticks.
1389 .PP
1390 The end result of such happenings is increased buffer-added latency
1391 \(em a standing queue \(em
1392 in the steady flow state after the latency of the IP network path went down.
1393 In the present example, the standing queue latency added as a lasting
1394 artifact of earlier network conditions at flow starting time is 40\ ms \(em
1395 but it can be greater or smaller, in 20\ ms increments, depending on how
1396 the IP network path changes between initial acquisition of a new RTP flow
1397 and its subsequent steady state.
1398 .PP
1399 The design of \fBtwjit\fP includes a mechanism for thinning these standing
1400 queues, gradually bringing buffer-added latency down to a maximum limit
1401 set by the administrator.
1402 The controlling parameter is the high water mark fill level;
1403 it is the second number given on the \&\fCbuffer\-depth\fP vty
1404 configuration line.
1405 This parameter must be greater than or equal to the flow-starting fill level,
1406 but it takes effect only in \fBFLOWING\fP state, not in \fBHUNT\fP.
1407 Any time the total depth of the flowing-out subbuf exceeds this high water
1408 mark on an output poll tick, tested just before consuming the head slot
1409 and any RTP packet contained therein,
1410 the standing queue thinning mechanism kicks in.
1411 This mechanism deletes every \fIN\^\fPth packet from the stream being thinned,
1412 where \fIN\fP is the \&\fCthinning\-interval\fP parameter set in vty config,
1413 for as long as the total depth of the flowing-out subbuf exceeds the
1414 high water mark fill level.
1415 More precisely, every \fIN\^\fPth output poll tick that happens in the state
1416 of high water mark being exceeded advances the head slot by two quantum
1417 units instead of one, with the first consumed head slot always discarded
1418 and the second consumed head slot passed to the output.
1419 This operation remains the same irrespective of whether each of the two
1420 thus consumed head slots holds a previously received RTP packet or is empty.
1421 .PP
1422 The act of deleting a quantum from the middle of an ongoing stream of
1423 speech or CSData is always disruptive: in the case of speech, a 20\ ms
1424 quantum, potentially in the middle of a speaker talking, suddenly disappears;
1425 in the case of CSData, the effect may be even worse with a potentially
1426 important data chunk likewise disappearing, plus a 20\ ms phase shift
1427 in the stream is always incurred.
1428 However, such disruptions are the only way to bring down a standing queue,
1429 whose added latency is another form of evil \(em thus as usual,
1430 engineering is all about trade-offs and compromises.
1431 .PP
1432 The default configuration for \fBtwjit\fP is
1433 \&\fCbuffer\-depth\|\|\|2\|\|\|4\fP and \&\fCthinning\-interval\|\|\|17\fP:
1434 the default high water mark fill level is 4,
1435 and the default thinning interval is to delete every 17th packet, i.e.,
1436 delete one 20\ ms quantum every 340\ ms.
1437 The exact scenario depicted in Figure\ 9 will not invoke the standing queue
1438 thinning mechanism with these default settings: in this depicted scenario,
1439 the total depth of the active subbuf rises to 4 at its highest,
1440 which is also the default high water mark fill level.
1441 However, if this high water mark setting is lowered or if the latency spike
1442 at the beginning of the flow is more substantial, then the standing queue
1443 thinning mechanism will kick in when the IP latency spike subsides.
1444 .PP
1445 The default thinning interval of deleting every 17th packet was chosen
1446 based on these considerations:
1447 .IP a)
1448 340\ ms is long enough to where 20\ ms quantum deletions spaced this far
1449 apart should be tolerable, yet short enough to where a standing queue
1450 would be reduced to the high water mark in reasonable time;
1451 .IP b)
1452 17 is a prime number, thereby reducing the probability that the thinning
1453 mechanism will interfere badly with intrinsic features of the stream
1454 being thinned.
1455 .PP
1456 The default high water mark fill level was chosen so as to provide some
1457 margin above the flow-starting fill level (allow IP network path latency
1458 variations without needless thinning followed by underrun and reacquisition),
1459 while still maintaining a constraint against unbounded growth of a
1460 standing queue.
1461 As always, the optimal engineering trade-off will depend very strongly
1462 on the actual characteristics of the IP network environment on top of which
1463 a GSM network or IP-PSTN system is being built, hence careful attention is
1464 required from the managing operator.
1465 .NH 4
1466 Guard against time traveler packets
1467 .PP
1468 Every time a new RTP packet is received in \fBFLOWING\fP state, a series
1469 of checks are made to answer this question: should the newly received packet
1470 be treated as a continuation of the current flow, or should it be
1471 treated as belonging to a new flow and thus a handover event per \(sc2.1?
1472 The first check is SSRC comparison: if the newly received RTP packet has a
1473 different SSRC than the currently active flow, it is a handover.
1474 .PP
1475 The RTP timestamp is checked next.
1476 In order to handle arbitrary starting 32-bit timestamps and wraparound of
1477 the absolute 32-bit timestamp value at any point, \fBtwjit\fP code computes
1478 the difference between current subbuf \&\fChead_ts\fP (subtrahend) and
1479 the newly received timestamp (minuend),
1480 and treats this difference as a signed 32-bit integer.
1481 If this difference is negative, the newly received packet is treated as a
1482 stale (too old) one, received with so much delay that it can no longer be
1483 accepted: \&\fCtoo_old\fP stats counter is incremented, and the packet in
1484 question is discarded without further processing.
1485 After this check the timestamp increment, now confirmed to be non-negative,
1486 is checked to see if it is an integral multiple of the samples-per-quantum
1487 constant, which is usually 160.
1488 If this integral multiple constraint is violated,
1489 the new packet cannot belong to the same flow as the currently active one,
1490 and it is necessary to invoke handover handling.
1491 .PP
1492 After these two checks, one final check is needed for robustness: a guard
1493 against time traveler packets.
1494 If the increment between current \&\fChead_ts\fP and the timestamp field
1495 in the newly received RTP packet is positive after wraparound handling,
1496 if it is an integral multiple of the samples-per-quantum constant,
1497 but it is excessively large (at 8000 timestamp units per second,
1498 the largest possible post-wraparound-handling RTP timestamp increment
1499 is just over 3 days into the future),
1500 such aberrant RTP packets are jocularly referred to as time travelers.
1501 .PP
1502 Assuming that actual time travel either does not exist at all
1503 or at least does not happen in the present context,
1504 we reason that when such ``time traveler'' RTP packets do arrive,
1505 we must be dealing with the effect of a software bug or misdesign
1506 or misconfiguration in whatever foreign network element is sending us RTP.
1507 In any case, irrespective of the cause, we must be prepared for the
1508 possibility of seeming ``time travel'' in the incoming RTP stream.
1509 We implement an arbitrary threshold: if the received RTP timestamp
1510 is too far into the future, we treat that packet as the
1511 beginning of a new flow, same as SSRC change or non-quantum
1512 timestamp increment, and invoke handover handling.
1513 .PP
1514 The threshold that guards against time traveler packets has 1\ s granularity,
1515 which is sufficient for its intended purpose of catching gross errors.
1516 It is set with \&\fCmax\-future\-sec\fP vty configuration line.
1517 The default value is 10\ s: very generous, perhaps overly so,
1518 to networks with really bad latency.
1519 .NH 3
1520 Handling of packet loss and gaps
1521 .PP
1522 The design of RTP makes it impossible to distinguish between packet loss
1523 and intentional gaps in real time:
1524 if a packet fails to arrive at the time when it is expected and needed
1525 on the receiving end,
1526 the receiver has no way of knowing \fIat that moment\fP
1527 whether the cause of this lack of packet arrival is an intentional gap
1528 emitted by the sender or packet loss along the way.
1529 This distinction can be made later, after the fact: when subsequent
1530 packets arrive, the receiver can examine the sequence number field
1531 in the RTP header and thereby determine which of the two events
1532 (intentional gap or packet loss) happened previously.
1533 However, because this knowledge is not available at the time when it would
1534 be needed, \fBtwjit\fP makes no distinction between these two possibilities
1535 outside of analytics.
1536 For the purpose of mapping received RTP packets to ticks of the fixed timing
1537 system on the output side of \fBtwjit\fP,
1538 any intentional gaps in the incoming RTP stream are treated indistinguishably
1539 from packet loss.
1540 (Please recall from \(sc1.2.1 that \fBtwjit\fP is designed for use with
1541 continuous streaming, not intentional gaps.)
1542 The operational (as opposed to analytics) part of \fBtwjit\fP looks only
1543 at SSRC and timestamp fields in the RTP header; it does not consider
1544 the sequence number at all.
1545 .PP
1546 For the sake of completeness, it should be noted that some other
1547 implementations of RTP stream Rx may invoke more sophisticated handling of
1548 DTX intentional gaps in real time by examining RTP packet payload and
1549 checking for SID frames that are supposed to precede speech pauses in DTX.
1550 However, this approach is not used in \fBtwjit\fP because it would
1551 constitute a layering violation: by design, \fBtwrtp\fP and its \fBtwjit\fP
1552 component are completely agnostic to payload formats.
1553 .PP
1554 The actual effect of a gap in the received RTP stream,
1555 whether intentional or caused by packet loss,
1556 depends on the size of the gap
1557 (how many consecutive packets are lost or omitted)
1558 and jitter buffer conditions at the time of its occurrence.
1559 The critical question is whether or not the gap in RTP reception
1560 incurs an underrun:
1561 .IP \(bu
1562 If the gap is small enough, or the running depth of the jitter buffer
1563 is high enough, to where no underrun occurs, then the gap is presented
1564 to the application on the output side of \fBtwjit\fP without distortion:
1565 the application will receive \fBNULL\fP (absence of received packet)
1566 in those quanta whose corresponding RTP packets (corresponding per RTP
1567 timestamp) were not received, while all packets which did arrive will
1568 be delivered correctly, each at its respective quantum point.
1569 No phase shift is incurred in the received stream of media.
1570 .IP \(bu
1571 If the gap results in an underrun,
1572 subsequent received packets after this gap will proceed as acquisition
1573 of a new flow, passing through \fBHUNT\fP state before entering \fBFLOWING\fP.
1574 Preservation of phase cannot be guaranteed under such conditions,
1575 i.e., the gap as perceived by the fixed timing application may be lengthened
1576 or shortened compared to the actual number of RTP packets lost or omitted.
1577 .IP \(bu
1578 If a gap incurs an underrun, then there is a single RTP packet following
1579 this gap, then another gap, the lone RTP packet between the two gaps
1580 will be dropped, assuming the default configuration with flow-starting fill
1581 level set to 2.
1582 This drop occurs because a single RTP packet following an underrun is not
1583 sufficient to establish a new flow when the flow-starting fill level
1584 is greater than 1,
1585 and when another RTP packet arrives after the second gap, the first
1586 between-gaps packet will be too stale.
1587 This failure scenario is the reason why the combination of \fBtwjit\fP,
1588 DTX and intentional gaps will not work, unless one enables the underrun
1589 extension feature described in the following section.
1590 .LP
1591 If \fBtwjit\fP is deployed in an IP network environment where packet loss
1592 occurs frequently enough to be a concern,
1593 it may be necessary to increase the amount of buffering (by increasing
1594 the flow-starting fill level) so that packet loss events do not turn
1595 into underruns.
1596 However, intentional gaps are always bad in principle and should be avoided
1597 \(em enable continuous streaming instead.
1598 .NH 4
1599 Optional underrun extension mechanism
1600 .PP
1601 As already covered in \(sc1.2.1, the original design of classic \fBtwjit\fP
1602 thoroughly assumes continuous streaming, rather than intentional gaps.
1603 However, there is now a desire among Osmocom developers to replace the use
1604 of Belledonne \fBortp\fP library in OsmoBTS with \fBtwrtp\fP and \fBtwjit\fP,
1605 which raises a new complication: intentional gaps are called for by 3GPP
1606 specs, are the default behavior for uplink RTP output from OsmoBTS,
1607 and are supported by \fBortp\fP by virtue of being a core feature of RTP
1608 as envisioned by IETF.
1609 .PP
1610 A total redesign of \fBtwjit\fP for the paradigm of intentional gaps
1611 would be beyond the ability of its author; furthermore, the classic \fBtwjit\fP
1612 algorithm optimized for continuous streaming is still desirable for those
1613 network deployments (Osmocom+ThemWi hybrid) whose operators do not hold
1614 themselves strictly to IETF ideals or 3GPP stipulations, but instead prefer
1615 to emulate the paradigm of TDM as closely as possible over IP.
1616 Ideally there would be two alternative jitter buffer implementations,
1617 \fBtwjit\fP for continuous streaming and an alternative implementation
1618 for intentional gaps, selectable under the same \fBtwrtp\fP top layer.
1619 However, until someone develops that alternative jitter buffer implementation,
1620 \fBosmo_twjit\fP includes a compromise mechanism called underrun extension.
1621 This mechanism improves \fBtwjit\fP performance in the presence of DTX
1622 effected via intentional gaps, making it acceptable in many cases,
1623 but still not perfectly reliable under all conditions.
1624 .PP
1625 Underrun extension mechanism is enabled via \&\fCunderrun\-extension\fP
1626 optional setting in vty config.
1627 This vty setting includes a numeric argument that sets the maximum number
1628 of RTP packets that can be omitted before a soft underrun turns into
1629 a hard underrun.
1630 When this mode is enabled, underrun handling previously described in
1631 \(sc2.3.4.1 is modified as follows: when an output poll occurs on a \fBtwjit\fP
1632 instance in \%\fBFLOWING\fP state and the flowing-out subbuf is empty,
1633 instead of transitioning into \fBEMPTY\fP state, the instance transitions
1634 into newly added \%\fBUNDERRUN\fP state that exists only when this mechanism
1635 is enabled.
1636 If \&\fCunderrun\-extension\fP is set to 1, \%\fBUNDERRUN\fP state persists
1637 for 1 tick; if \&\fCunderun\-extension\fP is set to 2, \%\fBUNDERRUN\fP state
1638 persists for 2 ticks, and so forth.
1639 While \%\fBUNDERRUN\fP state persists, \&\fChead_ts\fP in the now-empty
1640 flowing-out subbuf continues to increment by 160 on every tick just like
1641 it did in \%\fBFLOWING\fP state, making \%\fBUNDERRUN\fP state a special
1642 extension of \%\fBFLOWING\fP.
1643 Exit conditions from \%\fBUNDERRUN\fP state are as follows:
1644 .IP 1)
1645 If a new RTP packet arrives whose timestamp equals or is ahead of
1646 still-advancing \&\fChead_ts\fP, the overall state goes back to
1647 \%\fBFLOWING\fP.
1648 This case is the desired outcome: the intentional gap as emitted by the
1649 RTP stream source is passed accurately to the fixed timing system on the
1650 output of \fBtwjit\fP (exact number of gapped quantum units preserved)
1651 with no loss of synchronization.
1652 If the RTP stream source emits ``isolated island'' comfort noise update
1653 packets surrounded by gaps, as long as these gaps do not exceed
1654 the configured \%\&\fCunderrun\-extension\fP limit, all of these
1655 CN update packets will be passed to the application on the output side
1656 of \fBtwjit\fP.
1657 .IP 2)
1658 If a packet arrives that would constitute a handover in \%\fBFLOWING\fP
1659 state, or a packet would be considered too old in that state,
1660 the state machine transitions to \fBHUNT\fP and begins acquisition
1661 of the new flow per \(sc2.3.3.
1662 The resulting behavior is exactly the same as if there was no underrun
1663 extension mechanism and the state machine was in \fBEMPTY\fP state
1664 since the initial underrun.
1665 .IP 3)
1666 If the underrun condition persists past the configured
1667 \%\&\fCunderrun\-extension\fP limit with no new RTP packets arriving,
1668 the soft underrun turns into a hard underrun and the state machine
1669 transitions into \fBEMPTY\fP.
1670 .LP
1671 Compared to a hypothetical ideal solution for RTP streams with DTX
1672 represented via intentional gaps, the present solution has this
1673 shortcoming: intentional gaps are handled gracefully only when they are
1674 encountered in \%\fBFLOWING\fP state, but not during initial flow
1675 acquisition, reacquisition after a hard underrun (e.g., extensive packet
1676 loss beyond the maximum allowed intentional gap) or handover handling.
1677 In all of these other listed cases, if the RTP stream source is in a DTX
1678 pause and emits only ``isolated island'' comfort noise update packets,
1679 those CN packets will be dropped, thus failing to deliver correct
1680 CN updates under all possible conditions per DTX ``contract''.
1681 This limitation is the reason why \&\fCrtp\ continuous\-streaming\fP
1682 is still a better choice for all networks where this against-IETF,
1683 against-3GPP setting can be enabled.
1684 .PP
1685 When underrun extension feature needs to be enabled, the numeric parameter
1686 on \%\&\fCunderrun\-extension\fP vty configuration line should be set
1687 according to the maximum intentional gap that can be emitted per DTX rules
1688 associated with the codec(s) in use.
1689 If there is no need to accommodate for packet loss on top of these intentional
1690 gaps, the maximum allowed DTX gap is 23 with FR and EFR codecs, 11 with
1691 HRv1 codec and 7 with AMR codec.
1692 If there is a desire to allow up to one comfort noise update packet to be
1693 lost without incurring a hard underrun, the maximum allowed gap should be
1694 set to 47 with FR and EFR codecs, 23 with HRv1 codec and 15 with AMR codec.
1695 .NH 3
1696 Handling of packet reordering
1697 .PP
1698 As already covered in \(sc2.2, we (Themyscira Wireless) have no operational
1699 experience with packet reordering, as it is a behavior which is not exhibited
1700 by IP networks in our part of the world.
1701 Let us consider nonetheless how \fBtwjit\fP would handle theoretically
1702 envisioned cases of packet reordering that were presented in that section.
1703 .PP
1704 The hypothetical scenario depicted in Figure\ 5 calls for exactly the same
1705 \fBtwjit\fP configuration as the real world scenario of Figure\ 4.
1706 If the IP network frequently exhibits effects like those depicted in
1707 Figure\ 4 and Figure\ 5,
1708 the flow-starting fill level would need to be set to 7.
1709 As long as episodes of packets bunched together, with or without reordering,
1710 are separated by periods of smooth packet flow, \fBtwjit\fP would proceed
1711 through its acquisition stage (\fBHUNT\fP state) in one of these smooth flow
1712 periods, and then episodes of the form depicted in Figure\ 4 or Figure\ 5
1713 would be handled gracefully, without any loss or distortion of transported
1714 media.
1715 .PP
1716 The more fantastical scenario of Figure\ 6 would be handled well by
1717 setting the flow-starting fill level to 4.
1718 Visualizing the state of the sole active subbuf after each RTP packet arrival
1719 and after each output poll tick in that figure is left as an exercise
1720 for the reader \(em however, each media quantum carried by each of the packets
1721 shown in the figure will be delivered to the application on the output side
1722 of \fBtwjit\fP in the correct order, without any loss or distortion.
1723 .NH 3
1724 Handling of handovers
1725 .PP
1726 As covered in \(sc2.1, a handover in \fBtwjit\fP terminology is
1727 a transition from one RTP flow to the next, within the context of a single
1728 RTP stream.
1729 A handover occurs when the incoming RTP stream exhibits a change of SSRC,
1730 a timestamp increment that is not an integral multiple of the
1731 samples-per-quantum constant,
1732 or a ``time travel'' event as described in \(sc2.3.4.3.
1733 .PP
1734 In order to be treated as a handover by \fBtwjit\fP, the newly received RTP
1735 packet that breaks the previous flow in one of the just-listed ways must
1736 arrive while the previous flow (the one it breaks from) is still active,
1737 while the state of the \fBtwjit\fP instance is \fBFLOWING\fP.
1738 If a handover happens after the previous flow underruns, such that the
1739 \fBtwjit\fP instance is in \fBEMPTY\fP or \%\fBUNDERRUN\fP state when
1740 the first packet of the new flow arrives,
1741 acquisition of the new flow proceeds via \fBHUNT\fP state in the same way
1742 whether this new flow is continuous or discontinuous with the previous one.
1743 Similarly, if a flow discontinuity (the kind that would be treated as a
1744 handover if it occurred in \fB\%FLOWING\fP state) occurs in \fBHUNT\fP state,
1745 it is handled by reinitializing the \fBHUNT\fP state, without entering
1746 the special \%\fBHANDOVER\fP state, as detailed in \(sc2.3.3.1.
1747 .PP
1748 True handover handling happens when a flow-breaking RTP packet arrives
1749 in \fBFLOWING\fP state.
1750 This event causes a transition into the dedicated \fBHANDOVER\fP state,
1751 described here.
1752 In this state both subbufs of \fBtwjit\fP are active and valid:
1753 the subbuf that was active in \fBFLOWING\fP state continues to flow out,
1754 while the other subbuf is initialized for the new flow just like in
1755 \fBHUNT\fP state.
1756 Accounting for \fBHANDOVER\fP state, each \fBtwjit\fP instance has
1757 a potentially valid write subbuf and a potentially valid read subbuf,
1758 breaking down as follows:
1759 .IP \(bu
1760 In \fBEMPTY\fP state, there is neither a valid write subbuf nor a valid
1761 read subbuf;
1762 .IP \(bu
1763 In \fBHUNT\fP state, there is a valid write subbuf, but no valid read subbuf;
1764 .IP \(bu
1765 In \fBFLOWING\fP and \fBUNDERRUN\fP states, the sole active subbuf is both
1766 the write subbuf and the read subbuf;
1767 .IP \(bu
1768 In \fBHANDOVER\fP state, the read subbuf and the write subbuf are different,
1769 and each is valid.
1770 .LP
1771 Once \fBHANDOVER\fP state has been entered, the code path that handles
1772 incoming RTP packets operates like it does in \fBHUNT\fP state,
1773 while the output path that executes on ticks of the fixed timing system
1774 operates like it does in \%\fBFLOWING\fP, each operating on its
1775 respective subbuf.
1776 There are two possible exit conditions from this state:
1777 .IP 1)
1778 If the new write subbuf reaches ready state (the same criterion as applied
1779 for transition from \fBHUNT\fP to \fBFLOWING\fP, covered in \(sc2.3.3)
1780 before the old read subbuf underruns,
1781 the \fBtwjit\fP instance transitions from \fBHANDOVER\fP back into
1782 \fBFLOWING\fP state.
1783 Any packets that remain in the old read subbuf are discarded, and the new
1784 write subbuf becomes the sole active subbuf for both reading and writing.
1785 .IP 2)
1786 If the old read subbuf underruns before the new write subbuf is ready
1787 to start flowing out,
1788 a handover underrun occurs (same as a regular underrun, but increments
1789 a different stats counter) and \fBtwjit\fP state transitions to \fBHUNT\fP.
1790 This handover underrun will occur if the new write subbuf does not become
1791 ready quickly enough, as the old read subbuf no longer receives any
1792 new packets in \fBHANDOVER\fP state.
1793 .NH 3
1794 Handling of RTP marker bit
1795 .PP
1796 This feature of \fBosmo_twjit\fP does not originate from Themyscira
1797 and is not present in \fCtwrtp\-native\fP version,
1798 instead it was added to the Osmocom-integrated version of this library
1799 per request of Osmocom reviewers.
1800 Vty configuration parameter \fCmarker\-handling\fP controls how
1801 \fBosmo_twjit\fP should react to incoming RTP packets that have
1802 \fBM\fP bit set.
1803 The two possible settings are \fChandover\fP and \fCignore\fP:
1804 .IP \(bu
1805 If \fCmarker\-handling\fP is set to \fChandover\fP, received
1806 packets with \fBM\fP bit set are treated like SSRC changes:
1807 if the previous state was \%\fBFLOWING\fP, the state of \fBosmo_twjit\fP
1808 instance transitions to \%\fBHANDOVER\fP.
1809 .IP \(bu
1810 If \fCmarker\-handling\fP is set to \fCignore\fP, incoming marker bits
1811 are ignored just like in \fCtwrtp\-native\fP version used by ThemWi
1812 network elements.
1813 .NH 3
1814 Additional notes
1815 .LP
1816 Some additional notes about \fBtwjit\fP design that don't fit anywhere else:
1817 .IP \(bu
1818 Neither the payload type nor any of payload content are checked by \fBtwjit\fP:
1819 all payload handling is the responsibility of the application.
1820 .IP \(bu
1821 RTP packets with zero-length payloads are treated as no different from
1822 other valid packets; such packets may be needed to ensure continuous
1823 streaming, as covered in \(sc1.2.1.
1824 .IP \(bu
1825 Only SSRC and timestamp fields in the RTP header (and possibly the marker bit)
1826 are considered for the purpose of mapping received RTP packets to ticks
1827 of the fixed timing system on the output of \fBtwjit\fP.
1828 The sequence number field is examined only for analytics (see Chapter\ 3),
1829 but not for actual operation.
1830 .NH 2
1831 Summary of configuration parameters
1832 .PP
1833 Applications that use \fBtwjit\fP, usually as part of \fBtwrtp\fP,
1834 are expected to also use Osmocom vty system for configuration.
1835 All tunable configuration parameters for \fBtwjit\fP are gathered into
1836 a config structure, named \%\fCstruct\ osmo_twjit_config\fP in
1837 the present Osmocom-integrated version;
1838 this config structure must be provided every time a \fBtwjit\fP instance
1839 is created.
1840 Every application that uses \fBtwrtp\fP with \fBtwjit\fP is expected
1841 to maintain one or more of these config structures, accessible
1842 to tuning via vty.
1843 Multiple \fBtwjit\fP configuration parameter sets in one application
1844 may be needed if the application creates different kinds of RTP endpoints
1845 that may need different \fBtwjit\fP tunings:
1846 for example, \&\fCtw\-border\-mgw\fP has one \fBtwjit\fP configuration
1847 parameter set for GSM RAN side and another for IP-PSTN side.
1848 Vty configuration for \fBtwjit\fP looks like this
1849 (excerpt from \&\fCtw\-border\-mgw.cfg\fP):
1850 .DS
1851 .ft C
1852 .tr -\-
1853 twjit-gsm
1854 buffer-depth 2 4
1855 thinning-interval 17
1856 max-future-sec 10
1857 twjit-pstn
1858 buffer-depth 2 4
1859 thinning-interval 17
1860 max-future-sec 10
1861 .ft
1862 .DE
1863 .tr --
1864 .LP
1865 All numbers in the example above are defaults for the respective settings.
1866 Individual settings are as follows:
1867 .IP \(bu
1868 \&\fCbuffer\-depth\fP line controls the flow-starting fill level (first number)
1869 and the high water mark fill level (second number), parameters that affect
1870 the amount of latency added by \fBtwjit\fP in return for tolerance to jitter
1871 and longer-term variations in IP network path delay.
1872 The flow-starting fill level is described in detail in \(sc2.3.3;
1873 the high water mark fill level is described in \(sc2.3.4.2.
1874 .IP \(bu
1875 \&\fCthinning\-interval\fP setting controls the interval at which quantum
1876 units are deleted from the received stream when the standing queue
1877 thinning mechanism kicks in \(em see \(sc2.3.4.2 for the detailed description.
1878 .IP \(bu
1879 \&\fCmax\-future\-sec\fP setting adjusts the guard against time traveler
1880 packets, described in detail in \(sc2.3.4.3.
1881 .IP \(bu
1882 \&\fCstart\-min\-delta\fP and \&\fCstart\-max\-delta\fP optional settings
1883 (each can be set or unset)
1884 allow the managing operator to set additional timing constraints
1885 that need to be met in order to start a new flow: see \(sc2.3.3.2
1886 for full details.
1887 .IP \(bu
1888 \&\fCunderrun\-extension\fP setting is described in \(sc2.3.5.1.
1889 .IP \(bu
1890 \&\fCmarker\-handling\fP setting is described in \(sc2.3.8.
1891 .NH 1
1892 Stats and analytics
1893 .PP
1894 Every \fBtwjit\fP instance maintains a set of statistical counters,
1895 collected into \&\fCstruct\ osmo_twjit_stats\fP in the present
1896 Osmocom-integrated version.
1897 The purpose of these counters is to assist network operations staff:
1898 applications that use \fBtwrtp\fP with \fBtwjit\fP are expected
1899 to provide vty introspection commands that display these statistical
1900 counters in real time for ongoing calls or connections,
1901 and then log any non-zero counters at call completion.
1902 Implementors of new applications are encouraged to examine the source
1903 for \%\fCtw\-border\-mgw\fP or \%\fCtw\-e1abis\-mgw\fP,
1904 C modules named \%\fCend_stats.c\fP and \%\fCintrospect.c\fP,
1905 for an example of how these functions should be implemented.
1906 .PP
1907 The rest of this chapter provides a description of every counter
1908 in \fBtwjit\fP stats structure.
1909 .NH 2
1910 Normal operation counters
1911 .PP
1912 The following counters record events that are expected to occur in
1913 normal operation, in the absence of any errors or adverse conditions:
1914 .sp .3
1915 .de St
1916 .IP \fC\\$1\fP 22
1917 ..
1918 .St rx_packets
1919 This counter increments for every packet that was fed to \fBtwjit\fP input
1920 and passed the basic RTP header validity check.
1921 .St delivered_pkt
1922 This counter increments for every packet that was pulled from the head of
1923 a read subbuf for delivery to the fixed timing application
1924 on the output side of \fBtwjit\fP.
1925 .St handovers_in
1926 This counter increments when \fBtwjit\fP state transitions from
1927 \fB\%FLOWING\fP into \fB\%HANDOVER\fP, as described in \(sc2.3.7.
1928 .St handovers_out
1929 This counter increments when \fBtwjit\fP state transitions from
1930 \fB\%HANDOVER\fP back to \fB\%FLOWING\fP \fIwithout\fP incurring a handover
1931 underrun first, i.e., when the new (post-handover) packet flow becomes ready
1932 to flow out before the old one underruns.
1933 .St marker_resets
1934 This counter increments when a packet is received with \fBM\fP bit set
1935 other than in \fB\%EMPTY\fP state,
1936 and this \fBosmo_twjit\fP instance is configured to treat it as a flow reset,
1937 causing a handover or a reset of flow acquisition process.
1938 .NH 2
1939 Adverse event counters
1940 .LP
1941 The following counters record events that are undesirable,
1942 but not totally unexpected:
1943 .sp .3
1944 .St too_old
1945 This counter increments when an RTP packet received in \fBFLOWING\fP state
1946 has a timestamp that precedes the active subbuf's \&\fChead_ts\fP.
1947 This event can only occur if some packet reordering took place, such that
1948 an earlier-sent packet arrived later than a later-sent one,
1949 or if the buffer is in ``pre-underrun'' state (see \(sc2.3.4.1)
1950 and the very last RTP packet that just flowed out is duplicated.
1951 .St underruns
1952 This counter increments when a \fBFLOWING\fP state underrun occurs,
1953 followed by reception of
1954 at least one post-underrun RTP packet that is then treated as the beginning
1955 of a new flow \(em see \(sc2.3.4.1.
1956 .St ho_underruns
1957 This counter increments when an underrun occurs in \fBHANDOVER\fP state,
1958 i.e., when the previous flow underruns before the new one is ready to
1959 start flowing out.
1960 .St soft_underruns
1961 This counter increments when the underrun extension mechanism of \(sc2.3.5.1
1962 is enabled and a soft underrun occurs, a transition from \%\fBFLOWING\fP state
1963 into \%\fBUNDERRUN\fP state.
1964 .St output_gaps
1965 This counter increments for every gap in the output stream from \fBtwjit\fP
1966 that occurs in \fBFLOWING\fP or \fBHANDOVER\fP state \fIwithout\fP an underrun,
1967 i.e., with the flow still continuing and further queued packets present
1968 past the gap.
1969 .St thinning_drops
1970 This counter increments when the standing queue thinning mechanism
1971 described in \(sc2.3.4.2 deletes a quantum from the stream delivered
1972 to the application on the output of \fBtwjit\fP.
1973 Please note that \&\fCdelivered_pkt\fP or \&\fCoutput_gaps\fP
1974 is still incremented for the quantum that is pulled from the read subbuf,
1975 but then artificially deleted by the thinning mechanism.
1976 .NH 2
1977 Error counters
1978 .LP
1979 The following counters record truly unusual and unexpected error events:
1980 .sp .3
1981 .St bad_packets
1982 This counter increments when a packet that was fed to \fBtwjit\fP input
1983 is too short for RTP (shorter than the minimum RTP header length of 12 bytes)
1984 or has an unknown value in the RTP version field.
1985 .St duplicate_ts
1986 This counter increments when \fBtwjit\fP attempts to add a newly received
1987 RTP packet to the active-for-write subbuf, but a previous packet is already
1988 held with the same timestamp and thus at the same depth position.
1989 For the sake of implementation simplicity in this error case that should not
1990 occur in a correctly working system, \fBtwjit\fP drops the new packet
1991 and keeps the old one.
1992 .NH 2
1993 Independent analytics
1994 .PP
1995 In addition to its main function of mapping received RTP packets
1996 to ticks of the fixed timing system on its output,
1997 \fBtwjit\fP performs some ``raw'' analytics on the stream of RTP packet
1998 it receives.
1999 These analytic steps are independent of \fBtwjit\fP algorithm details,
2000 of any configuration settings summarized in \(sc2.4, and
2001 independent of what happens on the output (fixed timing) side of \fBtwjit\fP
2002 \(em thus they bear no direct relation to \fBtwjit\fP state transitions,
2003 subbuf conditions and so forth.
2004 Instead these analytics depend only on the shape of the incoming RTP stream
2005 itself, same as if an analyst were looking at a pcap file after the fact.
2006 Some of these analytic steps are performed in order to gather information
2007 for the purpose of generating RTCP reception report blocks
2008 (see Chapter\ 5),
2009 but some simple analytic steps are done solely to produce some additional
2010 stats counters that are expected to be valuable to network operations staff.
2011 The following counters are maintained as part of these independent analytics:
2012 .sp .3
2013 .St ssrc_changes
2014 This counter increments when the received RTP stream exhibits a change of SSRC,
2015 or more precisely, every time an RTP packet arrives whose SSRC differs from
2016 that of the packet received just prior.
2017 .LP
2018 All following counters record events that occur within a same-SSRC substream:
2019 .sp .3
2020 .St seq_skips
2021 This counter increments every time a packet is received whose sequence number
2022 increment (over the packet received just prior) is positive and greater than 1.
2023 Such occurrence indicates either packet loss or reordering in the IP network.
2024 .St seq_backwards
2025 This counter increments every time a packet is received whose sequence number
2026 goes backward, relative to the packet received just prior.
2027 Such occurrence indicates packet reordering in the IP network.
2028 .St seq_repeats
2029 This counter increments every time a packet is received whose sequence number
2030 is the same as the packet received just prior.
2031 Such occurrence indicates packet duplication somewhere.
2032 .St intentional_gaps
2033 This counter increments every time a packet is received whose sequence number
2034 increments by 1 over the packet received just prior,
2035 indicating no packet loss or reordering at the hands of the transited
2036 IP network,
2037 the timestamp increment is positive and an integral multiple of the
2038 samples-per-quantum constant,
2039 but this increment does not equal exactly one quantum.
2040 Such occurrence indicates that the RTP stream sender emitted
2041 an intentional gap.
2042 .St ts_resets
2043 This counter increments every time a packet is received whose sequence number
2044 increments by 1 over the packet received just prior,
2045 but the timestamp relation between this packet and the previous one
2046 is neither the expected single quantum increment
2047 nor an increment of multiple quanta consistent with an intentional gap.
2048 .St jitter_max
2049 This reporting variable is not a counter, but a quantitative measure.
2050 It reports the highest interarrival jitter that was encountered within
2051 the present same-SSRC substream, measured as prescribed by RFC\ 3550:
2052 the absolute value of the difference between the timestamp delta
2053 of two adjacently-received packets and the \(*D in time of arrival,
2054 converted from seconds and nanoseconds to RTP timestamp units.
2055 .NH 1
2056 RTP endpoint functionality
2057 .PP
2058 The previous two chapters covered \fBtwjit\fP, the jitter buffer component
2059 of ThemWi RTP endpoint implementation.
2060 However, this \fBtwjit\fP layer is not expected to be used directly
2061 by applications: an application that needs to implement an RTP endpoint
2062 will need an endpoint implementation that actually sends and receives
2063 RTP packets, and possibly RTCP as well.
2064 The top layer of ThemWi RTP endpoint library, named \fBtwrtp\fP,
2065 provides this functionality.
2066 .PP
2067 This chapter describes the API to \fBosmo_twrtp\fP, the version of
2068 \fBtwrtp\fP layer that has been integrated into \fClibosmo\-netif\fP.
2069 .NH 2
2070 Endpoint life cycle
2071 .PP
2072 Every \fBtwrtp\fP endpoint is represented by opaque \&\fCstruct\ osmo_twrtp\fP,
2073 which is a talloc context.
2074 These endpoints are created with \&\fCosmo_twrtp_create()\fP and freed
2075 with \&\fCosmo_twrtp_destroy()\fP.
2076 Every \fBtwrtp\fP instance (\&\fCstruct\ osmo_twrtp\fP) owns
2077 the two UDP sockets that are bound to RTP and RTCP ports,
2078 their corresponding \&\fCstruct\ osmo_io_fd\fP instances,
2079 the subordinate \fBtwjit\fP instance if one exists,
2080 and any buffered packets.
2081 All of these resources are released upon \&\fCosmo_twrtp_destroy()\fP.
2082 .NH 2
2083 Supplying UDP sockets for RTP and RTCP
2084 .PP
2085 For every \fBtwrtp\fP endpoint, there is one file descriptor referring
2086 to the UDP socket to be used for RTP, and another file descriptor
2087 referring to the UDP socket to be used for RTCP.
2088 How do these UDP sockets and file descriptors come into being?
2089 Two ways are supported:
2090 .IP 1)
2091 In self-contained Osmocom applications where \fBtwrtp\fP is to be made
2092 available as an alternative to Belledonne \fBortp\fP,
2093 as well as \%\fCtw\-e1abis\-mgw\fP fitting in the place of OsmoMGW-E1,
2094 \&\fCosmo_twrtp_bind_local()\fP (or its \fCtwrtp\-native\fP equivalent
2095 in the case of \%\fCtw\-e1abis\-mgw\fP) creates both sockets and binds them
2096 to a specified IP:port address, supporting both IPv4 and IPv6
2097 and automatically incrementing the port number by 1 for RTCP.
2098 .IP 2)
2099 In Themyscira Wireless CN environment, there is a separate daemon process
2100 that manages the pool of local UDP ports for RTP+RTCP pairs,
2101 and that daemon passes allocated sockets to its clients
2102 via UNIX domain socket file descriptor passing mechanism.
2103 A network element that uses this mechanism will receive a pair of
2104 file descriptors for already-bound UDP sockets from \&\fCthemwi\-rtp\-mgr\fP;
2105 these two already-allocated and already-bound UDP socket
2106 file descriptors are then passed to a dedicated \fBtwrtp\fP API function.
2107 In \fCtwrtp\-native\fP this API function is \&\fCtwna_twrtp_supply_fds()\fP;
2108 the present Osmocom-integrated version provides an equivalent in the form of
2109 \&\fCosmo_twrtp_supply_fds()\fP.
2110 .LP
2111 Either way, the two UDP sockets and their file descriptors
2112 are then owned by the containing \fBtwrtp\fP instance, and will be closed
2113 upon \&\fCosmo_twrtp_destroy()\fP.
2114 .NH 2
2115 RTP remote address
2116 .PP
2117 The two UDP sockets for RTP and RTCP always remain unconnected
2118 at the kernel level \(em instead the notion of the remote peer address
2119 is maintained by \fBtwrtp\fP library.
2120 This remote address needs to be set with \&\fCosmo_twrtp_set_remote()\fP;
2121 until it is set, no RTP or RTCP packets can be sent or received.
2122 Once the remote address is set, the library will send outgoing RTP and RTCP
2123 packets to the correct destination,
2124 and the same remote address is also used to filter incoming packets:
2125 incoming RTP and RTCP packets are accepted only if the UDP source address
2126 matches the currently set remote peer.
2127 This remote peer address can be changed as needed throughout
2128 the lifetime of the RTP endpoint.
2129 .NH 2
2130 RTP receive path
2131 .PP
2132 Applications using \fBtwrtp\fP can receive incoming RTP packets in two ways:
2133 with or without \fBtwjit\fP.
2134 Every application that uses \fBtwrtp\fP must decide, at the time of endpoint
2135 creation via \&\fCosmo_twrtp_create()\fP,
2136 whether or not this endpoint should be equipped with \fBtwjit\fP;
2137 if a \fBtwjit\fP instance is needed along with \fBtwrtp\fP,
2138 the application must provide a \&\fCstruct\ osmo_twjit_config\fP
2139 \(em see \(sc2.4.
2140 .PP
2141 API functions for receiving incoming RTP traffic via \fBtwjit\fP, namely
2142 \&\fCosmo_twrtp_twjit_rx_ctrl()\fP and \&\fCosmo_twrtp_twjit_rx_poll()\fP,
2143 can be used only on \fBtwrtp\fP endpoints that were created with \fBtwjit\fP
2144 included \(em
2145 however, the other RTP Rx API, namely \&\fCosmo_twrtp_set_raw_rx_cb()\fP
2146 for non-delayed unbuffered Rx path, is available with all \fBtwrtp\fP
2147 endpoints.
2148 Applications are allowed to mix \fBtwjit\fP and raw Rx paths:
2149 if a raw Rx callback is set, that callback function is called first
2150 for every received packet, and it can either consume the \fBmsgb\fP
2151 passed to it, or leave it alone.
2152 If the callback function returns \fBtrue\fP, indicating that it consumed
2153 the \fBmsgb\fP, \fBtwrtp\fP Rx processing ends there;
2154 if it returns \fBfalse\fP, or if there is no raw Rx callback installed,
2155 then the packet is passed to \fBtwjit\fP if present and enabled,
2156 otherwise it is discarded.
2157 .PP
2158 The ability to use both \fBtwjit\fP and the non-delayed unbuffered Rx path
2159 at the same time is particularly useful for speech transcoder implementations
2160 that support AMR codec on the RAN side:
2161 such TC will use \fBtwjit\fP to feed the incoming RTP stream
2162 to the speech decoder function that runs on fixed timing, but the
2163 non-delayed Rx path can also be used to ``peek'' at received RTP packets
2164 as they come in and extract the CMR field \(em to be fed to the speech
2165 encoder element, which is separate from the speech decoder fed via \fBtwjit\fP.
2166 .NH 2
2167 RTP Tx output
2168 .PP
2169 The primary purpose of \fBtwrtp\fP library is to facilitate
2170 implementation of bidirectional interfaces
2171 between an RTP stream and a fixed timing system such as
2172 GSM Um TCH or T1/E1 TDM.
2173 Most of the work is in receiving the incoming RTP stream
2174 and mapping incoming RTP packets to ticks of the fixed timing system,
2175 as covered in Chapter\ 2 \(em however, output from the fixed timing system
2176 to RTP also requires some consideration.
2177 .NH 3
2178 Choice of output SSRC
2179 .PP
2180 A random Tx SSRC is assigned to each \fBtwrtp\fP endpoint when it is
2181 created with \&\fCosmo_twrtp_create()\fP.
2182 No loop detection or SSRC collision logic is implemented:
2183 if it so happens that both ends of the RTP link pick the same SSRC,
2184 no adverse effects will occur for \fBtwrtp\fP.
2185 If the foreign RTP implementation on the other end does object
2186 to SSRC collisions and applies some logic along the lines of
2187 RFC\ 3550 \(sc8.2, it is welcome to change its SSRC:
2188 on \fBtwrtp\fP receiving end such incoming SSRC changes will be treated
2189 by \fBtwjit\fP like any other handover.
2190 However, the SSRC emitted by the local \fBtwrtp\fP end will remain
2191 the same throughout the lifetime of the endpoint.
2192 .NH 3
2193 Starting, stopping and restarting Tx flow
2194 .PP
2195 The initial Tx flow is established when the application calls
2196 \&\fCosmo_twrtp_tx_quantum()\fP for the first time on a given endpoint.
2197 At this point the initial RTP timestamp for this Tx flow is set,
2198 based on the current UTC time (CLOCK_REALTIME) reading plus an
2199 optional random addend.
2200 The current UTC reading at the moment of Tx flow start is used,
2201 rather than a purely random number, for consistency with timestamp
2202 computation in the case of restart, as we shall see momentarily.
2203 Once the flow is started in this manner,
2204 the application must commit to calling \&\fCosmo_twrtp_tx_quantum()\fP
2205 or \&\fCosmo_twrtp_tx_skip()\fP every 20\ ms without fail;
2206 each of those calls will increment the timestamp by the samples-per-quantum
2207 constant, usually 160.
2208 .PP
2209 If this Tx flow continues uninterrupted for the lifetime of the RTP
2210 endpoint, the receiving end will see a timestamp increment of one quantum
2211 in every successive packet, forming a perfectly continuous flow.
2212 In this case the starting absolute value of the RTP timestamp does not
2213 matter at all; the UTC-based starting timestamp derivation used by \fBtwrtp\fP
2214 is indistinguishable from a random number.
2215 But what if the sending endpoint needs to interrupt and then restart
2216 its output?
2217 .PP
2218 A dedicated mechanism is provided for such restarts after interruption.
2219 If an application stops emitting packets via \&\fCosmo_twrtp_tx_quantum()\fP
2220 but later restarts, it must call \&\fCosmo_twrtp_tx_restart()\fP any time
2221 between the last quantum Tx call of the old flow and the first such call
2222 of the new flow.
2223 When \&\fCosmo_twrtp_tx_quantum()\fP is called with the internal restart
2224 flag set, a timestamp reset is performed.
2225 The new timestamp is computed from the current UTC reading just like
2226 on initial Tx start, but then the resulting delta relative to timestamps
2227 of the previous flow is checked, and the new timestamp may be adjusted
2228 so that the timestamp increment seen by the remote peer is always positive
2229 per 32-bit timestamp wraparound rules, and is \fBnot\fP an integral multiple
2230 of the samples-per-quantum constant.
2231 The resulting effect is that the far end will see a discontinuity which
2232 \fBtwjit\fP would treat as a handover, yet the increment of the RTP timestamp
2233 over this discontinuity gap is a best effort approximation of the actual
2234 time difference.
2235 .NH 3
2236 Ability to emit intentional gaps
2237 .PP
2238 As already covered in other parts of this document, Themyscira Wireless
2239 philosophy is opposed to the practice of intentional gaps in an RTP stream,
2240 and \fBtwjit\fP receiver performs suboptimally in the presence of such.
2241 However, \fBtwrtp\fP must be able to function as a drop-in replacement
2242 for Belledonne \fBortp\fP library in the context of OsmoBTS application;
2243 OsmoBTS defaults to intentional gaps unless
2244 \&\fCrtp\ continuous\-streaming\fP vty option is set.
2245 Therefore, \fBtwrtp\fP library provides the necessary support for
2246 emitting intentional gaps: it is \&\fCosmo_twrtp_tx_skip()\fP function.
2247 .NH 3
2248 Setting RTP marker bit
2249 .PP
2250 In an environment that uses continuous streaming (no intentional gaps),
2251 Themyscira recommendation is to set \fBM\fP bit to 1 on the very first
2252 emitted RTP packet and on the first packet following a restart
2253 (induced discontinuity), and set it to 0 on all other packets.
2254 To produce this behavior with \fBtwrtp\fP, pass the first two Boolean
2255 arguments to \&\fCosmo_twrtp_tx_quantum()\fP as \fBfalse\fP and \fBtrue\fP.
2256 For other policies with respect to setting the \fBM\fP bit
2257 (for example, as would be needed when using \fBtwrtp\fP in the place of
2258 \fBortp\fP in OsmoBTS),
2259 see \&\fCosmo_twrtp_tx_quantum()\fP API documentation.
2260 .NH 2
2261 No-delay forwarding between RTP endpoints
2262 .PP
2263 The present library supports building applications that forward RTP
2264 packets from one \fBtwrtp\fP endpoint to another without passing through
2265 \fBtwjit\fP and thus without adding buffering delay.
2266 To establish such a shortcut path, register a raw (unbuffered) RTP
2267 receiver on one endpoint via \&\fCosmo_twrtp_set_raw_rx_cb()\fP,
2268 and in that callback function, pass the \fBmsgb\fP to
2269 \&\fCosmo_twrtp_tx_forward()\fP.
2270 Such cross-connect may be applied in one or both directions as needed.
2271 .PP
2272 Each endpoint that is involved in such cross-connection can switch
2273 at any time between forwarding packets as just described and
2274 emitting internally generated in-band tones or announcements;
2275 the latter should be emitted with \&\fCosmo_twrtp_tx_quantum()\fP,
2276 and be sure to also call \&\fCosmo_twrtp_tx_restart()\fP between
2277 separate episodes of locally generated output.
2278 The receiving RTP end will see handover events as SSRC switches between
2279 the one emitted by \fBtwrtp\fP and the one coming from the other remote party.
2280 Actual timing will also switch, as there is no realistic way that your own
2281 20\ ms timing for announcement playout will exactly match the timing of the
2282 RTP stream switched from the other remote party.
2283 .NH 1
2284 Support for RTCP
2285 .PP
2286 ThemWi RTP endpoint library includes a built-in receiver and parser
2287 for RTCP packets: it knows how to parse SR and RR packets, it extracts
2288 information from RTCP reception report blocks that may be useful
2289 to the application,
2290 and it saves information from the sender info portion of SR packets
2291 for use in generating its own reception report blocks.
2292 The library also includes a facility for generating its own RTCP packets,
2293 either SR or RR,
2294 using information from \fBtwjit\fP to fill out the reception report block.
2295 This chapter describes all library facilities related to RTCP,
2296 across both \fBtwrtp\fP and \fBtwjit\fP layers.
2297 .NH 2
2298 Collection of RR info in twjit
2299 .PP
2300 The analytic function of \fBtwjit\fP, described in \(sc3.4, collects
2301 not only the set of statistical counters described in that section,
2302 but also a set of info for the purpose of generating RTCP reception reports.
2303 In the present version of \fBtwrtp\fP and \fBtwjit\fP,
2304 these tidbits are collected into \&\fCstruct\ osmo_twjit_rr_info\fP;
2305 this structure is to be retrieved from a \fBtwjit\fP instance
2306 via \&\fCosmo_twjit_get_rr_info()\fP.
2307 RTCP sender function in the upper layer of \fBtwrtp\fP uses this RR info
2308 structure to fill out the reception report block.
2309 .NH 2
2310 RTCP receiver in twrtp
2311 .PP
2312 Every packet that arrives at a \fBtwrtp\fP endpoint's RTCP port,
2313 coming from the correct source address that matches the current remote peer,
2314 is parsed by the library.
2315 The parser captures SR and RR information; since these two groups of data
2316 are captured for different purposes, they are best studied separately.
2317 .NH 3
2318 Extraction of SR info from received RTCP packets
2319 .PP
2320 If the received RTCP packet is a correctly formed SR packet per
2321 RFC\ 3550 \(sc6.4.1, \fBtwrtp\fP notes that an SR packet was received,
2322 captures the local time (CLOCK_MONOTONIC) of its arrival,
2323 notes the SSRC of the SR sender,
2324 and saves the middle 32 bits of the 64-bit NTP timestamp in the SR.
2325 This saved information will be used later if and when this \fBtwrtp\fP
2326 instance generates its own reception report.
2327 .NH 3
2328 Extraction of RR info from received RTCP packets
2329 .PP
2330 If the received RTCP packet is either SR or RR (either packet type
2331 is allowed to carry anywhere from 0 to 31 reception report blocks),
2332 the RTCP receiver in the library checks every included RR block
2333 to see if it describes our Tx SSRC, i.e., the one that was assigned
2334 as described in \(sc4.5.1.
2335 If such SSRC-matching RR block is seen, \fBtwrtp\fP sets a flag noting so,
2336 and captures two words of useful info from the report: the word describing
2337 packet loss and the word that expresses interarrival jitter.
2338 Both words are described in RFC\ 3550 \(sc6.4.1.
2339 These words are captured for retrieval by the application,
2340 to be made accessible for vty introspection and logged upon call completion,
2341 along with locally collected stats as described in Chapter\ 3.
2342 .NH 2
2343 Emitting RTCP packets
2344 .LP
2345 The library is capable of generating 3 forms of RTCP packet:
2346 .IP \(bu
2347 SR packet containing a single RR block;
2348 .IP \(bu
2349 SR packet containing no RR block;
2350 .IP \(bu
2351 RR packet containing a single reception report block.
2352 .LP
2353 The following sections describe how these RTCP packets
2354 may be generated and emitted.
2355 .NH 3
2356 Setting SDES strings
2357 .PP
2358 RFC\ 3550 \(sc6.1 stipulates that every RTCP SR or RR packet also
2359 needs to include an SDES block, containing at least a CNAME string.
2360 These SDES strings (the mandatory CNAME and any optional ones)
2361 are set with \&\fCosmo_twrtp_set_sdes()\fP API function;
2362 the application must call this function before any SR or RR packets
2363 can be emitted.
2364 .NH 3
2365 Emitting SR packets
2366 .PP
2367 In this library implementation, SR packets can be emitted in only one path:
2368 together with locally generated (not forwarded) RTP data output,
2369 as a result of the application calling \&\fCosmo_twrtp_tx_quantum()\fP.
2370 There are two ways to cause \&\fCosmo_twrtp_tx_quantum()\fP to emit
2371 RTCP SR in addition to its regular RTP data packet carrying
2372 its normally emitted quantum of media:
2373 .IP \(bu
2374 The application can call \&\fCosmo_twrtp_set_auto_rtcp_interval()\fP
2375 and thus configure the library to automatically emit an RTCP SR packet
2376 after every so many regular RTP data packets sent via
2377 \&\fCosmo_twrtp_tx_quantum()\fP.
2378 .IP \(bu
2379 The application can control directly which calls to
2380 \&\fCosmo_twrtp_tx_quantum()\fP should emit RTCP SR via the last Boolean
2381 argument to this function.
2382 .LP
2383 Whichever condition is used to trigger emission of RTCP SR packet,
2384 the decision as to whether or not this SR packet will include an RR block
2385 in addition to the required sender info is made by the library.
2386 This RR block will be included if and only if:
2387 .IP a)
2388 this \fBtwrtp\fP instance is equipped with \fBtwjit\fP, and
2389 .IP b)
2390 at least one valid RTP packet has been received by this \fBtwjit\fP instance,
2391 producing the necessary SSRC-keyed RR info structure.
2392 .LP
2393 The first 3 words in the RR block (the packet loss word,
2394 extended highest sequence number received and interarrival jitter)
2395 are always filled based on the info provided by \fBtwjit\fP via
2396 \&\fCstruct\ osmo_twjit_rr_info\fP.
2397 However, the last 2 words (LSR and DLSR) are filled based on info
2398 captured by \fBtwrtp\fP layer's RTCP receiver, as described in \(sc5.2.1.
2399 If an SR was previously received by this \fBtwrtp\fP endpoint
2400 and the sender of that SR had the same SSRC as the one for which
2401 we are producing our reception report
2402 (the SSRC in \&\fCstruct\ osmo_twjit_rr_info\fP),
2403 then information from that received SR
2404 (its time of arrival and saved NTP timestamp bits)
2405 is used to fill LSR and DLSR words in the generated RR block.
2406 Otherwise, these two words are set to 0.
2407 .NH 3
2408 Emitting standalone RR packets
2409 .PP
2410 In most RTCP-enabled RTP applications, it is most useful to emit SR packets
2411 and convey reception report blocks as part of them.
2412 However, \fBtwrtp\fP library also provides a way to emit standalone RR
2413 packets, which can be useful for applications that receive RTP via \fBtwjit\fP
2414 but don't send out their own originated RTP traffic.
2415 To generate a standalone RR packet, call \&\fCosmo_twrtp_send_rtcp_rr()\fP.
2416 This operation will succeed only if SDES strings have been set,
2417 if this \fBtwrtp\fP instance is equipped with \fBtwjit\fP, if that \fBtwjit\fP
2418 instance was actually used to receive traffic, and if at least one RTP packet
2419 has been received.
2420 The content of the generated standalone RR packet is exactly the same
2421 as the RR block that is more commonly included in an SR packet,
2422 as described in the previous section.
2423 .NH 2
2424 RTCP support limitations
2425 .PP
2426 RTCP support in \fBtwrtp\fP is subject to the following limitations:
2427 .IP \(bu
2428 Sender reports (SR packets) emitted by \fBtwrtp\fP can only describe
2429 traffic that is generated locally via \&\fCosmo_twrtp_tx_quantum()\fP,
2430 not forwarded traffic that is emitted via \&\fCosmo_twrtp_tx_forward()\fP.
2431 There is no way to generate SR packets at all outside of
2432 \&\fCosmo_twrtp_tx_quantum()\fP.
2433 .IP \(bu
2434 The library can only generate reception reports (either standalone RR packets
2435 or as part of SR packets) for traffic that is received via \fBtwjit\fP,
2436 but not for traffic that is received via non-delayed unbuffered path
2437 \(em see \(sc4.4.
2438 .IP \(bu
2439 The built-in RTCP receiver and parser can only extract potentially useful
2440 RR info (reports of packet loss and interarrival jitter) from far end
2441 reception reports when those far end RRs describe our own Tx SSRC
2442 (see \(sc4.5.1), not some foreign SSRC we forward per \(sc4.6.
2443 .LP
2444 The summary of these limitations is that \fBtwrtp\fP has truly functional
2445 RTCP support only when \fBtwrtp\fP is used to implement a full endpoint,
2446 one that interfaces between RTP and a fixed timing system such as GSM Um TCH,
2447 T1/E1 TDM or a software transcoder that runs on its own CLOCK_MONOTONIC
2448 timerfd time base.
2449 ``Light'' RTP endpoints that omit some components of this full endpoint
2450 ensemble will most likely be unable to support RTCP.
2451 .NH 2
2452 Usefulness of RTCP
2453 .PP
2454 In the opinion of \fBtwrtp\fP author, RTCP is most useful in IP-PSTN
2455 environment where RTP traffic is exchanged between peer entities under
2456 different ownership and different administrative control,
2457 traveling across public Internet.
2458 In that environment, proper implementation of RTCP can be seen as
2459 good netizenship: the administrator of one fiefdom can see \fBtwjit\fP stats
2460 (or full pcap when needed for deeper debugging)
2461 on RTP traffic \fIreceived\fP by her queendom, but she can only know if her
2462 outgoing traffic suffers from packet loss or jitter if administrators of
2463 other fiefdoms have configured \fItheir\fP systems to emit RTCP reception
2464 reports.
2465 For this reason, \&\fCtw\-border\-mgw\fP instances at Themyscira MSCs
2466 are configured to dutifully emit RTCP SR (which includes RR block)
2467 on IP-PSTN side
2468 every 5\ s, or after every 250 RTP data packets sent every 20\ ms.
2469 .PP
2470 On the other hand,
2471 RTCP is \fBnot\fP really useful in a single-administration GSM RAN,
2472 i.e., in environments where both ends of the RTP transport leg
2473 are controlled by the same administration.
2474 In Themyscira environment, each GSM-codec-carrying RTP transport leg
2475 runs between \&\fCtw\-border\-mgw\fP or other ThemWi CN components on one end,
2476 located at an MSC site, and either OsmoBTS or \&\fCtw\-e1abis\-mgw\fP
2477 on the other end, located at a cell site, carried across public Internet
2478 in a WireGuard tunnel.
2479 Because transport across public Internet is involved, RTP performance
2480 needs to be closely monitored with an eye out for packet loss, jitter or
2481 even reordering, and \fBtwjit\fP configuration needs to be carefully tuned.
2482 However, direct examination of \fBtwjit\fP stats on both CN and BSS ends
2483 will yield much more detailed information than the constrained data model
2484 of RTCP \(em hence RTCP is not really useful.
2485 .NH 2
2486 Non-RTCP operation
2487 .PP
2488 If \fBtwrtp\fP needs to be used in an environment where RTCP is not needed,
2489 or even one where use of RTCP is forbidden,
2490 nothing special needs to be done to achieve non-RTCP operation.
2491 No RTCP packets will be emitted if the application never calls
2492 \&\fCosmo_twrtp_set_sdes()\fP;
2493 any received RTCP packets will still be parsed as described in \(sc5.2,
2494 but the existence of saved bits from this parsing can be simply ignored.
2495 RR info from \fBtwjit\fP, collected as described in \(sc5.1, can be
2496 likewise ignored.
2497 .PP
2498 As an additional feature in \fBosmo_twrtp\fP, it is also possible to
2499 disable RTCP completely by not binding a UDP socket for the odd-numbered
2500 RTCP port and not having an active file descriptor or
2501 \%\fCstruct\ osmo_io_fd\fP for it.
2502 Referring to \(sc4.2 for socket binding or file descriptor initialization
2503 procedures, such non-RTCP operation can be achieved by passing \fBfalse\fP
2504 as the last Boolean argument to \%\fCosmo_twrtp_bind_local()\fP or by
2505 passing a negative RTCP file descriptor to \%\fCosmo_twrtp_supply_fds()\fP.
2506 .NH 1
2507 Stats at twrtp level
2508 .PP
2509 In addition to \fBtwjit\fP stats counters described in Chapter\ 3,
2510 \fBtwrtp\fP layer has its own stats structure with a few additional counters,
2511 dealing with both RTP and RTCP packets in both Rx and Tx directions.
2512 This stats structure is \%\fCstruct\ osmo_twrtp_stats\fP
2513 in the present Osmocom-integrated version.
2514 Here is a description of all counters in this set:
2515 .sp .3
2516 .de St
2517 .IP \fC\\$1\fP 24
2518 ..
2519 .St rx_rtp_pkt
2520 This counter increments for every packet that is received on the RTP UDP
2521 socket \fBand\fP has a source address that matches the current remote peer
2522 set with \%\fCosmo_twrtp_set_remote()\fP.
2523 .St rx_rtp_badsrc
2524 This counter counts packets that were received on the RTP UDP socket,
2525 but then discarded because their source address was wrong.
2526 .St rx_rtcp_pkt
2527 This counter increments for every packet that is received on the RTCP UDP
2528 socket \fBand\fP has a source address that matches the current remote peer
2529 set with \%\fCosmo_twrtp_set_remote()\fP.
2530 .St rx_rtcp_badsrc
2531 This counter counts packets that were received on the RTCP UDP socket,
2532 but then discarded because their source address was wrong.
2533 .St rx_rtcp_invalid
2534 This counter counts packets that were received on the RTCP UDP socket,
2535 passed the source address check, but were deemed invalid in parsing.
2536 .St rx_rtcp_wrong_ssrc
2537 This counter increments for every parsed reception report block within
2538 a received RTCP SR or RR packet that describes an SSRC other than
2539 our Tx SSRC of \(sc4.5.1.
2540 .St tx_rtp_pkt
2541 This counter increments for every RTP data packet emitted via
2542 \%\fCosmo_twrtp_tx_quantum()\fP;
2543 it is also emitted in RTCP SR packets in the ``sender's packet count'' word.
2544 Packets transmitted via \%\fCosmo_twrtp_tx_forward()\fP are \fBnot\fP
2545 counted here; as explained in \(sc5.4, there is no RTCP support in \fBtwrtp\fP
2546 for this path.
2547 .St tx_rtp_bytes
2548 This counter counts payload bytes transmitted via
2549 \%\fCosmo_twrtp_tx_quantum()\fP;
2550 it is also emitted in RTCP SR packets in the ``sender's octet count'' word.
2551 Just like \%\fCtx_rtp_pkt\fP, this counter is not affected by
2552 \%\fCosmo_twrtp_tx_forward()\fP path.
2553 .St tx_rtcp_pkt
2554 This counter increments for every RTCP packet emitted by this
2555 \fBtwrtp\fP instance.