2 * Copyright 1991-1999, Be Incorporated.
3 * Copyright (c) 1999-2000, Eric Moon.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
35 #include "NodeHarnessWin.h"
36 #include "LoggingConsumer.h"
37 #include <app/Application.h>
38 #include <interface/Button.h>
39 //#include <storage/Entry.h>
41 #include <media/MediaRoster.h>
42 #include <media/MediaAddOn.h>
43 #include <media/TimeSource.h>
44 #include <media/MediaTheme.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
)
60 fprintf(stderr
, "* FATAL ERROR (%s): %s\n", strerror(err
), msg
);
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)
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
);
77 mStartButton
= new BButton(r
, "Start", "Start", new BMessage(BUTTON_START
));
78 mStartButton
->SetEnabled(false);
79 AddChild(mStartButton
);
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();
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
);
104 NodeHarnessWin::Quit()
106 be_app
->PostMessage(B_QUIT_REQUESTED
);
111 NodeHarnessWin::MessageReceived(BMessage
*msg
)
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
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");
144 r
->GetVideoInput(&mConnection
.producer
);
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
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
);
166 win
->ResizeTo(view
->Bounds().Width(), view
->Bounds().Height());
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
;
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!
190 format
.type
= B_MEDIA_RAW_AUDIO
; // !!! hmmm.. how to fully wildcard this?
191 format
.u
.raw_audio
= media_raw_audio_format::wildcard
;
193 format
.type
= B_MEDIA_RAW_VIDEO
; // !!! hmmm.. how to fully wildcard this?
194 format
.u
.raw_video
= media_raw_video_format::wildcard
;
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
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());
218 mStartButton
->SetEnabled(false);
219 mStopButton
->SetEnabled(true);
221 // start the consumer running
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
);
238 BWindow::MessageReceived(msg
);
245 NodeHarnessWin::StopNodes()
247 mStartButton
->SetEnabled(true);
248 mStopButton
->SetEnabled(false);
252 BMediaRoster
* r
= BMediaRoster::Roster();
253 r
->StopNode(mConnection
.producer
, 0, true); // synchronous stop