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
This commit is contained in:
henrik.lundin@webrtc.org 2014-06-19 10:02:11 +00:00
parent 5c3f4e3b0f
commit 496a98463b
4 changed files with 159 additions and 0 deletions

View File

@ -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',

View File

@ -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 <string>
#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_

View File

@ -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_

View File

@ -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 <assert.h>
#include <stdio.h>
#include <string>
#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_