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: Capturing Audio to WAV File

In this example, we capture audio from the sound device and save it to WAVE file.

See also
page_pjmedia_samples_playfile_c

This file is pjsip-apps/src/samples/recfile.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
33#include <pjmedia.h>
34#include <pjlib.h>
35
36#include <stdio.h>
37
38/* For logging purpose. */
39#define THIS_FILE "recfile.c"
40
41
42/* Configs */
43#define CLOCK_RATE 44100
44#define NCHANNELS 2
45#define SAMPLES_PER_FRAME (NCHANNELS * (CLOCK_RATE * 10 / 1000))
46#define BITS_PER_SAMPLE 16
47
48
49static const char *desc =
50 " FILE \n"
51 " recfile.c \n"
52 " \n"
53 " PURPOSE: \n"
54 " Record microphone to WAVE file. \n"
55 " \n"
56 " USAGE: \n"
57 " recfile FILE.WAV \n"
58 "";
59
60
61/* Util to display the error message for the specified error code */
62static int app_perror( const char *sender, const char *title,
63 pj_status_t status)
64{
65 char errmsg[PJ_ERR_MSG_SIZE];
66
67 PJ_UNUSED_ARG(sender);
68
69 pj_strerror(status, errmsg, sizeof(errmsg));
70
71 printf("%s: %s [code=%d]\n", title, errmsg, status);
72 return 1;
73}
74
75
76/*
77 * main()
78 */
79int main(int argc, char *argv[])
80{
82 pjmedia_endpt *med_endpt;
83 pj_pool_t *pool;
84 pjmedia_port *file_port;
85 pjmedia_snd_port *snd_port;
86 char tmp[10];
87 pj_status_t status;
88
89
90 /* Verify cmd line arguments. */
91 if (argc != 2) {
92 puts("");
93 puts(desc);
94 return 0;
95 }
96
97 /* Must init PJLIB first: */
98 status = pj_init();
99 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
100
101 /* Must create a pool factory before we can allocate any memory. */
103
104 /*
105 * Initialize media endpoint.
106 * This will implicitly initialize PJMEDIA too.
107 */
108 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
109 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
110
111 /* Create memory pool for our file player */
112 pool = pj_pool_create( &cp.factory, /* pool factory */
113 "app", /* pool name. */
114 4000, /* init size */
115 4000, /* increment size */
116 NULL /* callback on error */
117 );
118
119 /* Create WAVE file writer port. */
120 status = pjmedia_wav_writer_port_create( pool, argv[1],
121 CLOCK_RATE,
122 NCHANNELS,
123 SAMPLES_PER_FRAME,
124 BITS_PER_SAMPLE,
125 0, 0,
126 &file_port);
127 if (status != PJ_SUCCESS) {
128 app_perror(THIS_FILE, "Unable to open WAV file for writing", status);
129 return 1;
130 }
131
132 /* Create sound player port. */
134 pool, /* pool */
135 -1, /* use default dev. */
136 PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate. */
137 PJMEDIA_PIA_CCNT(&file_port->info),/* # of channels. */
138 PJMEDIA_PIA_SPF(&file_port->info), /* samples per frame. */
139 PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample. */
140 0, /* options */
141 &snd_port /* returned port */
142 );
143 if (status != PJ_SUCCESS) {
144 app_perror(THIS_FILE, "Unable to open sound device", status);
145 return 1;
146 }
147
148 /* Connect file port to the sound player.
149 * Stream playing will commence immediately.
150 */
151 status = pjmedia_snd_port_connect( snd_port, file_port);
152 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
153
154
155
156 /*
157 * Recording should be started now.
158 */
159
160
161 /* Sleep to allow log messages to flush */
162 pj_thread_sleep(10);
163
164
165 printf("Recodring %s..\n", argv[1]);
166 puts("");
167 puts("Press <ENTER> to stop recording and quit");
168
169 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
170 puts("EOF while reading stdin, will quit now..");
171 }
172
173
174 /* Start deinitialization: */
175
176 /* Destroy sound device */
177 status = pjmedia_snd_port_destroy( snd_port );
178 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
179
180
181 /* Destroy file port */
182 status = pjmedia_port_destroy( file_port );
183 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
184
185
186 /* Release application pool */
187 pj_pool_release( pool );
188
189 /* Destroy media endpoint. */
190 pjmedia_endpt_destroy( med_endpt );
191
192 /* Destroy pool factory */
194
195 /* Shutdown PJLIB */
196 pj_shutdown();
197
198
199 /* Done. */
200 return 0;
201}
pj_status_t pjmedia_wav_writer_port_create(pj_pool_t *pool, const char *filename, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, 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_destroy(pjmedia_snd_port *snd_port)
pj_status_t pjmedia_snd_port_create_rec(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)
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)
#define PJ_UNUSED_ARG(arg)
#define PJ_ERR_MSG_SIZE
pj_str_t pj_strerror(pj_status_t statcode, char *buf, pj_size_t bufsize)
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.