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
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00033 #include <pjmedia.h>
00034 #include <pjlib.h>
00035
00036 #include <stdio.h>
00037
00038
00039 #define THIS_FILE "recfile.c"
00040
00041
00042
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
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
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
00091 if (argc != 2) {
00092 puts("");
00093 puts(desc);
00094 return 0;
00095 }
00096
00097
00098 status = pj_init();
00099 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00100
00101
00102 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00103
00104
00105
00106
00107
00108 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00109 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00110
00111
00112 pool = pj_pool_create( &cp.factory,
00113 "app",
00114 4000,
00115 4000,
00116 NULL
00117 );
00118
00119
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
00133 status = pjmedia_snd_port_create_rec(
00134 pool,
00135 -1,
00136 file_port->info.clock_rate,
00137 file_port->info.channel_count,
00138 file_port->info.samples_per_frame,
00139 file_port->info.bits_per_sample,
00140 0,
00141 &snd_port
00142 );
00143 if (status != PJ_SUCCESS) {
00144 app_perror(THIS_FILE, "Unable to open sound device", status);
00145 return 1;
00146 }
00147
00148
00149
00150
00151 status = pjmedia_snd_port_connect( snd_port, file_port);
00152 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00153
00154
00155
00156
00157
00158
00159
00160
00161
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
00173
00174
00175 status = pjmedia_snd_port_destroy( snd_port );
00176 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00177
00178
00179
00180 status = pjmedia_port_destroy( file_port );
00181 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00182
00183
00184
00185 pj_pool_release( pool );
00186
00187
00188 pjmedia_endpt_destroy( med_endpt );
00189
00190
00191 pj_caching_pool_destroy( &cp );
00192
00193
00194 pj_shutdown();
00195
00196
00197
00198 return 0;
00199 }