vfs: check userland buffers before reading them.
[haiku.git] / src / apps / cortex / addons / LoggingConsumer / NodeHarnessWin.cpp
blob41240297551e4077d3484d4b6ff7fac981177742
1 /*
2 * Copyright 1991-1999, Be Incorporated.
3 * Copyright (c) 1999-2000, Eric Moon.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer.
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.
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.
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.
33 // NodeHarnessWin.cpp
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 #include <stdlib.h>
48 const int32 BUTTON_CONNECT = 'Cnct';
49 const int32 BUTTON_START = 'Strt';
50 const int32 BUTTON_STOP = 'Stop';
52 #define TEST_WITH_AUDIO 1
54 // --------------------
55 // static utility functions
56 static void ErrorCheck(status_t err, const char* msg)
58 if (err)
60 fprintf(stderr, "* FATAL ERROR (%s): %s\n", strerror(err), msg);
61 exit(1);
65 // --------------------
66 // NodeHarnessWin implementation
67 NodeHarnessWin::NodeHarnessWin(BRect frame, const char *title)
68 : BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
69 mLogNode(NULL), mIsConnected(false), mIsRunning(false)
71 // build the UI
72 BRect r(10, 10, 100, 40);
73 mConnectButton = new BButton(r, "Connect", "Connect", new BMessage(BUTTON_CONNECT));
74 mConnectButton->SetEnabled(true);
75 AddChild(mConnectButton);
76 r.OffsetBy(0, 40);
77 mStartButton = new BButton(r, "Start", "Start", new BMessage(BUTTON_START));
78 mStartButton->SetEnabled(false);
79 AddChild(mStartButton);
80 r.OffsetBy(0, 40);
81 mStopButton = new BButton(r, "Stop", "Stop", new BMessage(BUTTON_STOP));
82 mStopButton->SetEnabled(false);
83 AddChild(mStopButton);
86 NodeHarnessWin::~NodeHarnessWin()
88 BMediaRoster* r = BMediaRoster::Roster();
90 // tear down the node network
91 if (mIsRunning) StopNodes();
92 if (mIsConnected)
94 printf("Total late buffers: %ld\n", mLogNode->LateBuffers());
95 r->StopNode(mConnection.consumer, 0, true);
96 r->Disconnect(mConnection.producer.node, mConnection.source,
97 mConnection.consumer.node, mConnection.destination);
98 r->ReleaseNode(mConnection.producer);
99 r->ReleaseNode(mConnection.consumer);
103 void
104 NodeHarnessWin::Quit()
106 be_app->PostMessage(B_QUIT_REQUESTED);
107 BWindow::Quit();
110 void
111 NodeHarnessWin::MessageReceived(BMessage *msg)
113 status_t err;
115 switch (msg->what)
117 case BUTTON_CONNECT:
118 mIsConnected = true;
120 // set the button states appropriately
121 mConnectButton->SetEnabled(false);
122 mStartButton->SetEnabled(true);
124 // set up the node network
126 BMediaRoster* r = BMediaRoster::Roster();
128 // find a node that can handle an audio file
129 #if TEST_WITH_AUDIO
130 entry_ref inRef;
131 dormant_node_info info;
133 ::get_ref_for_path("/boot/optional/sound/virtual (void)", &inRef);
134 err = r->SniffRef(inRef, B_BUFFER_PRODUCER | B_FILE_INTERFACE, &info);
135 ErrorCheck(err, "couldn't find file reader node\n");
137 err = r->InstantiateDormantNode(info, &mConnection.producer, B_FLAVOR_IS_LOCAL);
138 ErrorCheck(err, "couldn't instantiate file reader node\n");
140 bigtime_t dummy_length; // output = media length; we don't use it
141 err = r->SetRefFor(mConnection.producer, inRef, false, &dummy_length);
142 ErrorCheck(err, "unable to SetRefFor() to read that sound file!\n");
143 #else
144 r->GetVideoInput(&mConnection.producer);
145 #endif
147 entry_ref logRef;
148 ::get_ref_for_path("/tmp/node_log", &logRef);
150 mLogNode = new LoggingConsumer(logRef);
151 err = r->RegisterNode(mLogNode);
152 ErrorCheck(err, "unable to register LoggingConsumer node!\n");
153 // make sure the Media Roster knows that we're using the node
154 r->GetNodeFor(mLogNode->Node().node, &mConnection.consumer);
156 // trim down the log's verbosity a touch
157 mLogNode->SetEnabled(LOG_HANDLE_EVENT, false);
159 // fire off a window with the LoggingConsumer's controls in it
160 BParameterWeb* web;
161 r->GetParameterWebFor(mConnection.consumer, &web);
162 BView* view = BMediaTheme::ViewFor(web);
163 BWindow* win = new BWindow(BRect(250, 200, 300, 300), "Controls",
164 B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS);
165 win->AddChild(view);
166 win->ResizeTo(view->Bounds().Width(), view->Bounds().Height());
167 win->Show();
169 // set the nodes' time sources
170 r->GetTimeSource(&mTimeSource);
171 r->SetTimeSourceFor(mConnection.consumer.node, mTimeSource.node);
172 r->SetTimeSourceFor(mConnection.producer.node, mTimeSource.node);
174 // got the nodes; now we find the endpoints of the connection
175 media_input logInput;
176 media_output soundOutput;
177 int32 count;
178 err = r->GetFreeOutputsFor(mConnection.producer, &soundOutput, 1, &count);
179 ErrorCheck(err, "unable to get a free output from the producer node");
180 err = r->GetFreeInputsFor(mConnection.consumer, &logInput, 1, &count);
181 ErrorCheck(err, "unable to get a free input to the LoggingConsumer");
183 // fill in the rest of the Connection object
184 mConnection.source = soundOutput.source;
185 mConnection.destination = logInput.destination;
187 // got the endpoints; now we connect it!
188 media_format format;
189 #if TEST_WITH_AUDIO
190 format.type = B_MEDIA_RAW_AUDIO; // !!! hmmm.. how to fully wildcard this?
191 format.u.raw_audio = media_raw_audio_format::wildcard;
192 #else
193 format.type = B_MEDIA_RAW_VIDEO; // !!! hmmm.. how to fully wildcard this?
194 format.u.raw_video = media_raw_video_format::wildcard;
195 #endif
196 err = r->Connect(mConnection.source, mConnection.destination, &format, &soundOutput, &logInput);
197 ErrorCheck(err, "unable to connect nodes");
198 mConnection.format = format;
200 // for video input, we need to set the downstream latency for record -> playback
201 bigtime_t latency;
202 r->GetLatencyFor(mConnection.producer, &latency);
203 printf("Setting producer run mode latency to %" B_PRIdBIGTIME "\n", latency);
204 r->SetProducerRunModeDelay(mConnection.producer, latency + 6000);
206 // preroll first, to be a good citizen
207 r->PrerollNode(mConnection.consumer);
208 r->PrerollNode(mConnection.producer);
210 // start the LoggingConsumer and leave it running
211 BTimeSource* ts = r->MakeTimeSourceFor(mTimeSource);
212 r->StartNode(mConnection.consumer, ts->Now());
213 ts->Release();
215 break;
217 case BUTTON_START:
218 mStartButton->SetEnabled(false);
219 mStopButton->SetEnabled(true);
221 // start the consumer running
223 bigtime_t latency;
224 BMediaRoster* r = BMediaRoster::Roster();
225 BTimeSource* ts = r->MakeTimeSourceFor(mConnection.consumer);
226 r->GetLatencyFor(mConnection.producer, &latency);
227 r->StartNode(mConnection.producer, ts->Now() + latency);
228 ts->Release();
229 mIsRunning = true;
231 break;
233 case BUTTON_STOP:
234 StopNodes();
235 break;
237 default:
238 BWindow::MessageReceived(msg);
239 break;
243 // Private routines
244 void
245 NodeHarnessWin::StopNodes()
247 mStartButton->SetEnabled(true);
248 mStopButton->SetEnabled(false);
250 // stop the producer
252 BMediaRoster* r = BMediaRoster::Roster();
253 r->StopNode(mConnection.producer, 0, true); // synchronous stop