From 496a98463b1b10c024c0b773a37d25c902a43890 Mon Sep 17 00:00:00 2001 From: "henrik.lundin@webrtc.org" Date: Thu, 19 Jun 2014 10:02:11 +0000 Subject: [PATCH] Adding test::AudioSink interface and derived classes The AudioSink interface is supposed to be used by tests that produce audio output. Two implementation classes are also provided: OutputAudioFile: Writes the audio to a pcm file. AudioChecksum: Calculates the MD5 checksum of the audio. These will both be used in future changes. R=kwiberg@webrtc.org Review URL: https://webrtc-codereview.appspot.com/17729004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@6490 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/modules/audio_coding/neteq/neteq.gypi | 3 + .../audio_coding/neteq/tools/audio_checksum.h | 60 +++++++++++++++++++ .../audio_coding/neteq/tools/audio_sink.h | 46 ++++++++++++++ .../neteq/tools/output_audio_file.h | 50 ++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 webrtc/modules/audio_coding/neteq/tools/audio_checksum.h create mode 100644 webrtc/modules/audio_coding/neteq/tools/audio_sink.h create mode 100644 webrtc/modules/audio_coding/neteq/tools/output_audio_file.h diff --git a/webrtc/modules/audio_coding/neteq/neteq.gypi b/webrtc/modules/audio_coding/neteq/neteq.gypi index ccdc9f5df7..21ccee41e1 100644 --- a/webrtc/modules/audio_coding/neteq/neteq.gypi +++ b/webrtc/modules/audio_coding/neteq/neteq.gypi @@ -182,10 +182,13 @@ 'tools', ], 'sources': [ + 'tools/audio_checksum.h', 'tools/audio_loop.cc', 'tools/audio_loop.h', + 'tools/audio_sink.h', 'tools/input_audio_file.cc', 'tools/input_audio_file.h', + 'tools/output_audio_file.h', 'tools/packet.cc', 'tools/packet.h', 'tools/packet_source.h', diff --git a/webrtc/modules/audio_coding/neteq/tools/audio_checksum.h b/webrtc/modules/audio_coding/neteq/tools/audio_checksum.h new file mode 100644 index 0000000000..ac5682651b --- /dev/null +++ b/webrtc/modules/audio_coding/neteq/tools/audio_checksum.h @@ -0,0 +1,60 @@ +/* + * 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_AUDIO_CHECKSUM_H_ +#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_ + +#include + +#include "webrtc/base/constructormagic.h" +#include "webrtc/base/md5digest.h" +#include "webrtc/base/stringencode.h" +#include "webrtc/modules/audio_coding/neteq/tools/audio_sink.h" +#include "webrtc/system_wrappers/interface/compile_assert.h" +#include "webrtc/typedefs.h" + +namespace webrtc { +namespace test { + +class AudioChecksum : public AudioSink { + public: + AudioChecksum() : finished_(false) {} + + virtual bool WriteArray(const int16_t* audio, size_t num_samples) OVERRIDE { + if (finished_) + return false; + +#ifndef WEBRTC_ARCH_LITTLE_ENDIAN +#error "Big-endian gives a different checksum" +#endif + checksum_.Update(audio, num_samples * sizeof(*audio)); + return true; + } + + // Finalizes the computations, and returns the checksum. + std::string Finish() { + if (!finished_) { + finished_ = true; + checksum_.Finish(checksum_result_, rtc::Md5Digest::kSize); + } + return rtc::hex_encode(checksum_result_, rtc::Md5Digest::kSize); + } + + private: + rtc::Md5Digest checksum_; + char checksum_result_[rtc::Md5Digest::kSize]; + bool finished_; + + DISALLOW_COPY_AND_ASSIGN(AudioChecksum); +}; + +} // namespace test +} // namespace webrtc +#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_ diff --git a/webrtc/modules/audio_coding/neteq/tools/audio_sink.h b/webrtc/modules/audio_coding/neteq/tools/audio_sink.h new file mode 100644 index 0000000000..6e159e657b --- /dev/null +++ b/webrtc/modules/audio_coding/neteq/tools/audio_sink.h @@ -0,0 +1,46 @@ +/* + * 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_AUDIO_SINK_H_ +#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_SINK_H_ + +#include "webrtc/base/constructormagic.h" +#include "webrtc/modules/interface/module_common_types.h" +#include "webrtc/typedefs.h" + +namespace webrtc { +namespace test { + +// Interface class for an object receiving raw output audio from test +// applications. +class AudioSink { + public: + AudioSink(); + virtual ~AudioSink() {} + + // Writes |num_samples| from |audio| to the AudioSink. Returns true if + // successful, otherwise false. + virtual bool WriteArray(const int16_t* audio, size_t num_samples) = 0; + + // Writes |audio_frame| to the AudioSink. Returns true if successful, + // otherwise false. + bool WriteAudioFrame(const AudioFrame& audio_frame) { + return WriteArray( + audio_frame.data_, + audio_frame.samples_per_channel_ * audio_frame.num_channels_); + } + + private: + DISALLOW_COPY_AND_ASSIGN(AudioSink); +}; + +} // namespace test +} // namespace webrtc +#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_SINK_H_ diff --git a/webrtc/modules/audio_coding/neteq/tools/output_audio_file.h b/webrtc/modules/audio_coding/neteq/tools/output_audio_file.h new file mode 100644 index 0000000000..1d6128076e --- /dev/null +++ b/webrtc/modules/audio_coding/neteq/tools/output_audio_file.h @@ -0,0 +1,50 @@ +/* + * 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_AUDIO_FILE_H_ +#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_OUTPUT_AUDIO_FILE_H_ + +#include +#include +#include + +#include "webrtc/base/constructormagic.h" +#include "webrtc/modules/audio_coding/neteq/tools/audio_sink.h" + +namespace webrtc { +namespace test { + +class OutputAudioFile : public AudioSink { + public: + // Creates an OutputAudioFile, opening a file named |file_name| for writing. + // The file format is 16-bit signed host-endian PCM. + explicit OutputAudioFile(const std::string& file_name) { + out_file_ = fopen(file_name.c_str(), "wb"); + } + + virtual ~OutputAudioFile() { + if (out_file_) + fclose(out_file_); + } + + virtual bool WriteArray(const int16_t* audio, size_t num_samples) OVERRIDE { + assert(out_file_); + return fwrite(audio, sizeof(*audio), num_samples, out_file_) == num_samples; + } + + private: + FILE* out_file_; + + DISALLOW_COPY_AND_ASSIGN(OutputAudioFile); +}; + +} // namespace test +} // namespace webrtc +#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_OUTPUT_AUDIO_FILE_H_