Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq4 / tools / neteq_rtpplay.cc
1 /*
2  *  Copyright (c) 2013 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): The functionality in this file should be moved into one or
12 // several classes.
13
14 #include <assert.h>
15 #include <stdio.h>
16
17 #include <algorithm>
18 #include <iostream>
19 #include <string>
20
21 #include "google/gflags.h"
22 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
23 #include "webrtc/modules/audio_coding/neteq4/interface/neteq.h"
24 #include "webrtc/modules/audio_coding/neteq4/test/NETEQTEST_RTPpacket.h"
25 #include "webrtc/modules/audio_coding/neteq4/test/NETEQTEST_DummyRTPpacket.h"
26 #include "webrtc/modules/audio_coding/neteq4/tools/input_audio_file.h"
27 #include "webrtc/modules/interface/module_common_types.h"
28 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
29 #include "webrtc/system_wrappers/interface/trace.h"
30 #include "webrtc/test/testsupport/fileutils.h"
31 #include "webrtc/typedefs.h"
32
33 using webrtc::NetEq;
34 using webrtc::WebRtcRTPHeader;
35
36 // Flag validators.
37 static bool ValidatePayloadType(const char* flagname, int32_t value) {
38   if (value >= 0 && value <= 127)  // Value is ok.
39     return true;
40   printf("Invalid value for --%s: %d\n", flagname, static_cast<int>(value));
41   return false;
42 }
43
44 // Define command line flags.
45 DEFINE_int32(pcmu, 0, "RTP payload type for PCM-u");
46 static const bool pcmu_dummy =
47     google::RegisterFlagValidator(&FLAGS_pcmu, &ValidatePayloadType);
48 DEFINE_int32(pcma, 8, "RTP payload type for PCM-a");
49 static const bool pcma_dummy =
50     google::RegisterFlagValidator(&FLAGS_pcma, &ValidatePayloadType);
51 DEFINE_int32(ilbc, 102, "RTP payload type for iLBC");
52 static const bool ilbc_dummy =
53     google::RegisterFlagValidator(&FLAGS_ilbc, &ValidatePayloadType);
54 DEFINE_int32(isac, 103, "RTP payload type for iSAC");
55 static const bool isac_dummy =
56     google::RegisterFlagValidator(&FLAGS_isac, &ValidatePayloadType);
57 DEFINE_int32(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
58 static const bool isac_swb_dummy =
59     google::RegisterFlagValidator(&FLAGS_isac_swb, &ValidatePayloadType);
60 DEFINE_int32(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
61 static const bool pcm16b_dummy =
62     google::RegisterFlagValidator(&FLAGS_pcm16b, &ValidatePayloadType);
63 DEFINE_int32(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
64 static const bool pcm16b_wb_dummy =
65     google::RegisterFlagValidator(&FLAGS_pcm16b_wb, &ValidatePayloadType);
66 DEFINE_int32(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
67 static const bool pcm16b_swb32_dummy =
68     google::RegisterFlagValidator(&FLAGS_pcm16b_swb32, &ValidatePayloadType);
69 DEFINE_int32(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
70 static const bool pcm16b_swb48_dummy =
71     google::RegisterFlagValidator(&FLAGS_pcm16b_swb48, &ValidatePayloadType);
72 DEFINE_int32(g722, 9, "RTP payload type for G.722");
73 static const bool g722_dummy =
74     google::RegisterFlagValidator(&FLAGS_g722, &ValidatePayloadType);
75 DEFINE_int32(avt, 106, "RTP payload type for AVT/DTMF");
76 static const bool avt_dummy =
77     google::RegisterFlagValidator(&FLAGS_avt, &ValidatePayloadType);
78 DEFINE_int32(red, 117, "RTP payload type for redundant audio (RED)");
79 static const bool red_dummy =
80     google::RegisterFlagValidator(&FLAGS_red, &ValidatePayloadType);
81 DEFINE_int32(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
82 static const bool cn_nb_dummy =
83     google::RegisterFlagValidator(&FLAGS_cn_nb, &ValidatePayloadType);
84 DEFINE_int32(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
85 static const bool cn_wb_dummy =
86     google::RegisterFlagValidator(&FLAGS_cn_wb, &ValidatePayloadType);
87 DEFINE_int32(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
88 static const bool cn_swb32_dummy =
89     google::RegisterFlagValidator(&FLAGS_cn_swb32, &ValidatePayloadType);
90 DEFINE_int32(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
91 static const bool cn_swb48_dummy =
92     google::RegisterFlagValidator(&FLAGS_cn_swb48, &ValidatePayloadType);
93 DEFINE_bool(codec_map, false, "Prints the mapping between RTP payload type and "
94     "codec");
95 DEFINE_bool(dummy_rtp, false, "The input file contains ""dummy"" RTP data, "
96             "i.e., only headers");
97 DEFINE_string(replacement_audio_file, "",
98               "A PCM file that will be used to populate ""dummy"" RTP packets");
99
100 // Declaring helper functions (defined further down in this file).
101 std::string CodecName(webrtc::NetEqDecoder codec);
102 void RegisterPayloadTypes(NetEq* neteq);
103 void PrintCodecMapping();
104 size_t ReplacePayload(webrtc::test::InputAudioFile* replacement_audio_file,
105                       webrtc::scoped_ptr<int16_t[]>* replacement_audio,
106                       webrtc::scoped_ptr<uint8_t[]>* payload,
107                       size_t* payload_mem_size_bytes,
108                       size_t* frame_size_samples,
109                       WebRtcRTPHeader* rtp_header,
110                       NETEQTEST_RTPpacket* next_rtp);
111 int CodecSampleRate(uint8_t payload_type);
112 int CodecTimestampRate(uint8_t payload_type);
113 bool IsComfortNosie(uint8_t payload_type);
114
115 int main(int argc, char* argv[]) {
116   static const int kMaxChannels = 5;
117   static const int kMaxSamplesPerMs = 48000 / 1000;
118   static const int kOutputBlockSizeMs = 10;
119
120   std::string program_name = argv[0];
121   std::string usage = "Tool for decoding an RTP dump file using NetEq.\n"
122       "Run " + program_name + " --helpshort for usage.\n"
123       "Example usage:\n" + program_name +
124       " input.rtp output.pcm\n";
125   google::SetUsageMessage(usage);
126   google::ParseCommandLineFlags(&argc, &argv, true);
127
128   if (FLAGS_codec_map) {
129     PrintCodecMapping();
130   }
131
132   if (argc != 3) {
133     if (FLAGS_codec_map) {
134       // We have already printed the codec map. Just end the program.
135       return 0;
136     }
137     // Print usage information.
138     std::cout << google::ProgramUsage();
139     return 0;
140   }
141
142   FILE* in_file = fopen(argv[1], "rb");
143   if (!in_file) {
144     std::cerr << "Cannot open input file " << argv[1] << std::endl;
145     exit(1);
146   }
147   std::cout << "Input file: " << argv[1] << std::endl;
148
149   FILE* out_file = fopen(argv[2], "wb");
150   if (!in_file) {
151     std::cerr << "Cannot open output file " << argv[2] << std::endl;
152     exit(1);
153   }
154   std::cout << "Output file: " << argv[2] << std::endl;
155
156   // Check if a replacement audio file was provided, and if so, open it.
157   bool replace_payload = false;
158   webrtc::scoped_ptr<webrtc::test::InputAudioFile> replacement_audio_file;
159   if (!FLAGS_replacement_audio_file.empty()) {
160     replacement_audio_file.reset(
161         new webrtc::test::InputAudioFile(FLAGS_replacement_audio_file));
162     replace_payload = true;
163   }
164
165   // Read RTP file header.
166   if (NETEQTEST_RTPpacket::skipFileHeader(in_file) != 0) {
167     std::cerr << "Wrong format in RTP file" << std::endl;
168     exit(1);
169   }
170
171   // Enable tracing.
172   webrtc::Trace::CreateTrace();
173   webrtc::Trace::SetTraceFile((webrtc::test::OutputPath() +
174       "neteq_trace.txt").c_str());
175   webrtc::Trace::set_level_filter(webrtc::kTraceAll);
176
177   // Initialize NetEq instance.
178   int sample_rate_hz = 16000;
179   NetEq* neteq = NetEq::Create(sample_rate_hz);
180   RegisterPayloadTypes(neteq);
181
182   // Read first packet.
183   NETEQTEST_RTPpacket* rtp;
184   NETEQTEST_RTPpacket* next_rtp = NULL;
185   if (!FLAGS_dummy_rtp) {
186     rtp = new NETEQTEST_RTPpacket();
187     if (replace_payload) {
188       next_rtp = new NETEQTEST_RTPpacket();
189     }
190   } else {
191     rtp = new NETEQTEST_DummyRTPpacket();
192     if (replace_payload) {
193       next_rtp = new NETEQTEST_DummyRTPpacket();
194     }
195   }
196   rtp->readFromFile(in_file);
197   if (rtp->dataLen() < 0) {
198     std::cout  << "Warning: RTP file is empty" << std::endl;
199   }
200
201   // Set up variables for audio replacement if needed.
202   size_t input_frame_size_timestamps = 0;
203   webrtc::scoped_ptr<int16_t[]> replacement_audio;
204   webrtc::scoped_ptr<uint8_t[]> payload;
205   size_t payload_mem_size_bytes = 0;
206   if (replace_payload) {
207     // Initially assume that the frame size is 30 ms at the initial sample rate.
208     // This value will be replaced with the correct one as soon as two
209     // consecutive packets are found.
210     input_frame_size_timestamps = 30 * sample_rate_hz / 1000;
211     replacement_audio.reset(new int16_t[input_frame_size_timestamps]);
212     payload_mem_size_bytes = 2 * input_frame_size_timestamps;
213     payload.reset(new uint8_t[payload_mem_size_bytes]);
214     assert(next_rtp);
215     next_rtp->readFromFile(in_file);
216   }
217
218   // This is the main simulation loop.
219   int time_now_ms = rtp->time();  // Start immediately with the first packet.
220   int next_input_time_ms = rtp->time();
221   int next_output_time_ms = time_now_ms;
222   if (time_now_ms % kOutputBlockSizeMs != 0) {
223     // Make sure that next_output_time_ms is rounded up to the next multiple
224     // of kOutputBlockSizeMs. (Legacy bit-exactness.)
225     next_output_time_ms +=
226         kOutputBlockSizeMs - time_now_ms % kOutputBlockSizeMs;
227   }
228   while (rtp->dataLen() >= 0) {
229     // Check if it is time to insert packet.
230     while (time_now_ms >= next_input_time_ms && rtp->dataLen() >= 0) {
231       if (rtp->dataLen() > 0) {
232         // Parse RTP header.
233         WebRtcRTPHeader rtp_header;
234         rtp->parseHeader(&rtp_header);
235         uint8_t* payload_ptr = rtp->payload();
236         size_t payload_len = rtp->payloadLen();
237         if (replace_payload) {
238           payload_len = ReplacePayload(replacement_audio_file.get(),
239                                        &replacement_audio,
240                                        &payload,
241                                        &payload_mem_size_bytes,
242                                        &input_frame_size_timestamps,
243                                        &rtp_header,
244                                        next_rtp);
245           payload_ptr = payload.get();
246         }
247         int error = neteq->InsertPacket(rtp_header, payload_ptr,
248                                         static_cast<int>(payload_len),
249                                         rtp->time() * sample_rate_hz / 1000);
250         if (error != NetEq::kOK) {
251           std::cerr << "InsertPacket returned error code " <<
252               neteq->LastError() << std::endl;
253         }
254       }
255       // Get next packet from file.
256       rtp->readFromFile(in_file);
257       if (replace_payload) {
258         // At this point |rtp| contains the packet *after* |next_rtp|.
259         // Swap RTP packet objects between |rtp| and |next_rtp|.
260         NETEQTEST_RTPpacket* temp_rtp = rtp;
261         rtp = next_rtp;
262         next_rtp = temp_rtp;
263       }
264       next_input_time_ms = rtp->time();
265     }
266
267     // Check if it is time to get output audio.
268     if (time_now_ms >= next_output_time_ms) {
269       static const int kOutDataLen = kOutputBlockSizeMs * kMaxSamplesPerMs *
270           kMaxChannels;
271       int16_t out_data[kOutDataLen];
272       int num_channels;
273       int samples_per_channel;
274       int error = neteq->GetAudio(kOutDataLen, out_data, &samples_per_channel,
275                                    &num_channels, NULL);
276       if (error != NetEq::kOK) {
277         std::cerr << "GetAudio returned error code " <<
278             neteq->LastError() << std::endl;
279       } else {
280         // Calculate sample rate from output size.
281         sample_rate_hz = 1000 * samples_per_channel / kOutputBlockSizeMs;
282       }
283
284       // Write to file.
285       // TODO(hlundin): Make writing to file optional.
286       size_t write_len = samples_per_channel * num_channels;
287       if (fwrite(out_data, sizeof(out_data[0]), write_len, out_file) !=
288           write_len) {
289         std::cerr << "Error while writing to file" << std::endl;
290         webrtc::Trace::ReturnTrace();
291         exit(1);
292       }
293       next_output_time_ms += kOutputBlockSizeMs;
294     }
295     // Advance time to next event.
296     time_now_ms = std::min(next_input_time_ms, next_output_time_ms);
297   }
298
299   std::cout << "Simulation done" << std::endl;
300
301   fclose(in_file);
302   fclose(out_file);
303   delete rtp;
304   delete next_rtp;
305   delete neteq;
306   webrtc::Trace::ReturnTrace();
307   return 0;
308 }
309
310
311 // Help functions.
312
313 // Maps a codec type to a printable name string.
314 std::string CodecName(webrtc::NetEqDecoder codec) {
315   switch (codec) {
316     case webrtc::kDecoderPCMu:
317       return "PCM-u";
318     case webrtc::kDecoderPCMa:
319       return "PCM-a";
320     case webrtc::kDecoderILBC:
321       return "iLBC";
322     case webrtc::kDecoderISAC:
323       return "iSAC";
324     case webrtc::kDecoderISACswb:
325       return "iSAC-swb (32 kHz)";
326     case webrtc::kDecoderPCM16B:
327       return "PCM16b-nb (8 kHz)";
328     case webrtc::kDecoderPCM16Bwb:
329       return "PCM16b-wb (16 kHz)";
330     case webrtc::kDecoderPCM16Bswb32kHz:
331       return "PCM16b-swb32 (32 kHz)";
332     case webrtc::kDecoderPCM16Bswb48kHz:
333       return "PCM16b-swb48 (48 kHz)";
334     case webrtc::kDecoderG722:
335       return "G.722";
336     case webrtc::kDecoderRED:
337       return "redundant audio (RED)";
338     case webrtc::kDecoderAVT:
339       return "AVT/DTMF";
340     case webrtc::kDecoderCNGnb:
341       return "comfort noise (8 kHz)";
342     case webrtc::kDecoderCNGwb:
343       return "comfort noise (16 kHz)";
344     case webrtc::kDecoderCNGswb32kHz:
345       return "comfort noise (32 kHz)";
346     case webrtc::kDecoderCNGswb48kHz:
347       return "comfort noise (48 kHz)";
348     default:
349       assert(false);
350       return "undefined";
351   }
352 }
353
354 // Registers all decoders in |neteq|.
355 void RegisterPayloadTypes(NetEq* neteq) {
356   assert(neteq);
357   int error;
358   error = neteq->RegisterPayloadType(webrtc::kDecoderPCMu, FLAGS_pcmu);
359   if (error) {
360     std::cerr << "Cannot register payload type " << FLAGS_pcmu <<
361         " as " << CodecName(webrtc::kDecoderPCMu).c_str() << std::endl;
362     exit(1);
363   }
364   error = neteq->RegisterPayloadType(webrtc::kDecoderPCMa, FLAGS_pcma);
365   if (error) {
366     std::cerr << "Cannot register payload type " << FLAGS_pcma <<
367         " as " << CodecName(webrtc::kDecoderPCMa).c_str() << std::endl;
368     exit(1);
369   }
370   error = neteq->RegisterPayloadType(webrtc::kDecoderILBC, FLAGS_ilbc);
371   if (error) {
372     std::cerr << "Cannot register payload type " << FLAGS_ilbc <<
373         " as " << CodecName(webrtc::kDecoderILBC).c_str() << std::endl;
374     exit(1);
375   }
376   error = neteq->RegisterPayloadType(webrtc::kDecoderISAC, FLAGS_isac);
377   if (error) {
378     std::cerr << "Cannot register payload type " << FLAGS_isac <<
379         " as " << CodecName(webrtc::kDecoderISAC).c_str() << std::endl;
380     exit(1);
381   }
382   error = neteq->RegisterPayloadType(webrtc::kDecoderISACswb, FLAGS_isac_swb);
383   if (error) {
384     std::cerr << "Cannot register payload type " << FLAGS_isac_swb <<
385         " as " << CodecName(webrtc::kDecoderISACswb).c_str() << std::endl;
386     exit(1);
387   }
388   error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16B, FLAGS_pcm16b);
389   if (error) {
390     std::cerr << "Cannot register payload type " << FLAGS_pcm16b <<
391         " as " << CodecName(webrtc::kDecoderPCM16B).c_str() << std::endl;
392     exit(1);
393   }
394   error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16Bwb,
395                                       FLAGS_pcm16b_wb);
396   if (error) {
397     std::cerr << "Cannot register payload type " << FLAGS_pcm16b_wb <<
398         " as " << CodecName(webrtc::kDecoderPCM16Bwb).c_str() << std::endl;
399     exit(1);
400   }
401   error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16Bswb32kHz,
402                                       FLAGS_pcm16b_swb32);
403   if (error) {
404     std::cerr << "Cannot register payload type " << FLAGS_pcm16b_swb32 <<
405         " as " << CodecName(webrtc::kDecoderPCM16Bswb32kHz).c_str() <<
406         std::endl;
407     exit(1);
408   }
409   error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16Bswb48kHz,
410                                       FLAGS_pcm16b_swb48);
411   if (error) {
412     std::cerr << "Cannot register payload type " << FLAGS_pcm16b_swb48 <<
413         " as " << CodecName(webrtc::kDecoderPCM16Bswb48kHz).c_str() <<
414         std::endl;
415     exit(1);
416   }
417   error = neteq->RegisterPayloadType(webrtc::kDecoderG722, FLAGS_g722);
418   if (error) {
419     std::cerr << "Cannot register payload type " << FLAGS_g722 <<
420         " as " << CodecName(webrtc::kDecoderG722).c_str() << std::endl;
421     exit(1);
422   }
423   error = neteq->RegisterPayloadType(webrtc::kDecoderAVT, FLAGS_avt);
424   if (error) {
425     std::cerr << "Cannot register payload type " << FLAGS_avt <<
426         " as " << CodecName(webrtc::kDecoderAVT).c_str() << std::endl;
427     exit(1);
428   }
429   error = neteq->RegisterPayloadType(webrtc::kDecoderRED, FLAGS_red);
430   if (error) {
431     std::cerr << "Cannot register payload type " << FLAGS_red <<
432         " as " << CodecName(webrtc::kDecoderRED).c_str() << std::endl;
433     exit(1);
434   }
435   error = neteq->RegisterPayloadType(webrtc::kDecoderCNGnb, FLAGS_cn_nb);
436   if (error) {
437     std::cerr << "Cannot register payload type " << FLAGS_cn_nb <<
438         " as " << CodecName(webrtc::kDecoderCNGnb).c_str() << std::endl;
439     exit(1);
440   }
441   error = neteq->RegisterPayloadType(webrtc::kDecoderCNGwb, FLAGS_cn_wb);
442   if (error) {
443     std::cerr << "Cannot register payload type " << FLAGS_cn_wb <<
444         " as " << CodecName(webrtc::kDecoderCNGwb).c_str() << std::endl;
445     exit(1);
446   }
447   error = neteq->RegisterPayloadType(webrtc::kDecoderCNGswb32kHz,
448                                       FLAGS_cn_swb32);
449   if (error) {
450     std::cerr << "Cannot register payload type " << FLAGS_cn_swb32 <<
451         " as " << CodecName(webrtc::kDecoderCNGswb32kHz).c_str() << std::endl;
452     exit(1);
453   }
454   error = neteq->RegisterPayloadType(webrtc::kDecoderCNGswb48kHz,
455                                      FLAGS_cn_swb48);
456   if (error) {
457     std::cerr << "Cannot register payload type " << FLAGS_cn_swb48 <<
458         " as " << CodecName(webrtc::kDecoderCNGswb48kHz).c_str() << std::endl;
459     exit(1);
460   }
461 }
462
463 void PrintCodecMapping() {
464   std::cout << CodecName(webrtc::kDecoderPCMu).c_str() << ": " << FLAGS_pcmu <<
465       std::endl;
466   std::cout << CodecName(webrtc::kDecoderPCMa).c_str() << ": " << FLAGS_pcma <<
467       std::endl;
468   std::cout << CodecName(webrtc::kDecoderILBC).c_str() << ": " << FLAGS_ilbc <<
469       std::endl;
470   std::cout << CodecName(webrtc::kDecoderISAC).c_str() << ": " << FLAGS_isac <<
471       std::endl;
472   std::cout << CodecName(webrtc::kDecoderISACswb).c_str() << ": " <<
473       FLAGS_isac_swb << std::endl;
474   std::cout << CodecName(webrtc::kDecoderPCM16B).c_str() << ": " <<
475       FLAGS_pcm16b << std::endl;
476   std::cout << CodecName(webrtc::kDecoderPCM16Bwb).c_str() << ": " <<
477       FLAGS_pcm16b_wb << std::endl;
478   std::cout << CodecName(webrtc::kDecoderPCM16Bswb32kHz).c_str() << ": " <<
479       FLAGS_pcm16b_swb32 << std::endl;
480   std::cout << CodecName(webrtc::kDecoderPCM16Bswb48kHz).c_str() << ": " <<
481       FLAGS_pcm16b_swb48 << std::endl;
482   std::cout << CodecName(webrtc::kDecoderG722).c_str() << ": " << FLAGS_g722 <<
483       std::endl;
484   std::cout << CodecName(webrtc::kDecoderAVT).c_str() << ": " << FLAGS_avt <<
485       std::endl;
486   std::cout << CodecName(webrtc::kDecoderRED).c_str() << ": " << FLAGS_red <<
487       std::endl;
488   std::cout << CodecName(webrtc::kDecoderCNGnb).c_str() << ": " <<
489       FLAGS_cn_nb << std::endl;
490   std::cout << CodecName(webrtc::kDecoderCNGwb).c_str() << ": " <<
491       FLAGS_cn_wb << std::endl;
492   std::cout << CodecName(webrtc::kDecoderCNGswb32kHz).c_str() << ": " <<
493       FLAGS_cn_swb32 << std::endl;
494   std::cout << CodecName(webrtc::kDecoderCNGswb48kHz).c_str() << ": " <<
495       FLAGS_cn_swb48 << std::endl;
496 }
497
498 size_t ReplacePayload(webrtc::test::InputAudioFile* replacement_audio_file,
499                       webrtc::scoped_ptr<int16_t[]>* replacement_audio,
500                       webrtc::scoped_ptr<uint8_t[]>* payload,
501                       size_t* payload_mem_size_bytes,
502                       size_t* frame_size_samples,
503                       WebRtcRTPHeader* rtp_header,
504                       NETEQTEST_RTPpacket* next_rtp) {
505   size_t payload_len = 0;
506   // Check for CNG.
507   if (IsComfortNosie(rtp_header->header.payloadType)) {
508     // If CNG, simply insert a zero-energy one-byte payload.
509     if (*payload_mem_size_bytes < 1) {
510       (*payload).reset(new uint8_t[1]);
511       *payload_mem_size_bytes = 1;
512     }
513     (*payload)[0] = 127;  // Max attenuation of CNG.
514     payload_len = 1;
515   } else {
516     if (next_rtp->payloadLen() > 0) {
517       // Check if payload length has changed.
518       if (next_rtp->sequenceNumber() == rtp_header->header.sequenceNumber + 1) {
519         if (*frame_size_samples !=
520             next_rtp->timeStamp() - rtp_header->header.timestamp) {
521           *frame_size_samples =
522               next_rtp->timeStamp() - rtp_header->header.timestamp;
523           (*replacement_audio).reset(
524               new int16_t[*frame_size_samples]);
525           *payload_mem_size_bytes = 2 * *frame_size_samples;
526           (*payload).reset(new uint8_t[*payload_mem_size_bytes]);
527         }
528       }
529     }
530     // Get new speech.
531     assert((*replacement_audio).get());
532     if (CodecTimestampRate(rtp_header->header.payloadType) !=
533         CodecSampleRate(rtp_header->header.payloadType) ||
534         rtp_header->header.payloadType == FLAGS_red ||
535         rtp_header->header.payloadType == FLAGS_avt) {
536       // Some codecs have different sample and timestamp rates. And neither
537       // RED nor DTMF is supported for replacement.
538       std::cerr << "Codec not supported for audio replacement." <<
539           std::endl;
540       webrtc::Trace::ReturnTrace();
541       exit(1);
542     }
543     assert(*frame_size_samples > 0);
544     if (!replacement_audio_file->Read(*frame_size_samples,
545                                       (*replacement_audio).get())) {
546       std::cerr << "Could no read replacement audio file." << std::endl;
547       webrtc::Trace::ReturnTrace();
548       exit(1);
549     }
550     // Encode it as PCM16.
551     assert((*payload).get());
552     payload_len = WebRtcPcm16b_Encode((*replacement_audio).get(),
553                                       static_cast<int16_t>(*frame_size_samples),
554                                       (*payload).get());
555     assert(payload_len == 2 * *frame_size_samples);
556     // Change payload type to PCM16.
557     switch (CodecSampleRate(rtp_header->header.payloadType)) {
558       case 8000:
559         rtp_header->header.payloadType = FLAGS_pcm16b;
560         break;
561       case 16000:
562         rtp_header->header.payloadType = FLAGS_pcm16b_wb;
563         break;
564       case 32000:
565         rtp_header->header.payloadType = FLAGS_pcm16b_swb32;
566         break;
567       case 48000:
568         rtp_header->header.payloadType = FLAGS_pcm16b_swb48;
569         break;
570       default:
571         std::cerr << "Payload type " <<
572             static_cast<int>(rtp_header->header.payloadType) <<
573             " not supported or unknown." << std::endl;
574         webrtc::Trace::ReturnTrace();
575         exit(1);
576         assert(false);
577     }
578   }
579   return payload_len;
580 }
581
582 int CodecSampleRate(uint8_t payload_type) {
583   if (payload_type == FLAGS_pcmu ||
584       payload_type == FLAGS_pcma ||
585       payload_type == FLAGS_ilbc ||
586       payload_type == FLAGS_pcm16b ||
587       payload_type == FLAGS_cn_nb) {
588     return 8000;
589   } else if (payload_type == FLAGS_isac ||
590       payload_type == FLAGS_pcm16b_wb ||
591       payload_type == FLAGS_g722 ||
592       payload_type == FLAGS_cn_wb) {
593     return 16000;
594   } else if (payload_type == FLAGS_isac_swb ||
595       payload_type == FLAGS_pcm16b_swb32 ||
596       payload_type == FLAGS_cn_swb32) {
597     return 32000;
598   } else if (payload_type == FLAGS_pcm16b_swb48 ||
599       payload_type == FLAGS_cn_swb48) {
600     return 48000;
601   } else if (payload_type == FLAGS_avt ||
602       payload_type == FLAGS_red) {
603       return 0;
604   } else {
605     return -1;
606   }
607 }
608
609 int CodecTimestampRate(uint8_t payload_type) {
610   if (payload_type == FLAGS_g722) {
611     return 8000;
612   } else {
613     return CodecSampleRate(payload_type);
614   }
615 }
616
617 bool IsComfortNosie(uint8_t payload_type) {
618   if (payload_type == FLAGS_cn_nb ||
619       payload_type == FLAGS_cn_wb ||
620       payload_type == FLAGS_cn_swb32 ||
621       payload_type == FLAGS_cn_swb48) {
622     return true;
623   } else {
624     return false;
625   }
626 }