| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include <pjmedia.h> |
| 34 |
#include <pjlib.h> |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
|
| 38 |
|
| 39 |
#define THIS_FILE "recfile.c" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define CLOCK_RATE 44100 |
| 44 |
#define NCHANNELS 2 |
| 45 |
#define SAMPLES_PER_FRAME (NCHANNELS * (CLOCK_RATE * 10 / 1000)) |
| 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 |
|
| 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 |
|
| 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 |
|
| 91 |
if (argc != 2) { |
| 92 |
puts(""); |
| 93 |
puts(desc); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
status = pj_init(); |
| 99 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 100 |
|
| 101 |
|
| 102 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 109 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 110 |
|
| 111 |
|
| 112 |
pool = pj_pool_create( &cp.factory, |
| 113 |
"app", |
| 114 |
4000, |
| 115 |
4000, |
| 116 |
NULL |
| 117 |
); |
| 118 |
|
| 119 |
|
| 120 |
status = pjmedia_wav_writer_port_create( pool, argv[1], |
| 121 |
CLOCK_RATE, |
| 122 |
NCHANNELS, |
| 123 |
SAMPLES_PER_FRAME, |
| 124 |
BITS_PER_SAMPLE, |
| 125 |
0, 0, |
| 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 |
|
| 133 |
status = pjmedia_snd_port_create_rec( |
| 134 |
pool, |
| 135 |
-1, |
| 136 |
PJMEDIA_PIA_SRATE(&file_port->info), |
| 137 |
PJMEDIA_PIA_CCNT(&file_port->info), |
| 138 |
PJMEDIA_PIA_SPF(&file_port->info), |
| 139 |
PJMEDIA_PIA_BITS(&file_port->info), |
| 140 |
0, |
| 141 |
&snd_port |
| 142 |
); |
| 143 |
if (status != PJ_SUCCESS) { |
| 144 |
app_perror(THIS_FILE, "Unable to open sound device", status); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
status = pjmedia_snd_port_connect( snd_port, file_port); |
| 152 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
if (fgets(tmp, sizeof(tmp), stdin) == NULL) { |
| 170 |
puts("EOF while reading stdin, will quit now.."); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
status = pjmedia_snd_port_destroy( snd_port ); |
| 178 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
status = pjmedia_port_destroy( file_port ); |
| 183 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
pj_pool_release( pool ); |
| 188 |
|
| 189 |
|
| 190 |
pjmedia_endpt_destroy( med_endpt ); |
| 191 |
|
| 192 |
|
| 193 |
pj_caching_pool_destroy( &cp ); |
| 194 |
|
| 195 |
|
| 196 |
pj_shutdown(); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
return 0; |
| 201 |
} |