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

Revision 918, 26.3 kB (checked in by ppalmers, 13 years ago)

fix bogus startup timestamp issue

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