BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

Samples: Playing WAV File to Sound Device

This is a very simple example to use the AVI 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 /* $Id: playfile.c 3664 2011-07-19 03:42:28Z nanang $ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <pjmedia.h>
22 #include <pjlib-util.h>
23 #include <pjlib.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include "util.h"
28 
29 
46 /*
47  * playfile.c
48  *
49  * PURPOSE:
50  * Play a WAV file to sound player device.
51  *
52  * USAGE:
53  * playfile FILE.WAV
54  *
55  * The WAV file could have mono or stereo channels with arbitrary
56  * sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM.
57  *
58  */
59 
60 
61 /* For logging purpose. */
62 #define THIS_FILE "playfile.c"
63 
64 
65 static const char *desc =
66 " FILE \n"
67 " \n"
68 " playfile.c \n"
69 " \n"
70 " PURPOSE \n"
71 " \n"
72 " Demonstrate how to play a WAV file. \n"
73 " \n"
74 " USAGE \n"
75 " \n"
76 " playfile FILE.WAV \n"
77 " \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";
80 
81 
82 /*
83  * main()
84  */
85 int main(int argc, char *argv[])
86 {
87  pj_caching_pool cp;
88  pjmedia_endpt *med_endpt;
89  pj_pool_t *pool;
90  pjmedia_port *file_port;
91  pjmedia_snd_port *snd_port;
92  char tmp[10];
93  pj_status_t status;
94 
95 
96  if (argc != 2) {
97  puts("Error: filename required");
98  puts(desc);
99  return 1;
100  }
101 
102 
103  /* Must init PJLIB first: */
104  status = pj_init();
105  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
106 
107  /* Must create a pool factory before we can allocate any memory. */
109 
110  /*
111  * Initialize media endpoint.
112  * This will implicitly initialize PJMEDIA too.
113  */
114  status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
115  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
116 
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 */
123  );
124 
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 */
128  20, /* ptime. */
129  0, /* flags */
130  0, /* default buffer */
131  &file_port/* returned port */
132  );
133  if (status != PJ_SUCCESS) {
134  app_perror(THIS_FILE, "Unable to use WAV file", status);
135  return 1;
136  }
137 
138  /* Create sound player port. */
140  pool, /* pool */
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. */
146  0, /* options */
147  &snd_port /* returned port */
148  );
149  if (status != PJ_SUCCESS) {
150  app_perror(THIS_FILE, "Unable to open sound device", status);
151  return 1;
152  }
153 
154  /* Connect file port to the sound player.
155  * Stream playing will commence immediately.
156  */
157  status = pjmedia_snd_port_connect( snd_port, file_port);
158  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
159 
160 
161 
162  /*
163  * File should be playing and looping now, using sound device's thread.
164  */
165 
166 
167  /* Sleep to allow log messages to flush */
168  pj_thread_sleep(100);
169 
170 
171  printf("Playing %s..\n", argv[1]);
172  puts("");
173  puts("Press <ENTER> to stop playing and quit");
174 
175  if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
176  puts("EOF while reading stdin, will quit now..");
177  }
178 
179 
180  /* Start deinitialization: */
181 
182  /* Disconnect sound port from file port */
183  status = pjmedia_snd_port_disconnect(snd_port);
184  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
185 
186  /* Without this sleep, Windows/DirectSound will repeteadly
187  * play the last frame during destroy.
188  */
189  pj_thread_sleep(100);
190 
191  /* Destroy sound device */
192  status = pjmedia_snd_port_destroy( snd_port );
193  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
194 
195 
196  /* Destroy file port */
197  status = pjmedia_port_destroy( file_port );
198  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
199 
200 
201  /* Release application pool */
202  pj_pool_release( pool );
203 
204  /* Destroy media endpoint. */
205  pjmedia_endpt_destroy( med_endpt );
206 
207  /* Destroy pool factory */
209 
210  /* Shutdown PJLIB */
211  pj_shutdown();
212 
213 
214  /* Done. */
215  return 0;
216 }
217 
PJMEDIA main header file.
#define PJ_ASSERT_RETURN(expr, retval)
pjmedia_port_info info
Definition: port.h:366
void pj_pool_release(pj_pool_t *pool)
pj_status_t pjmedia_port_destroy(pjmedia_port *port)
struct pjmedia_endpt pjmedia_endpt
Definition: types.h:187
pj_status_t pjmedia_snd_port_destroy(pjmedia_snd_port *snd_port)
struct pjmedia_snd_port pjmedia_snd_port
Definition: sound_port.h:145
int pj_status_t
pj_pool_factory_policy pj_pool_factory_default_policy
Definition: port.h:364
pj_pool_factory factory
unsigned PJMEDIA_PIA_CCNT(const pjmedia_port_info *pia)
Definition: port.h:257
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:105
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
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_wav_player_port_create(pj_pool_t *pool, const char *filename, unsigned ptime, unsigned flags, pj_ssize_t buff_size, pjmedia_port **p_port)
unsigned PJMEDIA_PIA_SPF(const pjmedia_port_info *pia)
Definition: port.h:298
pj_status_t pjmedia_snd_port_connect(pjmedia_snd_port *snd_port, pjmedia_port *port)
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)
pj_status_t pjmedia_snd_port_disconnect(pjmedia_snd_port *snd_port)
unsigned PJMEDIA_PIA_SRATE(const pjmedia_port_info *pia)
Definition: port.h:244
unsigned PJMEDIA_PIA_BITS(const pjmedia_port_info *pia)
Definition: port.h:270
void pj_shutdown(void)
pj_status_t pj_thread_sleep(unsigned msec)
PJ_SUCCESS
pj_status_t pj_init(void)
pj_status_t pjmedia_endpt_destroy(pjmedia_endpt *endpt)
Definition: endpoint.h:146
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)

 


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