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

Revision 811, 5.6 kB (checked in by ppalmers, 14 years ago)

fixes for introduced bugs

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