blob: 18e1d6c2e81641cba0a141959cf07b062b93a072 [file] [log] [blame]
Benny Prijonobc797312006-03-24 20:44:27 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonobc797312006-03-24 20:44:27 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono1ec70b32006-06-20 15:39:07 +000019
20/**
21 * \page page_pjmedia_samples_resampleplay_c Samples: Using Resample Port
22 *
23 * This example demonstrates how to use @ref PJMEDIA_RESAMPLE_PORT to
24 * change the sampling rate of the media streams.
25 *
26 * This file is pjsip-apps/src/samples/resampleplay.c
27 *
28 * \includelineno resampleplay.c
29 */
30
Benny Prijonobc797312006-03-24 20:44:27 +000031#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/* For logging purpose. */
41#define THIS_FILE "resampleplay.c"
42
43
44static const char *desc =
45" FILE \n"
46" \n"
47" resampleplay.c \n"
48" \n"
49" PURPOSE \n"
50" \n"
51" Demonstrate how use resample port to play a WAV file to sound \n"
52" device using different sampling rate. \n"
53" \n"
54" USAGE \n"
55" \n"
56" resampleplay [options] FILE.WAV \n"
57" \n"
58" where options: \n"
59SND_USAGE
60" \n"
61" The WAV file could have mono or stereo channels with arbitrary \n"
62" sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n";
63
64
65int main(int argc, char *argv[])
66{
67 pj_caching_pool cp;
68 pjmedia_endpt *med_endpt;
69 pj_pool_t *pool;
70 pjmedia_port *file_port;
71 pjmedia_port *resample_port;
72 pjmedia_snd_port *snd_port;
73 char tmp[10];
74 pj_status_t status;
75
76 int dev_id = -1;
77 int sampling_rate = CLOCK_RATE;
78 int channel_count = NCHANNELS;
79 int samples_per_frame = NSAMPLES;
80 int bits_per_sample = NBITS;
81 //int ptime;
82 //int down_samples;
83
84 /* Must init PJLIB first: */
85 status = pj_init();
86 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
87
88
89 /* Get options */
90 if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate,
91 &channel_count, &samples_per_frame, &bits_per_sample))
92 {
93 puts("");
94 puts(desc);
95 return 1;
96 }
97
98 if (!argv[pj_optind]) {
99 puts("Error: no file is specified");
100 puts(desc);
101 return 1;
102 }
103
104 /* Must create a pool factory before we can allocate any memory. */
105 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
106
107 /*
108 * Initialize media endpoint.
109 * This will implicitly initialize PJMEDIA too.
110 */
111 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
112 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
113
114 /* Create memory pool for our file player */
115 pool = pj_pool_create( &cp.factory, /* pool factory */
116 "app", /* pool name. */
117 4000, /* init size */
118 4000, /* increment size */
119 NULL /* callback on error */
120 );
121
122 /* Create the file port. */
Benny Prijono15953012006-04-27 22:37:08 +0000123 status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0,
Benny Prijono6fd4b8f2006-06-22 18:51:50 +0000124 0, &file_port);
Benny Prijonobc797312006-03-24 20:44:27 +0000125 if (status != PJ_SUCCESS) {
126 app_perror(THIS_FILE, "Unable to open file", status);
127 return 1;
128 }
129
130 /* File must have same number of channels. */
131 if (file_port->info.channel_count != (unsigned)channel_count) {
132 PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. "
133 "Perhaps you'd need -c option?"));
134 pjmedia_port_destroy(file_port);
135 return 1;
136 }
137
138 /* Calculate number of samples per frame to be taken from file port */
139 //ptime = samples_per_frame * 1000 / sampling_rate;
140
141 /* Create the resample port. */
Benny Prijono37235af2006-06-18 02:02:36 +0000142 status = pjmedia_resample_port_create( pool, file_port,
143 sampling_rate, 0,
Benny Prijonobc797312006-03-24 20:44:27 +0000144 &resample_port);
145 if (status != PJ_SUCCESS) {
146 app_perror(THIS_FILE, "Unable to create resample port", status);
147 return 1;
148 }
149
Benny Prijonobc797312006-03-24 20:44:27 +0000150 /* Create sound player port. */
151 status = pjmedia_snd_port_create(
152 pool, /* pool */
153 dev_id, /* device */
154 dev_id, /* device */
155 sampling_rate, /* clock rate. */
156 channel_count, /* # of channels. */
157 samples_per_frame, /* samples per frame. */
158 bits_per_sample, /* bits per sample. */
159 0, /* options */
160 &snd_port /* returned port */
161 );
162 if (status != PJ_SUCCESS) {
163 app_perror(THIS_FILE, "Unable to open sound device", status);
164 return 1;
165 }
166
167 /* Connect resample port to sound device */
168 status = pjmedia_snd_port_connect( snd_port, resample_port);
169 if (status != PJ_SUCCESS) {
170 app_perror(THIS_FILE, "Error connecting sound ports", status);
171 return 1;
172 }
173
174
175 /* Dump memory usage */
176 dump_pool_usage(THIS_FILE, &cp);
177
178 /*
179 * File should be playing and looping now, using sound device's thread.
180 */
181
182
183 /* Sleep to allow log messages to flush */
184 pj_thread_sleep(100);
185
186
187 printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n",
Benny Prijono15953012006-04-27 22:37:08 +0000188 argv[pj_optind], sampling_rate, file_port->info.clock_rate);
Benny Prijonobc797312006-03-24 20:44:27 +0000189 puts("");
190 puts("Press <ENTER> to stop playing and quit");
191
192 fgets(tmp, sizeof(tmp), stdin);
193
194
195 /* Start deinitialization: */
196
197
198 /* Destroy sound device */
199 status = pjmedia_snd_port_destroy( snd_port );
200 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
201
202
203 /* Destroy resample port.
204 * This will destroy all downstream ports (e.g. the file port)
205 */
206 status = pjmedia_port_destroy( resample_port );
207 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
208
209
210 /* Release application pool */
211 pj_pool_release( pool );
212
213 /* Destroy media endpoint. */
214 pjmedia_endpt_destroy( med_endpt );
215
216 /* Destroy pool factory */
217 pj_caching_pool_destroy( &cp );
218
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000219 /* Shutdown PJLIB */
220 pj_shutdown();
221
Benny Prijonobc797312006-03-24 20:44:27 +0000222
223 /* Done. */
224 return 0;
225
226}
227
228
229