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