Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 1 | /* $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 | |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 20 | /** |
| 21 | * \page page_pjmedia_samples_recfile_c Samples: Capturing Audio to WAV File |
| 22 | * |
| 23 | * In this example, we capture audio from the sound device and save it to |
| 24 | * WAVE file. |
| 25 | * |
| 26 | * @see page_pjmedia_samples_playfile_c |
| 27 | * |
| 28 | * This file is pjsip-apps/src/samples/recfile.c |
| 29 | * |
| 30 | * \includelineno recfile.c |
| 31 | */ |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 32 | |
| 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 | |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 41 | |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 42 | /* Configs */ |
| 43 | #define CLOCK_RATE 44100 |
| 44 | #define SAMPLES_PER_FRAME (CLOCK_RATE * 20 / 1000) |
| 45 | #define NCHANNELS 2 |
| 46 | #define BITS_PER_SAMPLE 16 |
| 47 | |
| 48 | |
| 49 | static 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 */ |
| 62 | static 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 | */ |
| 79 | int main(int argc, char *argv[]) |
| 80 | { |
| 81 | pj_caching_pool cp; |
| 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. */ |
| 102 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 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. */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 120 | status = pjmedia_wav_writer_port_create( pool, argv[1], |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 121 | CLOCK_RATE, |
| 122 | NCHANNELS, |
| 123 | SAMPLES_PER_FRAME, |
| 124 | BITS_PER_SAMPLE, |
Benny Prijono | 6fd4b8f | 2006-06-22 18:51:50 +0000 | [diff] [blame] | 125 | 0, 0, |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 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. */ |
| 133 | status = pjmedia_snd_port_create_rec( |
| 134 | pool, /* pool */ |
| 135 | -1, /* use default dev. */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 136 | file_port->info.clock_rate, /* clock rate. */ |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 137 | file_port->info.channel_count, /* # of channels. */ |
| 138 | file_port->info.samples_per_frame, /* samples per frame. */ |
| 139 | file_port->info.bits_per_sample, /* 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 | fgets(tmp, sizeof(tmp), stdin); |
| 170 | |
| 171 | |
| 172 | /* Start deinitialization: */ |
| 173 | |
| 174 | /* Destroy sound device */ |
| 175 | status = pjmedia_snd_port_destroy( snd_port ); |
| 176 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 177 | |
| 178 | |
| 179 | /* Destroy file port */ |
| 180 | status = pjmedia_port_destroy( file_port ); |
| 181 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 182 | |
| 183 | |
| 184 | /* Release application pool */ |
| 185 | pj_pool_release( pool ); |
| 186 | |
| 187 | /* Destroy media endpoint. */ |
| 188 | pjmedia_endpt_destroy( med_endpt ); |
| 189 | |
| 190 | /* Destroy pool factory */ |
| 191 | pj_caching_pool_destroy( &cp ); |
| 192 | |
| 193 | |
| 194 | /* Done. */ |
| 195 | return 0; |
| 196 | } |