root/trunk/libffado/src/libstreaming/StreamProcessorManager.h

Revision 1005, 5.0 kB (checked in by ppalmers, 13 years ago)

Improve thread synchronisation. Switch back to separate threads for transmit and
receive since it is not possible to statically schedule things properly. One
of the threads (i.e. the client thread) is out of our control, hence it's
execution can't be controlled. Using separate threads and correct priorities
will shift this problem to the OS. Note that the priority of the packet
receive thread should be lower than the client thread (such that the client
thread is woken ASAP), and the priority of the transmit thread should be
higher than the client thread (such that packets are queued ASAP).
Extra benefit: multi-cores are used.

Some other startup improvements.

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_STREAMPROCESSORMANAGER__
25 #define __FFADO_STREAMPROCESSORMANAGER__
26
27 #include "generic/Port.h"
28 #include "generic/StreamProcessor.h"
29
30 #include "debugmodule/debugmodule.h"
31 #include "libutil/Thread.h"
32 #include "libutil/PosixMutex.h"
33 #include "libutil/OptionContainer.h"
34
35 #include <vector>
36 #include <semaphore.h>
37
38 namespace Streaming {
39
40 class StreamProcessor;
41
42 typedef std::vector<StreamProcessor *> StreamProcessorVector;
43 typedef std::vector<StreamProcessor *>::iterator StreamProcessorVectorIterator;
44
45 /*!
46 \brief Manages a collection of StreamProcessors and provides a synchronisation interface
47
48 */
49 class StreamProcessorManager : public Util::OptionContainer {
50     friend class StreamProcessor;
51
52 public:
53     enum eADT_AudioDataType {
54         eADT_Int24,
55         eADT_Float,
56     };
57
58     StreamProcessorManager();
59     StreamProcessorManager(unsigned int period, unsigned int rate, unsigned int nb_buffers);
60     virtual ~StreamProcessorManager();
61
62     void handleBusReset();
63
64     bool prepare(); ///< to be called after the processors are registered
65
66     bool start();
67     bool stop();
68
69     bool startDryRunning();
70     bool syncStartAll();
71     // activity signaling
72     enum eActivityResult {
73         eAR_Activity,
74         eAR_Timeout,
75         eAR_Interrupted,
76         eAR_Error
77     };
78     void signalActivity();
79     enum eActivityResult waitForActivity();
80
81     // this is the setup API
82     bool registerProcessor(StreamProcessor *processor); ///< start managing a streamprocessor
83     bool unregisterProcessor(StreamProcessor *processor); ///< stop managing a streamprocessor
84
85     void setPeriodSize(unsigned int period)
86             {m_period = period;};
87     unsigned int getPeriodSize()
88             {return m_period;};
89
90     bool setAudioDataType(enum eADT_AudioDataType t)
91         {m_audio_datatype = t; return true;};
92     enum eADT_AudioDataType getAudioDataType()
93         {return m_audio_datatype;}
94
95     void setNbBuffers(unsigned int nb_buffers)
96             {m_nb_buffers = nb_buffers;};
97     int getNbBuffers()
98             {return m_nb_buffers;};
99
100     int getPortCount(enum Port::E_PortType, enum Port::E_Direction);
101     int getPortCount(enum Port::E_Direction);
102     Port* getPortByIndex(int idx, enum Port::E_Direction);
103
104     // the client-side functions
105     bool waitForPeriod();
106     bool transfer();
107     bool transfer(enum StreamProcessor::eProcessorType);
108 private:
109     bool transferSilence();
110     bool transferSilence(enum StreamProcessor::eProcessorType);
111
112     bool alignReceivedStreams();
113 public:
114     int getDelayedUsecs() {return m_delayed_usecs;};
115     bool xrunOccurred();
116     bool shutdownNeeded() {return m_shutdown_needed;};
117     int getXrunCount() {return m_xruns;};
118
119     void setNominalRate(unsigned int r) {m_nominal_framerate = r;};
120     unsigned int getNominalRate() {return m_nominal_framerate;};
121     uint64_t getTimeOfLastTransfer() { return m_time_of_transfer;};
122
123 private:
124     int m_delayed_usecs;
125     // this stores the time at which the next transfer should occur
126     // usually this is in the past, but it is needed as a timestamp
127     // for the transmit SP's
128     uint64_t m_time_of_transfer;
129
130 public:
131     bool handleXrun(); ///< reset the streams & buffers after xrun
132
133     bool setThreadParameters(bool rt, int priority);
134
135     virtual void setVerboseLevel(int l);
136     void dumpInfo();
137
138 private: // slaving support
139     bool m_is_slave;
140
141     // the sync source stuff
142 private:
143     StreamProcessor *m_SyncSource;
144
145 public:
146     bool setSyncSource(StreamProcessor *s);
147     StreamProcessor& getSyncSource()
148         {return *m_SyncSource;};
149
150 protected: // FIXME: private?
151
152     // thread related vars
153     bool m_xrun_happened;
154     int m_activity_wait_timeout_usec;
155     bool m_thread_realtime;
156     int m_thread_priority;
157
158     // activity signaling
159     sem_t m_activity_semaphore;
160
161     // processor list
162     StreamProcessorVector m_ReceiveProcessors;
163     StreamProcessorVector m_TransmitProcessors;
164
165     unsigned int m_nb_buffers;
166     unsigned int m_period;
167     enum eADT_AudioDataType m_audio_datatype;
168     unsigned int m_nominal_framerate;
169     unsigned int m_xruns;
170     bool m_shutdown_needed;
171
172     unsigned int m_nbperiods;
173
174     Util::PosixMutex m_WaitLock;
175
176     DECLARE_DEBUG_MODULE;
177
178 };
179
180 }
181
182 #endif /* __FFADO_STREAMPROCESSORMANAGER__ */
183
184
Note: See TracBrowser for help on using the browser.