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

Revision 1531, 7.4 kB (checked in by ppalmers, 12 years ago)

svn merge -r 1506:HEAD svn+ssh://ffadosvn@ffado.org/ffado/branches/libffado-2.0

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