Refactoring one of the ACM tests: TestStereo, to follow the style guide.

(First patch: formatting the test file)

TEST=audio_coding_module_test

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2124 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
tina.legrand@webrtc.org 2012-04-26 07:54:30 +00:00
parent 61331137b6
commit a6ecd1ebb5
4 changed files with 1043 additions and 1149 deletions

View File

@ -21,282 +21,212 @@ namespace webrtc {
#define MAX_FILE_NAME_LENGTH_BYTE 500 #define MAX_FILE_NAME_LENGTH_BYTE 500
PCMFile::PCMFile(): PCMFile::PCMFile()
_pcmFile(NULL), : pcm_file_(NULL),
_nSamples10Ms(160), samples_10ms_(160),
_frequency(16000), frequency_(16000),
_endOfFile(false), end_of_file_(false),
_autoRewind(false), auto_rewind_(false),
_rewinded(false), rewinded_(false),
_timestamp(0), read_stereo_(false),
_readStereo(false), save_stereo_(false) {
_saveStereo(false) timestamp_ = (((WebRtc_UWord32)rand() & 0x0000FFFF) << 16) |
{
_timestamp = (((WebRtc_UWord32)rand() & 0x0000FFFF) << 16) |
((WebRtc_UWord32)rand() & 0x0000FFFF); ((WebRtc_UWord32)rand() & 0x0000FFFF);
} }
/* PCMFile::PCMFile(WebRtc_UWord32 timestamp)
PCMFile::~PCMFile() : pcm_file_(NULL),
{ samples_10ms_(160),
if(_pcmFile != NULL) frequency_(16000),
{ end_of_file_(false),
fclose(_pcmFile); auto_rewind_(false),
_pcmFile = NULL; rewinded_(false),
} read_stereo_(false),
save_stereo_(false) {
timestamp_ = timestamp;
} }
*/
WebRtc_Word16 WebRtc_Word16 PCMFile::ChooseFile(char* filename, WebRtc_Word16 max_len) {
PCMFile::ChooseFile( char tmp_name[MAX_FILE_NAME_LENGTH_BYTE];
char* fileName,
WebRtc_Word16 maxLen) EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
{ tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0';
char tmpName[MAX_FILE_NAME_LENGTH_BYTE];
//strcpy(_fileName, "in.pcm");
//printf("\n\nPlease enter the input file: ");
EXPECT_TRUE(fgets(tmpName, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
tmpName[MAX_FILE_NAME_LENGTH_BYTE-1] = '\0';
WebRtc_Word16 n = 0; WebRtc_Word16 n = 0;
// removing leading spaces // Removing leading spaces.
while((isspace(tmpName[n]) || iscntrl(tmpName[n])) && while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (tmp_name[n] != 0)
(tmpName[n] != 0) && && (n < MAX_FILE_NAME_LENGTH_BYTE)) {
(n < MAX_FILE_NAME_LENGTH_BYTE))
{
n++; n++;
} }
if(n > 0) if (n > 0) {
{ memmove(tmp_name, &tmp_name[n], MAX_FILE_NAME_LENGTH_BYTE - n);
memmove(tmpName, &tmpName[n], MAX_FILE_NAME_LENGTH_BYTE - n);
} }
//removing trailing spaces // Removing trailing spaces.
n = (WebRtc_Word16)(strlen(tmpName) - 1); n = (WebRtc_Word16)(strlen(tmp_name) - 1);
if(n >= 0) if (n >= 0) {
{ while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (n >= 0)) {
while((isspace(tmpName[n]) || iscntrl(tmpName[n])) &&
(n >= 0))
{
n--; n--;
} }
} }
if(n >= 0) if (n >= 0) {
{ tmp_name[n + 1] = '\0';
tmpName[n + 1] = '\0';
} }
WebRtc_Word16 len = (WebRtc_Word16)strlen(tmpName); WebRtc_Word16 len = (WebRtc_Word16) strlen(tmp_name);
if(len > maxLen) if (len > max_len) {
{
return -1; return -1;
} }
if(len > 0) if (len > 0) {
{ strncpy(filename, tmp_name, len + 1);
strncpy(fileName, tmpName, len+1);
} }
return 0; return 0;
} }
WebRtc_Word16 WebRtc_Word16 PCMFile::ChooseFile(char* filename, WebRtc_Word16 max_len,
PCMFile::ChooseFile( WebRtc_UWord16* frequency_hz) {
char* fileName, char tmp_name[MAX_FILE_NAME_LENGTH_BYTE];
WebRtc_Word16 maxLen,
WebRtc_UWord16* frequencyHz) EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
{ tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0';
char tmpName[MAX_FILE_NAME_LENGTH_BYTE];
//strcpy(_fileName, "in.pcm");
//printf("\n\nPlease enter the input file: ");
EXPECT_TRUE(fgets(tmpName, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
tmpName[MAX_FILE_NAME_LENGTH_BYTE-1] = '\0';
WebRtc_Word16 n = 0; WebRtc_Word16 n = 0;
// removing leading spaces // Removing trailing spaces.
while((isspace(tmpName[n]) || iscntrl(tmpName[n])) && while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (tmp_name[n] != 0)
(tmpName[n] != 0) && && (n < MAX_FILE_NAME_LENGTH_BYTE)) {
(n < MAX_FILE_NAME_LENGTH_BYTE))
{
n++; n++;
} }
if(n > 0) if (n > 0) {
{ memmove(tmp_name, &tmp_name[n], MAX_FILE_NAME_LENGTH_BYTE - n);
memmove(tmpName, &tmpName[n], MAX_FILE_NAME_LENGTH_BYTE - n);
} }
//removing trailing spaces // Removing trailing spaces.
n = (WebRtc_Word16)(strlen(tmpName) - 1); n = (WebRtc_Word16)(strlen(tmp_name) - 1);
if(n >= 0) if (n >= 0) {
{ while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (n >= 0)) {
while((isspace(tmpName[n]) || iscntrl(tmpName[n])) &&
(n >= 0))
{
n--; n--;
} }
} }
if(n >= 0) if (n >= 0) {
{ tmp_name[n + 1] = '\0';
tmpName[n + 1] = '\0';
} }
WebRtc_Word16 len = (WebRtc_Word16)strlen(tmpName); WebRtc_Word16 len = (WebRtc_Word16) strlen(tmp_name);
if(len > maxLen) if (len > max_len) {
{
return -1; return -1;
} }
if(len > 0) if (len > 0) {
{ strncpy(filename, tmp_name, len + 1);
strncpy(fileName, tmpName, len+1);
} }
printf("Enter the sampling frequency (in Hz) of the above file [%u]: ", *frequencyHz); printf("Enter the sampling frequency (in Hz) of the above file [%u]: ",
EXPECT_TRUE(fgets(tmpName, 10, stdin) != NULL); *frequency_hz);
WebRtc_UWord16 tmpFreq = (WebRtc_UWord16)atoi(tmpName); EXPECT_TRUE(fgets(tmp_name, 10, stdin) != NULL);
if(tmpFreq > 0) WebRtc_UWord16 tmp_frequency = (WebRtc_UWord16) atoi(tmp_name);
{ if (tmp_frequency > 0) {
*frequencyHz = tmpFreq; *frequency_hz = tmp_frequency;
} }
return 0; return 0;
} }
void void PCMFile::Open(const char* filename, WebRtc_UWord16 frequency,
PCMFile::Open( const char* mode, bool auto_rewind) {
const char* filename, if ((pcm_file_ = fopen(filename, mode)) == NULL) {
WebRtc_UWord16 frequency,
const char* mode,
bool autoRewind)
{
if ((_pcmFile = fopen(filename, mode)) == NULL)
{
printf("Cannot open file %s.\n", filename); printf("Cannot open file %s.\n", filename);
ADD_FAILURE() << "Unable to read file"; ADD_FAILURE() << "Unable to read file";
} }
_frequency = frequency; frequency_ = frequency;
_nSamples10Ms = (WebRtc_UWord16)(_frequency / 100); samples_10ms_ = (WebRtc_UWord16)(frequency_ / 100);
_autoRewind = autoRewind; auto_rewind_ = auto_rewind;
_endOfFile = false; end_of_file_ = false;
_rewinded = false; rewinded_ = false;
} }
WebRtc_Word32 WebRtc_Word32 PCMFile::SamplingFrequency() const {
PCMFile::SamplingFrequency() const return frequency_;
{
return _frequency;
} }
WebRtc_UWord16 WebRtc_UWord16 PCMFile::PayloadLength10Ms() const {
PCMFile::PayloadLength10Ms() const return samples_10ms_;
{
return _nSamples10Ms;
} }
WebRtc_Word32 WebRtc_Word32 PCMFile::Read10MsData(AudioFrame& audio_frame) {
PCMFile::Read10MsData( WebRtc_UWord16 channels = 1;
AudioFrame& audioFrame) if (read_stereo_) {
{ channels = 2;
WebRtc_UWord16 noChannels = 1;
if (_readStereo)
{
noChannels = 2;
} }
WebRtc_Word32 payloadSize = (WebRtc_Word32)fread(audioFrame._payloadData, sizeof(WebRtc_UWord16), _nSamples10Ms*noChannels, _pcmFile); WebRtc_Word32 payload_size = (WebRtc_Word32) fread(audio_frame._payloadData,
if (payloadSize < _nSamples10Ms*noChannels) { sizeof(WebRtc_UWord16),
for (int k = payloadSize; k < _nSamples10Ms*noChannels; k++) samples_10ms_ * channels,
{ pcm_file_);
audioFrame._payloadData[k] = 0; if (payload_size < samples_10ms_ * channels) {
for (int k = payload_size; k < samples_10ms_ * channels; k++) {
audio_frame._payloadData[k] = 0;
} }
if(_autoRewind) if (auto_rewind_) {
{ rewind(pcm_file_);
rewind(_pcmFile); rewinded_ = true;
_rewinded = true; } else {
} end_of_file_ = true;
else
{
_endOfFile = true;
} }
} }
audioFrame._payloadDataLengthInSamples = _nSamples10Ms; audio_frame._payloadDataLengthInSamples = samples_10ms_;
audioFrame._frequencyInHz = _frequency; audio_frame._frequencyInHz = frequency_;
audioFrame._audioChannel = noChannels; audio_frame._audioChannel = channels;
audioFrame._timeStamp = _timestamp; audio_frame._timeStamp = timestamp_;
_timestamp += _nSamples10Ms; timestamp_ += samples_10ms_;
return _nSamples10Ms; return samples_10ms_;
} }
void void PCMFile::Write10MsData(AudioFrame& audio_frame) {
PCMFile::Write10MsData( if (audio_frame._audioChannel == 1) {
AudioFrame& audioFrame) if (!save_stereo_) {
{ fwrite(audio_frame._payloadData, sizeof(WebRtc_UWord16),
if(audioFrame._audioChannel == 1) audio_frame._payloadDataLengthInSamples, pcm_file_);
{ } else {
if(!_saveStereo) WebRtc_Word16* stereo_audio =
{ new WebRtc_Word16[2 * audio_frame._payloadDataLengthInSamples];
fwrite(audioFrame._payloadData, sizeof(WebRtc_UWord16),
audioFrame._payloadDataLengthInSamples, _pcmFile);
}
else
{
WebRtc_Word16* stereoAudio = new WebRtc_Word16[2 *
audioFrame._payloadDataLengthInSamples];
int k; int k;
for(k = 0; k < audioFrame._payloadDataLengthInSamples; k++) for (k = 0; k < audio_frame._payloadDataLengthInSamples; k++) {
{ stereo_audio[k << 1] = audio_frame._payloadData[k];
stereoAudio[k<<1] = audioFrame._payloadData[k]; stereo_audio[(k << 1) + 1] = audio_frame._payloadData[k];
stereoAudio[(k<<1) + 1] = audioFrame._payloadData[k];
} }
fwrite(stereoAudio, sizeof(WebRtc_Word16), 2*audioFrame._payloadDataLengthInSamples, fwrite(stereo_audio, sizeof(WebRtc_Word16),
_pcmFile); 2 * audio_frame._payloadDataLengthInSamples, pcm_file_);
delete [] stereoAudio; delete[] stereo_audio;
} }
} } else {
else fwrite(audio_frame._payloadData, sizeof(WebRtc_Word16),
{ audio_frame._audioChannel * audio_frame._payloadDataLengthInSamples,
fwrite(audioFrame._payloadData, sizeof(WebRtc_Word16), pcm_file_);
audioFrame._audioChannel * audioFrame._payloadDataLengthInSamples, _pcmFile);
} }
} }
void PCMFile::Write10MsData(WebRtc_Word16* playout_buffer,
void WebRtc_UWord16 length_smpls) {
PCMFile::Write10MsData( fwrite(playout_buffer, sizeof(WebRtc_UWord16), length_smpls, pcm_file_);
WebRtc_Word16* playoutBuffer,
WebRtc_UWord16 playoutLengthSmpls)
{
fwrite(playoutBuffer, sizeof(WebRtc_UWord16), playoutLengthSmpls, _pcmFile);
} }
void PCMFile::Close() {
void fclose(pcm_file_);
PCMFile::Close() pcm_file_ = NULL;
{
fclose(_pcmFile);
_pcmFile = NULL;
} }
void void PCMFile::Rewind() {
PCMFile::Rewind() rewind(pcm_file_);
{ end_of_file_ = false;
rewind(_pcmFile);
_endOfFile = false;
} }
bool bool PCMFile::Rewinded() {
PCMFile::Rewinded() return rewinded_;
{
return _rewinded;
} }
void void PCMFile::SaveStereo(bool is_stereo) {
PCMFile::SaveStereo( save_stereo_ = is_stereo;
bool saveStereo)
{
_saveStereo = saveStereo;
} }
void void PCMFile::ReadStereo(bool is_stereo) {
PCMFile::ReadStereo( read_stereo_ = is_stereo;
bool readStereo)
{
_readStereo = readStereo;
} }
} // namespace webrtc } // namespace webrtc

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
* *
* Use of this source code is governed by a BSD-style license * 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 * that can be found in the LICENSE file in the root of the source
@ -8,60 +8,60 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#ifndef PCMFILE_H #ifndef WEBRTC_MODULES_AUDIO_CODING_MAIN_TEST_PCMFILE_H_
#define PCMFILE_H #define WEBRTC_MODULES_AUDIO_CODING_MAIN_TEST_PCMFILE_H_
#include "typedefs.h"
#include "module_common_types.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include "module_common_types.h"
#include "typedefs.h"
namespace webrtc { namespace webrtc {
class PCMFile class PCMFile {
{ public:
public:
PCMFile(); PCMFile();
~PCMFile() PCMFile(WebRtc_UWord32 timestamp);
{ ~PCMFile() {
if(_pcmFile != NULL) if (pcm_file_ != NULL) {
{ fclose(pcm_file_);
fclose(_pcmFile);
} }
} }
void Open(const char *filename, WebRtc_UWord16 frequency, const char *mode, void Open(const char *filename, WebRtc_UWord16 frequency, const char *mode,
bool autoRewind = false); bool auto_rewind = false);
WebRtc_Word32 Read10MsData(AudioFrame& audioFrame); WebRtc_Word32 Read10MsData(AudioFrame& audio_frame);
void Write10MsData(WebRtc_Word16 *playoutBuffer, WebRtc_UWord16 playoutLengthSmpls); void Write10MsData(WebRtc_Word16 *playout_buffer,
void Write10MsData(AudioFrame& audioFrame); WebRtc_UWord16 length_smpls);
void Write10MsData(AudioFrame& audio_frame);
WebRtc_UWord16 PayloadLength10Ms() const; WebRtc_UWord16 PayloadLength10Ms() const;
WebRtc_Word32 SamplingFrequency() const; WebRtc_Word32 SamplingFrequency() const;
void Close(); void Close();
bool EndOfFile() const { return _endOfFile; } bool EndOfFile() const {
return end_of_file_;
}
void Rewind(); void Rewind();
static WebRtc_Word16 ChooseFile(char* fileName, WebRtc_Word16 maxLen, static WebRtc_Word16 ChooseFile(char* filename, WebRtc_Word16 max_len,
WebRtc_UWord16* frequencyHz); WebRtc_UWord16* frequency_hz);
static WebRtc_Word16 ChooseFile(char* fileName, WebRtc_Word16 maxLen); static WebRtc_Word16 ChooseFile(char* filename, WebRtc_Word16 max_len);
bool Rewinded(); bool Rewinded();
void SaveStereo( void SaveStereo(bool is_stereo = true);
bool saveStereo = true); void ReadStereo(bool is_stereo = true);
void ReadStereo( private:
bool readStereo = true); FILE* pcm_file_;
private: WebRtc_UWord16 samples_10ms_;
FILE* _pcmFile; WebRtc_Word32 frequency_;
WebRtc_UWord16 _nSamples10Ms; bool end_of_file_;
WebRtc_Word32 _frequency; bool auto_rewind_;
bool _endOfFile; bool rewinded_;
bool _autoRewind; WebRtc_UWord32 timestamp_;
bool _rewinded; bool read_stereo_;
WebRtc_UWord32 _timestamp; bool save_stereo_;
bool _readStereo;
bool _saveStereo;
}; };
} // namespace webrtc } // namespace webrtc
#endif #endif // WEBRTC_MODULES_AUDIO_CODING_MAIN_TEST_PCMFILE_H_

File diff suppressed because it is too large Load Diff

View File

@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#ifndef TEST_STEREO_H #ifndef WEBRTC_MODULES_AUDIO_CODING_MAIN_TEST_TEST_STEREO_H_
#define TEST_STEREO_H #define WEBRTC_MODULES_AUDIO_CODING_MAIN_TEST_TEST_STEREO_H_
#include <math.h> #include <math.h>
@ -25,86 +25,80 @@ enum StereoMonoMode {
kStereo kStereo
}; };
class TestPackStereo : public AudioPacketizationCallback class TestPackStereo : public AudioPacketizationCallback {
{ public:
public:
TestPackStereo(); TestPackStereo();
~TestPackStereo(); ~TestPackStereo();
void RegisterReceiverACM(AudioCodingModule* acm); void RegisterReceiverACM(AudioCodingModule* acm);
virtual WebRtc_Word32 SendData(const FrameType frameType, virtual WebRtc_Word32 SendData(const FrameType frame_type,
const WebRtc_UWord8 payloadType, const WebRtc_UWord8 payload_type,
const WebRtc_UWord32 timeStamp, const WebRtc_UWord32 timestamp,
const WebRtc_UWord8* payloadData, const WebRtc_UWord8* payload_data,
const WebRtc_UWord16 payloadSize, const WebRtc_UWord16 payload_size,
const RTPFragmentationHeader* fragmentation); const RTPFragmentationHeader* fragmentation);
WebRtc_UWord16 GetPayloadSize(); WebRtc_UWord16 payload_size();
WebRtc_UWord32 GetTimeStampDiff(); WebRtc_UWord32 timestamp_diff();
void ResetPayloadSize(); void reset_payload_size();
void set_codec_mode(StereoMonoMode mode); void set_codec_mode(StereoMonoMode mode);
void set_lost_packet(bool lost); void set_lost_packet(bool lost);
private: private:
AudioCodingModule* _receiverACM; AudioCodingModule* receiver_acm_;
WebRtc_Word16 _seqNo; WebRtc_Word16 seq_no_;
WebRtc_UWord8 _payloadData[60 * 32 * 2 * 2]; WebRtc_UWord8 payload_data_[60 * 32 * 2 * 2];
WebRtc_UWord32 _timeStampDiff; WebRtc_UWord32 timestamp_diff_;
WebRtc_UWord32 _lastInTimestamp; WebRtc_UWord32 last_in_timestamp_;
WebRtc_UWord64 _totalBytes; WebRtc_UWord64 total_bytes_;
WebRtc_UWord16 _payloadSize; WebRtc_UWord16 payload_size_;
StereoMonoMode _codec_mode; StereoMonoMode codec_mode_;
// Simulate packet losses // Simulate packet losses
bool _lost_packet; bool lost_packet_;
}; };
class TestStereo : public ACMTest class TestStereo : public ACMTest {
{ public:
public: TestStereo(int test_mode);
TestStereo(int testMode);
~TestStereo(); ~TestStereo();
void Perform(); void Perform();
private: private:
// The default value of '-1' indicates that the registration is based only on codec name // The default value of '-1' indicates that the registration is based only on
// and a sampling frequncy matching is not required. This is useful for codecs which support // codec name and a sampling frequncy matching is not required. This is useful
// several sampling frequency. // for codecs which support several sampling frequency.
WebRtc_Word16 RegisterSendCodec(char side, WebRtc_Word16 RegisterSendCodec(char side, char* codec_name,
char* codecName, WebRtc_Word32 samp_freq_hz, int rate,
WebRtc_Word32 sampFreqHz, int pack_size, int channels,
int rate,
int packSize,
int channels,
int payload_type); int payload_type);
void Run(TestPackStereo* channel, int in_channels, int out_channels, void Run(TestPackStereo* channel, int in_channels, int out_channels,
int percent_loss = 0); int percent_loss = 0);
void OpenOutFile(WebRtc_Word16 testNumber); void OpenOutFile(WebRtc_Word16 test_number);
void DisplaySendReceiveCodec(); void DisplaySendReceiveCodec();
WebRtc_Word32 SendData( WebRtc_Word32 SendData(const FrameType frame_type,
const FrameType frameType, const WebRtc_UWord8 payload_type,
const WebRtc_UWord8 payloadType, const WebRtc_UWord32 timestamp,
const WebRtc_UWord32 timeStamp, const WebRtc_UWord8* payload_data,
const WebRtc_UWord8* payloadData, const WebRtc_UWord16 payload_size,
const WebRtc_UWord16 payloadSize,
const RTPFragmentationHeader* fragmentation); const RTPFragmentationHeader* fragmentation);
int _testMode; int test_mode_;
AudioCodingModule* _acmA; AudioCodingModule* acm_a_;
AudioCodingModule* _acmB; AudioCodingModule* acm_b_;
TestPackStereo* _channelA2B; TestPackStereo* channel_a2b_;
PCMFile _in_file_stereo; PCMFile* in_file_stereo_;
PCMFile _in_file_mono; PCMFile* in_file_mono_;
PCMFile _outFileB; PCMFile out_file_;
WebRtc_Word16 _testCntr; WebRtc_Word16 test_cntr_;
WebRtc_UWord16 _packSizeSamp; WebRtc_UWord16 pack_size_samp_;
WebRtc_UWord16 _packSizeBytes; WebRtc_UWord16 pack_size_bytes_;
int _counter; int counter_;
// Payload types for stereo codecs and CNG // Payload types for stereo codecs and CNG
int g722_pltype_; int g722_pltype_;
@ -121,5 +115,4 @@ private:
} // namespace webrtc } // namespace webrtc
#endif #endif // WEBRTC_MODULES_AUDIO_CODING_MAIN_TEST_TEST_STEREO_H_