Add wav output capability to neteq_rtpplay

This CL makes neteq_rtpplay capable of writing to wav files as well as
pcm files. This is done through the new class OutputWavFile, which
wraps a WavWriter object in an AudioSink interface.

BUG=2692
R=kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/32139004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7740 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrik.lundin@webrtc.org 2014-11-24 14:50:53 +00:00
parent aff1751c96
commit 03499a0e95
3 changed files with 87 additions and 24 deletions

View File

@ -171,6 +171,7 @@
'type': 'static_library',
'dependencies': [
'rtp_rtcp',
'<(webrtc_root)/common_audio/common_audio.gyp:common_audio',
'<(webrtc_root)/test/test.gyp:rtp_test_utils',
],
'direct_dependent_settings': {
@ -191,6 +192,7 @@
'tools/input_audio_file.cc',
'tools/input_audio_file.h',
'tools/output_audio_file.h',
'tools/output_wav_file.h',
'tools/packet.cc',
'tools/packet.h',
'tools/packet_source.h',

View File

@ -26,6 +26,8 @@
#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
#include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
#include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
#include "webrtc/modules/audio_coding/neteq/tools/output_audio_file.h"
#include "webrtc/modules/audio_coding/neteq/tools/output_wav_file.h"
#include "webrtc/modules/audio_coding/neteq/tools/packet.h"
#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
#include "webrtc/modules/interface/module_common_types.h"
@ -163,7 +165,7 @@ int main(int argc, char* argv[]) {
std::string usage = "Tool for decoding an RTP dump file using NetEq.\n"
"Run " + program_name + " --helpshort for usage.\n"
"Example usage:\n" + program_name +
" input.rtp output.pcm\n";
" input.rtp output.{pcm, wav}\n";
google::SetUsageMessage(usage);
google::ParseCommandLineFlags(&argc, &argv, true);
@ -193,13 +195,6 @@ int main(int argc, char* argv[]) {
file_source->SelectSsrc(ssrc);
}
FILE* out_file = fopen(argv[2], "wb");
if (!out_file) {
std::cerr << "Cannot open output file " << argv[2] << std::endl;
exit(1);
}
std::cout << "Output file: " << argv[2] << std::endl;
// Check if a replacement audio file was provided, and if so, open it.
bool replace_payload = false;
webrtc::scoped_ptr<webrtc::test::InputAudioFile> replacement_audio_file;
@ -209,19 +204,6 @@ int main(int argc, char* argv[]) {
replace_payload = true;
}
// Enable tracing.
webrtc::Trace::CreateTrace();
webrtc::Trace::SetTraceFile((webrtc::test::OutputPath() +
"neteq_trace.txt").c_str());
webrtc::Trace::set_level_filter(webrtc::kTraceAll);
// Initialize NetEq instance.
int sample_rate_hz = 16000;
NetEq::Config config;
config.sample_rate_hz = sample_rate_hz;
NetEq* neteq = NetEq::Create(config);
RegisterPayloadTypes(neteq);
// Read first packet.
webrtc::scoped_ptr<webrtc::test::Packet> packet(file_source->NextPacket());
if (!packet) {
@ -233,6 +215,44 @@ int main(int argc, char* argv[]) {
}
bool packet_available = true;
// Check the sample rate.
int sample_rate_hz = CodecSampleRate(packet->header().payloadType);
if (sample_rate_hz <= 0) {
printf("Warning: Invalid sample rate from RTP packet.\n");
webrtc::Trace::ReturnTrace();
return 0;
}
// Open the output file now that we know the sample rate. (Rate is only needed
// for wav files.)
// Check output file type.
std::string output_file_name = argv[2];
webrtc::scoped_ptr<webrtc::test::AudioSink> output;
if (output_file_name.size() >= 4 &&
output_file_name.substr(output_file_name.size() - 4) == ".wav") {
// Open a wav file.
output.reset(
new webrtc::test::OutputWavFile(output_file_name, sample_rate_hz));
} else {
// Open a pcm file.
output.reset(new webrtc::test::OutputAudioFile(output_file_name));
}
std::cout << "Output file: " << argv[2] << std::endl;
// Enable tracing.
webrtc::Trace::CreateTrace();
webrtc::Trace::SetTraceFile((webrtc::test::OutputPath() +
"neteq_trace.txt").c_str());
webrtc::Trace::set_level_filter(webrtc::kTraceAll);
// Initialize NetEq instance.
NetEq::Config config;
config.sample_rate_hz = sample_rate_hz;
NetEq* neteq = NetEq::Create(config);
RegisterPayloadTypes(neteq);
// Set up variables for audio replacement if needed.
webrtc::scoped_ptr<webrtc::test::Packet> next_packet;
bool next_packet_available = false;
@ -349,8 +369,7 @@ int main(int argc, char* argv[]) {
// Write to file.
// TODO(hlundin): Make writing to file optional.
size_t write_len = samples_per_channel * num_channels;
if (fwrite(out_data, sizeof(out_data[0]), write_len, out_file) !=
write_len) {
if (!output->WriteArray(out_data, write_len)) {
std::cerr << "Error while writing to file" << std::endl;
webrtc::Trace::ReturnTrace();
exit(1);
@ -363,7 +382,6 @@ int main(int argc, char* argv[]) {
std::cout << "Simulation done" << std::endl;
fclose(out_file);
delete neteq;
webrtc::Trace::ReturnTrace();
return 0;

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_OUTPUT_WAV_FILE_H_
#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_OUTPUT_WAV_FILE_H_
#include <string>
#include "webrtc/base/constructormagic.h"
#include "webrtc/common_audio/wav_file.h"
#include "webrtc/modules/audio_coding/neteq/tools/audio_sink.h"
namespace webrtc {
namespace test {
class OutputWavFile : public AudioSink {
public:
// Creates an OutputWavFile, opening a file named |file_name| for writing.
// The output file is a PCM encoded wav file.
OutputWavFile(const std::string& file_name, int sample_rate_hz)
: wav_writer_(file_name, sample_rate_hz, 1) {}
virtual bool WriteArray(const int16_t* audio, size_t num_samples) OVERRIDE {
wav_writer_.WriteSamples(audio, num_samples);
return true;
}
private:
WavWriter wav_writer_;
DISALLOW_COPY_AND_ASSIGN(OutputWavFile);
};
} // namespace test
} // namespace webrtc
#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_OUTPUT_WAV_FILE_H_