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

 


PJMEDIA small footprint Open Source media stack
(C)2003-2008 Benny Prijono