1 |
/* $Id$ */ |
---|
2 |
|
---|
3 |
/* |
---|
4 |
* FreeBob Streaming API |
---|
5 |
* FreeBob = Firewire (pro-)audio for linux |
---|
6 |
* |
---|
7 |
* http://freebob.sf.net |
---|
8 |
* |
---|
9 |
* Copyright (C) 2005,2006 Pieter Palmers <pieterpalmers@users.sourceforge.net> |
---|
10 |
* |
---|
11 |
* This program is free software {} you can redistribute it and/or modify |
---|
12 |
* it under the terms of the GNU General Public License as published by |
---|
13 |
* the Free Software Foundation {} either version 2 of the License, or |
---|
14 |
* (at your option) any later version. |
---|
15 |
* |
---|
16 |
* This program is distributed in the hope that it will be useful, |
---|
17 |
* but WITHOUT ANY WARRANTY {} without even the implied warranty of |
---|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 |
* GNU General Public License for more details. |
---|
20 |
* |
---|
21 |
* You should have received a copy of the GNU General Public License |
---|
22 |
* along with this program {} if not, write to the Free Software |
---|
23 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
24 |
* |
---|
25 |
* |
---|
26 |
* |
---|
27 |
*/ |
---|
28 |
|
---|
29 |
#include "StreamProcessorManager.h" |
---|
30 |
#include "StreamProcessor.h" |
---|
31 |
#include "Port.h" |
---|
32 |
#include <errno.h> |
---|
33 |
#include <assert.h> |
---|
34 |
|
---|
35 |
|
---|
36 |
namespace FreebobStreaming { |
---|
37 |
|
---|
38 |
IMPL_DEBUG_MODULE( StreamProcessorManager, StreamProcessorManager, DEBUG_LEVEL_NORMAL ); |
---|
39 |
|
---|
40 |
StreamProcessorManager::StreamProcessorManager(unsigned int period, unsigned int nb_buffers) |
---|
41 |
: m_nb_buffers(nb_buffers), m_period(period), m_xruns(0), m_isoManager(0), m_nbperiods(0) { |
---|
42 |
|
---|
43 |
} |
---|
44 |
|
---|
45 |
StreamProcessorManager::~StreamProcessorManager() { |
---|
46 |
if (m_isoManager) delete m_isoManager; |
---|
47 |
|
---|
48 |
} |
---|
49 |
|
---|
50 |
/** |
---|
51 |
* Registers \ref processor with this manager. |
---|
52 |
* |
---|
53 |
* also registers it with the isohandlermanager |
---|
54 |
* |
---|
55 |
* be sure to call isohandlermanager->init() first! |
---|
56 |
* and be sure that the processors are also ->init()'ed |
---|
57 |
* |
---|
58 |
* @param processor |
---|
59 |
* @return true if successfull |
---|
60 |
*/ |
---|
61 |
bool StreamProcessorManager::registerProcessor(StreamProcessor *processor) |
---|
62 |
{ |
---|
63 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Registering processor (%p)\n",processor); |
---|
64 |
assert(processor); |
---|
65 |
assert(m_isoManager); |
---|
66 |
|
---|
67 |
if (processor->getType()==StreamProcessor::E_Receive) { |
---|
68 |
processor->setVerboseLevel(getDebugLevel()); // inherit debug level |
---|
69 |
|
---|
70 |
m_ReceiveProcessors.push_back(processor); |
---|
71 |
|
---|
72 |
processor->setManager(this); |
---|
73 |
|
---|
74 |
return true; |
---|
75 |
} |
---|
76 |
|
---|
77 |
if (processor->getType()==StreamProcessor::E_Transmit) { |
---|
78 |
processor->setVerboseLevel(getDebugLevel()); // inherit debug level |
---|
79 |
|
---|
80 |
m_TransmitProcessors.push_back(processor); |
---|
81 |
|
---|
82 |
processor->setManager(this); |
---|
83 |
|
---|
84 |
return true; |
---|
85 |
} |
---|
86 |
|
---|
87 |
debugFatal("Unsupported processor type!\n"); |
---|
88 |
|
---|
89 |
return false; |
---|
90 |
} |
---|
91 |
|
---|
92 |
bool StreamProcessorManager::unregisterProcessor(StreamProcessor *processor) |
---|
93 |
{ |
---|
94 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Unregistering processor (%p)\n",processor); |
---|
95 |
assert(processor); |
---|
96 |
|
---|
97 |
if (processor->getType()==StreamProcessor::E_Receive) { |
---|
98 |
|
---|
99 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
100 |
it != m_ReceiveProcessors.end(); |
---|
101 |
++it ) { |
---|
102 |
|
---|
103 |
if ( *it == processor ) { |
---|
104 |
m_ReceiveProcessors.erase(it); |
---|
105 |
|
---|
106 |
processor->clearManager(); |
---|
107 |
|
---|
108 |
if(!m_isoManager->unregisterStream(processor)) { |
---|
109 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister receive stream processor from the Iso manager\n"); |
---|
110 |
|
---|
111 |
return false; |
---|
112 |
|
---|
113 |
} |
---|
114 |
|
---|
115 |
return true; |
---|
116 |
} |
---|
117 |
} |
---|
118 |
} |
---|
119 |
|
---|
120 |
if (processor->getType()==StreamProcessor::E_Transmit) { |
---|
121 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
122 |
it != m_TransmitProcessors.end(); |
---|
123 |
++it ) { |
---|
124 |
|
---|
125 |
if ( *it == processor ) { |
---|
126 |
m_TransmitProcessors.erase(it); |
---|
127 |
|
---|
128 |
processor->clearManager(); |
---|
129 |
|
---|
130 |
if(!m_isoManager->unregisterStream(processor)) { |
---|
131 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister transmit stream processor from the Iso manager\n"); |
---|
132 |
|
---|
133 |
return false; |
---|
134 |
|
---|
135 |
} |
---|
136 |
|
---|
137 |
return true; |
---|
138 |
} |
---|
139 |
} |
---|
140 |
} |
---|
141 |
|
---|
142 |
debugFatal("Processor (%p) not found!\n",processor); |
---|
143 |
|
---|
144 |
return false; //not found |
---|
145 |
|
---|
146 |
} |
---|
147 |
|
---|
148 |
bool StreamProcessorManager::init() |
---|
149 |
{ |
---|
150 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
151 |
|
---|
152 |
// and the tread that runs the runner |
---|
153 |
m_streamingThread=new FreebobUtil::PosixThread(this, m_thread_realtime, m_thread_priority, PTHREAD_CANCEL_DEFERRED); |
---|
154 |
if(!m_streamingThread) { |
---|
155 |
debugFatal("Could not create streaming thread\n"); |
---|
156 |
return false; |
---|
157 |
} |
---|
158 |
|
---|
159 |
m_isoManager=new IsoHandlerManager(); |
---|
160 |
|
---|
161 |
if(!m_isoManager) { |
---|
162 |
debugFatal("Could not create IsoHandlerManager\n"); |
---|
163 |
return false; |
---|
164 |
} |
---|
165 |
|
---|
166 |
m_isoManager->setVerboseLevel(getDebugLevel()); |
---|
167 |
|
---|
168 |
if(!m_isoManager->Init()) { |
---|
169 |
debugFatal("Could not init IsoHandlerManager\n"); |
---|
170 |
return false; |
---|
171 |
} |
---|
172 |
|
---|
173 |
return true; |
---|
174 |
} |
---|
175 |
|
---|
176 |
bool StreamProcessorManager::Init() |
---|
177 |
{ |
---|
178 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Initializing runner...\n"); |
---|
179 |
|
---|
180 |
// no xrun has occurred (yet) |
---|
181 |
m_xrun_happened=false; |
---|
182 |
|
---|
183 |
if(sem_init(&m_period_semaphore, 0, 0)) { |
---|
184 |
debugFatal( "Cannot init packet transfer semaphore\n"); |
---|
185 |
debugFatal( " Error: %s\n",strerror(errno)); |
---|
186 |
return false; |
---|
187 |
} |
---|
188 |
|
---|
189 |
return true; |
---|
190 |
} |
---|
191 |
|
---|
192 |
bool StreamProcessorManager::prepare() { |
---|
193 |
|
---|
194 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n"); |
---|
195 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n"); |
---|
196 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
197 |
it != m_ReceiveProcessors.end(); |
---|
198 |
++it ) { |
---|
199 |
if(!(*it)->prepare()) { |
---|
200 |
debugFatal( " could not prepare (%p)...\n",(*it)); |
---|
201 |
return false; |
---|
202 |
|
---|
203 |
} |
---|
204 |
} |
---|
205 |
|
---|
206 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n"); |
---|
207 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
208 |
it != m_TransmitProcessors.end(); |
---|
209 |
++it ) { |
---|
210 |
if(!(*it)->prepare()) { |
---|
211 |
debugFatal( " could not prepare (%p)...\n",(*it)); |
---|
212 |
return false; |
---|
213 |
|
---|
214 |
} |
---|
215 |
|
---|
216 |
} |
---|
217 |
|
---|
218 |
return true; |
---|
219 |
} |
---|
220 |
|
---|
221 |
bool StreamProcessorManager::Execute() |
---|
222 |
{ |
---|
223 |
|
---|
224 |
bool period_ready=true; |
---|
225 |
bool xrun_has_occured=false; |
---|
226 |
bool this_period_ready; |
---|
227 |
|
---|
228 |
unsigned long tstamp_enter=debugGetCurrentTSC(); |
---|
229 |
|
---|
230 |
// debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "------------- EXECUTE -----------\n"); |
---|
231 |
|
---|
232 |
if(!m_isoManager->Execute()) { |
---|
233 |
debugFatal("Could not execute isoManager\n"); |
---|
234 |
return false; |
---|
235 |
} |
---|
236 |
|
---|
237 |
unsigned long tstamp_iso=debugGetCurrentTSC(); |
---|
238 |
|
---|
239 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " RCV PROC: "); |
---|
240 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
241 |
it != m_ReceiveProcessors.end(); |
---|
242 |
++it ) { |
---|
243 |
|
---|
244 |
this_period_ready = (*it)->isOnePeriodReady(); |
---|
245 |
period_ready = period_ready && this_period_ready; |
---|
246 |
// if (this_period_ready) { |
---|
247 |
// m_isoManager->disablePolling(*it); |
---|
248 |
// } |
---|
249 |
// |
---|
250 |
xrun_has_occured = xrun_has_occured || (*it)->xrunOccurred(); |
---|
251 |
debugOutputShort( DEBUG_LEVEL_VERY_VERBOSE, "(%d/%d/%d) ", period_ready, xrun_has_occured,(*it)->m_framecounter); |
---|
252 |
} |
---|
253 |
debugOutputShort( DEBUG_LEVEL_VERY_VERBOSE, "\n"); |
---|
254 |
|
---|
255 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, " XMIT PROC: "); |
---|
256 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
257 |
it != m_TransmitProcessors.end(); |
---|
258 |
++it ) { |
---|
259 |
this_period_ready = (*it)->isOnePeriodReady(); |
---|
260 |
period_ready = period_ready && this_period_ready; |
---|
261 |
// if (this_period_ready) { |
---|
262 |
// m_isoManager->disablePolling(*it); |
---|
263 |
// } |
---|
264 |
xrun_has_occured = xrun_has_occured || (*it)->xrunOccurred(); |
---|
265 |
debugOutputShort( DEBUG_LEVEL_VERY_VERBOSE, "(%d/%d/%d) ", period_ready, xrun_has_occured,(*it)->m_framecounter); |
---|
266 |
} |
---|
267 |
debugOutputShort( DEBUG_LEVEL_VERY_VERBOSE, "\n"); |
---|
268 |
|
---|
269 |
unsigned long tstamp_periodcheck=debugGetCurrentTSC(); |
---|
270 |
|
---|
271 |
if(xrun_has_occured) { |
---|
272 |
// do xrun signaling/handling |
---|
273 |
debugWarning("Streaming thread detected xrun\n"); |
---|
274 |
m_xruns++; |
---|
275 |
m_xrun_happened=true; |
---|
276 |
sem_post(&m_period_semaphore); |
---|
277 |
return false; // stop thread |
---|
278 |
} |
---|
279 |
|
---|
280 |
if(period_ready) { |
---|
281 |
// signal the waiting thread(s?) that a period is ready |
---|
282 |
sem_post(&m_period_semaphore); |
---|
283 |
debugOutputShort( DEBUG_LEVEL_VERY_VERBOSE, "Period done...\n"); |
---|
284 |
|
---|
285 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
286 |
it != m_ReceiveProcessors.end(); |
---|
287 |
++it ) { |
---|
288 |
(*it)->decrementFrameCounter(); |
---|
289 |
// m_isoManager->enablePolling(*it); |
---|
290 |
|
---|
291 |
} |
---|
292 |
|
---|
293 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
294 |
it != m_TransmitProcessors.end(); |
---|
295 |
++it ) { |
---|
296 |
(*it)->decrementFrameCounter(); |
---|
297 |
// m_isoManager->enablePolling(*it); |
---|
298 |
} |
---|
299 |
|
---|
300 |
m_nbperiods++; |
---|
301 |
} |
---|
302 |
|
---|
303 |
unsigned long tstamp_exit=debugGetCurrentTSC(); |
---|
304 |
|
---|
305 |
// debugOutput( DEBUG_LEVEL_VERBOSE, "EXECUTE TIME: ISO: %6d | PeriodCheck: %6d | FrameCounter: %6d \n", |
---|
306 |
// tstamp_iso-tstamp_enter, tstamp_periodcheck-tstamp_iso, tstamp_exit-tstamp_periodcheck |
---|
307 |
// ); |
---|
308 |
|
---|
309 |
return true; |
---|
310 |
|
---|
311 |
} |
---|
312 |
|
---|
313 |
bool StreamProcessorManager::start() { |
---|
314 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Starting Processors...\n"); |
---|
315 |
assert(m_isoManager); |
---|
316 |
|
---|
317 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Creating handlers for the StreamProcessors...\n"); |
---|
318 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n"); |
---|
319 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
320 |
it != m_ReceiveProcessors.end(); |
---|
321 |
++it ) { |
---|
322 |
if (!m_isoManager->registerStream(*it)) { |
---|
323 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Could not register receive stream processor (%p) with the Iso manager\n",*it); |
---|
324 |
return false; |
---|
325 |
} |
---|
326 |
|
---|
327 |
|
---|
328 |
} |
---|
329 |
|
---|
330 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n"); |
---|
331 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
332 |
it != m_TransmitProcessors.end(); |
---|
333 |
++it ) { |
---|
334 |
if (!m_isoManager->registerStream(*it)) { |
---|
335 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Could not register transmit stream processor (%p) with the Iso manager\n",*it); |
---|
336 |
return false; |
---|
337 |
} |
---|
338 |
|
---|
339 |
} |
---|
340 |
|
---|
341 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing IsoHandlerManager...\n"); |
---|
342 |
if (!m_isoManager->prepare()) { |
---|
343 |
debugFatal("Could not prepare isoManager\n"); |
---|
344 |
return false; |
---|
345 |
} |
---|
346 |
|
---|
347 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Starting IsoHandler...\n"); |
---|
348 |
if (!m_isoManager->startHandlers(0)) { |
---|
349 |
debugFatal("Could not start handlers...\n"); |
---|
350 |
return false; |
---|
351 |
} |
---|
352 |
|
---|
353 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Starting streaming thread...\n"); |
---|
354 |
|
---|
355 |
// start the runner thread |
---|
356 |
m_streamingThread->Start(); |
---|
357 |
|
---|
358 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Waiting for all StreamProcessors to start running...\n"); |
---|
359 |
// we have to wait until all streamprocessors indicate that they are running |
---|
360 |
// i.e. that there is actually some data stream flowing |
---|
361 |
int wait_cycles=2000; // two seconds |
---|
362 |
bool notRunning=true; |
---|
363 |
while (notRunning && wait_cycles) { |
---|
364 |
wait_cycles--; |
---|
365 |
notRunning=false; |
---|
366 |
|
---|
367 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
368 |
it != m_ReceiveProcessors.end(); |
---|
369 |
++it ) { |
---|
370 |
if(!(*it)->isRunning()) notRunning=true; |
---|
371 |
} |
---|
372 |
|
---|
373 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
374 |
it != m_TransmitProcessors.end(); |
---|
375 |
++it ) { |
---|
376 |
if(!(*it)->isRunning()) notRunning=true; |
---|
377 |
} |
---|
378 |
usleep(1000); |
---|
379 |
} |
---|
380 |
|
---|
381 |
if(!wait_cycles) { // timout has occurred |
---|
382 |
debugFatal("One or more streams are not starting up (timeout):\n"); |
---|
383 |
|
---|
384 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
385 |
it != m_ReceiveProcessors.end(); |
---|
386 |
++it ) { |
---|
387 |
if(!(*it)->isRunning()) { |
---|
388 |
debugFatal(" receive stream %p not running\n",*it); |
---|
389 |
} else { |
---|
390 |
debugFatal(" receive stream %p running\n",*it); |
---|
391 |
} |
---|
392 |
} |
---|
393 |
|
---|
394 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
395 |
it != m_TransmitProcessors.end(); |
---|
396 |
++it ) { |
---|
397 |
if(!(*it)->isRunning()) { |
---|
398 |
debugFatal(" transmit stream %p not running\n",*it); |
---|
399 |
} else { |
---|
400 |
debugFatal(" transmit stream %p running\n",*it); |
---|
401 |
} |
---|
402 |
} |
---|
403 |
return false; |
---|
404 |
} |
---|
405 |
|
---|
406 |
debugOutput( DEBUG_LEVEL_VERBOSE, "StreamProcessors running...\n"); |
---|
407 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting frame counters...\n"); |
---|
408 |
|
---|
409 |
// now we reset the frame counters |
---|
410 |
// FIXME: check how we are going to do sync |
---|
411 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
412 |
it != m_ReceiveProcessors.end(); |
---|
413 |
++it ) { |
---|
414 |
|
---|
415 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
416 |
(*it)->dumpInfo(); |
---|
417 |
} |
---|
418 |
|
---|
419 |
(*it)->reset(); |
---|
420 |
|
---|
421 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
422 |
(*it)->dumpInfo(); |
---|
423 |
} |
---|
424 |
|
---|
425 |
} |
---|
426 |
|
---|
427 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
428 |
it != m_TransmitProcessors.end(); |
---|
429 |
++it ) { |
---|
430 |
|
---|
431 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
432 |
(*it)->dumpInfo(); |
---|
433 |
} |
---|
434 |
|
---|
435 |
(*it)->reset(); |
---|
436 |
|
---|
437 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
438 |
(*it)->dumpInfo(); |
---|
439 |
} |
---|
440 |
} |
---|
441 |
|
---|
442 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Enabling StreamProcessors...\n"); |
---|
443 |
// and we enable the streamprocessors |
---|
444 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
445 |
it != m_ReceiveProcessors.end(); |
---|
446 |
++it ) { |
---|
447 |
(*it)->enable(); |
---|
448 |
m_isoManager->enablePolling(*it); |
---|
449 |
} |
---|
450 |
|
---|
451 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
452 |
it != m_TransmitProcessors.end(); |
---|
453 |
++it ) { |
---|
454 |
(*it)->enable(); |
---|
455 |
m_isoManager->enablePolling(*it); |
---|
456 |
} |
---|
457 |
|
---|
458 |
// dump the iso stream information when in verbose mode |
---|
459 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
460 |
m_isoManager->dumpInfo(); |
---|
461 |
} |
---|
462 |
|
---|
463 |
return true; |
---|
464 |
|
---|
465 |
} |
---|
466 |
|
---|
467 |
bool StreamProcessorManager::stop() { |
---|
468 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping...\n"); |
---|
469 |
assert(m_isoManager); |
---|
470 |
assert(m_streamingThread); |
---|
471 |
|
---|
472 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Waiting for all StreamProcessors to prepare to stop...\n"); |
---|
473 |
// Most stream processors can just stop without special treatment. However, some |
---|
474 |
// (like the MOTU) need to do a few things before it's safe to turn off the iso |
---|
475 |
// handling. |
---|
476 |
int wait_cycles=2000; // two seconds ought to be sufficient |
---|
477 |
bool allReady = false; |
---|
478 |
while (!allReady && wait_cycles) { |
---|
479 |
wait_cycles--; |
---|
480 |
allReady = true; |
---|
481 |
|
---|
482 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
483 |
it != m_ReceiveProcessors.end(); |
---|
484 |
++it ) { |
---|
485 |
if(!(*it)->preparedForStop()) allReady = false; |
---|
486 |
} |
---|
487 |
|
---|
488 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
489 |
it != m_TransmitProcessors.end(); |
---|
490 |
++it ) { |
---|
491 |
if(!(*it)->preparedForStop()) allReady = false; |
---|
492 |
} |
---|
493 |
usleep(1000); |
---|
494 |
} |
---|
495 |
|
---|
496 |
|
---|
497 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping thread...\n"); |
---|
498 |
|
---|
499 |
m_streamingThread->Stop(); |
---|
500 |
|
---|
501 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping handlers...\n"); |
---|
502 |
if(!m_isoManager->stopHandlers()) { |
---|
503 |
debugFatal("Could not stop ISO handlers\n"); |
---|
504 |
return false; |
---|
505 |
} |
---|
506 |
|
---|
507 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Unregistering processors from handlers...\n"); |
---|
508 |
// now unregister all streams from iso manager |
---|
509 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n"); |
---|
510 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
511 |
it != m_ReceiveProcessors.end(); |
---|
512 |
++it ) { |
---|
513 |
if (!m_isoManager->unregisterStream(*it)) { |
---|
514 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister receive stream processor (%p) from the Iso manager\n",*it); |
---|
515 |
return false; |
---|
516 |
} |
---|
517 |
|
---|
518 |
} |
---|
519 |
|
---|
520 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n"); |
---|
521 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
522 |
it != m_TransmitProcessors.end(); |
---|
523 |
++it ) { |
---|
524 |
if (!m_isoManager->unregisterStream(*it)) { |
---|
525 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Could not unregister transmit stream processor (%p) from the Iso manager\n",*it); |
---|
526 |
return false; |
---|
527 |
} |
---|
528 |
|
---|
529 |
} |
---|
530 |
|
---|
531 |
return true; |
---|
532 |
|
---|
533 |
} |
---|
534 |
|
---|
535 |
bool StreamProcessorManager::waitForPeriod() { |
---|
536 |
|
---|
537 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "enter...\n"); |
---|
538 |
|
---|
539 |
// Wait for packetizer thread to signal a period completion |
---|
540 |
sem_wait(&m_period_semaphore); |
---|
541 |
|
---|
542 |
if(m_xrun_happened) { |
---|
543 |
debugWarning("Detected underrun\n"); |
---|
544 |
dumpInfo(); |
---|
545 |
return false; |
---|
546 |
} |
---|
547 |
|
---|
548 |
return true; |
---|
549 |
|
---|
550 |
} |
---|
551 |
|
---|
552 |
bool StreamProcessorManager::handleXrun() { |
---|
553 |
|
---|
554 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Handling Xrun ...\n"); |
---|
555 |
|
---|
556 |
/* |
---|
557 |
* Reset means: |
---|
558 |
* 1) Stopping the packetizer thread |
---|
559 |
* 2) Bringing all buffers & streamprocessors into a know state |
---|
560 |
* - Clear all capture buffers |
---|
561 |
* - Put nb_periods*period_size of null frames into the playback buffers |
---|
562 |
* 3) Restarting the packetizer thread |
---|
563 |
*/ |
---|
564 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping processormanager...\n"); |
---|
565 |
if(!stop()) { |
---|
566 |
debugFatal("Could not stop.\n"); |
---|
567 |
return false; |
---|
568 |
} |
---|
569 |
|
---|
570 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting Processors...\n"); |
---|
571 |
|
---|
572 |
// now we reset the frame counters |
---|
573 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
574 |
it != m_ReceiveProcessors.end(); |
---|
575 |
++it ) { |
---|
576 |
|
---|
577 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
578 |
(*it)->dumpInfo(); |
---|
579 |
} |
---|
580 |
|
---|
581 |
(*it)->reset(); |
---|
582 |
|
---|
583 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
584 |
(*it)->dumpInfo(); |
---|
585 |
} |
---|
586 |
|
---|
587 |
} |
---|
588 |
|
---|
589 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
590 |
it != m_TransmitProcessors.end(); |
---|
591 |
++it ) { |
---|
592 |
|
---|
593 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
594 |
(*it)->dumpInfo(); |
---|
595 |
} |
---|
596 |
|
---|
597 |
(*it)->reset(); |
---|
598 |
|
---|
599 |
if(getDebugLevel()>=DEBUG_LEVEL_VERBOSE) { |
---|
600 |
(*it)->dumpInfo(); |
---|
601 |
} |
---|
602 |
} |
---|
603 |
|
---|
604 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Starting processormanager...\n"); |
---|
605 |
|
---|
606 |
if(!start()) { |
---|
607 |
debugFatal("Could not start.\n"); |
---|
608 |
return false; |
---|
609 |
} |
---|
610 |
|
---|
611 |
|
---|
612 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Xrun handled...\n"); |
---|
613 |
|
---|
614 |
|
---|
615 |
return true; |
---|
616 |
} |
---|
617 |
|
---|
618 |
bool StreamProcessorManager::transfer() { |
---|
619 |
|
---|
620 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Transferring period...\n"); |
---|
621 |
|
---|
622 |
// a static cast could make sure that there is no performance |
---|
623 |
// penalty for the virtual functions (to be checked) |
---|
624 |
|
---|
625 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
626 |
it != m_ReceiveProcessors.end(); |
---|
627 |
++it ) { |
---|
628 |
if(!(*it)->transfer()) { |
---|
629 |
debugFatal("could not transfer() stream processor (%p)",*it); |
---|
630 |
return false; |
---|
631 |
} |
---|
632 |
} |
---|
633 |
|
---|
634 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
635 |
it != m_TransmitProcessors.end(); |
---|
636 |
++it ) { |
---|
637 |
if(!(*it)->transfer()) { |
---|
638 |
debugFatal("could not transfer() stream processor (%p)",*it); |
---|
639 |
return false; |
---|
640 |
} |
---|
641 |
} |
---|
642 |
|
---|
643 |
return true; |
---|
644 |
} |
---|
645 |
|
---|
646 |
bool StreamProcessorManager::transfer(enum StreamProcessor::EProcessorType t) { |
---|
647 |
|
---|
648 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Transferring period...\n"); |
---|
649 |
|
---|
650 |
// a static cast could make sure that there is no performance |
---|
651 |
// penalty for the virtual functions (to be checked) |
---|
652 |
if (t==StreamProcessor::E_Receive) { |
---|
653 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
654 |
it != m_ReceiveProcessors.end(); |
---|
655 |
++it ) { |
---|
656 |
if(!(*it)->transfer()) { |
---|
657 |
debugFatal("could not transfer() stream processor (%p)",*it); |
---|
658 |
return false; |
---|
659 |
} |
---|
660 |
} |
---|
661 |
} else { |
---|
662 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
663 |
it != m_TransmitProcessors.end(); |
---|
664 |
++it ) { |
---|
665 |
if(!(*it)->transfer()) { |
---|
666 |
debugFatal("could not transfer() stream processor (%p)",*it); |
---|
667 |
return false; |
---|
668 |
} |
---|
669 |
} |
---|
670 |
} |
---|
671 |
|
---|
672 |
return true; |
---|
673 |
} |
---|
674 |
|
---|
675 |
void StreamProcessorManager::dumpInfo() { |
---|
676 |
debugOutputShort( DEBUG_LEVEL_NORMAL, "----------------------------------------------------\n"); |
---|
677 |
debugOutputShort( DEBUG_LEVEL_NORMAL, "Dumping StreamProcessorManager information...\n"); |
---|
678 |
debugOutputShort( DEBUG_LEVEL_NORMAL, "Period count: %d\n", m_nbperiods); |
---|
679 |
|
---|
680 |
debugOutputShort( DEBUG_LEVEL_NORMAL, " Receive processors...\n"); |
---|
681 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
682 |
it != m_ReceiveProcessors.end(); |
---|
683 |
++it ) { |
---|
684 |
(*it)->dumpInfo(); |
---|
685 |
} |
---|
686 |
|
---|
687 |
debugOutputShort( DEBUG_LEVEL_NORMAL, " Transmit processors...\n"); |
---|
688 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
689 |
it != m_TransmitProcessors.end(); |
---|
690 |
++it ) { |
---|
691 |
(*it)->dumpInfo(); |
---|
692 |
} |
---|
693 |
|
---|
694 |
debugOutputShort( DEBUG_LEVEL_NORMAL, "Iso handler info:\n"); |
---|
695 |
m_isoManager->dumpInfo(); |
---|
696 |
debugOutputShort( DEBUG_LEVEL_NORMAL, "----------------------------------------------------\n"); |
---|
697 |
|
---|
698 |
} |
---|
699 |
|
---|
700 |
void StreamProcessorManager::setVerboseLevel(int l) { |
---|
701 |
setDebugLevel(l); |
---|
702 |
|
---|
703 |
if (m_isoManager) m_isoManager->setVerboseLevel(l); |
---|
704 |
|
---|
705 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Receive processors...\n"); |
---|
706 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
707 |
it != m_ReceiveProcessors.end(); |
---|
708 |
++it ) { |
---|
709 |
(*it)->setVerboseLevel(l); |
---|
710 |
} |
---|
711 |
|
---|
712 |
debugOutput( DEBUG_LEVEL_VERBOSE, " Transmit processors...\n"); |
---|
713 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
714 |
it != m_TransmitProcessors.end(); |
---|
715 |
++it ) { |
---|
716 |
(*it)->setVerboseLevel(l); |
---|
717 |
} |
---|
718 |
} |
---|
719 |
|
---|
720 |
|
---|
721 |
int StreamProcessorManager::getPortCount(enum Port::E_PortType type, enum Port::E_Direction direction) { |
---|
722 |
int count=0; |
---|
723 |
|
---|
724 |
if (direction == Port::E_Capture) { |
---|
725 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
726 |
it != m_ReceiveProcessors.end(); |
---|
727 |
++it ) { |
---|
728 |
count += (*it)->getPortCount(type); |
---|
729 |
} |
---|
730 |
} else { |
---|
731 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
732 |
it != m_TransmitProcessors.end(); |
---|
733 |
++it ) { |
---|
734 |
count += (*it)->getPortCount(type); |
---|
735 |
} |
---|
736 |
} |
---|
737 |
return count; |
---|
738 |
} |
---|
739 |
|
---|
740 |
int StreamProcessorManager::getPortCount(enum Port::E_Direction direction) { |
---|
741 |
int count=0; |
---|
742 |
|
---|
743 |
if (direction == Port::E_Capture) { |
---|
744 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
745 |
it != m_ReceiveProcessors.end(); |
---|
746 |
++it ) { |
---|
747 |
count += (*it)->getPortCount(); |
---|
748 |
} |
---|
749 |
} else { |
---|
750 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
751 |
it != m_TransmitProcessors.end(); |
---|
752 |
++it ) { |
---|
753 |
count += (*it)->getPortCount(); |
---|
754 |
} |
---|
755 |
} |
---|
756 |
return count; |
---|
757 |
} |
---|
758 |
|
---|
759 |
// TODO: implement a port map here, instead of the loop |
---|
760 |
|
---|
761 |
Port* StreamProcessorManager::getPortByIndex(int idx, enum Port::E_Direction direction) { |
---|
762 |
int count=0; |
---|
763 |
int prevcount=0; |
---|
764 |
|
---|
765 |
if (direction == Port::E_Capture) { |
---|
766 |
for ( StreamProcessorVectorIterator it = m_ReceiveProcessors.begin(); |
---|
767 |
it != m_ReceiveProcessors.end(); |
---|
768 |
++it ) { |
---|
769 |
count += (*it)->getPortCount(); |
---|
770 |
if (count > idx) { |
---|
771 |
return (*it)->getPortAtIdx(idx-prevcount); |
---|
772 |
} |
---|
773 |
prevcount=count; |
---|
774 |
} |
---|
775 |
} else { |
---|
776 |
for ( StreamProcessorVectorIterator it = m_TransmitProcessors.begin(); |
---|
777 |
it != m_TransmitProcessors.end(); |
---|
778 |
++it ) { |
---|
779 |
count += (*it)->getPortCount(); |
---|
780 |
if (count > idx) { |
---|
781 |
return (*it)->getPortAtIdx(idx-prevcount); |
---|
782 |
} |
---|
783 |
prevcount=count; |
---|
784 |
} |
---|
785 |
} |
---|
786 |
return NULL; |
---|
787 |
} |
---|
788 |
|
---|
789 |
bool StreamProcessorManager::setThreadParameters(bool rt, int priority) { |
---|
790 |
m_thread_realtime=rt; |
---|
791 |
m_thread_priority=priority; |
---|
792 |
return true; |
---|
793 |
} |
---|
794 |
|
---|
795 |
|
---|
796 |
} // end of namespace |
---|