pjsip logo pjsip.org
Open source SIP stack and media stack for presence, im/instant messaging, and multimedia communication

HOME

SIP/media Features
High Performance SIP
Small Footprint SIP
Symbian Port

FAQ

Documentation

Licensing

Download

Development (Trac)

Projects using pjsip

Mailing List

Open Source Links


About: PJLIB, PJLIB-UTIL, PJSIP, and PJMEDIA are created by: Benny Prijono
<bennylp@pjsip.org>


 

Home --> Documentations --> PJMEDIA Reference

Samples: Using Resample Port

This example demonstrates how to use Resample Port to change the sampling rate of the media streams.

This file is pjsip-apps/src/samples/resampleplay.c

00001 /* $Id: resampleplay.c 2408 2009-01-01 22:08:21Z bennylp $ */
00002 /* 
00003  * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
00004  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00019  */
00020 
00032 #include <pjmedia.h>
00033 #include <pjlib-util.h>
00034 #include <pjlib.h>
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 
00039 #include "util.h"
00040 
00041 /* For logging purpose. */
00042 #define THIS_FILE   "resampleplay.c"
00043 
00044 
00045 static const char *desc = 
00046 " FILE                                                              \n"
00047 "                                                                   \n"
00048 "  resampleplay.c                                                   \n"
00049 "                                                                   \n"
00050 " PURPOSE                                                           \n"
00051 "                                                                   \n"
00052 "  Demonstrate how use resample port to play a WAV file to sound    \n"
00053 "  device using different sampling rate.                            \n"
00054 "                                                                   \n"
00055 " USAGE                                                             \n"
00056 "                                                                   \n"
00057 "  resampleplay [options] FILE.WAV                                  \n"
00058 "                                                                   \n"
00059 " where options:                                                    \n"
00060 SND_USAGE
00061 "                                                                   \n"
00062 "  The WAV file could have mono or stereo channels with arbitrary   \n"
00063 "  sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM.   \n";
00064 
00065 
00066 int main(int argc, char *argv[])
00067 {
00068     pj_caching_pool cp;
00069     pjmedia_endpt *med_endpt;
00070     pj_pool_t *pool;
00071     pjmedia_port *file_port;
00072     pjmedia_port *resample_port;
00073     pjmedia_snd_port *snd_port;
00074     char tmp[10];
00075     pj_status_t status;
00076 
00077     int dev_id = -1;
00078     int sampling_rate = CLOCK_RATE;
00079     int channel_count = NCHANNELS;
00080     int samples_per_frame = NSAMPLES;
00081     int bits_per_sample = NBITS;
00082     //int ptime;
00083     //int down_samples;
00084 
00085     /* Must init PJLIB first: */
00086     status = pj_init();
00087     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00088 
00089 
00090     /* Get options */
00091     if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate,
00092                         &channel_count, &samples_per_frame, &bits_per_sample))
00093     {
00094         puts("");
00095         puts(desc);
00096         return 1;
00097     }
00098 
00099     if (!argv[pj_optind]) {
00100         puts("Error: no file is specified");
00101         puts(desc);
00102         return 1;
00103     }
00104 
00105     /* Must create a pool factory before we can allocate any memory. */
00106     pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00107 
00108     /* 
00109      * Initialize media endpoint.
00110      * This will implicitly initialize PJMEDIA too.
00111      */
00112     status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00113     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00114 
00115     /* Create memory pool for our file player */
00116     pool = pj_pool_create( &cp.factory,     /* pool factory         */
00117                            "app",           /* pool name.           */
00118                            4000,            /* init size            */
00119                            4000,            /* increment size       */
00120                            NULL             /* callback on error    */
00121                            );
00122 
00123     /* Create the file port. */
00124     status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0,
00125                                              0, &file_port);
00126     if (status != PJ_SUCCESS) {
00127         app_perror(THIS_FILE, "Unable to open file", status);
00128         return 1;
00129     }
00130 
00131     /* File must have same number of channels. */
00132     if (file_port->info.channel_count != (unsigned)channel_count) {
00133         PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. "
00134                              "Perhaps you'd need -c option?"));
00135         pjmedia_port_destroy(file_port);
00136         return 1;
00137     }
00138 
00139     /* Calculate number of samples per frame to be taken from file port */
00140     //ptime = samples_per_frame * 1000 / sampling_rate;
00141 
00142     /* Create the resample port. */
00143     status = pjmedia_resample_port_create( pool, file_port,
00144                                            sampling_rate, 0,
00145                                            &resample_port);
00146     if (status != PJ_SUCCESS) {
00147         app_perror(THIS_FILE, "Unable to create resample port", status);
00148         return 1;
00149     }
00150 
00151     /* Create sound player port. */
00152     status = pjmedia_snd_port_create( 
00153                  pool,                  /* pool                     */
00154                  dev_id,                /* device                   */
00155                  dev_id,                /* device                   */
00156                  sampling_rate,         /* clock rate.              */
00157                  channel_count,         /* # of channels.           */
00158                  samples_per_frame,     /* samples per frame.       */
00159                  bits_per_sample,       /* bits per sample.         */
00160                  0,                     /* options                  */
00161                  &snd_port              /* returned port            */
00162                  );
00163     if (status != PJ_SUCCESS) {
00164         app_perror(THIS_FILE, "Unable to open sound device", status);
00165         return 1;
00166     }
00167 
00168     /* Connect resample port to sound device */
00169     status = pjmedia_snd_port_connect( snd_port, resample_port);
00170     if (status != PJ_SUCCESS) {
00171         app_perror(THIS_FILE, "Error connecting sound ports", status);
00172         return 1;
00173     }
00174 
00175 
00176     /* Dump memory usage */
00177     dump_pool_usage(THIS_FILE, &cp);
00178 
00179     /* 
00180      * File should be playing and looping now, using sound device's thread. 
00181      */
00182 
00183 
00184     /* Sleep to allow log messages to flush */
00185     pj_thread_sleep(100);
00186 
00187 
00188     printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n",
00189            argv[pj_optind], sampling_rate, file_port->info.clock_rate);
00190     puts("");
00191     puts("Press <ENTER> to stop playing and quit");
00192 
00193     if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
00194         puts("EOF while reading stdin, will quit now..");
00195     }
00196     
00197     /* Start deinitialization: */
00198 
00199 
00200     /* Destroy sound device */
00201     status = pjmedia_snd_port_destroy( snd_port );
00202     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00203 
00204 
00205     /* Destroy resample port. 
00206      * This will destroy all downstream ports (e.g. the file port)
00207      */
00208     status = pjmedia_port_destroy( resample_port );
00209     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00210 
00211 
00212     /* Release application pool */
00213     pj_pool_release( pool );
00214 
00215     /* Destroy media endpoint. */
00216     pjmedia_endpt_destroy( med_endpt );
00217 
00218     /* Destroy pool factory */
00219     pj_caching_pool_destroy( &cp );
00220 
00221     /* Shutdown PJLIB */
00222     pj_shutdown();
00223 
00224 
00225     /* Done. */
00226     return 0;
00227 
00228 }
00229 
00230 
00231 

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.