xref: /haiku/src/tests/kits/media/nodetest/main.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 #include <Application.h>
2 #include <MediaDefs.h>
3 #include <MediaRoster.h>
4 #include <TimeSource.h>
5 
6 #include <string.h>
7 
8 #include "ConsumerNode.h"
9 #include "misc.h"
10 #include "ProducerNode.h"
11 
12 
13 BMediaRoster *roster;
14 ProducerNode *producer;
15 ConsumerNode *consumer;
16 status_t rv;
17 
18 int main()
19 {
20 	out("Basic BBufferProducer, BBufferConsumer, BMediaRoster test\n");
21 	out("for Haiku by Marcus Overhagen <Marcus@Overhagen.de>\n\n");
22 	out("Creating BApplication now\n");
23 	BApplication app("application/x-vnd.OpenBeOS-NodeTest");
24 
25 	out("Creating MediaRoster\n");
26 	roster = BMediaRoster::Roster();
27 	val(roster);
28 
29 	out("Creating ProducerNode\n");
30 	producer = new ProducerNode();
31 	val(producer);
32 
33 	out("Creating ConsumerNode\n");
34 	consumer = new ConsumerNode();
35 	val(consumer);
36 
37 	out("Registering ProducerNode\n");
38 	rv = roster->RegisterNode(producer);
39 	val(rv);
40 
41 	out("Registering ConsumerNode\n");
42 	rv = roster->RegisterNode(consumer);
43 	val(rv);
44 
45 	media_node sourceNode;
46 	media_node destinationNode;
47 
48 	out("Calling producer->Node()\n");
49 	sourceNode = producer->Node();
50 
51 	out("Calling consumer->Node()\n");
52 	destinationNode = consumer->Node();
53 
54 
55 	live_node_info live_nodes[100];
56 	int32 live_count;
57 	media_format liveformat;
58 	memset(&liveformat, 0, sizeof(liveformat));
59 
60 	liveformat.type = B_MEDIA_RAW_AUDIO;
61 
62 	out("Calling GetLiveNodes(), has_input = B_MEDIA_RAW_AUDIO\n");
63 	live_count = 100;
64 	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat);
65 	val(rv);
66 	out("Found %ld\n",live_count);
67 
68 	out("Calling GetLiveNodes(), has_output = B_MEDIA_RAW_AUDIO\n");
69 	live_count = 100;
70 	rv = roster->GetLiveNodes(live_nodes, &live_count, NULL, &liveformat);
71 	val(rv);
72 	out("Found %ld\n",live_count);
73 
74 	out("Calling GetLiveNodes(), has_input = has_output = B_MEDIA_RAW_AUDIO\n");
75 	live_count = 100;
76 	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat, &liveformat);
77 	val(rv);
78 	out("Found %ld\n",live_count);
79 
80 	liveformat.type = B_MEDIA_RAW_VIDEO;
81 
82 	out("Calling GetLiveNodes(), has_input = B_MEDIA_RAW_VIDEO\n");
83 	live_count = 100;
84 	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat);
85 	val(rv);
86 	out("Found %ld\n",live_count);
87 
88 	out("Calling GetLiveNodes(), has_output = B_MEDIA_RAW_VIDEO\n");
89 	live_count = 100;
90 	rv = roster->GetLiveNodes(live_nodes, &live_count, NULL, &liveformat);
91 	val(rv);
92 	out("Found %ld\n",live_count);
93 
94 	out("Calling GetLiveNodes(), has_input = has_output = B_MEDIA_RAW_VIDEO\n");
95 	live_count = 100;
96 	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat, &liveformat);
97 	val(rv);
98 	out("Found %ld\n",live_count);
99 
100 
101 	media_output output;
102 	media_input input;
103 	media_output outputs[2];
104 	media_input inputs[2];
105 	int32 count;
106 
107 	out("Calling GetAllOutputsFor(source)\n");
108 	rv = roster->GetAllOutputsFor(sourceNode,outputs,2,&count);
109 	val(rv);
110 	out("Found %ld\n",count);
111 	rv = (count == 1) ? B_OK : B_ERROR;
112 	val(rv);
113 
114 	out("Calling GetAllInputsFor(destination)\n");
115 	rv = roster->GetAllInputsFor(destinationNode,inputs,2,&count);
116 	val(rv);
117 	out("Found %ld\n",count);
118 	rv = (count == 1) ? B_OK : B_ERROR;
119 	val(rv);
120 
121 	out("Calling GetAllInputsFor(source) (should fail)\n");
122 	rv = roster->GetAllInputsFor(sourceNode,inputs,2,&count);
123 	val(rv);
124 	out("Found %ld\n",count);
125 
126 	out("Calling GetAllOutputsFor(destination) (should fail)\n");
127 	rv = roster->GetAllOutputsFor(destinationNode,outputs,2,&count);
128 	val(rv);
129 	out("Found %ld\n",count);
130 
131 	out("Calling GetConnectedOutputsFor(source)\n");
132 	rv = roster->GetConnectedOutputsFor(sourceNode,outputs,2,&count);
133 	val(rv);
134 	out("Found %ld\n",count);
135 	rv = (count == 0) ? B_OK : B_ERROR;
136 	val(rv);
137 
138 	out("Calling GetConnectedInputsFor(destination)\n");
139 	rv = roster->GetConnectedInputsFor(destinationNode,inputs,2,&count);
140 	val(rv);
141 	out("Found %ld\n",count);
142 	rv = (count == 0) ? B_OK : B_ERROR;
143 	val(rv);
144 
145 	out("Calling GetConnectedInputsFor(source) (should fail)\n");
146 	rv = roster->GetConnectedInputsFor(sourceNode,inputs,2,&count);
147 	val(rv);
148 	out("Found %ld\n",count);
149 
150 	out("Calling GetConnectedOutputsFor(destination) (should fail)\n");
151 	rv = roster->GetConnectedOutputsFor(destinationNode,outputs,2,&count);
152 	val(rv);
153 	out("Found %ld\n",count);
154 
155 	out("Calling GetFreeOutputsFor(source)\n");
156 	rv = roster->GetFreeOutputsFor(sourceNode,&output,1,&count,B_MEDIA_RAW_AUDIO);
157 	val(rv);
158 	out("Found %ld\n",count);
159 	rv = (count == 1) ? B_OK : B_ERROR;
160 	val(rv);
161 
162 	out("Calling GetFreeInputsFor(destination)\n");
163 	rv = roster->GetFreeInputsFor(destinationNode,&input,1,&count,B_MEDIA_RAW_AUDIO);
164 	val(rv);
165 	out("Found %ld\n",count);
166 	rv = (count == 1) ? B_OK : B_ERROR;
167 	val(rv);
168 
169 	out("Calling GetFreeOutputsFor(destination) (should fail)\n");
170 	rv = roster->GetFreeOutputsFor(destinationNode,outputs,2,&count,B_MEDIA_RAW_AUDIO);
171 	val(rv);
172 	out("Found %ld\n",count);
173 
174 	out("Calling GetFreeInputsFor(source) (should fail)\n");
175 	rv = roster->GetFreeInputsFor(sourceNode,inputs,2,&count,B_MEDIA_RAW_AUDIO);
176 	val(rv);
177 	out("Found %ld\n",count);
178 
179 	out("Calling GetAttNodeAttributesFor(source)\n");
180 	media_node_attribute attr[10];
181 	ssize_t size = roster->GetNodeAttributesFor(sourceNode, attr, 10);
182 	val_size(size);
183 	out("Found %" B_PRIdSSIZE "\n", size);
184 
185 	media_format format;
186 	format.type = B_MEDIA_RAW_AUDIO;
187 	format.u.raw_audio = media_raw_audio_format::wildcard;
188 
189 	out("Connecting nodes\n");
190 	rv = roster->Connect(output.source, input.destination, &format, &output, &input);
191 	val(rv);
192 
193 	out("Prerolling Producer()\n");
194 	rv = roster->PrerollNode(sourceNode);
195 	val(rv);
196 
197 	out("Prerolling Consumer\n");
198 	rv = roster->PrerollNode(destinationNode);
199 	val(rv);
200 
201 	bigtime_t time1;
202 	bigtime_t time2;
203 	bigtime_t start;
204 
205 	out("Getting Producer startlatency\n");
206 	rv = roster->GetStartLatencyFor(destinationNode, &time1);
207 	val(rv);
208 
209 	out("Getting Consumer startlatency\n");
210 	rv = roster->GetStartLatencyFor(sourceNode, &time2);
211 	val(rv);
212 
213 	start = max_c(time1,time2) + producer->TimeSource()->PerformanceTimeFor(BTimeSource::RealTime() + 2000000);
214 
215 	out("Starting Consumer in 2 sec\n");
216 	rv = roster->StartNode(destinationNode, start);
217 	val(rv);
218 
219 	out("Starting Producer in 2 sec\n");
220 	rv = roster->StartNode(sourceNode, start);
221 	val(rv);
222 
223 	out("Testing SyncToNode performance time set is 5 sec\n");
224 	rv = roster->SyncToNode(sourceNode,
225 		producer->TimeSource()->PerformanceTimeFor(
226 			BTimeSource::RealTime() + 5000000), B_INFINITE_TIMEOUT);
227 	val(rv);
228 
229 	out("########################## PRESS ENTER TO QUIT ##########################\n");
230 	getchar();
231 
232 	media_node_id sourceNodeID;
233 	media_node_id destinationNodeID;
234 
235 	out("Calling producer->ID()\n");
236 	sourceNodeID = producer->ID();
237 
238 	out("Calling consumer->ID()\n");
239 	destinationNodeID = consumer->ID();
240 
241 	out("Stopping Producer()\n");
242 	rv = roster->StopNode(sourceNode, 0, true);
243 	val(rv);
244 
245 	out("Stopping Consumer\n");
246 	rv = roster->StopNode(destinationNode, 0, true);
247 	val(rv);
248 
249 	out("Disconnecting nodes\n");
250 	rv = roster->Disconnect(sourceNodeID, output.source, destinationNodeID, input.destination);
251 	val(rv);
252 
253 	out("Unregistering ProducerNode\n");
254 	rv = roster->UnregisterNode(producer);
255 	val(rv);
256 
257 	out("Unregistering ConsumerNode\n");
258 	rv = roster->UnregisterNode(consumer);
259 	val(rv);
260 
261 	out("Releasing ProducerNode\n");
262 	rv = (producer->Release() == NULL) ? B_OK : B_ERROR;
263 	val(rv);
264 
265 	out("Releasing ConsumerNode\n");
266 	rv = (consumer->Release() == NULL) ? B_OK : B_ERROR;
267 	val(rv);
268 
269 	return 0;
270 }
271 
272