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/confsample.c

    r352 r361  
    1919 
    2020#include <pjmedia.h> 
     21#include <pjlib-util.h> /* pj_getopt */ 
     22#include <pjlib.h> 
     23 
     24#include <stdlib.h>     /* atoi() */ 
     25#include <stdio.h> 
     26 
     27#include "util.h" 
     28 
     29/* For logging purpose. */ 
     30#define THIS_FILE   "confsample.c" 
     31 
     32 
     33/* Shall we put recorder in the conference */ 
     34#define RECORDER    1 
     35 
    2136 
    2237static const char *desc =  
     
    3146 " USAGE:                                                                   \n" 
    3247 "                                                                          \n" 
    33  "  confsample [file1.wav] [file2.wav] ...                                  \n" 
    34  "                                                                          \n" 
    35  " where:                                                                   \n" 
     48 "  confsample [options] [file1.wav] [file2.wav] ...                        \n" 
     49 "                                                                          \n" 
     50 " options:                                                                 \n" 
     51 SND_USAGE 
     52 "                                                                          \n" 
    3653 "  fileN.wav are optional WAV files to be connected to the conference      \n" 
    3754 "  bridge. The WAV files MUST have single channel (mono) and 16 bit PCM    \n" 
     
    4663 "  to slot starting from number one in the bridge. The WAV files can have  \n" 
    4764 "  arbitrary sampling rate; the bridge will convert it to its clock rate.  \n" 
    48  "  However, the files MUST have a single audio channel only (i.e. mono).   \n"; 
    49  
    50 #include <pjmedia.h> 
    51 #include <pjlib.h> 
    52  
    53 #include <stdlib.h>     /* atoi() */ 
    54 #include <stdio.h> 
    55  
    56  
    57 /* For logging purpose. */ 
    58 #define THIS_FILE   "confsample.c" 
    59  
    60 /* Constants */ 
    61 #define CLOCK_RATE      44100 
    62 #define NSAMPLES        (CLOCK_RATE * 20 / 1000) 
    63 #define NCHANNELS       1 
    64 #define NBITS           16 
     65 "  However, the files MUST have a single audio channel only (i.e. mono).  \n"; 
     66 
    6567 
    6668  
     
    7476/* Display VU meter */ 
    7577static void monitor_level(pjmedia_conf *conf, int slot, int dir, int dur); 
    76  
    77  
    78 /* Util to display the error message for the specified error code  */ 
    79 static int app_perror( const char *sender, const char *title,  
    80                        pj_status_t status) 
    81 { 
    82     char errmsg[PJ_ERR_MSG_SIZE]; 
    83  
    84     pj_strerror(status, errmsg, sizeof(errmsg)); 
    85  
    86     printf("%s: %s [code=%d]\n", title, errmsg, status); 
    87     return 1; 
    88 } 
    8978 
    9079 
     
    124113int main(int argc, char *argv[]) 
    125114{ 
     115    int dev_id = -1; 
     116    int clock_rate = CLOCK_RATE; 
     117    int channel_count = NCHANNELS; 
     118    int samples_per_frame = NSAMPLES; 
     119    int bits_per_sample = NBITS; 
     120 
    126121    pj_caching_pool cp; 
    127122    pjmedia_endpt *med_endpt; 
     
    131126    int i, port_count, file_count; 
    132127    pjmedia_port **file_port;   /* Array of file ports */ 
     128    pjmedia_port *rec_port = NULL;  /* Wav writer port */ 
    133129 
    134130    char tmp[10]; 
    135131    pj_status_t status; 
    136  
    137  
    138     /* Just in case user needs help */ 
    139     if (argc > 1 && (*argv[1]=='-' || *argv[1]=='/' || *argv[1]=='?')) { 
    140         usage(); 
    141         return 1; 
    142     } 
    143132 
    144133 
     
    146135    status = pj_init(); 
    147136    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); 
     137 
     138    /* Get command line options. */ 
     139    if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &clock_rate, 
     140                        &channel_count, &samples_per_frame, &bits_per_sample)) 
     141    { 
     142        usage(); 
     143        return 1; 
     144    } 
    148145 
    149146    /* Must create a pool factory before we can allocate any memory. */ 
     
    166163 
    167164 
    168     file_count = argc-1; 
    169     port_count = file_count + 1; 
     165    file_count = argc - pj_optind; 
     166    port_count = file_count + 1 + RECORDER; 
    170167 
    171168    /* Create the conference bridge.  
     
    175172    status = pjmedia_conf_create( pool,     /* pool to use          */ 
    176173                                  port_count,/* number of ports     */ 
    177                                   CLOCK_RATE,/* sampling rate       */ 
    178                                   NCHANNELS,/* # of channels.       */ 
    179                                   NSAMPLES, /* samples per frame    */ 
    180                                   NBITS,    /* bits per sample      */ 
     174                                  clock_rate, 
     175                                  channel_count, 
     176                                  samples_per_frame, 
     177                                  bits_per_sample, 
    181178                                  0,        /* options              */ 
    182179                                  &conf     /* result               */ 
     
    187184    } 
    188185 
     186#if RECORDER 
     187    status = pjmedia_file_writer_port_create( pool, "confrecord.wav", 
     188                                              clock_rate, channel_count, 
     189                                              samples_per_frame,  
     190                                              bits_per_sample, 0, 0, NULL, 
     191                                              &rec_port); 
     192    if (status != PJ_SUCCESS) { 
     193        app_perror(THIS_FILE, "Unable to create WAV writer", status); 
     194        return 1; 
     195    } 
     196 
     197    pjmedia_conf_add_port(conf, pool, rec_port, NULL, NULL); 
     198#endif 
     199 
    189200 
    190201    /* Create file ports. */ 
     
    194205 
    195206        /* Load the WAV file to file port. */ 
    196         status = pjmedia_file_player_port_create( pool,     /* pool.        */ 
    197                                                   argv[i+1],/* filename     */ 
    198                                                   0,        /* flags        */ 
    199                                                   0,        /* buf size     */ 
    200                                                   NULL,     /* user data    */ 
    201                                                   &file_port[i] /* result   */ 
    202                                                   ); 
     207        status = pjmedia_file_player_port_create(  
     208                        pool,               /* pool.        */ 
     209                        argv[i+pj_optind],  /* filename     */ 
     210                        0,                  /* flags        */ 
     211                        0,                  /* buf size     */ 
     212                        NULL,               /* user data    */ 
     213                        &file_port[i]       /* result       */ 
     214                        ); 
    203215        if (status != PJ_SUCCESS) { 
    204216            char title[80]; 
    205             pj_ansi_sprintf(title, "Unable to use %s", argv[i+1]); 
     217            pj_ansi_sprintf(title, "Unable to use %s", argv[i+pj_optind]); 
    206218            app_perror(THIS_FILE, title, status); 
    207219            usage(); 
     
    214226                                        file_port[i],   /* port to connect  */ 
    215227                                        NULL,           /* Use port's name  */ 
    216                                         NULL            /* ptr to receive slot # */ 
     228                                        NULL            /* ptr for slot #  */ 
    217229                                        ); 
    218230        if (status != PJ_SUCCESS) { 
     
    229241     */ 
    230242 
     243 
     244    /* Dump memory usage */ 
     245    dump_pool_usage(THIS_FILE, &cp); 
    231246 
    232247    /* Sleep to allow log messages to flush */ 
     
    421436    } 
    422437 
     438    /* Destroy recorder port */ 
     439    if (rec_port) 
     440        pjmedia_port_destroy(rec_port); 
     441 
    423442    /* Release application pool */ 
    424443    pj_pool_release( pool ); 
     
    484503                   "  Name                    : %.*s\n" 
    485504                   "  Sampling rate           : %d Hz\n" 
     505                   "  Samples per frame       : %d\n" 
    486506                   "  Frame time              : %d ms\n" 
    487507                   "  Signal level adjustment : tx=%d, rx=%d\n" 
     
    492512                   port_info->name.ptr, 
    493513                   port_info->clock_rate, 
     514                   port_info->samples_per_frame, 
    494515                   port_info->samples_per_frame*1000/port_info->clock_rate, 
    495516                   port_info->tx_adj_level, 
Note: See TracChangeset for help on using the changeset viewer.