WARNING: The online documentation has moved to https://docs.pjsip.org.

Visit the new documentation at https://docs.pjsip.org:

BLOG | DOCUMENTATION | GITHUB

Home --> Documentations --> PJMEDIA Reference

Samples: Playing WAV File to Sound Device

This is a very simple example to use the WAV File Player and Sound Device Port. In this example, we open both the file and sound device, and connect the two of them, and voila! Sound will be playing the contents of the file.

See also
page_pjmedia_samples_recfile_c

This file is pjsip-apps/src/samples/playfile.c

1/*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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
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
64static 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 */
84int main(int argc, char *argv[])
85{
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. */
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. */
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 */
208
209 /* Shutdown PJLIB */
210 pj_shutdown();
211
212
213 /* Done. */
214 return 0;
215}
216
pj_status_t pjmedia_wav_player_port_create(pj_pool_t *pool, const char *filename, unsigned ptime, unsigned flags, pj_ssize_t buff_size, pjmedia_port **p_port)
pj_status_t pjmedia_port_destroy(pjmedia_port *port)
unsigned PJMEDIA_PIA_BITS(const pjmedia_port_info *pia)
Definition: port.h:283
unsigned PJMEDIA_PIA_SPF(const pjmedia_port_info *pia)
Definition: port.h:311
unsigned PJMEDIA_PIA_CCNT(const pjmedia_port_info *pia)
Definition: port.h:270
unsigned PJMEDIA_PIA_SRATE(const pjmedia_port_info *pia)
Definition: port.h:257
struct pjmedia_endpt pjmedia_endpt
Definition: pjmedia/types.h:186
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:127
pj_status_t pjmedia_endpt_destroy(pjmedia_endpt *endpt)
Definition: endpoint.h:168
pj_status_t pjmedia_snd_port_connect(pjmedia_snd_port *snd_port, pjmedia_port *port)
pj_status_t pjmedia_snd_port_disconnect(pjmedia_snd_port *snd_port)
pj_status_t pjmedia_snd_port_create_player(pj_pool_t *pool, int index, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_snd_port **p_port)
pj_status_t pjmedia_snd_port_destroy(pjmedia_snd_port *snd_port)
struct pjmedia_snd_port pjmedia_snd_port
Definition: sound_port.h:145
pj_status_t pj_init(void)
int pj_status_t
void pj_shutdown(void)
PJ_SUCCESS
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
pj_pool_factory_policy pj_pool_factory_default_policy
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void pj_pool_release(pj_pool_t *pool)
pj_status_t pj_thread_sleep(unsigned msec)
#define PJ_ASSERT_RETURN(expr, retval)
PJMEDIA main header file.
pj_pool_factory factory
Definition: port.h:378
pjmedia_port_info info
Definition: port.h:379

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.