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