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

Revision 937, 26.5 kB (checked in by ppalmers, 13 years ago)

cleanup some compilation warnings

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, %8s) 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     // we should prevent a poll() where no events are specified, since that will only time-out
139     bool no_one_to_poll = true;
140     while(no_one_to_poll) {
141         if (m_type==eTT_Transmit) {
142             // if we are a transmit thread, we should only poll on
143             // those handlers that have a client that is ready to send
144             // something. poll'ing the others will only cause busy-wait
145             // looping.
146             for (i = 0; i < m_poll_nfds_shadow; i++) {
147                 short events = 0;
148                 if (m_IsoHandler_map_shadow[i]->tryWaitForClient()) {
149                     events = POLLIN | POLLPRI;
150                     no_one_to_poll = false;
151                 }
152                 m_poll_fds_shadow[i].events = events;
153             }
154         } else {
155             // for receive handlers, we can do the same. we might not have to though
156             for (i = 0; i < m_poll_nfds_shadow; i++) {
157                 short events = 0;
158                 if (m_IsoHandler_map_shadow[i]->tryWaitForClient()) {
159                     events = POLLIN | POLLERR | POLLHUP;
160                     no_one_to_poll = false;
161                 }
162                 m_poll_fds_shadow[i].events = events;
163             }
164         }
165         if(no_one_to_poll) {
166             debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE,
167                                "(%p, %8s) No one to poll, waiting on the first handler to become ready\n",
168                                this, m_type==eTT_Transmit?"Transmit":"Receive");
169
170             m_IsoHandler_map_shadow[0]->waitForClient();
171
172             debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE,
173                                "(%p, %8s) handler ready\n",
174                                this, m_type==eTT_Transmit?"Transmit":"Receive");
175         }
176     }
177
178     // Use a shadow map of the fd's such that the poll call is not in a critical section
179     err = poll (m_poll_fds_shadow, m_poll_nfds_shadow, m_poll_timeout);
180
181     if (err < 0) {
182         if (errno == EINTR) {
183             return true;
184         }
185         debugFatal("poll error: %s\n", strerror (errno));
186         return false;
187     }
188
189     for (i = 0; i < m_poll_nfds_shadow; i++) {
190         #ifdef DEBUG
191         if(m_poll_fds_shadow[i].revents) {
192             debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
193                         "received events: %08X for (%d/%d, %p, %s)\n",
194                         m_poll_fds_shadow[i].revents,
195                         i, m_poll_nfds_shadow,
196                         m_IsoHandler_map_shadow[i],
197                         m_IsoHandler_map_shadow[i]->getTypeString());
198         }
199         #endif
200
201         // if we get here, it means two things:
202         // 1) the kernel can accept or provide packets (poll returned POLLIN)
203         // 2) the client can provide or accept packets (we enabled polling)
204         if(m_poll_fds_shadow[i].revents & (POLLIN)) {
205             m_IsoHandler_map_shadow[i]->iterate();
206         } else {
207             // there might be some error condition
208             if (m_poll_fds_shadow[i].revents & POLLERR) {
209                 debugWarning("error on fd for %d\n",i);
210             }
211             if (m_poll_fds_shadow[i].revents & POLLHUP) {
212                 debugWarning("hangup on fd for %d\n",i);
213             }
214         }
215
216 //         #ifdef DEBUG
217 //         // check if the handler is still alive
218 //         if(m_IsoHandler_map_shadow[i]->isDead()) {
219 //             debugError("Iso handler (%p, %s) is dead!\n",
220 //                        m_IsoHandler_map_shadow[i],
221 //                        m_IsoHandler_map_shadow[i]->getTypeString());
222 //             return false; // shutdown the system
223 //         }
224 //         #endif
225
226     }
227     return true;
228
229 }
230
231 void IsoTask::setVerboseLevel(int i) {
232     setDebugLevel(i);
233 }
234
235 // -- the ISO handler manager -- //
236 IsoHandlerManager::IsoHandlerManager(Ieee1394Service& service)
237    : m_State(E_Created)
238    , m_service( service )
239    , m_realtime(false), m_priority(0)
240    , m_ReceiveThread ( NULL )
241    , m_TransmitThread ( NULL )
242    , m_ReceiveTask ( NULL )
243    , m_TransmitTask ( NULL )
244 {}
245
246 IsoHandlerManager::IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio)
247    : m_State(E_Created)
248    , m_service( service )
249    , m_realtime(run_rt), m_priority(rt_prio)
250    , m_ReceiveThread ( NULL )
251    , m_TransmitThread ( NULL )
252    , m_ReceiveTask ( NULL )
253    , m_TransmitTask ( NULL )
254 {}
255
256 IsoHandlerManager::~IsoHandlerManager()
257 {
258     stopHandlers();
259     pruneHandlers();
260     if(m_IsoHandlers.size() > 0) {
261         debugError("Still some handlers in use\n");
262     }
263     if (m_ReceiveThread) {
264         m_ReceiveThread->Stop();
265         delete m_ReceiveThread;
266     }
267     if (m_ReceiveTask) {
268         delete m_ReceiveTask;
269     }
270
271     if (m_TransmitThread) {
272         m_TransmitThread->Stop();
273         delete m_TransmitThread;
274     }
275     if (m_TransmitTask) {
276         delete m_TransmitTask;
277     }
278 }
279
280 bool
281 IsoHandlerManager::setThreadParameters(bool rt, int priority) {
282     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) switch to: (rt=%d, prio=%d)...\n", this, rt, priority);
283     if (priority > THREAD_MAX_RTPRIO) priority = THREAD_MAX_RTPRIO; // cap the priority
284     m_realtime = rt;
285     m_priority = priority;
286     bool result = true;
287
288     if (m_ReceiveThread) {
289         if (m_realtime) {
290             m_ReceiveThread->AcquireRealTime(m_priority);
291         } else {
292             m_ReceiveThread->DropRealTime();
293         }
294     }
295     if (m_TransmitThread) {
296         if (m_realtime) {
297             m_TransmitThread->AcquireRealTime(m_priority);
298         } else {
299             m_TransmitThread->DropRealTime();
300         }
301     }
302
303     return result;
304 }
305
306 bool IsoHandlerManager::init()
307 {
308     debugOutput( DEBUG_LEVEL_VERBOSE, "Initializing ISO manager %p...\n", this);
309     // check state
310     if(m_State != E_Created) {
311         debugError("Manager already initialized...\n");
312         return false;
313     }
314
315     // create a thread to iterate our receive handlers
316     debugOutput( DEBUG_LEVEL_VERBOSE, "Create receive thread for %p...\n", this);
317     m_ReceiveTask = new IsoTask( *this, IsoTask::eTT_Receive );
318     if(!m_ReceiveTask) {
319         debugFatal("No task\n");
320         return false;
321     }
322     m_ReceiveThread = new Util::PosixThread(m_ReceiveTask, m_realtime,
323                                             m_priority + ISOHANDLERMANAGER_RECEIVE_PRIO_INCREASE,
324                                             PTHREAD_CANCEL_DEFERRED);
325
326     if(!m_ReceiveThread) {
327         debugFatal("No thread\n");
328         return false;
329     }
330     if (m_ReceiveThread->Start() != 0) {
331         debugFatal("Could not start receive thread\n");
332         return false;
333     }
334
335     // create a thread to iterate our transmit handlers
336     debugOutput( DEBUG_LEVEL_VERBOSE, "Create transmit thread for %p...\n", this);
337     m_TransmitTask = new IsoTask( *this, IsoTask::eTT_Transmit );
338     if(!m_TransmitTask) {
339         debugFatal("No task\n");
340         return false;
341     }
342     m_TransmitThread = new Util::PosixThread(m_TransmitTask, m_realtime,
343                                              m_priority + ISOHANDLERMANAGER_TRANSMIT_PRIO_INCREASE,
344                                              PTHREAD_CANCEL_DEFERRED);
345     if(!m_TransmitThread) {
346         debugFatal("No thread\n");
347         return false;
348     }
349     if (m_TransmitThread->Start() != 0) {
350         debugFatal("Could not start transmit thread\n");
351         return false;
352     }
353
354     m_State=E_Running;
355     return true;
356 }
357
358
359 bool
360 IsoHandlerManager::updateShadowMapFor(IsoHandler *h)
361 {
362     // update the shadow map
363     if(h->getType() == IsoHandler::eHT_Receive) {
364         if(!m_ReceiveTask->requestShadowMapUpdate()) {
365             debugError("failed to update shadow map\n");
366             return false;
367         }
368     } else {
369         if(!m_TransmitTask->requestShadowMapUpdate()) {
370             debugError("failed to update shadow map\n");
371             return false;
372         }
373     }
374     return true;
375 }
376
377 bool
378 IsoHandlerManager::disable(IsoHandler *h) {
379     bool result;
380     int i=0;
381     debugOutput(DEBUG_LEVEL_VERBOSE, "Disable on IsoHandler %p\n", h);
382     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
383         it != m_IsoHandlers.end();
384         ++it )
385     {
386         if ((*it) == h) {
387             result = h->disable();
388             result &= updateShadowMapFor(h);
389             debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " disabled\n");
390             return result;
391         }
392         i++;
393     }
394     debugError("Handler not found\n");
395     return false;
396 }
397
398 bool
399 IsoHandlerManager::enable(IsoHandler *h) {
400     bool result;
401     int i=0;
402     debugOutput(DEBUG_LEVEL_VERBOSE, "Enable on IsoHandler %p\n", h);
403     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
404         it != m_IsoHandlers.end();
405         ++it )
406     {
407         if ((*it) == h) {
408             result = h->enable();
409             result &= updateShadowMapFor(h);
410             debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " enabled\n");
411             return result;
412         }
413         i++;
414     }
415     debugError("Handler not found\n");
416     return false;
417 }
418
419 bool IsoHandlerManager::registerHandler(IsoHandler *handler)
420 {
421     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
422     assert(handler);
423     handler->setVerboseLevel(getDebugLevel());
424     m_IsoHandlers.push_back(handler);
425     return updateShadowMapFor(handler);
426 }
427
428 bool IsoHandlerManager::unregisterHandler(IsoHandler *handler)
429 {
430     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
431     assert(handler);
432
433     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
434       it != m_IsoHandlers.end();
435       ++it )
436     {
437         if ( *it == handler ) {
438             m_IsoHandlers.erase(it);
439             return updateShadowMapFor(handler);
440         }
441     }
442     debugFatal("Could not find handler (%p)\n", handler);
443     return false; //not found
444 }
445
446 /**
447  * Registers an StreamProcessor with the IsoHandlerManager.
448  *
449  * If nescessary, an IsoHandler is created to handle this stream.
450  * Once an StreamProcessor is registered to the handler, it will be included
451  * in the ISO streaming cycle (i.e. receive/transmit of it will occur).
452  *
453  * @param stream the stream to register
454  * @return true if registration succeeds
455  *
456  * \todo : currently there is a one-to-one mapping
457  *        between streams and handlers, this is not ok for
458  *        multichannel receive
459  */
460 bool IsoHandlerManager::registerStream(StreamProcessor *stream)
461 {
462     debugOutput( DEBUG_LEVEL_VERBOSE, "Registering stream %p\n",stream);
463     assert(stream);
464
465     IsoHandler* h = NULL;
466
467     // make sure the stream isn't already attached to a handler
468     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
469       it != m_IsoHandlers.end();
470       ++it )
471     {
472         if((*it)->isStreamRegistered(stream)) {
473             debugError( "stream already registered!\n");
474             return false;
475         }
476     }
477
478     // clean up all handlers that aren't used
479     pruneHandlers();
480
481     // allocate a handler for this stream
482     if (stream->getType()==StreamProcessor::ePT_Receive) {
483         // setup the optimal parameters for the raw1394 ISO buffering
484         unsigned int packets_per_period = stream->getPacketsPerPeriod();
485         unsigned int max_packet_size = stream->getMaxPacketSize();
486         unsigned int page_size = getpagesize() - 2; // for one reason or another this is necessary
487
488         // Ensure we don't request a packet size bigger than the
489         // kernel-enforced maximum which is currently 1 page.
490         if (max_packet_size > page_size) {
491             debugError("max packet size (%u) > page size (%u)\n", max_packet_size, page_size);
492             return false;
493         }
494
495         unsigned int irq_interval = packets_per_period / MINIMUM_INTERRUPTS_PER_PERIOD;
496         if(irq_interval <= 0) irq_interval=1;
497        
498         // the receive buffer size doesn't matter for the latency,
499         // but it has a minimal value in order for libraw to operate correctly (300)
500         int buffers=400;
501
502         // create the actual handler
503         h = new IsoHandler(*this, IsoHandler::eHT_Receive,
504                            buffers, max_packet_size, irq_interval);
505
506         debugOutput( DEBUG_LEVEL_VERBOSE, " creating IsoRecvHandler\n");
507
508         if(!h) {
509             debugFatal("Could not create IsoRecvHandler\n");
510             return false;
511         }
512
513     } else if (stream->getType()==StreamProcessor::ePT_Transmit) {
514         // setup the optimal parameters for the raw1394 ISO buffering
515 //        unsigned int packets_per_period = stream->getPacketsPerPeriod();
516         unsigned int max_packet_size = stream->getMaxPacketSize();
517 //         unsigned int page_size = getpagesize();
518
519         // Ensure we don't request a packet size bigger than the
520         // kernel-enforced maximum which is currently 1 page.
521 //         if (max_packet_size > page_size) {
522 //             debugError("max packet size (%u) > page size (%u)\n", max_packet_size, page_size);
523 //             return false;
524 //         }
525         if (max_packet_size > MAX_XMIT_PACKET_SIZE) {
526             debugError("max packet size (%u) > MAX_XMIT_PACKET_SIZE (%u)\n",
527                        max_packet_size, MAX_XMIT_PACKET_SIZE);
528             return false;
529         }
530
531         // the SP specifies how many packets to ISO-buffer
532         int buffers = stream->getNbPacketsIsoXmitBuffer();
533         if (buffers > MAX_XMIT_NB_BUFFERS) {
534             debugOutput(DEBUG_LEVEL_VERBOSE,
535                         "nb buffers (%u) > MAX_XMIT_NB_BUFFERS (%u)\n",
536                         buffers, MAX_XMIT_NB_BUFFERS);
537             buffers = MAX_XMIT_NB_BUFFERS;
538         }
539         unsigned int irq_interval = buffers / MINIMUM_INTERRUPTS_PER_PERIOD;
540         if(irq_interval <= 0) irq_interval=1;
541
542         debugOutput( DEBUG_LEVEL_VERBOSE, " creating IsoXmitHandler\n");
543
544         // create the actual handler
545         h = new IsoHandler(*this, IsoHandler::eHT_Transmit,
546                            buffers, max_packet_size, irq_interval);
547
548         if(!h) {
549             debugFatal("Could not create IsoXmitHandler\n");
550             return false;
551         }
552
553     } else {
554         debugFatal("Bad stream type\n");
555         return false;
556     }
557
558     h->setVerboseLevel(getDebugLevel());
559
560     // init the handler
561     if(!h->init()) {
562         debugFatal("Could not initialize receive handler\n");
563         return false;
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     #ifdef DEBUG
811     unsigned int i=0;
812     debugOutputShort( DEBUG_LEVEL_NORMAL, "Dumping IsoHandlerManager Stream handler information...\n");
813     debugOutputShort( DEBUG_LEVEL_NORMAL, " State: %d\n",(int)m_State);
814
815     for ( IsoHandlerVectorIterator it = m_IsoHandlers.begin();
816           it != m_IsoHandlers.end();
817           ++it )
818     {
819         debugOutputShort( DEBUG_LEVEL_NORMAL, " IsoHandler %d (%p)\n",i++,*it);
820         (*it)->dumpInfo();
821     }
822     #endif
823 }
824
825 const char *
826 IsoHandlerManager::eHSToString(enum eHandlerStates s) {
827     switch (s) {
828         default: return "Invalid";
829         case E_Created: return "Created";
830         case E_Prepared: return "Prepared";
831         case E_Running: return "Running";
832         case E_Error: return "Error";
833     }
834 }
Note: See TracBrowser for help on using the browser.