1
/* $Id: playfile.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
#include <pjlib-util.h>
31
* \page page_pjmedia_samples_playfile_c Samples: Playing WAV File to Sound Device
33
* This is a very simple example to use the @ref PJMEDIA_FILE_PLAY and
34
* @ref PJMED_SND_PORT. In this example, we open both the file and sound
35
* device, and connect the two of them, and voila! Sound will be playing
36
* the contents of the file.
38
* @see page_pjmedia_samples_recfile_c
40
* This file is pjsip-apps/src/samples/playfile.c
42
* \includelineno playfile.c
50
* Play a WAV file to sound player device.
55
* The WAV file could have mono or stereo channels with arbitrary
56
* sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM.
61
/* For logging purpose. */
62
#define THIS_FILE "playfile.c"
65
static const char *desc =
72
" Demonstrate how to play a WAV file. \n"
76
" playfile FILE.WAV \n"
78
" The WAV file could have mono or stereo channels with arbitrary \n"
79
" sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n";
85
int main(int argc, char *argv[])
88
pjmedia_endpt *med_endpt;
90
pjmedia_port *file_port;
91
pjmedia_snd_port *snd_port;
97
puts("Error: filename required");
103
/* Must init PJLIB first: */
105
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
107
/* Must create a pool factory before we can allocate any memory. */
108
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
111
* Initialize media endpoint.
112
* This will implicitly initialize PJMEDIA too.
114
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
115
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
117
/* Create memory pool for our file player */
118
pool = pj_pool_create( &cp.factory, /* pool factory */
119
"wav", /* pool name. */
120
4000, /* init size */
121
4000, /* increment size */
122
NULL /* callback on error */
125
/* Create file media port from the WAV file */
126
status = pjmedia_wav_player_port_create( pool, /* memory pool */
127
argv[1], /* file to play */
130
0, /* default buffer */
131
&file_port/* returned port */
133
if (status != PJ_SUCCESS) {
134
app_perror(THIS_FILE, "Unable to use WAV file", status);
138
/* Create sound player port. */
139
status = pjmedia_snd_port_create_player(
141
-1, /* use default dev. */
142
PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate. */
143
PJMEDIA_PIA_CCNT(&file_port->info),/* # of channels. */
144
PJMEDIA_PIA_SPF(&file_port->info), /* samples per frame. */
145
PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample. */
147
&snd_port /* returned port */
149
if (status != PJ_SUCCESS) {
150
app_perror(THIS_FILE, "Unable to open sound device", status);
154
/* Connect file port to the sound player.
155
* Stream playing will commence immediately.
157
status = pjmedia_snd_port_connect( snd_port, file_port);
158
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
163
* File should be playing and looping now, using sound device's thread.
167
/* Sleep to allow log messages to flush */
168
pj_thread_sleep(100);
171
printf("Playing %s..\n", argv[1]);
173
puts("Press <ENTER> to stop playing and quit");
175
if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
176
puts("EOF while reading stdin, will quit now..");
180
/* Start deinitialization: */
182
/* Disconnect sound port from file port */
183
status = pjmedia_snd_port_disconnect(snd_port);
184
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
186
/* Without this sleep, Windows/DirectSound will repeteadly
187
* play the last frame during destroy.
189
pj_thread_sleep(100);
191
/* Destroy sound device */
192
status = pjmedia_snd_port_destroy( snd_port );
193
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
196
/* Destroy file port */
197
status = pjmedia_port_destroy( file_port );
198
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
201
/* Release application pool */
202
pj_pool_release( pool );
204
/* Destroy media endpoint. */
205
pjmedia_endpt_destroy( med_endpt );
207
/* Destroy pool factory */
208
pj_caching_pool_destroy( &cp );