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

Revision 938, 6.2 kB (checked in by ppalmers, 16 years ago)

implement static iso handler scheduling

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
110     public:
111
112         IsoHandlerManager(Ieee1394Service& service);
113         IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio);
114         virtual ~IsoHandlerManager();
115
116         bool setThreadParameters(bool rt, int priority);
117
118         void setVerboseLevel(int l); ///< set the verbose level
119
120         void dumpInfo(); ///< print some information about the manager to stdout/stderr
121
122         bool registerStream(Streaming::StreamProcessor *); ///< register an iso stream with the manager
123         bool unregisterStream(Streaming::StreamProcessor *); ///< unregister an iso stream from the manager
124
125         bool startHandlers(); ///< start the managed ISO handlers
126         bool startHandlers(int cycle); ///< start the managed ISO handlers
127         bool stopHandlers(); ///< stop the managed ISO handlers
128
129         bool reset(); ///< reset the ISO manager and all streams
130         bool init();
131
132         bool disable(IsoHandler *); ///< disables a handler
133         bool enable(IsoHandler *); ///< enables a handler
134         ///> disables the handler attached to the stream
135         bool stopHandlerForStream(Streaming::StreamProcessor *);
136         ///> starts the handler attached to the specific stream
137         bool startHandlerForStream(Streaming::StreamProcessor *);
138         ///> starts the handler attached to the specific stream on a specific cycle
139         bool startHandlerForStream(Streaming::StreamProcessor *, int cycle);
140
141         /**
142          * returns the latency of a wake-up for this stream.
143          * The latency is the time it takes for a packet is delivered to the
144          * stream after it has been received (was on the wire).
145          * expressed in cycles
146          */
147         int getPacketLatencyForStream(Streaming::StreamProcessor *);
148
149         void flushHandlerForStream(Streaming::StreamProcessor *stream);
150
151         Ieee1394Service& get1394Service() {return m_service;};
152
153     // the state machine
154     private:
155         enum eHandlerStates {
156             E_Created,
157             E_Prepared,
158             E_Running,
159             E_Error
160         };
161
162         enum eHandlerStates m_State;
163         const char *eHSToString(enum eHandlerStates);
164
165     private:
166         Ieee1394Service&  m_service;
167         // note: there is a disctinction between streams and handlers
168         // because one handler can serve multiple streams (in case of
169         // multichannel receive)
170
171         // only streams are allowed to be registered externally.
172         // we allocate a handler if we need one, otherwise the stream
173         // is assigned to another handler
174
175         // the collection of handlers
176         IsoHandlerVector m_IsoHandlers;
177
178         bool registerHandler(IsoHandler *);
179         bool unregisterHandler(IsoHandler *);
180         void pruneHandlers();
181
182         // the collection of streams
183         Streaming::StreamProcessorVector m_StreamProcessors;
184
185         // handler thread/task
186         bool            m_realtime;
187         int             m_priority;
188         Util::Thread *  m_IsoThread;
189         IsoTask *       m_IsoTask;
190
191         // debug stuff
192         DECLARE_DEBUG_MODULE;
193
194 };
195
196 #endif /* __FFADO_ISOHANDLERMANAGER__  */
197
198
199
Note: See TracBrowser for help on using the browser.