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

Revision 967, 4.7 kB (checked in by ppalmers, 13 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_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
72     // this is the setup API
73     bool registerProcessor(StreamProcessor *processor); ///< start managing a streamprocessor
74     bool unregisterProcessor(StreamProcessor *processor); ///< stop managing a streamprocessor
75
76     void setPeriodSize(unsigned int period)
77             {m_period = period;};
78     unsigned int getPeriodSize()
79             {return m_period;};
80
81     bool setAudioDataType(enum eADT_AudioDataType t)
82         {m_audio_datatype = t; return true;};
83     enum eADT_AudioDataType getAudioDataType()
84         {return m_audio_datatype;}
85
86     void setNbBuffers(unsigned int nb_buffers)
87             {m_nb_buffers = nb_buffers;};
88     int getNbBuffers()
89             {return m_nb_buffers;};
90
91     int getPortCount(enum Port::E_PortType, enum Port::E_Direction);
92     int getPortCount(enum Port::E_Direction);
93     Port* getPortByIndex(int idx, enum Port::E_Direction);
94
95     // the client-side functions
96     bool waitForPeriod();
97     bool transfer();
98     bool transfer(enum StreamProcessor::eProcessorType);
99 private:
100     bool transferSilence();
101     bool transferSilence(enum StreamProcessor::eProcessorType);
102
103     bool alignReceivedStreams();
104 public:
105     int getDelayedUsecs() {return m_delayed_usecs;};
106     bool xrunOccurred();
107     bool shutdownNeeded() {return m_shutdown_needed;};
108     int getXrunCount() {return m_xruns;};
109
110     void setNominalRate(unsigned int r) {m_nominal_framerate = r;};
111     unsigned int getNominalRate() {return m_nominal_framerate;};
112     uint64_t getTimeOfLastTransfer() { return m_time_of_transfer;};
113
114 private:
115     int m_delayed_usecs;
116     // this stores the time at which the next transfer should occur
117     // usually this is in the past, but it is needed as a timestamp
118     // for the transmit SP's
119     uint64_t m_time_of_transfer;
120
121 public:
122     bool handleXrun(); ///< reset the streams & buffers after xrun
123
124     bool setThreadParameters(bool rt, int priority);
125
126     virtual void setVerboseLevel(int l);
127     void dumpInfo();
128
129 private: // slaving support
130     bool m_is_slave;
131
132     // the sync source stuff
133 private:
134     StreamProcessor *m_SyncSource;
135
136 public:
137     bool setSyncSource(StreamProcessor *s);
138     StreamProcessor& getSyncSource()
139         {return *m_SyncSource;};
140
141 protected:
142
143     // thread sync primitives
144     bool m_xrun_happened;
145     bool m_thread_realtime;
146     int m_thread_priority;
147
148     // processor list
149     StreamProcessorVector m_ReceiveProcessors;
150     StreamProcessorVector m_TransmitProcessors;
151
152     unsigned int m_nb_buffers;
153     unsigned int m_period;
154     enum eADT_AudioDataType m_audio_datatype;
155     unsigned int m_nominal_framerate;
156     unsigned int m_xruns;
157     bool m_shutdown_needed;
158
159     unsigned int m_nbperiods;
160
161     Util::PosixMutex m_WaitLock;
162
163     DECLARE_DEBUG_MODULE;
164
165 };
166
167 }
168
169 #endif /* __FFADO_STREAMPROCESSORMANAGER__ */
170
171
Note: See TracBrowser for help on using the browser.