00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00083
00084
00085
00086 status = pj_init();
00087 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00088
00089
00090
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
00106 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00107
00108
00109
00110
00111
00112 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00113 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00114
00115
00116 pool = pj_pool_create( &cp.factory,
00117 "app",
00118 4000,
00119 4000,
00120 NULL
00121 );
00122
00123
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
00132 if (PJMEDIA_PIA_CCNT(&file_port->info) != (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
00140
00141
00142
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
00152 status = pjmedia_snd_port_create(
00153 pool,
00154 dev_id,
00155 dev_id,
00156 sampling_rate,
00157 channel_count,
00158 samples_per_frame,
00159 bits_per_sample,
00160 0,
00161 &snd_port
00162 );
00163 if (status != PJ_SUCCESS) {
00164 app_perror(THIS_FILE, "Unable to open sound device", status);
00165 return 1;
00166 }
00167
00168
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
00177 dump_pool_usage(THIS_FILE, &cp);
00178
00179
00180
00181
00182
00183
00184
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,
00190 PJMEDIA_PIA_SRATE(&file_port->info));
00191 puts("");
00192 puts("Press <ENTER> to stop playing and quit");
00193
00194 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
00195 puts("EOF while reading stdin, will quit now..");
00196 }
00197
00198
00199
00200
00201
00202 status = pjmedia_snd_port_destroy( snd_port );
00203 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00204
00205
00206
00207
00208
00209 status = pjmedia_port_destroy( resample_port );
00210 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00211
00212
00213
00214 pj_pool_release( pool );
00215
00216
00217 pjmedia_endpt_destroy( med_endpt );
00218
00219
00220 pj_caching_pool_destroy( &cp );
00221
00222
00223 pj_shutdown();
00224
00225
00226
00227 return 0;
00228
00229 }
00230
00231
00232