xref: /haiku/src/bin/media_client/MediaTest.cpp (revision 3af8011358bd4c624a0979336d48dabb466171ed)
1 /*
2  * Copyright 2017, Dario Casalinuovo. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "MediaTest.h"
7 
8 #include <SimpleMediaClient.h>
9 #include <MediaConnection.h>
10 #include <SupportDefs.h>
11 
12 #include <assert.h>
13 #include <stdio.h>
14 
15 #include "MediaDebug.h"
16 
17 #ifdef DEBUG
18 #define DELAYED_MODE 1
19 #define SNOOZE_FOR 10000000
20 #endif
21 
22 #define MAX_MULTI_CLIENTS 3
23 
24 static BSimpleMediaClient* sProducer = NULL;
25 static BSimpleMediaClient* sConsumer = NULL;
26 static BSimpleMediaClient* sFilter = NULL;
27 
28 static BSimpleMediaClient* sProducers[MAX_MULTI_CLIENTS];
29 static BSimpleMediaClient* sConsumers[MAX_MULTI_CLIENTS];
30 
31 
32 void
33 _InitClients(bool hasFilter)
34 {
35 	sProducer = new BSimpleMediaClient("MediaClientProducer");
36 	sConsumer = new BSimpleMediaClient("MediaClientConsumer");
37 
38 	if (hasFilter)
39 		sFilter = new BSimpleMediaClient("MediaClientFilter");
40 	else
41 		sFilter = NULL;
42 }
43 
44 
45 void
46 _InitClientsMulti(bool isMixer)
47 {
48 	if (!isMixer) {
49 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
50 			sConsumers[i] = new BSimpleMediaClient("Test Consumer");
51 		}
52 		sProducer = new BSimpleMediaClient("MediaClientProducer");
53 	} else {
54 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
55 			sProducers[i] = new BSimpleMediaClient("Test Producer");
56 		}
57 		sConsumer = new BSimpleMediaClient("MediaClientConsumer");
58 	}
59 
60 	sFilter = new BSimpleMediaClient("MediaClientFilter");
61 }
62 
63 
64 void
65 _DeleteClients()
66 {
67 	delete sProducer;
68 	delete sConsumer;
69 	delete sFilter;
70 }
71 
72 
73 void
74 _DeleteClientsMulti(bool isMixer)
75 {
76 	if (!isMixer) {
77 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
78 			delete sConsumers[i];
79 		}
80 		delete sProducer;
81 	} else {
82 		for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
83 			delete sProducers[i];
84 		}
85 		delete sConsumer;
86 	}
87 	delete sFilter;
88 }
89 
90 
91 media_format
92 _BuildRawAudioFormat()
93 {
94 	media_format format;
95 	format.type = B_MEDIA_RAW_AUDIO;
96 	format.u.raw_audio = media_multi_audio_format::wildcard;
97 
98 	return format;
99 }
100 
101 
102 void
103 _ConsumerProducerTest()
104 {
105 	_InitClients(false);
106 
107 	BSimpleMediaOutput* output = sProducer->BeginOutput();
108 	BSimpleMediaInput* input = sConsumer->BeginInput();
109 
110 	output->SetAcceptedFormat(_BuildRawAudioFormat());
111 	input->SetAcceptedFormat(_BuildRawAudioFormat());
112 
113 	assert(sConsumer->Connect(input, output) == B_OK);
114 
115 	#ifdef DELAYED_MODE
116 	snooze(SNOOZE_FOR);
117 	#endif
118 
119 	assert(input->Disconnect() == B_OK);
120 
121 	_DeleteClients();
122 }
123 
124 
125 void
126 _ProducerConsumerTest()
127 {
128 	_InitClients(false);
129 
130 	BMediaOutput* output = sProducer->BeginOutput();
131 	BMediaInput* input = sConsumer->BeginInput();
132 
133 	assert(sProducer->Connect(output, input) == B_OK);
134 
135 	#ifdef DELAYED_MODE
136 	snooze(SNOOZE_FOR);
137 	#endif
138 
139 	assert(sProducer->Disconnect() == B_OK);
140 
141 	_DeleteClients();
142 }
143 
144 
145 void
146 _ProducerFilterConsumerTest()
147 {
148 	_InitClients(true);
149 
150 	BMediaOutput* output = sProducer->BeginOutput();
151 	BMediaInput* input = sConsumer->BeginInput();
152 
153 	BMediaInput* filterInput = sFilter->BeginInput();
154 	BMediaOutput* filterOutput = sFilter->BeginOutput();
155 
156 	assert(sFilter->Bind(filterInput, filterOutput) == B_OK);
157 
158 	assert(sProducer->Connect(output, filterInput) == B_OK);
159 	assert(sFilter->Connect(filterOutput, input) == B_OK);
160 	#ifdef DELAYED_MODE
161 	snooze(SNOOZE_FOR);
162 	#endif
163 
164 	assert(sFilter->Disconnect() == B_OK);
165 
166 	_DeleteClients();
167 }
168 
169 
170 void
171 _SplitterConfigurationTest()
172 {
173 	_InitClientsMulti(false);
174 
175 	for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
176 		BMediaOutput* output = sFilter->BeginOutput();
177 		assert(sFilter->Connect(output, sConsumers[i]->BeginInput()) == B_OK);
178 	}
179 
180 	assert(sProducer->Connect(sProducer->BeginOutput(),
181 		sFilter->BeginInput()) == B_OK);
182 
183 	#ifdef DELAYED_MODE
184 	snooze(SNOOZE_FOR);
185 	#endif
186 
187 	_DeleteClientsMulti(false);
188 }
189 
190 
191 void
192 _MixerConfigurationTest()
193 {
194 	_InitClientsMulti(true);
195 
196 	for (int i = 0; i < MAX_MULTI_CLIENTS; i++) {
197 		BMediaInput* input = sFilter->BeginInput();
198 		assert(sFilter->Connect(input, sConsumers[i]->BeginInput()) == B_OK);
199 	}
200 
201 	assert(sConsumer->Connect(sConsumer->BeginInput(),
202 		sFilter->BeginOutput()) == B_OK);
203 
204 	#ifdef DELAYED_MODE
205 	snooze(SNOOZE_FOR);
206 	#endif
207 
208 	_DeleteClientsMulti(true);
209 }
210 
211 
212 void
213 media_test()
214 {
215 	printf("Testing Simple (1:1) Producer-Consumer configuration: ");
216 	_ConsumerProducerTest();
217 	_ProducerConsumerTest();
218 	printf("OK\n");
219 
220 	printf("Testing Simple (1:1:1) Producer-Filter-Consumer configuration: ");
221 	_ProducerFilterConsumerTest();
222 	printf("OK\n");
223 
224 	printf("Testing Splitter Configuration (N:1:1): ");
225 	_SplitterConfigurationTest();
226 	printf("OK\n");
227 
228 	printf("Testing Mixer Configuration (N:1:1): ");
229 	_SplitterConfigurationTest();
230 	printf("OK\n");
231 }
232