|
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
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
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00082
00083
00084
00085 status = pj_init();
00086 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00087
00088
00089
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
00105 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00106
00107
00108
00109
00110
00111 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00112 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00113
00114
00115 pool = pj_pool_create( &cp.factory,
00116 "app",
00117 4000,
00118 4000,
00119 NULL
00120 );
00121
00122
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
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
00139
00140
00141
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
00151 status = pjmedia_snd_port_create(
00152 pool,
00153 dev_id,
00154 dev_id,
00155 sampling_rate,
00156 channel_count,
00157 samples_per_frame,
00158 bits_per_sample,
00159 0,
00160 &snd_port
00161 );
00162 if (status != PJ_SUCCESS) {
00163 app_perror(THIS_FILE, "Unable to open sound device", status);
00164 return 1;
00165 }
00166
00167
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
00176 dump_pool_usage(THIS_FILE, &cp);
00177
00178
00179
00180
00181
00182
00183
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
00196
00197
00198
00199 status = pjmedia_snd_port_destroy( snd_port );
00200 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00201
00202
00203
00204
00205
00206 status = pjmedia_port_destroy( resample_port );
00207 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00208
00209
00210
00211 pj_pool_release( pool );
00212
00213
00214 pjmedia_endpt_destroy( med_endpt );
00215
00216
00217 pj_caching_pool_destroy( &cp );
00218
00219
00220 pj_shutdown();
00221
00222
00223
00224 return 0;
00225
00226 }
00227
00228
00229
PJMEDIA small footprint Open Source media stack
(C)2003-2008 Benny Prijono
|
|