| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <pjmedia.h> |
| 32 |
#include <pjlib-util.h> |
| 33 |
#include <pjlib.h> |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
|
| 38 |
#include "util.h" |
| 39 |
|
| 40 |
#define REC_CLOCK_RATE 16000 |
| 41 |
#define PTIME 20 |
| 42 |
|
| 43 |
#define MODE_PLAY 1 |
| 44 |
#define MODE_RECORD 2 |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#define THIS_FILE "stereotest.c" |
| 49 |
|
| 50 |
|
| 51 |
static const char *desc = |
| 52 |
" FILE \n" |
| 53 |
" \n" |
| 54 |
" stereotest.c \n" |
| 55 |
" \n" |
| 56 |
" PURPOSE \n" |
| 57 |
" \n" |
| 58 |
" Demonstrate how use stereo port to play a WAV file to sound \n" |
| 59 |
" device or record to a WAV file from sound device with different \n" |
| 60 |
" channel count. \n" |
| 61 |
" \n" |
| 62 |
" USAGE \n" |
| 63 |
" \n" |
| 64 |
" stereotest [options] WAV \n" |
| 65 |
" \n" |
| 66 |
" Options: \n" |
| 67 |
" -m, --mode=N Operation mode: 1 = playing, 2 = recording.\n" |
| 68 |
" -C, --rec-ch-cnt=N Number of channel for recording file. \n" |
| 69 |
" -c, --snd-ch-cnt=N Number of channel for opening sound device.\n" |
| 70 |
" \n"; |
| 71 |
|
| 72 |
int main(int argc, char *argv[]) |
| 73 |
{ |
| 74 |
pj_caching_pool cp; |
| 75 |
pjmedia_endpt *med_endpt; |
| 76 |
pj_pool_t *pool; |
| 77 |
|
| 78 |
pjmedia_port *file_port = NULL; |
| 79 |
pjmedia_port *stereo_port = NULL; |
| 80 |
pjmedia_snd_port *snd_port = NULL; |
| 81 |
|
| 82 |
int dev_id = -1; |
| 83 |
char tmp[10]; |
| 84 |
pj_status_t status; |
| 85 |
|
| 86 |
char *wav_file = NULL; |
| 87 |
unsigned mode = 0; |
| 88 |
unsigned rec_ch_cnt = 1; |
| 89 |
unsigned snd_ch_cnt = 2; |
| 90 |
|
| 91 |
enum { |
| 92 |
OPT_MODE = 'm', |
| 93 |
OPT_REC_CHANNEL = 'C', |
| 94 |
OPT_SND_CHANNEL = 'c', |
| 95 |
}; |
| 96 |
|
| 97 |
struct pj_getopt_option long_options[] = { |
| 98 |
{ "mode", 1, 0, OPT_MODE }, |
| 99 |
{ "rec-ch-cnt", 1, 0, OPT_REC_CHANNEL }, |
| 100 |
{ "snd-ch-cnt", 1, 0, OPT_SND_CHANNEL }, |
| 101 |
{ NULL, 0, 0, 0 }, |
| 102 |
}; |
| 103 |
|
| 104 |
int c; |
| 105 |
int option_index; |
| 106 |
|
| 107 |
|
| 108 |
status = pj_init(); |
| 109 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 110 |
|
| 111 |
|
| 112 |
pj_optind = 0; |
| 113 |
while((c=pj_getopt_long(argc,argv, "m:C:c:", long_options, &option_index))!=-1) { |
| 114 |
|
| 115 |
switch (c) { |
| 116 |
case OPT_MODE: |
| 117 |
if (mode) { |
| 118 |
app_perror(THIS_FILE, "Cannot record and play at once!", |
| 119 |
PJ_EINVAL); |
| 120 |
return 1; |
| 121 |
} |
| 122 |
mode = atoi(pj_optarg); |
| 123 |
break; |
| 124 |
|
| 125 |
case OPT_REC_CHANNEL: |
| 126 |
rec_ch_cnt = atoi(pj_optarg); |
| 127 |
break; |
| 128 |
|
| 129 |
case OPT_SND_CHANNEL: |
| 130 |
snd_ch_cnt = atoi(pj_optarg); |
| 131 |
break; |
| 132 |
|
| 133 |
default: |
| 134 |
printf("Invalid options %s\n", argv[pj_optind]); |
| 135 |
puts(desc); |
| 136 |
return 1; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
wav_file = argv[pj_optind]; |
| 142 |
|
| 143 |
|
| 144 |
if (!wav_file) { |
| 145 |
app_perror(THIS_FILE, "WAV file not specified!", PJ_EINVAL); |
| 146 |
puts(desc); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
if (!snd_ch_cnt || !rec_ch_cnt || rec_ch_cnt > 6) { |
| 150 |
app_perror(THIS_FILE, "Invalid or too many channel count!", PJ_EINVAL); |
| 151 |
puts(desc); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
if (mode != MODE_RECORD && mode != MODE_PLAY) { |
| 155 |
app_perror(THIS_FILE, "Invalid operation mode!", PJ_EINVAL); |
| 156 |
puts(desc); |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 168 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 169 |
|
| 170 |
|
| 171 |
pool = pj_pool_create( &cp.factory, |
| 172 |
"app", |
| 173 |
4000, |
| 174 |
4000, |
| 175 |
NULL |
| 176 |
); |
| 177 |
|
| 178 |
if (mode == MODE_PLAY) { |
| 179 |
|
| 180 |
status = pjmedia_wav_player_port_create( pool, wav_file, PTIME, 0, |
| 181 |
0, &file_port); |
| 182 |
if (status != PJ_SUCCESS) { |
| 183 |
app_perror(THIS_FILE, "Unable to open file", status); |
| 184 |
return 1; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
status = pjmedia_snd_port_create_player( |
| 189 |
pool, |
| 190 |
dev_id, |
| 191 |
PJMEDIA_PIA_SRATE(&file_port->info), |
| 192 |
snd_ch_cnt, |
| 193 |
snd_ch_cnt * PTIME * |
| 194 |
PJMEDIA_PIA_SRATE(&file_port->info) / 1000, |
| 195 |
PJMEDIA_PIA_BITS(&file_port->info), |
| 196 |
0, |
| 197 |
&snd_port |
| 198 |
); |
| 199 |
if (status != PJ_SUCCESS) { |
| 200 |
app_perror(THIS_FILE, "Unable to open sound device", status); |
| 201 |
return 1; |
| 202 |
} |
| 203 |
|
| 204 |
if (snd_ch_cnt != PJMEDIA_PIA_CCNT(&file_port->info)) { |
| 205 |
status = pjmedia_stereo_port_create( pool, |
| 206 |
file_port, |
| 207 |
snd_ch_cnt, |
| 208 |
0, |
| 209 |
&stereo_port); |
| 210 |
if (status != PJ_SUCCESS) { |
| 211 |
app_perror(THIS_FILE, "Unable to create stereo port", status); |
| 212 |
return 1; |
| 213 |
} |
| 214 |
|
| 215 |
status = pjmedia_snd_port_connect(snd_port, stereo_port); |
| 216 |
} else { |
| 217 |
status = pjmedia_snd_port_connect(snd_port, file_port); |
| 218 |
} |
| 219 |
|
| 220 |
if (status != PJ_SUCCESS) { |
| 221 |
app_perror(THIS_FILE, "Unable to connect sound port", status); |
| 222 |
return 1; |
| 223 |
} |
| 224 |
|
| 225 |
} else { |
| 226 |
|
| 227 |
status = pjmedia_wav_writer_port_create(pool, wav_file, |
| 228 |
REC_CLOCK_RATE, |
| 229 |
rec_ch_cnt, |
| 230 |
rec_ch_cnt * PTIME * |
| 231 |
REC_CLOCK_RATE / 1000, |
| 232 |
NBITS, |
| 233 |
0, 0, |
| 234 |
&file_port); |
| 235 |
if (status != PJ_SUCCESS) { |
| 236 |
app_perror(THIS_FILE, "Unable to open file", status); |
| 237 |
return 1; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
status = pjmedia_snd_port_create_rec( |
| 242 |
pool, |
| 243 |
dev_id, |
| 244 |
REC_CLOCK_RATE, |
| 245 |
snd_ch_cnt, |
| 246 |
snd_ch_cnt * PTIME * |
| 247 |
REC_CLOCK_RATE / 1000, |
| 248 |
NBITS, |
| 249 |
0, |
| 250 |
&snd_port |
| 251 |
); |
| 252 |
if (status != PJ_SUCCESS) { |
| 253 |
app_perror(THIS_FILE, "Unable to open sound device", status); |
| 254 |
return 1; |
| 255 |
} |
| 256 |
|
| 257 |
if (rec_ch_cnt != snd_ch_cnt) { |
| 258 |
status = pjmedia_stereo_port_create( pool, |
| 259 |
file_port, |
| 260 |
snd_ch_cnt, |
| 261 |
0, |
| 262 |
&stereo_port); |
| 263 |
if (status != PJ_SUCCESS) { |
| 264 |
app_perror(THIS_FILE, "Unable to create stereo port", status); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
status = pjmedia_snd_port_connect(snd_port, stereo_port); |
| 269 |
} else { |
| 270 |
status = pjmedia_snd_port_connect(snd_port, file_port); |
| 271 |
} |
| 272 |
|
| 273 |
if (status != PJ_SUCCESS) { |
| 274 |
app_perror(THIS_FILE, "Unable to connect sound port", status); |
| 275 |
return 1; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
dump_pool_usage(THIS_FILE, &cp); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
pj_thread_sleep(100); |
| 289 |
|
| 290 |
printf("Mode = %s\n", (mode == MODE_PLAY? "playing" : "recording") ); |
| 291 |
printf("File port channel count = %d\n", PJMEDIA_PIA_CCNT(&file_port->info)); |
| 292 |
printf("Sound port channel count = %d\n", |
| 293 |
PJMEDIA_PIA_CCNT(&pjmedia_snd_port_get_port(snd_port)->info)); |
| 294 |
puts(""); |
| 295 |
puts("Press <ENTER> to stop and quit"); |
| 296 |
|
| 297 |
if (fgets(tmp, sizeof(tmp), stdin) == NULL) { |
| 298 |
puts("EOF while reading stdin, will quit now.."); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
status = pjmedia_snd_port_destroy( snd_port ); |
| 306 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
status = pjmedia_port_destroy( stereo_port? stereo_port : file_port); |
| 313 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
pj_pool_release( pool ); |
| 318 |
|
| 319 |
|
| 320 |
pjmedia_endpt_destroy( med_endpt ); |
| 321 |
|
| 322 |
|
| 323 |
pj_caching_pool_destroy( &cp ); |
| 324 |
|
| 325 |
|
| 326 |
pj_shutdown(); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
return 0; |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|