Asterisk - The Open Source Telephony Project  18.5.0
test_format_cap.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Joshua Colp <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \brief Format Capabilities API Unit Tests
22  *
23  * \author Joshua Colp <[email protected]>
24  *
25  */
26 
27 /*** MODULEINFO
28  <depend>TEST_FRAMEWORK</depend>
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include "asterisk/test.h"
35 #include "asterisk/module.h"
36 #include "asterisk/codec.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/format.h"
39 #include "asterisk/format_cap.h"
40 
41 AST_TEST_DEFINE(format_cap_alloc)
42 {
43  struct ast_format_cap *caps;
44 
45  switch (cmd) {
46  case TEST_INIT:
47  info->name = "format_cap_alloc";
48  info->category = "/main/format_cap/";
49  info->summary = "format capabilities allocation unit test";
50  info->description =
51  "Test that allocation of a format capabilities structure succeeds";
52  return AST_TEST_NOT_RUN;
53  case TEST_EXECUTE:
54  break;
55  }
56 
58  if (!caps) {
59  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
60  return AST_TEST_FAIL;
61  }
62  ao2_ref(caps, -1);
63 
64  return AST_TEST_PASS;
65 }
66 
67 AST_TEST_DEFINE(format_cap_append_single)
68 {
69  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
70  RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
72  RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
73 
74  switch (cmd) {
75  case TEST_INIT:
76  info->name = __PRETTY_FUNCTION__;
77  info->category = "/main/format_cap/";
78  info->summary = "format capabilities adding unit test";
79  info->description =
80  "Test that adding a single format to a format capabilities structure succeeds";
81  return AST_TEST_NOT_RUN;
82  case TEST_EXECUTE:
83  break;
84  }
85 
87  if (!caps) {
88  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
89  return AST_TEST_FAIL;
90  }
91 
92  codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
93  if (!codec) {
94  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
95  return AST_TEST_FAIL;
96  }
97 
98  format = ast_format_create(codec);
99  if (!format) {
100  ast_test_status_update(test, "Could not create format using built-in codec\n");
101  return AST_TEST_FAIL;
102  }
103 
104  if (ast_format_cap_append(caps, format, 42)) {
105  ast_test_status_update(test, "Could not add newly created format to capabilities structure\n");
106  return AST_TEST_FAIL;
107  } else if (ast_format_cap_count(caps) != 1) {
108  ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
109  ast_format_cap_count(caps));
110  return AST_TEST_FAIL;
111  }
112 
113  retrieved = ast_format_cap_get_format(caps, 0);
114  if (!retrieved) {
115  ast_test_status_update(test, "Attempted to get single format from capabilities structure but got nothing\n");
116  return AST_TEST_FAIL;
117  } else if (retrieved != format) {
118  ast_test_status_update(test, "Retrieved format is not the same as the one we added\n");
119  return AST_TEST_FAIL;
120  } else if (ast_format_cap_get_format_framing(caps, retrieved) != 42) {
121  ast_test_status_update(test, "Framing for format in capabilities structure does not match what we provided\n");
122  return AST_TEST_FAIL;
123  }
124 
125  return AST_TEST_PASS;
126 }
127 
128 AST_TEST_DEFINE(format_cap_append_multiple)
129 {
130  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
131  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
132  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
133  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
134  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
135  RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
136 
137  switch (cmd) {
138  case TEST_INIT:
139  info->name = __PRETTY_FUNCTION__;
140  info->category = "/main/format_cap/";
141  info->summary = "format capabilities adding unit test";
142  info->description =
143  "Test that adding multiple formats to a format capabilities structure succeeds";
144  return AST_TEST_NOT_RUN;
145  case TEST_EXECUTE:
146  break;
147  }
148 
150  if (!caps) {
151  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
152  return AST_TEST_FAIL;
153  }
154 
155  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
156  if (!ulaw) {
157  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
158  return AST_TEST_FAIL;
159  }
160 
161  ulaw_format = ast_format_create(ulaw);
162  if (!ulaw_format) {
163  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
164  return AST_TEST_FAIL;
165  }
166 
167  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
168  if (!alaw) {
169  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
170  return AST_TEST_FAIL;
171  }
172 
173  alaw_format = ast_format_create(alaw);
174  if (!alaw_format) {
175  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
176  return AST_TEST_FAIL;
177  }
178 
179  if (ast_format_cap_append(caps, ulaw_format, 42)) {
180  ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
181  return AST_TEST_FAIL;
182  } else if (ast_format_cap_append(caps, alaw_format, 84)) {
183  ast_test_status_update(test, "Could not add newly created alaw format to capabilities structure\n");
184  return AST_TEST_FAIL;
185  } else if (ast_format_cap_count(caps) != 2) {
186  ast_test_status_update(test, "Number of formats in capabilities structure should be 2 but is %zu\n",
187  ast_format_cap_count(caps));
188  return AST_TEST_FAIL;
189  }
190 
191  retrieved = ast_format_cap_get_format(caps, 0);
192  if (!retrieved) {
193  ast_test_status_update(test, "Attempted to get first format from capabilities structure but got nothing\n");
194  return AST_TEST_FAIL;
195  } else if (retrieved != ulaw_format) {
196  ast_test_status_update(test, "First retrieved format is not the ulaw one we added\n");
197  return AST_TEST_FAIL;
198  } else if (ast_format_cap_get_format_framing(caps, retrieved) != 42) {
199  ast_test_status_update(test, "Framing for ulaw format in capabilities structure does not match what we provided\n");
200  }
201  ao2_ref(retrieved, -1);
202 
203  retrieved = ast_format_cap_get_format(caps, 1);
204  if (!retrieved) {
205  ast_test_status_update(test, "Attempted to get second format from capabilities structure but got nothing\n");
206  return AST_TEST_FAIL;
207  } else if (retrieved != alaw_format) {
208  ast_test_status_update(test, "First retrieved format is not the alaw one we added\n");
209  return AST_TEST_FAIL;
210  } else if (ast_format_cap_get_format_framing(caps, retrieved) != 84) {
211  ast_test_status_update(test, "Framing for alaw format in capabilities structure does not match what we provided\n");
212  }
213 
214  return AST_TEST_PASS;
215 }
216 
217 AST_TEST_DEFINE(format_cap_append_all_unknown)
218 {
219  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
220 
221  switch (cmd) {
222  case TEST_INIT:
223  info->name = __PRETTY_FUNCTION__;
224  info->category = "/main/format_cap/";
225  info->summary = "format capabilities adding unit test";
226  info->description =
227  "Test that adding of all formats to a format capabilities structure succeeds";
228  return AST_TEST_NOT_RUN;
229  case TEST_EXECUTE:
230  break;
231  }
232 
234  if (!caps) {
235  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
236  return AST_TEST_FAIL;
238  ast_test_status_update(test, "Failed to add all media formats of all types to capabilities structure\n");
239  return AST_TEST_FAIL;
240  } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_AUDIO)) {
241  ast_test_status_update(test, "Added all media formats but no audio formats exist when they should\n");
242  return AST_TEST_FAIL;
243  } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_VIDEO)) {
244  ast_test_status_update(test, "Added all media formats but no video formats exist when they should\n");
245  return AST_TEST_FAIL;
246  } else if ((ast_format_cap_count(caps) + 1) != (ast_codec_get_max() - 1)) {
247  ast_test_status_update(test, "The number of formats in the capabilities structure does not match known number\n");
248  return AST_TEST_FAIL;
249  }
250 
251  return AST_TEST_PASS;
252 }
253 
254 AST_TEST_DEFINE(format_cap_append_all_audio)
255 {
256  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
257 
258  switch (cmd) {
259  case TEST_INIT:
260  info->name = __PRETTY_FUNCTION__;
261  info->category = "/main/format_cap/";
262  info->summary = "format capabilities adding unit test";
263  info->description =
264  "Test that adding of all audio formats to a format capabilities structure succeeds";
265  return AST_TEST_NOT_RUN;
266  case TEST_EXECUTE:
267  break;
268  }
269 
271  if (!caps) {
272  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
273  return AST_TEST_FAIL;
275  ast_test_status_update(test, "Failed to add all audio media formats to capabilities structure\n");
276  return AST_TEST_FAIL;
277  } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_AUDIO)) {
278  ast_test_status_update(test, "Added audio media formats but no audio formats exist when they should\n");
279  return AST_TEST_FAIL;
280  } else if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_VIDEO)) {
281  ast_test_status_update(test, "Added only audio media formats but video formats exist when they should not\n");
282  return AST_TEST_FAIL;
283  } else if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_TEXT)) {
284  ast_test_status_update(test, "Added only audio media formats but text formats exist when they should not\n");
285  return AST_TEST_FAIL;
286  } else if (ast_format_cap_has_type(caps, AST_MEDIA_TYPE_IMAGE)) {
287  ast_test_status_update(test, "Added only audio media formats but image formats exist when they should not\n");
288  return AST_TEST_FAIL;
289  }
290 
291  return AST_TEST_PASS;
292 }
293 
294 AST_TEST_DEFINE(format_cap_append_duplicate)
295 {
296  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
297  RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
299  RAII_VAR(struct ast_format *, format_named, NULL, ao2_cleanup);
300  RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
301 
302  switch (cmd) {
303  case TEST_INIT:
304  info->name = __PRETTY_FUNCTION__;
305  info->category = "/main/format_cap/";
306  info->summary = "format capabilities duplication unit test";
307  info->description =
308  "Test that adding a single format multiple times to a capabilities structure results in only a single format";
309  return AST_TEST_NOT_RUN;
310  case TEST_EXECUTE:
311  break;
312  }
313 
315  if (!caps) {
316  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
317  return AST_TEST_FAIL;
318  }
319 
320  codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
321  if (!codec) {
322  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
323  return AST_TEST_FAIL;
324  }
325 
326  format = ast_format_create(codec);
327  if (!format) {
328  ast_test_status_update(test, "Could not create format using built-in codec\n");
329  return AST_TEST_FAIL;
330  }
331 
332  format_named = ast_format_create_named("[email protected]", codec);
333  if (!format_named) {
334  ast_test_status_update(test, "Could not create named format using built-in codec\n");
335  return AST_TEST_FAIL;
336  }
337 
338  if (ast_format_cap_append(caps, format, 42)) {
339  ast_test_status_update(test, "Could not add newly created format to capabilities structure\n");
340  return AST_TEST_FAIL;
341  } else if (ast_format_cap_count(caps) != 1) {
342  ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
343  ast_format_cap_count(caps));
344  return AST_TEST_FAIL;
345  }
346 
347  /* Note: regardless of it being a duplicate, ast_format_cap_append should return success */
348  if (ast_format_cap_append(caps, format, 0)) {
349  ast_test_status_update(test, "Adding of duplicate format to capabilities structure failed\n");
350  return AST_TEST_FAIL;
351  } else if (ast_format_cap_count(caps) != 1) {
352  ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
353  ast_format_cap_count(caps));
354  return AST_TEST_FAIL;
355  }
356 
357  if (ast_format_cap_append(caps, format_named, 0)) {
358  ast_test_status_update(test, "Adding of duplicate named format to capabilities structure failed\n");
359  return AST_TEST_FAIL;
360  } else if (ast_format_cap_count(caps) != 1) {
361  ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
362  ast_format_cap_count(caps));
363  return AST_TEST_FAIL;
364  }
365 
366  retrieved = ast_format_cap_get_format(caps, 0);
367  if (!retrieved) {
368  ast_test_status_update(test, "Attempted to get single format from capabilities structure but got nothing\n");
369  return AST_TEST_FAIL;
370  } else if (retrieved != format) {
371  ast_test_status_update(test, "Retrieved format is not the same as the one we added\n");
372  return AST_TEST_FAIL;
373  } else if (ast_format_cap_get_format_framing(caps, retrieved) != 42) {
374  ast_test_status_update(test, "Framing for format in capabilities structure does not match what we provided\n");
375  return AST_TEST_FAIL;
376  }
377 
378  return AST_TEST_PASS;
379 }
380 
381 AST_TEST_DEFINE(format_cap_append_from_cap)
382 {
383  RAII_VAR(struct ast_format_cap *, dst_caps, NULL, ao2_cleanup);
384  RAII_VAR(struct ast_format_cap *, src_caps, NULL, ao2_cleanup);
385 
386  switch (cmd) {
387  case TEST_INIT:
388  info->name = __PRETTY_FUNCTION__;
389  info->category = "/main/format_cap/";
390  info->summary = "format capabilities append unit test";
391  info->description =
392  "Test that appending video formats from one capabilities structure to another succeeds";
393  return AST_TEST_NOT_RUN;
394  case TEST_EXECUTE:
395  break;
396  }
397 
399  if (!dst_caps) {
400  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
401  return AST_TEST_FAIL;
402  } else if (ast_format_cap_append_by_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
403  ast_test_status_update(test, "Failed to add all audio media formats to capabilities structure\n");
404  return AST_TEST_FAIL;
405  }
406 
408  if (!src_caps) {
409  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
410  return AST_TEST_FAIL;
411  } else if (ast_format_cap_append_by_type(src_caps, AST_MEDIA_TYPE_VIDEO)) {
412  ast_test_status_update(test, "Failed to add all video media formats to capabilities structure\n");
413  return AST_TEST_FAIL;
414  }
415 
416  if (ast_format_cap_append_from_cap(dst_caps, src_caps, AST_MEDIA_TYPE_UNKNOWN)) {
417  ast_test_status_update(test, "Failed to append formats to capabilities structure\n");
418  return AST_TEST_FAIL;
419  } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
420  ast_test_status_update(test, "Successfully appended video formats to destination capabilities but it no longer contains audio formats\n");
421  return AST_TEST_FAIL;
422  } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_VIDEO)) {
423  ast_test_status_update(test, "Successfully appended formats but video formats do not exist in destination capabilities\n");
424  return AST_TEST_FAIL;
425  }
426 
427  return AST_TEST_PASS;
428 }
429 
430 AST_TEST_DEFINE(format_cap_append_from_cap_duplicate)
431 {
432  RAII_VAR(struct ast_format_cap *, dst_caps, NULL, ao2_cleanup);
433  RAII_VAR(struct ast_format_cap *, src_caps, NULL, ao2_cleanup);
434  unsigned int count;
435  unsigned int total_count;
436 
437  switch (cmd) {
438  case TEST_INIT:
439  info->name = __PRETTY_FUNCTION__;
440  info->category = "/main/format_cap/";
441  info->summary = "format capabilities append duplicate unit test";
442  info->description =
443  "Test that appending capabilities structures multiple times does not result in duplicate formats";
444  return AST_TEST_NOT_RUN;
445  case TEST_EXECUTE:
446  break;
447  }
448 
450  if (!dst_caps) {
451  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
452  return AST_TEST_FAIL;
453  } else if (ast_format_cap_append_by_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
454  ast_test_status_update(test, "Failed to add all audio media formats to capabilities structure\n");
455  return AST_TEST_FAIL;
456  }
457 
459  if (!src_caps) {
460  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
461  return AST_TEST_FAIL;
462  } else if (ast_format_cap_append_by_type(src_caps, AST_MEDIA_TYPE_VIDEO)) {
463  ast_test_status_update(test, "Failed to add all video media formats to capabilities structure\n");
464  return AST_TEST_FAIL;
465  }
466 
467  total_count = ast_format_cap_count(src_caps) + ast_format_cap_count(dst_caps);
468 
469  if (ast_format_cap_append_from_cap(dst_caps, src_caps, AST_MEDIA_TYPE_UNKNOWN)) {
470  ast_test_status_update(test, "Failed to append formats to capabilities structure\n");
471  return AST_TEST_FAIL;
472  } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_AUDIO)) {
473  ast_test_status_update(test, "Successfully appended video formats to destination capabilities but it no longer contains audio formats\n");
474  return AST_TEST_FAIL;
475  } else if (!ast_format_cap_has_type(dst_caps, AST_MEDIA_TYPE_VIDEO)) {
476  ast_test_status_update(test, "Successfully appended formats but video formats do not exist in destination capabilities\n");
477  return AST_TEST_FAIL;
478  }
479 
480  count = ast_format_cap_count(dst_caps);
481 
482  if (ast_format_cap_append_from_cap(dst_caps, src_caps, AST_MEDIA_TYPE_UNKNOWN)) {
483  ast_test_status_update(test, "Failed to append duplicate formats to capabilities structure\n");
484  return AST_TEST_FAIL;
485  }
486 
487  ast_test_validate(test, count == ast_format_cap_count(dst_caps));
488  ast_test_validate(test, count == total_count);
489 
490  return AST_TEST_PASS;
491 }
492 
493 AST_TEST_DEFINE(format_cap_set_framing)
494 {
495  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
496  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
497  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
498  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
499  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
500 
501  switch (cmd) {
502  case TEST_INIT:
503  info->name = "format_cap_set_framing";
504  info->category = "/main/format_cap/";
505  info->summary = "format capabilities framing unit test";
506  info->description =
507  "Test that global framing on a format capabilities structure is used when it should be";
508  return AST_TEST_NOT_RUN;
509  case TEST_EXECUTE:
510  break;
511  }
512 
514  if (!caps) {
515  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
516  return AST_TEST_FAIL;
517  }
518 
519  ast_format_cap_set_framing(caps, 160);
520 
521  ast_test_validate(test, ast_format_cap_get_framing(caps) == 160);
522 
523  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
524  if (!ulaw) {
525  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
526  return AST_TEST_FAIL;
527  }
528 
529  ulaw_format = ast_format_create(ulaw);
530  if (!ulaw_format) {
531  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
532  return AST_TEST_FAIL;
533  }
534 
535  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
536  if (!alaw) {
537  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
538  return AST_TEST_FAIL;
539  }
540 
541  alaw_format = ast_format_create(alaw);
542  if (!alaw_format) {
543  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
544  return AST_TEST_FAIL;
545  }
546 
547  if (ast_format_cap_append(caps, ulaw_format, 42)) {
548  ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
549  return AST_TEST_FAIL;
550  } else if (ast_format_cap_append(caps, alaw_format, 0)) {
551  ast_test_status_update(test, "Could not add newly created alaw format to capabilities structure\n");
552  return AST_TEST_FAIL;
553  }
554 
555  if (ast_format_cap_get_format_framing(caps, ulaw_format) != 42) {
556  ast_test_status_update(test, "Added ulaw format to capabilities structure with explicit framing but did not get it back\n");
557  return AST_TEST_FAIL;
558  } else if (ast_format_cap_get_format_framing(caps, alaw_format) != ast_format_get_default_ms(alaw_format)) {
559  ast_test_status_update(test, "Added alaw format to capabilities structure with no explicit framing but did not get global back\n");
560  return AST_TEST_FAIL;
561  }
562  ast_test_validate(test, ast_format_cap_get_framing(caps) == ast_format_get_default_ms(alaw_format));
563 
564  return AST_TEST_PASS;
565 }
566 
567 AST_TEST_DEFINE(format_cap_remove_single)
568 {
569  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
570  RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
572 
573  switch (cmd) {
574  case TEST_INIT:
575  info->name = "format_cap_remove_single";
576  info->category = "/main/format_cap/";
577  info->summary = "format capabilities removal unit test";
578  info->description =
579  "Test that removing a single format from a format capabilities structure succeeds";
580  return AST_TEST_NOT_RUN;
581  case TEST_EXECUTE:
582  break;
583  }
584 
586  if (!caps) {
587  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
588  return AST_TEST_FAIL;
589  }
590 
591  codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
592  if (!codec) {
593  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
594  return AST_TEST_FAIL;
595  }
596 
597  format = ast_format_create(codec);
598  if (!format) {
599  ast_test_status_update(test, "Could not create format using built-in codec\n");
600  return AST_TEST_FAIL;
601  }
602 
603  if (ast_format_cap_append(caps, format, 42)) {
604  ast_test_status_update(test, "Could not add newly created format to capabilities structure\n");
605  return AST_TEST_FAIL;
606  } else if (ast_format_cap_remove(caps, format)) {
607  ast_test_status_update(test, "Could not remove format that was just added to capabilities structure\n");
608  return AST_TEST_FAIL;
609  } else if (!ast_format_cap_remove(caps, format)) {
610  ast_test_status_update(test, "Successfully removed a format twice from the capabilities structure\n");
611  return AST_TEST_FAIL;
612  } else if (ast_format_cap_count(caps)) {
613  ast_test_status_update(test, "Capabilities structure should be empty but instead it contains '%zu' formats\n",
614  ast_format_cap_count(caps));
615  return AST_TEST_FAIL;
616  }
617 
618  return AST_TEST_PASS;
619 }
620 
621 AST_TEST_DEFINE(format_cap_remove_multiple)
622 {
623  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
624  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
625  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
626  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
627  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
628  RAII_VAR(struct ast_format *, retrieved, NULL, ao2_cleanup);
629 
630  switch (cmd) {
631  case TEST_INIT:
632  info->name = "format_cap_remove_multiple";
633  info->category = "/main/format_cap/";
634  info->summary = "format capabilities removal unit test";
635  info->description =
636  "Test that removing a format from a format capabilities structure containing multiple formats succeeds";
637  return AST_TEST_NOT_RUN;
638  case TEST_EXECUTE:
639  break;
640  }
641 
643  if (!caps) {
644  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
645  return AST_TEST_FAIL;
646  }
647 
648  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
649  if (!ulaw) {
650  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
651  return AST_TEST_FAIL;
652  }
653 
654  ulaw_format = ast_format_create(ulaw);
655  if (!ulaw_format) {
656  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
657  return AST_TEST_FAIL;
658  }
659 
660  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
661  if (!alaw) {
662  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
663  return AST_TEST_FAIL;
664  }
665 
666  alaw_format = ast_format_create(alaw);
667  if (!alaw_format) {
668  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
669  return AST_TEST_FAIL;
670  }
671 
672  if (ast_format_cap_append(caps, ulaw_format, 42)) {
673  ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
674  return AST_TEST_FAIL;
675  } else if (ast_format_cap_append(caps, alaw_format, 84)) {
676  ast_test_status_update(test, "Could not add newly created alaw format to capabilities structure\n");
677  return AST_TEST_FAIL;
678  } else if (ast_format_cap_remove(caps, ulaw_format)) {
679  ast_test_status_update(test, "Could not remove the ulaw format we just added to capabilities structure\n");
680  return AST_TEST_FAIL;
681  } else if (ast_format_cap_count(caps) != 1) {
682  ast_test_status_update(test, "Capabilities structure should contain 1 format but it contains '%zu'\n",
683  ast_format_cap_count(caps));
684  return AST_TEST_FAIL;
685  }
686 
687  retrieved = ast_format_cap_get_format(caps, 0);
688  if (!retrieved) {
689  ast_test_status_update(test, "Attempted to get first format from capabilities structure but got nothing\n");
690  return AST_TEST_FAIL;
691  } else if (retrieved != alaw_format) {
692  ast_test_status_update(test, "First retrieved format is not the alaw one we added\n");
693  return AST_TEST_FAIL;
694  }
695 
696  return AST_TEST_PASS;
697 }
698 
699 AST_TEST_DEFINE(format_cap_remove_bytype)
700 {
701  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
702 
703  switch (cmd) {
704  case TEST_INIT:
705  info->name = "format_cap_remove_bytype";
706  info->category = "/main/format_cap/";
707  info->summary = "format capabilities removal unit test";
708  info->description =
709  "Test that removal of a specific type of format from a format capabilities structure succeeds";
710  return AST_TEST_NOT_RUN;
711  case TEST_EXECUTE:
712  break;
713  }
714 
716  if (!caps) {
717  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
718  return AST_TEST_FAIL;
720  ast_test_status_update(test, "Failed to add all media formats of all types to capabilities structure\n");
721  return AST_TEST_FAIL;
722  }
723 
726  ast_test_status_update(test, "Removed all audio type formats from capabilities structure but some remain\n");
727  return AST_TEST_FAIL;
728  } else if (!ast_format_cap_has_type(caps, AST_MEDIA_TYPE_VIDEO)) {
729  ast_test_status_update(test, "Removed audio type formats from capabilities structure but video are gone as well\n");
730  return AST_TEST_FAIL;
731  }
732 
733  return AST_TEST_PASS;
734 }
735 
736 AST_TEST_DEFINE(format_cap_remove_all)
737 {
738  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
739 
740  switch (cmd) {
741  case TEST_INIT:
742  info->name = "format_cap_remove_all";
743  info->category = "/main/format_cap/";
744  info->summary = "format capabilities removal unit test";
745  info->description =
746  "Test that removal of all formats from a format capabilities structure succeeds";
747  return AST_TEST_NOT_RUN;
748  case TEST_EXECUTE:
749  break;
750  }
751 
753  if (!caps) {
754  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
755  return AST_TEST_FAIL;
757  ast_test_status_update(test, "Failed to add all media formats of all types to capabilities structure\n");
758  return AST_TEST_FAIL;
759  }
760 
762 
763  if (ast_format_cap_count(caps)) {
764  ast_test_status_update(test, "Removed all formats from capabilities structure but some remain\n");
765  return AST_TEST_FAIL;
766  }
767 
768  return AST_TEST_PASS;
769 }
770 
771 AST_TEST_DEFINE(format_cap_get_compatible_format)
772 {
773  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
774  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
775  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
776  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
777  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
778  RAII_VAR(struct ast_format *, compatible, NULL, ao2_cleanup);
779 
780  switch (cmd) {
781  case TEST_INIT:
782  info->name = "format_cap_get_compatible_format";
783  info->category = "/main/format_cap/";
784  info->summary = "format capabilities negotiation unit test";
785  info->description =
786  "Test that getting a compatible format from a capabilities structure succeeds";
787  return AST_TEST_NOT_RUN;
788  case TEST_EXECUTE:
789  break;
790  }
791 
793  if (!caps) {
794  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
795  return AST_TEST_FAIL;
796  }
797 
798  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
799  if (!ulaw) {
800  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
801  return AST_TEST_FAIL;
802  }
803 
804  ulaw_format = ast_format_create(ulaw);
805  if (!ulaw_format) {
806  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
807  return AST_TEST_FAIL;
808  }
809 
810  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
811  if (!alaw) {
812  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
813  return AST_TEST_FAIL;
814  }
815 
816  alaw_format = ast_format_create(alaw);
817  if (!alaw_format) {
818  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
819  return AST_TEST_FAIL;
820  }
821 
822  if (ast_format_cap_append(caps, ulaw_format, 42)) {
823  ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
824  return AST_TEST_FAIL;
825  }
826 
827  compatible = ast_format_cap_get_compatible_format(caps, alaw_format);
828  if (compatible) {
829  ast_test_status_update(test, "Retrieved a compatible format from capabilities structure when none should exist\n");
830  return AST_TEST_FAIL;
831  }
832 
833  compatible = ast_format_cap_get_compatible_format(caps, ulaw_format);
834  if (!compatible) {
835  ast_test_status_update(test, "Did not retrieve a compatible format from capabilities structure when there should be one\n");
836  return AST_TEST_FAIL;
837  } else if (compatible != ulaw_format) {
838  ast_test_status_update(test, "Compatible format is not the format we added to the capabilities structure\n");
839  return AST_TEST_FAIL;
840  }
841 
842  return AST_TEST_PASS;
843 }
844 
845 AST_TEST_DEFINE(format_cap_iscompatible_format)
846 {
847  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
848  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
849  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
850  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
851  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
852 
853  switch (cmd) {
854  case TEST_INIT:
855  info->name = "format_cap_iscompatible_format";
856  info->category = "/main/format_cap/";
857  info->summary = "format capabilities negotiation unit test";
858  info->description =
859  "Test that checking whether a format is compatible with a capabilities structure succeeds";
860  return AST_TEST_NOT_RUN;
861  case TEST_EXECUTE:
862  break;
863  }
864 
866  if (!caps) {
867  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
868  return AST_TEST_FAIL;
869  }
870 
871  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
872  if (!ulaw) {
873  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
874  return AST_TEST_FAIL;
875  }
876 
877  ulaw_format = ast_format_create(ulaw);
878  if (!ulaw_format) {
879  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
880  return AST_TEST_FAIL;
881  }
882 
883  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
884  if (!alaw) {
885  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
886  return AST_TEST_FAIL;
887  }
888 
889  alaw_format = ast_format_create(alaw);
890  if (!alaw_format) {
891  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
892  return AST_TEST_FAIL;
893  }
894 
895  if (ast_format_cap_append(caps, ulaw_format, 42)) {
896  ast_test_status_update(test, "Could not add newly created ulaw format to capabilities structure\n");
897  return AST_TEST_FAIL;
898  } else if (ast_format_cap_iscompatible_format(caps, alaw_format) != AST_FORMAT_CMP_NOT_EQUAL) {
899  ast_test_status_update(test, "Alaw format is compatible with capabilities structure when it only contains ulaw\n");
900  return AST_TEST_FAIL;
901  } else if (ast_format_cap_iscompatible_format(caps, ulaw_format) == AST_FORMAT_CMP_NOT_EQUAL) {
902  ast_test_status_update(test, "Ulaw format is not compatible with capabilities structure when it should be\n");
903  return AST_TEST_FAIL;
904  }
905 
906  return AST_TEST_PASS;
907 }
908 
909 AST_TEST_DEFINE(format_cap_get_compatible)
910 {
911  RAII_VAR(struct ast_format_cap *, alaw_caps, NULL, ao2_cleanup);
912  RAII_VAR(struct ast_format_cap *, ulaw_caps, NULL, ao2_cleanup);
913  RAII_VAR(struct ast_format_cap *, compatible_caps, NULL, ao2_cleanup);
914  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
915  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
916  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
917  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
918 
919  switch (cmd) {
920  case TEST_INIT:
921  info->name = "format_cap_get_compatible";
922  info->category = "/main/format_cap/";
923  info->summary = "format capabilities negotiation unit test";
924  info->description =
925  "Test that getting the compatible formats between two capabilities structures succeeds";
926  return AST_TEST_NOT_RUN;
927  case TEST_EXECUTE:
928  break;
929  }
930 
932  if (!alaw_caps) {
933  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
934  return AST_TEST_FAIL;
935  }
936 
938  if (!ulaw_caps) {
939  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
940  return AST_TEST_FAIL;
941  }
942 
944  if (!compatible_caps) {
945  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
946  return AST_TEST_FAIL;
947  }
948 
949  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
950  if (!ulaw) {
951  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
952  return AST_TEST_FAIL;
953  }
954 
955  ulaw_format = ast_format_create(ulaw);
956  if (!ulaw_format) {
957  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
958  return AST_TEST_FAIL;
959  }
960 
961  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
962  if (!alaw) {
963  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
964  return AST_TEST_FAIL;
965  }
966 
967  alaw_format = ast_format_create(alaw);
968  if (!alaw_format) {
969  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
970  return AST_TEST_FAIL;
971  }
972 
973  if (ast_format_cap_append(ulaw_caps, ulaw_format, 0)) {
974  ast_test_status_update(test, "Could not add ulaw format to ulaw capabilities\n");
975  return AST_TEST_FAIL;
976  } else if (ast_format_cap_append(alaw_caps, alaw_format, 0)) {
977  ast_test_status_update(test, "Could not add alaw format to alaw capabilities\n");
978  return AST_TEST_FAIL;
979  }
980 
981  ast_format_cap_get_compatible(ulaw_caps, alaw_caps, compatible_caps);
982  if (ast_format_cap_count(compatible_caps)) {
983  ast_test_status_update(test, "A compatible format exists when none should\n");
984  return AST_TEST_FAIL;
985  }
986 
987  ast_format_cap_get_compatible(ulaw_caps, ulaw_caps, compatible_caps);
988  if (!ast_format_cap_count(compatible_caps)) {
989  ast_test_status_update(test, "No compatible formats exist when 1 should\n");
990  return AST_TEST_FAIL;
991  }
992 
993  return AST_TEST_PASS;
994 }
995 
996 AST_TEST_DEFINE(format_cap_iscompatible)
997 {
998  RAII_VAR(struct ast_format_cap *, alaw_caps, NULL, ao2_cleanup);
999  RAII_VAR(struct ast_format_cap *, ulaw_caps, NULL, ao2_cleanup);
1000  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
1001  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
1002  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
1003  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
1004 
1005  switch (cmd) {
1006  case TEST_INIT:
1007  info->name = "format_cap_iscompatible";
1008  info->category = "/main/format_cap/";
1009  info->summary = "format capabilities negotiation unit test";
1010  info->description =
1011  "Test that checking if there are compatible formats between two capabilities structures succeeds";
1012  return AST_TEST_NOT_RUN;
1013  case TEST_EXECUTE:
1014  break;
1015  }
1016 
1018  if (!alaw_caps) {
1019  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1020  return AST_TEST_FAIL;
1021  }
1022 
1024  if (!ulaw_caps) {
1025  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1026  return AST_TEST_FAIL;
1027  }
1028 
1029  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
1030  if (!ulaw) {
1031  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
1032  return AST_TEST_FAIL;
1033  }
1034 
1035  ulaw_format = ast_format_create(ulaw);
1036  if (!ulaw_format) {
1037  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
1038  return AST_TEST_FAIL;
1039  }
1040 
1041  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
1042  if (!alaw) {
1043  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
1044  return AST_TEST_FAIL;
1045  }
1046 
1047  alaw_format = ast_format_create(alaw);
1048  if (!alaw_format) {
1049  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
1050  return AST_TEST_FAIL;
1051  }
1052 
1053  if (ast_format_cap_append(ulaw_caps, ulaw_format, 0)) {
1054  ast_test_status_update(test, "Could not add ulaw format to ulaw capabilities\n");
1055  return AST_TEST_FAIL;
1056  } else if (ast_format_cap_append(alaw_caps, alaw_format, 0)) {
1057  ast_test_status_update(test, "Could not add alaw format to alaw capabilities\n");
1058  return AST_TEST_FAIL;
1059  } else if (ast_format_cap_iscompatible(ulaw_caps, alaw_caps)) {
1060  ast_test_status_update(test, "Two capability structures that should not be compatible are\n");
1061  return AST_TEST_FAIL;
1062  } else if (!ast_format_cap_iscompatible(ulaw_caps, ulaw_caps)) {
1063  ast_test_status_update(test, "Capability structure is not compatible with itself\n");
1064  return AST_TEST_FAIL;
1065  }
1066 
1067  return AST_TEST_PASS;
1068 }
1069 
1070 AST_TEST_DEFINE(format_cap_get_names)
1071 {
1072  RAII_VAR(struct ast_format_cap *, empty_caps, NULL, ao2_cleanup);
1073  RAII_VAR(struct ast_format_cap *, multi_caps, NULL, ao2_cleanup);
1074  RAII_VAR(struct ast_format_cap *, alaw_caps, NULL, ao2_cleanup);
1075  RAII_VAR(struct ast_format_cap *, ulaw_caps, NULL, ao2_cleanup);
1076  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
1077  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
1078  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
1079  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
1081 
1082  switch (cmd) {
1083  case TEST_INIT:
1084  info->name = "format_cap_get_names";
1085  info->category = "/main/format_cap/";
1086  info->summary = "Test getting the names of formats";
1087  info->description =
1088  "Test that obtaining the names from a format capabilities structure\n"
1089  "produces the expected output.";
1090  return AST_TEST_NOT_RUN;
1091  case TEST_EXECUTE:
1092  break;
1093  }
1094 
1096  if (!empty_caps) {
1097  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1098  return AST_TEST_FAIL;
1099  }
1100 
1102  if (!multi_caps) {
1103  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1104  return AST_TEST_FAIL;
1105  }
1106 
1108  if (!alaw_caps) {
1109  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1110  return AST_TEST_FAIL;
1111  }
1112 
1114  if (!ulaw_caps) {
1115  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1116  return AST_TEST_FAIL;
1117  }
1118 
1119  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
1120  if (!ulaw) {
1121  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
1122  return AST_TEST_FAIL;
1123  }
1124 
1125  ulaw_format = ast_format_create(ulaw);
1126  if (!ulaw_format) {
1127  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
1128  return AST_TEST_FAIL;
1129  }
1130 
1131  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
1132  if (!alaw) {
1133  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
1134  return AST_TEST_FAIL;
1135  }
1136 
1137  alaw_format = ast_format_create(alaw);
1138  if (!alaw_format) {
1139  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
1140  return AST_TEST_FAIL;
1141  }
1142 
1143  if (ast_format_cap_append(ulaw_caps, ulaw_format, 0)) {
1144  ast_test_status_update(test, "Could not add ulaw format to ulaw capabilities\n");
1145  return AST_TEST_FAIL;
1146  } else if (ast_format_cap_append(alaw_caps, alaw_format, 0)) {
1147  ast_test_status_update(test, "Could not add alaw format to alaw capabilities\n");
1148  return AST_TEST_FAIL;
1149  } else if (ast_format_cap_append(multi_caps, ulaw_format, 0)) {
1150  ast_test_status_update(test, "Could not add ulaw format to multi capabilities\n");
1151  return AST_TEST_FAIL;
1152  } else if (ast_format_cap_append(multi_caps, alaw_format, 0)) {
1153  ast_test_status_update(test, "Could not add alaw format to multi capabilities\n");
1154  return AST_TEST_FAIL;
1155  }
1156 
1157  ast_format_cap_get_names(empty_caps, &buffer);
1158  ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(nothing)"));
1159  ast_format_cap_get_names(ulaw_caps, &buffer);
1160  ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(ulaw)"));
1161  ast_format_cap_get_names(alaw_caps, &buffer);
1162  ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(alaw)"));
1163  ast_format_cap_get_names(multi_caps, &buffer);
1164  ast_test_validate(test, !strcmp(ast_str_buffer(buffer), "(ulaw|alaw)"));
1165 
1166 
1167  return AST_TEST_PASS;
1168 }
1169 
1170 AST_TEST_DEFINE(format_cap_best_by_type)
1171 {
1172  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
1173  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
1174  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
1175  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
1176  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
1177  RAII_VAR(struct ast_codec *, h263, NULL, ao2_cleanup);
1178  RAII_VAR(struct ast_format *, h263_format, NULL, ao2_cleanup);
1179  RAII_VAR(struct ast_format *, best_format, NULL, ao2_cleanup);
1180 
1181  switch (cmd) {
1182  case TEST_INIT:
1183  info->name = __PRETTY_FUNCTION__;
1184  info->category = "/main/format_cap/";
1185  info->summary = "format capabilities best by type unit test";
1186  info->description =
1187  "Test that we can get the best format type out of a capabilities structure";
1188  return AST_TEST_NOT_RUN;
1189  case TEST_EXECUTE:
1190  break;
1191  }
1192 
1194  if (!caps) {
1195  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1196  return AST_TEST_FAIL;
1197  }
1198 
1199  ulaw = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
1200  if (!ulaw) {
1201  ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
1202  return AST_TEST_FAIL;
1203  }
1204 
1205  ulaw_format = ast_format_create(ulaw);
1206  if (!ulaw_format) {
1207  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
1208  return AST_TEST_FAIL;
1209  }
1210 
1211  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
1212  if (!alaw) {
1213  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
1214  return AST_TEST_FAIL;
1215  }
1216 
1217  alaw_format = ast_format_create(alaw);
1218  if (!alaw_format) {
1219  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
1220  return AST_TEST_FAIL;
1221  }
1222 
1223  h263 = ast_codec_get("h263", AST_MEDIA_TYPE_VIDEO, 0);
1224  if (!h263) {
1225  ast_test_status_update(test, "Could not retrieve built-in h263 codec\n");
1226  return AST_TEST_FAIL;
1227  }
1228 
1229  h263_format = ast_format_create(h263);
1230  if (!alaw_format) {
1231  ast_test_status_update(test, "Could not create h263 format using built-in codec\n");
1232  return AST_TEST_FAIL;
1233  }
1234 
1235  if (ast_format_cap_append(caps, ulaw_format, 0)) {
1236  ast_test_status_update(test, "Could not add ulaw format to capabilities\n");
1237  return AST_TEST_FAIL;
1238  } else if (ast_format_cap_append(caps, alaw_format, 0)) {
1239  ast_test_status_update(test, "Could not add alaw format to capabilities\n");
1240  return AST_TEST_FAIL;
1241  } else if (ast_format_cap_append(caps, h263_format, 0)) {
1242  ast_test_status_update(test, "Could not add h263 format to capabilities\n");
1243  return AST_TEST_FAIL;
1244  }
1245 
1247  ast_test_validate(test, ast_format_cmp(best_format, ulaw_format) == AST_FORMAT_CMP_EQUAL);
1248  ao2_ref(best_format, -1);
1249 
1251  ast_test_validate(test, ast_format_cmp(best_format, ulaw_format) == AST_FORMAT_CMP_EQUAL);
1252  ao2_ref(best_format, -1);
1253 
1255  ast_test_validate(test, ast_format_cmp(best_format, h263_format) == AST_FORMAT_CMP_EQUAL);
1256  ao2_ref(best_format, -1);
1257 
1259  ast_test_validate(test, best_format == NULL);
1260 
1262  ast_test_validate(test, best_format == NULL);
1263 
1264  return AST_TEST_PASS;
1265 }
1266 
1267 static int test_law_samples(struct ast_frame *frame)
1268 {
1269  return frame->datalen;
1270 }
1271 
1272 static int test_law_length(unsigned int samples)
1273 {
1274  return samples;
1275 }
1276 
1277 static struct ast_codec test_law = {
1278  .name = "test_law",
1279  .description = "format cap unit test codec",
1280  .type = AST_MEDIA_TYPE_AUDIO,
1281  .sample_rate = 8000,
1282  .minimum_ms = 10,
1283  .maximum_ms = 150,
1284  .default_ms = 20,
1285  .samples_count = test_law_samples,
1286  .get_length = test_law_length,
1287  .smooth = 1,
1288 };
1289 
1290 static enum ast_format_cmp_res test_law_cmp(const struct ast_format *format1, const struct ast_format *format2)
1291 {
1292  ast_log(LOG_ERROR, "Comparing format1 %p and format2 %p\n", format1, format2);
1293  return format1 == format2 ? AST_FORMAT_CMP_EQUAL : AST_FORMAT_CMP_NOT_EQUAL;
1294 }
1295 
1296 static void test_law_destroy(struct ast_format *format)
1297 {
1298 }
1299 
1300 static int test_law_clone(const struct ast_format *src, struct ast_format *dst)
1301 {
1302  return 0;
1303 }
1304 
1307  .format_clone = test_law_clone,
1308  .format_destroy = test_law_destroy,
1309 };
1310 
1311 AST_TEST_DEFINE(format_cap_replace_from_cap)
1312 {
1313  RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
1314  RAII_VAR(struct ast_format_cap *, replace_caps, NULL, ao2_cleanup);
1315  RAII_VAR(struct ast_format_cap *, result_caps, NULL, ao2_cleanup);
1316  RAII_VAR(struct ast_codec *, ulaw, NULL, ao2_cleanup);
1317  RAII_VAR(struct ast_format *, ulaw_format, NULL, ao2_cleanup);
1318  RAII_VAR(struct ast_format *, ulaw_format_variant, NULL, ao2_cleanup);
1319  RAII_VAR(struct ast_codec *, alaw, NULL, ao2_cleanup);
1320  RAII_VAR(struct ast_format *, alaw_format, NULL, ao2_cleanup);
1321 
1322  switch (cmd) {
1323  case TEST_INIT:
1324  info->name = __PRETTY_FUNCTION__;
1325  info->category = "/main/format_cap/";
1326  info->summary = "format capabilities adding unit test";
1327  info->description =
1328  "Test that adding multiple formats to a format capabilities structure succeeds";
1329  return AST_TEST_NOT_RUN;
1330  case TEST_EXECUTE:
1331  break;
1332  }
1333 
1337  if (!caps || !replace_caps || !result_caps) {
1338  ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
1339  return AST_TEST_FAIL;
1340  }
1341 
1342  ulaw = ast_codec_get("test_law", AST_MEDIA_TYPE_AUDIO, 8000);
1343  if (!ulaw) {
1344  ast_test_status_update(test, "Could not retrieve test_law codec\n");
1345  return AST_TEST_FAIL;
1346  }
1347 
1348  ulaw_format = ast_format_create(ulaw);
1349  if (!ulaw_format) {
1350  ast_test_status_update(test, "Could not create ulaw format using built-in codec\n");
1351  return AST_TEST_FAIL;
1352  }
1353 
1354  ulaw_format_variant = ast_format_create(ulaw);
1355  if (!ulaw_format_variant) {
1356  ast_test_status_update(test, "Could not create ulaw format variant using built-in codec\n");
1357  return AST_TEST_FAIL;
1358  }
1359 
1360  alaw = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
1361  if (!alaw) {
1362  ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
1363  return AST_TEST_FAIL;
1364  }
1365 
1366  alaw_format = ast_format_create(alaw);
1367  if (!alaw_format) {
1368  ast_test_status_update(test, "Could not create alaw format using built-in codec\n");
1369  return AST_TEST_FAIL;
1370  }
1371 
1372  /* fill caps with ulaw and alaw */
1373  if (ast_format_cap_append(caps, ulaw_format, 42)) {
1374  ast_test_status_update(test, "Could not add ulaw format to capabilities structure\n");
1375  return AST_TEST_FAIL;
1376  }
1377  if (ast_format_cap_append(caps, alaw_format, 84)) {
1378  ast_test_status_update(test, "Could not add alaw format to capabilities structure\n");
1379  return AST_TEST_FAIL;
1380  }
1381  if (ast_format_cap_count(caps) != 2) {
1382  ast_test_status_update(test, "Number of formats in capabilities structure should be 2 but is %zu\n",
1383  ast_format_cap_count(caps));
1384  return AST_TEST_FAIL;
1385  }
1386 
1387  /* fill replace_caps with the ulaw variant */
1388  if (ast_format_cap_append(replace_caps, ulaw_format_variant, 42)) {
1389  ast_test_status_update(test, "Could not add ulaw format to capabilities structure\n");
1390  return AST_TEST_FAIL;
1391  }
1392  if (ast_format_cap_count(replace_caps) != 1) {
1393  ast_test_status_update(test, "Number of formats in capabilities structure should be 1 but is %zu\n",
1394  ast_format_cap_count(replace_caps));
1395  return AST_TEST_FAIL;
1396  }
1397 
1398  /* fill result_caps with ulaw_variant and alaw */
1399  if (ast_format_cap_append(result_caps, ulaw_format_variant, 42)) {
1400  ast_test_status_update(test, "Could not add ulaw variant to capabilities structure\n");
1401  return AST_TEST_FAIL;
1402  }
1403  if (ast_format_cap_append(result_caps, alaw_format, 84)) {
1404  ast_test_status_update(test, "Could not add alaw format to capabilities structure\n");
1405  return AST_TEST_FAIL;
1406  }
1407  if (ast_format_cap_count(result_caps) != 2) {
1408  ast_test_status_update(test, "Number of formats in capabilities structure should be 2 but is %zu\n",
1409  ast_format_cap_count(result_caps));
1410  return AST_TEST_FAIL;
1411  }
1412 
1413  /* replace caps formats from replace_caps */
1415 
1416  /* compare result_caps with caps */
1417  if (!ast_format_cap_identical(caps, result_caps)) {
1418  ast_test_status_update(test, "Actual and expected result caps differ\n");
1419  return AST_TEST_FAIL;
1420  }
1421 
1422  return AST_TEST_PASS;
1423 }
1424 
1425 static int unload_module(void)
1426 {
1427  AST_TEST_UNREGISTER(format_cap_alloc);
1428  AST_TEST_UNREGISTER(format_cap_append_single);
1429  AST_TEST_UNREGISTER(format_cap_append_multiple);
1430  AST_TEST_UNREGISTER(format_cap_append_all_unknown);
1431  AST_TEST_UNREGISTER(format_cap_append_all_audio);
1432  AST_TEST_UNREGISTER(format_cap_append_duplicate);
1433  AST_TEST_UNREGISTER(format_cap_append_from_cap);
1434  AST_TEST_UNREGISTER(format_cap_append_from_cap_duplicate);
1435  AST_TEST_UNREGISTER(format_cap_set_framing);
1436  AST_TEST_UNREGISTER(format_cap_remove_single);
1437  AST_TEST_UNREGISTER(format_cap_remove_multiple);
1438  AST_TEST_UNREGISTER(format_cap_remove_bytype);
1439  AST_TEST_UNREGISTER(format_cap_remove_all);
1440  AST_TEST_UNREGISTER(format_cap_get_names);
1441  AST_TEST_UNREGISTER(format_cap_get_compatible_format);
1442  AST_TEST_UNREGISTER(format_cap_iscompatible_format);
1443  AST_TEST_UNREGISTER(format_cap_get_compatible);
1444  AST_TEST_UNREGISTER(format_cap_iscompatible);
1445  AST_TEST_UNREGISTER(format_cap_best_by_type);
1446  AST_TEST_UNREGISTER(format_cap_replace_from_cap);
1447  return 0;
1448 }
1449 
1450 static int load_module(void)
1451 {
1452  AST_TEST_REGISTER(format_cap_alloc);
1453  AST_TEST_REGISTER(format_cap_append_single);
1454  AST_TEST_REGISTER(format_cap_append_multiple);
1455  AST_TEST_REGISTER(format_cap_append_all_unknown);
1456  AST_TEST_REGISTER(format_cap_append_all_audio);
1457  AST_TEST_REGISTER(format_cap_append_duplicate);
1458  AST_TEST_REGISTER(format_cap_append_from_cap);
1459  AST_TEST_REGISTER(format_cap_append_from_cap_duplicate);
1460  AST_TEST_REGISTER(format_cap_set_framing);
1461  AST_TEST_REGISTER(format_cap_remove_single);
1462  AST_TEST_REGISTER(format_cap_remove_multiple);
1463  AST_TEST_REGISTER(format_cap_remove_bytype);
1464  AST_TEST_REGISTER(format_cap_remove_all);
1465  AST_TEST_REGISTER(format_cap_get_names);
1466  AST_TEST_REGISTER(format_cap_get_compatible_format);
1467  AST_TEST_REGISTER(format_cap_iscompatible_format);
1468  AST_TEST_REGISTER(format_cap_get_compatible);
1469  AST_TEST_REGISTER(format_cap_iscompatible);
1470  AST_TEST_REGISTER(format_cap_best_by_type);
1471  AST_TEST_REGISTER(format_cap_replace_from_cap);
1472  ast_codec_register(&test_law);
1473  ast_format_interface_register("test_law", &test_law_interface);
1474  return AST_MODULE_LOAD_SUCCESS;
1475 }
1476 
1477 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Format capabilities API test module");
static void test_law_destroy(struct ast_format *format)
static int test_law_clone(const struct ast_format *src, struct ast_format *dst)
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
const char * name
Name for this codec.
Definition: codec.h:46
Asterisk main include file. File version handling, generic pbx functions.
unsigned int ast_format_cap_get_framing(const struct ast_format_cap *cap)
Get the global framing.
Definition: format_cap.c:438
Optional format interface to extend format operations.
Definition: format.h:44
AST_TEST_DEFINE(format_cap_alloc)
static struct ast_codec ulaw
static int test_law_length(unsigned int samples)
static int load_module(void)
struct ast_format * ast_format_create(struct ast_codec *codec)
Create a new media format.
Definition: format.c:196
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:326
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
Codec API.
Definition of a media format.
Definition: format.c:43
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition: format_cap.c:216
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
struct ast_format * ast_format_cap_get_best_by_type(const struct ast_format_cap *cap, enum ast_media_type type)
Get the most preferred format for a particular media type.
Definition: format_cap.c:417
#define ast_str_alloca(init_len)
Definition: strings.h:800
void ast_format_cap_replace_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Replace the formats of provided type in dst with equivalent formats from src.
Definition: format_cap.c:306
void ast_format_cap_set_framing(struct ast_format_cap *cap, unsigned int framing)
Set the global framing.
Definition: format_cap.c:136
struct ast_codec * ast_codec_get(const char *name, enum ast_media_type type, unsigned int sample_rate)
Retrieve a codec given a name, type, and sample rate.
Definition: codec.c:326
#define NULL
Definition: resample.c:96
enum ast_format_cmp_res(*const format_cmp)(const struct ast_format *format1, const struct ast_format *format2)
Determine if format 1 is a subset of format 2.
Definition: format.h:71
static struct ast_codec alaw
static struct ast_format_interface test_law_interface
static int test_law_samples(struct ast_frame *frame)
int ast_codec_get_max(void)
Retrieve the current maximum identifier for codec iteration.
Definition: codec.c:342
Media Format API.
enum ast_format_cmp_res ast_format_cap_iscompatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if ast_format is within the capabilities of the ast_format_cap object.
Definition: format_cap.c:583
#define ast_log
Definition: astobj2.c:42
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
Asterisk internal frame definitions.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_format * ast_format_cap_get_compatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if input ast_format is within the capabilities of the ast_format_cap object then return the comp...
Definition: format_cap.c:548
ast_format_cmp_res
Format comparison results.
Definition: format.h:34
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
#define ast_codec_register(codec)
This function is used to register a codec with the Asterisk core. Registering allows it to be passed ...
Definition: codec.h:122
void ast_format_cap_remove_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Remove all formats matching a specific format type.
Definition: format_cap.c:525
Format Capabilities API.
#define LOG_ERROR
Definition: logger.h:285
static struct ast_codec h263
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
static enum ast_format_cmp_res test_law_cmp(const struct ast_format *format1, const struct ast_format *format2)
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
#define ast_format_interface_register(codec, interface)
Register a format interface for use with the provided codec.
Definition: format.h:273
def info(msg)
unsigned int ast_format_cap_get_format_framing(const struct ast_format_cap *cap, const struct ast_format *format)
Get the framing for a format.
Definition: format_cap.c:443
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:736
unsigned int ast_format_get_default_ms(const struct ast_format *format)
Get the default framing size (in milliseconds) for a format.
Definition: format.c:359
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if two capabilities structures are identical.
Definition: format_cap.c:689
static struct ast_codec test_law
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_format * ast_format_create_named(const char *format_name, struct ast_codec *codec)
Create a new media format with a specific name.
Definition: format.c:157
Data structure associated with a single frame of data.
static int unload_module(void)
int ast_format_cap_remove(struct ast_format_cap *cap, struct ast_format *format)
Remove format capability from capability structure.
Definition: format_cap.c:497
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Append the formats of provided type in src to dst.
Definition: format_cap.c:269
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_format_cap_get_compatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, struct ast_format_cap *result)
Find the compatible formats between two capabilities structures.
Definition: format_cap.c:630
static snd_pcm_format_t format
Definition: chan_alsa.c:102
Represents a media codec within Asterisk.
Definition: codec.h:42
int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_media_type type)
Find out if the capabilities structure has any formats of a specific type.
Definition: format_cap.c:615
int ast_format_cap_iscompatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if any joint capabilities exist between two capabilities structures.
Definition: format_cap.c:655