pjsip logo pjsip.org
Open source SIP stack and media stack for presence, im/instant messaging, and multimedia communication

HOME

SIP/media Features
High Performance SIP
Small Footprint SIP
Symbian Port

FAQ

Documentation

Licensing

Download

Development (Trac)

Projects using pjsip

Mailing List

Open Source Links


About: PJLIB, PJLIB-UTIL, PJSIP, and PJMEDIA are created by: Benny Prijono
<bennylp@pjsip.org>


 

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

00001 /* $Id: recfile.c 2408 2009-01-01 22:08:21Z bennylp $ */
00002 /* 
00003  * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
00004  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00019  */
00020 
00034 #include <pjmedia.h>
00035 #include <pjlib.h>
00036 
00037 #include <stdio.h>
00038 
00039 /* For logging purpose. */
00040 #define THIS_FILE   "recfile.c"
00041 
00042 
00043 /* Configs */
00044 #define CLOCK_RATE          44100
00045 #define NCHANNELS           2
00046 #define SAMPLES_PER_FRAME   (NCHANNELS * (CLOCK_RATE * 10 / 1000))
00047 #define BITS_PER_SAMPLE     16
00048 
00049 
00050 static const char *desc = 
00051  " FILE                                     \n"
00052  "  recfile.c                               \n"
00053  "                                          \n"
00054  " PURPOSE:                                 \n"
00055  "  Record microphone to WAVE file.         \n"
00056  "                                          \n"
00057  " USAGE:                                   \n"
00058  "  recfile FILE.WAV                        \n"
00059  "";
00060 
00061 
00062 /* Util to display the error message for the specified error code  */
00063 static int app_perror( const char *sender, const char *title, 
00064                        pj_status_t status)
00065 {
00066     char errmsg[PJ_ERR_MSG_SIZE];
00067 
00068     PJ_UNUSED_ARG(sender);
00069 
00070     pj_strerror(status, errmsg, sizeof(errmsg));
00071 
00072     printf("%s: %s [code=%d]\n", title, errmsg, status);
00073     return 1;
00074 }
00075 
00076 
00077 /*
00078  * main()
00079  */
00080 int main(int argc, char *argv[])
00081 {
00082     pj_caching_pool cp;
00083     pjmedia_endpt *med_endpt;
00084     pj_pool_t *pool;
00085     pjmedia_port *file_port;
00086     pjmedia_snd_port *snd_port;
00087     char tmp[10];
00088     pj_status_t status;
00089 
00090 
00091     /* Verify cmd line arguments. */
00092     if (argc != 2) {
00093         puts("");
00094         puts(desc);
00095         return 0;
00096     }
00097 
00098     /* Must init PJLIB first: */
00099     status = pj_init();
00100     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00101 
00102     /* Must create a pool factory before we can allocate any memory. */
00103     pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00104 
00105     /* 
00106      * Initialize media endpoint.
00107      * This will implicitly initialize PJMEDIA too.
00108      */
00109     status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00110     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00111 
00112     /* Create memory pool for our file player */
00113     pool = pj_pool_create( &cp.factory,     /* pool factory         */
00114                            "app",           /* pool name.           */
00115                            4000,            /* init size            */
00116                            4000,            /* increment size       */
00117                            NULL             /* callback on error    */
00118                            );
00119 
00120     /* Create WAVE file writer port. */
00121     status = pjmedia_wav_writer_port_create(  pool, argv[1],
00122                                               CLOCK_RATE,
00123                                               NCHANNELS,
00124                                               SAMPLES_PER_FRAME,
00125                                               BITS_PER_SAMPLE,
00126                                               0, 0, 
00127                                               &file_port);
00128     if (status != PJ_SUCCESS) {
00129         app_perror(THIS_FILE, "Unable to open WAV file for writing", status);
00130         return 1;
00131     }
00132 
00133     /* Create sound player port. */
00134     status = pjmedia_snd_port_create_rec( 
00135                  pool,                              /* pool                 */
00136                  -1,                                /* use default dev.     */
00137                  file_port->info.clock_rate,        /* clock rate.          */
00138                  file_port->info.channel_count,     /* # of channels.       */
00139                  file_port->info.samples_per_frame, /* samples per frame.   */
00140                  file_port->info.bits_per_sample,   /* bits per sample.     */
00141                  0,                                 /* options              */
00142                  &snd_port                          /* returned port        */
00143                  );
00144     if (status != PJ_SUCCESS) {
00145         app_perror(THIS_FILE, "Unable to open sound device", status);
00146         return 1;
00147     }
00148 
00149     /* Connect file port to the sound player.
00150      * Stream playing will commence immediately.
00151      */
00152     status = pjmedia_snd_port_connect( snd_port, file_port);
00153     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00154 
00155 
00156 
00157     /* 
00158      * Recording should be started now. 
00159      */
00160 
00161 
00162     /* Sleep to allow log messages to flush */
00163     pj_thread_sleep(10);
00164 
00165 
00166     printf("Recodring %s..\n", argv[1]);
00167     puts("");
00168     puts("Press <ENTER> to stop recording and quit");
00169 
00170     if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
00171         puts("EOF while reading stdin, will quit now..");
00172     }
00173 
00174     
00175     /* Start deinitialization: */
00176 
00177     /* Destroy sound device */
00178     status = pjmedia_snd_port_destroy( snd_port );
00179     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00180 
00181 
00182     /* Destroy file port */
00183     status = pjmedia_port_destroy( file_port );
00184     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00185 
00186 
00187     /* Release application pool */
00188     pj_pool_release( pool );
00189 
00190     /* Destroy media endpoint. */
00191     pjmedia_endpt_destroy( med_endpt );
00192 
00193     /* Destroy pool factory */
00194     pj_caching_pool_destroy( &cp );
00195 
00196     /* Shutdown PJLIB */
00197     pj_shutdown();
00198 
00199 
00200     /* Done. */
00201     return 0;
00202 }

 


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