Skip to content
Snippets Groups Projects
test_jitterbuf.c 34.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2012, Matt Jordan
     *
     * Matt Jordan <mjordan@digium.com>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*!
     * \file
     * \brief Unit tests for jitterbuf.c
     *
     * \author\verbatim Matt Jordan <mjordan@digium.com> \endverbatim
     *
     * \ingroup tests
     */
    
    /*** MODULEINFO
    	<depend>TEST_FRAMEWORK</depend>
    	<support_level>core</support_level>
     ***/
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    #include "asterisk/utils.h"
    #include "asterisk/module.h"
    #include "asterisk/test.h"
    #include "jitterbuf.h"
    
    #define DEFAULT_MAX_JITTERBUFFER 1000
    #define DEFAULT_RESYNCH_THRESHOLD 1000
    #define DEFAULT_MAX_CONTIG_INTERP 10
    #define DEFAULT_TARGET_EXTRA -1
    #define DEFAULT_CODEC_INTERP_LEN 20
    
    /*! \internal
     * Test two numeric (long int) values.  Failure automatically attempts
     * to jump to a cleanup tag
     */
    #define JB_NUMERIC_TEST(attribute, expected) do { \
    	if ((attribute) != (expected)) { \
    		ast_test_status_update(test, #attribute ": expected [%ld]; actual [%ld]\n", (long int)(expected), (attribute)); \
    		goto cleanup; \
    	} \
    } while (0)
    
    /*! \internal
     * Print out as debug the frame related contents of a jb_info object
     */
    #define JB_INFO_PRINT_FRAME_DEBUG(jbinfo) do { \
    	ast_debug(1, "JitterBuffer Frame Info:\n" \
    		"\tFrames In: %ld\n\tFrames Out: %ld\n" \
    		"\tDropped Frames: %ld\n\tLate Frames: %ld\n" \
    		"\tLost Frames: %ld\n\tOut of Order Frames: %ld\n" \
    		"\tCurrent Frame: %ld\n", jbinfo.frames_in, jbinfo.frames_out, \
    		jbinfo.frames_dropped, jbinfo.frames_late, jbinfo.frames_lost, \
    		jbinfo.frames_ooo, jbinfo.frames_cur); \
    } while (0)
    
    /*! \internal
     * This macro installs the error, warning, and debug functions for a test.  It is
     * expected that at the end of a test, the functions are removed.
     * Note that the debug statement is in here merely to aid in tracing in a log where
     * the jitter buffer debug output begins.
     */
    #define JB_TEST_BEGIN(test_name) do { \
    	jb_setoutput(test_jb_error_output, test_jb_warn_output, test_jb_debug_output); \
    	ast_debug(1, "Starting %s\n", test_name); \
    } while (0)
    
    /*! \internal
     * Uninstall the error, warning, and debug functions from a test
     */
    #define JB_TEST_END do { \
    	jb_setoutput(NULL, NULL, NULL); \
    } while (0)
    
    static const char *jitter_buffer_return_codes[] = {
    	"JB_OK",            /* 0 */
    	"JB_EMPTY",         /* 1 */
    	"JB_NOFRAME",       /* 2 */
    	"JB_INTERP",        /* 3 */
    	"JB_DROP",          /* 4 */
    	"JB_SCHED"          /* 5 */
    };
    
    /*! \internal \brief Make a default jitter buffer configuration */
    static void test_jb_populate_config(struct jb_conf *jbconf)
    {
    	if (!jbconf) {
    		return;
    	}
    
    	jbconf->max_jitterbuf = DEFAULT_MAX_JITTERBUFFER;
    	jbconf->resync_threshold = DEFAULT_RESYNCH_THRESHOLD;
    	jbconf->max_contig_interp = DEFAULT_MAX_CONTIG_INTERP;
    	jbconf->target_extra = 0;
    }
    
    /*! \internal \brief Debug callback function for the jitter buffer's jb_dbg function */
    static void __attribute__((format(printf, 1, 2))) test_jb_debug_output(const char *fmt, ...)
    {
    	va_list args;
    	char buf[1024];
    
    	va_start(args, fmt);
    	vsnprintf(buf, sizeof(buf), fmt, args);
    	va_end(args);
    
    	ast_debug(1, "%s", buf);
    }
    
    /*! \internal \brief Warning callback function for the jitter buffer's jb_warn function */
    static void __attribute__((format(printf, 1, 2))) test_jb_warn_output(const char *fmt, ...)
    {
    	va_list args;
    	char buf[1024];
    
    	va_start(args, fmt);
    	vsnprintf(buf, sizeof(buf), fmt, args);
    	va_end(args);
    
    	ast_log(AST_LOG_WARNING, "%s", buf);
    }
    
    /*! \internal \brief Error callback function for the jitter buffer's jb_err function */
    static void __attribute__((format(printf, 1, 2))) test_jb_error_output(const char *fmt, ...)
    {
    	va_list args;
    	char buf[1024];
    
    	va_start(args, fmt);
    	vsnprintf(buf, sizeof(buf), fmt, args);
    	va_end(args);
    
    	ast_log(AST_LOG_ERROR, "%s", buf);
    }
    
    /*! \internal \brief Insert frames into the jitter buffer for the nominal tests */
    static int test_jb_nominal_frame_insertion(struct ast_test *test, struct jitterbuf *jb, enum jb_frame_type frame_type)
    {
    	int i = 0, ret = 0;
    
    	for (i = 0; i < 40; i++) {
    		if (jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 5) == JB_DROP) {
    			ast_test_status_update(test, "Jitter buffer dropped packet %d\n", i);
    			ret = 1;
    			break;
    		}
    	}
    
    	return ret;
    }
    
    AST_TEST_DEFINE(jitterbuffer_nominal_voice_frames)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_conf jbconf;
    	struct jb_info jbinfo;
    	int i = 0;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_nominal_voice_frames";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Nominal operation of jitter buffer with audio data";
    		info->description =
    			"Tests the nominal case of putting audio data into a jitter buffer, "
    			"retrieving the frames, and querying for the next frame";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_nominal_voice_frames");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_nominal_frame_insertion(test, jb, JB_TYPE_VOICE)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		/* We should have a frame for each point in time */
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			ast_test_status_update(test,
    				"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    				jitter_buffer_return_codes[ret], i);
    			goto cleanup;
    		}
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    		JB_NUMERIC_TEST(jb_next(jb), (i + 1) * 20 + 5);
    	}
    
    	result = AST_TEST_PASS;
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    AST_TEST_DEFINE(jitterbuffer_nominal_control_frames)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_conf jbconf;
    	struct jb_info jbinfo;
    	int i = 0;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_nominal_control_frames";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Nominal operation of jitter buffer with control frames";
    		info->description =
    			"Tests the nominal case of putting control frames into a jitter buffer, "
    			"retrieving the frames, and querying for the next frame";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_nominal_control_frames");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_nominal_frame_insertion(test, jb, JB_TYPE_CONTROL)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		/* We should have a frame for each point in time */
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			ast_test_status_update(test,
    				"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    				jitter_buffer_return_codes[ret], i);
    			goto cleanup;
    		}
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    /*! \internal \brief Insert frames into the jitter buffer for the out of order tests */
    static int test_jb_out_of_order_frame_insertion(struct ast_test *test, struct jitterbuf *jb, enum jb_frame_type frame_type)
    {
    	int i = 0, ret = 0;
    
    	for (i = 0; i < 40; i++) {
    		if (i % 4 == 0) {
    			/* Add the next frame */
    			if (jb_put(jb, NULL, frame_type, 20, (i + 1) * 20, (i + 1) * 20 + 5) == JB_DROP) {
    				ast_test_status_update(test, "Jitter buffer dropped packet %d\n", (i+1));
    				ret = 1;
    				break;
    			}
    			/* Add the current frame */
    			if (jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 5) == JB_DROP) {
    				ast_test_status_update(test, "Jitter buffer dropped packet %d\n", i);
    				ret = 1;
    				break;
    			}
    			i++;
    		} else {
    			if (jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 5) == JB_DROP) {
    				ast_test_status_update(test, "Jitter buffer dropped packet %d\n", i);
    				ret = 1;
    				break;
    			}
    		}
    	}
    
    	return ret;
    }
    
    AST_TEST_DEFINE(jitterbuffer_out_of_order_voice)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_info jbinfo;
    	struct jb_conf jbconf;
    	int i;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_out_of_order_voice";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests sending out of order audio frames to a jitter buffer";
    		info->description =
    			"Every 5th frame sent to a jitter buffer is reversed with the previous "
    			"frame.  The expected result is to have a jitter buffer with the frames "
    			"in order, while a total of 10 frames should be recorded as having been "
    			"received out of order.";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_out_of_order_voice");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_out_of_order_frame_insertion(test, jb, JB_TYPE_VOICE)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		/* We should have a frame for each point in time */
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			ast_test_status_update(test,
    				"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    				jitter_buffer_return_codes[ret], i);
    			goto cleanup;
    		}
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 10);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    AST_TEST_DEFINE(jitterbuffer_out_of_order_control)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_info jbinfo;
    	struct jb_conf jbconf;
    	int i;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_out_of_order_voice";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests sending out of order audio frames to a jitter buffer";
    		info->description =
    			"Every 5th frame sent to a jitter buffer is reversed with the previous "
    			"frame.  The expected result is to have a jitter buffer with the frames "
    			"in order, while a total of 10 frames should be recorded as having been "
    			"received out of order.";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_out_of_order_control");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_out_of_order_frame_insertion(test, jb, JB_TYPE_CONTROL)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		/* We should have a frame for each point in time */
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			ast_test_status_update(test,
    				"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    				jitter_buffer_return_codes[ret], i);
    			goto cleanup;
    		}
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 10);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    /*! \internal \brief Insert frames into the jitter buffer for the lost frame tests */
    static int test_jb_lost_frame_insertion(struct ast_test *test, struct jitterbuf *jb, enum jb_frame_type frame_type)
    {
    	int i = 0, ret = 0;
    
    	for (i = 0; i < 40; i++) {
    		if (i % 5 == 0) {
    			i++;
    		}
    		if (jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 5) == JB_DROP) {
    			ast_test_status_update(test, "Jitter buffer dropped packet %d\n", i);
    			ret = 1;
    			break;
    		}
    	}
    
    	return ret;
    }
    
    AST_TEST_DEFINE(jitterbuffer_lost_voice)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_conf jbconf;
    	struct jb_info jbinfo;
    	int i;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_lost_voice";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests missing frames in the jitterbuffer";
    		info->description =
    			"Every 5th frame that would be sent to a jitter buffer is instead"
    			"dropped.  When reading data from the jitter buffer, the jitter buffer"
    			"should interpolate the voice frame.";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_lost_voice");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_lost_frame_insertion(test, jb, JB_TYPE_VOICE)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			/* If we didn't get an OK, make sure that it was an expected lost frame */
    			if (!((ret == JB_INTERP && i % 5 == 0) || (ret == JB_NOFRAME && i == 0))) {
    				ast_test_status_update(test,
    					"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    					jitter_buffer_return_codes[ret], i);
    				goto cleanup;
    			}
    		} else {
    			JB_NUMERIC_TEST(frame.ms, 20);
    			JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    		}
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	/* Note: The first frame (at i = 0) never got added, so nothing existed at that point.
    	 * Its neither dropped nor lost.
    	 */
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 7);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 32);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 32);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    AST_TEST_DEFINE(jitterbuffer_lost_control)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_conf jbconf;
    	struct jb_info jbinfo;
    	int i;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_lost_control";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests missing frames in the jitterbuffer";
    		info->description =
    			"Every 5th frame that would be sent to a jitter buffer is instead"
    			"dropped.  When reading data from the jitter buffer, the jitter buffer"
    			"simply reports that no frame exists for that time slot";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_lost_control");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_lost_frame_insertion(test, jb, JB_TYPE_CONTROL)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			/* If we didn't get an OK, make sure that it was an expected lost frame */
    			if (!(ret == JB_NOFRAME && i % 5 == 0)) {
    				ast_test_status_update(test,
    					"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    					jitter_buffer_return_codes[ret], i);
    				goto cleanup;
    			}
    		} else {
    			JB_NUMERIC_TEST(frame.ms, 20);
    			JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    		}
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	/* Note: The first frame (at i = 0) never got added, so nothing existed at that point.
    	 * Its neither dropped nor lost.
    	 */
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 32);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 32);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    /*! \internal \brief Insert frames into the jitter buffer for the late frame tests */
    static int test_jb_late_frame_insertion(struct ast_test *test, struct jitterbuf *jb, enum jb_frame_type frame_type)
    {
    	int i = 0, ret = 0;
    
    	for (i = 0; i < 40; i++) {
    		if (i % 5 == 0) {
    			/* Add 5th frame */
    			if (jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 20) == JB_DROP) {
    				ast_test_status_update(test, "Jitter buffer dropped packet %d\n", (i+1));
    				ret = 1;
    				break;
    			}
    		} else {
    			if (jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 5) == JB_DROP) {
    				ast_test_status_update(test, "Jitter buffer dropped packet %d\n", i);
    				ret = 1;
    				break;
    			}
    		}
    	}
    
    	return ret;
    }
    
    AST_TEST_DEFINE(jitterbuffer_late_voice)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_info jbinfo;
    	struct jb_conf jbconf;
    	int i;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_late_voice";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests sending frames to a jitter buffer that arrive late";
    		info->description =
    			"Every 5th frame sent to a jitter buffer arrives late, but still in "
    			"order with respect to the previous and next packet";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_late_voice");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_late_frame_insertion(test, jb, JB_TYPE_VOICE)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		/* We should have a frame for each point in time */
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			ast_test_status_update(test,
    				"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    				jitter_buffer_return_codes[ret], i);
    			goto cleanup;
    		}
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    AST_TEST_DEFINE(jitterbuffer_late_control)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_info jbinfo;
    	struct jb_conf jbconf;
    	int i;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_late_control";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests sending frames to a jitter buffer that arrive late";
    		info->description =
    			"Every 5th frame sent to a jitter buffer arrives late, but still in "
    			"order with respect to the previous and next packet";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_late_voice");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	if (test_jb_late_frame_insertion(test, jb, JB_TYPE_CONTROL)) {
    		goto cleanup;
    	}
    
    	for (i = 0; i < 40; i++) {
    		enum jb_return_code ret;
    		/* We should have a frame for each point in time */
    		if ((ret = jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN)) != JB_OK) {
    			ast_test_status_update(test,
    				"Unexpected jitter buffer return code [%s] when retrieving frame %d\n",
    				jitter_buffer_return_codes[ret], i);
    			goto cleanup;
    		}
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 0);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 40);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 0);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    /*! \internal \brief Insert frames into the jitter buffer for the overflow tests */
    static void test_jb_overflow_frame_insertion(struct jitterbuf *jb, enum jb_frame_type frame_type)
    {
    	int i = 0;
    
    	for (i = 0; i < 100; i++) {
    		jb_put(jb, NULL, frame_type, 20, i * 20, i * 20 + 5);
    	}
    }
    
    AST_TEST_DEFINE(jitterbuffer_overflow_voice)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_info jbinfo;
    	struct jb_conf jbconf;
    	int i = 0;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_overflow_voice";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests overfilling a jitter buffer with voice frames";
    		info->description = "Tests overfilling a jitter buffer with voice frames";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_overflow_voice");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}
    
    	test_jb_populate_config(&jbconf);
    	if (jb_setconf(jb, &jbconf) != JB_OK) {
    		ast_test_status_update(test, "Failed to set jitterbuffer configuration\n");
    		goto cleanup;
    	}
    
    	test_jb_overflow_frame_insertion(jb, JB_TYPE_VOICE);
    
    	while (jb_get(jb, &frame, i * 20 + 5, DEFAULT_CODEC_INTERP_LEN) == JB_OK) {
    		JB_NUMERIC_TEST(frame.ms, 20);
    		JB_NUMERIC_TEST(frame.ts, i * 20 - jb->info.resync_offset);
    		++i;
    	}
    
    	if (jb_getinfo(jb, &jbinfo) != JB_OK) {
    		ast_test_status_update(test, "Failed to get jitterbuffer information\n");
    		goto cleanup;
    	}
    
    	JB_INFO_PRINT_FRAME_DEBUG(jbinfo);
    	JB_NUMERIC_TEST(jbinfo.frames_dropped, 49);
    	JB_NUMERIC_TEST(jbinfo.frames_out, 51);
    	JB_NUMERIC_TEST(jbinfo.frames_in, 51);
    	JB_NUMERIC_TEST(jbinfo.frames_late, 0);
    	/* Note that the last frame will be interpolated */
    	JB_NUMERIC_TEST(jbinfo.frames_lost, 1);
    	JB_NUMERIC_TEST(jbinfo.frames_ooo, 0);
    
    	result = AST_TEST_PASS;
    
    cleanup:
    	if (jb) {
    		/* No need to do anything - this will put all frames on the 'free' list,
    		 * so jb_destroy will dispose of them */
    		while (jb_getall(jb, &frame) == JB_OK) { }
    		jb_destroy(jb);
    	}
    
    	JB_TEST_END;
    
    	return result;
    }
    
    AST_TEST_DEFINE(jitterbuffer_overflow_control)
    {
    	enum ast_test_result_state result = AST_TEST_FAIL;
    	struct jitterbuf *jb = NULL;
    	struct jb_frame frame;
    	struct jb_info jbinfo;
    	struct jb_conf jbconf;
    	int i = 0;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "jitterbuffer_overflow_control";
    		info->category = "/main/jitterbuf/";
    		info->summary = "Tests overfilling a jitter buffer with control frames";
    		info->description = "Tests overfilling a jitter buffer with control frames";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	JB_TEST_BEGIN("jitterbuffer_overflow_control");
    
    	if (!(jb = jb_new())) {
    		ast_test_status_update(test, "Failed to allocate memory for jitterbuffer\n");
    		goto cleanup;
    	}