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

Revision 967, 6.3 kB (checked in by ppalmers, 16 years ago)

- first attempt at not causing total havoc when devices are removed from the bus.

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