blob: 86314382bf4a162a5a3954fde07327f5a7130b9c [file] [log] [blame]
Benny Prijonobc797312006-03-24 20:44:27 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonobc797312006-03-24 20:44:27 +00005 *
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
Benny Prijono1ec70b32006-06-20 15:39:07 +000021/**
22 * \page page_pjmedia_samples_recfile_c Samples: Capturing Audio to WAV File
23 *
24 * In this example, we capture audio from the sound device and save it to
25 * WAVE file.
26 *
27 * @see page_pjmedia_samples_playfile_c
28 *
29 * This file is pjsip-apps/src/samples/recfile.c
30 *
31 * \includelineno recfile.c
32 */
Benny Prijonobc797312006-03-24 20:44:27 +000033
34#include <pjmedia.h>
35#include <pjlib.h>
36
37#include <stdio.h>
38
39/* For logging purpose. */
40#define THIS_FILE "recfile.c"
41
Benny Prijono1ec70b32006-06-20 15:39:07 +000042
Benny Prijonobc797312006-03-24 20:44:27 +000043/* Configs */
44#define CLOCK_RATE 44100
Benny Prijonobc797312006-03-24 20:44:27 +000045#define NCHANNELS 2
Benny Prijono8cc996a2008-03-07 14:32:29 +000046#define SAMPLES_PER_FRAME (NCHANNELS * (CLOCK_RATE * 10 / 1000))
Benny Prijonobc797312006-03-24 20:44:27 +000047#define BITS_PER_SAMPLE 16
48
49
50static const char *desc =
51 " FILE \n"
52 " recfile.c \n"
53 " \n"
54 " PURPOSE: \n"
55 " Record microphone to WAVE file. \n"
56 " \n"
57 " USAGE: \n"
58 " recfile FILE.WAV \n"
59 "";
60
61
62/* Util to display the error message for the specified error code */
63static int app_perror( const char *sender, const char *title,
64 pj_status_t status)
65{
66 char errmsg[PJ_ERR_MSG_SIZE];
67
68 PJ_UNUSED_ARG(sender);
69
70 pj_strerror(status, errmsg, sizeof(errmsg));
71
72 printf("%s: %s [code=%d]\n", title, errmsg, status);
73 return 1;
74}
75
76
77/*
78 * main()
79 */
80int main(int argc, char *argv[])
81{
82 pj_caching_pool cp;
83 pjmedia_endpt *med_endpt;
84 pj_pool_t *pool;
85 pjmedia_port *file_port;
86 pjmedia_snd_port *snd_port;
87 char tmp[10];
88 pj_status_t status;
89
90
91 /* Verify cmd line arguments. */
92 if (argc != 2) {
93 puts("");
94 puts(desc);
95 return 0;
96 }
97
98 /* Must init PJLIB first: */
99 status = pj_init();
100 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
101
102 /* Must create a pool factory before we can allocate any memory. */
103 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
104
105 /*
106 * Initialize media endpoint.
107 * This will implicitly initialize PJMEDIA too.
108 */
109 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
110 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
111
112 /* Create memory pool for our file player */
113 pool = pj_pool_create( &cp.factory, /* pool factory */
114 "app", /* pool name. */
115 4000, /* init size */
116 4000, /* increment size */
117 NULL /* callback on error */
118 );
119
120 /* Create WAVE file writer port. */
Benny Prijono15953012006-04-27 22:37:08 +0000121 status = pjmedia_wav_writer_port_create( pool, argv[1],
Benny Prijonobc797312006-03-24 20:44:27 +0000122 CLOCK_RATE,
123 NCHANNELS,
124 SAMPLES_PER_FRAME,
125 BITS_PER_SAMPLE,
Benny Prijono6fd4b8f2006-06-22 18:51:50 +0000126 0, 0,
Benny Prijonobc797312006-03-24 20:44:27 +0000127 &file_port);
128 if (status != PJ_SUCCESS) {
129 app_perror(THIS_FILE, "Unable to open WAV file for writing", status);
130 return 1;
131 }
132
133 /* Create sound player port. */
134 status = pjmedia_snd_port_create_rec(
135 pool, /* pool */
136 -1, /* use default dev. */
Benny Prijonoc45d9512010-12-10 11:04:30 +0000137 PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate. */
138 PJMEDIA_PIA_CCNT(&file_port->info),/* # of channels. */
139 PJMEDIA_PIA_SPF(&file_port->info), /* samples per frame. */
140 PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample. */
Benny Prijonobc797312006-03-24 20:44:27 +0000141 0, /* options */
142 &snd_port /* returned port */
143 );
144 if (status != PJ_SUCCESS) {
145 app_perror(THIS_FILE, "Unable to open sound device", status);
146 return 1;
147 }
148
149 /* Connect file port to the sound player.
150 * Stream playing will commence immediately.
151 */
152 status = pjmedia_snd_port_connect( snd_port, file_port);
153 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
154
155
156
157 /*
158 * Recording should be started now.
159 */
160
161
162 /* Sleep to allow log messages to flush */
163 pj_thread_sleep(10);
164
165
166 printf("Recodring %s..\n", argv[1]);
167 puts("");
168 puts("Press <ENTER> to stop recording and quit");
169
Benny Prijono32d267b2009-01-01 22:08:21 +0000170 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
171 puts("EOF while reading stdin, will quit now..");
172 }
Benny Prijonobc797312006-03-24 20:44:27 +0000173
174
175 /* Start deinitialization: */
176
177 /* Destroy sound device */
178 status = pjmedia_snd_port_destroy( snd_port );
179 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
180
181
182 /* Destroy file port */
183 status = pjmedia_port_destroy( file_port );
184 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
185
186
187 /* Release application pool */
188 pj_pool_release( pool );
189
190 /* Destroy media endpoint. */
191 pjmedia_endpt_destroy( med_endpt );
192
193 /* Destroy pool factory */
194 pj_caching_pool_destroy( &cp );
195
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000196 /* Shutdown PJLIB */
197 pj_shutdown();
198
Benny Prijonobc797312006-03-24 20:44:27 +0000199
200 /* Done. */
201 return 0;
202}