xref: /haiku/src/bin/media_client/MediaTest.cpp (revision 3995592cdf304335132305e27c40cbb0b1ac46e3)
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 <debug.h>
16 
17 #ifdef DEBUG
18 #define DELAYED_MODE 1
19 #define SNOOZE_FOR 10000000
20 #endif
21 
22 
23 static BSimpleMediaClient* sProducer = NULL;
24 static BSimpleMediaClient* sConsumer = NULL;
25 static BSimpleMediaClient* sFilter = NULL;
26 
27 
28 void
29 _InitClients(bool hasFilter)
30 {
31 	sProducer = new BSimpleMediaClient("MediaClientProducer");
32 	sConsumer = new BSimpleMediaClient("MediaClientConsumer");
33 
34 	if (hasFilter)
35 		sFilter = new BSimpleMediaClient("MediaClientFilter");
36 	else
37 		sFilter = NULL;
38 }
39 
40 
41 void
42 _DeleteClients()
43 {
44 	delete sProducer;
45 	delete sConsumer;
46 	delete sFilter;
47 }
48 
49 
50 media_format
51 _BuildRawAudioFormat()
52 {
53 	media_format format;
54 	format.type = B_MEDIA_RAW_AUDIO;
55 	format.u.raw_audio = media_multi_audio_format::wildcard;
56 
57 	return format;
58 }
59 
60 
61 void
62 _ConsumerProducerTest()
63 {
64 	_InitClients(false);
65 
66 	BSimpleMediaOutput* output = sProducer->BeginOutput();
67 	BSimpleMediaInput* input = sConsumer->BeginInput();
68 
69 	output->SetAcceptedFormat(_BuildRawAudioFormat());
70 	input->SetAcceptedFormat(_BuildRawAudioFormat());
71 
72 	assert(sConsumer->Connect(input, output) == B_OK);
73 
74 	#ifdef DELAYED_MODE
75 	snooze(SNOOZE_FOR);
76 	#endif
77 
78 	assert(sConsumer->Disconnect() == B_OK);
79 
80 	_DeleteClients();
81 }
82 
83 
84 void
85 _ProducerConsumerTest()
86 {
87 	_InitClients(false);
88 
89 	BMediaOutput* output = sProducer->BeginOutput();
90 	BMediaInput* input = sConsumer->BeginInput();
91 
92 	assert(sProducer->Connect(output, input) == B_OK);
93 
94 	#ifdef DELAYED_MODE
95 	snooze(SNOOZE_FOR);
96 	#endif
97 
98 	assert(sProducer->Disconnect() == B_OK);
99 
100 	_DeleteClients();
101 }
102 
103 
104 void
105 _ProducerFilterConsumerTest()
106 {
107 	_InitClients(true);
108 
109 	BMediaOutput* output = sProducer->BeginOutput();
110 	BMediaInput* input = sConsumer->BeginInput();
111 
112 	BMediaInput* filterInput = sFilter->BeginInput();
113 	BMediaOutput* filterOutput = sFilter->BeginOutput();
114 
115 	assert(sFilter->Bind(filterInput, filterOutput) == B_OK);
116 
117 	assert(sProducer->Connect(output, filterInput) == B_OK);
118 	assert(sFilter->Connect(filterOutput, input) == B_OK);
119 	#ifdef DELAYED_MODE
120 	snooze(SNOOZE_FOR);
121 	#endif
122 
123 	assert(sFilter->Disconnect() == B_OK);
124 
125 	_DeleteClients();
126 }
127 
128 
129 void
130 media_test()
131 {
132 	printf("Testing Simple (1:1) Producer-Consumer configuration: ");
133 	_ConsumerProducerTest();
134 	_ProducerConsumerTest();
135 	printf("OK\n");
136 
137 	printf("Testing Simple (1:1:1) Producer-Filter-Consumer configuration: ");
138 	_ProducerFilterConsumerTest();
139 	printf("OK\n");
140 }
141