Ignore:
Timestamp:
Mar 24, 2006 8:44:27 PM (18 years ago)
Author:
bennylp
Message:

Added more samples: WAV recorder, resample, etc., and also moved some common functions to util.h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/samples/util.h

    r328 r361  
    1  
    2 /* Include all PJSIP core headers. */ 
    3 #include <pjsip.h> 
    4  
    5 /* Include all PJMEDIA headers. */ 
    6 #include <pjmedia.h> 
    7  
    8 /* Include all PJMEDIA-CODEC headers. */ 
    9 #include <pjmedia-codec.h> 
    10  
    11 /* Include all PJSIP-UA headers */ 
    12 #include <pjsip_ua.h> 
    13  
    14 /* Include all PJSIP-SIMPLE headers */ 
    15 #include <pjsip_simple.h> 
    16  
    17 /* Include all PJLIB-UTIL headers. */ 
    18 #include <pjlib-util.h> 
    19  
    20 /* Include all PJLIB headers. */ 
    21 #include <pjlib.h> 
    221 
    232 
    24 /* Global endpoint instance. */ 
    25 static pjsip_endpoint *g_endpt; 
    26  
    27 /* Global caching pool factory. */ 
    28 static pj_caching_pool cp; 
    29  
    30 /* Global media endpoint. */ 
    31 static pjmedia_endpt *g_med_endpt; 
    32  
    33 /*  
    34  * Show error. 
    35  */ 
     3/* Util to display the error message for the specified error code  */ 
    364static int app_perror( const char *sender, const char *title,  
    375                       pj_status_t status) 
     
    419    pj_strerror(status, errmsg, sizeof(errmsg)); 
    4210 
    43     PJ_LOG(1,(sender, "%s: %s [code=%d]", title, errmsg, status)); 
     11    PJ_LOG(3,(sender, "%s: %s [code=%d]", title, errmsg, status)); 
    4412    return 1; 
    4513} 
    4614 
     15 
     16 
     17/* Constants */ 
     18#define CLOCK_RATE      44100 
     19#define NSAMPLES        (CLOCK_RATE * 20 / 1000) 
     20#define NCHANNELS       1 
     21#define NBITS           16 
     22 
    4723/* 
    48  * Perform the very basic initialization: 
    49  *  - init PJLIB. 
    50  *  - init memory pool 
    51  *  - create SIP endpoint instance. 
     24 * Common sound options. 
    5225 */ 
    53 static pj_status_t util_init(void) 
     26#define SND_USAGE   \ 
     27"  -d, --dev=NUM        Sound device use device id NUM (default=-1)      \n"\ 
     28"  -r, --rate=HZ        Set clock rate in samples per sec (default=44100)\n"\ 
     29"  -c, --channel=NUM    Set # of channels (default=1 for mono).          \n"\ 
     30"  -f, --frame=NUM      Set # of samples per frame (default equival 20ms)\n"\ 
     31"  -b, --bit=NUM        Set # of bits per sample (default=16)            \n" 
     32 
     33 
     34/* 
     35 * This utility function parses the command line and look for 
     36 * common sound options. 
     37 */ 
     38static pj_status_t get_snd_options( const char *app_name, 
     39                                    int argc,  
     40                                    char *argv[], 
     41                                    int *dev_id, 
     42                                    int *clock_rate, 
     43                                    int *channel_count, 
     44                                    int *samples_per_frame, 
     45                                    int *bits_per_sample) 
    5446{ 
    55     pj_status_t status; 
     47    struct pj_getopt_option long_options[] = { 
     48        { "dev",        1, 0, 'd' }, 
     49        { "rate",       1, 0, 'r' }, 
     50        { "channel",    1, 0, 'c' }, 
     51        { "frame",      1, 0, 'f' }, 
     52        { "bit",        1, 0, 'b' }, 
     53        { NULL, 0, 0, 0 }, 
     54    }; 
     55    int c; 
     56    int option_index; 
     57    long val; 
     58    char *err; 
    5659 
    57     /* Init PJLIB */ 
    58     status = pj_init(); 
    59     if (status != PJ_SUCCESS) { 
    60         app_perror(THIS_FILE, "pj_init() error", status); 
    61         return status; 
     60    *samples_per_frame = 0; 
     61 
     62    pj_optind = 0; 
     63    while((c=pj_getopt_long(argc,argv, "d:r:c:f:b:",  
     64                            long_options, &option_index))!=-1)  
     65    { 
     66 
     67        switch (c) { 
     68        case 'd': 
     69            /* device */ 
     70            val = strtol(pj_optarg, &err, 10); 
     71            if (*err) { 
     72                PJ_LOG(3,(app_name, "Error: invalid value for device id")); 
     73                return PJ_EINVAL; 
     74            } 
     75            *dev_id = val; 
     76            break; 
     77 
     78        case 'r': 
     79            /* rate */ 
     80            val = strtol(pj_optarg, &err, 10); 
     81            if (*err) { 
     82                PJ_LOG(3,(app_name, "Error: invalid value for clock rate")); 
     83                return PJ_EINVAL; 
     84            } 
     85            *clock_rate = val; 
     86            break; 
     87 
     88        case 'c': 
     89            /* channel count */ 
     90            val = strtol(pj_optarg, &err, 10); 
     91            if (*err) { 
     92                PJ_LOG(3,(app_name, "Error: invalid channel count")); 
     93                return PJ_EINVAL; 
     94            } 
     95            *channel_count = val; 
     96            break; 
     97 
     98        case 'f': 
     99            /* frame count/samples per frame */ 
     100            val = strtol(pj_optarg, &err, 10); 
     101            if (*err) { 
     102                PJ_LOG(3,(app_name, "Error: invalid samples per frame")); 
     103                return PJ_EINVAL; 
     104            } 
     105            *samples_per_frame = val; 
     106            break; 
     107 
     108        case 'b': 
     109            /* bit per sample */ 
     110            val = strtol(pj_optarg, &err, 10); 
     111            if (*err) { 
     112                PJ_LOG(3,(app_name, "Error: invalid samples bits per sample")); 
     113                return PJ_EINVAL; 
     114            } 
     115            *bits_per_sample = val; 
     116            break; 
     117 
     118        default: 
     119            /* Unknown options */ 
     120            PJ_LOG(3,(app_name, "Error: unknown options '%c'", pj_optopt)); 
     121            return PJ_EINVAL; 
     122        } 
     123 
    62124    } 
    63125 
    64     /* Init PJLIB-UTIL: */ 
    65     status = pjlib_util_init(); 
    66     if (status != PJ_SUCCESS) { 
    67         app_perror(THIS_FILE, "pjlib_util_init() error", status); 
    68         return status; 
     126    if (*samples_per_frame == 0) { 
     127        *samples_per_frame = *clock_rate * *channel_count * 20 / 1000; 
    69128    } 
    70129 
    71     /* Init memory pool: */ 
     130    return 0; 
     131} 
    72132 
    73     /* Init caching pool. */ 
    74     pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); 
    75133 
    76     /* Create global endpoint: */ 
     134/* Dump memory pool usage. */ 
     135static void dump_pool_usage( const char *app_name, pj_caching_pool *cp ) 
     136{ 
     137    pj_pool_t   *p; 
     138    unsigned     total_alloc = 0; 
     139    unsigned     total_used = 0; 
    77140 
    78     { 
    79         const pj_str_t *hostname; 
    80         const char *endpt_name; 
    81  
    82         /* Endpoint MUST be assigned a globally unique name. 
    83          * The name will be used as the hostname in Warning header. 
    84          */ 
    85  
    86         /* For this implementation, we'll use hostname for simplicity */ 
    87         hostname = pj_gethostname(); 
    88         endpt_name = hostname->ptr; 
    89  
    90         /* Create the endpoint: */ 
    91  
    92         status = pjsip_endpt_create(&cp.factory, endpt_name,  
    93                                     &g_endpt); 
    94         if (status != PJ_SUCCESS) { 
    95             app_perror(THIS_FILE, "Unable to create SIP endpoint", status); 
    96             return status; 
    97         } 
     141    /* Accumulate memory usage in active list. */ 
     142    p = cp->used_list.next; 
     143    while (p != (pj_pool_t*) &cp->used_list) { 
     144        total_alloc += pj_pool_get_capacity(p); 
     145        total_used += pj_pool_get_used_size(p); 
     146        p = p->next; 
    98147    } 
    99148 
    100     return PJ_SUCCESS; 
     149    PJ_LOG(3, (app_name, "Total pool memory allocated=%d KB, used=%d KB", 
     150               total_alloc / 1000, 
     151               total_used / 1000)); 
    101152} 
    102  
    103 /* 
    104  * Add UDP transport to endpoint. 
    105  */ 
    106 static pj_status_t util_add_udp_transport(int port) 
    107 { 
    108     pj_sockaddr_in addr; 
    109     pj_status_t status; 
    110      
    111     addr.sin_family = PJ_AF_INET; 
    112     addr.sin_addr.s_addr = 0; 
    113     addr.sin_port = port; 
    114  
    115     status = pjsip_udp_transport_start( g_endpt, &addr, NULL, 1, NULL); 
    116     if (status != PJ_SUCCESS) { 
    117         app_perror(THIS_FILE, "Unable to start UDP transport", status); 
    118         return status; 
    119     } 
    120  
    121     return status; 
    122 } 
    123  
Note: See TracChangeset for help on using the changeset viewer.