| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjmedia.h> |
| 20 |
#include <pjlib.h> |
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
#define THIS_FILE "audio_tool.c" |
| 24 |
|
| 25 |
static pj_caching_pool caching_pool; |
| 26 |
static pj_pool_factory *pf; |
| 27 |
static FILE *fhnd; |
| 28 |
static pj_med_mgr_t *mm; |
| 29 |
static pjmedia_codec *codec; |
| 30 |
static pjmedia_codec_param cattr; |
| 31 |
|
| 32 |
|
| 33 |
#define WRITE_ORIGINAL_PCM 0 |
| 34 |
#if WRITE_ORIGINAL_PCM |
| 35 |
static FILE *fhnd_pcm; |
| 36 |
#endif |
| 37 |
|
| 38 |
static char talker_sdp[] = |
| 39 |
"v=0\r\n" |
| 40 |
"o=- 0 0 IN IP4 127.0.0.1\r\n" |
| 41 |
"s=-\r\n" |
| 42 |
"c=IN IP4 127.0.0.1\r\n" |
| 43 |
"t=0 0\r\n" |
| 44 |
"m=audio 4002 RTP/AVP 0\r\n" |
| 45 |
"a=rtpmap:0 PCMU/8000\r\n" |
| 46 |
"a=sendonly\r\n"; |
| 47 |
static char listener_sdp[] = |
| 48 |
"v=0\r\n" |
| 49 |
"o=- 0 0 IN IP4 127.0.0.1\r\n" |
| 50 |
"s=-\r\n" |
| 51 |
"c=IN IP4 127.0.0.1\r\n" |
| 52 |
"t=0 0\r\n" |
| 53 |
"m=audio 4000 RTP/AVP 0\r\n" |
| 54 |
"a=rtpmap:0 PCMU/8000\r\n" |
| 55 |
"a=recvonly\r\n"; |
| 56 |
|
| 57 |
static pj_status_t play_callback( void *user_data, |
| 58 |
pj_uint32_t timestamp, |
| 59 |
void *frame, |
| 60 |
unsigned size) |
| 61 |
{ |
| 62 |
char pkt[160]; |
| 63 |
struct pjmedia_frame in, out; |
| 64 |
int frmsz = cattr.avg_bps / 8 * cattr.ptime / 1000; |
| 65 |
|
| 66 |
if (fread(pkt, frmsz, 1, fhnd) != 1) { |
| 67 |
puts("EOF"); |
| 68 |
return -1; |
| 69 |
} else { |
| 70 |
in.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 71 |
in.buf = pkt; |
| 72 |
in.size = frmsz; |
| 73 |
out.buf = frame; |
| 74 |
if (codec->op->decode (codec, &in, size, &out) != 0) |
| 75 |
return -1; |
| 76 |
|
| 77 |
size = out.size; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static pj_status_t rec_callback( void *user_data, |
| 83 |
pj_uint32_t timestamp, |
| 84 |
const void *frame, |
| 85 |
unsigned size) |
| 86 |
{ |
| 87 |
char pkt[160]; |
| 88 |
struct pjmedia_frame in, out; |
| 89 |
|
| 90 |
|
| 91 |
#if WRITE_ORIGINAL_PCM |
| 92 |
fwrite(frame, size, 1, fhnd_pcm); |
| 93 |
#endif |
| 94 |
|
| 95 |
in.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 96 |
in.buf = (void*)frame; |
| 97 |
in.size = size; |
| 98 |
out.buf = pkt; |
| 99 |
|
| 100 |
if (codec->op->encode(codec, &in, sizeof(pkt), &out) != 0) |
| 101 |
return -1; |
| 102 |
|
| 103 |
if (fwrite(pkt, out.size, 1, fhnd) != 1) |
| 104 |
return -1; |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static pj_status_t init() |
| 109 |
{ |
| 110 |
pjmedia_codec_mgr *cm; |
| 111 |
pjmedia_codec_info id; |
| 112 |
int i; |
| 113 |
|
| 114 |
pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0); |
| 115 |
pf = &caching_pool.factory; |
| 116 |
|
| 117 |
if (pj_snd_init(&caching_pool.factory)) |
| 118 |
return -1; |
| 119 |
|
| 120 |
PJ_LOG(3,(THIS_FILE, "Dumping audio devices:")); |
| 121 |
for (i=0; i<pj_snd_get_dev_count(); ++i) { |
| 122 |
const pj_snd_dev_info *info; |
| 123 |
info = pj_snd_get_dev_info(i); |
| 124 |
PJ_LOG(3,(THIS_FILE, " %d: %s\t(%d in, %d out", |
| 125 |
i, info->name, |
| 126 |
info->input_count, info->output_count)); |
| 127 |
} |
| 128 |
|
| 129 |
mm = pj_med_mgr_create (&caching_pool.factory); |
| 130 |
cm = pj_med_mgr_get_codec_mgr (mm); |
| 131 |
|
| 132 |
id.type = PJMEDIA_TYPE_AUDIO; |
| 133 |
id.pt = 0; |
| 134 |
id.encoding_name = pj_str("PCMU"); |
| 135 |
id.sample_rate = 8000; |
| 136 |
|
| 137 |
codec = pjmedia_codec_mgr_alloc_codec (cm, &id); |
| 138 |
codec->op->default_attr(codec, &cattr); |
| 139 |
codec->op->open(codec, &cattr); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static pj_status_t deinit() |
| 144 |
{ |
| 145 |
pjmedia_codec_mgr *cm; |
| 146 |
cm = pj_med_mgr_get_codec_mgr (mm); |
| 147 |
codec->op->close(codec); |
| 148 |
pjmedia_codec_mgr_dealloc_codec (cm, codec); |
| 149 |
pj_med_mgr_destroy (mm); |
| 150 |
pj_caching_pool_destroy(&caching_pool); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
static pj_status_t record_file (const char *filename) |
| 155 |
{ |
| 156 |
pj_snd_stream *stream; |
| 157 |
pj_snd_stream_info info; |
| 158 |
int status; |
| 159 |
char s[10]; |
| 160 |
|
| 161 |
printf("Recording to file %s...\n", filename); |
| 162 |
|
| 163 |
fhnd = fopen(filename, "wb"); |
| 164 |
if (!fhnd) |
| 165 |
return -1; |
| 166 |
|
| 167 |
#if WRITE_ORIGINAL_PCM |
| 168 |
fhnd_pcm = fopen("ORIGINAL.PCM", "wb"); |
| 169 |
if (!fhnd_pcm) |
| 170 |
return -1; |
| 171 |
#endif |
| 172 |
|
| 173 |
pj_bzero(&info, sizeof(info)); |
| 174 |
info.bits_per_sample = 16; |
| 175 |
info.bytes_per_frame = 2; |
| 176 |
info.frames_per_packet = 160; |
| 177 |
info.samples_per_frame = 1; |
| 178 |
info.samples_per_sec = 8000; |
| 179 |
|
| 180 |
stream = pj_snd_open_recorder(-1, &info, &rec_callback, NULL); |
| 181 |
if (!stream){ |
| 182 |
goto on_error; |
| 183 |
} |
| 184 |
|
| 185 |
status = pj_snd_stream_start(stream); |
| 186 |
if (status != 0) |
| 187 |
goto on_error; |
| 188 |
|
| 189 |
puts("Press <ENTER> to exit recording"); |
| 190 |
fgets(s, sizeof(s), stdin); |
| 191 |
|
| 192 |
pj_snd_stream_stop(stream); |
| 193 |
pj_snd_stream_close(stream); |
| 194 |
|
| 195 |
#if WRITE_ORIGINAL_PCM |
| 196 |
fclose(fhnd_pcm); |
| 197 |
#endif |
| 198 |
fclose(fhnd); |
| 199 |
return 0; |
| 200 |
|
| 201 |
on_error: |
| 202 |
#if WRITE_ORIGINAL_PCM |
| 203 |
fclose(fhnd_pcm); |
| 204 |
#endif |
| 205 |
fclose(fhnd); |
| 206 |
|
| 207 |
pj_snd_stream_stop(stream); |
| 208 |
pj_snd_stream_close(stream); |
| 209 |
return -1; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static pj_status_t play_file (const char *filename) |
| 214 |
{ |
| 215 |
pj_snd_stream *stream; |
| 216 |
pj_snd_stream_info info; |
| 217 |
int status; |
| 218 |
char s[10]; |
| 219 |
|
| 220 |
printf("Playing file %s...\n", filename); |
| 221 |
|
| 222 |
fhnd = fopen(filename, "rb"); |
| 223 |
if (!fhnd) |
| 224 |
return -1; |
| 225 |
|
| 226 |
pj_bzero(&info, sizeof(info)); |
| 227 |
info.bits_per_sample = 16; |
| 228 |
info.bytes_per_frame = 2; |
| 229 |
info.frames_per_packet = 160; |
| 230 |
info.samples_per_frame = 1; |
| 231 |
info.samples_per_sec = 8000; |
| 232 |
|
| 233 |
stream = pj_snd_open_player(-1, &info, &play_callback, NULL); |
| 234 |
if (!stream){ |
| 235 |
fclose(fhnd); |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
status = pj_snd_stream_start(stream); |
| 240 |
if (status != 0) |
| 241 |
goto on_error; |
| 242 |
|
| 243 |
puts("Press <ENTER> to exit playing"); |
| 244 |
fgets(s, sizeof(s), stdin); |
| 245 |
|
| 246 |
pj_snd_stream_stop(stream); |
| 247 |
pj_snd_stream_close(stream); |
| 248 |
|
| 249 |
fclose(fhnd); |
| 250 |
return 0; |
| 251 |
|
| 252 |
on_error: |
| 253 |
pj_snd_stream_stop(stream); |
| 254 |
pj_snd_stream_close(stream); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
|
| 258 |
static int create_ses_by_remote_sdp(int local_port, char *sdp) |
| 259 |
{ |
| 260 |
pj_media_session_t *ses = NULL; |
| 261 |
pjsdp_session_desc *sdp_ses; |
| 262 |
pj_media_sock_info skinfo; |
| 263 |
pj_pool_t *pool; |
| 264 |
char s[4]; |
| 265 |
const pj_media_stream_info *info[2]; |
| 266 |
int i, count; |
| 267 |
|
| 268 |
pool = pj_pool_create(pf, "sdp", 1024, 0, NULL); |
| 269 |
if (!pool) { |
| 270 |
PJ_LOG(1,(THIS_FILE, "Unable to create pool")); |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
pj_bzero(&skinfo, sizeof(skinfo)); |
| 275 |
skinfo.rtp_sock = skinfo.rtcp_sock = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 0); |
| 276 |
if (skinfo.rtp_sock == PJ_INVALID_SOCKET) { |
| 277 |
PJ_LOG(1,(THIS_FILE, "Unable to create socket")); |
| 278 |
goto on_error; |
| 279 |
} |
| 280 |
|
| 281 |
pj_sockaddr_init2(&skinfo.rtp_addr_name, "0.0.0.0", local_port); |
| 282 |
if (pj_sock_bind(skinfo.rtp_sock, (struct pj_sockaddr*)&skinfo.rtp_addr_name, sizeof(pj_sockaddr_in)) != 0) { |
| 283 |
PJ_LOG(1,(THIS_FILE, "Unable to bind socket")); |
| 284 |
goto on_error; |
| 285 |
} |
| 286 |
|
| 287 |
sdp_ses = pjsdp_parse(sdp, strlen(sdp), pool); |
| 288 |
if (!sdp_ses) { |
| 289 |
PJ_LOG(1,(THIS_FILE, "Error parsing SDP")); |
| 290 |
goto on_error; |
| 291 |
} |
| 292 |
|
| 293 |
ses = pj_media_session_create_from_sdp(mm, sdp_ses, &skinfo); |
| 294 |
if (!ses) { |
| 295 |
PJ_LOG(1,(THIS_FILE, "Unable to create session from SDP")); |
| 296 |
goto on_error; |
| 297 |
} |
| 298 |
|
| 299 |
if (pj_media_session_activate(ses) != 0) { |
| 300 |
PJ_LOG(1,(THIS_FILE, "Error activating session")); |
| 301 |
goto on_error; |
| 302 |
} |
| 303 |
|
| 304 |
count = pj_media_session_enum_streams(ses, 2, info); |
| 305 |
printf("\nDumping streams: \n"); |
| 306 |
for (i=0; i<count; ++i) { |
| 307 |
const char *dir; |
| 308 |
char *local_ip; |
| 309 |
|
| 310 |
switch (info[i]->dir) { |
| 311 |
case PJMEDIA_DIR_NONE: |
| 312 |
dir = "- NONE -"; break; |
| 313 |
case PJMEDIA_DIR_ENCODING: |
| 314 |
dir = "SENDONLY"; break; |
| 315 |
case PJMEDIA_DIR_DECODING: |
| 316 |
dir = "RECVONLY"; break; |
| 317 |
case PJMEDIA_DIR_ENCODING_DECODING: |
| 318 |
dir = "SENDRECV"; break; |
| 319 |
default: |
| 320 |
dir = "?UNKNOWN"; break; |
| 321 |
} |
| 322 |
|
| 323 |
local_ip = pj_sockaddr_get_str_addr(&info[i]->sock_info.rtp_addr_name); |
| 324 |
|
| 325 |
printf(" Stream %d: %.*s %s local=%s:%d remote=%.*s:%d\n", |
| 326 |
i, info[i]->type.slen, info[i]->type.ptr, |
| 327 |
dir, |
| 328 |
local_ip, pj_sockaddr_get_port(&info[i]->sock_info.rtp_addr_name), |
| 329 |
info[i]->rem_addr.slen, info[i]->rem_addr.ptr, info[i]->rem_port); |
| 330 |
} |
| 331 |
|
| 332 |
puts("Press <ENTER> to quit"); |
| 333 |
fgets(s, sizeof(s), stdin); |
| 334 |
|
| 335 |
pj_media_session_destroy(ses); |
| 336 |
pj_sock_close(skinfo.rtp_sock); |
| 337 |
pj_pool_release(pool); |
| 338 |
|
| 339 |
return 0; |
| 340 |
|
| 341 |
on_error: |
| 342 |
if (ses) |
| 343 |
pj_media_session_destroy(ses); |
| 344 |
if (skinfo.rtp_sock != PJ_INVALID_SOCKET) |
| 345 |
pj_sock_close(skinfo.rtp_sock); |
| 346 |
if (pool) |
| 347 |
pj_pool_release(pool); |
| 348 |
return -1; |
| 349 |
} |
| 350 |
|
| 351 |
#if WRITE_ORIGINAL_PCM |
| 352 |
static pj_status_t convert(const char *src, const char *dst) |
| 353 |
{ |
| 354 |
char pcm[320]; |
| 355 |
char frame[160]; |
| 356 |
struct pjmedia_frame in, out; |
| 357 |
|
| 358 |
fhnd_pcm = fopen(src, "rb"); |
| 359 |
if (!fhnd_pcm) |
| 360 |
return -1; |
| 361 |
fhnd = fopen(dst, "wb"); |
| 362 |
if (!fhnd) |
| 363 |
return -1; |
| 364 |
|
| 365 |
while (fread(pcm, 320, 1, fhnd_pcm) == 1) { |
| 366 |
|
| 367 |
in.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 368 |
in.buf = pcm; |
| 369 |
in.size = 320; |
| 370 |
out.buf = frame; |
| 371 |
|
| 372 |
if (codec->op->encode(codec, &in, 160, &out) != 0) |
| 373 |
break; |
| 374 |
|
| 375 |
if (fwrite(frame, out.size, 1, fhnd) != 1) |
| 376 |
break; |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
fclose(fhnd); |
| 381 |
fclose(fhnd_pcm); |
| 382 |
return 0; |
| 383 |
} |
| 384 |
#endif |
| 385 |
|
| 386 |
static void usage(const char *exe) |
| 387 |
{ |
| 388 |
printf("Usage: %s <command> <file>\n", exe); |
| 389 |
puts("where:"); |
| 390 |
puts(" <command> play|record|send|recv"); |
| 391 |
} |
| 392 |
|
| 393 |
int main(int argc, char *argv[]) |
| 394 |
{ |
| 395 |
if (argc < 2) { |
| 396 |
usage(argv[0]); |
| 397 |
return 1; |
| 398 |
} |
| 399 |
|
| 400 |
pj_init(); |
| 401 |
|
| 402 |
init(); |
| 403 |
|
| 404 |
if (stricmp(argv[1], "record")==0) { |
| 405 |
record_file("FILE.PCM"); |
| 406 |
} else if (stricmp(argv[1], "play")==0) { |
| 407 |
play_file("FILE.PCM"); |
| 408 |
} else if (stricmp(argv[1], "send")==0) { |
| 409 |
create_ses_by_remote_sdp(4002, listener_sdp); |
| 410 |
} else if (stricmp(argv[1], "recv")==0) { |
| 411 |
create_ses_by_remote_sdp(4000, talker_sdp); |
| 412 |
} else { |
| 413 |
usage(argv[0]); |
| 414 |
} |
| 415 |
deinit(); |
| 416 |
return 0; |
| 417 |
} |