1
/* $Id: stereotest.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
* \page page_pjmedia_samples_stereo_c Samples: Using Stereo Port
24
* This example demonstrates how to use @ref PJMEDIA_STEREO_PORT to
25
* change the channel count of the media streams.
27
* This file is pjsip-apps/src/samples/stereotest.c
29
* \includelineno stereotest.c
33
#include <pjlib-util.h>
41
#define REC_CLOCK_RATE 16000
48
/* For logging purpose. */
49
#define THIS_FILE "stereotest.c"
52
static const char *desc =
59
" Demonstrate how use stereo port to play a WAV file to sound \n"
60
" device or record to a WAV file from sound device with different \n"
65
" stereotest [options] WAV \n"
68
" -m, --mode=N Operation mode: 1 = playing, 2 = recording.\n"
69
" -C, --rec-ch-cnt=N Number of channel for recording file. \n"
70
" -c, --snd-ch-cnt=N Number of channel for opening sound device.\n"
73
int main(int argc, char *argv[])
76
pjmedia_endpt *med_endpt;
79
pjmedia_port *file_port = NULL;
80
pjmedia_port *stereo_port = NULL;
81
pjmedia_snd_port *snd_port = NULL;
87
char *wav_file = NULL;
89
unsigned rec_ch_cnt = 1;
90
unsigned snd_ch_cnt = 2;
94
OPT_REC_CHANNEL = 'C',
95
OPT_SND_CHANNEL = 'c',
98
struct pj_getopt_option long_options[] = {
99
{ "mode", 1, 0, OPT_MODE },
100
{ "rec-ch-cnt", 1, 0, OPT_REC_CHANNEL },
101
{ "snd-ch-cnt", 1, 0, OPT_SND_CHANNEL },
108
/* Must init PJLIB first: */
110
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
112
/* Parse arguments */
114
while((c=pj_getopt_long(argc,argv, "m:C:c:", long_options, &option_index))!=-1) {
119
app_perror(THIS_FILE, "Cannot record and play at once!",
123
mode = atoi(pj_optarg);
126
case OPT_REC_CHANNEL:
127
rec_ch_cnt = atoi(pj_optarg);
130
case OPT_SND_CHANNEL:
131
snd_ch_cnt = atoi(pj_optarg);
135
printf("Invalid options %s\n", argv[pj_optind]);
142
wav_file = argv[pj_optind];
144
/* Verify arguments. */
146
app_perror(THIS_FILE, "WAV file not specified!", PJ_EINVAL);
150
if (!snd_ch_cnt || !rec_ch_cnt || rec_ch_cnt > 6) {
151
app_perror(THIS_FILE, "Invalid or too many channel count!", PJ_EINVAL);
155
if (mode != MODE_RECORD && mode != MODE_PLAY) {
156
app_perror(THIS_FILE, "Invalid operation mode!", PJ_EINVAL);
161
/* Must create a pool factory before we can allocate any memory. */
162
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
165
* Initialize media endpoint.
166
* This will implicitly initialize PJMEDIA too.
168
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
169
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
171
/* Create memory pool for our file player */
172
pool = pj_pool_create( &cp.factory, /* pool factory */
173
"app", /* pool name. */
174
4000, /* init size */
175
4000, /* increment size */
176
NULL /* callback on error */
179
if (mode == MODE_PLAY) {
180
/* Create WAVE file player port. */
181
status = pjmedia_wav_player_port_create( pool, wav_file, PTIME, 0,
183
if (status != PJ_SUCCESS) {
184
app_perror(THIS_FILE, "Unable to open file", status);
188
/* Create sound player port. */
189
status = pjmedia_snd_port_create_player(
191
dev_id, /* device id. */
192
PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate. */
193
snd_ch_cnt, /* # of channels. */
194
snd_ch_cnt * PTIME * /* samples per frame. */
195
PJMEDIA_PIA_SRATE(&file_port->info) / 1000,
196
PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample. */
198
&snd_port /* returned port */
200
if (status != PJ_SUCCESS) {
201
app_perror(THIS_FILE, "Unable to open sound device", status);
205
if (snd_ch_cnt != PJMEDIA_PIA_CCNT(&file_port->info)) {
206
status = pjmedia_stereo_port_create( pool,
211
if (status != PJ_SUCCESS) {
212
app_perror(THIS_FILE, "Unable to create stereo port", status);
216
status = pjmedia_snd_port_connect(snd_port, stereo_port);
218
status = pjmedia_snd_port_connect(snd_port, file_port);
221
if (status != PJ_SUCCESS) {
222
app_perror(THIS_FILE, "Unable to connect sound port", status);
227
/* Create WAVE file writer port. */
228
status = pjmedia_wav_writer_port_create(pool, wav_file,
232
REC_CLOCK_RATE / 1000,
236
if (status != PJ_SUCCESS) {
237
app_perror(THIS_FILE, "Unable to open file", status);
241
/* Create sound player port. */
242
status = pjmedia_snd_port_create_rec(
244
dev_id, /* device id. */
245
REC_CLOCK_RATE, /* clock rate. */
246
snd_ch_cnt, /* # of channels. */
248
REC_CLOCK_RATE / 1000, /* samples per frame. */
249
NBITS, /* bits per sample. */
251
&snd_port /* returned port */
253
if (status != PJ_SUCCESS) {
254
app_perror(THIS_FILE, "Unable to open sound device", status);
258
if (rec_ch_cnt != snd_ch_cnt) {
259
status = pjmedia_stereo_port_create( pool,
264
if (status != PJ_SUCCESS) {
265
app_perror(THIS_FILE, "Unable to create stereo port", status);
269
status = pjmedia_snd_port_connect(snd_port, stereo_port);
271
status = pjmedia_snd_port_connect(snd_port, file_port);
274
if (status != PJ_SUCCESS) {
275
app_perror(THIS_FILE, "Unable to connect sound port", status);
280
/* Dump memory usage */
281
dump_pool_usage(THIS_FILE, &cp);
284
* File should be playing and looping now, using sound device's thread.
288
/* Sleep to allow log messages to flush */
289
pj_thread_sleep(100);
291
printf("Mode = %s\n", (mode == MODE_PLAY? "playing" : "recording") );
292
printf("File port channel count = %d\n", PJMEDIA_PIA_CCNT(&file_port->info));
293
printf("Sound port channel count = %d\n",
294
PJMEDIA_PIA_CCNT(&pjmedia_snd_port_get_port(snd_port)->info));
296
puts("Press <ENTER> to stop and quit");
298
if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
299
puts("EOF while reading stdin, will quit now..");
302
/* Start deinitialization: */
305
/* Destroy sound device */
306
status = pjmedia_snd_port_destroy( snd_port );
307
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
310
/* Destroy stereo port and file_port.
311
* Stereo port will destroy all downstream ports (e.g. the file port)
313
status = pjmedia_port_destroy( stereo_port? stereo_port : file_port);
314
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
317
/* Release application pool */
318
pj_pool_release( pool );
320
/* Destroy media endpoint. */
321
pjmedia_endpt_destroy( med_endpt );
323
/* Destroy pool factory */
324
pj_caching_pool_destroy( &cp );