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

 


PJMEDIA small footprint Open Source media stack
(C)2003-2008 Benny Prijono