summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/audio/tdav_jitterbuffer.c
blob: b2cd287b61d051c53dd4ca119af6df15926b2e1a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
/* File from: http://cms.speakup.nl/tech/opensource/jitterbuffer/verslag-20051209.pdf/ */

/*******************************************************
* jitterbuffer:
* an application-independent jitterbuffer, which tries
* to achieve the maximum user perception during a call.
* For more information look at:
* http://www.speakup.nl/opensource/jitterbuffer/
*
* Copyright on this file is held by:
* - Jesse Kaijen <jesse@speakup.nl>
* - SpeakUp <info@speakup.nl>
*
* Contributors:
* Jesse Kaijen <jesse@speakup.nl>
*
* This program is free software, distributed under the terms of:
* - the GNU Lesser (Library) General Public License
* - the Mozilla Public License
*
* if you are interested in an different licence type, please contact us.
*
* How to use the jitterbuffer, please look at the comments
* in the headerfile.
*
* Further details on specific implementations,
* please look at the comments in the code file.
*/
#include "tinydav/audio/tdav_jitterbuffer.h"

#if !(HAVE_SPEEX_DSP && HAVE_SPEEX_JB)

#include "tsk_memory.h"

#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define jb_warn(...) (warnf ? warnf(__VA_ARGS__) : (void)0)
#define jb_err(...) (errf ? errf(__VA_ARGS__) : (void)0)
#define jb_dbg(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0)

//public functions
jitterbuffer *jb_new();
void jb_reset(jitterbuffer *jb);
void jb_reset_all(jitterbuffer *jb);
void jb_destroy(jitterbuffer *jb);
void jb_set_settings(jitterbuffer *jb, jb_settings *settings);

void jb_get_info(jitterbuffer *jb, jb_info *stats);
void jb_get_settings(jitterbuffer *jb, jb_settings *settings);
float jb_guess_mos(float p, long d, int codec);
int jb_has_frames(jitterbuffer *jb);

void jb_put(jitterbuffer *jb, void *data, int type, long ms, long ts, long now, int codec);
int jb_get(jitterbuffer *jb, void **data, long now, long interpl);



//private functions
static void set_default_settings(jitterbuffer *jb);
static void reset(jitterbuffer *jb);
static long find_pointer(long *array, long max_index, long value);
static void frame_free(jb_frame *frame);

static void put_control(jitterbuffer *jb, void *data, int type, long ts);
static void put_voice(jitterbuffer *jb, void *data, int type, long ms, long ts, int codec);
static void put_history(jitterbuffer *jb, long ts, long now, long ms, int codec);
static void calculate_info(jitterbuffer *jb, long ts, long now, int codec);

static int get_control(jitterbuffer *jb, void **data);
static int get_voice(jitterbuffer *jb, void **data, long now, long interpl);
static int get_voicecase(jitterbuffer *jb, void **data, long now, long interpl, long diff);

static int get_next_frametype(jitterbuffer *jb, long ts);
static long get_next_framets(jitterbuffer *jb);
static jb_frame *get_frame(jitterbuffer *jb, long ts);
static jb_frame *get_all_frames(jitterbuffer *jb);

//debug...
static jb_output_function_t warnf, errf, dbgf;
void jb_setoutput(jb_output_function_t warn, jb_output_function_t err, jb_output_function_t dbg)
{
    warnf = warn;
    errf = err;
    dbgf = dbg;
}


/***********
 * create a new jitterbuffer
 * return NULL if malloc doesn't work
 * else return jb with default_settings.
 */
jitterbuffer *jb_new()
{
    jitterbuffer *jb;

    jb_dbg("N");
    jb = tsk_calloc(1, sizeof(jitterbuffer));
    if (!jb) {
        jb_err("cannot allocate jitterbuffer\n");
        return NULL;
    }
    set_default_settings(jb);
    reset(jb);
    return jb;
}


/***********
 * empty voice messages
 * reset statistics
 * keep the settings
 */
void jb_reset(jitterbuffer *jb)
{
    jb_frame *frame;

    jb_dbg("R");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_reset()\n");
        return;
    }

    //free voice
    while(jb->voiceframes) {
        frame = get_all_frames(jb);
        frame_free(frame);
    }
    //reset stats
    memset(&(jb->info),0,sizeof(jb_info) );
    // set default settings
    reset(jb);
}


/***********
 * empty nonvoice messages
 * empty voice messages
 * reset statistics
 * reset settings to default
 */
void jb_reset_all(jitterbuffer *jb)
{
    jb_frame *frame;

    jb_dbg("r");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_reset_all()\n");
        return;
    }

    // free nonvoice
    while(jb->controlframes) {
        frame = jb->controlframes;
        jb->controlframes = frame->next;
        frame_free(frame);
    }
    // free voice and reset statistics is done by jb_reset
    jb_reset(jb);
    set_default_settings(jb);
}


/***********
 * destroy the jitterbuffer
 * free all the [non]voice frames with reset_all
 * free the jitterbuffer
 */
void jb_destroy(jitterbuffer *jb)
{
    jb_dbg("D");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_destroy()\n");
        return;
    }

    jb_reset_all(jb);
    free(jb);
}


/***********
 * Set settings for the jitterbuffer.
 * Only if a setting is defined it will be written
 * in the jb->settings.
 * This means that no setting can be set to zero
 */
void jb_set_settings(jitterbuffer *jb, jb_settings *settings)
{
    jb_dbg("S");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_set_settings()\n");
        return;
    }

    if (settings->min_jb) {
        jb->settings.min_jb = settings->min_jb;
    }
    if (settings->max_jb) {
        jb->settings.max_jb = settings->max_jb;
    }
    if (settings->max_successive_interp) {
        jb->settings.max_successive_interp = settings->max_successive_interp;
    }
    if (settings->extra_delay) {
        jb->settings.extra_delay = settings->extra_delay;
    }
    if (settings->wait_grow) {
        jb->settings.wait_grow = settings->wait_grow;
    }
    if (settings->wait_shrink) {
        jb->settings.wait_shrink = settings->wait_shrink;
    }
    if (settings->max_diff) {
        jb->settings.max_diff = settings->max_diff;
    }
}


/***********
 * validates the statistics
 * the losspct due the jitterbuffer will be calculated.
 * delay and delay_target will be calculated
 * *stats = info
 */
void jb_get_info(jitterbuffer *jb, jb_info *stats)
{
    long max_index, pointer;

    jb_dbg("I");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_get_info()\n");
        return;
    }

    jb->info.delay = jb->current - jb->min;
    jb->info.delay_target = jb->target - jb->min;

    //calculate the losspct...
    max_index = (jb->hist_pointer < JB_HISTORY_SIZE) ?
                jb->hist_pointer : JB_HISTORY_SIZE-1;
    if (max_index>1) {
        pointer = find_pointer(&jb->hist_sorted_delay[0], max_index,
                               jb->current);
        jb->info.losspct = ((max_index - pointer)*100/max_index);
        if (jb->info.losspct < 0) {
            jb->info.losspct = 0;
        }
    }
    else {
        jb->info.losspct = 0;
    }

    *stats = jb->info;
}


/***********
 * gives the settings for this jitterbuffer
 * *settings = settings
 */
void jb_get_settings(jitterbuffer *jb, jb_settings *settings)
{
    jb_dbg("S");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_get_settings()\n");
        return;
    }

    *settings = jb->settings;
}


/***********
 * returns an estimate on the MOS with given loss, delay and codec
 * if the formula is not present the default will be used
 * please use the JB_CODEC_OTHER if you want to define your own formula
 *
 */
float jb_guess_mos(float p, long d, int codec)
{
    float result;

    switch (codec) {
    case JB_CODEC_GSM_EFR:
        result = (4.31f - 0.23f*p - 0.0071f*d);
        break;

    case JB_CODEC_G723_1:
        result = (3.99f - 0.16f*p - 0.0071f*d);
        break;

    case JB_CODEC_G729:
    case JB_CODEC_G729A:
        result = (4.13f - 0.14f*p - 0.0071f*d);
        break;

    case JB_CODEC_G711x_PLC:
        result = (4.42f - 0.087f*p - 0.0071f*d);
        break;

    case JB_CODEC_G711x:
        result = (4.42f - 0.63f*p - 0.0071f*d);
        break;

    case JB_CODEC_OTHER:
    default:
        result = (4.42f - 0.63f*p - 0.0071f*d);

    }
    return result;
}


/***********
 * if there are any frames left in JB returns JB_OK, otherwise returns JB_EMPTY
 */
int jb_has_frames(jitterbuffer *jb)
{
    jb_dbg("H");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_has_frames()\n");
        return JB_NOJB;
    }

    if(jb->controlframes || jb->voiceframes) {
        return JB_OK;
    }
    else {
        return JB_EMPTY;
    }
}


/***********
 * Put a packet into the jitterbuffers
 * Only the timestamps of voicepackets are put in the history
 * this because the jitterbuffer only works for voicepackets
 * don't put packets twice in history and queue (e.g. transmitting every frame twice)
 * keep track of statistics
 */
void jb_put(jitterbuffer *jb, void *data, int type, long ms, long ts, long now, int codec)
{
    long pointer, max_index;

    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_put()\n");
        return;
    }

    jb->info.frames_received++;

    if (type == JB_TYPE_CONTROL) {
        //put the packet into the contol-queue of the jitterbuffer
        jb_dbg("pC");
        put_control(jb,data,type,ts);

    }
    else if (type == JB_TYPE_VOICE) {
        // only add voice that aren't already in the buffer
        max_index = (jb->hist_pointer < JB_HISTORY_SIZE) ? jb->hist_pointer : JB_HISTORY_SIZE-1;
        pointer = find_pointer(&jb->hist_sorted_timestamp[0], max_index, ts);
        if (jb->hist_sorted_timestamp[pointer]==ts) { //timestamp already in queue
            jb_dbg("pT");
            free(data);
            jb->info.frames_dropped_twice++;
        }
        else {   //add
            jb_dbg("pV");
            /* add voicepacket to history */
            put_history(jb,ts,now,ms,codec);
            /*calculate jitterbuffer size*/
            calculate_info(jb, ts, now, codec);
            /*put the packet into the queue of the jitterbuffer*/
            put_voice(jb,data,type,ms,ts,codec);
        }

    }
    else if (type == JB_TYPE_SILENCE) {  //silence
        jb_dbg("pS");
        put_voice(jb,data,type,ms,ts,codec);

    }
    else {  //should NEVER happen
        jb_err("jb_put(): type not known\n");
        free(data);
    }
}


/***********
 * control frames have a higher priority then voice frames
 * returns JB_OK if a frame is available and *data points to the packet
 * returns JB_NOFRAME if it's no time to play voice and no control available
 * returns JB_INTERP if interpolating is required
 * returns JB_EMPTY if no voice frame is in the jitterbuffer (only during silence)
 */
int jb_get(jitterbuffer *jb, void **data, long now, long interpl)
{
    int result;

    jb_dbg("A");
    if (jb == NULL) {
        jb_err("no jitterbuffer in jb_get()\n");
        return JB_NOJB;
    }

    result = get_control(jb, data);
    if (result != JB_OK ) { //no control message available maybe there is voice...
        result = get_voice(jb, data, now, interpl);
    }
    return result;
}


/***********
 * set all the settings to default
 */
static void set_default_settings(jitterbuffer *jb)
{
    jb->settings.min_jb = JB_MIN_SIZE;
    jb->settings.max_jb = JB_MAX_SIZE;
    jb->settings.max_successive_interp = JB_MAX_SUCCESSIVE_INTERP;
    jb->settings.extra_delay = JB_ALLOW_EXTRA_DELAY;
    jb->settings.wait_grow = JB_WAIT_GROW;
    jb->settings.wait_shrink = JB_WAIT_SHRINK;
    jb->settings.max_diff = JB_MAX_DIFF;
}


/***********
 * reset the jitterbuffer so we can start in silence and
 * we start with a new history
 */
static void reset(jitterbuffer *jb)
{
    jb->hist_pointer = 0; //start over
    jb->silence_begin_ts = 0; //no begin_ts defined
    jb->info.silence =1; //we always start in silence
}


/***********
 * Search algorithm
 * @REQUIRE max_index is within array
 *
 * Find the position of value in hist_sorted_delay
 * if value doesn't exist return first pointer where array[low]>value
 * int low;   //the lowest index being examined
 * int max_index; //the highest index being examined
 * int mid;  //the middle index between low and max_index.
 * mid ==(low+max_index)/2
 * at the end low is the position of value or where array[low]>value
 */
static long find_pointer(long *array, long max_index, long value)
{
    register long low, mid, high;
    low = 0;
    high = max_index;
    while (low<=high) {
        mid= (low+high)/2;
        if (array[mid] < value) {
            low = mid+1;
        }
        else {
            high = mid-1;
        }
    }
    while(low < max_index && (array[low]==array[(low+1)]) ) {
        low++;
    }
    return low;
}


/***********
 * free the given frame, afterwards the framepointer is undefined
 */
static void frame_free(jb_frame *frame)
{
    if (frame->data) {
        free(frame->data);
    }
    free(frame);
}


/***********
 * put a nonvoice frame into the nonvoice queue
 */
static void put_control(jitterbuffer *jb, void *data, int type, long ts)
{
    jb_frame *frame, *p;

    frame = malloc(sizeof(jb_frame));
    if(!frame) {
        jb_err("cannot allocate frame\n");
        return;
    }
    frame->data = data;
    frame->ts = ts;
    frame->type = type;
    frame->next = NULL;
    data = NULL;//to avoid stealing memory

    p = jb->controlframes;
    if (p) { //there are already control messages
        if (ts < p->ts) {
            jb->controlframes = frame;
            frame->next = p;
        }
        else {
            while (p->next && (ts >=p->next->ts)) {//sort on timestamps! so find place to put...
                p = p->next;
            }
            if (p->next) {
                frame->next = p->next;
            }
            p->next = frame;
        }
    }
    else {
        jb->controlframes = frame;
    }
}


/***********
 * put a voice or silence frame into the jitterbuffer
 */
static void put_voice(jitterbuffer *jb, void *data, int type, long ms, long ts, int codec)
{
    jb_frame *frame, *p;
    frame = malloc(sizeof(jb_frame));
    if(!frame) {
        jb_err("cannot allocate frame\n");
        return;
    }

    frame->data = data;
    frame->ts = ts;
    frame->ms = ms;
    frame->type = type;
    frame->codec = codec;

    data = NULL; //to avoid stealing the memory location
    /*
     * frames are a circular list, jb->voiceframes points to to the lowest ts,
     * jb->voiceframes->prev points to the highest ts
     */
    if(!jb->voiceframes) {  /* queue is empty */
        jb->voiceframes = frame;
        frame->next = frame;
        frame->prev = frame;
    }
    else {
        p = jb->voiceframes;
        if(ts < p->prev->ts) { //frame is out of order
            jb->info.frames_ooo++;
        }
        if (ts < p->ts) { //frame is lowest, let voiceframes point to it!
            jb->voiceframes = frame;
        }
        else {
            while(ts < p->prev->ts ) {
                p = p->prev;
            }
        }
        frame->next = p;
        frame->prev = p->prev;
        frame->next->prev = frame;
        frame->prev->next = frame;
    }
}


/***********
 * puts the timestamps of a received packet in the history of *jb
 * for later calculations of the size of jitterbuffer *jb.
 *
 * summary of function:
 * - calculate delay difference
 * - delete old value from hist & sorted_history_delay & sorted_history_timestamp if needed
 * - add new value to history & sorted_history_delay & sorted_history_timestamp
 * - we keep sorted_history_delay for calculations
 * - we keep sorted_history_timestamp for ensuring each timestamp isn't put twice in the buffer.
 */
static void put_history(jitterbuffer *jb, long ts, long now, long ms, int codec)
{
    jb_hist_element out, in;
    long max_index, pointer, location;

    // max_index is the highest possible index
    max_index = (jb->hist_pointer < JB_HISTORY_SIZE) ? jb->hist_pointer : JB_HISTORY_SIZE-1;
    location = (jb->hist_pointer % JB_HISTORY_SIZE);

    // we want to delete a value from the jitterbuffer
    // only when we are through the history.
    if (jb->hist_pointer > JB_HISTORY_SIZE-1) {
        /* the value we need to delete from sorted histories */
        out = jb->hist[location];
        //delete delay from hist_sorted_delay
        pointer = find_pointer(&jb->hist_sorted_delay[0], max_index, out.delay);
        /* move over pointer is the position of kicked*/
        if (pointer<max_index) { //only move if we have something to move
            memmove(  &(jb->hist_sorted_delay[pointer]),
                      &(jb->hist_sorted_delay[pointer+1]),
                      ((JB_HISTORY_SIZE-(pointer+1)) * sizeof(long)) );
        }

        //delete timestamp from hist_sorted_timestamp
        pointer = find_pointer(&jb->hist_sorted_timestamp[0], max_index, out.ts);
        /* move over pointer is the position of kicked*/
        if (pointer<max_index) { //only move if we have something to move
            memmove(  &(jb->hist_sorted_timestamp[pointer]),
                      &(jb->hist_sorted_timestamp[pointer+1]),
                      ((JB_HISTORY_SIZE-(pointer+1)) * sizeof(long)) );
        }
    }

    in.delay = now - ts;    //delay of current packet
    in.ts = ts;      //timestamp of current packet
    in.ms = ms;      //length of current packet
    in.codec = codec;      //codec of current packet

    /* adding the new delay to the sorted history
     * first special cases:
     * - delay is the first history stamp
     * - delay > highest history stamp
     */
    if (max_index==0 || in.delay >= jb->hist_sorted_delay[max_index-1]) {
        jb->hist_sorted_delay[max_index] = in.delay;
    }
    else {
        pointer = find_pointer(&jb->hist_sorted_delay[0], (max_index-1), in.delay);
        /* move over and add delay */
        memmove(  &(jb->hist_sorted_delay[pointer+1]),
                  &(jb->hist_sorted_delay[pointer]),
                  ((JB_HISTORY_SIZE-(pointer+1)) * sizeof(long)) );
        jb->hist_sorted_delay[pointer] = in.delay;
    }

    /* adding the new timestamp to the sorted history
     * first special cases:
     * - timestamp is the first history stamp
     * - timestamp > highest history stamp
     */
    if (max_index==0 || in.ts >= jb->hist_sorted_timestamp[max_index-1]) {
        jb->hist_sorted_timestamp[max_index] = in.ts;
    }
    else {

        pointer = find_pointer(&jb->hist_sorted_timestamp[0], (max_index-1), in.ts);
        /* move over and add timestamp */
        memmove(  &(jb->hist_sorted_timestamp[pointer+1]),
                  &(jb->hist_sorted_timestamp[pointer]),
                  ((JB_HISTORY_SIZE-(pointer+1)) * sizeof(long)) );
        jb->hist_sorted_timestamp[pointer] = in.ts;
    }

    /* put the jb_hist_element in the history
    * then increase hist_pointer for next time
    */
    jb->hist[location] = in;
    jb->hist_pointer++;
}


/***********
 * this tries to make a jitterbuffer that behaves like
 * the jitterbuffer proposed in this article:
 * Adaptive Playout Buffer Algorithm for Enhancing Perceived Quality of Streaming Applications
 * by: Kouhei Fujimoto & Shingo Ata & Masayuki Murata
 * http://www.nal.ics.es.osaka-u.ac.jp/achievements/web2002/pdf/journal/k-fujimo02TSJ-AdaptivePlayoutBuffer.pdf
 *
 * it calculates jitter and minimum delay
 * get the best delay for the specified codec

 */
static void calculate_info(jitterbuffer *jb, long ts, long now, int codec)
{
    long diff, size, max_index, d, d1, d2, n;
    float p, p1, p2, A, B;
    //size = how many items there in the history
    size = (jb->hist_pointer < JB_HISTORY_SIZE) ? jb->hist_pointer : JB_HISTORY_SIZE;
    max_index = size-1;

    /*
     * the Inter-Quartile Range can be used for estimating jitter
     * http://www.slac.stanford.edu/comp/net/wan-mon/tutorial.html#variable
     * just take the square root of the iqr for jitter
     */
    jb->info.iqr = jb->hist_sorted_delay[max_index*3/4] - jb->hist_sorted_delay[max_index/4];


    /*
     * The RTP way of calculating jitter.
     * This one is used at the moment, although it is not correct.
     * But in this way the other side understands us.
     */
    diff = now - ts - jb->last_delay;
    if (!jb->last_delay) {
        diff = 0; //this to make sure we won't get odd jitter due first ts.
    }
    jb->last_delay = now - ts;
    if (diff <0) {
        diff = -diff;
    }
    jb->info.jitter = jb->info.jitter + (diff - jb->info.jitter)/16;

    /* jb->min is minimum delay in hist_sorted_delay, we don't look at the lowest 2% */
    /* because sometimes there are odd delays in there */
    jb->min = jb->hist_sorted_delay[(max_index*2/100)];

    /*
     * calculating the preferred size of the jitterbuffer:
     * instead of calculating the optimum delay using the Pareto equation
     * I use look at the array of sorted delays and choose my optimum from there
     * always walk trough a percentage of the history this because imagine following tail:
     * [...., 12, 300, 301 ,302]
     * her we want to discard last three but that won't happen if we won't walk the array
     * the number of frames we walk depends on how scattered the sorted delays are.
     * For that we look at the iqr. The dependencies of the iqr are based on
     * tests we've done here in the lab. But are not optimized.
     */
    //init:
    //the higest delay..
    d = d1= d2 = jb->hist_sorted_delay[max_index]- jb->min;
    A=B=LONG_MIN;
    p = p2 =0;
    n=0;
    p1 = 5; //always look at the top 5%
    if (jb->info.iqr >200) { //with more jitter look at more delays
        p1=25;
    }
    else if (jb->info.iqr >100) {
        p1=20;
    }
    else if (jb->info.iqr >50) {
        p1=11;
    }

    //find the optimum delay..
    while(max_index>10 && (B > A ||p2<p1)) { // By MDI: from ">=" to ">"
        //the packetloss with this delay
        p2 =(n*100.0f/size);
        // estimate MOS-value
        B = jb_guess_mos(p2,d2,codec);
        if (B > A) {
            p = p2;
            d = d2;
            A = B;
        }
        d1 = d2;
        //find next delay != delay so the same delay isn't calculated twice
        //don't look further if we have seen half of the history
        while((d2>=d1) && ((n*2)<max_index) ) {
            n++;
            d2 = jb->hist_sorted_delay[(max_index-n)] - jb->min;
        }
    }
    //the targeted size of the jitterbuffer
    if (jb->settings.min_jb && (jb->settings.min_jb > d) ) {
        jb->target = jb->min + jb->settings.min_jb;
    }
    else if (jb->settings.max_jb && (jb->settings.max_jb > d) ) {
        jb->target = jb->min + jb->settings.max_jb;
    }
    else {
        jb->target = jb->min + d;
    }
}


/***********
 * if there is a nonvoice frame it will be returned [*data] and the frame
 * will be made free
 */
static int get_control(jitterbuffer *jb, void **data)
{
    jb_frame *frame;
    int result;

    frame = jb->controlframes;
    if (frame) {
        jb_dbg("gC");
        *data = frame->data;
        frame->data = NULL;
        jb->controlframes = frame->next;
        frame_free(frame);
        result = JB_OK;
    }
    else {
        result = JB_NOFRAME;
    }
    return result;
}


/***********
 * returns JB_OK if a frame is available and *data points to the packet
 * returns JB_NOFRAME if it's no time to play voice and or no frame available
 * returns JB_INTERP if interpolating is required
 * returns JB_EMPTY if no voice frame is in the jitterbuffer (only during silence)
 *
 * if the next frame is a silence frame we will go in silence-mode
 * each new instance of the jitterbuffer will start in silence mode
 * in silence mode we will set the jitterbuffer to the size we want
 * when we are not in silence mode get_voicecase will handle the rest.
 */
static int get_voice(jitterbuffer *jb, void **data, long now, long interpl)
{
    jb_frame *frame;
    long diff;
    int result;

    diff = jb->target - jb->current;

    //if the next frame is a silence frame, go in silence mode...
    if((get_next_frametype(jb, now - jb->current) == JB_TYPE_SILENCE) ) {
        jb_dbg("gs");
        frame = get_frame(jb, now - jb->current);
        *data = frame->data;
        frame->data = NULL;
        jb->info.silence =1;
        jb->silence_begin_ts = frame->ts;
        frame_free(frame);
        result = JB_OK;
    }
    else {
        if(jb->info.silence) { // we are in silence
            /*
             * During silence we can set the jitterbuffer size to the size
             * we want...
             */
            if (diff) {
                jb->current = jb->target;
            }
            frame = get_frame(jb, now - jb->current);
            if (frame) {
                if (jb->silence_begin_ts && frame->ts < jb->silence_begin_ts) {
                    jb_dbg("gL");
                    /* voice frame is late, next!*/
                    jb->info.frames_late++;
                    frame_free(frame);
                    result = get_voice(jb, data, now, interpl);
                }
                else {
                    jb_dbg("gP");
                    /* voice frame */
                    jb->info.silence = 0;
                    jb->silence_begin_ts = 0;
                    jb->next_voice_time = frame->ts + frame->ms;
                    jb->info.last_voice_ms = frame->ms;
                    *data = frame->data;
                    frame->data = NULL;
                    frame_free(frame);
                    result = JB_OK;
                }
            }
            else {      //no frame
                jb_dbg("gS");
                result = JB_EMPTY;
            }
        }
        else {   //voice case
            result = get_voicecase(jb,data,now,interpl,diff);
        }
    }
    return result;
}


/***********
 * The voicecase has four 'options'
 * - difference is way off, reset
 * - diff > 0, we may need to grow
 * - diff < 0, we may need to shrink
 * - everything else
 */
static int get_voicecase(jitterbuffer *jb, void **data, long now, long interpl, long diff)
{
    jb_frame *frame;
    int result;

    // * - difference is way off, reset
    if (diff > jb->settings.max_diff || -diff > jb->settings.max_diff) {
        jb_err("wakko diff in get_voicecase\n");
        reset(jb); //reset hist because the timestamps are wakko.
        result = JB_NOFRAME;
        //- diff > 0, we may need to grow
    }
    else if ((diff > 0) &&
             (now > (jb->last_adjustment + jb->settings.wait_grow)
              || (now + jb->current + interpl) < get_next_framets(jb) ) ) { //grow
        /* first try to grow */
        if (diff<interpl/2) {
            jb_dbg("ag");
            jb->current +=diff;
        }
        else {
            jb_dbg("aG");
            /* grow by interp frame len */
            jb->current += interpl;
        }
        jb->last_adjustment = now;
        result = get_voice(jb, data, now, interpl);
        //- diff < 0, we may need to shrink
    }
    else if ( (diff < 0)
              && (now > (jb->last_adjustment + jb->settings.wait_shrink))
              && ((-diff) > jb->settings.extra_delay) ) {
        /* now try to shrink
         * if there is a frame shrink by frame length
         * otherwise shrink by interpl
         */
        jb->last_adjustment = now;

        frame = get_frame(jb, now - jb->current);
        if(frame) {
            jb_dbg("as");
            /* shrink by frame size we're throwing out */
            jb->info.frames_dropped++;
            jb->current -= frame->ms;
            frame_free(frame);
        }
        else {
            jb_dbg("aS");
            /* shrink by interpl */
            jb->current -= interpl;
        }
        result = get_voice(jb, data, now, interpl);
    }
    else  {
        /* if it is not the time to play a result = JB_NOFRAME
         * else We try to play a frame if a frame is available
         * and not late it is played otherwise
         * if available it is dropped and the next is tried
         * last option is interpolating
         */
        if (now - jb->current < jb->next_voice_time) {
            jb_dbg("aN");
            result = JB_NOFRAME;
        }
        else {
            frame = get_frame(jb, now - jb->current);
            if (frame) { //there is a frame
                /* voice frame is late */
                if(frame->ts < jb->next_voice_time) {   //late
                    jb_dbg("aL");
                    jb->info.frames_late++;
                    frame_free(frame);
                    result = get_voice(jb, data, now, interpl);
                }
                else {
                    jb_dbg("aP");
                    /* normal case; return the frame, increment stuff */
                    *data = frame->data;
                    frame->data = NULL;
                    jb->next_voice_time = frame->ts + frame->ms;
                    jb->cnt_successive_interp = 0;
                    frame_free(frame);
                    result = JB_OK;
                }
            }
            else {   // no frame, thus interpolate
                jb->cnt_successive_interp++;
                /* assume silence instead of continuing to interpolate */
                if (jb->settings.max_successive_interp && jb->cnt_successive_interp >= jb->settings.max_successive_interp) {
                    jb->info.silence = 1;
                    jb->silence_begin_ts = jb->next_voice_time;
                }
                jb_dbg("aI");
                jb->next_voice_time += interpl;
                result = JB_INTERP;
            }
        }
    }
    return result;

}


/***********
 * if there are frames and next frame->ts is smaller or equal ts
 *   return type of next frame.
 * else return 0
 */
static int get_next_frametype(jitterbuffer *jb, long ts)
{
    jb_frame *frame;
    int result;

    result = 0;
    frame = jb->voiceframes;
    if (frame && frame->ts <= ts) {
        result = frame->type;
    }
    return result;
}


/***********
 * returns ts from next frame in jb->voiceframes
 * or returns LONG_MAX if there is no frame
 */
static long get_next_framets(jitterbuffer *jb)
{
    if (jb->voiceframes) {
        return jb->voiceframes->ts;
    }
    return LONG_MAX;
}


/***********
 * if there is a frame in jb->voiceframes and
 * has a timestamp smaller/equal to ts
 * this frame will be returned and
 * removed from the queue
 */
static jb_frame *get_frame(jitterbuffer *jb, long ts)
{
    jb_frame *frame;

    frame = jb->voiceframes;
    if (frame && frame->ts <= ts) {
        if(frame->next == frame) {
            jb->voiceframes = NULL;
        }
        else {
            /* remove this frame */
            frame->prev->next = frame->next;
            frame->next->prev = frame->prev;
            jb->voiceframes = frame->next;
        }
        return frame;
    }
    return NULL;
}

/***********
 * if there is a frame in jb->voiceframes
 * this frame will be unconditionally returned and
 * removed from the queue
 */
static jb_frame *get_all_frames(jitterbuffer *jb)
{
    jb_frame *frame;

    frame = jb->voiceframes;
    if (frame) {
        if(frame->next == frame) {
            jb->voiceframes = NULL;
        }
        else {
            /* remove this frame */
            frame->prev->next = frame->next;
            frame->next->prev = frame->prev;
            jb->voiceframes = frame->next;
        }
        return frame;
    }
    return NULL;
}


#endif // !(HAVE_SPEEX_DSP && HAVE_SPEEX_JB)
OpenPOWER on IntegriCloud