Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/playfile.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 #include <pjmedia.h>
21 #include <pjlib-util.h>
22 #include <pjlib.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "util.h"
27
28
29 /**
30 * \page page_pjmedia_samples_playfile_c Samples: Playing WAV File to Sound Device
31 *
32 * This is a very simple example to use the @ref PJMEDIA_FILE_PLAY and
33 * @ref PJMED_SND_PORT. In this example, we open both the file and sound
34 * device, and connect the two of them, and voila! Sound will be playing
35 * the contents of the file.
36 *
37 * @see page_pjmedia_samples_recfile_c
38 *
39 * This file is pjsip-apps/src/samples/playfile.c
40 *
41 * \includelineno playfile.c
42 */
43
44
45 /*
46 * playfile.c
47 *
48 * PURPOSE:
49 * Play a WAV file to sound player device.
50 *
51 * USAGE:
52 * playfile FILE.WAV
53 *
54 * The WAV file could have mono or stereo channels with arbitrary
55 * sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM.
56 *
57 */
58
59
60 /* For logging purpose. */
61 #define THIS_FILE "playfile.c"
62
63
64 static const char *desc =
65 " FILE \n"
66 " \n"
67 " playfile.c \n"
68 " \n"
69 " PURPOSE \n"
70 " \n"
71 " Demonstrate how to play a WAV file. \n"
72 " \n"
73 " USAGE \n"
74 " \n"
75 " playfile FILE.WAV \n"
76 " \n"
77 " The WAV file could have mono or stereo channels with arbitrary \n"
78 " sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n";
79
80
81 /*
82 * main()
83 */
84 int main(int argc, char *argv[])
85 {
86 pj_caching_pool cp;
87 pjmedia_endpt *med_endpt;
88 pj_pool_t *pool;
89 pjmedia_port *file_port;
90 pjmedia_snd_port *snd_port;
91 char tmp[10];
92 pj_status_t status;
93
94
95 if (argc != 2) {
96 puts("Error: filename required");
97 puts(desc);
98 return 1;
99 }
100
101
102 /* Must init PJLIB first: */
103 status = pj_init();
104 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
105
106 /* Must create a pool factory before we can allocate any memory. */
107 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
108
109 /*
110 * Initialize media endpoint.
111 * This will implicitly initialize PJMEDIA too.
112 */
113 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
114 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
115
116 /* Create memory pool for our file player */
117 pool = pj_pool_create( &cp.factory, /* pool factory */
118 "wav", /* pool name. */
119 4000, /* init size */
120 4000, /* increment size */
121 NULL /* callback on error */
122 );
123
124 /* Create file media port from the WAV file */
125 status = pjmedia_wav_player_port_create( pool, /* memory pool */
126 argv[1], /* file to play */
127 20, /* ptime. */
128 0, /* flags */
129 0, /* default buffer */
130 &file_port/* returned port */
131 );
132 if (status != PJ_SUCCESS) {
133 app_perror(THIS_FILE, "Unable to use WAV file", status);
134 return 1;
135 }
136
137 /* Create sound player port. */
138 status = pjmedia_snd_port_create_player(
139 pool, /* pool */
140 -1, /* use default dev. */
141 PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate. */
142 PJMEDIA_PIA_CCNT(&file_port->info),/* # of channels. */
143 PJMEDIA_PIA_SPF(&file_port->info), /* samples per frame. */
144 PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample. */
145 0, /* options */
146 &snd_port /* returned port */
147 );
148 if (status != PJ_SUCCESS) {
149 app_perror(THIS_FILE, "Unable to open sound device", status);
150 return 1;
151 }
152
153 /* Connect file port to the sound player.
154 * Stream playing will commence immediately.
155 */
156 status = pjmedia_snd_port_connect( snd_port, file_port);
157 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
158
159
160
161 /*
162 * File should be playing and looping now, using sound device's thread.
163 */
164
165
166 /* Sleep to allow log messages to flush */
167 pj_thread_sleep(100);
168
169
170 printf("Playing %s..\n", argv[1]);
171 puts("");
172 puts("Press <ENTER> to stop playing and quit");
173
174 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
175 puts("EOF while reading stdin, will quit now..");
176 }
177
178
179 /* Start deinitialization: */
180
181 /* Disconnect sound port from file port */
182 status = pjmedia_snd_port_disconnect(snd_port);
183 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
184
185 /* Without this sleep, Windows/DirectSound will repeteadly
186 * play the last frame during destroy.
187 */
188 pj_thread_sleep(100);
189
190 /* Destroy sound device */
191 status = pjmedia_snd_port_destroy( snd_port );
192 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
193
194
195 /* Destroy file port */
196 status = pjmedia_port_destroy( file_port );
197 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
198
199
200 /* Release application pool */
201 pj_pool_release( pool );
202
203 /* Destroy media endpoint. */
204 pjmedia_endpt_destroy( med_endpt );
205
206 /* Destroy pool factory */
207 pj_caching_pool_destroy( &cp );
208
209 /* Shutdown PJLIB */
210 pj_shutdown();
211
212
213 /* Done. */
214 return 0;
215 }
216