root/branches/libffado-2.0/src/libavc/general/avc_plug.h

Revision 1238, 12.3 kB (checked in by ppalmers, 16 years ago)

move global id bookkeeping to the plugmanager, being a per-device singleton. plug id's only have to be unique within one device. correctly detect the case where a PCR output plug has no connections.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  * Copyright (C) 2005-2008 by Daniel Wagner
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) version 3 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #ifndef AVC_PLUG
26 #define AVC_PLUG
27
28 #include "../ccm/avc_signal_source.h"
29 #include "../streamformat/avc_extended_stream_format.h"
30 #include "../avc_definitions.h"
31 #include "avc_generic.h"
32
33 #include "libutil/serialize.h"
34
35 #include "debugmodule/debugmodule.h"
36
37 #include <string>
38
39 class Ieee1394Service;
40 class ConfigRom;
41
42 namespace AVC {
43
44 class Unit;
45 class Subunit;
46 class PlugManager;
47
48 class Plug;
49 typedef std::vector<Plug*> PlugVector;
50
51 class PlugConnection;
52 typedef std::vector<PlugConnection*> PlugConnectionVector;
53
54
55 class Plug {
56 public:
57
58     enum EPlugAddressType {
59         eAPA_PCR,
60         eAPA_ExternalPlug,
61         eAPA_AsynchronousPlug,
62         eAPA_SubunitPlug,
63         eAPA_FunctionBlockPlug,
64         eAPA_Undefined,
65     };
66     std::string plugAddressTypeToString(enum EPlugAddressType t);
67
68     enum EPlugType {
69         eAPT_IsoStream,
70         eAPT_AsyncStream,
71         eAPT_Midi,
72         eAPT_Sync,
73         eAPT_Analog,
74         eAPT_Digital,
75         eAPT_Unknown,
76     };
77     std::string plugTypeToString(enum EPlugType t);
78
79     enum EPlugDirection {
80         eAPD_Input,
81         eAPD_Output,
82         eAPD_Unknown,
83     };
84     std::string plugDirectionToString(enum EPlugDirection t);
85
86     // \todo This constructors sucks. too many parameters. fix it.
87     Plug( Unit* unit,
88           Subunit* subunit,
89           function_block_type_t functionBlockType,
90           function_block_type_t functionBlockId,
91           EPlugAddressType plugAddressType,
92           EPlugDirection plugDirection,
93           plug_id_t plugId );
94     Plug( Unit* unit,
95           Subunit* subunit,
96           function_block_type_t functionBlockType,
97           function_block_type_t functionBlockId,
98           EPlugAddressType plugAddressType,
99           EPlugDirection plugDirection,
100           plug_id_t plugId,
101           int globalId );
102     Plug( const Plug& rhs );
103     virtual ~Plug();
104
105     bool inquireConnnection( Plug& plug );
106     bool setConnection( Plug& plug );
107
108     int getSignalSource();
109
110     int getGlobalId() const
111         { return m_globalId; }
112     plug_id_t getPlugId() const
113         { return m_id; }
114     ESubunitType getSubunitType() const;
115     subunit_id_t getSubunitId() const;
116
117     const char* getName() const
118         { return m_name.c_str(); }
119     bool setName(const char *n)
120         { m_name=n; return true; }
121     bool setName(std::string n)
122         { m_name=n; return true; }
123     EPlugDirection getPlugDirection() const
124         { return m_direction; }
125     bool setPlugDirection(EPlugDirection d)
126         { m_direction=d; return true; }
127     sampling_frequency_t getSamplingFrequency() const
128         { return m_samplingFrequency; }
129     int getSampleRate() const; // 22050, 24000, 32000, ...
130     bool setSampleRate( int rate );
131
132     int getNrOfChannels() const;
133     bool setNrOfChannels(int i);
134     int getNrOfStreams() const;
135
136     // FIXME: this is the same as getPlugDirection
137     EPlugDirection getDirection() const
138         { return m_direction; }
139     EPlugAddressType getPlugAddressType() const
140         { return m_addressType; }
141     EPlugType getPlugType() const
142         { return m_infoPlugType; }
143     bool setPlugType(EPlugType t)
144         { m_infoPlugType=t; return true; }
145
146     function_block_type_t getFunctionBlockType() const
147         { return m_functionBlockType; }
148     function_block_id_t getFunctionBlockId() const
149         { return m_functionBlockId; }
150
151 //     const PlugVector& getInputConnections() const
152 //         { return m_inputConnections; }
153 //     const PlugVector& getOutputConnections() const
154 //         { return m_outputConnections; }
155     PlugVector& getInputConnections()
156         { return m_inputConnections; }
157     PlugVector& getOutputConnections()
158         { return m_outputConnections; }
159
160     static PlugAddress::EPlugDirection convertPlugDirection(
161     EPlugDirection direction);
162
163     void showPlug() const;
164
165     bool serialize( std::string basePath, Util::IOSerialize& ser ) const;
166     static Plug* deserialize( std::string basePath,
167                               Util::IODeserialize& deser,
168                               Unit& avDevice,
169                               PlugManager& plugManager );
170     bool deserializeConnections( std::string basePath,
171                                  Util::IODeserialize& deser );
172     bool deserializeUpdateSubunit();
173
174  public:
175     struct ChannelInfo {
176         stream_position_t          m_streamPosition;
177         stream_position_location_t m_location;
178         std::string              m_name;
179     };
180     typedef std::vector<ChannelInfo> ChannelInfoVector;
181
182     struct ClusterInfo {
183         int                 m_index;
184         port_type_t         m_portType;
185         std::string         m_name;
186
187         nr_of_channels_t    m_nrOfChannels;
188         ChannelInfoVector   m_channelInfos;
189         stream_format_t     m_streamFormat;
190     };
191     typedef std::vector<ClusterInfo> ClusterInfoVector;
192
193     ClusterInfoVector& getClusterInfos()
194         { return m_clusterInfos; }
195
196     virtual void setVerboseLevel( int i );
197
198 // the discovery interface
199 // everything can be overloaded, or
200 // can be left as is
201 public:
202     virtual bool discover();
203     virtual bool discoverConnections();
204     virtual bool propagateFromConnectedPlug( );
205
206 protected:
207     virtual bool discoverPlugType();
208     virtual bool discoverName();
209     virtual bool discoverNoOfChannels();
210     virtual bool discoverChannelPosition();
211     virtual bool discoverChannelName();
212     virtual bool discoverClusterInfo();
213     virtual bool discoverStreamFormat();
214     virtual bool discoverSupportedStreamFormats();
215     virtual bool discoverConnectionsInput();
216     virtual bool discoverConnectionsOutput();
217     virtual bool initFromDescriptor();
218
219     bool propagateFromPlug( Plug *p );
220
221     ExtendedStreamFormatCmd setPlugAddrToStreamFormatCmd(
222             ExtendedStreamFormatCmd::ESubFunction subFunction);
223
224 protected:
225     Plug();
226
227     SignalSourceCmd setSrcPlugAddrToSignalCmd();
228
229     void setDestPlugAddrToSignalCmd(
230             SignalSourceCmd& signalSourceCmd, Plug& plug );
231
232     void debugOutputClusterInfos( int debugLevel );
233
234     bool addPlugConnection( PlugVector& connections, Plug& plug );
235
236     bool discoverConnectionsFromSpecificData(
237         EPlugDirection discoverDirection,
238         PlugAddressSpecificData* plugAddress,
239         PlugVector& connections );
240
241     Plug* getPlugDefinedBySpecificData(
242         UnitPlugSpecificDataPlugAddress* pUnitPlugAddress,
243         SubunitPlugSpecificDataPlugAddress* pSubunitPlugAddress,
244         FunctionBlockPlugSpecificDataPlugAddress* pFunctionBlockPlugAddress );
245
246     EPlugDirection toggleDirection( EPlugDirection direction ) const;
247
248     const ClusterInfo* getClusterInfoByIndex( int index ) const;
249
250     bool serializeChannelInfos( std::string basePath,
251                                 Util::IOSerialize& ser,
252                                 const ClusterInfo& clusterInfo ) const;
253     bool deserializeChannelInfos( std::string basePath,
254                                   Util::IODeserialize& deser,
255                                   ClusterInfo& clusterInfo );
256
257     bool serializeClusterInfos( std::string basePath,
258                                 Util::IOSerialize& ser ) const;
259     bool deserializeClusterInfos( std::string basePath,
260                                   Util::IODeserialize& deser );
261
262     bool serializeFormatInfos( std::string basePath,
263                                Util::IOSerialize& ser ) const;
264     bool deserializeFormatInfos( std::string basePath,
265                                  Util::IODeserialize& deser );
266
267 protected:
268     // Supported stream formats
269     struct FormatInfo {
270         FormatInfo()
271             : m_samplingFrequency( eSF_DontCare )
272             , m_isSyncStream( false )
273             , m_audioChannels( 0 )
274             , m_midiChannels( 0 )
275             , m_index( 0xff )
276             {}
277         sampling_frequency_t  m_samplingFrequency;
278         bool                       m_isSyncStream;
279         number_of_channels_t  m_audioChannels;
280         number_of_channels_t  m_midiChannels;
281         byte_t                     m_index;
282     };
283     typedef std::vector<FormatInfo> FormatInfoVector;
284
285
286     Unit*                       m_unit;
287     Subunit*                    m_subunit;
288     ESubunitType                m_subunitType;
289     subunit_t                   m_subunitId;
290     function_block_type_t       m_functionBlockType;
291     function_block_id_t         m_functionBlockId;
292     EPlugAddressType            m_addressType;
293     EPlugDirection              m_direction;
294     plug_id_t                   m_id;
295     EPlugType                   m_infoPlugType;
296     nr_of_channels_t            m_nrOfChannels;
297     std::string                 m_name;
298     ClusterInfoVector           m_clusterInfos;
299     sampling_frequency_t        m_samplingFrequency;
300     FormatInfoVector            m_formatInfos;
301     PlugVector                  m_inputConnections;
302     PlugVector                  m_outputConnections;
303     int                         m_globalId;
304
305     DECLARE_DEBUG_MODULE;
306 };
307
308 const char* avPlugAddressTypeToString( Plug::EPlugAddressType addressType );
309 const char* avPlugTypeToString( Plug::EPlugType type );
310 const char* avPlugDirectionToString( Plug::EPlugDirection direction );
311
312 class PlugManager
313 {
314 public:
315     PlugManager( );
316     PlugManager( const PlugManager& rhs );
317     ~PlugManager();
318
319     bool addPlug( Plug& plug );
320     bool remPlug( Plug& plug );
321
322     void showPlugs() const;
323
324     int getPlugCount()
325         { return m_plugs.size(); };
326
327     int requestNewGlobalId()
328         { return m_globalIdCounter++; };
329
330     Plug* getPlug( ESubunitType subunitType,
331                    subunit_id_t subunitId,
332                    function_block_type_t functionBlockType,
333                    function_block_id_t functionBlockId,
334                    Plug::EPlugAddressType plugAddressType,
335                    Plug::EPlugDirection plugDirection,
336                    plug_id_t plugId ) const;
337     Plug* getPlug( int iGlobalId ) const;
338     PlugVector getPlugsByType( ESubunitType subunitType,
339                                subunit_id_t subunitId,
340                                function_block_type_t functionBlockType,
341                                function_block_id_t functionBlockId,
342                                Plug::EPlugAddressType plugAddressType,
343                                Plug::EPlugDirection plugDirection,
344                                Plug::EPlugType type) const;
345
346     bool serialize( std::string basePath, Util::IOSerialize& ser ) const;
347     static  PlugManager* deserialize( std::string basePath,
348                                       Util::IODeserialize& deser,
349                                       Unit& avDevice );
350     void setVerboseLevel( int i );
351     PlugVector& getPlugs()
352         { return m_plugs; }
353     bool tidyPlugConnections(PlugConnectionVector&);
354
355     bool deserializeUpdate( std::string basePath, 
356                             Util::IODeserialize& deser );
357
358 private:
359     int m_globalIdCounter;
360
361     PlugVector   m_plugs;
362
363     DECLARE_DEBUG_MODULE;
364 };
365
366 class PlugConnection {
367 public:
368     PlugConnection( Plug& srcPlug, Plug& destPlug );
369
370     Plug& getSrcPlug() const
371         { return *m_srcPlug; }
372     Plug& getDestPlug() const
373         { return *m_destPlug; }
374
375     bool serialize( std::string basePath, Util::IOSerialize& ser ) const;
376     static PlugConnection* deserialize( std::string basePath,
377                                         Util::IODeserialize& deser,
378                                         Unit& avDevice );
379 private:
380     PlugConnection();
381
382 private:
383     Plug* m_srcPlug;
384     Plug* m_destPlug;
385 };
386
387 typedef std::vector<PlugConnection> PlugConnectionOwnerVector;
388
389
390 bool serializePlugVector( std::string basePath,
391                           Util::IOSerialize& ser,
392                           const PlugVector& vec);
393
394 bool deserializePlugVector( std::string basePath,
395                             Util::IODeserialize& deser,
396                             const PlugManager& plugManager,
397                             PlugVector& vec );
398
399 }
400
401 #endif // AVC_PLUG
Note: See TracBrowser for help on using the browser.