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

Revision 1373, 7.6 kB (checked in by ppalmers, 12 years ago)

make IsoManager? and SPM settings from config.h overridable by the user/distro config file.

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