152a38012Sejakowatz#include <Application.h>
2fb6c8a04SDario Casalinuovo#include <MediaDefs.h>
352a38012Sejakowatz#include <MediaRoster.h>
452a38012Sejakowatz#include <TimeSource.h>
5fb6c8a04SDario Casalinuovo
6fb6c8a04SDario Casalinuovo#include <string.h>
7fb6c8a04SDario Casalinuovo
852a38012Sejakowatz#include "ConsumerNode.h"
952a38012Sejakowatz#include "misc.h"
10fb6c8a04SDario Casalinuovo#include "ProducerNode.h"
11fb6c8a04SDario Casalinuovo
1252a38012Sejakowatz
1352a38012SejakowatzBMediaRoster *roster;
1452a38012SejakowatzProducerNode *producer;
1552a38012SejakowatzConsumerNode *consumer;
1652a38012Sejakowatzstatus_t rv;
1752a38012Sejakowatz
1852a38012Sejakowatzint main()
1952a38012Sejakowatz{
2052a38012Sejakowatz	out("Basic BBufferProducer, BBufferConsumer, BMediaRoster test\n");
2152a38012Sejakowatz	out("for OpenBeOS by Marcus Overhagen <Marcus@Overhagen.de>\n\n");
2252a38012Sejakowatz	out("Creating BApplication now\n");
2352a38012Sejakowatz	BApplication app("application/x-vnd.OpenBeOS-NodeTest");
2452a38012Sejakowatz
2552a38012Sejakowatz	out("Creating MediaRoster\n");
2652a38012Sejakowatz	roster = BMediaRoster::Roster();
2752a38012Sejakowatz	val(roster);
2852a38012Sejakowatz
2952a38012Sejakowatz	out("Creating ProducerNode\n");
3052a38012Sejakowatz	producer = new ProducerNode();
3152a38012Sejakowatz	val(producer);
3252a38012Sejakowatz
3352a38012Sejakowatz	out("Creating ConsumerNode\n");
3452a38012Sejakowatz	consumer = new ConsumerNode();
3552a38012Sejakowatz	val(consumer);
3652a38012Sejakowatz
3752a38012Sejakowatz	out("Registering ProducerNode\n");
3852a38012Sejakowatz	rv = roster->RegisterNode(producer);
3952a38012Sejakowatz	val(rv);
4052a38012Sejakowatz
4152a38012Sejakowatz	out("Registering ConsumerNode\n");
4252a38012Sejakowatz	rv = roster->RegisterNode(consumer);
4352a38012Sejakowatz	val(rv);
4452a38012Sejakowatz
4552a38012Sejakowatz	media_node sourceNode;
4652a38012Sejakowatz	media_node destinationNode;
4752a38012Sejakowatz
4852a38012Sejakowatz	out("Calling producer->Node()\n");
4952a38012Sejakowatz	sourceNode = producer->Node();
5052a38012Sejakowatz
5152a38012Sejakowatz	out("Calling consumer->Node()\n");
5252a38012Sejakowatz	destinationNode = consumer->Node();
5352a38012Sejakowatz
54d09f3b56Sbeveloper
55d09f3b56Sbeveloper	live_node_info live_nodes[100];
56d09f3b56Sbeveloper	int32 live_count;
57d09f3b56Sbeveloper	media_format liveformat;
58d09f3b56Sbeveloper	memset(&liveformat, 0, sizeof(liveformat));
59d09f3b56Sbeveloper
60d09f3b56Sbeveloper	liveformat.type = B_MEDIA_RAW_AUDIO;
61d09f3b56Sbeveloper
62d09f3b56Sbeveloper	out("Calling GetLiveNodes(), has_input = B_MEDIA_RAW_AUDIO\n");
63d09f3b56Sbeveloper	live_count = 100;
64d09f3b56Sbeveloper	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat);
65d09f3b56Sbeveloper	val(rv);
66d09f3b56Sbeveloper	out("Found %ld\n",live_count);
67d09f3b56Sbeveloper
68d09f3b56Sbeveloper	out("Calling GetLiveNodes(), has_output = B_MEDIA_RAW_AUDIO\n");
69d09f3b56Sbeveloper	live_count = 100;
70d09f3b56Sbeveloper	rv = roster->GetLiveNodes(live_nodes, &live_count, NULL, &liveformat);
71d09f3b56Sbeveloper	val(rv);
72d09f3b56Sbeveloper	out("Found %ld\n",live_count);
73d09f3b56Sbeveloper
74d09f3b56Sbeveloper	out("Calling GetLiveNodes(), has_input = has_output = B_MEDIA_RAW_AUDIO\n");
75d09f3b56Sbeveloper	live_count = 100;
76d09f3b56Sbeveloper	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat, &liveformat);
77d09f3b56Sbeveloper	val(rv);
78d09f3b56Sbeveloper	out("Found %ld\n",live_count);
79d09f3b56Sbeveloper
80d09f3b56Sbeveloper	liveformat.type = B_MEDIA_RAW_VIDEO;
81d09f3b56Sbeveloper
82d09f3b56Sbeveloper	out("Calling GetLiveNodes(), has_input = B_MEDIA_RAW_VIDEO\n");
83d09f3b56Sbeveloper	live_count = 100;
84d09f3b56Sbeveloper	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat);
85d09f3b56Sbeveloper	val(rv);
86d09f3b56Sbeveloper	out("Found %ld\n",live_count);
87d09f3b56Sbeveloper
88d09f3b56Sbeveloper	out("Calling GetLiveNodes(), has_output = B_MEDIA_RAW_VIDEO\n");
89d09f3b56Sbeveloper	live_count = 100;
90d09f3b56Sbeveloper	rv = roster->GetLiveNodes(live_nodes, &live_count, NULL, &liveformat);
91d09f3b56Sbeveloper	val(rv);
92d09f3b56Sbeveloper	out("Found %ld\n",live_count);
93d09f3b56Sbeveloper
94d09f3b56Sbeveloper	out("Calling GetLiveNodes(), has_input = has_output = B_MEDIA_RAW_VIDEO\n");
95d09f3b56Sbeveloper	live_count = 100;
96d09f3b56Sbeveloper	rv = roster->GetLiveNodes(live_nodes, &live_count, &liveformat, &liveformat);
97d09f3b56Sbeveloper	val(rv);
98d09f3b56Sbeveloper	out("Found %ld\n",live_count);
99d09f3b56Sbeveloper
100d09f3b56Sbeveloper
10152a38012Sejakowatz	media_output output;
10252a38012Sejakowatz	media_input input;
103d09f3b56Sbeveloper	media_output outputs[2];
104d09f3b56Sbeveloper	media_input inputs[2];
10552a38012Sejakowatz	int32 count;
10652a38012Sejakowatz
10752a38012Sejakowatz	out("Calling GetAllOutputsFor(source)\n");
108d09f3b56Sbeveloper	rv = roster->GetAllOutputsFor(sourceNode,outputs,2,&count);
10952a38012Sejakowatz	val(rv);
110d09f3b56Sbeveloper	out("Found %ld\n",count);
11152a38012Sejakowatz	rv = (count == 1) ? B_OK : B_ERROR;
11252a38012Sejakowatz	val(rv);
11352a38012Sejakowatz
11452a38012Sejakowatz	out("Calling GetAllInputsFor(destination)\n");
115d09f3b56Sbeveloper	rv = roster->GetAllInputsFor(destinationNode,inputs,2,&count);
11652a38012Sejakowatz	val(rv);
117d09f3b56Sbeveloper	out("Found %ld\n",count);
11852a38012Sejakowatz	rv = (count == 1) ? B_OK : B_ERROR;
11952a38012Sejakowatz	val(rv);
120d09f3b56Sbeveloper
121d09f3b56Sbeveloper	out("Calling GetAllInputsFor(source) (should fail)\n");
122d09f3b56Sbeveloper	rv = roster->GetAllInputsFor(sourceNode,inputs,2,&count);
123d09f3b56Sbeveloper	val(rv);
124d09f3b56Sbeveloper	out("Found %ld\n",count);
125d09f3b56Sbeveloper
126d09f3b56Sbeveloper	out("Calling GetAllOutputsFor(destination) (should fail)\n");
127d09f3b56Sbeveloper	rv = roster->GetAllOutputsFor(destinationNode,outputs,2,&count);
128d09f3b56Sbeveloper	val(rv);
129d09f3b56Sbeveloper	out("Found %ld\n",count);
130d09f3b56Sbeveloper
131d09f3b56Sbeveloper	out("Calling GetConnectedOutputsFor(source)\n");
132d09f3b56Sbeveloper	rv = roster->GetConnectedOutputsFor(sourceNode,outputs,2,&count);
133d09f3b56Sbeveloper	val(rv);
134d09f3b56Sbeveloper	out("Found %ld\n",count);
135d09f3b56Sbeveloper	rv = (count == 0) ? B_OK : B_ERROR;
136d09f3b56Sbeveloper	val(rv);
137d09f3b56Sbeveloper
138d09f3b56Sbeveloper	out("Calling GetConnectedInputsFor(destination)\n");
139d09f3b56Sbeveloper	rv = roster->GetConnectedInputsFor(destinationNode,inputs,2,&count);
140d09f3b56Sbeveloper	val(rv);
141d09f3b56Sbeveloper	out("Found %ld\n",count);
142d09f3b56Sbeveloper	rv = (count == 0) ? B_OK : B_ERROR;
143d09f3b56Sbeveloper	val(rv);
144d09f3b56Sbeveloper
145d09f3b56Sbeveloper	out("Calling GetConnectedInputsFor(source) (should fail)\n");
146d09f3b56Sbeveloper	rv = roster->GetConnectedInputsFor(sourceNode,inputs,2,&count);
147d09f3b56Sbeveloper	val(rv);
148d09f3b56Sbeveloper	out("Found %ld\n",count);
149d09f3b56Sbeveloper
150d09f3b56Sbeveloper	out("Calling GetConnectedOutputsFor(destination) (should fail)\n");
151d09f3b56Sbeveloper	rv = roster->GetConnectedOutputsFor(destinationNode,outputs,2,&count);
152d09f3b56Sbeveloper	val(rv);
153d09f3b56Sbeveloper	out("Found %ld\n",count);
154d09f3b56Sbeveloper
15552a38012Sejakowatz	out("Calling GetFreeOutputsFor(source)\n");
15652a38012Sejakowatz	rv = roster->GetFreeOutputsFor(sourceNode,&output,1,&count,B_MEDIA_RAW_AUDIO);
15752a38012Sejakowatz	val(rv);
158d09f3b56Sbeveloper	out("Found %ld\n",count);
15952a38012Sejakowatz	rv = (count == 1) ? B_OK : B_ERROR;
16052a38012Sejakowatz	val(rv);
16152a38012Sejakowatz
16252a38012Sejakowatz	out("Calling GetFreeInputsFor(destination)\n");
16352a38012Sejakowatz	rv = roster->GetFreeInputsFor(destinationNode,&input,1,&count,B_MEDIA_RAW_AUDIO);
16452a38012Sejakowatz	val(rv);
165d09f3b56Sbeveloper	out("Found %ld\n",count);
16652a38012Sejakowatz	rv = (count == 1) ? B_OK : B_ERROR;
16752a38012Sejakowatz	val(rv);
16852a38012Sejakowatz
169d09f3b56Sbeveloper	out("Calling GetFreeOutputsFor(destination) (should fail)\n");
170d09f3b56Sbeveloper	rv = roster->GetFreeOutputsFor(destinationNode,outputs,2,&count,B_MEDIA_RAW_AUDIO);
171d09f3b56Sbeveloper	val(rv);
172d09f3b56Sbeveloper	out("Found %ld\n",count);
173d09f3b56Sbeveloper
174d09f3b56Sbeveloper	out("Calling GetFreeInputsFor(source) (should fail)\n");
175d09f3b56Sbeveloper	rv = roster->GetFreeInputsFor(sourceNode,inputs,2,&count,B_MEDIA_RAW_AUDIO);
176d09f3b56Sbeveloper	val(rv);
177d09f3b56Sbeveloper	out("Found %ld\n",count);
178d09f3b56Sbeveloper
179fb6c8a04SDario Casalinuovo	out("Calling GetAttNodeAttributesFor(source)\n");
180fb6c8a04SDario Casalinuovo	media_node_attribute attr[10];
181fb6c8a04SDario Casalinuovo	ssize_t size = roster->GetNodeAttributesFor(sourceNode, attr, 10);
182fb6c8a04SDario Casalinuovo	val_size(size);
183fb6c8a04SDario Casalinuovo	out("Found %" B_PRIdSSIZE "\n", size);
184fb6c8a04SDario Casalinuovo
18552a38012Sejakowatz	media_format format;
18652a38012Sejakowatz	format.type = B_MEDIA_RAW_AUDIO;
18752a38012Sejakowatz	format.u.raw_audio = media_raw_audio_format::wildcard;
18852a38012Sejakowatz
18952a38012Sejakowatz	out("Connecting nodes\n");
19052a38012Sejakowatz	rv = roster->Connect(output.source, input.destination, &format, &output, &input);
19152a38012Sejakowatz	val(rv);
19252a38012Sejakowatz
19352a38012Sejakowatz	out("Prerolling Producer()\n");
19452a38012Sejakowatz	rv = roster->PrerollNode(sourceNode);
19552a38012Sejakowatz	val(rv);
19652a38012Sejakowatz
19752a38012Sejakowatz	out("Prerolling Consumer\n");
19852a38012Sejakowatz	rv = roster->PrerollNode(destinationNode);
19952a38012Sejakowatz	val(rv);
20052a38012Sejakowatz
20152a38012Sejakowatz	bigtime_t time1;
20252a38012Sejakowatz	bigtime_t time2;
20352a38012Sejakowatz	bigtime_t start;
20452a38012Sejakowatz
20552a38012Sejakowatz	out("Getting Producer startlatency\n");
20652a38012Sejakowatz	rv = roster->GetStartLatencyFor(destinationNode, &time1);
20752a38012Sejakowatz	val(rv);
20852a38012Sejakowatz
20952a38012Sejakowatz	out("Getting Consumer startlatency\n");
21052a38012Sejakowatz	rv = roster->GetStartLatencyFor(sourceNode, &time2);
21152a38012Sejakowatz	val(rv);
21252a38012Sejakowatz
21352a38012Sejakowatz	start = max_c(time1,time2) + producer->TimeSource()->PerformanceTimeFor(BTimeSource::RealTime() + 2000000);
21452a38012Sejakowatz
21552a38012Sejakowatz	out("Starting Consumer in 2 sec\n");
21652a38012Sejakowatz	rv = roster->StartNode(destinationNode, start);
21752a38012Sejakowatz	val(rv);
21852a38012Sejakowatz
21952a38012Sejakowatz	out("Starting Producer in 2 sec\n");
22052a38012Sejakowatz	rv = roster->StartNode(sourceNode, start);
22152a38012Sejakowatz	val(rv);
22252a38012Sejakowatz
2235720dfd4SDario Casalinuovo	out("Testing SyncToNode performance time set is 5 sec\n");
2245720dfd4SDario Casalinuovo	rv = roster->SyncToNode(sourceNode,
2255720dfd4SDario Casalinuovo		producer->TimeSource()->PerformanceTimeFor(
2265720dfd4SDario Casalinuovo			BTimeSource::RealTime() + 5000000), B_INFINITE_TIMEOUT);
2275720dfd4SDario Casalinuovo	val(rv);
2285720dfd4SDario Casalinuovo
22952a38012Sejakowatz	out("########################## PRESS ENTER TO QUIT ##########################\n");
23052a38012Sejakowatz	getchar();
23152a38012Sejakowatz
23252a38012Sejakowatz	media_node_id sourceNodeID;
23352a38012Sejakowatz	media_node_id destinationNodeID;
23452a38012Sejakowatz
23552a38012Sejakowatz	out("Calling producer->ID()\n");
23652a38012Sejakowatz	sourceNodeID = producer->ID();
23752a38012Sejakowatz
23852a38012Sejakowatz	out("Calling consumer->ID()\n");
23952a38012Sejakowatz	destinationNodeID = consumer->ID();
24052a38012Sejakowatz
24152a38012Sejakowatz	out("Stopping Producer()\n");
24252a38012Sejakowatz	rv = roster->StopNode(sourceNode, 0, true);
24352a38012Sejakowatz	val(rv);
24452a38012Sejakowatz
24552a38012Sejakowatz	out("Stopping Consumer\n");
24652a38012Sejakowatz	rv = roster->StopNode(destinationNode, 0, true);
24752a38012Sejakowatz	val(rv);
24852a38012Sejakowatz
24952a38012Sejakowatz	out("Disconnecting nodes\n");
25052a38012Sejakowatz	rv = roster->Disconnect(sourceNodeID, output.source, destinationNodeID, input.destination);
25152a38012Sejakowatz	val(rv);
25252a38012Sejakowatz
25352a38012Sejakowatz	out("Unregistering ProducerNode\n");
25452a38012Sejakowatz	rv = roster->UnregisterNode(producer);
25552a38012Sejakowatz	val(rv);
25652a38012Sejakowatz
25752a38012Sejakowatz	out("Unregistering ConsumerNode\n");
25852a38012Sejakowatz	rv = roster->UnregisterNode(consumer);
25952a38012Sejakowatz	val(rv);
26052a38012Sejakowatz
26152a38012Sejakowatz	out("Releasing ProducerNode\n");
26252a38012Sejakowatz	rv = (producer->Release() == NULL) ? B_OK : B_ERROR;
26352a38012Sejakowatz	val(rv);
26452a38012Sejakowatz
26552a38012Sejakowatz	out("Releasing ConsumerNode\n");
26652a38012Sejakowatz	rv = (consumer->Release() == NULL) ? B_OK : B_ERROR;
26752a38012Sejakowatz	val(rv);
26852a38012Sejakowatz
26952a38012Sejakowatz	return 0;
27052a38012Sejakowatz}
27152a38012Sejakowatz
272