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

Revision 750, 6.3 kB (checked in by ppalmers, 15 years ago)

Code refactoring. Tries to simplify things and tries to put all code where it belongs.

Line 
1 /*
2  * Copyright (C) 2005-2007 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 3 of the License, or
12  * (at your option) any later version.
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 "debugmodule/debugmodule.h"
28
29 #include "libutil/Thread.h"
30
31 #include <sys/poll.h>
32 #include <errno.h>
33
34 #include <vector>
35
36 #define FFADO_MAX_ISO_HANDLERS_PER_PORT 16
37
38 #define USLEEP_AFTER_UPDATE_FAILURE 10
39 #define USLEEP_AFTER_UPDATE 100
40 #define MAX_UPDATE_TRIES 10
41 class Ieee1394Service;
42 namespace Util {
43     class PosixThread;
44 }
45
46 class IsoHandler;
47 namespace Streaming {
48     class StreamProcessor;
49     class StreamProcessorManager;
50     typedef std::vector<StreamProcessor *> StreamProcessorVector;
51     typedef std::vector<StreamProcessor *>::iterator StreamProcessorVectorIterator;
52 }
53
54 typedef std::vector<IsoHandler *> IsoHandlerVector;
55 typedef std::vector<IsoHandler *>::iterator IsoHandlerVectorIterator;
56
57 /*!
58 \brief The ISO Handler management class
59
60  This class manages the use of ISO handlers by ISO streams.
61  You can register an Streaming::StreamProcessor with an IsoHandlerManager. This
62  manager will assign an IsoHandler to the stream. If nescessary
63  the manager allocates a new handler. If there is already a handler
64  that can handle the Streaming::StreamProcessor (e.g. in case of multichannel receive),
65  it can be assigned.
66
67 */
68 class IsoHandlerManager : public Util::RunnableInterface
69 {
70     friend class Streaming::StreamProcessorManager;
71
72     public:
73
74         IsoHandlerManager(Ieee1394Service& service);
75         IsoHandlerManager(Ieee1394Service& service, bool run_rt, unsigned int rt_prio);
76         virtual ~IsoHandlerManager();
77
78         bool setThreadParameters(bool rt, int priority);
79
80         void setPollTimeout(int t) {m_poll_timeout=t;}; ///< set the timeout used for poll()
81         int getPollTimeout() {return m_poll_timeout;};  ///< get the timeout used for poll()
82
83         void setTransmitBufferNbFrames(unsigned int t) {m_xmit_nb_frames = t;};
84         int getTransmitBufferNbFrames() {return m_xmit_nb_frames;};
85
86         void setVerboseLevel(int l); ///< set the verbose level
87
88         void dumpInfo(); ///< print some information about the manager to stdout/stderr
89
90         bool registerStream(Streaming::StreamProcessor *); ///< register an iso stream with the manager
91         bool unregisterStream(Streaming::StreamProcessor *); ///< unregister an iso stream from the manager
92
93         bool startHandlers(); ///< start the managed ISO handlers
94         bool startHandlers(int cycle); ///< start the managed ISO handlers
95         bool stopHandlers(); ///< stop the managed ISO handlers
96
97         bool reset(); ///< reset the ISO manager and all streams
98         bool init();
99
100         bool disable(IsoHandler *); ///< disables a handler
101         bool enable(IsoHandler *); ///< enables a handler
102         ///> disables the handler attached to the stream
103         bool stopHandlerForStream(Streaming::StreamProcessor *);
104         ///> starts the handler attached to the specific stream
105         bool startHandlerForStream(Streaming::StreamProcessor *);
106         ///> starts the handler attached to the specific stream on a specific cycle
107         bool startHandlerForStream(Streaming::StreamProcessor *, int cycle);
108
109         /**
110          * returns the latency of a wake-up for this stream.
111          * The latency is the time it takes for a packet is delivered to the
112          * stream after it has been received (was on the wire).
113          * expressed in cycles
114          */
115         int getPacketLatencyForStream(Streaming::StreamProcessor *);
116
117         void flushHandlerForStream(Streaming::StreamProcessor *stream);
118
119         Ieee1394Service& get1394Service() {return m_service;};
120     // RunnableInterface interface
121     public:
122         bool Execute(); // note that this is called in we while(running) loop
123         bool Init();
124        
125         // protects the operations on the lists
126         // (FIXME: should be changed into a lock-free approach)
127         pthread_mutex_t m_list_lock;
128
129     // the state machine
130     private:
131         enum eHandlerStates {
132             E_Created,
133             E_Prepared,
134             E_Running,
135             E_Error
136         };
137
138         enum eHandlerStates m_State;
139         const char *eHSToString(enum eHandlerStates);
140     private:
141         /// iterate all child handlers
142         bool iterate();
143
144     private:
145         Ieee1394Service&  m_service;
146         // note: there is a disctinction between streams and handlers
147         // because one handler can serve multiple streams (in case of
148         // multichannel receive)
149
150         // only streams are allowed to be registered externally.
151         // we allocate a handler if we need one, otherwise the stream
152         // is assigned to another handler
153
154         // the collection of handlers
155         IsoHandlerVector m_IsoHandlers;
156
157         bool registerHandler(IsoHandler *);
158         bool unregisterHandler(IsoHandler *);
159         void pruneHandlers();
160
161         // the collection of streams
162         Streaming::StreamProcessorVector m_StreamProcessors;
163
164         // poll stuff
165         int m_poll_timeout;
166         // FD map sync requested
167         int32_t m_request_fdmap_update;
168         void updateShadowVars();
169
170         // shadow variables
171         struct pollfd m_poll_fds_shadow[FFADO_MAX_ISO_HANDLERS_PER_PORT];
172         IsoHandler *m_IsoHandler_map_shadow[FFADO_MAX_ISO_HANDLERS_PER_PORT];
173         unsigned int m_poll_nfds_shadow;
174
175         void requestShadowUpdate();
176
177         // threading
178         bool m_realtime;
179         unsigned int m_priority;
180         Util::PosixThread *m_isoManagerThread;
181
182         // the preferred number of packets to buffer on xmit
183         unsigned int m_xmit_nb_frames;
184
185         // debug stuff
186         DECLARE_DEBUG_MODULE;
187
188 };
189
190 #endif /* __FFADO_ISOHANDLERMANAGER__  */
191
192
193
Note: See TracBrowser for help on using the browser.