Ignore:
Timestamp:
Jun 24, 2009 2:46:36 PM (15 years ago)
Author:
nanang
Message:

Ticket #763: backported changes from ticket #762

Location:
pjproject/branches/1.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/1.0

  • pjproject/branches/1.0/pjmedia/src/test/jbuf_test.c

    r2394 r2788  
    2727#define JB_MAX_PREFETCH     10 
    2828#define JB_PTIME            20 
    29 #define JB_BUF_SIZE         20 
     29#define JB_BUF_SIZE         50 
    3030 
    3131#define REPORT 
    3232//#define PRINT_COMMENT 
    3333 
     34typedef struct test_param_t { 
     35    pj_bool_t adaptive; 
     36    unsigned init_prefetch; 
     37    unsigned min_prefetch; 
     38    unsigned max_prefetch; 
     39} test_param_t; 
     40 
     41typedef struct test_cond_t { 
     42    int burst; 
     43    int discard; 
     44    int lost; 
     45    int empty; 
     46    int delay;      /**< Maximum delay, in frames.          */ 
     47} test_cond_t; 
     48 
     49static pj_bool_t parse_test_headers(char *line, test_param_t *param,  
     50                               test_cond_t *cond) 
     51{ 
     52    char *p = line; 
     53 
     54    if (*p == '%') { 
     55        /* Test params. */ 
     56        char mode_st[16]; 
     57 
     58        sscanf(p+1, "%s %u %u %u", mode_st, &param->init_prefetch,  
     59               &param->min_prefetch, &param->max_prefetch); 
     60        param->adaptive = (pj_ansi_stricmp(mode_st, "adaptive") == 0); 
     61 
     62    } else if (*p == '!') { 
     63        /* Success condition. */ 
     64        char cond_st[16]; 
     65        unsigned cond_val; 
     66 
     67        sscanf(p+1, "%s %u", cond_st, &cond_val); 
     68        if (pj_ansi_stricmp(cond_st, "burst") == 0) 
     69            cond->burst = cond_val; 
     70        else if (pj_ansi_stricmp(cond_st, "delay") == 0) 
     71            cond->delay = cond_val; 
     72        else if (pj_ansi_stricmp(cond_st, "discard") == 0) 
     73            cond->discard = cond_val; 
     74        else if (pj_ansi_stricmp(cond_st, "empty") == 0) 
     75            cond->empty = cond_val; 
     76        else if (pj_ansi_stricmp(cond_st, "lost") == 0) 
     77            cond->lost = cond_val; 
     78 
     79    } else if (*p == '=') { 
     80        /* Test title. */ 
     81        ++p; 
     82        while (*p && isspace(*p)) ++p; 
     83        printf("%s", p); 
     84    } else { 
     85        /* Unknown header, perhaps this is the test data */ 
     86 
     87        /* Skip spaces */ 
     88        while (*p && isspace(*p)) ++p; 
     89 
     90        /* Test data started.*/ 
     91        if (*p != 0) 
     92            return PJ_FALSE; 
     93    } 
     94 
     95    return PJ_TRUE; 
     96} 
     97 
     98static pj_bool_t process_test_data(char data, pjmedia_jbuf *jb,  
     99                                   pj_uint16_t *seq, pj_uint16_t *last_seq) 
     100{ 
     101    char frame[1]; 
     102    char f_type; 
     103    pj_bool_t print_state = PJ_TRUE; 
     104    pj_bool_t data_eos = PJ_FALSE; 
     105 
     106    switch (toupper(data)) { 
     107    case 'G': /* Get */ 
     108        pjmedia_jbuf_get_frame(jb, frame, &f_type); 
     109        break; 
     110    case 'P': /* Put */ 
     111        pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq); 
     112        *last_seq = *seq; 
     113        ++*seq; 
     114        break; 
     115    case 'L': /* Lost */ 
     116        *last_seq = *seq; 
     117        ++*seq; 
     118        printf("Lost\n"); 
     119        break; 
     120    case 'R': /* Sequence restarts */ 
     121        *seq = 1; 
     122        printf("Sequence restarting, from %u to %u\n", *last_seq, *seq); 
     123        break; 
     124    case 'J': /* Sequence jumps */ 
     125        (*seq) += 5000; 
     126        printf("Sequence jumping, from %u to %u\n", *last_seq, *seq); 
     127        break; 
     128    case 'D': /* Frame duplicated */ 
     129        pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq - 1); 
     130        break; 
     131    case 'O': /* Old/late frame */ 
     132        pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq - 10 - pj_rand()%40); 
     133        break; 
     134    case '.': /* End of test session. */ 
     135        data_eos = PJ_TRUE; 
     136        break; 
     137    default: 
     138        print_state = PJ_FALSE; 
     139        printf("Unknown test data '%c'\n", data); 
     140        break; 
     141    } 
     142 
     143    if (data_eos) 
     144        return PJ_FALSE; 
     145 
     146#ifdef REPORT 
     147    if (print_state) { 
     148        pjmedia_jb_state state; 
     149 
     150        pjmedia_jbuf_get_state(jb, &state); 
     151        printf("seq=%d\t%c\tsize=%d\tprefetch=%d\n",  
     152               *last_seq, toupper(data), state.size, state.prefetch); 
     153    } 
     154#endif 
     155 
     156    return PJ_TRUE; 
     157} 
     158 
    34159int jbuf_main(void) 
    35160{ 
    36     pjmedia_jbuf *jb; 
    37161    FILE *input = fopen("JBTEST.DAT", "rt"); 
    38     unsigned seq; 
    39     char line[1024 * 10], *p; 
    40     pj_pool_t *pool; 
    41     pjmedia_jb_state state; 
    42     pj_str_t jb_name = {"JBTEST", 6}; 
    43  
    44     pj_init(); 
    45     pool = pj_pool_create(mem, "JBPOOL", 256*16, 256*16, NULL); 
    46  
    47     pjmedia_jbuf_create(pool, &jb_name, 1, JB_PTIME, JB_BUF_SIZE, &jb); 
    48     pjmedia_jbuf_set_adaptive(jb, JB_INIT_PREFETCH, JB_MIN_PREFETCH,  
    49                               JB_MAX_PREFETCH); 
    50  
    51     while ((p=fgets(line, sizeof(line), input)) != NULL) { 
    52  
    53         while (*p && isspace(*p)) 
    54             ++p; 
    55  
    56         if (!*p) 
    57             continue; 
    58  
    59         if (*p == '#') { 
    60 #ifdef PRINT_COMMENT 
    61             printf("%s", p); 
    62 #endif 
    63             continue; 
    64         } 
    65  
     162    pj_bool_t data_eof = PJ_FALSE; 
     163    int old_log_level; 
     164    int rc = 0; 
     165 
     166    old_log_level = pj_log_get_level(); 
     167    pj_log_set_level(5); 
     168 
     169    while (rc == 0 && !data_eof) { 
     170        pj_str_t jb_name = {"JBTEST", 6}; 
     171        pjmedia_jbuf *jb; 
     172        pj_pool_t *pool; 
     173        pjmedia_jb_state state; 
     174        pj_uint16_t last_seq = 0; 
     175        pj_uint16_t seq = 1; 
     176        char line[1024], *p = NULL; 
     177 
     178        test_param_t param; 
     179        test_cond_t cond; 
     180 
     181        param.adaptive = PJ_TRUE; 
     182        param.init_prefetch = JB_INIT_PREFETCH; 
     183        param.min_prefetch = JB_MIN_PREFETCH; 
     184        param.max_prefetch = JB_MAX_PREFETCH; 
     185 
     186        cond.burst = -1; 
     187        cond.delay = -1; 
     188        cond.discard = -1; 
     189        cond.empty = -1; 
     190        cond.lost = -1; 
     191 
     192        printf("\n\n"); 
     193 
     194        /* Parse test session title, param, and conditions */ 
     195        do { 
     196            p = fgets(line, sizeof(line), input); 
     197        } while (p && parse_test_headers(line, &param, &cond)); 
     198 
     199        /* EOF test data */ 
     200        if (p == NULL) 
     201            break; 
     202 
     203        //printf("======================================================\n"); 
     204 
     205        /* Initialize test session */ 
     206        pool = pj_pool_create(mem, "JBPOOL", 256*16, 256*16, NULL); 
     207        pjmedia_jbuf_create(pool, &jb_name, 1, JB_PTIME, JB_BUF_SIZE, &jb); 
    66208        pjmedia_jbuf_reset(jb); 
    67         seq = 1; 
     209 
     210        if (param.adaptive) { 
     211            pjmedia_jbuf_set_adaptive(jb,  
     212                                      param.init_prefetch,  
     213                                      param.min_prefetch, 
     214                                      param.max_prefetch); 
     215        } else { 
     216            pjmedia_jbuf_set_fixed(jb, param.init_prefetch); 
     217        } 
    68218 
    69219#ifdef REPORT 
    70220        pjmedia_jbuf_get_state(jb, &state); 
    71221        printf("Initial\tsize=%d\tprefetch=%d\tmin.pftch=%d\tmax.pftch=%d\n",  
    72             state.size, state.prefetch, state.min_prefetch, state.max_prefetch); 
     222               state.size, state.prefetch, state.min_prefetch,  
     223               state.max_prefetch); 
    73224#endif 
    74225 
    75         while (*p) { 
     226 
     227        /* Test session start */ 
     228        while (1) { 
    76229            int c; 
    77             char frame[1]; 
    78             char f_type; 
    79230             
     231            /* Get next line of test data */ 
     232            if (!p || *p == 0) { 
     233                p = fgets(line, sizeof(line), input); 
     234                if (p == NULL) { 
     235                    data_eof = PJ_TRUE; 
     236                    break; 
     237                } 
     238            } 
     239 
     240            /* Get next char of test data */ 
    80241            c = *p++; 
     242 
     243            /* Skip spaces */ 
    81244            if (isspace(c)) 
    82245                continue; 
    83              
    84             if (c == '/') { 
    85                 putchar('\n'); 
    86  
    87                 while (*++p && *p != '/') 
    88                     putchar(*p); 
    89  
    90                 putchar('\n'); 
    91  
    92                 if (*++p == 0) 
    93                     break; 
    94  
     246 
     247            /* Print comment line */ 
     248            if (c == '#') { 
     249#ifdef PRINT_COMMENT 
     250                while (*p && isspace(*p)) ++p; 
     251                if (*p) printf("..%s", p); 
     252#endif 
     253                *p = 0; 
    95254                continue; 
    96255            } 
    97256 
    98             switch (toupper(c)) { 
    99             case 'G': 
    100                 pjmedia_jbuf_get_frame(jb, frame, &f_type); 
     257            /* Process test data */ 
     258            if (!process_test_data(c, jb, &seq, &last_seq)) 
    101259                break; 
    102             case 'P': 
    103                 pjmedia_jbuf_put_frame(jb, (void*)frame, 1, seq); 
    104                 seq++; 
    105                 break; 
    106             case 'L': 
    107                 seq++; 
    108                 printf("Lost\n"); 
    109                 break; 
    110             default: 
    111                 printf("Unknown character '%c'\n", c); 
    112                 break; 
    113             } 
    114  
    115 #ifdef REPORT 
    116             if (toupper(c) != 'L') { 
    117                 pjmedia_jbuf_get_state(jb, &state); 
    118                 printf("seq=%d\t%c\tsize=%d\tprefetch=%d\n",  
    119                        seq, toupper(c), state.size, state.prefetch); 
    120             } 
    121 #endif 
    122         } 
     260        } 
     261 
     262        /* Print JB states */ 
     263        pjmedia_jbuf_get_state(jb, &state); 
     264        printf("------------------------------------------------------\n"); 
     265        printf("Summary:\n"); 
     266        printf("  size=%d prefetch=%d\n", state.size, state.prefetch); 
     267        printf("  delay (min/max/avg/dev)=%d/%d/%d/%d ms\n", 
     268               state.min_delay, state.max_delay, state.avg_delay,  
     269               state.dev_delay); 
     270        printf("  lost=%d discard=%d empty=%d burst(avg)=%d\n",  
     271               state.lost, state.discard, state.empty, state.avg_burst); 
     272 
     273        /* Evaluate test session */ 
     274        if (cond.burst >= 0 && (int)state.avg_burst > cond.burst) { 
     275            printf("! 'Burst' should be %d, it is %d\n",  
     276                   cond.burst, state.avg_burst); 
     277            rc |= 1; 
     278        } 
     279        if (cond.delay >= 0 && (int)state.avg_delay/JB_PTIME > cond.delay) { 
     280            printf("! 'Delay' should be %d, it is %d\n",  
     281                   cond.delay, state.avg_delay/JB_PTIME); 
     282            rc |= 2; 
     283        } 
     284        if (cond.discard >= 0 && (int)state.discard > cond.discard) { 
     285            printf("! 'Discard' should be %d, it is %d\n", 
     286                   cond.discard, state.discard); 
     287            rc |= 4; 
     288        } 
     289        if (cond.empty >= 0 && (int)state.empty > cond.empty) { 
     290            printf("! 'Empty' should be %d, it is %d\n",  
     291                   cond.empty, state.empty); 
     292            rc |= 8; 
     293        } 
     294        if (cond.lost >= 0 && (int)state.lost > cond.lost) { 
     295            printf("! 'Lost' should be %d, it is %d\n",  
     296                   cond.lost, state.lost); 
     297            rc |= 16; 
     298        } 
     299 
     300        pjmedia_jbuf_destroy(jb); 
     301        pj_pool_release(pool); 
    123302    } 
    124303 
    125     pjmedia_jbuf_destroy(jb); 
    126  
    127     if (input != stdin) 
    128         fclose(input); 
    129  
    130     pj_pool_release(pool); 
    131     return 0; 
     304    fclose(input); 
     305    pj_log_set_level(old_log_level); 
     306 
     307    return rc; 
    132308} 
Note: See TracChangeset for help on using the changeset viewer.