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 <Catalog.h> 41 #include <Entry.h> 42 #include <media/MediaRoster.h> 43 #include <media/MediaAddOn.h> 44 #include <media/TimeSource.h> 45 #include <media/MediaTheme.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 49 50 #undef B_TRANSLATION_CONTEXT 51 #define B_TRANSLATION_CONTEXT "CortexAddOnsLoggingConsumerNodeHarnessWin" 52 53 54 const int32 BUTTON_CONNECT = 'Cnct'; 55 const int32 BUTTON_START = 'Strt'; 56 const int32 BUTTON_STOP = 'Stop'; 57 58 #define TEST_WITH_AUDIO 1 59 60 // -------------------- 61 // static utility functions 62 static void ErrorCheck(status_t err, const char* msg) 63 { 64 if (err) 65 { 66 fprintf(stderr, "* FATAL ERROR (%s): %s\n", strerror(err), msg); 67 exit(1); 68 } 69 } 70 71 // -------------------- 72 // NodeHarnessWin implementation 73 NodeHarnessWin::NodeHarnessWin(BRect frame, const char *title) 74 : BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS), 75 mLogNode(NULL), mIsConnected(false), mIsRunning(false) 76 { 77 // build the UI 78 BRect r(10, 10, 100, 40); 79 mConnectButton = new BButton(r, "Connect", B_TRANSLATE("Connect"), 80 new BMessage(BUTTON_CONNECT)); 81 mConnectButton->SetEnabled(true); 82 AddChild(mConnectButton); 83 r.OffsetBy(0, 40); 84 mStartButton = new BButton(r, "Start", B_TRANSLATE("Start"), 85 new BMessage(BUTTON_START)); 86 mStartButton->SetEnabled(false); 87 AddChild(mStartButton); 88 r.OffsetBy(0, 40); 89 mStopButton = new BButton(r, "Stop", B_TRANSLATE("Stop"), 90 new BMessage(BUTTON_STOP)); 91 mStopButton->SetEnabled(false); 92 AddChild(mStopButton); 93 } 94 95 NodeHarnessWin::~NodeHarnessWin() 96 { 97 BMediaRoster* r = BMediaRoster::Roster(); 98 99 // tear down the node network 100 if (mIsRunning) StopNodes(); 101 if (mIsConnected) 102 { 103 printf("Total late buffers: %ld\n", mLogNode->LateBuffers()); 104 r->StopNode(mConnection.consumer, 0, true); 105 r->Disconnect(mConnection.producer.node, mConnection.source, 106 mConnection.consumer.node, mConnection.destination); 107 r->ReleaseNode(mConnection.producer); 108 r->ReleaseNode(mConnection.consumer); 109 } 110 } 111 112 void 113 NodeHarnessWin::Quit() 114 { 115 be_app->PostMessage(B_QUIT_REQUESTED); 116 BWindow::Quit(); 117 } 118 119 void 120 NodeHarnessWin::MessageReceived(BMessage *msg) 121 { 122 status_t err; 123 124 switch (msg->what) 125 { 126 case BUTTON_CONNECT: 127 mIsConnected = true; 128 129 // set the button states appropriately 130 mConnectButton->SetEnabled(false); 131 mStartButton->SetEnabled(true); 132 133 // set up the node network 134 { 135 BMediaRoster* r = BMediaRoster::Roster(); 136 137 // find a node that can handle an audio file 138 #if TEST_WITH_AUDIO 139 entry_ref inRef; 140 dormant_node_info info; 141 142 ::get_ref_for_path("/boot/optional/sound/virtual (void)", &inRef); 143 err = r->SniffRef(inRef, B_BUFFER_PRODUCER | B_FILE_INTERFACE, &info); 144 ErrorCheck(err, "couldn't find file reader node\n"); 145 146 err = r->InstantiateDormantNode(info, &mConnection.producer, B_FLAVOR_IS_LOCAL); 147 ErrorCheck(err, "couldn't instantiate file reader node\n"); 148 149 bigtime_t dummy_length; // output = media length; we don't use it 150 err = r->SetRefFor(mConnection.producer, inRef, false, &dummy_length); 151 ErrorCheck(err, "unable to SetRefFor() to read that sound file!\n"); 152 #else 153 r->GetVideoInput(&mConnection.producer); 154 #endif 155 156 entry_ref logRef; 157 ::get_ref_for_path("/tmp/node_log", &logRef); 158 159 mLogNode = new LoggingConsumer(logRef); 160 err = r->RegisterNode(mLogNode); 161 ErrorCheck(err, "unable to register LoggingConsumer node!\n"); 162 // make sure the Media Roster knows that we're using the node 163 r->GetNodeFor(mLogNode->Node().node, &mConnection.consumer); 164 165 // trim down the log's verbosity a touch 166 mLogNode->SetEnabled(LOG_HANDLE_EVENT, false); 167 168 // fire off a window with the LoggingConsumer's controls in it 169 BParameterWeb* web; 170 r->GetParameterWebFor(mConnection.consumer, &web); 171 BView* view = BMediaTheme::ViewFor(web); 172 BWindow* win = new BWindow(BRect(250, 200, 300, 300), 173 B_TRANSLATE("Controls"), B_TITLED_WINDOW, 174 B_ASYNCHRONOUS_CONTROLS); 175 win->AddChild(view); 176 win->ResizeTo(view->Bounds().Width(), view->Bounds().Height()); 177 win->Show(); 178 179 // set the nodes' time sources 180 r->GetTimeSource(&mTimeSource); 181 r->SetTimeSourceFor(mConnection.consumer.node, mTimeSource.node); 182 r->SetTimeSourceFor(mConnection.producer.node, mTimeSource.node); 183 184 // got the nodes; now we find the endpoints of the connection 185 media_input logInput; 186 media_output soundOutput; 187 int32 count; 188 err = r->GetFreeOutputsFor(mConnection.producer, &soundOutput, 1, &count); 189 ErrorCheck(err, "unable to get a free output from the producer node"); 190 err = r->GetFreeInputsFor(mConnection.consumer, &logInput, 1, &count); 191 ErrorCheck(err, "unable to get a free input to the LoggingConsumer"); 192 193 // fill in the rest of the Connection object 194 mConnection.source = soundOutput.source; 195 mConnection.destination = logInput.destination; 196 197 // got the endpoints; now we connect it! 198 media_format format; 199 #if TEST_WITH_AUDIO 200 format.type = B_MEDIA_RAW_AUDIO; // !!! hmmm.. how to fully wildcard this? 201 format.u.raw_audio = media_raw_audio_format::wildcard; 202 #else 203 format.type = B_MEDIA_RAW_VIDEO; // !!! hmmm.. how to fully wildcard this? 204 format.u.raw_video = media_raw_video_format::wildcard; 205 #endif 206 err = r->Connect(mConnection.source, mConnection.destination, &format, &soundOutput, &logInput); 207 ErrorCheck(err, "unable to connect nodes"); 208 mConnection.format = format; 209 210 // for video input, we need to set the downstream latency for record -> playback 211 bigtime_t latency; 212 r->GetLatencyFor(mConnection.producer, &latency); 213 printf("Setting producer run mode latency to %" B_PRIdBIGTIME "\n", latency); 214 r->SetProducerRunModeDelay(mConnection.producer, latency + 6000); 215 216 // preroll first, to be a good citizen 217 r->PrerollNode(mConnection.consumer); 218 r->PrerollNode(mConnection.producer); 219 220 // start the LoggingConsumer and leave it running 221 BTimeSource* ts = r->MakeTimeSourceFor(mTimeSource); 222 r->StartNode(mConnection.consumer, ts->Now()); 223 ts->Release(); 224 } 225 break; 226 227 case BUTTON_START: 228 mStartButton->SetEnabled(false); 229 mStopButton->SetEnabled(true); 230 231 // start the consumer running 232 { 233 bigtime_t latency; 234 BMediaRoster* r = BMediaRoster::Roster(); 235 BTimeSource* ts = r->MakeTimeSourceFor(mConnection.consumer); 236 r->GetLatencyFor(mConnection.producer, &latency); 237 r->StartNode(mConnection.producer, ts->Now() + latency); 238 ts->Release(); 239 mIsRunning = true; 240 } 241 break; 242 243 case BUTTON_STOP: 244 StopNodes(); 245 break; 246 247 default: 248 BWindow::MessageReceived(msg); 249 break; 250 } 251 } 252 253 // Private routines 254 void 255 NodeHarnessWin::StopNodes() 256 { 257 mStartButton->SetEnabled(true); 258 mStopButton->SetEnabled(false); 259 260 // stop the producer 261 { 262 BMediaRoster* r = BMediaRoster::Roster(); 263 r->StopNode(mConnection.producer, 0, true); // synchronous stop 264 } 265 } 266 267