xref: /haiku/src/apps/cortex/addons/LoggingConsumer/NodeHarnessWin.cpp (revision 362efe0c9f36d3dd38b22d2c24ac02e54b189d7c)
1 /*
2  * Copyright 1991-1999, Be Incorporated.
3  * Copyright (c) 1999-2000, Eric Moon.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions, and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 // NodeHarnessWin.cpp
34 
35 #include "NodeHarnessWin.h"
36 #include "LoggingConsumer.h"
37 #include <app/Application.h>
38 #include <interface/Button.h>
39 //#include <storage/Entry.h>
40 #include <Entry.h>
41 #include <media/MediaRoster.h>
42 #include <media/MediaAddOn.h>
43 #include <media/TimeSource.h>
44 #include <media/MediaTheme.h>
45 #include <stdio.h>
46 
47 const int32 BUTTON_CONNECT = 'Cnct';
48 const int32 BUTTON_START = 'Strt';
49 const int32 BUTTON_STOP = 'Stop';
50 
51 #define TEST_WITH_AUDIO 1
52 
53 // --------------------
54 // static utility functions
55 static void ErrorCheck(status_t err, const char* msg)
56 {
57 	if (err)
58 	{
59 		fprintf(stderr, "* FATAL ERROR (%s): %s\n", strerror(err), msg);
60 		exit(1);
61 	}
62 }
63 
64 // --------------------
65 // NodeHarnessWin implementation
66 NodeHarnessWin::NodeHarnessWin(BRect frame, const char *title)
67 	:	BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
68 		mLogNode(NULL), mIsConnected(false), mIsRunning(false)
69 {
70 	// build the UI
71 	BRect r(10, 10, 100, 40);
72 	mConnectButton = new BButton(r, "Connect", "Connect", new BMessage(BUTTON_CONNECT));
73 	mConnectButton->SetEnabled(true);
74 	AddChild(mConnectButton);
75 	r.OffsetBy(0, 40);
76 	mStartButton = new BButton(r, "Start", "Start", new BMessage(BUTTON_START));
77 	mStartButton->SetEnabled(false);
78 	AddChild(mStartButton);
79 	r.OffsetBy(0, 40);
80 	mStopButton = new BButton(r, "Stop", "Stop", new BMessage(BUTTON_STOP));
81 	mStopButton->SetEnabled(false);
82 	AddChild(mStopButton);
83 }
84 
85 NodeHarnessWin::~NodeHarnessWin()
86 {
87 	BMediaRoster* r = BMediaRoster::Roster();
88 
89 	// tear down the node network
90 	if (mIsRunning) StopNodes();
91 	if (mIsConnected)
92 	{
93 		printf("Total late buffers: %ld\n", mLogNode->LateBuffers());
94 		r->StopNode(mConnection.consumer, 0, true);
95 		r->Disconnect(mConnection.producer.node, mConnection.source,
96 			mConnection.consumer.node, mConnection.destination);
97 		r->ReleaseNode(mConnection.producer);
98 		r->ReleaseNode(mConnection.consumer);
99 	}
100 }
101 
102 void
103 NodeHarnessWin::Quit()
104 {
105 	be_app->PostMessage(B_QUIT_REQUESTED);
106 	BWindow::Quit();
107 }
108 
109 void
110 NodeHarnessWin::MessageReceived(BMessage *msg)
111 {
112 	status_t err;
113 
114 	switch (msg->what)
115 	{
116 	case BUTTON_CONNECT:
117 		mIsConnected = true;
118 
119 		// set the button states appropriately
120 		mConnectButton->SetEnabled(false);
121 		mStartButton->SetEnabled(true);
122 
123 		// set up the node network
124 		{
125 			BMediaRoster* r = BMediaRoster::Roster();
126 
127 			// find a node that can handle an audio file
128 #if TEST_WITH_AUDIO
129 			entry_ref inRef;
130 			dormant_node_info info;
131 
132 			::get_ref_for_path("/boot/optional/sound/virtual (void)", &inRef);
133 			err = r->SniffRef(inRef, B_BUFFER_PRODUCER | B_FILE_INTERFACE, &info);
134 			ErrorCheck(err, "couldn't find file reader node\n");
135 
136 			err = r->InstantiateDormantNode(info, &mConnection.producer, B_FLAVOR_IS_LOCAL);
137 			ErrorCheck(err, "couldn't instantiate file reader node\n");
138 
139 			bigtime_t dummy_length;			// output = media length; we don't use it
140 			err = r->SetRefFor(mConnection.producer, inRef, false, &dummy_length);
141 			ErrorCheck(err, "unable to SetRefFor() to read that sound file!\n");
142 #else
143 			r->GetVideoInput(&mConnection.producer);
144 #endif
145 
146 			entry_ref logRef;
147 			::get_ref_for_path("/tmp/node_log", &logRef);
148 
149 			mLogNode = new LoggingConsumer(logRef);
150 			err = r->RegisterNode(mLogNode);
151 			ErrorCheck(err, "unable to register LoggingConsumer node!\n");
152 			// make sure the Media Roster knows that we're using the node
153 			r->GetNodeFor(mLogNode->Node().node, &mConnection.consumer);
154 
155 			// trim down the log's verbosity a touch
156 			mLogNode->SetEnabled(LOG_HANDLE_EVENT, false);
157 
158 			// fire off a window with the LoggingConsumer's controls in it
159 			BParameterWeb* web;
160 			r->GetParameterWebFor(mConnection.consumer, &web);
161 			BView* view = BMediaTheme::ViewFor(web);
162 			BWindow* win = new BWindow(BRect(250, 200, 300, 300), "Controls",
163 				B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS);
164 			win->AddChild(view);
165 			win->ResizeTo(view->Bounds().Width(), view->Bounds().Height());
166 			win->Show();
167 
168 			// set the nodes' time sources
169 			r->GetTimeSource(&mTimeSource);
170 			r->SetTimeSourceFor(mConnection.consumer.node, mTimeSource.node);
171 			r->SetTimeSourceFor(mConnection.producer.node, mTimeSource.node);
172 
173 			// got the nodes; now we find the endpoints of the connection
174 			media_input logInput;
175 			media_output soundOutput;
176 			int32 count;
177 			err = r->GetFreeOutputsFor(mConnection.producer, &soundOutput, 1, &count);
178 			ErrorCheck(err, "unable to get a free output from the producer node");
179 			err = r->GetFreeInputsFor(mConnection.consumer, &logInput, 1, &count);
180 			ErrorCheck(err, "unable to get a free input to the LoggingConsumer");
181 
182 			// fill in the rest of the Connection object
183 			mConnection.source = soundOutput.source;
184 			mConnection.destination = logInput.destination;
185 
186 			// got the endpoints; now we connect it!
187 			media_format format;
188 #if TEST_WITH_AUDIO
189 			format.type = B_MEDIA_RAW_AUDIO;			// !!! hmmm.. how to fully wildcard this?
190 			format.u.raw_audio = media_raw_audio_format::wildcard;
191 #else
192 			format.type = B_MEDIA_RAW_VIDEO;			// !!! hmmm.. how to fully wildcard this?
193 			format.u.raw_video = media_raw_video_format::wildcard;
194 #endif
195 			err = r->Connect(mConnection.source, mConnection.destination, &format, &soundOutput, &logInput);
196 			ErrorCheck(err, "unable to connect nodes");
197 			mConnection.format = format;
198 
199 			// for video input, we need to set the downstream latency for record -> playback
200 			bigtime_t latency;
201 			r->GetLatencyFor(mConnection.producer, &latency);
202 			printf("Setting producer run mode latency to %Ld\n", latency);
203 			r->SetProducerRunModeDelay(mConnection.producer, latency + 6000);
204 
205 			// preroll first, to be a good citizen
206 			r->PrerollNode(mConnection.consumer);
207 			r->PrerollNode(mConnection.producer);
208 
209 			// start the LoggingConsumer and leave it running
210 			BTimeSource* ts = r->MakeTimeSourceFor(mTimeSource);
211 			r->StartNode(mConnection.consumer, ts->Now());
212 			ts->Release();
213 		}
214 		break;
215 
216 	case BUTTON_START:
217 		mStartButton->SetEnabled(false);
218 		mStopButton->SetEnabled(true);
219 
220 		// start the consumer running
221 		{
222 			bigtime_t latency;
223 			BMediaRoster* r = BMediaRoster::Roster();
224 			BTimeSource* ts = r->MakeTimeSourceFor(mConnection.consumer);
225 			r->GetLatencyFor(mConnection.producer, &latency);
226 			r->StartNode(mConnection.producer, ts->Now() + latency);
227 			ts->Release();
228 			mIsRunning = true;
229 		}
230 		break;
231 
232 	case BUTTON_STOP:
233 		StopNodes();
234 		break;
235 
236 	default:
237 		BWindow::MessageReceived(msg);
238 		break;
239 	}
240 }
241 
242 // Private routines
243 void
244 NodeHarnessWin::StopNodes()
245 {
246 	mStartButton->SetEnabled(true);
247 	mStopButton->SetEnabled(false);
248 
249 	// stop the producer
250 	{
251 		BMediaRoster* r = BMediaRoster::Roster();
252 		r->StopNode(mConnection.producer, 0, true);		// synchronous stop
253 	}
254 }
255 
256