root/branches/libffado-2.0/src/libieee1394/IsoHandlerManager.h

Revision 1263, 7.3 kB (checked in by ppalmers, 16 years ago)

improve the behavior when confronted with dying iso transmit handlers (usually due to host controller issues)

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 #ifndef __FFADO_ISOHANDLERMANAGER__
25 #define __FFADO_ISOHANDLERMANAGER__
26
27 #include "config.h"
28 #include "debugmodule/debugmodule.h"
29
30 #include "libutil/Thread.h"
31
32 #include "IsoHandler.h"
33
34 #include <sys/poll.h>
35 #include <errno.h>
36 #include <vector>
37 #include <semaphore.h>
38
39 class Ieee1394Service;
40 //class IsoHandler;
41 //enum IsoHandler::EHandlerType;
42
43 namespace Streaming {
44     class StreamProcessor;
45     typedef std::vector<StreamProcessor *> StreamProcessorVector;
46     typedef std::vector<StreamProcessor *>::iterator StreamProcessorVectorIterator;
47 }
48
49 typedef std::vector<IsoHandler *> IsoHandlerVector;
50 typedef std::vector<IsoHandler *>::iterator IsoHandlerVectorIterator;
51
52 class IsoHandlerManager;
53
54 // threads that will handle the packet framing
55 // one thread per direction, as a compromise for one per
56 // channel and one for all
57 class IsoTask : public Util::RunnableInterface
58 {
59     public:
60         IsoTask(IsoHandlerManager& manager, enum IsoHandler::EHandlerType);
61         virtual ~IsoTask();
62
63     public:
64         bool Init();
65         bool Execute();
66
67         /**
68          * @brief requests the thread to sync it's stream map with the manager
69          */
70         bool requestShadowMapUpdate();
71         enum eActivityResult {
72             eAR_Activity,
73             eAR_Timeout,
74             eAR_Interrupted,
75             eAR_Error
76         };
77
78         /**
79          * @brief signals that something happened in one of the clients of this task
80          */
81         void signalActivity();
82         /**
83          * @brief wait until something happened in one of the clients of this task
84          */
85         enum eActivityResult waitForActivity();
86
87         void setVerboseLevel(int i);
88     protected:
89         IsoHandlerManager& m_manager;
90
91         // the event request structure
92         int32_t request_update;
93
94         // static allocation due to RT constraints
95         // this is the map used by the actual thread
96         // it is a shadow of the m_StreamProcessors vector
97         struct pollfd   m_poll_fds_shadow[ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT];
98         IsoHandler *    m_IsoHandler_map_shadow[ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT];
99         unsigned int    m_poll_nfds_shadow;
100         IsoHandler *    m_SyncIsoHandler;
101
102         // updates the streams map
103         void updateShadowMapHelper();
104
105 #ifdef DEBUG
106         uint64_t m_last_loop_entry;
107         int m_successive_short_loops;
108 #endif
109
110         // activity signaling
111         sem_t m_activity_semaphore;
112
113         enum IsoHandler::EHandlerType m_handlerType;
114         bool m_running;
115
116         // debug stuff
117         DECLARE_DEBUG_MODULE;
118 };
119
120 /*!
121 \brief The ISO Handler management class
122
123  This class manages the use of ISO handlers by ISO streams.
124  You can register an Streaming::StreamProcessor with an IsoHandlerManager. This
125  manager will assign an IsoHandler to the stream. If nescessary
126  the manager allocates a new handler. If there is already a handler
127  that can handle the Streaming::StreamProcessor (e.g. in case of multichannel receive),
128  it can be assigned.
129
130 */
131
132 class IsoHandlerManager
133 {
134     friend class IsoTask;
135
136     public:
137
138         IsoHandlerManager(Ieee1394Service& service);
139         IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio);
140         virtual ~IsoHandlerManager();
141
142         bool setThreadParameters(bool rt, int priority);
143
144         void setVerboseLevel(int l); ///< set the verbose level
145
146         void dumpInfo(); ///< print some information about the manager to stdout/stderr
147
148         bool registerStream(Streaming::StreamProcessor *); ///< register an iso stream with the manager
149         bool unregisterStream(Streaming::StreamProcessor *); ///< unregister an iso stream from the manager
150
151         bool startHandlers(); ///< start the managed ISO handlers
152         bool startHandlers(int cycle); ///< start the managed ISO handlers
153         bool stopHandlers(); ///< stop the managed ISO handlers
154
155         bool reset(); ///< reset the ISO manager and all streams
156         bool init();
157
158         bool disable(IsoHandler *); ///< disables a handler
159         bool enable(IsoHandler *); ///< enables a handler
160
161         /**
162          * @brief signals that something happened in one of the clients
163          */
164         void signalActivityTransmit();
165         void signalActivityReceive();
166
167         ///> disables the handler attached to the stream
168         bool stopHandlerForStream(Streaming::StreamProcessor *);
169         ///> starts the handler attached to the specific stream
170         bool startHandlerForStream(Streaming::StreamProcessor *);
171         ///> starts the handler attached to the specific stream on a specific cycle
172         bool startHandlerForStream(Streaming::StreamProcessor *, int cycle);
173
174         /**
175          * returns the latency of a wake-up for this stream.
176          * The latency is the time it takes for a packet is delivered to the
177          * stream after it has been received (was on the wire).
178          * expressed in cycles
179          */
180         int getPacketLatencyForStream(Streaming::StreamProcessor *);
181
182         void flushHandlerForStream(Streaming::StreamProcessor *stream);
183         IsoHandler * getHandlerForStream(Streaming::StreamProcessor *stream);
184
185         Ieee1394Service& get1394Service() {return m_service;};
186
187         void requestShadowMapUpdate();
188
189     // the state machine
190     private:
191         enum eHandlerStates {
192             E_Created,
193             E_Prepared,
194             E_Running,
195             E_Error
196         };
197
198         enum eHandlerStates m_State;
199         const char *eHSToString(enum eHandlerStates);
200
201     private:
202         Ieee1394Service&  m_service;
203         // note: there is a disctinction between streams and handlers
204         // because one handler can serve multiple streams (in case of
205         // multichannel receive)
206
207         // only streams are allowed to be registered externally.
208         // we allocate a handler if we need one, otherwise the stream
209         // is assigned to another handler
210
211         // the collection of handlers
212         IsoHandlerVector m_IsoHandlers;
213
214         bool registerHandler(IsoHandler *);
215         bool unregisterHandler(IsoHandler *);
216         void pruneHandlers();
217
218         // the collection of streams
219         Streaming::StreamProcessorVector m_StreamProcessors;
220
221         // handler thread/task
222         bool            m_realtime;
223         int             m_priority;
224         Util::Thread *  m_IsoThreadTransmit;
225         IsoTask *       m_IsoTaskTransmit;
226         Util::Thread *  m_IsoThreadReceive;
227         IsoTask *       m_IsoTaskReceive;
228
229         // debug stuff
230         DECLARE_DEBUG_MODULE;
231
232 };
233
234 #endif /* __FFADO_ISOHANDLERMANAGER__  */
235
236
237
Note: See TracBrowser for help on using the browser.