Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/resampleplay.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
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 */
19
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
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 /* For logging purpose. */
41 #define THIS_FILE "resampleplay.c"
42
43
44 static 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"
59 SND_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
65 int 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. */
123 status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0,
124 0, &file_port);
125 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 (PJMEDIA_PIA_CCNT(&file_port->info) != (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. */
142 status = pjmedia_resample_port_create( pool, file_port,
143 sampling_rate, 0,
144 &resample_port);
145 if (status != PJ_SUCCESS) {
146 app_perror(THIS_FILE, "Unable to create resample port", status);
147 return 1;
148 }
149
150 /* 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",
188 argv[pj_optind], sampling_rate,
189 PJMEDIA_PIA_SRATE(&file_port->info));
190 puts("");
191 puts("Press <ENTER> to stop playing and quit");
192
193 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
194 puts("EOF while reading stdin, will quit now..");
195 }
196
197 /* Start deinitialization: */
198
199
200 /* Destroy sound device */
201 status = pjmedia_snd_port_destroy( snd_port );
202 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
203
204
205 /* Destroy resample port.
206 * This will destroy all downstream ports (e.g. the file port)
207 */
208 status = pjmedia_port_destroy( resample_port );
209 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
210
211
212 /* Release application pool */
213 pj_pool_release( pool );
214
215 /* Destroy media endpoint. */
216 pjmedia_endpt_destroy( med_endpt );
217
218 /* Destroy pool factory */
219 pj_caching_pool_destroy( &cp );
220
221 /* Shutdown PJLIB */
222 pj_shutdown();
223
224
225 /* Done. */
226 return 0;
227
228 }
229
230
231