File: UT_RebufferModule.cpp

package info (click to toggle)
audiofile 0.3.6-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,980 kB
  • sloc: cpp: 36,534; sh: 11,090; ansic: 6,060; makefile: 439
file content (336 lines) | stat: -rw-r--r-- 9,786 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
	Audio File Library
	Copyright (C) 2012, Michael Pruett <michael@68k.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the
	Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
	Boston, MA  02110-1301  USA
*/

#include "config.h"

#include <audiofile.h>
#include <gtest/gtest.h>
#include <limits>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

#include "byteorder.h"
#include "RebufferModule.h"

static void setChunkData(Chunk &chunk, int frameOffset)
{
	int16_t *data = static_cast<int16_t *>(chunk.buffer);
	int channels = chunk.f.channelCount;
	for (unsigned i=0; i<chunk.frameCount; i++)
		for (int c=0; c<channels; c++)
			data[i*channels + c] = (frameOffset + i) * channels + c;
}

static void validateChunkData(const Chunk &chunk, int frameOffset)
{
	const int16_t *data = static_cast<const int16_t *>(chunk.buffer);
	int channels = chunk.f.channelCount;
	for (unsigned i=0; i < chunk.frameCount; i++)
		for (int c=0; c < channels; c++)
			EXPECT_EQ(data[i * channels + c],
				int(frameOffset + i) * channels + c);
}

class TestSourceModule : public Module
{
public:
	TestSourceModule() :
		m_startFrame(0),
		m_frameCount(0),
		m_expectedRequestLength(0)
	{
	}
	unsigned startFrame() const { return m_startFrame; }
	void setStartFrame(unsigned startFrame) { m_startFrame = startFrame; }
	unsigned frameCount() const { return m_frameCount; }
	void setFrameCount(unsigned frameCount) { m_frameCount = frameCount; }
	unsigned expectedRequestLength() const { return m_expectedRequestLength; }
	void setExpectedRequestLength(unsigned expectedRequestLength)
	{
		m_expectedRequestLength = expectedRequestLength;
	}

	void runPull()
	{
		EXPECT_EQ(m_outChunk->frameCount, m_expectedRequestLength);
		unsigned frameCount = std::min<unsigned>(m_outChunk->frameCount, m_frameCount);
		m_outChunk->frameCount = frameCount;
		setChunkData(*m_outChunk, m_startFrame);
		m_startFrame += frameCount;
		m_frameCount -= frameCount;
	}

private:
	unsigned m_startFrame;
	unsigned m_frameCount;
	unsigned m_expectedRequestLength;
};

class TestSinkModule : public Module
{
public:
	TestSinkModule() :
		m_startFrame(0),
		m_expectedRequestLength(0)
	{
	}
	unsigned startFrame() const { return m_startFrame; }
	void setStartFrame(unsigned startFrame) { m_startFrame = startFrame; }
	unsigned expectedRequestLength() const { return m_expectedRequestLength; }
	void setExpectedRequestLength(unsigned expectedRequestLength)
	{
		m_expectedRequestLength = expectedRequestLength;
	}
	void runPush()
	{
		EXPECT_EQ(m_inChunk->frameCount, m_expectedRequestLength);
		validateChunkData(*m_inChunk, m_startFrame);
		m_startFrame += m_inChunk->frameCount;
	}

private:
	unsigned m_startFrame;
	unsigned m_expectedRequestLength;
};

static AudioFormat createAudioFormat(int channels)
{
	AudioFormat f =
	{
		44100,
		AF_SAMPFMT_TWOSCOMP,
		16,
		_AF_BYTEORDER_NATIVE,
		{ 0, 0, 0, 0 },
		channels,
		AF_COMPRESSION_NONE,
		NULL,
		false
	};

	return f;
}

static void testFixedToVariable(bool multiple)
{
	const int channels = 2;
	AudioFormat f = createAudioFormat(channels);

	SharedPtr<RebufferModule> rebuffer =
		new RebufferModule(RebufferModule::FixedToVariable, f.bytesPerFrame(),
			10, multiple);

	SharedPtr<TestSourceModule> source = new TestSourceModule();
	rebuffer->setSource(source.get());

	SharedPtr<Chunk> fixedChunk(new Chunk());
	SharedPtr<Chunk> variableChunk(new Chunk());

	const int maxFrameCount = 50;
	fixedChunk->f = f;
	fixedChunk->allocate(maxFrameCount * f.bytesPerFrame());

	variableChunk->f = f;
	variableChunk->allocate(maxFrameCount * f.bytesPerFrame());

	rebuffer->setInChunk(fixedChunk.get());
	rebuffer->setOutChunk(variableChunk.get());

	source->setOutChunk(fixedChunk.get());

	// Initialize source to contain 100 frames.
	source->setFrameCount(100);

	// Request 22 frames from rebuffer module.
	variableChunk->frameCount = 22;
	source->setExpectedRequestLength(multiple ? 30 : 10);
	rebuffer->runPull();
	// Check that 30 frames have been pulled from source module.
	EXPECT_EQ(30u, source->startFrame());
	// Check that rebuffer module has fulfilled request of 22 frames.
	EXPECT_EQ(22u, variableChunk->frameCount);
	// Validate output data.
	validateChunkData(*variableChunk, 0);

	// Request 30 frames from rebuffer module.
	variableChunk->frameCount = 30;
	source->setExpectedRequestLength(multiple ? 30 : 10);
	rebuffer->runPull();
	// Check that 60 frames have been pulled from source module.
	EXPECT_EQ(60u, source->startFrame());
	// Check that rebuffer module has fulfilled request of 28 frames.
	EXPECT_EQ(30u, variableChunk->frameCount);
	// Validate output data.
	validateChunkData(*variableChunk, 22);

	// Request 50 frames from rebuffer module.
	variableChunk->frameCount = 50;
	source->setExpectedRequestLength(multiple ? 50 : 10);
	rebuffer->runPull();
	// Check that 100 frames have been pulled from source module.
	EXPECT_EQ(100u, source->startFrame());
	// Check that rebuffer module has filled 48 of 50 frames requested.
	EXPECT_EQ(48u, variableChunk->frameCount);
	// Validate output data.
	validateChunkData(*variableChunk, 52);
}

TEST(RebufferModule, FixedToVariable)
{
	testFixedToVariable(false);
}

TEST(RebufferModule, FixedToVariable_Multiple)
{
	testFixedToVariable(true);
}

/*
	Make a request to the rebuffer module which is large enough
	to pull the final short chunk from the test source but which
	doesn't consume all the frames of that chunk.

	Verify that a subsequent request to the rebuffer module correctly
	produces the remaining frames of that chunk.
*/
static void testBufferingAfterShortChunk(bool multiple)
{
	const int channels = 2;
	AudioFormat f = createAudioFormat(channels);

	SharedPtr<RebufferModule> rebuffer =
		new RebufferModule(RebufferModule::FixedToVariable, f.bytesPerFrame(),
			10, multiple);

	SharedPtr<TestSourceModule> source = new TestSourceModule();
	rebuffer->setSource(source.get());

	SharedPtr<Chunk> fixedChunk(new Chunk());
	SharedPtr<Chunk> variableChunk(new Chunk());

	const int maxFrameCount = 30;
	fixedChunk->f = f;
	fixedChunk->allocate(maxFrameCount * f.bytesPerFrame());

	variableChunk->f = f;
	variableChunk->allocate(maxFrameCount * f.bytesPerFrame());

	rebuffer->setInChunk(fixedChunk.get());
	rebuffer->setOutChunk(variableChunk.get());

	source->setOutChunk(fixedChunk.get());

	// Initialize source to contain 23 frames.
	source->setFrameCount(23);

	// Request 21 frames from rebuffer module.
	variableChunk->frameCount = 21;
	source->setExpectedRequestLength(multiple ? 30 : 10);
	rebuffer->runPull();
	// Check that all 23 frames have been pulled from source module.
	EXPECT_EQ(23u, source->startFrame());
	// Check that rebuffer module has fulfilled request of 21 frames.
	EXPECT_EQ(21u, variableChunk->frameCount);
	// Validate output data.
	validateChunkData(*variableChunk, 0);

	// Request 5 frames from rebuffer module.
	variableChunk->frameCount = 5;
	source->setExpectedRequestLength(-1);
	rebuffer->runPull();
	// Check that rebuffer module has delivered remaining 2 frames.
	EXPECT_EQ(2u, variableChunk->frameCount);
	// Validate output data.
	validateChunkData(*variableChunk, 21);
}

TEST(RebufferModule, FixedToVariable_BufferingAfterShortChunk)
{
	testBufferingAfterShortChunk(false);
}

TEST(RebufferModule, FixedToVariable_BufferingAfterShortChunk_Multiple)
{
	testBufferingAfterShortChunk(true);
}

static void testVariableToFixed(bool multiple)
{
	const int channels = 2;
	AudioFormat f = createAudioFormat(channels);

	SharedPtr<RebufferModule> rebuffer =
		new RebufferModule(RebufferModule::VariableToFixed, f.bytesPerFrame(),
			10, multiple);
	SharedPtr<TestSinkModule> sink = new TestSinkModule();
	rebuffer->setSink(sink.get());

	SharedPtr<Chunk> variableChunk(new Chunk());
	SharedPtr<Chunk> fixedChunk(new Chunk());

	const int maxFrameCount = 40;
	variableChunk->f = f;
	variableChunk->allocate(maxFrameCount * f.bytesPerFrame());

	fixedChunk->f = f;
	fixedChunk->allocate(maxFrameCount * f.bytesPerFrame());

	rebuffer->setInChunk(variableChunk.get());
	rebuffer->setOutChunk(fixedChunk.get());

	sink->setInChunk(fixedChunk.get());

	// Push 23 frames to the rebuffer module.
	variableChunk->frameCount = 23;
	setChunkData(*variableChunk, 0);
	sink->setExpectedRequestLength(multiple ? 20 : 10);
	rebuffer->runPush();
	// Check that 20 frames have been pushed to the sink module.
	EXPECT_EQ(sink->startFrame(), 20u);
	// Check that the last push contained 20 (multiple) or 10 (single) frames.
	EXPECT_EQ(fixedChunk->frameCount, multiple ? 20u : 10u);

	// Push another 37 frames to the rebuffer module.
	variableChunk->frameCount = 37;
	setChunkData(*variableChunk, 23);
	sink->setExpectedRequestLength(multiple ? 40 : 10);
	rebuffer->runPush();
	// Check that 60 frames have been pushed to the sink module.
	EXPECT_EQ(sink->startFrame(), 60u);
	// Check that the last push contained 40 (multiple) or 10 (single) frames.
	EXPECT_EQ(fixedChunk->frameCount, multiple ? 40u : 10u);
}

TEST(RebufferModule, VariableToFixed)
{
	testVariableToFixed(false);
}

TEST(RebufferModule, VariableToFixed_Multiple)
{
	testVariableToFixed(true);
}

int main(int argc, char **argv)
{
	::testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}