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