Debian Codegraph @

debian/patches/2016_opus_plc.patch

raw symbols SCIP
1 Description: detect lost (and late) RTP packets for native PLC
2 Original patch adapted by Jonas Smedegaard
3 to fit Debian packaging,
4 disable debugging NOTICEs,
5 and to remove original patch file to help detect later upstream changes.
6 Author: Alexander Traud <[email protected]>
7 Author: Jonas Smedegaard <[email protected]>
8 Source: https://github.com/traud/asterisk-opus/blob/asterisk-13.7/enable_native_plc.patch
9 Bug-Debian: https://bugs.debian.org/1060275
10 Forwarded: not-needed
11 Last-Update: 2024-01-30
12 ---
13 This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
14 --- a/main/translate.c
15 +++ b/main/translate.c
16 @@ -414,6 +414,7 @@
17 pvt->f.offset = AST_FRIENDLY_OFFSET;
18 pvt->f.src = pvt->t->name;
19 pvt->f.data.ptr = pvt->outbuf.c;
20 + pvt->f.seqno = 0x10000;
21
22 /*
23 * If the translator has not provided a format
24 @@ -622,13 +623,47 @@
25 /*! \brief do the actual translation */
26 struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
27 {
28 + const unsigned int rtp_seqno_max_value = 0xffff;
29 struct ast_trans_pvt *p = path;
30 - struct ast_frame *out;
31 + struct ast_frame *out_last, *out = NULL;
32 struct timeval delivery;
33 int has_timing_info;
34 long ts;
35 long len;
36 - int seqno;
37 + int seqno, frames_missing;
38 +
39 + /* Determine the amount of lost packets for PLC */
40 + /* But not at start with first frame = path->f.seqno is still 0x10000 */
41 + /* But not when there is no sequence number = frame created internally */
42 + if ((path->f.seqno <= rtp_seqno_max_value) && (path->f.seqno != f->seqno)) {
43 + if (f->seqno < path->f.seqno) { /* seqno overrun situation */
44 + frames_missing = rtp_seqno_max_value + f->seqno - path->f.seqno - 1;
45 + } else {
46 + frames_missing = f->seqno - path->f.seqno - 1;
47 + }
48 + /* Out-of-order packet - more precise: late packet */
49 + if ((rtp_seqno_max_value + 1) / 2 < frames_missing) {
50 + if (consume) {
51 + ast_frfree(f);
52 + }
53 + /*
54 + * Do not pass late packets to any transcoding module, because that
55 + * confuses the state of any library (packets inter-depend). With
56 + * the next packet, this one is going to be treated as lost packet.
57 + */
58 + return NULL;
59 + }
60 +
61 + if (frames_missing > 96) {
62 + struct ast_str *str = ast_str_alloca(256);
63 +
64 + /* disabled by default to not flood log in production even in non-Opus use
65 + /* not DEBUG but NOTICE because of WARNING in main/channel.c:__ast_queue_frame */
66 + //ast_log(LOG_NOTICE, "%d lost frame(s) %d/%d %s\n", frames_missing, f->seqno, path->f.seqno, ast_translate_path_to_str(path, &str));
67 + }
68 + } else {
69 + frames_missing = 0;
70 + }
71
72 if (f->frametype == AST_FRAME_RTCP) {
73 /* Just pass the feedback to the right callback, if it exists.
74 @@ -669,19 +704,94 @@
75 f->samples, ast_format_get_sample_rate(f->subclass.format)));
76 }
77 delivery = f->delivery;
78 - for (out = f; out && p ; p = p->next) {
79 - struct ast_frame *current = out;
80
81 - do {
82 - framein(p, current);
83 - current = AST_LIST_NEXT(current, frame_list);
84 - } while (current);
85 - if (out != f) {
86 - ast_frfree(out);
87 + for (out_last = NULL; frames_missing + 1; frames_missing--) {
88 + struct ast_frame *frame_to_translate, *inner_head;
89 + struct ast_frame missed = {
90 + .frametype = AST_FRAME_VOICE,
91 + .subclass.format = f->subclass.format,
92 + .datalen = 0,
93 + /* In RTP, the amount of samples might change anytime */
94 + /* If that happened while frames got lost, what to do? */
95 + .samples = f->samples, /* FIXME */
96 + .src = __FUNCTION__,
97 + .data.uint32 = 0,
98 + .delivery.tv_sec = 0,
99 + .delivery.tv_usec = 0,
100 + .flags = 0,
101 + /* RTP sequence number is between 0x0001 and 0xffff */
102 + .seqno = (rtp_seqno_max_value + 1 + f->seqno - frames_missing) & rtp_seqno_max_value,
103 + };
104 +
105 + if (frames_missing) {
106 + frame_to_translate = &missed;
107 + } else {
108 + frame_to_translate = f;
109 + }
110 +
111 + /* The translation path from one format to another might contain several steps */
112 + /* out* collects the result for missed frame(s) and input frame(s) */
113 + /* out is the result of the conversion of all frames, translated into the destination format */
114 + /* out_last is the last frame in that list, to add frames faster */
115 + for (p = path, inner_head = frame_to_translate; inner_head && p; p = p->next) {
116 + struct ast_frame *current, *inner_last, *inner_prev = frame_to_translate;
117 +
118 + /* inner* collects the result of each conversion step, the input for the next step */
119 + /* inner_head is a list of frames created by each conversion step */
120 + /* inner_last is the last frame in that list, to add frames faster */
121 + for (inner_last = NULL, current = inner_head; current; current = AST_LIST_NEXT(current, frame_list)) {
122 + struct ast_frame *tmp;
123 +
124 + framein(p, current);
125 + tmp = p->t->frameout(p);
126 +
127 + if (!tmp) {
128 + continue;
129 + } else if (inner_last) {
130 + struct ast_frame *t;
131 +
132 + /* Determine the last frame of the list before appending to it */
133 + while ((t = AST_LIST_NEXT(inner_last, frame_list))) {
134 + inner_last = t;
135 + }
136 + AST_LIST_NEXT(inner_last, frame_list) = tmp;
137 + } else {
138 + inner_prev = inner_head;
139 + inner_head = tmp;
140 + inner_last = tmp;
141 + }
142 + }
143 +
144 + /* The current step did not create any frames = no frames for the next step */
145 + /* The steps are not lost because framein buffered those for the next input frame */
146 + if (!inner_last) {
147 + inner_prev = inner_head;
148 + inner_head = NULL;
149 + }
150 + if (inner_prev != frame_to_translate) {
151 + ast_frfree(inner_prev); /* Frees just the intermediate lists */
152 + }
153 + }
154 +
155 + /* This frame created no frames after translation = continue with next frame */
156 + /* The frame is not lost because framein buffered it to be combined with the next frame */
157 + if (!inner_head) {
158 + continue;
159 + } else if (out_last) {
160 + struct ast_frame *t;
161 +
162 + /* Determine the last frame of the list before appending to it */
163 + while ((t = AST_LIST_NEXT(out_last, frame_list))) {
164 + out_last = t;
165 + }
166 + AST_LIST_NEXT(out_last, frame_list) = inner_head;
167 + } else {
168 + out = inner_head;
169 + out_last = inner_head;
170 }
171 - out = p->t->frameout(p);
172 }
173
174 +
175 if (!out) {
176 out = generate_interpolated_slin(path, f);
177 }
178 --- a/Xopus/enable_native_plc.patch
179 +++ /dev/null
180 @@ -1,156 +0,0 @@
181 ---- a/main/translate.c (Asterisk 13.17.2)
182 -+++ b/main/translate.c (working copy)
183 -@@ -359,4 +359,5 @@
184 - pvt->f.src = pvt->t->name;
185 - pvt->f.data.ptr = pvt->outbuf.c;
186 -+ pvt->f.seqno = 0x10000;
187 -
188 - /*
189 -@@ -531,11 +532,44 @@
190 - struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
191 - {
192 -+ const unsigned int rtp_seqno_max_value = 0xffff;
193 - struct ast_trans_pvt *p = path;
194 -- struct ast_frame *out;
195 -+ struct ast_frame *out_last, *out = NULL;
196 - struct timeval delivery;
197 - int has_timing_info;
198 - long ts;
199 - long len;
200 -- int seqno;
201 -+ int seqno, frames_missing;
202 -+
203 -+ /* Determine the amount of lost packets for PLC */
204 -+ /* But not at start with first frame = path->f.seqno is still 0x10000 */
205 -+ /* But not when there is no sequence number = frame created internally */
206 -+ if ((path->f.seqno <= rtp_seqno_max_value) && (path->f.seqno != f->seqno)) {
207 -+ if (f->seqno < path->f.seqno) { /* seqno overrun situation */
208 -+ frames_missing = rtp_seqno_max_value + f->seqno - path->f.seqno - 1;
209 -+ } else {
210 -+ frames_missing = f->seqno - path->f.seqno - 1;
211 -+ }
212 -+ /* Out-of-order packet - more precise: late packet */
213 -+ if ((rtp_seqno_max_value + 1) / 2 < frames_missing) {
214 -+ if (consume) {
215 -+ ast_frfree(f);
216 -+ }
217 -+ /*
218 -+ * Do not pass late packets to any transcoding module, because that
219 -+ * confuses the state of any library (packets inter-depend). With
220 -+ * the next packet, this one is going to be treated as lost packet.
221 -+ */
222 -+ return NULL;
223 -+ }
224 -+
225 -+ if (frames_missing > 96) {
226 -+ struct ast_str *str = ast_str_alloca(256);
227 -+
228 -+ /* not DEBUG but NOTICE because of WARNING in main/cannel.c:__ast_queue_frame */
229 -+ ast_log(LOG_NOTICE, "%d lost frame(s) %d/%d %s\n", frames_missing, f->seqno, path->f.seqno, ast_translate_path_to_str(path, &str));
230 -+ }
231 -+ } else {
232 -+ frames_missing = 0;
233 -+ }
234 -
235 - has_timing_info = ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO);
236 -@@ -567,16 +601,91 @@
237 - }
238 - delivery = f->delivery;
239 -- for (out = f; out && p ; p = p->next) {
240 -- struct ast_frame *current = out;
241 -
242 -- do {
243 -- framein(p, current);
244 -- current = AST_LIST_NEXT(current, frame_list);
245 -- } while (current);
246 -- if (out != f) {
247 -- ast_frfree(out);
248 -+ for (out_last = NULL; frames_missing + 1; frames_missing--) {
249 -+ struct ast_frame *frame_to_translate, *inner_head;
250 -+ struct ast_frame missed = {
251 -+ .frametype = AST_FRAME_VOICE,
252 -+ .subclass.format = f->subclass.format,
253 -+ .datalen = 0,
254 -+ /* In RTP, the amount of samples might change anytime */
255 -+ /* If that happened while frames got lost, what to do? */
256 -+ .samples = f->samples, /* FIXME */
257 -+ .src = __FUNCTION__,
258 -+ .data.uint32 = 0,
259 -+ .delivery.tv_sec = 0,
260 -+ .delivery.tv_usec = 0,
261 -+ .flags = 0,
262 -+ /* RTP sequence number is between 0x0001 and 0xffff */
263 -+ .seqno = (rtp_seqno_max_value + 1 + f->seqno - frames_missing) & rtp_seqno_max_value,
264 -+ };
265 -+
266 -+ if (frames_missing) {
267 -+ frame_to_translate = &missed;
268 -+ } else {
269 -+ frame_to_translate = f;
270 -+ }
271 -+
272 -+ /* The translation path from one format to another might contain several steps */
273 -+ /* out* collects the result for missed frame(s) and input frame(s) */
274 -+ /* out is the result of the conversion of all frames, translated into the destination format */
275 -+ /* out_last is the last frame in that list, to add frames faster */
276 -+ for (p = path, inner_head = frame_to_translate; inner_head && p; p = p->next) {
277 -+ struct ast_frame *current, *inner_last, *inner_prev = frame_to_translate;
278 -+
279 -+ /* inner* collects the result of each conversion step, the input for the next step */
280 -+ /* inner_head is a list of frames created by each conversion step */
281 -+ /* inner_last is the last frame in that list, to add frames faster */
282 -+ for (inner_last = NULL, current = inner_head; current; current = AST_LIST_NEXT(current, frame_list)) {
283 -+ struct ast_frame *tmp;
284 -+
285 -+ framein(p, current);
286 -+ tmp = p->t->frameout(p);
287 -+
288 -+ if (!tmp) {
289 -+ continue;
290 -+ } else if (inner_last) {
291 -+ struct ast_frame *t;
292 -+
293 -+ /* Determine the last frame of the list before appending to it */
294 -+ while ((t = AST_LIST_NEXT(inner_last, frame_list))) {
295 -+ inner_last = t;
296 -+ }
297 -+ AST_LIST_NEXT(inner_last, frame_list) = tmp;
298 -+ } else {
299 -+ inner_prev = inner_head;
300 -+ inner_head = tmp;
301 -+ inner_last = tmp;
302 -+ }
303 -+ }
304 -+
305 -+ /* The current step did not create any frames = no frames for the next step */
306 -+ /* The steps are not lost because framein buffered those for the next input frame */
307 -+ if (!inner_last) {
308 -+ inner_prev = inner_head;
309 -+ inner_head = NULL;
310 -+ }
311 -+ if (inner_prev != frame_to_translate) {
312 -+ ast_frfree(inner_prev); /* Frees just the intermediate lists */
313 -+ }
314 -+ }
315 -+
316 -+ /* This frame created no frames after translation = continue with next frame */
317 -+ /* The frame is not lost because framein buffered it to be combined with the next frame */
318 -+ if (!inner_head) {
319 -+ continue;
320 -+ } else if (out_last) {
321 -+ struct ast_frame *t;
322 -+
323 -+ /* Determine the last frame of the list before appending to it */
324 -+ while ((t = AST_LIST_NEXT(out_last, frame_list))) {
325 -+ out_last = t;
326 -+ }
327 -+ AST_LIST_NEXT(out_last, frame_list) = inner_head;
328 -+ } else {
329 -+ out = inner_head;
330 -+ out_last = inner_head;
331 - }
332 -- out = p->t->frameout(p);
333 - }
334 -+
335 - if (out) {
336 - /* we have a frame, play with times */