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 #include <stdlib.h> 47 48 const int32 BUTTON_CONNECT = 'Cnct'; 49 const int32 BUTTON_START = 'Strt'; 50 const int32 BUTTON_STOP = 'Stop'; 51 52 #define TEST_WITH_AUDIO 1 53 54 // -------------------- 55 // static utility functions 56 static void ErrorCheck(status_t err, const char* msg) 57 { 58 if (err) 59 { 60 fprintf(stderr, "* FATAL ERROR (%s): %s\n", strerror(err), msg); 61 exit(1); 62 } 63 } 64 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) 70 { 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); 84 } 85 86 NodeHarnessWin::~NodeHarnessWin() 87 { 88 BMediaRoster* r = BMediaRoster::Roster(); 89 90 // tear down the node network 91 if (mIsRunning) StopNodes(); 92 if (mIsConnected) 93 { 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); 100 } 101 } 102 103 void 104 NodeHarnessWin::Quit() 105 { 106 be_app->PostMessage(B_QUIT_REQUESTED); 107 BWindow::Quit(); 108 } 109 110 void 111 NodeHarnessWin::MessageReceived(BMessage *msg) 112 { 113 status_t err; 114 115 switch (msg->what) 116 { 117 case BUTTON_CONNECT: 118 mIsConnected = true; 119 120 // set the button states appropriately 121 mConnectButton->SetEnabled(false); 122 mStartButton->SetEnabled(true); 123 124 // set up the node network 125 { 126 BMediaRoster* r = BMediaRoster::Roster(); 127 128 // find a node that can handle an audio file 129 #if TEST_WITH_AUDIO 130 entry_ref inRef; 131 dormant_node_info info; 132 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"); 136 137 err = r->InstantiateDormantNode(info, &mConnection.producer, B_FLAVOR_IS_LOCAL); 138 ErrorCheck(err, "couldn't instantiate file reader node\n"); 139 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 146 147 entry_ref logRef; 148 ::get_ref_for_path("/tmp/node_log", &logRef); 149 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); 155 156 // trim down the log's verbosity a touch 157 mLogNode->SetEnabled(LOG_HANDLE_EVENT, false); 158 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(); 168 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); 173 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"); 182 183 // fill in the rest of the Connection object 184 mConnection.source = soundOutput.source; 185 mConnection.destination = logInput.destination; 186 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; 199 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); 205 206 // preroll first, to be a good citizen 207 r->PrerollNode(mConnection.consumer); 208 r->PrerollNode(mConnection.producer); 209 210 // start the LoggingConsumer and leave it running 211 BTimeSource* ts = r->MakeTimeSourceFor(mTimeSource); 212 r->StartNode(mConnection.consumer, ts->Now()); 213 ts->Release(); 214 } 215 break; 216 217 case BUTTON_START: 218 mStartButton->SetEnabled(false); 219 mStopButton->SetEnabled(true); 220 221 // start the consumer running 222 { 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; 230 } 231 break; 232 233 case BUTTON_STOP: 234 StopNodes(); 235 break; 236 237 default: 238 BWindow::MessageReceived(msg); 239 break; 240 } 241 } 242 243 // Private routines 244 void 245 NodeHarnessWin::StopNodes() 246 { 247 mStartButton->SetEnabled(true); 248 mStopButton->SetEnabled(false); 249 250 // stop the producer 251 { 252 BMediaRoster* r = BMediaRoster::Roster(); 253 r->StopNode(mConnection.producer, 0, true); // synchronous stop 254 } 255 } 256 257