1 |
/* |
---|
2 |
* Copyright (C) 2005-2007 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB. |
---|
8 |
* |
---|
9 |
* This library is free software; you can redistribute it and/or |
---|
10 |
* modify it under the terms of the GNU Lesser General Public |
---|
11 |
* License version 2.1, as published by the Free Software Foundation; |
---|
12 |
* |
---|
13 |
* This library is distributed in the hope that it will be useful, |
---|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 |
* Lesser General Public License for more details. |
---|
17 |
* |
---|
18 |
* You should have received a copy of the GNU Lesser General Public |
---|
19 |
* License along with this library; if not, write to the Free Software |
---|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
21 |
* MA 02110-1301 USA |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#include "IsoHandlerManager.h" |
---|
25 |
#include "IsoHandler.h" |
---|
26 |
#include "../generic/IsoStream.h" |
---|
27 |
|
---|
28 |
#include "libutil/PosixThread.h" |
---|
29 |
|
---|
30 |
#include <assert.h> |
---|
31 |
|
---|
32 |
#define MINIMUM_INTERRUPTS_PER_PERIOD 4U |
---|
33 |
#define PACKETS_PER_INTERRUPT 4U |
---|
34 |
|
---|
35 |
namespace Streaming |
---|
36 |
{ |
---|
37 |
|
---|
38 |
IMPL_DEBUG_MODULE( IsoHandlerManager, IsoHandlerManager, DEBUG_LEVEL_NORMAL ); |
---|
39 |
|
---|
40 |
IsoHandlerManager::IsoHandlerManager() : |
---|
41 |
m_State(E_Created), |
---|
42 |
m_poll_timeout(100), m_poll_fds(0), m_poll_nfds(0), |
---|
43 |
m_realtime(false), m_priority(0) |
---|
44 |
{ |
---|
45 |
|
---|
46 |
} |
---|
47 |
|
---|
48 |
IsoHandlerManager::IsoHandlerManager(bool run_rt, unsigned int rt_prio) : |
---|
49 |
m_State(E_Created), |
---|
50 |
m_poll_timeout(1), m_poll_fds(0), m_poll_nfds(0), |
---|
51 |
m_realtime(run_rt), m_priority(rt_prio) |
---|
52 |
{ |
---|
53 |
|
---|
54 |
} |
---|
55 |
|
---|
56 |
IsoHandlerManager::~IsoHandlerManager() |
---|
57 |
{ |
---|
58 |
|
---|
59 |
} |
---|
60 |
|
---|
61 |
bool IsoHandlerManager::init() |
---|
62 |
{ |
---|
63 |
// the tread that performs the actual packet transfer |
---|
64 |
// needs high priority |
---|
65 |
unsigned int prio=m_priority+6; |
---|
66 |
|
---|
67 |
if (prio>98) prio=98; |
---|
68 |
|
---|
69 |
m_isoManagerThread=new Util::PosixThread( |
---|
70 |
this, |
---|
71 |
m_realtime, prio, |
---|
72 |
PTHREAD_CANCEL_DEFERRED); |
---|
73 |
|
---|
74 |
if(!m_isoManagerThread) { |
---|
75 |
debugFatal("Could not create iso manager thread\n"); |
---|
76 |
return false; |
---|
77 |
} |
---|
78 |
|
---|
79 |
// propagate the debug level |
---|
80 |
// m_isoManagerThread->setVerboseLevel(getDebugLevel()); |
---|
81 |
|
---|
82 |
return true; |
---|
83 |
} |
---|
84 |
|
---|
85 |
bool IsoHandlerManager::Init() |
---|
86 |
{ |
---|
87 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
88 |
pthread_mutex_init(&m_debug_lock, NULL); |
---|
89 |
|
---|
90 |
return true; |
---|
91 |
} |
---|
92 |
|
---|
93 |
/** |
---|
94 |
* the IsoHandlerManager thread execute function iterates the handlers. |
---|
95 |
* |
---|
96 |
* This means that once the thread is running, streams are |
---|
97 |
* transmitted and received (if present on the bus). Make sure |
---|
98 |
* that the clients are registered & ready before starting the |
---|
99 |
* thread! |
---|
100 |
* |
---|
101 |
* The register and unregister functions are thread unsafe, so |
---|
102 |
* should not be used when the thread is running. |
---|
103 |
* |
---|
104 |
* @return false if the handlers could not be iterated. |
---|
105 |
*/ |
---|
106 |
bool IsoHandlerManager::Execute() |
---|
107 |
{ |
---|
108 |
// updateCycleTimers(); |
---|
109 |
|
---|
110 |
pthread_mutex_lock(&m_debug_lock); |
---|
111 |
|
---|
112 |
if(!iterate()) { |
---|
113 |
debugFatal("Could not iterate the isoManager\n"); |
---|
114 |
pthread_mutex_unlock(&m_debug_lock); |
---|
115 |
return false; |
---|
116 |
} |
---|
117 |
|
---|
118 |
pthread_mutex_unlock(&m_debug_lock); |
---|
119 |
|
---|
120 |
return true; |
---|
121 |
} |
---|
122 |
|
---|
123 |
/** |
---|
124 |
* Poll the handlers managed by this manager, and iterate them |
---|
125 |
* when ready |
---|
126 |
* |
---|
127 |
* @return true when successful |
---|
128 |
*/ |
---|
129 |
bool IsoHandlerManager::iterate() |
---|
130 |
{ |
---|
131 |
int err; |
---|
132 |
int i=0; |
---|
133 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "enter...\n"); |
---|
134 |
|
---|
135 |
err = poll (m_poll_fds, m_poll_nfds, m_poll_timeout); |
---|
136 |
|
---|
137 |
if (err == -1) { |
---|
138 |
if (errno == EINTR) { |
---|
139 |
return true; |
---|
140 |
} |
---|
141 |
debugFatal("poll error: %s\n", strerror (errno)); |
---|
142 |
return false; |
---|
143 |
} |
---|
144 |
|
---|
145 |
for (i = 0; i < m_poll_nfds; i++) { |
---|
146 |
if (m_poll_fds[i].revents & POLLERR) { |
---|
147 |
debugWarning("error on fd for %d\n",i); |
---|
148 |
} |
---|
149 |
|
---|
150 |
if (m_poll_fds[i].revents & POLLHUP) { |
---|
151 |
debugWarning("hangup on fd for %d\n",i); |
---|
152 |
} |
---|
153 |
|
---|
154 |
if(m_poll_fds[i].revents & (POLLIN)) { |
---|
155 |
IsoHandler *s=m_IsoHandlers.at(i); |
---|
156 |
assert(s); |
---|
157 |
|
---|
158 |
s->iterate(); |
---|
159 |
} |
---|
160 |
} |
---|
161 |
|
---|
162 |
return true; |
---|
163 |
|
---|
164 |
} |
---|
165 |
|
---|
166 |
bool IsoHandlerManager::registerHandler(IsoHandler *handler) |
---|
167 |
{ |
---|
168 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
169 |
assert(handler); |
---|
170 |
|
---|
171 |
m_IsoHandlers.push_back(handler); |
---|
172 |
|
---|
173 |
handler->setVerboseLevel(getDebugLevel()); |
---|
174 |
|
---|
175 |
// rebuild the fd map for poll()'ing. |
---|
176 |
return rebuildFdMap(); |
---|
177 |
|
---|
178 |
} |
---|
179 |
|
---|
180 |
bool IsoHandlerManager::unregisterHandler(IsoHandler *handler) |
---|
181 |
{ |
---|
182 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
183 |
assert(handler); |
---|
184 |
|
---|
185 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
186 |
it != m_IsoHandlers.end(); |
---|
187 |
++it ) |
---|
188 |
{ |
---|
189 |
if ( *it == handler ) { |
---|
190 |
// erase the iso handler from the list |
---|
191 |
m_IsoHandlers.erase(it); |
---|
192 |
// rebuild the fd map for poll()'ing. |
---|
193 |
return rebuildFdMap(); |
---|
194 |
} |
---|
195 |
} |
---|
196 |
debugFatal("Could not find handler (%p)\n", handler); |
---|
197 |
|
---|
198 |
return false; //not found |
---|
199 |
|
---|
200 |
} |
---|
201 |
|
---|
202 |
bool IsoHandlerManager::rebuildFdMap() { |
---|
203 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
204 |
int i=0; |
---|
205 |
|
---|
206 |
m_poll_nfds=0; |
---|
207 |
if(m_poll_fds) free(m_poll_fds); |
---|
208 |
|
---|
209 |
// count the number of handlers |
---|
210 |
m_poll_nfds=m_IsoHandlers.size(); |
---|
211 |
|
---|
212 |
// allocate the fd array |
---|
213 |
m_poll_fds = (struct pollfd *) calloc (m_poll_nfds, sizeof (struct pollfd)); |
---|
214 |
if(!m_poll_fds) { |
---|
215 |
debugFatal("Could not allocate memory for poll FD array\n"); |
---|
216 |
return false; |
---|
217 |
} |
---|
218 |
|
---|
219 |
// fill the fd map |
---|
220 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
221 |
it != m_IsoHandlers.end(); |
---|
222 |
++it ) |
---|
223 |
{ |
---|
224 |
m_poll_fds[i].fd=(*it)->getFileDescriptor(); |
---|
225 |
m_poll_fds[i].events = POLLIN; |
---|
226 |
i++; |
---|
227 |
} |
---|
228 |
|
---|
229 |
return true; |
---|
230 |
} |
---|
231 |
|
---|
232 |
void IsoHandlerManager::disablePolling(IsoStream *stream) { |
---|
233 |
int i=0; |
---|
234 |
|
---|
235 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "Disable polling on stream %p\n",stream); |
---|
236 |
|
---|
237 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
238 |
it != m_IsoHandlers.end(); |
---|
239 |
++it ) |
---|
240 |
{ |
---|
241 |
if ((*it)->isStreamRegistered(stream)) { |
---|
242 |
m_poll_fds[i].events = 0; |
---|
243 |
m_poll_fds[i].revents = 0; |
---|
244 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "polling disabled\n"); |
---|
245 |
} |
---|
246 |
|
---|
247 |
i++; |
---|
248 |
} |
---|
249 |
} |
---|
250 |
|
---|
251 |
void IsoHandlerManager::enablePolling(IsoStream *stream) { |
---|
252 |
int i=0; |
---|
253 |
|
---|
254 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "Enable polling on stream %p\n",stream); |
---|
255 |
|
---|
256 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
257 |
it != m_IsoHandlers.end(); |
---|
258 |
++it ) |
---|
259 |
{ |
---|
260 |
if ((*it)->isStreamRegistered(stream)) { |
---|
261 |
m_poll_fds[i].events = POLLIN; |
---|
262 |
m_poll_fds[i].revents = 0; |
---|
263 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "polling enabled\n"); |
---|
264 |
} |
---|
265 |
|
---|
266 |
i++; |
---|
267 |
} |
---|
268 |
} |
---|
269 |
|
---|
270 |
|
---|
271 |
/** |
---|
272 |
* Registers an IsoStream with the IsoHandlerManager. |
---|
273 |
* |
---|
274 |
* If nescessary, an IsoHandler is created to handle this stream. |
---|
275 |
* Once an IsoStream is registered to the handler, it will be included |
---|
276 |
* in the ISO streaming cycle (i.e. receive/transmit of it will occur). |
---|
277 |
* |
---|
278 |
* @param stream the stream to register |
---|
279 |
* @return true if registration succeeds |
---|
280 |
* |
---|
281 |
* \todo : currently there is a one-to-one mapping |
---|
282 |
* between streams and handlers, this is not ok for |
---|
283 |
* multichannel receive |
---|
284 |
*/ |
---|
285 |
bool IsoHandlerManager::registerStream(IsoStream *stream) |
---|
286 |
{ |
---|
287 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Registering stream %p\n",stream); |
---|
288 |
assert(stream); |
---|
289 |
|
---|
290 |
// make sure the stream isn't already attached to a handler |
---|
291 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
292 |
it != m_IsoHandlers.end(); |
---|
293 |
++it ) |
---|
294 |
{ |
---|
295 |
if((*it)->isStreamRegistered(stream)) { |
---|
296 |
debugWarning( "stream already registered!\n"); |
---|
297 |
(*it)->unregisterStream(stream); |
---|
298 |
|
---|
299 |
} |
---|
300 |
} |
---|
301 |
|
---|
302 |
// clean up all handlers that aren't used |
---|
303 |
pruneHandlers(); |
---|
304 |
|
---|
305 |
// allocate a handler for this stream |
---|
306 |
if (stream->getStreamType()==IsoStream::eST_Receive) { |
---|
307 |
// setup the optimal parameters for the raw1394 ISO buffering |
---|
308 |
unsigned int packets_per_period=stream->getPacketsPerPeriod(); |
---|
309 |
|
---|
310 |
#if 1 |
---|
311 |
// hardware interrupts occur when one DMA block is full, and the size of one DMA |
---|
312 |
// block = PAGE_SIZE. Setting the max_packet_size makes sure that the HW irq |
---|
313 |
// occurs at a period boundary (optimal CPU use) |
---|
314 |
|
---|
315 |
// NOTE: try and use MINIMUM_INTERRUPTS_PER_PERIOD hardware interrupts |
---|
316 |
// per period for better latency. |
---|
317 |
unsigned int max_packet_size=(MINIMUM_INTERRUPTS_PER_PERIOD * getpagesize()) / packets_per_period; |
---|
318 |
if (max_packet_size < stream->getMaxPacketSize()) { |
---|
319 |
max_packet_size=stream->getMaxPacketSize(); |
---|
320 |
} |
---|
321 |
|
---|
322 |
// Ensure we don't request a packet size bigger than the |
---|
323 |
// kernel-enforced maximum which is currently 1 page. |
---|
324 |
if (max_packet_size > (unsigned int)getpagesize()) |
---|
325 |
max_packet_size = getpagesize(); |
---|
326 |
|
---|
327 |
unsigned int irq_interval=packets_per_period / MINIMUM_INTERRUPTS_PER_PERIOD; |
---|
328 |
if(irq_interval <= 0) irq_interval=1; |
---|
329 |
#else |
---|
330 |
// hardware interrupts occur when one DMA block is full, and the size of one DMA |
---|
331 |
// block = PAGE_SIZE. Setting the max_packet_size enables control over the IRQ |
---|
332 |
// frequency, as the controller uses max_packet_size, and not the effective size |
---|
333 |
// when writing to the DMA buffer. |
---|
334 |
|
---|
335 |
// configure it such that we have an irq for every PACKETS_PER_INTERRUPT packets |
---|
336 |
unsigned int irq_interval=PACKETS_PER_INTERRUPT; |
---|
337 |
|
---|
338 |
// unless the period size doesn't allow this |
---|
339 |
if ((packets_per_period/MINIMUM_INTERRUPTS_PER_PERIOD) < irq_interval) { |
---|
340 |
irq_interval=1; |
---|
341 |
} |
---|
342 |
|
---|
343 |
// FIXME: test |
---|
344 |
irq_interval=1; |
---|
345 |
#warning Using fixed irq_interval |
---|
346 |
|
---|
347 |
unsigned int max_packet_size=getpagesize() / irq_interval; |
---|
348 |
|
---|
349 |
if (max_packet_size < stream->getMaxPacketSize()) { |
---|
350 |
max_packet_size=stream->getMaxPacketSize(); |
---|
351 |
} |
---|
352 |
|
---|
353 |
// Ensure we don't request a packet size bigger than the |
---|
354 |
// kernel-enforced maximum which is currently 1 page. |
---|
355 |
if (max_packet_size > (unsigned int)getpagesize()) |
---|
356 |
max_packet_size = getpagesize(); |
---|
357 |
|
---|
358 |
#endif |
---|
359 |
/* the receive buffer size doesn't matter for the latency, |
---|
360 |
but it has a minimal value in order for libraw to operate correctly (300) */ |
---|
361 |
int buffers=400; |
---|
362 |
|
---|
363 |
// create the actual handler |
---|
364 |
IsoRecvHandler *h = new IsoRecvHandler(stream->getPort(), buffers, |
---|
365 |
max_packet_size, irq_interval); |
---|
366 |
|
---|
367 |
debugOutput( DEBUG_LEVEL_VERBOSE, " registering IsoRecvHandler\n"); |
---|
368 |
|
---|
369 |
if(!h) { |
---|
370 |
debugFatal("Could not create IsoRecvHandler\n"); |
---|
371 |
return false; |
---|
372 |
} |
---|
373 |
|
---|
374 |
h->setVerboseLevel(getDebugLevel()); |
---|
375 |
|
---|
376 |
// init the handler |
---|
377 |
if(!h->init()) { |
---|
378 |
debugFatal("Could not initialize receive handler\n"); |
---|
379 |
return false; |
---|
380 |
} |
---|
381 |
|
---|
382 |
// register the stream with the handler |
---|
383 |
if(!h->registerStream(stream)) { |
---|
384 |
debugFatal("Could not register receive stream with handler\n"); |
---|
385 |
return false; |
---|
386 |
} |
---|
387 |
|
---|
388 |
// register the handler with the manager |
---|
389 |
if(!registerHandler(h)) { |
---|
390 |
debugFatal("Could not register receive handler with manager\n"); |
---|
391 |
return false; |
---|
392 |
} |
---|
393 |
debugOutput( DEBUG_LEVEL_VERBOSE, " registered stream (%p) with handler (%p)\n",stream,h); |
---|
394 |
} |
---|
395 |
|
---|
396 |
if (stream->getStreamType()==IsoStream::eST_Transmit) { |
---|
397 |
// setup the optimal parameters for the raw1394 ISO buffering |
---|
398 |
unsigned int packets_per_period=stream->getPacketsPerPeriod(); |
---|
399 |
|
---|
400 |
#if 1 |
---|
401 |
// hardware interrupts occur when one DMA block is full, and the size of one DMA |
---|
402 |
// block = PAGE_SIZE. Setting the max_packet_size makes sure that the HW irq |
---|
403 |
// occurs at a period boundary (optimal CPU use) |
---|
404 |
// NOTE: try and use MINIMUM_INTERRUPTS_PER_PERIOD interrupts per period |
---|
405 |
// for better latency. |
---|
406 |
unsigned int max_packet_size=MINIMUM_INTERRUPTS_PER_PERIOD * getpagesize() / packets_per_period; |
---|
407 |
if (max_packet_size < stream->getMaxPacketSize()) { |
---|
408 |
max_packet_size=stream->getMaxPacketSize(); |
---|
409 |
} |
---|
410 |
|
---|
411 |
// Ensure we don't request a packet size bigger than the |
---|
412 |
// kernel-enforced maximum which is currently 1 page. |
---|
413 |
if (max_packet_size > (unsigned int)getpagesize()) |
---|
414 |
max_packet_size = getpagesize(); |
---|
415 |
|
---|
416 |
unsigned int irq_interval=packets_per_period / MINIMUM_INTERRUPTS_PER_PERIOD; |
---|
417 |
if(irq_interval <= 0) irq_interval=1; |
---|
418 |
#else |
---|
419 |
// hardware interrupts occur when one DMA block is full, and the size of one DMA |
---|
420 |
// block = PAGE_SIZE. Setting the max_packet_size enables control over the IRQ |
---|
421 |
// frequency, as the controller uses max_packet_size, and not the effective size |
---|
422 |
// when writing to the DMA buffer. |
---|
423 |
|
---|
424 |
// configure it such that we have an irq for every PACKETS_PER_INTERRUPT packets |
---|
425 |
unsigned int irq_interval=PACKETS_PER_INTERRUPT; |
---|
426 |
|
---|
427 |
// unless the period size doesn't allow this |
---|
428 |
if ((packets_per_period/MINIMUM_INTERRUPTS_PER_PERIOD) < irq_interval) { |
---|
429 |
irq_interval=1; |
---|
430 |
} |
---|
431 |
|
---|
432 |
// FIXME: test |
---|
433 |
irq_interval=1; |
---|
434 |
#warning Using fixed irq_interval |
---|
435 |
|
---|
436 |
unsigned int max_packet_size=getpagesize() / irq_interval; |
---|
437 |
|
---|
438 |
if (max_packet_size < stream->getMaxPacketSize()) { |
---|
439 |
max_packet_size=stream->getMaxPacketSize(); |
---|
440 |
} |
---|
441 |
|
---|
442 |
// Ensure we don't request a packet size bigger than the |
---|
443 |
// kernel-enforced maximum which is currently 1 page. |
---|
444 |
if (max_packet_size > (unsigned int)getpagesize()) |
---|
445 |
max_packet_size = getpagesize(); |
---|
446 |
#endif |
---|
447 |
// the transmit buffer size should be as low as possible for latency. |
---|
448 |
// note however that the raw1394 subsystem tries to keep this buffer |
---|
449 |
// full, so we have to make sure that we have enough events in our |
---|
450 |
// event buffers |
---|
451 |
|
---|
452 |
// FIXME: latency spoiler |
---|
453 |
// every irq_interval packets an interrupt will occur. that is when |
---|
454 |
// buffers get transfered, meaning that we should have at least some |
---|
455 |
// margin here |
---|
456 |
int buffers=irq_interval * 2; |
---|
457 |
|
---|
458 |
// half a period. the xmit handler will take care of this |
---|
459 |
// int buffers=packets_per_period/4; |
---|
460 |
|
---|
461 |
// NOTE: this is dangerous: what if there is not enough prefill? |
---|
462 |
// if (buffers<10) buffers=10; |
---|
463 |
|
---|
464 |
// create the actual handler |
---|
465 |
IsoXmitHandler *h = new IsoXmitHandler(stream->getPort(), buffers, |
---|
466 |
max_packet_size, irq_interval); |
---|
467 |
|
---|
468 |
debugOutput( DEBUG_LEVEL_VERBOSE, " registering IsoXmitHandler\n"); |
---|
469 |
|
---|
470 |
if(!h) { |
---|
471 |
debugFatal("Could not create IsoXmitHandler\n"); |
---|
472 |
return false; |
---|
473 |
} |
---|
474 |
|
---|
475 |
h->setVerboseLevel(getDebugLevel()); |
---|
476 |
|
---|
477 |
// init the handler |
---|
478 |
if(!h->init()) { |
---|
479 |
debugFatal("Could not initialize transmit handler\n"); |
---|
480 |
return false; |
---|
481 |
} |
---|
482 |
|
---|
483 |
// register the stream with the handler |
---|
484 |
if(!h->registerStream(stream)) { |
---|
485 |
debugFatal("Could not register transmit stream with handler\n"); |
---|
486 |
return false; |
---|
487 |
} |
---|
488 |
|
---|
489 |
// register the handler with the manager |
---|
490 |
if(!registerHandler(h)) { |
---|
491 |
debugFatal("Could not register transmit handler with manager\n"); |
---|
492 |
return false; |
---|
493 |
} |
---|
494 |
debugOutput( DEBUG_LEVEL_VERBOSE, " registered stream (%p) with handler (%p)\n",stream,h); |
---|
495 |
} |
---|
496 |
|
---|
497 |
m_IsoStreams.push_back(stream); |
---|
498 |
debugOutput( DEBUG_LEVEL_VERBOSE, " %d streams, %d handlers registered\n", |
---|
499 |
m_IsoStreams.size(), m_IsoHandlers.size()); |
---|
500 |
|
---|
501 |
return true; |
---|
502 |
} |
---|
503 |
|
---|
504 |
bool IsoHandlerManager::unregisterStream(IsoStream *stream) |
---|
505 |
{ |
---|
506 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Unregistering stream %p\n",stream); |
---|
507 |
assert(stream); |
---|
508 |
|
---|
509 |
// make sure the stream isn't attached to a handler anymore |
---|
510 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
511 |
it != m_IsoHandlers.end(); |
---|
512 |
++it ) |
---|
513 |
{ |
---|
514 |
if((*it)->isStreamRegistered(stream)) { |
---|
515 |
if(!(*it)->unregisterStream(stream)) { |
---|
516 |
debugOutput( DEBUG_LEVEL_VERBOSE, " could not unregister stream (%p) from handler (%p)...\n",stream,*it); |
---|
517 |
return false; |
---|
518 |
} |
---|
519 |
|
---|
520 |
debugOutput( DEBUG_LEVEL_VERBOSE, " unregistered stream (%p) from handler (%p)...\n",stream,*it); |
---|
521 |
} |
---|
522 |
} |
---|
523 |
|
---|
524 |
// clean up all handlers that aren't used |
---|
525 |
pruneHandlers(); |
---|
526 |
|
---|
527 |
// remove the stream from the registered streams list |
---|
528 |
for ( IsoStreamVectorIterator it = m_IsoStreams.begin(); |
---|
529 |
it != m_IsoStreams.end(); |
---|
530 |
++it ) |
---|
531 |
{ |
---|
532 |
if ( *it == stream ) { |
---|
533 |
m_IsoStreams.erase(it); |
---|
534 |
|
---|
535 |
debugOutput( DEBUG_LEVEL_VERBOSE, " deleted stream (%p) from list...\n", *it); |
---|
536 |
return true; |
---|
537 |
} |
---|
538 |
} |
---|
539 |
|
---|
540 |
return false; //not found |
---|
541 |
|
---|
542 |
} |
---|
543 |
|
---|
544 |
void IsoHandlerManager::pruneHandlers() { |
---|
545 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
546 |
IsoHandlerVector toUnregister; |
---|
547 |
|
---|
548 |
// find all handlers that are not in use |
---|
549 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
550 |
it != m_IsoHandlers.end(); |
---|
551 |
++it ) |
---|
552 |
{ |
---|
553 |
if(!((*it)->inUse())) { |
---|
554 |
debugOutput( DEBUG_LEVEL_VERBOSE, " handler (%p) not in use\n",*it); |
---|
555 |
toUnregister.push_back(*it); |
---|
556 |
} |
---|
557 |
} |
---|
558 |
// delete them |
---|
559 |
for ( IsoHandlerVectorIterator it = toUnregister.begin(); |
---|
560 |
it != toUnregister.end(); |
---|
561 |
++it ) |
---|
562 |
{ |
---|
563 |
unregisterHandler(*it); |
---|
564 |
debugOutput( DEBUG_LEVEL_VERBOSE, " deleting handler (%p)\n",*it); |
---|
565 |
|
---|
566 |
// Now the handler's been unregistered it won't be reused |
---|
567 |
// again. Therefore it really needs to be formally deleted |
---|
568 |
// to free up the raw1394 handle. Otherwise things fall |
---|
569 |
// apart after several xrun recoveries as the system runs |
---|
570 |
// out of resources to support all the disused but still |
---|
571 |
// allocated raw1394 handles. At least this is the current |
---|
572 |
// theory as to why we end up with "memory allocation" |
---|
573 |
// failures after several Xrun recoveries. |
---|
574 |
delete *it; |
---|
575 |
} |
---|
576 |
|
---|
577 |
} |
---|
578 |
|
---|
579 |
|
---|
580 |
bool IsoHandlerManager::prepare() |
---|
581 |
{ |
---|
582 |
bool retval=true; |
---|
583 |
|
---|
584 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
585 |
|
---|
586 |
// check state |
---|
587 |
if(m_State != E_Created) { |
---|
588 |
debugError("Incorrect state, expected E_Created, got %d\n",(int)m_State); |
---|
589 |
return false; |
---|
590 |
} |
---|
591 |
|
---|
592 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
593 |
it != m_IsoHandlers.end(); |
---|
594 |
++it ) |
---|
595 |
{ |
---|
596 |
if(!(*it)->prepare()) { |
---|
597 |
debugFatal("Could not prepare handlers\n"); |
---|
598 |
retval=false; |
---|
599 |
} |
---|
600 |
} |
---|
601 |
|
---|
602 |
if (retval) { |
---|
603 |
m_State=E_Prepared; |
---|
604 |
} else { |
---|
605 |
m_State=E_Error; |
---|
606 |
} |
---|
607 |
|
---|
608 |
return retval; |
---|
609 |
} |
---|
610 |
|
---|
611 |
bool IsoHandlerManager::startHandlers() { |
---|
612 |
return startHandlers(-1); |
---|
613 |
} |
---|
614 |
|
---|
615 |
bool IsoHandlerManager::startHandlers(int cycle) { |
---|
616 |
bool retval=true; |
---|
617 |
|
---|
618 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
619 |
|
---|
620 |
// check state |
---|
621 |
if(m_State != E_Prepared) { |
---|
622 |
debugError("Incorrect state, expected E_Prepared, got %d\n",(int)m_State); |
---|
623 |
return false; |
---|
624 |
} |
---|
625 |
|
---|
626 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
627 |
it != m_IsoHandlers.end(); |
---|
628 |
++it ) |
---|
629 |
{ |
---|
630 |
debugOutput( DEBUG_LEVEL_VERBOSE, " starting handler (%p)\n",*it); |
---|
631 |
if(!(*it)->start(cycle)) { |
---|
632 |
debugOutput( DEBUG_LEVEL_VERBOSE, " could not start handler (%p)\n",*it); |
---|
633 |
retval=false; |
---|
634 |
} |
---|
635 |
} |
---|
636 |
|
---|
637 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Starting ISO iterator thread...\n"); |
---|
638 |
|
---|
639 |
// note: libraw1394 doesn't like it if you poll() and/or iterate() before |
---|
640 |
// starting the streams. |
---|
641 |
// start the iso runner thread |
---|
642 |
m_isoManagerThread->Start(); |
---|
643 |
|
---|
644 |
if (retval) { |
---|
645 |
m_State=E_Running; |
---|
646 |
} else { |
---|
647 |
m_State=E_Error; |
---|
648 |
} |
---|
649 |
|
---|
650 |
return retval; |
---|
651 |
} |
---|
652 |
|
---|
653 |
bool IsoHandlerManager::stopHandlers() { |
---|
654 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
655 |
|
---|
656 |
// check state |
---|
657 |
if(m_State != E_Running) { |
---|
658 |
debugError("Incorrect state, expected E_Running, got %d\n",(int)m_State); |
---|
659 |
return false; |
---|
660 |
} |
---|
661 |
|
---|
662 |
bool retval=true; |
---|
663 |
|
---|
664 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping ISO iterator thread...\n"); |
---|
665 |
m_isoManagerThread->Stop(); |
---|
666 |
|
---|
667 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
668 |
it != m_IsoHandlers.end(); |
---|
669 |
++it ) |
---|
670 |
{ |
---|
671 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping handler (%p)\n",*it); |
---|
672 |
if(!(*it)->stop()){ |
---|
673 |
debugOutput( DEBUG_LEVEL_VERBOSE, " could not stop handler (%p)\n",*it); |
---|
674 |
retval=false; |
---|
675 |
} |
---|
676 |
} |
---|
677 |
|
---|
678 |
if (retval) { |
---|
679 |
m_State=E_Prepared; |
---|
680 |
} else { |
---|
681 |
m_State=E_Error; |
---|
682 |
} |
---|
683 |
|
---|
684 |
return retval; |
---|
685 |
} |
---|
686 |
|
---|
687 |
bool IsoHandlerManager::reset() { |
---|
688 |
debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n"); |
---|
689 |
|
---|
690 |
// check state |
---|
691 |
if(m_State == E_Error) { |
---|
692 |
debugFatal("Resetting from error condition not yet supported...\n"); |
---|
693 |
return false; |
---|
694 |
} |
---|
695 |
|
---|
696 |
// if not in an error condition, reset means stop the handlers |
---|
697 |
return stopHandlers(); |
---|
698 |
} |
---|
699 |
|
---|
700 |
|
---|
701 |
void IsoHandlerManager::setVerboseLevel(int i) { |
---|
702 |
setDebugLevel(i); |
---|
703 |
|
---|
704 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
705 |
it != m_IsoHandlers.end(); |
---|
706 |
++it ) |
---|
707 |
{ |
---|
708 |
(*it)->setVerboseLevel(i); |
---|
709 |
} |
---|
710 |
} |
---|
711 |
|
---|
712 |
void IsoHandlerManager::dumpInfo() { |
---|
713 |
int i=0; |
---|
714 |
|
---|
715 |
debugOutputShort( DEBUG_LEVEL_NORMAL, "Dumping IsoHandlerManager Stream handler information...\n"); |
---|
716 |
debugOutputShort( DEBUG_LEVEL_NORMAL, " State: %d\n",(int)m_State); |
---|
717 |
|
---|
718 |
for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin(); |
---|
719 |
it != m_IsoHandlers.end(); |
---|
720 |
++it ) |
---|
721 |
{ |
---|
722 |
debugOutputShort( DEBUG_LEVEL_NORMAL, " IsoHandler %d (%p)\n",i++,*it); |
---|
723 |
|
---|
724 |
(*it)->dumpInfo(); |
---|
725 |
} |
---|
726 |
|
---|
727 |
} |
---|
728 |
|
---|
729 |
} // end of namespace Streaming |
---|
730 |
|
---|