1
/* $Id: resampleplay.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_resampleplay_c Samples: Using Resample Port
24
* This example demonstrates how to use @ref PJMEDIA_RESAMPLE_PORT to
25
* change the sampling rate of the media streams.
27
* This file is pjsip-apps/src/samples/resampleplay.c
29
* \includelineno resampleplay.c
33
#include <pjlib-util.h>
41
/* For logging purpose. */
42
#define THIS_FILE "resampleplay.c"
45
static const char *desc =
52
" Demonstrate how use resample port to play a WAV file to sound \n"
53
" device using different sampling rate. \n"
57
" resampleplay [options] FILE.WAV \n"
62
" The WAV file could have mono or stereo channels with arbitrary \n"
63
" sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n";
66
int main(int argc, char *argv[])
69
pjmedia_endpt *med_endpt;
71
pjmedia_port *file_port;
72
pjmedia_port *resample_port;
73
pjmedia_snd_port *snd_port;
78
int sampling_rate = CLOCK_RATE;
79
int channel_count = NCHANNELS;
80
int samples_per_frame = NSAMPLES;
81
int bits_per_sample = NBITS;
85
/* Must init PJLIB first: */
87
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
91
if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate,
92
&channel_count, &samples_per_frame, &bits_per_sample))
99
if (!argv[pj_optind]) {
100
puts("Error: no file is specified");
105
/* Must create a pool factory before we can allocate any memory. */
106
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
109
* Initialize media endpoint.
110
* This will implicitly initialize PJMEDIA too.
112
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
113
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
115
/* Create memory pool for our file player */
116
pool = pj_pool_create( &cp.factory, /* pool factory */
117
"app", /* pool name. */
118
4000, /* init size */
119
4000, /* increment size */
120
NULL /* callback on error */
123
/* Create the file port. */
124
status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0,
126
if (status != PJ_SUCCESS) {
127
app_perror(THIS_FILE, "Unable to open file", status);
131
/* File must have same number of channels. */
132
if (PJMEDIA_PIA_CCNT(&file_port->info) != (unsigned)channel_count) {
133
PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. "
134
"Perhaps you'd need -c option?"));
135
pjmedia_port_destroy(file_port);
139
/* Calculate number of samples per frame to be taken from file port */
140
//ptime = samples_per_frame * 1000 / sampling_rate;
142
/* Create the resample port. */
143
status = pjmedia_resample_port_create( pool, file_port,
146
if (status != PJ_SUCCESS) {
147
app_perror(THIS_FILE, "Unable to create resample port", status);
151
/* Create sound player port. */
152
status = pjmedia_snd_port_create(
156
sampling_rate, /* clock rate. */
157
channel_count, /* # of channels. */
158
samples_per_frame, /* samples per frame. */
159
bits_per_sample, /* bits per sample. */
161
&snd_port /* returned port */
163
if (status != PJ_SUCCESS) {
164
app_perror(THIS_FILE, "Unable to open sound device", status);
168
/* Connect resample port to sound device */
169
status = pjmedia_snd_port_connect( snd_port, resample_port);
170
if (status != PJ_SUCCESS) {
171
app_perror(THIS_FILE, "Error connecting sound ports", status);
176
/* Dump memory usage */
177
dump_pool_usage(THIS_FILE, &cp);
180
* File should be playing and looping now, using sound device's thread.
184
/* Sleep to allow log messages to flush */
185
pj_thread_sleep(100);
188
printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n",
189
argv[pj_optind], sampling_rate,
190
PJMEDIA_PIA_SRATE(&file_port->info));
192
puts("Press <ENTER> to stop playing and quit");
194
if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
195
puts("EOF while reading stdin, will quit now..");
198
/* Start deinitialization: */
201
/* Destroy sound device */
202
status = pjmedia_snd_port_destroy( snd_port );
203
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
206
/* Destroy resample port.
207
* This will destroy all downstream ports (e.g. the file port)
209
status = pjmedia_port_destroy( resample_port );
210
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
213
/* Release application pool */
214
pj_pool_release( pool );
216
/* Destroy media endpoint. */
217
pjmedia_endpt_destroy( med_endpt );
219
/* Destroy pool factory */
220
pj_caching_pool_destroy( &cp );