xref: /haiku/src/bin/media_client/MediaTest.cpp (revision 5731cdeb5f95ed06da2ed9158305f13fc7352fd7)
1dda3a9c3SDario Casalinuovo /*
2dda3a9c3SDario Casalinuovo  * Copyright 2017, Dario Casalinuovo. All rights reserved.
3dda3a9c3SDario Casalinuovo  * Distributed under the terms of the MIT License.
4dda3a9c3SDario Casalinuovo  */
5dda3a9c3SDario Casalinuovo 
6dda3a9c3SDario Casalinuovo #include "MediaTest.h"
7dda3a9c3SDario Casalinuovo 
8dda3a9c3SDario Casalinuovo #include <SimpleMediaClient.h>
9dda3a9c3SDario Casalinuovo #include <MediaConnection.h>
10dda3a9c3SDario Casalinuovo #include <SupportDefs.h>
11dda3a9c3SDario Casalinuovo 
12dda3a9c3SDario Casalinuovo #include <assert.h>
13dda3a9c3SDario Casalinuovo #include <stdio.h>
14dda3a9c3SDario Casalinuovo 
15*5731cdebSAugustin Cavalier #include "MediaDebug.h"
16dda3a9c3SDario Casalinuovo 
17dda3a9c3SDario Casalinuovo #ifdef DEBUG
18dda3a9c3SDario Casalinuovo #define DELAYED_MODE 1
19dda3a9c3SDario Casalinuovo #define SNOOZE_FOR 10000000
20dda3a9c3SDario Casalinuovo #endif
21dda3a9c3SDario Casalinuovo 
2280e9e5f3SBarrett17 #define MAX_MULTI_CLIENTS 3
23dda3a9c3SDario Casalinuovo 
24dda3a9c3SDario Casalinuovo static BSimpleMediaClient* sProducer = NULL;
25dda3a9c3SDario Casalinuovo static BSimpleMediaClient* sConsumer = NULL;
26dda3a9c3SDario Casalinuovo static BSimpleMediaClient* sFilter = NULL;
27dda3a9c3SDario Casalinuovo 
2880e9e5f3SBarrett17 static BSimpleMediaClient* sProducers[MAX_MULTI_CLIENTS];
2980e9e5f3SBarrett17 static BSimpleMediaClient* sConsumers[MAX_MULTI_CLIENTS];
3080e9e5f3SBarrett17 
31dda3a9c3SDario Casalinuovo 
32dda3a9c3SDario Casalinuovo void
_InitClients(bool hasFilter)33dda3a9c3SDario Casalinuovo _InitClients(bool hasFilter)
34dda3a9c3SDario Casalinuovo {
35dda3a9c3SDario Casalinuovo 	sProducer = new BSimpleMediaClient("MediaClientProducer");
36dda3a9c3SDario Casalinuovo 	sConsumer = new BSimpleMediaClient("MediaClientConsumer");
37dda3a9c3SDario Casalinuovo 
38dda3a9c3SDario Casalinuovo 	if (hasFilter)
39dda3a9c3SDario Casalinuovo 		sFilter = new BSimpleMediaClient("MediaClientFilter");
40dda3a9c3SDario Casalinuovo 	else
41dda3a9c3SDario Casalinuovo 		sFilter = NULL;
42dda3a9c3SDario Casalinuovo }
43dda3a9c3SDario Casalinuovo 
44dda3a9c3SDario Casalinuovo 
45dda3a9c3SDario Casalinuovo void
_InitClientsMulti(bool isMixer)4680e9e5f3SBarrett17 _InitClientsMulti(bool isMixer)
4780e9e5f3SBarrett17 {
4880e9e5f3SBarrett17 	if (!isMixer) {
4980e9e5f3SBarrett17 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
5080e9e5f3SBarrett17 			sConsumers[i] = new BSimpleMediaClient("Test Consumer");
5180e9e5f3SBarrett17 		}
5280e9e5f3SBarrett17 		sProducer = new BSimpleMediaClient("MediaClientProducer");
5380e9e5f3SBarrett17 	} else {
5480e9e5f3SBarrett17 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
5580e9e5f3SBarrett17 			sProducers[i] = new BSimpleMediaClient("Test Producer");
5680e9e5f3SBarrett17 		}
5780e9e5f3SBarrett17 		sConsumer = new BSimpleMediaClient("MediaClientConsumer");
5880e9e5f3SBarrett17 	}
5980e9e5f3SBarrett17 
6080e9e5f3SBarrett17 	sFilter = new BSimpleMediaClient("MediaClientFilter");
6180e9e5f3SBarrett17 }
6280e9e5f3SBarrett17 
6380e9e5f3SBarrett17 
6480e9e5f3SBarrett17 void
_DeleteClients()65dda3a9c3SDario Casalinuovo _DeleteClients()
66dda3a9c3SDario Casalinuovo {
67dda3a9c3SDario Casalinuovo 	delete sProducer;
68dda3a9c3SDario Casalinuovo 	delete sConsumer;
69dda3a9c3SDario Casalinuovo 	delete sFilter;
70dda3a9c3SDario Casalinuovo }
71dda3a9c3SDario Casalinuovo 
72dda3a9c3SDario Casalinuovo 
7380e9e5f3SBarrett17 void
_DeleteClientsMulti(bool isMixer)7480e9e5f3SBarrett17 _DeleteClientsMulti(bool isMixer)
7580e9e5f3SBarrett17 {
7680e9e5f3SBarrett17 	if (!isMixer) {
7780e9e5f3SBarrett17 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
7880e9e5f3SBarrett17 			delete sConsumers[i];
7980e9e5f3SBarrett17 		}
8080e9e5f3SBarrett17 		delete sProducer;
8180e9e5f3SBarrett17 	} else {
8280e9e5f3SBarrett17 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
8380e9e5f3SBarrett17 			delete sProducers[i];
8480e9e5f3SBarrett17 		}
8580e9e5f3SBarrett17 		delete sConsumer;
8680e9e5f3SBarrett17 	}
8780e9e5f3SBarrett17 	delete sFilter;
8880e9e5f3SBarrett17 }
8980e9e5f3SBarrett17 
9080e9e5f3SBarrett17 
91dda3a9c3SDario Casalinuovo media_format
_BuildRawAudioFormat()92dda3a9c3SDario Casalinuovo _BuildRawAudioFormat()
93dda3a9c3SDario Casalinuovo {
94dda3a9c3SDario Casalinuovo 	media_format format;
95dda3a9c3SDario Casalinuovo 	format.type = B_MEDIA_RAW_AUDIO;
96dda3a9c3SDario Casalinuovo 	format.u.raw_audio = media_multi_audio_format::wildcard;
97dda3a9c3SDario Casalinuovo 
98dda3a9c3SDario Casalinuovo 	return format;
99dda3a9c3SDario Casalinuovo }
100dda3a9c3SDario Casalinuovo 
101dda3a9c3SDario Casalinuovo 
102dda3a9c3SDario Casalinuovo void
_ConsumerProducerTest()103dda3a9c3SDario Casalinuovo _ConsumerProducerTest()
104dda3a9c3SDario Casalinuovo {
105dda3a9c3SDario Casalinuovo 	_InitClients(false);
106dda3a9c3SDario Casalinuovo 
107dda3a9c3SDario Casalinuovo 	BSimpleMediaOutput* output = sProducer->BeginOutput();
108dda3a9c3SDario Casalinuovo 	BSimpleMediaInput* input = sConsumer->BeginInput();
109dda3a9c3SDario Casalinuovo 
110dda3a9c3SDario Casalinuovo 	output->SetAcceptedFormat(_BuildRawAudioFormat());
111dda3a9c3SDario Casalinuovo 	input->SetAcceptedFormat(_BuildRawAudioFormat());
112dda3a9c3SDario Casalinuovo 
113dda3a9c3SDario Casalinuovo 	assert(sConsumer->Connect(input, output) == B_OK);
114dda3a9c3SDario Casalinuovo 
115dda3a9c3SDario Casalinuovo 	#ifdef DELAYED_MODE
116dda3a9c3SDario Casalinuovo 	snooze(SNOOZE_FOR);
117dda3a9c3SDario Casalinuovo 	#endif
118dda3a9c3SDario Casalinuovo 
1196072c6f1SBarrett17 	assert(input->Disconnect() == B_OK);
120dda3a9c3SDario Casalinuovo 
1212cb3cabcSBarrett17 	_DeleteClients();
122dda3a9c3SDario Casalinuovo }
123dda3a9c3SDario Casalinuovo 
124dda3a9c3SDario Casalinuovo 
125dda3a9c3SDario Casalinuovo void
_ProducerConsumerTest()126dda3a9c3SDario Casalinuovo _ProducerConsumerTest()
127dda3a9c3SDario Casalinuovo {
128dda3a9c3SDario Casalinuovo 	_InitClients(false);
129dda3a9c3SDario Casalinuovo 
130dda3a9c3SDario Casalinuovo 	BMediaOutput* output = sProducer->BeginOutput();
131dda3a9c3SDario Casalinuovo 	BMediaInput* input = sConsumer->BeginInput();
132dda3a9c3SDario Casalinuovo 
133dda3a9c3SDario Casalinuovo 	assert(sProducer->Connect(output, input) == B_OK);
134dda3a9c3SDario Casalinuovo 
135dda3a9c3SDario Casalinuovo 	#ifdef DELAYED_MODE
136dda3a9c3SDario Casalinuovo 	snooze(SNOOZE_FOR);
137dda3a9c3SDario Casalinuovo 	#endif
138dda3a9c3SDario Casalinuovo 
139dda3a9c3SDario Casalinuovo 	assert(sProducer->Disconnect() == B_OK);
140dda3a9c3SDario Casalinuovo 
141dda3a9c3SDario Casalinuovo 	_DeleteClients();
142dda3a9c3SDario Casalinuovo }
143dda3a9c3SDario Casalinuovo 
144dda3a9c3SDario Casalinuovo 
145dda3a9c3SDario Casalinuovo void
_ProducerFilterConsumerTest()146dda3a9c3SDario Casalinuovo _ProducerFilterConsumerTest()
147dda3a9c3SDario Casalinuovo {
148dda3a9c3SDario Casalinuovo 	_InitClients(true);
149dda3a9c3SDario Casalinuovo 
150dda3a9c3SDario Casalinuovo 	BMediaOutput* output = sProducer->BeginOutput();
151dda3a9c3SDario Casalinuovo 	BMediaInput* input = sConsumer->BeginInput();
152dda3a9c3SDario Casalinuovo 
153dda3a9c3SDario Casalinuovo 	BMediaInput* filterInput = sFilter->BeginInput();
154dda3a9c3SDario Casalinuovo 	BMediaOutput* filterOutput = sFilter->BeginOutput();
155dda3a9c3SDario Casalinuovo 
156dda3a9c3SDario Casalinuovo 	assert(sFilter->Bind(filterInput, filterOutput) == B_OK);
157dda3a9c3SDario Casalinuovo 
158dda3a9c3SDario Casalinuovo 	assert(sProducer->Connect(output, filterInput) == B_OK);
159dda3a9c3SDario Casalinuovo 	assert(sFilter->Connect(filterOutput, input) == B_OK);
160dda3a9c3SDario Casalinuovo 	#ifdef DELAYED_MODE
161dda3a9c3SDario Casalinuovo 	snooze(SNOOZE_FOR);
162dda3a9c3SDario Casalinuovo 	#endif
163dda3a9c3SDario Casalinuovo 
164dda3a9c3SDario Casalinuovo 	assert(sFilter->Disconnect() == B_OK);
165dda3a9c3SDario Casalinuovo 
166dda3a9c3SDario Casalinuovo 	_DeleteClients();
167dda3a9c3SDario Casalinuovo }
168dda3a9c3SDario Casalinuovo 
169dda3a9c3SDario Casalinuovo 
170dda3a9c3SDario Casalinuovo void
_SplitterConfigurationTest()17180e9e5f3SBarrett17 _SplitterConfigurationTest()
17280e9e5f3SBarrett17 {
17380e9e5f3SBarrett17 	_InitClientsMulti(false);
17480e9e5f3SBarrett17 
17580e9e5f3SBarrett17 	for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
17680e9e5f3SBarrett17 		BMediaOutput* output = sFilter->BeginOutput();
17780e9e5f3SBarrett17 		assert(sFilter->Connect(output, sConsumers[i]->BeginInput()) == B_OK);
17880e9e5f3SBarrett17 	}
17980e9e5f3SBarrett17 
18080e9e5f3SBarrett17 	assert(sProducer->Connect(sProducer->BeginOutput(),
18180e9e5f3SBarrett17 		sFilter->BeginInput()) == B_OK);
18280e9e5f3SBarrett17 
18380e9e5f3SBarrett17 	#ifdef DELAYED_MODE
18480e9e5f3SBarrett17 	snooze(SNOOZE_FOR);
18580e9e5f3SBarrett17 	#endif
18680e9e5f3SBarrett17 
18780e9e5f3SBarrett17 	_DeleteClientsMulti(false);
18880e9e5f3SBarrett17 }
18980e9e5f3SBarrett17 
19080e9e5f3SBarrett17 
19180e9e5f3SBarrett17 void
_MixerConfigurationTest()19280e9e5f3SBarrett17 _MixerConfigurationTest()
19380e9e5f3SBarrett17 {
19480e9e5f3SBarrett17 	_InitClientsMulti(true);
19580e9e5f3SBarrett17 
19680e9e5f3SBarrett17 	for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
19780e9e5f3SBarrett17 		BMediaInput* input = sFilter->BeginInput();
19880e9e5f3SBarrett17 		assert(sFilter->Connect(input, sConsumers[i]->BeginInput()) == B_OK);
19980e9e5f3SBarrett17 	}
20080e9e5f3SBarrett17 
20180e9e5f3SBarrett17 	assert(sConsumer->Connect(sConsumer->BeginInput(),
20280e9e5f3SBarrett17 		sFilter->BeginOutput()) == B_OK);
20380e9e5f3SBarrett17 
20480e9e5f3SBarrett17 	#ifdef DELAYED_MODE
20580e9e5f3SBarrett17 	snooze(SNOOZE_FOR);
20680e9e5f3SBarrett17 	#endif
20780e9e5f3SBarrett17 
20880e9e5f3SBarrett17 	_DeleteClientsMulti(true);
20980e9e5f3SBarrett17 }
21080e9e5f3SBarrett17 
21180e9e5f3SBarrett17 
21280e9e5f3SBarrett17 void
media_test()213dda3a9c3SDario Casalinuovo media_test()
214dda3a9c3SDario Casalinuovo {
215dda3a9c3SDario Casalinuovo 	printf("Testing Simple (1:1) Producer-Consumer configuration: ");
216dda3a9c3SDario Casalinuovo 	_ConsumerProducerTest();
2172cb3cabcSBarrett17 	_ProducerConsumerTest();
2182cb3cabcSBarrett17 	printf("OK\n");
219dda3a9c3SDario Casalinuovo 
2202cb3cabcSBarrett17 	printf("Testing Simple (1:1:1) Producer-Filter-Consumer configuration: ");
2212cb3cabcSBarrett17 	_ProducerFilterConsumerTest();
222dda3a9c3SDario Casalinuovo 	printf("OK\n");
22380e9e5f3SBarrett17 
22480e9e5f3SBarrett17 	printf("Testing Splitter Configuration (N:1:1): ");
22580e9e5f3SBarrett17 	_SplitterConfigurationTest();
22680e9e5f3SBarrett17 	printf("OK\n");
22780e9e5f3SBarrett17 
22880e9e5f3SBarrett17 	printf("Testing Mixer Configuration (N:1:1): ");
22980e9e5f3SBarrett17 	_SplitterConfigurationTest();
23080e9e5f3SBarrett17 	printf("OK\n");
231dda3a9c3SDario Casalinuovo }
232