RTPencode.cc revision d324546ced76d4e792338af4f7d02a5cd8819f92
1/* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11//TODO(hlundin): Reformat file to meet style guide. 12 13/* header includes */ 14#include <stdio.h> 15#include <stdlib.h> 16#include <string.h> 17#ifdef WIN32 18#include <winsock2.h> 19#endif 20#ifdef WEBRTC_LINUX 21#include <netinet/in.h> 22#endif 23 24#include <assert.h> 25 26#include "webrtc/typedefs.h" 27// needed for NetEqDecoder 28#include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h" 29#include "webrtc/modules/audio_coding/neteq/interface/neteq.h" 30 31/************************/ 32/* Define payload types */ 33/************************/ 34 35#include "PayloadTypes.h" 36 37 38 39/*********************/ 40/* Misc. definitions */ 41/*********************/ 42 43#define STOPSENDTIME 3000 44#define RESTARTSENDTIME 0 //162500 45#define FIRSTLINELEN 40 46#define CHECK_NOT_NULL(a) if((a)==0){printf("\n %s \n line: %d \nerror at %s\n",__FILE__,__LINE__,#a );return(-1);} 47 48//#define MULTIPLE_SAME_TIMESTAMP 49#define REPEAT_PACKET_DISTANCE 17 50#define REPEAT_PACKET_COUNT 1 // number of extra packets to send 51 52//#define INSERT_OLD_PACKETS 53#define OLD_PACKET 5 // how many seconds too old should the packet be? 54 55//#define TIMESTAMP_WRAPAROUND 56 57//#define RANDOM_DATA 58//#define RANDOM_PAYLOAD_DATA 59#define RANDOM_SEED 10 60 61//#define INSERT_DTMF_PACKETS 62//#define NO_DTMF_OVERDUB 63#define DTMF_PACKET_INTERVAL 2000 64#define DTMF_DURATION 500 65 66#define STEREO_MODE_FRAME 0 67#define STEREO_MODE_SAMPLE_1 1 //1 octet per sample 68#define STEREO_MODE_SAMPLE_2 2 //2 octets per sample 69 70/*************************/ 71/* Function declarations */ 72/*************************/ 73 74void NetEQTest_GetCodec_and_PT(char* name, 75 webrtc::NetEqDecoder* codec, 76 int* PT, 77 int frameLen, 78 int* fs, 79 int* bitrate, 80 int* useRed); 81int NetEQTest_init_coders(webrtc::NetEqDecoder coder, 82 int enc_frameSize, 83 int bitrate, 84 int sampfreq, 85 int vad, 86 int numChannels); 87void defineCodecs(webrtc::NetEqDecoder* usedCodec, int* noOfCodecs); 88int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels); 89int NetEQTest_encode(int coder, 90 int16_t* indata, 91 int frameLen, 92 unsigned char* encoded, 93 int sampleRate, 94 int* vad, 95 int useVAD, 96 int bitrate, 97 int numChannels); 98void makeRTPheader(unsigned char* rtp_data, 99 int payloadType, 100 int seqNo, 101 uint32_t timestamp, 102 uint32_t ssrc); 103int makeRedundantHeader(unsigned char* rtp_data, 104 int* payloadType, 105 int numPayloads, 106 uint32_t* timestamp, 107 uint16_t* blockLen, 108 int seqNo, 109 uint32_t ssrc); 110int makeDTMFpayload(unsigned char* payload_data, 111 int Event, 112 int End, 113 int Volume, 114 int Duration); 115void stereoDeInterleave(int16_t* audioSamples, int numSamples); 116void stereoInterleave(unsigned char* data, int dataLen, int stride); 117 118/*********************/ 119/* Codec definitions */ 120/*********************/ 121 122#include "webrtc_vad.h" 123 124#if ((defined CODEC_PCM16B)||(defined NETEQ_ARBITRARY_CODEC)) 125 #include "pcm16b.h" 126#endif 127#ifdef CODEC_G711 128 #include "g711_interface.h" 129#endif 130#ifdef CODEC_G729 131 #include "G729Interface.h" 132#endif 133#ifdef CODEC_G729_1 134 #include "G729_1Interface.h" 135#endif 136#ifdef CODEC_AMR 137 #include "AMRInterface.h" 138 #include "AMRCreation.h" 139#endif 140#ifdef CODEC_AMRWB 141 #include "AMRWBInterface.h" 142 #include "AMRWBCreation.h" 143#endif 144#ifdef CODEC_ILBC 145 #include "ilbc.h" 146#endif 147#if (defined CODEC_ISAC || defined CODEC_ISAC_SWB) 148 #include "isac.h" 149#endif 150#ifdef NETEQ_ISACFIX_CODEC 151 #include "isacfix.h" 152 #ifdef CODEC_ISAC 153 #error Cannot have both ISAC and ISACfix defined. Please de-select one in the beginning of RTPencode.cpp 154 #endif 155#endif 156#ifdef CODEC_G722 157 #include "g722_interface.h" 158#endif 159#ifdef CODEC_G722_1_24 160 #include "G722_1Interface.h" 161#endif 162#ifdef CODEC_G722_1_32 163 #include "G722_1Interface.h" 164#endif 165#ifdef CODEC_G722_1_16 166 #include "G722_1Interface.h" 167#endif 168#ifdef CODEC_G722_1C_24 169 #include "G722_1Interface.h" 170#endif 171#ifdef CODEC_G722_1C_32 172 #include "G722_1Interface.h" 173#endif 174#ifdef CODEC_G722_1C_48 175 #include "G722_1Interface.h" 176#endif 177#ifdef CODEC_G726 178 #include "G726Creation.h" 179 #include "G726Interface.h" 180#endif 181#ifdef CODEC_GSMFR 182 #include "GSMFRInterface.h" 183 #include "GSMFRCreation.h" 184#endif 185#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 186 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 187 #include "webrtc_cng.h" 188#endif 189#if ((defined CODEC_SPEEX_8)||(defined CODEC_SPEEX_16)) 190 #include "SpeexInterface.h" 191#endif 192 193/***********************************/ 194/* Global codec instance variables */ 195/***********************************/ 196 197WebRtcVadInst *VAD_inst[2]; 198 199#ifdef CODEC_G722 200 G722EncInst *g722EncState[2]; 201#endif 202 203#ifdef CODEC_G722_1_24 204 G722_1_24_encinst_t *G722_1_24enc_inst[2]; 205#endif 206#ifdef CODEC_G722_1_32 207 G722_1_32_encinst_t *G722_1_32enc_inst[2]; 208#endif 209#ifdef CODEC_G722_1_16 210 G722_1_16_encinst_t *G722_1_16enc_inst[2]; 211#endif 212#ifdef CODEC_G722_1C_24 213 G722_1C_24_encinst_t *G722_1C_24enc_inst[2]; 214#endif 215#ifdef CODEC_G722_1C_32 216 G722_1C_32_encinst_t *G722_1C_32enc_inst[2]; 217#endif 218#ifdef CODEC_G722_1C_48 219 G722_1C_48_encinst_t *G722_1C_48enc_inst[2]; 220#endif 221#ifdef CODEC_G726 222 G726_encinst_t *G726enc_inst[2]; 223#endif 224#ifdef CODEC_G729 225 G729_encinst_t *G729enc_inst[2]; 226#endif 227#ifdef CODEC_G729_1 228 G729_1_inst_t *G729_1_inst[2]; 229#endif 230#ifdef CODEC_AMR 231 AMR_encinst_t *AMRenc_inst[2]; 232 int16_t AMR_bitrate; 233#endif 234#ifdef CODEC_AMRWB 235 AMRWB_encinst_t *AMRWBenc_inst[2]; 236 int16_t AMRWB_bitrate; 237#endif 238#ifdef CODEC_ILBC 239 IlbcEncoderInstance *iLBCenc_inst[2]; 240#endif 241#ifdef CODEC_ISAC 242 ISACStruct *ISAC_inst[2]; 243#endif 244#ifdef NETEQ_ISACFIX_CODEC 245 ISACFIX_MainStruct *ISAC_inst[2]; 246#endif 247#ifdef CODEC_ISAC_SWB 248 ISACStruct *ISACSWB_inst[2]; 249#endif 250#ifdef CODEC_GSMFR 251 GSMFR_encinst_t *GSMFRenc_inst[2]; 252#endif 253#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 254 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 255 CNG_enc_inst *CNGenc_inst[2]; 256#endif 257#ifdef CODEC_SPEEX_8 258 SPEEX_encinst_t *SPEEX8enc_inst[2]; 259#endif 260#ifdef CODEC_SPEEX_16 261 SPEEX_encinst_t *SPEEX16enc_inst[2]; 262#endif 263 264int main(int argc, char* argv[]) 265{ 266 int packet_size, fs; 267 webrtc::NetEqDecoder usedCodec; 268 int payloadType; 269 int bitrate = 0; 270 int useVAD, vad; 271 int useRed=0; 272 int len, enc_len; 273 int16_t org_data[4000]; 274 unsigned char rtp_data[8000]; 275 int16_t seqNo=0xFFF; 276 uint32_t ssrc=1235412312; 277 uint32_t timestamp=0xAC1245; 278 uint16_t length, plen; 279 uint32_t offset; 280 double sendtime = 0; 281 int red_PT[2] = {0}; 282 uint32_t red_TS[2] = {0}; 283 uint16_t red_len[2] = {0}; 284 int RTPheaderLen=12; 285 uint8_t red_data[8000]; 286#ifdef INSERT_OLD_PACKETS 287 uint16_t old_length, old_plen; 288 int old_enc_len; 289 int first_old_packet=1; 290 unsigned char old_rtp_data[8000]; 291 int packet_age=0; 292#endif 293#ifdef INSERT_DTMF_PACKETS 294 int NTone = 1; 295 int DTMFfirst = 1; 296 uint32_t DTMFtimestamp; 297 bool dtmfSent = false; 298#endif 299 bool usingStereo = false; 300 int stereoMode = 0; 301 int numChannels = 1; 302 303 /* check number of parameters */ 304 if ((argc != 6) && (argc != 7)) { 305 /* print help text and exit */ 306 printf("Application to encode speech into an RTP stream.\n"); 307 printf("The program reads a PCM file and encodes is using the specified codec.\n"); 308 printf("The coded speech is packetized in RTP packest and written to the output file.\n"); 309 printf("The format of the RTP stream file is simlilar to that of rtpplay,\n"); 310 printf("but with the receive time euqal to 0 for all packets.\n"); 311 printf("Usage:\n\n"); 312 printf("%s PCMfile RTPfile frameLen codec useVAD bitrate\n", argv[0]); 313 printf("where:\n"); 314 315 printf("PCMfile : PCM speech input file\n\n"); 316 317 printf("RTPfile : RTP stream output file\n\n"); 318 319 printf("frameLen : 80...960... Number of samples per packet (limit depends on codec)\n\n"); 320 321 printf("codecName\n"); 322#ifdef CODEC_PCM16B 323 printf(" : pcm16b 16 bit PCM (8kHz)\n"); 324#endif 325#ifdef CODEC_PCM16B_WB 326 printf(" : pcm16b_wb 16 bit PCM (16kHz)\n"); 327#endif 328#ifdef CODEC_PCM16B_32KHZ 329 printf(" : pcm16b_swb32 16 bit PCM (32kHz)\n"); 330#endif 331#ifdef CODEC_PCM16B_48KHZ 332 printf(" : pcm16b_swb48 16 bit PCM (48kHz)\n"); 333#endif 334#ifdef CODEC_G711 335 printf(" : pcma g711 A-law (8kHz)\n"); 336#endif 337#ifdef CODEC_G711 338 printf(" : pcmu g711 u-law (8kHz)\n"); 339#endif 340#ifdef CODEC_G729 341 printf(" : g729 G729 (8kHz and 8kbps) CELP (One-Three frame(s)/packet)\n"); 342#endif 343#ifdef CODEC_G729_1 344 printf(" : g729.1 G729.1 (16kHz) variable rate (8--32 kbps)\n"); 345#endif 346#ifdef CODEC_G722_1_16 347 printf(" : g722.1_16 G722.1 coder (16kHz) (g722.1 with 16kbps)\n"); 348#endif 349#ifdef CODEC_G722_1_24 350 printf(" : g722.1_24 G722.1 coder (16kHz) (the 24kbps version)\n"); 351#endif 352#ifdef CODEC_G722_1_32 353 printf(" : g722.1_32 G722.1 coder (16kHz) (the 32kbps version)\n"); 354#endif 355#ifdef CODEC_G722_1C_24 356 printf(" : g722.1C_24 G722.1 C coder (32kHz) (the 24kbps version)\n"); 357#endif 358#ifdef CODEC_G722_1C_32 359 printf(" : g722.1C_32 G722.1 C coder (32kHz) (the 32kbps version)\n"); 360#endif 361#ifdef CODEC_G722_1C_48 362 printf(" : g722.1C_48 G722.1 C coder (32kHz) (the 48kbps)\n"); 363#endif 364 365#ifdef CODEC_G726 366 printf(" : g726_16 G726 coder (8kHz) 16kbps\n"); 367 printf(" : g726_24 G726 coder (8kHz) 24kbps\n"); 368 printf(" : g726_32 G726 coder (8kHz) 32kbps\n"); 369 printf(" : g726_40 G726 coder (8kHz) 40kbps\n"); 370#endif 371#ifdef CODEC_AMR 372 printf(" : AMRXk Adaptive Multi Rate CELP codec (8kHz)\n"); 373 printf(" X = 4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2 or 12.2\n"); 374#endif 375#ifdef CODEC_AMRWB 376 printf(" : AMRwbXk Adaptive Multi Rate Wideband CELP codec (16kHz)\n"); 377 printf(" X = 7, 9, 12, 14, 16, 18, 20, 23 or 24\n"); 378#endif 379#ifdef CODEC_ILBC 380 printf(" : ilbc iLBC codec (8kHz and 13.8kbps)\n"); 381#endif 382#ifdef CODEC_ISAC 383 printf(" : isac iSAC (16kHz and 32.0 kbps). To set rate specify a rate parameter as last parameter\n"); 384#endif 385#ifdef CODEC_ISAC_SWB 386 printf(" : isacswb iSAC SWB (32kHz and 32.0-52.0 kbps). To set rate specify a rate parameter as last parameter\n"); 387#endif 388#ifdef CODEC_GSMFR 389 printf(" : gsmfr GSM FR codec (8kHz and 13kbps)\n"); 390#endif 391#ifdef CODEC_G722 392 printf(" : g722 g722 coder (16kHz) (the 64kbps version)\n"); 393#endif 394#ifdef CODEC_SPEEX_8 395 printf(" : speex8 speex coder (8 kHz)\n"); 396#endif 397#ifdef CODEC_SPEEX_16 398 printf(" : speex16 speex coder (16 kHz)\n"); 399#endif 400#ifdef CODEC_RED 401#ifdef CODEC_G711 402 printf(" : red_pcm Redundancy RTP packet with 2*G711A frames\n"); 403#endif 404#ifdef CODEC_ISAC 405 printf(" : red_isac Redundancy RTP packet with 2*iSAC frames\n"); 406#endif 407#endif 408 printf("\n"); 409 410#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 411 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 412 printf("useVAD : 0 Voice Activity Detection is switched off\n"); 413 printf(" : 1 Voice Activity Detection is switched on\n\n"); 414#else 415 printf("useVAD : 0 Voice Activity Detection switched off (on not supported)\n\n"); 416#endif 417 printf("bitrate : Codec bitrate in bps (only applies to vbr codecs)\n\n"); 418 419 return(0); 420 } 421 422 FILE* in_file=fopen(argv[1],"rb"); 423 CHECK_NOT_NULL(in_file); 424 printf("Input file: %s\n",argv[1]); 425 FILE* out_file=fopen(argv[2],"wb"); 426 CHECK_NOT_NULL(out_file); 427 printf("Output file: %s\n\n",argv[2]); 428 packet_size=atoi(argv[3]); 429 CHECK_NOT_NULL(packet_size); 430 printf("Packet size: %i\n",packet_size); 431 432 // check for stereo 433 if(argv[4][strlen(argv[4])-1] == '*') { 434 // use stereo 435 usingStereo = true; 436 numChannels = 2; 437 argv[4][strlen(argv[4])-1] = '\0'; 438 } 439 440 NetEQTest_GetCodec_and_PT(argv[4], &usedCodec, &payloadType, packet_size, &fs, &bitrate, &useRed); 441 442 if(useRed) { 443 RTPheaderLen = 12 + 4 + 1; /* standard RTP = 12; 4 bytes per redundant payload, except last one which is 1 byte */ 444 } 445 446 useVAD=atoi(argv[5]); 447#if !(defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 448 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 449 if (useVAD!=0) { 450 printf("Error: this simulation does not support VAD/DTX/CNG\n"); 451 } 452#endif 453 454 // check stereo type 455 if(usingStereo) 456 { 457 switch(usedCodec) 458 { 459 // sample based codecs 460 case webrtc::kDecoderPCMu: 461 case webrtc::kDecoderPCMa: 462 case webrtc::kDecoderG722: 463 { 464 // 1 octet per sample 465 stereoMode = STEREO_MODE_SAMPLE_1; 466 break; 467 } 468 case webrtc::kDecoderPCM16B: 469 case webrtc::kDecoderPCM16Bwb: 470 case webrtc::kDecoderPCM16Bswb32kHz: 471 case webrtc::kDecoderPCM16Bswb48kHz: 472 { 473 // 2 octets per sample 474 stereoMode = STEREO_MODE_SAMPLE_2; 475 break; 476 } 477 478 // fixed-rate frame codecs (with internal VAD) 479 default: 480 { 481 printf("Cannot use codec %s as stereo codec\n", argv[4]); 482 exit(0); 483 } 484 } 485 } 486 487 if ((usedCodec == webrtc::kDecoderISAC) || (usedCodec == webrtc::kDecoderISACswb)) 488 { 489 if (argc != 7) 490 { 491 if (usedCodec == webrtc::kDecoderISAC) 492 { 493 bitrate = 32000; 494 printf( 495 "Running iSAC at default bitrate of 32000 bps (to specify explicitly add the bps as last parameter)\n"); 496 } 497 else // (usedCodec==webrtc::kDecoderISACswb) 498 { 499 bitrate = 56000; 500 printf( 501 "Running iSAC at default bitrate of 56000 bps (to specify explicitly add the bps as last parameter)\n"); 502 } 503 } 504 else 505 { 506 bitrate = atoi(argv[6]); 507 if (usedCodec == webrtc::kDecoderISAC) 508 { 509 if ((bitrate < 10000) || (bitrate > 32000)) 510 { 511 printf( 512 "Error: iSAC bitrate must be between 10000 and 32000 bps (%i is invalid)\n", 513 bitrate); 514 exit(0); 515 } 516 printf("Running iSAC at bitrate of %i bps\n", bitrate); 517 } 518 else // (usedCodec==webrtc::kDecoderISACswb) 519 { 520 if ((bitrate < 32000) || (bitrate > 56000)) 521 { 522 printf( 523 "Error: iSAC SWB bitrate must be between 32000 and 56000 bps (%i is invalid)\n", 524 bitrate); 525 exit(0); 526 } 527 } 528 } 529 } 530 else 531 { 532 if (argc == 7) 533 { 534 printf( 535 "Error: Bitrate parameter can only be specified for iSAC, G.723, and G.729.1\n"); 536 exit(0); 537 } 538 } 539 540 if(useRed) { 541 printf("Redundancy engaged. "); 542 } 543 printf("Used codec: %i\n",usedCodec); 544 printf("Payload type: %i\n",payloadType); 545 546 NetEQTest_init_coders(usedCodec, packet_size, bitrate, fs, useVAD, numChannels); 547 548 /* write file header */ 549 //fprintf(out_file, "#!RTPencode%s\n", "1.0"); 550 fprintf(out_file, "#!rtpplay%s \n", "1.0"); // this is the string that rtpplay needs 551 uint32_t dummy_variable = 0; // should be converted to network endian format, but does not matter when 0 552 if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { 553 return -1; 554 } 555 if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { 556 return -1; 557 } 558 if (fwrite(&dummy_variable, 4, 1, out_file) != 1) { 559 return -1; 560 } 561 if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { 562 return -1; 563 } 564 if (fwrite(&dummy_variable, 2, 1, out_file) != 1) { 565 return -1; 566 } 567 568#ifdef TIMESTAMP_WRAPAROUND 569 timestamp = 0xFFFFFFFF - fs*10; /* should give wrap-around in 10 seconds */ 570#endif 571#if defined(RANDOM_DATA) | defined(RANDOM_PAYLOAD_DATA) 572 srand(RANDOM_SEED); 573#endif 574 575 /* if redundancy is used, the first redundant payload is zero length */ 576 red_len[0] = 0; 577 578 /* read first frame */ 579 len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; 580 581 /* de-interleave if stereo */ 582 if ( usingStereo ) 583 { 584 stereoDeInterleave(org_data, len * numChannels); 585 } 586 587 while (len==packet_size) { 588 589#ifdef INSERT_DTMF_PACKETS 590 dtmfSent = false; 591 592 if ( sendtime >= NTone * DTMF_PACKET_INTERVAL ) { 593 if ( sendtime < NTone * DTMF_PACKET_INTERVAL + DTMF_DURATION ) { 594 // tone has not ended 595 if (DTMFfirst==1) { 596 DTMFtimestamp = timestamp; // save this timestamp 597 DTMFfirst=0; 598 } 599 makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc); 600 enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 0, 4, (int) (sendtime - NTone * DTMF_PACKET_INTERVAL)*(fs/1000) + len); 601 } 602 else { 603 // tone has ended 604 makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc); 605 enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 1, 4, DTMF_DURATION*(fs/1000)); 606 NTone++; 607 DTMFfirst=1; 608 } 609 610 /* write RTP packet to file */ 611 length = htons(12 + enc_len + 8); 612 plen = htons(12 + enc_len); 613 offset = (uint32_t) sendtime; //(timestamp/(fs/1000)); 614 offset = htonl(offset); 615 if (fwrite(&length, 2, 1, out_file) != 1) { 616 return -1; 617 } 618 if (fwrite(&plen, 2, 1, out_file) != 1) { 619 return -1; 620 } 621 if (fwrite(&offset, 4, 1, out_file) != 1) { 622 return -1; 623 } 624 if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { 625 return -1; 626 } 627 628 dtmfSent = true; 629 } 630#endif 631 632#ifdef NO_DTMF_OVERDUB 633 /* If DTMF is sent, we should not send any speech packets during the same time */ 634 if (dtmfSent) { 635 enc_len = 0; 636 } 637 else { 638#endif 639 /* encode frame */ 640 enc_len=NetEQTest_encode(usedCodec, org_data, packet_size, &rtp_data[12] ,fs,&vad, useVAD, bitrate, numChannels); 641 if (enc_len==-1) { 642 printf("Error encoding frame\n"); 643 exit(0); 644 } 645 646 if ( usingStereo && 647 stereoMode != STEREO_MODE_FRAME && 648 vad == 1 ) 649 { 650 // interleave the encoded payload for sample-based codecs (not for CNG) 651 stereoInterleave(&rtp_data[12], enc_len, stereoMode); 652 } 653#ifdef NO_DTMF_OVERDUB 654 } 655#endif 656 657 if (enc_len > 0 && (sendtime <= STOPSENDTIME || sendtime > RESTARTSENDTIME)) { 658 if(useRed) { 659 if(red_len[0] > 0) { 660 memmove(&rtp_data[RTPheaderLen+red_len[0]], &rtp_data[12], enc_len); 661 memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); 662 663 red_len[1] = enc_len; 664 red_TS[1] = timestamp; 665 if(vad) 666 red_PT[1] = payloadType; 667 else 668 red_PT[1] = NETEQ_CODEC_CN_PT; 669 670 makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc); 671 672 673 enc_len += red_len[0] + RTPheaderLen - 12; 674 } 675 else { // do not use redundancy payload for this packet, i.e., only last payload 676 memmove(&rtp_data[RTPheaderLen-4], &rtp_data[12], enc_len); 677 //memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]); 678 679 red_len[1] = enc_len; 680 red_TS[1] = timestamp; 681 if(vad) 682 red_PT[1] = payloadType; 683 else 684 red_PT[1] = NETEQ_CODEC_CN_PT; 685 686 makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc); 687 688 689 enc_len += red_len[0] + RTPheaderLen - 4 - 12; // 4 is length of redundancy header (not used) 690 } 691 } 692 else { 693 694 /* make RTP header */ 695 if (vad) // regular speech data 696 makeRTPheader(rtp_data, payloadType, seqNo++,timestamp, ssrc); 697 else // CNG data 698 makeRTPheader(rtp_data, NETEQ_CODEC_CN_PT, seqNo++,timestamp, ssrc); 699 700 } 701#ifdef MULTIPLE_SAME_TIMESTAMP 702 int mult_pack=0; 703 do { 704#endif //MULTIPLE_SAME_TIMESTAMP 705 /* write RTP packet to file */ 706 length = htons(12 + enc_len + 8); 707 plen = htons(12 + enc_len); 708 offset = (uint32_t) sendtime; 709 //(timestamp/(fs/1000)); 710 offset = htonl(offset); 711 if (fwrite(&length, 2, 1, out_file) != 1) { 712 return -1; 713 } 714 if (fwrite(&plen, 2, 1, out_file) != 1) { 715 return -1; 716 } 717 if (fwrite(&offset, 4, 1, out_file) != 1) { 718 return -1; 719 } 720#ifdef RANDOM_DATA 721 for (int k=0; k<12+enc_len; k++) { 722 rtp_data[k] = rand() + rand(); 723 } 724#endif 725#ifdef RANDOM_PAYLOAD_DATA 726 for (int k=12; k<12+enc_len; k++) { 727 rtp_data[k] = rand() + rand(); 728 } 729#endif 730 if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) { 731 return -1; 732 } 733#ifdef MULTIPLE_SAME_TIMESTAMP 734 } while ( (seqNo%REPEAT_PACKET_DISTANCE == 0) && (mult_pack++ < REPEAT_PACKET_COUNT) ); 735#endif //MULTIPLE_SAME_TIMESTAMP 736 737#ifdef INSERT_OLD_PACKETS 738 if (packet_age >= OLD_PACKET*fs) { 739 if (!first_old_packet) { 740 // send the old packet 741 if (fwrite(&old_length, 2, 1, 742 out_file) != 1) { 743 return -1; 744 } 745 if (fwrite(&old_plen, 2, 1, 746 out_file) != 1) { 747 return -1; 748 } 749 if (fwrite(&offset, 4, 1, 750 out_file) != 1) { 751 return -1; 752 } 753 if (fwrite(old_rtp_data, 12 + old_enc_len, 754 1, out_file) != 1) { 755 return -1; 756 } 757 } 758 // store current packet as old 759 old_length=length; 760 old_plen=plen; 761 memcpy(old_rtp_data,rtp_data,12+enc_len); 762 old_enc_len=enc_len; 763 first_old_packet=0; 764 packet_age=0; 765 766 } 767 packet_age += packet_size; 768#endif 769 770 if(useRed) { 771 /* move data to redundancy store */ 772#ifdef CODEC_ISAC 773 if(usedCodec==webrtc::kDecoderISAC) 774 { 775 assert(!usingStereo); // Cannot handle stereo yet 776 red_len[0] = 777 WebRtcIsac_GetRedPayload(ISAC_inst[0], red_data); 778 } 779 else 780 { 781#endif 782 memcpy(red_data, &rtp_data[RTPheaderLen+red_len[0]], enc_len); 783 red_len[0]=red_len[1]; 784#ifdef CODEC_ISAC 785 } 786#endif 787 red_TS[0]=red_TS[1]; 788 red_PT[0]=red_PT[1]; 789 } 790 791 } 792 793 /* read next frame */ 794 len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels; 795 /* de-interleave if stereo */ 796 if ( usingStereo ) 797 { 798 stereoDeInterleave(org_data, len * numChannels); 799 } 800 801 if (payloadType==NETEQ_CODEC_G722_PT) 802 timestamp+=len>>1; 803 else 804 timestamp+=len; 805 806 sendtime += (double) len/(fs/1000); 807 } 808 809 NetEQTest_free_coders(usedCodec, numChannels); 810 fclose(in_file); 811 fclose(out_file); 812 printf("Done!\n"); 813 814 return(0); 815} 816 817 818 819 820/****************/ 821/* Subfunctions */ 822/****************/ 823 824void NetEQTest_GetCodec_and_PT(char* name, 825 webrtc::NetEqDecoder* codec, 826 int* PT, 827 int frameLen, 828 int* fs, 829 int* bitrate, 830 int* useRed) { 831 832 *bitrate = 0; /* Default bitrate setting */ 833 *useRed = 0; /* Default no redundancy */ 834 835 if(!strcmp(name,"pcmu")){ 836 *codec=webrtc::kDecoderPCMu; 837 *PT=NETEQ_CODEC_PCMU_PT; 838 *fs=8000; 839 } 840 else if(!strcmp(name,"pcma")){ 841 *codec=webrtc::kDecoderPCMa; 842 *PT=NETEQ_CODEC_PCMA_PT; 843 *fs=8000; 844 } 845 else if(!strcmp(name,"pcm16b")){ 846 *codec=webrtc::kDecoderPCM16B; 847 *PT=NETEQ_CODEC_PCM16B_PT; 848 *fs=8000; 849 } 850 else if(!strcmp(name,"pcm16b_wb")){ 851 *codec=webrtc::kDecoderPCM16Bwb; 852 *PT=NETEQ_CODEC_PCM16B_WB_PT; 853 *fs=16000; 854 } 855 else if(!strcmp(name,"pcm16b_swb32")){ 856 *codec=webrtc::kDecoderPCM16Bswb32kHz; 857 *PT=NETEQ_CODEC_PCM16B_SWB32KHZ_PT; 858 *fs=32000; 859 } 860 else if(!strcmp(name,"pcm16b_swb48")){ 861 *codec=webrtc::kDecoderPCM16Bswb48kHz; 862 *PT=NETEQ_CODEC_PCM16B_SWB48KHZ_PT; 863 *fs=48000; 864 } 865 else if(!strcmp(name,"g722")){ 866 *codec=webrtc::kDecoderG722; 867 *PT=NETEQ_CODEC_G722_PT; 868 *fs=16000; 869 } 870 else if((!strcmp(name,"ilbc"))&&((frameLen%240==0)||(frameLen%160==0))){ 871 *fs=8000; 872 *codec=webrtc::kDecoderILBC; 873 *PT=NETEQ_CODEC_ILBC_PT; 874 } 875 else if(!strcmp(name,"isac")){ 876 *fs=16000; 877 *codec=webrtc::kDecoderISAC; 878 *PT=NETEQ_CODEC_ISAC_PT; 879 } 880 else if(!strcmp(name,"isacswb")){ 881 *fs=32000; 882 *codec=webrtc::kDecoderISACswb; 883 *PT=NETEQ_CODEC_ISACSWB_PT; 884 } 885 else if(!strcmp(name,"red_pcm")){ 886 *codec=webrtc::kDecoderPCMa; 887 *PT=NETEQ_CODEC_PCMA_PT; /* this will be the PT for the sub-headers */ 888 *fs=8000; 889 *useRed = 1; 890 } else if(!strcmp(name,"red_isac")){ 891 *codec=webrtc::kDecoderISAC; 892 *PT=NETEQ_CODEC_ISAC_PT; /* this will be the PT for the sub-headers */ 893 *fs=16000; 894 *useRed = 1; 895 } else { 896 printf("Error: Not a supported codec (%s)\n", name); 897 exit(0); 898 } 899 900} 901 902 903 904 905int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels){ 906 907 int ok=0; 908 909 for (int k = 0; k < numChannels; k++) 910 { 911 ok=WebRtcVad_Create(&VAD_inst[k]); 912 if (ok!=0) { 913 printf("Error: Couldn't allocate memory for VAD instance\n"); 914 exit(0); 915 } 916 ok=WebRtcVad_Init(VAD_inst[k]); 917 if (ok==-1) { 918 printf("Error: Initialization of VAD struct failed\n"); 919 exit(0); 920 } 921 922 923#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 924 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 925 ok=WebRtcCng_CreateEnc(&CNGenc_inst[k]); 926 if (ok!=0) { 927 printf("Error: Couldn't allocate memory for CNG encoding instance\n"); 928 exit(0); 929 } 930 if(sampfreq <= 16000) { 931 ok=WebRtcCng_InitEnc(CNGenc_inst[k],sampfreq, 200, 5); 932 if (ok==-1) { 933 printf("Error: Initialization of CNG struct failed. Error code %d\n", 934 WebRtcCng_GetErrorCodeEnc(CNGenc_inst[k])); 935 exit(0); 936 } 937 } 938#endif 939 940 switch (coder) { 941#ifdef CODEC_PCM16B 942 case webrtc::kDecoderPCM16B : 943#endif 944#ifdef CODEC_PCM16B_WB 945 case webrtc::kDecoderPCM16Bwb : 946#endif 947#ifdef CODEC_PCM16B_32KHZ 948 case webrtc::kDecoderPCM16Bswb32kHz : 949#endif 950#ifdef CODEC_PCM16B_48KHZ 951 case webrtc::kDecoderPCM16Bswb48kHz : 952#endif 953#ifdef CODEC_G711 954 case webrtc::kDecoderPCMu : 955 case webrtc::kDecoderPCMa : 956#endif 957 // do nothing 958 break; 959#ifdef CODEC_G729 960 case webrtc::kDecoderG729: 961 if (sampfreq==8000) { 962 if ((enc_frameSize==80)||(enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==400)||(enc_frameSize==480)) { 963 ok=WebRtcG729_CreateEnc(&G729enc_inst[k]); 964 if (ok!=0) { 965 printf("Error: Couldn't allocate memory for G729 encoding instance\n"); 966 exit(0); 967 } 968 } else { 969 printf("\nError: g729 only supports 10, 20, 30, 40, 50 or 60 ms!!\n\n"); 970 exit(0); 971 } 972 WebRtcG729_EncoderInit(G729enc_inst[k], vad); 973 if ((vad==1)&&(enc_frameSize!=80)) { 974 printf("\nError - This simulation only supports VAD for G729 at 10ms packets (not %dms)\n", (enc_frameSize>>3)); 975 } 976 } else { 977 printf("\nError - g729 is only developed for 8kHz \n"); 978 exit(0); 979 } 980 break; 981#endif 982#ifdef CODEC_G729_1 983 case webrtc::kDecoderG729_1: 984 if (sampfreq==16000) { 985 if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960) 986 ) { 987 ok=WebRtcG7291_Create(&G729_1_inst[k]); 988 if (ok!=0) { 989 printf("Error: Couldn't allocate memory for G.729.1 codec instance\n"); 990 exit(0); 991 } 992 } else { 993 printf("\nError: G.729.1 only supports 20, 40 or 60 ms!!\n\n"); 994 exit(0); 995 } 996 if (!(((bitrate >= 12000) && (bitrate <= 32000) && (bitrate%2000 == 0)) || (bitrate == 8000))) { 997 /* must be 8, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, or 32 kbps */ 998 printf("\nError: G.729.1 bitrate must be 8000 or 12000--32000 in steps of 2000 bps\n"); 999 exit(0); 1000 } 1001 WebRtcG7291_EncoderInit(G729_1_inst[k], bitrate, 0 /* flag8kHz*/, 0 /*flagG729mode*/); 1002 } else { 1003 printf("\nError - G.729.1 input is always 16 kHz \n"); 1004 exit(0); 1005 } 1006 break; 1007#endif 1008#ifdef CODEC_SPEEX_8 1009 case webrtc::kDecoderSPEEX_8 : 1010 if (sampfreq==8000) { 1011 if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1012 ok=WebRtcSpeex_CreateEnc(&SPEEX8enc_inst[k], sampfreq); 1013 if (ok!=0) { 1014 printf("Error: Couldn't allocate memory for Speex encoding instance\n"); 1015 exit(0); 1016 } 1017 } else { 1018 printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); 1019 exit(0); 1020 } 1021 if ((vad==1)&&(enc_frameSize!=160)) { 1022 printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>3)); 1023 vad=0; 1024 } 1025 ok=WebRtcSpeex_EncoderInit(SPEEX8enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); 1026 if (ok!=0) exit(0); 1027 } else { 1028 printf("\nError - Speex8 called with sample frequency other than 8 kHz.\n\n"); 1029 } 1030 break; 1031#endif 1032#ifdef CODEC_SPEEX_16 1033 case webrtc::kDecoderSPEEX_16 : 1034 if (sampfreq==16000) { 1035 if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960)) { 1036 ok=WebRtcSpeex_CreateEnc(&SPEEX16enc_inst[k], sampfreq); 1037 if (ok!=0) { 1038 printf("Error: Couldn't allocate memory for Speex encoding instance\n"); 1039 exit(0); 1040 } 1041 } else { 1042 printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n"); 1043 exit(0); 1044 } 1045 if ((vad==1)&&(enc_frameSize!=320)) { 1046 printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>4)); 1047 vad=0; 1048 } 1049 ok=WebRtcSpeex_EncoderInit(SPEEX16enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad); 1050 if (ok!=0) exit(0); 1051 } else { 1052 printf("\nError - Speex16 called with sample frequency other than 16 kHz.\n\n"); 1053 } 1054 break; 1055#endif 1056 1057#ifdef CODEC_G722_1_16 1058 case webrtc::kDecoderG722_1_16 : 1059 if (sampfreq==16000) { 1060 ok=WebRtcG7221_CreateEnc16(&G722_1_16enc_inst[k]); 1061 if (ok!=0) { 1062 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1063 exit(0); 1064 } 1065 if (enc_frameSize==320) { 1066 } else { 1067 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1068 exit(0); 1069 } 1070 WebRtcG7221_EncoderInit16((G722_1_16_encinst_t*)G722_1_16enc_inst[k]); 1071 } else { 1072 printf("\nError - G722.1 is only developed for 16kHz \n"); 1073 exit(0); 1074 } 1075 break; 1076#endif 1077#ifdef CODEC_G722_1_24 1078 case webrtc::kDecoderG722_1_24 : 1079 if (sampfreq==16000) { 1080 ok=WebRtcG7221_CreateEnc24(&G722_1_24enc_inst[k]); 1081 if (ok!=0) { 1082 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1083 exit(0); 1084 } 1085 if (enc_frameSize==320) { 1086 } else { 1087 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1088 exit(0); 1089 } 1090 WebRtcG7221_EncoderInit24((G722_1_24_encinst_t*)G722_1_24enc_inst[k]); 1091 } else { 1092 printf("\nError - G722.1 is only developed for 16kHz \n"); 1093 exit(0); 1094 } 1095 break; 1096#endif 1097#ifdef CODEC_G722_1_32 1098 case webrtc::kDecoderG722_1_32 : 1099 if (sampfreq==16000) { 1100 ok=WebRtcG7221_CreateEnc32(&G722_1_32enc_inst[k]); 1101 if (ok!=0) { 1102 printf("Error: Couldn't allocate memory for G.722.1 instance\n"); 1103 exit(0); 1104 } 1105 if (enc_frameSize==320) { 1106 } else { 1107 printf("\nError: G722.1 only supports 20 ms!!\n\n"); 1108 exit(0); 1109 } 1110 WebRtcG7221_EncoderInit32((G722_1_32_encinst_t*)G722_1_32enc_inst[k]); 1111 } else { 1112 printf("\nError - G722.1 is only developed for 16kHz \n"); 1113 exit(0); 1114 } 1115 break; 1116#endif 1117#ifdef CODEC_G722_1C_24 1118 case webrtc::kDecoderG722_1C_24 : 1119 if (sampfreq==32000) { 1120 ok=WebRtcG7221C_CreateEnc24(&G722_1C_24enc_inst[k]); 1121 if (ok!=0) { 1122 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1123 exit(0); 1124 } 1125 if (enc_frameSize==640) { 1126 } else { 1127 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1128 exit(0); 1129 } 1130 WebRtcG7221C_EncoderInit24((G722_1C_24_encinst_t*)G722_1C_24enc_inst[k]); 1131 } else { 1132 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1133 exit(0); 1134 } 1135 break; 1136#endif 1137#ifdef CODEC_G722_1C_32 1138 case webrtc::kDecoderG722_1C_32 : 1139 if (sampfreq==32000) { 1140 ok=WebRtcG7221C_CreateEnc32(&G722_1C_32enc_inst[k]); 1141 if (ok!=0) { 1142 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1143 exit(0); 1144 } 1145 if (enc_frameSize==640) { 1146 } else { 1147 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1148 exit(0); 1149 } 1150 WebRtcG7221C_EncoderInit32((G722_1C_32_encinst_t*)G722_1C_32enc_inst[k]); 1151 } else { 1152 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1153 exit(0); 1154 } 1155 break; 1156#endif 1157#ifdef CODEC_G722_1C_48 1158 case webrtc::kDecoderG722_1C_48 : 1159 if (sampfreq==32000) { 1160 ok=WebRtcG7221C_CreateEnc48(&G722_1C_48enc_inst[k]); 1161 if (ok!=0) { 1162 printf("Error: Couldn't allocate memory for G.722.1C instance\n"); 1163 exit(0); 1164 } 1165 if (enc_frameSize==640) { 1166 } else { 1167 printf("\nError: G722.1 C only supports 20 ms!!\n\n"); 1168 exit(0); 1169 } 1170 WebRtcG7221C_EncoderInit48((G722_1C_48_encinst_t*)G722_1C_48enc_inst[k]); 1171 } else { 1172 printf("\nError - G722.1 C is only developed for 32kHz \n"); 1173 exit(0); 1174 } 1175 break; 1176#endif 1177#ifdef CODEC_G722 1178 case webrtc::kDecoderG722 : 1179 if (sampfreq==16000) { 1180 if (enc_frameSize%2==0) { 1181 } else { 1182 printf("\nError - g722 frames must have an even number of enc_frameSize\n"); 1183 exit(0); 1184 } 1185 WebRtcG722_CreateEncoder(&g722EncState[k]); 1186 WebRtcG722_EncoderInit(g722EncState[k]); 1187 } else { 1188 printf("\nError - g722 is only developed for 16kHz \n"); 1189 exit(0); 1190 } 1191 break; 1192#endif 1193#ifdef CODEC_AMR 1194 case webrtc::kDecoderAMR : 1195 if (sampfreq==8000) { 1196 ok=WebRtcAmr_CreateEnc(&AMRenc_inst[k]); 1197 if (ok!=0) { 1198 printf("Error: Couldn't allocate memory for AMR encoding instance\n"); 1199 exit(0); 1200 }if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1201 } else { 1202 printf("\nError - AMR must have a multiple of 160 enc_frameSize\n"); 1203 exit(0); 1204 } 1205 WebRtcAmr_EncoderInit(AMRenc_inst[k], vad); 1206 WebRtcAmr_EncodeBitmode(AMRenc_inst[k], AMRBandwidthEfficient); 1207 AMR_bitrate = bitrate; 1208 } else { 1209 printf("\nError - AMR is only developed for 8kHz \n"); 1210 exit(0); 1211 } 1212 break; 1213#endif 1214#ifdef CODEC_AMRWB 1215 case webrtc::kDecoderAMRWB : 1216 if (sampfreq==16000) { 1217 ok=WebRtcAmrWb_CreateEnc(&AMRWBenc_inst[k]); 1218 if (ok!=0) { 1219 printf("Error: Couldn't allocate memory for AMRWB encoding instance\n"); 1220 exit(0); 1221 } 1222 if (((enc_frameSize/320)<0)||((enc_frameSize/320)>3)||((enc_frameSize%320)!=0)) { 1223 printf("\nError - AMRwb must have frameSize of 20, 40 or 60ms\n"); 1224 exit(0); 1225 } 1226 WebRtcAmrWb_EncoderInit(AMRWBenc_inst[k], vad); 1227 if (bitrate==7000) { 1228 AMRWB_bitrate = AMRWB_MODE_7k; 1229 } else if (bitrate==9000) { 1230 AMRWB_bitrate = AMRWB_MODE_9k; 1231 } else if (bitrate==12000) { 1232 AMRWB_bitrate = AMRWB_MODE_12k; 1233 } else if (bitrate==14000) { 1234 AMRWB_bitrate = AMRWB_MODE_14k; 1235 } else if (bitrate==16000) { 1236 AMRWB_bitrate = AMRWB_MODE_16k; 1237 } else if (bitrate==18000) { 1238 AMRWB_bitrate = AMRWB_MODE_18k; 1239 } else if (bitrate==20000) { 1240 AMRWB_bitrate = AMRWB_MODE_20k; 1241 } else if (bitrate==23000) { 1242 AMRWB_bitrate = AMRWB_MODE_23k; 1243 } else if (bitrate==24000) { 1244 AMRWB_bitrate = AMRWB_MODE_24k; 1245 } 1246 WebRtcAmrWb_EncodeBitmode(AMRWBenc_inst[k], AMRBandwidthEfficient); 1247 1248 } else { 1249 printf("\nError - AMRwb is only developed for 16kHz \n"); 1250 exit(0); 1251 } 1252 break; 1253#endif 1254#ifdef CODEC_ILBC 1255 case webrtc::kDecoderILBC : 1256 if (sampfreq==8000) { 1257 ok=WebRtcIlbcfix_EncoderCreate(&iLBCenc_inst[k]); 1258 if (ok!=0) { 1259 printf("Error: Couldn't allocate memory for iLBC encoding instance\n"); 1260 exit(0); 1261 } 1262 if ((enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==480)) { 1263 } else { 1264 printf("\nError - iLBC only supports 160, 240, 320 and 480 enc_frameSize (20, 30, 40 and 60 ms)\n"); 1265 exit(0); 1266 } 1267 if ((enc_frameSize==160)||(enc_frameSize==320)) { 1268 /* 20 ms version */ 1269 WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 20); 1270 } else { 1271 /* 30 ms version */ 1272 WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 30); 1273 } 1274 } else { 1275 printf("\nError - iLBC is only developed for 8kHz \n"); 1276 exit(0); 1277 } 1278 break; 1279#endif 1280#ifdef CODEC_ISAC 1281 case webrtc::kDecoderISAC: 1282 if (sampfreq==16000) { 1283 ok=WebRtcIsac_Create(&ISAC_inst[k]); 1284 if (ok!=0) { 1285 printf("Error: Couldn't allocate memory for iSAC instance\n"); 1286 exit(0); 1287 }if ((enc_frameSize==480)||(enc_frameSize==960)) { 1288 } else { 1289 printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); 1290 exit(0); 1291 } 1292 WebRtcIsac_EncoderInit(ISAC_inst[k],1); 1293 if ((bitrate<10000)||(bitrate>32000)) { 1294 printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); 1295 exit(0); 1296 } 1297 WebRtcIsac_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); 1298 } else { 1299 printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); 1300 exit(0); 1301 } 1302 break; 1303#endif 1304#ifdef NETEQ_ISACFIX_CODEC 1305 case webrtc::kDecoderISAC: 1306 if (sampfreq==16000) { 1307 ok=WebRtcIsacfix_Create(&ISAC_inst[k]); 1308 if (ok!=0) { 1309 printf("Error: Couldn't allocate memory for iSAC instance\n"); 1310 exit(0); 1311 }if ((enc_frameSize==480)||(enc_frameSize==960)) { 1312 } else { 1313 printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n"); 1314 exit(0); 1315 } 1316 WebRtcIsacfix_EncoderInit(ISAC_inst[k],1); 1317 if ((bitrate<10000)||(bitrate>32000)) { 1318 printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate); 1319 exit(0); 1320 } 1321 WebRtcIsacfix_Control(ISAC_inst[k], bitrate, enc_frameSize>>4); 1322 } else { 1323 printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n"); 1324 exit(0); 1325 } 1326 break; 1327#endif 1328#ifdef CODEC_ISAC_SWB 1329 case webrtc::kDecoderISACswb: 1330 if (sampfreq==32000) { 1331 ok=WebRtcIsac_Create(&ISACSWB_inst[k]); 1332 if (ok!=0) { 1333 printf("Error: Couldn't allocate memory for iSAC SWB instance\n"); 1334 exit(0); 1335 }if (enc_frameSize==960) { 1336 } else { 1337 printf("\nError - iSAC SWB only supports frameSize 30 ms\n"); 1338 exit(0); 1339 } 1340 ok = WebRtcIsac_SetEncSampRate(ISACSWB_inst[k], 32000); 1341 if (ok!=0) { 1342 printf("Error: Couldn't set sample rate for iSAC SWB instance\n"); 1343 exit(0); 1344 } 1345 WebRtcIsac_EncoderInit(ISACSWB_inst[k],1); 1346 if ((bitrate<32000)||(bitrate>56000)) { 1347 printf("\nError - iSAC SWB bitrate has to be between 32000 and 56000 bps (not %i)\n", bitrate); 1348 exit(0); 1349 } 1350 WebRtcIsac_Control(ISACSWB_inst[k], bitrate, enc_frameSize>>5); 1351 } else { 1352 printf("\nError - iSAC SWB only supports 960 enc_frameSize (30 ms)\n"); 1353 exit(0); 1354 } 1355 break; 1356#endif 1357#ifdef CODEC_GSMFR 1358 case webrtc::kDecoderGSMFR: 1359 if (sampfreq==8000) { 1360 ok=WebRtcGSMFR_CreateEnc(&GSMFRenc_inst[k]); 1361 if (ok!=0) { 1362 printf("Error: Couldn't allocate memory for GSM FR encoding instance\n"); 1363 exit(0); 1364 } 1365 if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) { 1366 } else { 1367 printf("\nError - GSM FR must have a multiple of 160 enc_frameSize\n"); 1368 exit(0); 1369 } 1370 WebRtcGSMFR_EncoderInit(GSMFRenc_inst[k], 0); 1371 } else { 1372 printf("\nError - GSM FR is only developed for 8kHz \n"); 1373 exit(0); 1374 } 1375 break; 1376#endif 1377 default : 1378 printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); 1379 exit(0); 1380 break; 1381 } 1382 1383 if (ok != 0) { 1384 return(ok); 1385 } 1386 } // end for 1387 1388 return(0); 1389} 1390 1391 1392 1393 1394int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels) { 1395 1396 for (int k = 0; k < numChannels; k++) 1397 { 1398 WebRtcVad_Free(VAD_inst[k]); 1399#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \ 1400 defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48)) 1401 WebRtcCng_FreeEnc(CNGenc_inst[k]); 1402#endif 1403 1404 switch (coder) 1405 { 1406#ifdef CODEC_PCM16B 1407 case webrtc::kDecoderPCM16B : 1408#endif 1409#ifdef CODEC_PCM16B_WB 1410 case webrtc::kDecoderPCM16Bwb : 1411#endif 1412#ifdef CODEC_PCM16B_32KHZ 1413 case webrtc::kDecoderPCM16Bswb32kHz : 1414#endif 1415#ifdef CODEC_PCM16B_48KHZ 1416 case webrtc::kDecoderPCM16Bswb48kHz : 1417#endif 1418#ifdef CODEC_G711 1419 case webrtc::kDecoderPCMu : 1420 case webrtc::kDecoderPCMa : 1421#endif 1422 // do nothing 1423 break; 1424#ifdef CODEC_G729 1425 case webrtc::kDecoderG729: 1426 WebRtcG729_FreeEnc(G729enc_inst[k]); 1427 break; 1428#endif 1429#ifdef CODEC_G729_1 1430 case webrtc::kDecoderG729_1: 1431 WebRtcG7291_Free(G729_1_inst[k]); 1432 break; 1433#endif 1434#ifdef CODEC_SPEEX_8 1435 case webrtc::kDecoderSPEEX_8 : 1436 WebRtcSpeex_FreeEnc(SPEEX8enc_inst[k]); 1437 break; 1438#endif 1439#ifdef CODEC_SPEEX_16 1440 case webrtc::kDecoderSPEEX_16 : 1441 WebRtcSpeex_FreeEnc(SPEEX16enc_inst[k]); 1442 break; 1443#endif 1444 1445#ifdef CODEC_G722_1_16 1446 case webrtc::kDecoderG722_1_16 : 1447 WebRtcG7221_FreeEnc16(G722_1_16enc_inst[k]); 1448 break; 1449#endif 1450#ifdef CODEC_G722_1_24 1451 case webrtc::kDecoderG722_1_24 : 1452 WebRtcG7221_FreeEnc24(G722_1_24enc_inst[k]); 1453 break; 1454#endif 1455#ifdef CODEC_G722_1_32 1456 case webrtc::kDecoderG722_1_32 : 1457 WebRtcG7221_FreeEnc32(G722_1_32enc_inst[k]); 1458 break; 1459#endif 1460#ifdef CODEC_G722_1C_24 1461 case webrtc::kDecoderG722_1C_24 : 1462 WebRtcG7221C_FreeEnc24(G722_1C_24enc_inst[k]); 1463 break; 1464#endif 1465#ifdef CODEC_G722_1C_32 1466 case webrtc::kDecoderG722_1C_32 : 1467 WebRtcG7221C_FreeEnc32(G722_1C_32enc_inst[k]); 1468 break; 1469#endif 1470#ifdef CODEC_G722_1C_48 1471 case webrtc::kDecoderG722_1C_48 : 1472 WebRtcG7221C_FreeEnc48(G722_1C_48enc_inst[k]); 1473 break; 1474#endif 1475#ifdef CODEC_G722 1476 case webrtc::kDecoderG722 : 1477 WebRtcG722_FreeEncoder(g722EncState[k]); 1478 break; 1479#endif 1480#ifdef CODEC_AMR 1481 case webrtc::kDecoderAMR : 1482 WebRtcAmr_FreeEnc(AMRenc_inst[k]); 1483 break; 1484#endif 1485#ifdef CODEC_AMRWB 1486 case webrtc::kDecoderAMRWB : 1487 WebRtcAmrWb_FreeEnc(AMRWBenc_inst[k]); 1488 break; 1489#endif 1490#ifdef CODEC_ILBC 1491 case webrtc::kDecoderILBC : 1492 WebRtcIlbcfix_EncoderFree(iLBCenc_inst[k]); 1493 break; 1494#endif 1495#ifdef CODEC_ISAC 1496 case webrtc::kDecoderISAC: 1497 WebRtcIsac_Free(ISAC_inst[k]); 1498 break; 1499#endif 1500#ifdef NETEQ_ISACFIX_CODEC 1501 case webrtc::kDecoderISAC: 1502 WebRtcIsacfix_Free(ISAC_inst[k]); 1503 break; 1504#endif 1505#ifdef CODEC_ISAC_SWB 1506 case webrtc::kDecoderISACswb: 1507 WebRtcIsac_Free(ISACSWB_inst[k]); 1508 break; 1509#endif 1510#ifdef CODEC_GSMFR 1511 case webrtc::kDecoderGSMFR: 1512 WebRtcGSMFR_FreeEnc(GSMFRenc_inst[k]); 1513 break; 1514#endif 1515 default : 1516 printf("Error: unknown codec in call to NetEQTest_init_coders.\n"); 1517 exit(0); 1518 break; 1519 } 1520 } 1521 1522 return(0); 1523} 1524 1525 1526 1527 1528 1529 1530int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate , 1531 int * vad, int useVAD, int bitrate, int numChannels){ 1532 1533 short cdlen = 0; 1534 int16_t *tempdata; 1535 static int first_cng=1; 1536 int16_t tempLen; 1537 1538 *vad =1; 1539 1540 // check VAD first 1541 if(useVAD) 1542 { 1543 *vad = 0; 1544 1545 for (int k = 0; k < numChannels; k++) 1546 { 1547 tempLen = frameLen; 1548 tempdata = &indata[k*frameLen]; 1549 int localVad=0; 1550 /* Partition the signal and test each chunk for VAD. 1551 All chunks must be VAD=0 to produce a total VAD=0. */ 1552 while (tempLen >= 10*sampleRate/1000) { 1553 if ((tempLen % 30*sampleRate/1000) == 0) { // tempLen is multiple of 30ms 1554 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 30*sampleRate/1000); 1555 tempdata += 30*sampleRate/1000; 1556 tempLen -= 30*sampleRate/1000; 1557 } 1558 else if (tempLen >= 20*sampleRate/1000) { // tempLen >= 20ms 1559 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 20*sampleRate/1000); 1560 tempdata += 20*sampleRate/1000; 1561 tempLen -= 20*sampleRate/1000; 1562 } 1563 else { // use 10ms 1564 localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 10*sampleRate/1000); 1565 tempdata += 10*sampleRate/1000; 1566 tempLen -= 10*sampleRate/1000; 1567 } 1568 } 1569 1570 // aggregate all VAD decisions over all channels 1571 *vad |= localVad; 1572 } 1573 1574 if(!*vad){ 1575 // all channels are silent 1576 cdlen = 0; 1577 for (int k = 0; k < numChannels; k++) 1578 { 1579 WebRtcCng_Encode(CNGenc_inst[k],&indata[k*frameLen], (frameLen <= 640 ? frameLen : 640) /* max 640 */, 1580 encoded,&tempLen,first_cng); 1581 encoded += tempLen; 1582 cdlen += tempLen; 1583 } 1584 *vad=0; 1585 first_cng=0; 1586 return(cdlen); 1587 } 1588 } 1589 1590 1591 // loop over all channels 1592 int totalLen = 0; 1593 1594 for (int k = 0; k < numChannels; k++) 1595 { 1596 /* Encode with the selected coder type */ 1597 if (coder==webrtc::kDecoderPCMu) { /*g711 u-law */ 1598#ifdef CODEC_G711 1599 cdlen = WebRtcG711_EncodeU(indata, frameLen, encoded); 1600#endif 1601 } 1602 else if (coder==webrtc::kDecoderPCMa) { /*g711 A-law */ 1603#ifdef CODEC_G711 1604 cdlen = WebRtcG711_EncodeA(indata, frameLen, encoded); 1605 } 1606#endif 1607#ifdef CODEC_PCM16B 1608 else if ((coder==webrtc::kDecoderPCM16B)||(coder==webrtc::kDecoderPCM16Bwb)|| 1609 (coder==webrtc::kDecoderPCM16Bswb32kHz)||(coder==webrtc::kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, 32kHz or 48kHz) */ 1610 cdlen = WebRtcPcm16b_Encode(indata, frameLen, encoded); 1611 } 1612#endif 1613#ifdef CODEC_G722 1614 else if (coder==webrtc::kDecoderG722) { /*g722 */ 1615 cdlen=WebRtcG722_Encode(g722EncState[k], indata, frameLen, encoded); 1616 assert(cdlen == frameLen>>1); 1617 } 1618#endif 1619#ifdef CODEC_ILBC 1620 else if (coder==webrtc::kDecoderILBC) { /*iLBC */ 1621 cdlen = WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata, 1622 frameLen, encoded); 1623 } 1624#endif 1625#if (defined(CODEC_ISAC) || defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all NETEQ_ISACFIX_CODEC 1626 else if (coder==webrtc::kDecoderISAC) { /*iSAC */ 1627 int noOfCalls=0; 1628 cdlen=0; 1629 while (cdlen<=0) { 1630#ifdef CODEC_ISAC /* floating point */ 1631 cdlen = WebRtcIsac_Encode(ISAC_inst[k], 1632 &indata[noOfCalls * 160], 1633 encoded); 1634#else /* fixed point */ 1635 cdlen = WebRtcIsacfix_Encode(ISAC_inst[k], 1636 &indata[noOfCalls * 160], 1637 encoded); 1638#endif 1639 noOfCalls++; 1640 } 1641 } 1642#endif 1643#ifdef CODEC_ISAC_SWB 1644 else if (coder==webrtc::kDecoderISACswb) { /* iSAC SWB */ 1645 int noOfCalls=0; 1646 cdlen=0; 1647 while (cdlen<=0) { 1648 cdlen = WebRtcIsac_Encode(ISACSWB_inst[k], 1649 &indata[noOfCalls * 320], 1650 encoded); 1651 noOfCalls++; 1652 } 1653 } 1654#endif 1655 indata += frameLen; 1656 encoded += cdlen; 1657 totalLen += cdlen; 1658 1659 } // end for 1660 1661 first_cng=1; 1662 return(totalLen); 1663} 1664 1665 1666 1667void makeRTPheader(unsigned char* rtp_data, 1668 int payloadType, 1669 int seqNo, 1670 uint32_t timestamp, 1671 uint32_t ssrc) { 1672 rtp_data[0] = 0x80; 1673 rtp_data[1] = payloadType & 0xFF; 1674 rtp_data[2] = (seqNo >> 8) & 0xFF; 1675 rtp_data[3] = seqNo & 0xFF; 1676 rtp_data[4] = timestamp >> 24; 1677 rtp_data[5] = (timestamp >> 16) & 0xFF; 1678 rtp_data[6] = (timestamp >> 8) & 0xFF; 1679 rtp_data[7] = timestamp & 0xFF; 1680 rtp_data[8] = ssrc >> 24; 1681 rtp_data[9] = (ssrc >> 16) & 0xFF; 1682 rtp_data[10] = (ssrc >> 8) & 0xFF; 1683 rtp_data[11] = ssrc & 0xFF; 1684} 1685 1686 1687int makeRedundantHeader(unsigned char* rtp_data, 1688 int* payloadType, 1689 int numPayloads, 1690 uint32_t* timestamp, 1691 uint16_t* blockLen, 1692 int seqNo, 1693 uint32_t ssrc) 1694{ 1695 int i; 1696 unsigned char* rtpPointer; 1697 uint16_t offset; 1698 1699 /* first create "standard" RTP header */ 1700 makeRTPheader(rtp_data, NETEQ_CODEC_RED_PT, seqNo, timestamp[numPayloads-1], 1701 ssrc); 1702 1703 rtpPointer = &rtp_data[12]; 1704 1705 /* add one sub-header for each redundant payload (not the primary) */ 1706 for (i = 0; i < numPayloads - 1; i++) { 1707 if (blockLen[i] > 0) { 1708 offset = static_cast<uint16_t>( 1709 timestamp[numPayloads - 1] - timestamp[i]); 1710 1711 // Byte |0| |1 2 | 3 | 1712 // Bit |0|1234567|01234567012345|6701234567| 1713 // |F|payload| timestamp | block | 1714 // | | type | offset | length | 1715 rtpPointer[0] = (payloadType[i] & 0x7F) | 0x80; 1716 rtpPointer[1] = (offset >> 6) & 0xFF; 1717 rtpPointer[2] = 1718 ((offset & 0x3F) << 2) | ((blockLen[i] >> 8) & 0x03); 1719 rtpPointer[3] = blockLen[i] & 0xFF; 1720 1721 rtpPointer += 4; 1722 } 1723 } 1724 1725 // Bit |0|1234567| 1726 // |0|payload| 1727 // | | type | 1728 rtpPointer[0] = payloadType[numPayloads - 1] & 0x7F; 1729 ++rtpPointer; 1730 1731 return rtpPointer - rtp_data; // length of header in bytes 1732} 1733 1734 1735 1736int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration) { 1737 unsigned char E,R,V; 1738 R=0; 1739 V=(unsigned char)Volume; 1740 if (End==0) { 1741 E = 0x00; 1742 } else { 1743 E = 0x80; 1744 } 1745 payload_data[0]=(unsigned char)Event; 1746 payload_data[1]=(unsigned char)(E|R|V); 1747 //Duration equals 8 times time_ms, default is 8000 Hz. 1748 payload_data[2]=(unsigned char)((Duration>>8)&0xFF); 1749 payload_data[3]=(unsigned char)(Duration&0xFF); 1750 return(4); 1751} 1752 1753void stereoDeInterleave(int16_t* audioSamples, int numSamples) 1754{ 1755 1756 int16_t *tempVec; 1757 int16_t *readPtr, *writeL, *writeR; 1758 1759 if (numSamples <= 0) 1760 return; 1761 1762 tempVec = (int16_t *) malloc(sizeof(int16_t) * numSamples); 1763 if (tempVec == NULL) { 1764 printf("Error allocating memory\n"); 1765 exit(0); 1766 } 1767 1768 memcpy(tempVec, audioSamples, numSamples*sizeof(int16_t)); 1769 1770 writeL = audioSamples; 1771 writeR = &audioSamples[numSamples/2]; 1772 readPtr = tempVec; 1773 1774 for (int k = 0; k < numSamples; k += 2) 1775 { 1776 *writeL = *readPtr; 1777 readPtr++; 1778 *writeR = *readPtr; 1779 readPtr++; 1780 writeL++; 1781 writeR++; 1782 } 1783 1784 free(tempVec); 1785 1786} 1787 1788 1789void stereoInterleave(unsigned char* data, int dataLen, int stride) 1790{ 1791 1792 unsigned char *ptrL, *ptrR; 1793 unsigned char temp[10]; 1794 1795 if (stride > 10) 1796 { 1797 exit(0); 1798 } 1799 1800 if (dataLen%1 != 0) 1801 { 1802 // must be even number of samples 1803 printf("Error: cannot interleave odd sample number\n"); 1804 exit(0); 1805 } 1806 1807 ptrL = data + stride; 1808 ptrR = &data[dataLen/2]; 1809 1810 while (ptrL < ptrR) { 1811 // copy from right pointer to temp 1812 memcpy(temp, ptrR, stride); 1813 1814 // shift data between pointers 1815 memmove(ptrL + stride, ptrL, ptrR - ptrL); 1816 1817 // copy from temp to left pointer 1818 memcpy(ptrL, temp, stride); 1819 1820 // advance pointers 1821 ptrL += stride*2; 1822 ptrR += stride; 1823 } 1824 1825} 1826