root/trunk/libffado/src/libieee1394/IsoHandlerManager.cpp

Revision 940, 25.7 kB (checked in by ppalmers, 13 years ago)

use RT watchdog for 1394 service threads

Line 
1 /*
2  * Copyright (C) 2005-2008 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 program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "config.h"
25 #include "IsoHandlerManager.h"
26 #include "ieee1394service.h"
27 #include "IsoHandler.h"
28 #include "libstreaming/generic/StreamProcessor.h"
29
30 #include "libutil/Atomic.h"
31 #include "libutil/PosixThread.h"
32 #include "libutil/SystemTimeSource.h"
33 #include "libutil/Watchdog.h"
34
35 #include <assert.h>
36
37 IMPL_DEBUG_MODULE( IsoHandlerManager, IsoHandlerManager, DEBUG_LEVEL_NORMAL );
38 IMPL_DEBUG_MODULE( IsoTask, IsoTask, DEBUG_LEVEL_NORMAL );
39
40 using namespace Streaming;
41
42 // --- ISO Thread --- //
43
44 IsoTask::IsoTask(IsoHandlerManager& manager)
45     : m_manager( manager )
46     , m_SyncIsoHandler ( NULL )
47 {
48 }
49
50 bool
51 IsoTask::Init()
52 {
53     request_update = 0;
54
55     int i;
56     for (i=0; i < ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT; i++) {
57         m_IsoHandler_map_shadow[i] = NULL;
58         m_poll_fds_shadow[i].events = 0;
59     }
60     m_poll_nfds_shadow = 0;
61     return true;
62 }
63
64 bool
65 IsoTask::requestShadowMapUpdate()
66 {
67     debugOutput(DEBUG_LEVEL_VERBOSE, "(%p) enter\n", this);
68     INC_ATOMIC(&request_update);
69     return true;
70 }
71
72 // updates the internal stream map
73 // note that this should be executed with the guarantee that
74 // nobody will modify the parent data structures
75 void
76 IsoTask::updateShadowMapHelper()
77 {
78     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) updating shadow vars...\n", this);
79     unsigned int i, cnt, max;
80     max = m_manager.m_IsoHandlers.size();
81     m_SyncIsoHandler = NULL;
82     for (i = 0, cnt = 0; i < max; i++) {
83         IsoHandler *h = m_manager.m_IsoHandlers.at(i);
84         assert(h);
85
86         if (h->isEnabled()) {
87             m_IsoHandler_map_shadow[cnt] = h;
88             m_poll_fds_shadow[cnt].fd = h->getFileDescriptor();
89             m_poll_fds_shadow[cnt].revents = 0;
90             m_poll_fds_shadow[cnt].events = POLLIN;
91             cnt++;
92             // FIXME: need a more generic approach here
93             if(   m_SyncIsoHandler == NULL
94                && h->getType() == IsoHandler::eHT_Transmit) {
95                 m_SyncIsoHandler = h;
96             }
97
98             debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) %s handler %p added\n",
99                                               this, h->getTypeString(), h);
100         } else {
101             debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) %s handler %p skipped (disabled)\n",
102                                               this, h->getTypeString(), h);
103         }
104         if(cnt > ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT) {
105             debugWarning("Too much ISO Handlers in thread...\n");
106             break;
107         }
108     }
109
110     // FIXME: need a more generic approach here
111     // if there are no active transmit handlers,
112     // use the first receive handler
113     if(   m_SyncIsoHandler == NULL
114        && m_poll_nfds_shadow) {
115         m_SyncIsoHandler = m_IsoHandler_map_shadow[0];
116     }
117     m_poll_nfds_shadow = cnt;
118     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) updated shadow vars...\n", this);
119 }
120
121 bool
122 IsoTask::Execute()
123 {
124     debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE,
125                        "(%p) Execute\n", this);
126     int err;
127     unsigned int i;
128     unsigned int m_poll_timeout = 10;
129
130     // if some other thread requested a shadow map update, do it
131     if(request_update) {
132         updateShadowMapHelper();
133         DEC_ATOMIC(&request_update); // ack the update
134     }
135
136     // bypass if no handlers are registered
137     if (m_poll_nfds_shadow == 0) {
138         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE,
139                            "(%p) bypass iterate since no handlers to poll\n",
140                            this);
141         usleep(m_poll_timeout * 1000);
142         return true;
143     }
144
145     // FIXME: what can happen is that poll() returns, but not all clients are
146     // ready. there might be some busy waiting behavior that still has to be solved.
147
148     // setup the poll here
149     // we should prevent a poll() where no events are specified, since that will only time-out
150     bool no_one_to_poll = true;
151     while(no_one_to_poll) {
152         for (i = 0; i < m_poll_nfds_shadow; i++) {
153             short events = 0;
154             IsoHandler *h = m_IsoHandler_map_shadow[i];
155             if(h->getType() == IsoHandler::eHT_Transmit) {
156                 // we should only poll on a transmit handler
157                 // that has a client that is ready to send
158                 // something. Otherwise it will end up in
159                 // busy wait looping since the packet function
160                 // will defer processing (also avoids the
161                 // AGAIN problem)
162                 if (h->tryWaitForClient()) {
163                     events = POLLIN | POLLPRI;
164                     no_one_to_poll = false;
165                 }
166             } else {
167                 // a receive handler should only be polled if
168                 // it's client doesn't already have enough data
169                 // and if it can still accept data.
170                 if (h->tryWaitForClient()) { // FIXME
171                     events = POLLIN | POLLPRI;
172                     no_one_to_poll = false;
173                 }
174             }
175             m_poll_fds_shadow[i].events = events;
176         }
177
178         if(no_one_to_poll) {
179             debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE,
180                                "(%p) No one to poll, waiting on the sync handler to become ready\n",
181                                this);
182
183             if(!m_SyncIsoHandler->waitForClient()) {
184                 debugError("Failed to wait for client\n");
185                 return false;
186             }
187
188             #ifdef DEBUG
189             // if this happens we end up in a deadlock!
190             if(!m_SyncIsoHandler->tryWaitForClient()) {
191                 debugFatal("inconsistency in wait functions!\n");
192                 return false;
193             }
194             #endif
195
196             debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE,
197                                "(%p) sync handler ready\n",
198                                this);
199         }
200     }
201
202     // Use a shadow map of the fd's such that we don't have to update
203     // the fd map everytime we run poll(). It doesn't change that much
204     // anyway
205     err = poll (m_poll_fds_shadow, m_poll_nfds_shadow, m_poll_timeout);
206
207     if (err < 0) {
208         if (errno == EINTR) {
209             return true;
210         }
211         debugFatal("poll error: %s\n", strerror (errno));
212         return false;
213     }
214
215     for (i = 0; i < m_poll_nfds_shadow; i++) {
216         #ifdef DEBUG
217         if(m_poll_fds_shadow[i].revents) {
218             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
219                         "received events: %08X for (%d/%d, %p, %s)\n",
220                         m_poll_fds_shadow[i].revents,
221                         i, m_poll_nfds_shadow,
222                         m_IsoHandler_map_shadow[i],
223                         m_IsoHandler_map_shadow[i]->getTypeString());
224         }
225         #endif
226
227         // if we get here, it means two things:
228         // 1) the kernel can accept or provide packets (poll returned POLLIN)
229         // 2) the client can provide or accept packets (since we enabled polling)
230         if(m_poll_fds_shadow[i].revents & (POLLIN)) {
231             m_IsoHandler_map_shadow[i]->iterate();
232         } else {
233             // there might be some error condition
234             if (m_poll_fds_shadow[i].revents & POLLERR) {
235                 debugWarning("error on fd for %d\n",i);
236             }
237             if (m_poll_fds_shadow[i].revents & POLLHUP) {
238                 debugWarning("hangup on fd for %d\n",i);
239             }
240         }
241
242 //         #ifdef DEBUG
243 //         // check if the handler is still alive
244 //         if(m_IsoHandler_map_shadow[i]->isDead()) {
245 //             debugError("Iso handler (%p, %s) is dead!\n",
246 //                        m_IsoHandler_map_shadow[i],
247 //                        m_IsoHandler_map_shadow[i]->getTypeString());
248 //             return false; // shutdown the system
249 //         }
250 //         #endif
251
252     }
253     return true;
254
255 }
256
257 void IsoTask::setVerboseLevel(int i) {
258     setDebugLevel(i);
259 }
260
261 // -- the ISO handler manager -- //
262 IsoHandlerManager::IsoHandlerManager(Ieee1394Service& service)
263    : m_State(E_Created)
264    , m_service( service )
265    , m_realtime(false), m_priority(0)
266    , m_IsoThread ( NULL )
267    , m_IsoTask ( NULL )
268 {}
269
270 IsoHandlerManager::IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio)
271    : m_State(E_Created)
272    , m_service( service )
273    , m_realtime(run_rt), m_priority(rt_prio)
274    , m_IsoThread ( NULL )
275    , m_IsoTask ( NULL )
276 {}
277
278 IsoHandlerManager::~IsoHandlerManager()
279 {
280     stopHandlers();
281     pruneHandlers();
282     if(m_IsoHandlers.size() > 0) {
283         debugError("Still some handlers in use\n");
284     }
285     if (m_IsoThread) {
286         m_IsoThread->Stop();
287         delete m_IsoThread;
288     }
289     if (m_IsoTask) {
290         delete m_IsoTask;
291     }
292 }
293
294 bool
295 IsoHandlerManager::setThreadParameters(bool rt, int priority) {
296     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) switch to: (rt=%d, prio=%d)...\n", this, rt, priority);
297     if (priority > THREAD_MAX_RTPRIO) priority = THREAD_MAX_RTPRIO; // cap the priority
298     m_realtime = rt;
299     m_priority = priority;
300
301     if (m_IsoThread) {
302         if (m_realtime) {
303             m_IsoThread->AcquireRealTime(m_priority);
304         } else {
305             m_IsoThread->DropRealTime();
306         }
307     }
308
309     return true;
310 }
311
312 bool IsoHandlerManager::init()
313 {
314     debugOutput( DEBUG_LEVEL_VERBOSE, "Initializing ISO manager %p...\n", this);
315     // check state
316     if(m_State != E_Created) {
317         debugError("Manager already initialized...\n");
318         return false;
319     }
320
321     // create a thread to iterate our ISO handlers
322     debugOutput( DEBUG_LEVEL_VERBOSE, "Create iso thread for %p...\n", this);
323     m_IsoTask = new IsoTask( *this );
324     if(!m_IsoTask) {
325         debugFatal("No task\n");
326         return false;
327     }
328     m_IsoThread = new Util::PosixThread(m_IsoTask, m_realtime,
329                                         m_priority + ISOHANDLERMANAGER_ISO_PRIO_INCREASE,
330                                         PTHREAD_CANCEL_DEFERRED);
331
332     if(!m_IsoThread) {
333         debugFatal("No thread\n");
334         return false;
335     }
336     // register the thread with the RT watchdog
337     Util::Watchdog *watchdog = m_service.getWatchdog();
338     if(watchdog) {
339         if(!watchdog->registerThread(m_IsoThread)) {
340             debugWarning("could not register iso thread with watchdog\n");
341         }
342     } else {
343         debugWarning("could not find valid watchdog\n");
344     }
345
346     if (m_IsoThread->Start() != 0) {
347         debugFatal("Could not start ISO thread\n");
348         return false;
349     }
350
351     m_State=E_Running;
352     return true;
353 }
354
355 bool
356 IsoHandlerManager::disable(IsoHandler *h) {
357     bool result;
358     int i=0;
359     debugOutput(DEBUG_LEVEL_VERBOSE, "Disable on IsoHandler %p\n", h);
360     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
361         it != m_IsoHandlers.end();
362         ++it )
363     {
364         if ((*it) == h) {
365             result = h->disable();
366             result &= m_IsoTask->requestShadowMapUpdate();
367             debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " disabled\n");
368             return result;
369         }
370         i++;
371     }
372     debugError("Handler not found\n");
373     return false;
374 }
375
376 bool
377 IsoHandlerManager::enable(IsoHandler *h) {
378     bool result;
379     int i=0;
380     debugOutput(DEBUG_LEVEL_VERBOSE, "Enable on IsoHandler %p\n", h);
381     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
382         it != m_IsoHandlers.end();
383         ++it )
384     {
385         if ((*it) == h) {
386             result = h->enable();
387             result &= m_IsoTask->requestShadowMapUpdate();
388             debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " enabled\n");
389             return result;
390         }
391         i++;
392     }
393     debugError("Handler not found\n");
394     return false;
395 }
396
397 bool IsoHandlerManager::registerHandler(IsoHandler *handler)
398 {
399     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
400     assert(handler);
401     handler->setVerboseLevel(getDebugLevel());
402     m_IsoHandlers.push_back(handler);
403     return m_IsoTask->requestShadowMapUpdate();
404 }
405
406 bool IsoHandlerManager::unregisterHandler(IsoHandler *handler)
407 {
408     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
409     assert(handler);
410
411     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
412       it != m_IsoHandlers.end();
413       ++it )
414     {
415         if ( *it == handler ) {
416             m_IsoHandlers.erase(it);
417             return m_IsoTask->requestShadowMapUpdate();
418         }
419     }
420     debugFatal("Could not find handler (%p)\n", handler);
421     return false; //not found
422 }
423
424 /**
425  * Registers an StreamProcessor with the IsoHandlerManager.
426  *
427  * If nescessary, an IsoHandler is created to handle this stream.
428  * Once an StreamProcessor is registered to the handler, it will be included
429  * in the ISO streaming cycle (i.e. receive/transmit of it will occur).
430  *
431  * @param stream the stream to register
432  * @return true if registration succeeds
433  *
434  * \todo : currently there is a one-to-one mapping
435  *        between streams and handlers, this is not ok for
436  *        multichannel receive
437  */
438 bool IsoHandlerManager::registerStream(StreamProcessor *stream)
439 {
440     debugOutput( DEBUG_LEVEL_VERBOSE, "Registering stream %p\n",stream);
441     assert(stream);
442
443     IsoHandler* h = NULL;
444
445     // make sure the stream isn't already attached to a handler
446     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
447       it != m_IsoHandlers.end();
448       ++it )
449     {
450         if((*it)->isStreamRegistered(stream)) {
451             debugError( "stream already registered!\n");
452             return false;
453         }
454     }
455
456     // clean up all handlers that aren't used
457     pruneHandlers();
458
459     // allocate a handler for this stream
460     if (stream->getType()==StreamProcessor::ePT_Receive) {
461         // setup the optimal parameters for the raw1394 ISO buffering
462         unsigned int packets_per_period = stream->getPacketsPerPeriod();
463         unsigned int max_packet_size = stream->getMaxPacketSize();
464         unsigned int page_size = getpagesize() - 2; // for one reason or another this is necessary
465
466         // Ensure we don't request a packet size bigger than the
467         // kernel-enforced maximum which is currently 1 page.
468         if (max_packet_size > page_size) {
469             debugError("max packet size (%u) > page size (%u)\n", max_packet_size, page_size);
470             return false;
471         }
472
473         unsigned int irq_interval = packets_per_period / MINIMUM_INTERRUPTS_PER_PERIOD;
474         if(irq_interval <= 0) irq_interval=1;
475        
476         // the receive buffer size doesn't matter for the latency,
477         // but it has a minimal value in order for libraw to operate correctly (300)
478         int buffers=400;
479
480         // create the actual handler
481         h = new IsoHandler(*this, IsoHandler::eHT_Receive,
482                            buffers, max_packet_size, irq_interval);
483
484         debugOutput( DEBUG_LEVEL_VERBOSE, " creating IsoRecvHandler\n");
485
486         if(!h) {
487             debugFatal("Could not create IsoRecvHandler\n");
488             return false;
489         }
490
491     } else if (stream->getType()==StreamProcessor::ePT_Transmit) {
492         // setup the optimal parameters for the raw1394 ISO buffering
493 //        unsigned int packets_per_period = stream->getPacketsPerPeriod();
494         unsigned int max_packet_size = stream->getMaxPacketSize();
495 //         unsigned int page_size = getpagesize();
496
497         // Ensure we don't request a packet size bigger than the
498         // kernel-enforced maximum which is currently 1 page.
499 //         if (max_packet_size > page_size) {
500 //             debugError("max packet size (%u) > page size (%u)\n", max_packet_size, page_size);
501 //             return false;
502 //         }
503         if (max_packet_size > MAX_XMIT_PACKET_SIZE) {
504             debugError("max packet size (%u) > MAX_XMIT_PACKET_SIZE (%u)\n",
505                        max_packet_size, MAX_XMIT_PACKET_SIZE);
506             return false;
507         }
508
509         // the SP specifies how many packets to ISO-buffer
510         int buffers = stream->getNbPacketsIsoXmitBuffer();
511         if (buffers > MAX_XMIT_NB_BUFFERS) {
512             debugOutput(DEBUG_LEVEL_VERBOSE,
513                         "nb buffers (%u) > MAX_XMIT_NB_BUFFERS (%u)\n",
514                         buffers, MAX_XMIT_NB_BUFFERS);
515             buffers = MAX_XMIT_NB_BUFFERS;
516         }
517         unsigned int irq_interval = buffers / MINIMUM_INTERRUPTS_PER_PERIOD;
518         if(irq_interval <= 0) irq_interval=1;
519
520         debugOutput( DEBUG_LEVEL_VERBOSE, " creating IsoXmitHandler\n");
521
522         // create the actual handler
523         h = new IsoHandler(*this, IsoHandler::eHT_Transmit,
524                            buffers, max_packet_size, irq_interval);
525
526         if(!h) {
527             debugFatal("Could not create IsoXmitHandler\n");
528             return false;
529         }
530
531     } else {
532         debugFatal("Bad stream type\n");
533         return false;
534     }
535
536     h->setVerboseLevel(getDebugLevel());
537
538     // init the handler
539     if(!h->init()) {
540         debugFatal("Could not initialize receive handler\n");
541         return false;
542     }
543
544     // register the stream with the handler
545     if(!h->registerStream(stream)) {
546         debugFatal("Could not register receive stream with handler\n");
547         return false;
548     }
549
550     // register the handler with the manager
551     if(!registerHandler(h)) {
552         debugFatal("Could not register receive handler with manager\n");
553         return false;
554     }
555     debugOutput( DEBUG_LEVEL_VERBOSE, " registered stream (%p) with handler (%p)\n", stream, h);
556
557     m_StreamProcessors.push_back(stream);
558     debugOutput( DEBUG_LEVEL_VERBOSE, " %d streams, %d handlers registered\n",
559                                       m_StreamProcessors.size(), m_IsoHandlers.size());
560     return true;
561 }
562
563 bool IsoHandlerManager::unregisterStream(StreamProcessor *stream)
564 {
565     debugOutput( DEBUG_LEVEL_VERBOSE, "Unregistering stream %p\n",stream);
566     assert(stream);
567
568     // make sure the stream isn't attached to a handler anymore
569     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
570       it != m_IsoHandlers.end();
571       ++it )
572     {
573         if((*it)->isStreamRegistered(stream)) {
574             if(!(*it)->unregisterStream(stream)) {
575                 debugOutput( DEBUG_LEVEL_VERBOSE, " could not unregister stream (%p) from handler (%p)...\n",stream,*it);
576                 return false;
577             }
578             debugOutput( DEBUG_LEVEL_VERBOSE, " unregistered stream (%p) from handler (%p)...\n",stream,*it);
579         }
580     }
581
582     // clean up all handlers that aren't used
583     pruneHandlers();
584
585     // remove the stream from the registered streams list
586     for ( StreamProcessorVectorIterator it = m_StreamProcessors.begin();
587       it != m_StreamProcessors.end();
588       ++it )
589     {
590         if ( *it == stream ) {
591             m_StreamProcessors.erase(it);
592             debugOutput( DEBUG_LEVEL_VERBOSE, " deleted stream (%p) from list...\n", *it);
593             return true;
594         }
595     }
596     return false; //not found
597 }
598
599 /**
600  * @brief unregister a handler from the manager
601  * @note called without the lock held.
602  */
603 void IsoHandlerManager::pruneHandlers() {
604     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
605     IsoHandlerVector toUnregister;
606
607     // find all handlers that are not in use
608     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
609           it != m_IsoHandlers.end();
610           ++it )
611     {
612         if(!((*it)->inUse())) {
613             debugOutput( DEBUG_LEVEL_VERBOSE, " handler (%p) not in use\n",*it);
614             toUnregister.push_back(*it);
615         }
616     }
617     // delete them
618     for ( IsoHandlerVectorIterator it = toUnregister.begin();
619           it != toUnregister.end();
620           ++it )
621     {
622         unregisterHandler(*it);
623
624         debugOutput( DEBUG_LEVEL_VERBOSE, " deleting handler (%p)\n",*it);
625
626         // Now the handler's been unregistered it won't be reused
627         // again.  Therefore it really needs to be formally deleted
628         // to free up the raw1394 handle.  Otherwise things fall
629         // apart after several xrun recoveries as the system runs
630         // out of resources to support all the disused but still
631         // allocated raw1394 handles.  At least this is the current
632         // theory as to why we end up with "memory allocation"
633         // failures after several Xrun recoveries.
634         delete *it;
635     }
636 }
637
638 bool
639 IsoHandlerManager::stopHandlerForStream(Streaming::StreamProcessor *stream) {
640     // check state
641     if(m_State != E_Running) {
642         debugError("Incorrect state, expected E_Running, got %s\n", eHSToString(m_State));
643         return false;
644     }
645     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
646       it != m_IsoHandlers.end();
647       ++it )
648     {
649         if((*it)->isStreamRegistered(stream)) {
650             debugOutput( DEBUG_LEVEL_VERBOSE, " stopping handler %p for stream %p\n", *it, stream);
651             if(!(*it)->disable()) {
652                 debugOutput( DEBUG_LEVEL_VERBOSE, " could not disable handler (%p)\n",*it);
653                 return false;
654             }
655             if(!m_IsoTask->requestShadowMapUpdate()) {
656                 debugOutput( DEBUG_LEVEL_VERBOSE, " could not update shadow map for handler (%p)\n",*it);
657                 return false;
658             }
659             return true;
660         }
661     }
662     debugError("Stream %p has no attached handler\n", stream);
663     return false;
664 }
665
666 int
667 IsoHandlerManager::getPacketLatencyForStream(Streaming::StreamProcessor *stream) {
668     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
669       it != m_IsoHandlers.end();
670       ++it )
671     {
672         if((*it)->isStreamRegistered(stream)) {
673             return (*it)->getPacketLatency();
674         }
675     }
676     debugError("Stream %p has no attached handler\n", stream);
677     return 0;
678 }
679
680 void
681 IsoHandlerManager::flushHandlerForStream(Streaming::StreamProcessor *stream) {
682     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
683       it != m_IsoHandlers.end();
684       ++it )
685     {
686         if((*it)->isStreamRegistered(stream)) {
687             return (*it)->flush();
688         }
689     }
690     debugError("Stream %p has no attached handler\n", stream);
691     return;
692 }
693
694 bool
695 IsoHandlerManager::startHandlerForStream(Streaming::StreamProcessor *stream) {
696     return startHandlerForStream(stream, -1);
697 }
698
699 bool
700 IsoHandlerManager::startHandlerForStream(Streaming::StreamProcessor *stream, int cycle) {
701     // check state
702     if(m_State != E_Running) {
703         debugError("Incorrect state, expected E_Running, got %s\n", eHSToString(m_State));
704         return false;
705     }
706     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
707       it != m_IsoHandlers.end();
708       ++it )
709     {
710         if((*it)->isStreamRegistered(stream)) {
711             debugOutput( DEBUG_LEVEL_VERBOSE, " starting handler %p for stream %p\n", *it, stream);
712             if(!(*it)->enable(cycle)) {
713                 debugOutput( DEBUG_LEVEL_VERBOSE, " could not enable handler (%p)\n",*it);
714                 return false;
715             }
716             if(!m_IsoTask->requestShadowMapUpdate()) {
717                 debugOutput( DEBUG_LEVEL_VERBOSE, " could not update shadow map for handler (%p)\n",*it);
718                 return false;
719             }
720             return true;
721         }
722     }
723     debugError("Stream %p has no attached handler\n", stream);
724     return false;
725 }
726
727 bool IsoHandlerManager::stopHandlers() {
728     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
729
730     // check state
731     if(m_State != E_Running) {
732         debugError("Incorrect state, expected E_Running, got %s\n", eHSToString(m_State));
733         return false;
734     }
735
736     bool retval=true;
737
738     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
739         it != m_IsoHandlers.end();
740         ++it )
741     {
742         debugOutput( DEBUG_LEVEL_VERBOSE, "Stopping handler (%p)\n",*it);
743         if(!(*it)->disable()){
744             debugOutput( DEBUG_LEVEL_VERBOSE, " could not stop handler (%p)\n",*it);
745             retval=false;
746         }
747         if(!m_IsoTask->requestShadowMapUpdate()) {
748             debugOutput( DEBUG_LEVEL_VERBOSE, " could not update shadow map for handler (%p)\n",*it);
749             retval=false;
750         }
751     }
752
753     if (retval) {
754         m_State=E_Prepared;
755     } else {
756         m_State=E_Error;
757     }
758     return retval;
759 }
760
761 bool IsoHandlerManager::reset() {
762     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
763     // check state
764     if(m_State == E_Error) {
765         debugFatal("Resetting from error condition not yet supported...\n");
766         return false;
767     }
768     // if not in an error condition, reset means stop the handlers
769     return stopHandlers();
770 }
771
772 void IsoHandlerManager::setVerboseLevel(int i) {
773     setDebugLevel(i);
774     // propagate the debug level
775     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
776           it != m_IsoHandlers.end();
777           ++it )
778     {
779         (*it)->setVerboseLevel(i);
780     }
781     if(m_IsoThread) m_IsoThread->setVerboseLevel(i);
782     if(m_IsoTask)   m_IsoTask->setVerboseLevel(i);
783 }
784
785 void IsoHandlerManager::dumpInfo() {
786     #ifdef DEBUG
787     unsigned int i=0;
788     debugOutputShort( DEBUG_LEVEL_NORMAL, "Dumping IsoHandlerManager Stream handler information...\n");
789     debugOutputShort( DEBUG_LEVEL_NORMAL, " State: %d\n",(int)m_State);
790
791     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
792           it != m_IsoHandlers.end();
793           ++it )
794     {
795         debugOutputShort( DEBUG_LEVEL_NORMAL, " IsoHandler %d (%p)\n",i++,*it);
796         (*it)->dumpInfo();
797     }
798     #endif
799 }
800
801 const char *
802 IsoHandlerManager::eHSToString(enum eHandlerStates s) {
803     switch (s) {
804         default: return "Invalid";
805         case E_Created: return "Created";
806         case E_Prepared: return "Prepared";
807         case E_Running: return "Running";
808         case E_Error: return "Error";
809     }
810 }
Note: See TracBrowser for help on using the browser.