OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // TODO(hlundin): The functionality in this file should be moved into one or | 11 // TODO(hlundin): The functionality in this file should be moved into one or |
12 // several classes. | 12 // several classes. |
13 | 13 |
14 #include <assert.h> | 14 #include <assert.h> |
15 #include <errno.h> | 15 #include <errno.h> |
16 #include <limits.h> // For ULONG_MAX returned by strtoul. | 16 #include <limits.h> // For ULONG_MAX returned by strtoul. |
17 #include <stdio.h> | 17 #include <stdio.h> |
18 #include <stdlib.h> // For strtoul. | 18 #include <stdlib.h> // For strtoul. |
19 | 19 |
20 #include <algorithm> | 20 #include <algorithm> |
21 #include <iostream> | 21 #include <iostream> |
22 #include <string> | 22 #include <string> |
23 | 23 |
24 #include "google/gflags.h" | 24 #include "google/gflags.h" |
25 #include "webrtc/base/checks.h" | 25 #include "webrtc/base/checks.h" |
| 26 #include "webrtc/base/safe_conversions.h" |
26 #include "webrtc/base/scoped_ptr.h" | 27 #include "webrtc/base/scoped_ptr.h" |
27 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h" | 28 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h" |
28 #include "webrtc/modules/audio_coding/neteq/interface/neteq.h" | 29 #include "webrtc/modules/audio_coding/neteq/interface/neteq.h" |
29 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h" | 30 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h" |
30 #include "webrtc/modules/audio_coding/neteq/tools/output_audio_file.h" | 31 #include "webrtc/modules/audio_coding/neteq/tools/output_audio_file.h" |
31 #include "webrtc/modules/audio_coding/neteq/tools/output_wav_file.h" | 32 #include "webrtc/modules/audio_coding/neteq/tools/output_wav_file.h" |
32 #include "webrtc/modules/audio_coding/neteq/tools/packet.h" | 33 #include "webrtc/modules/audio_coding/neteq/tools/packet.h" |
33 #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h" | 34 #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h" |
34 #include "webrtc/modules/interface/module_common_types.h" | 35 #include "webrtc/modules/interface/module_common_types.h" |
35 #include "webrtc/system_wrappers/interface/trace.h" | 36 #include "webrtc/system_wrappers/interface/trace.h" |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 assert(*frame_size_samples > 0); | 318 assert(*frame_size_samples > 0); |
318 if (!replacement_audio_file->Read(*frame_size_samples, | 319 if (!replacement_audio_file->Read(*frame_size_samples, |
319 (*replacement_audio).get())) { | 320 (*replacement_audio).get())) { |
320 std::cerr << "Could not read replacement audio file." << std::endl; | 321 std::cerr << "Could not read replacement audio file." << std::endl; |
321 webrtc::Trace::ReturnTrace(); | 322 webrtc::Trace::ReturnTrace(); |
322 exit(1); | 323 exit(1); |
323 } | 324 } |
324 // Encode it as PCM16. | 325 // Encode it as PCM16. |
325 assert((*payload).get()); | 326 assert((*payload).get()); |
326 payload_len = WebRtcPcm16b_Encode((*replacement_audio).get(), | 327 payload_len = WebRtcPcm16b_Encode((*replacement_audio).get(), |
327 static_cast<int16_t>(*frame_size_samples), | 328 *frame_size_samples, |
328 (*payload).get()); | 329 (*payload).get()); |
329 assert(payload_len == 2 * *frame_size_samples); | 330 assert(payload_len == 2 * *frame_size_samples); |
330 // Change payload type to PCM16. | 331 // Change payload type to PCM16. |
331 switch (CodecSampleRate(rtp_header->header.payloadType)) { | 332 switch (CodecSampleRate(rtp_header->header.payloadType)) { |
332 case 8000: | 333 case 8000: |
333 rtp_header->header.payloadType = static_cast<uint8_t>(FLAGS_pcm16b); | 334 rtp_header->header.payloadType = static_cast<uint8_t>(FLAGS_pcm16b); |
334 break; | 335 break; |
335 case 16000: | 336 case 16000: |
336 rtp_header->header.payloadType = static_cast<uint8_t>(FLAGS_pcm16b_wb); | 337 rtp_header->header.payloadType = static_cast<uint8_t>(FLAGS_pcm16b_wb); |
337 break; | 338 break; |
(...skipping 13 matching lines...) Expand all Loading... |
351 exit(1); | 352 exit(1); |
352 } | 353 } |
353 } | 354 } |
354 return payload_len; | 355 return payload_len; |
355 } | 356 } |
356 | 357 |
357 } // namespace | 358 } // namespace |
358 | 359 |
359 int main(int argc, char* argv[]) { | 360 int main(int argc, char* argv[]) { |
360 static const int kMaxChannels = 5; | 361 static const int kMaxChannels = 5; |
361 static const int kMaxSamplesPerMs = 48000 / 1000; | 362 static const size_t kMaxSamplesPerMs = 48000 / 1000; |
362 static const int kOutputBlockSizeMs = 10; | 363 static const int kOutputBlockSizeMs = 10; |
363 | 364 |
364 std::string program_name = argv[0]; | 365 std::string program_name = argv[0]; |
365 std::string usage = "Tool for decoding an RTP dump file using NetEq.\n" | 366 std::string usage = "Tool for decoding an RTP dump file using NetEq.\n" |
366 "Run " + program_name + " --helpshort for usage.\n" | 367 "Run " + program_name + " --helpshort for usage.\n" |
367 "Example usage:\n" + program_name + | 368 "Example usage:\n" + program_name + |
368 " input.rtp output.{pcm, wav}\n"; | 369 " input.rtp output.{pcm, wav}\n"; |
369 google::SetUsageMessage(usage); | 370 google::SetUsageMessage(usage); |
370 google::ParseCommandLineFlags(&argc, &argv, true); | 371 google::ParseCommandLineFlags(&argc, &argv, true); |
371 | 372 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 if (packet_available != next_packet_available) { | 546 if (packet_available != next_packet_available) { |
546 packet_available = !packet_available; | 547 packet_available = !packet_available; |
547 next_packet_available = !next_packet_available; | 548 next_packet_available = !next_packet_available; |
548 } | 549 } |
549 } | 550 } |
550 next_input_time_ms = packet->time_ms(); | 551 next_input_time_ms = packet->time_ms(); |
551 } | 552 } |
552 | 553 |
553 // Check if it is time to get output audio. | 554 // Check if it is time to get output audio. |
554 if (time_now_ms >= next_output_time_ms) { | 555 if (time_now_ms >= next_output_time_ms) { |
555 static const int kOutDataLen = | 556 static const size_t kOutDataLen = |
556 kOutputBlockSizeMs * kMaxSamplesPerMs * kMaxChannels; | 557 kOutputBlockSizeMs * kMaxSamplesPerMs * kMaxChannels; |
557 int16_t out_data[kOutDataLen]; | 558 int16_t out_data[kOutDataLen]; |
558 int num_channels; | 559 int num_channels; |
559 int samples_per_channel; | 560 size_t samples_per_channel; |
560 int error = neteq->GetAudio(kOutDataLen, out_data, &samples_per_channel, | 561 int error = neteq->GetAudio(kOutDataLen, out_data, &samples_per_channel, |
561 &num_channels, NULL); | 562 &num_channels, NULL); |
562 if (error != NetEq::kOK) { | 563 if (error != NetEq::kOK) { |
563 std::cerr << "GetAudio returned error code " << | 564 std::cerr << "GetAudio returned error code " << |
564 neteq->LastError() << std::endl; | 565 neteq->LastError() << std::endl; |
565 } else { | 566 } else { |
566 // Calculate sample rate from output size. | 567 // Calculate sample rate from output size. |
567 sample_rate_hz = 1000 * samples_per_channel / kOutputBlockSizeMs; | 568 sample_rate_hz = rtc::checked_cast<int>( |
| 569 1000 * samples_per_channel / kOutputBlockSizeMs); |
568 } | 570 } |
569 | 571 |
570 // Write to file. | 572 // Write to file. |
571 // TODO(hlundin): Make writing to file optional. | 573 // TODO(hlundin): Make writing to file optional. |
572 size_t write_len = samples_per_channel * num_channels; | 574 size_t write_len = samples_per_channel * num_channels; |
573 if (!output->WriteArray(out_data, write_len)) { | 575 if (!output->WriteArray(out_data, write_len)) { |
574 std::cerr << "Error while writing to file" << std::endl; | 576 std::cerr << "Error while writing to file" << std::endl; |
575 webrtc::Trace::ReturnTrace(); | 577 webrtc::Trace::ReturnTrace(); |
576 exit(1); | 578 exit(1); |
577 } | 579 } |
578 next_output_time_ms += kOutputBlockSizeMs; | 580 next_output_time_ms += kOutputBlockSizeMs; |
579 } | 581 } |
580 // Advance time to next event. | 582 // Advance time to next event. |
581 time_now_ms = std::min(next_input_time_ms, next_output_time_ms); | 583 time_now_ms = std::min(next_input_time_ms, next_output_time_ms); |
582 } | 584 } |
583 | 585 |
584 printf("Simulation done\n"); | 586 printf("Simulation done\n"); |
585 printf("Produced %i ms of audio\n", time_now_ms - start_time_ms); | 587 printf("Produced %i ms of audio\n", time_now_ms - start_time_ms); |
586 | 588 |
587 delete neteq; | 589 delete neteq; |
588 webrtc::Trace::ReturnTrace(); | 590 webrtc::Trace::ReturnTrace(); |
589 return 0; | 591 return 0; |
590 } | 592 } |
OLD | NEW |