root/branches/api-cleanup/src/libstreaming/generic/Port.cpp

Revision 809, 5.4 kB (checked in by ppalmers, 14 years ago)

First round of cleanup:
- make Ports auto-register to a PortManager?
- remove the different 'signal' types, everything is now period-signaled.
- removed obsolete streaming test programs

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 #include "Port.h"
25 #include "PortManager.h"
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 namespace Streaming {
31
32 IMPL_DEBUG_MODULE( Port, Port, DEBUG_LEVEL_NORMAL );
33
34 Port::Port(PortManager& m, std::string name,
35            enum E_PortType porttype, enum E_Direction direction, enum E_DataType d)
36     : m_Name( name )
37     , m_disabled( true )
38     , m_buffersize( 0 )
39     , m_eventsize( 0 )
40     , m_DataType( d )
41     , m_PortType( porttype )
42     , m_Direction( direction )
43     , m_buffer( NULL )
44     , m_manager( m )
45     , m_State( E_Created )
46 {
47     m_manager.registerPort(this);
48 }
49
50 Port::~Port() {
51     m_manager.unregisterPort(this);
52 }
53
54 /**
55  * The idea is that you set all port parameters, and then initialize the port.
56  * This allocates all resources and makes the port usable. However, you can't
57  * change the port properties anymore after this.
58  *
59  * @return true if successfull. false if not (all resources are freed).
60  */
61 bool Port::init() {
62     debugOutput( DEBUG_LEVEL_VERBOSE, "Initialize port %s\n", m_Name.c_str());
63     if (m_State != E_Created) {
64         debugFatal("Port (%s) not in E_Created state: %d\n", m_Name.c_str(), m_State);
65         return false;
66     }
67
68     if (m_buffersize==0) {
69         debugFatal("Cannot initialize a port with buffersize=0\n");
70         return false;
71     }
72
73     m_eventsize = getEventSize(); // this won't change, so cache it
74
75     m_State = E_Initialized;
76     return true;
77 }
78
79 bool Port::reset() {
80     return true;
81 }
82
83 bool Port::setName(std::string name) {
84     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting name to %s for port %s\n",name.c_str(),m_Name.c_str());
85
86     if (m_State != E_Created) {
87         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State);
88         return false;
89     }
90     m_Name=name;
91     return true;
92 }
93
94 bool Port::setBufferSize(unsigned int newsize) {
95     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting buffersize to %d for port %s\n",newsize,m_Name.c_str());
96     if (m_State != E_Created) {
97         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State);
98         return false;
99     }
100     m_buffersize=newsize;
101     return true;
102 }
103
104 unsigned int Port::getEventSize() {
105     switch (m_DataType) {
106         case E_Float:
107             return sizeof(float);
108         case E_Int24: // 24 bit 2's complement, packed in a 32bit integer (LSB's)
109             return sizeof(int32_t);
110         case E_MidiEvent:
111             return sizeof(uint32_t);
112         case E_ControlEvent:
113             return sizeof(uint32_t);
114         default:
115             return 0;
116     }
117 }
118
119 bool Port::setDataType(enum E_DataType d) {
120     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting datatype to %d for port %s\n",(int) d,m_Name.c_str());
121     if (m_State != E_Created) {
122         debugFatal("Port (%s) not in E_Created state: %d\n",m_Name.c_str(),m_State);
123         return false;
124     }
125
126     // do some sanity checks
127     bool type_is_ok=false;
128     switch (m_PortType) {
129         case E_Audio:
130             if(d == E_Int24) type_is_ok=true;
131             if(d == E_Float) type_is_ok=true;
132             break;
133         case E_Midi:
134             if(d == E_MidiEvent) type_is_ok=true;
135             break;
136         case E_Control:
137             if(d == E_ControlEvent) type_is_ok=true;
138             break;
139         default:
140             break;
141     }
142
143     if(!type_is_ok) {
144         debugFatal("Datatype not supported by this type of port!\n");
145         return false;
146     }
147
148     m_DataType=d;
149     return true;
150 }
151
152 // buffer handling api's for pointer buffers
153 /**
154  * Get the buffer address
155  *
156  * @param buff
157  */
158 void *Port::getBufferAddress() {
159     return m_buffer;
160 };
161
162 /**
163  * Set the external buffer address.
164  *
165  * @param buff
166  */
167 void Port::setBufferAddress(void *buff) {
168     m_buffer=buff;
169 }
170
171 /// Enable the port. (this can be called anytime)
172 void
173 Port::enable()  {
174     debugOutput(DEBUG_LEVEL_VERBOSE, "Enabling port %s...\n",m_Name.c_str());
175     m_disabled=false;
176 }
177
178 /// Disable the port. (this can be called anytime)
179 void
180 Port::disable() {
181     debugOutput(DEBUG_LEVEL_VERBOSE, "Disabling port %s...\n",m_Name.c_str());
182     m_disabled=false;
183 }
184
185 void Port::show() {
186     debugOutput(DEBUG_LEVEL_VERBOSE,"Name          : %s\n", m_Name.c_str());
187     debugOutput(DEBUG_LEVEL_VERBOSE,"Enabled?      : %d\n", m_disabled);
188     debugOutput(DEBUG_LEVEL_VERBOSE,"State?        : %d\n", m_State);
189     debugOutput(DEBUG_LEVEL_VERBOSE,"Buffer Size   : %d\n", m_buffersize);
190     debugOutput(DEBUG_LEVEL_VERBOSE,"Event Size    : %d\n", m_eventsize);
191     debugOutput(DEBUG_LEVEL_VERBOSE,"Data Type     : %d\n", m_DataType);
192     debugOutput(DEBUG_LEVEL_VERBOSE,"Port Type     : %d\n", m_PortType);
193     debugOutput(DEBUG_LEVEL_VERBOSE,"Direction     : %d\n", m_Direction);
194 }
195
196 void Port::setVerboseLevel(int l) {
197     setDebugLevel(l);
198 }
199
200 }
Note: See TracBrowser for help on using the browser.