1 |
/* $Id$ */ |
---|
2 |
|
---|
3 |
/* |
---|
4 |
* FreeBob Streaming API |
---|
5 |
* FreeBob = Firewire (pro-)audio for linux |
---|
6 |
* |
---|
7 |
* http://freebob.sf.net |
---|
8 |
* |
---|
9 |
* Copyright (C) 2005,2006 Pieter Palmers <pieterpalmers@users.sourceforge.net> |
---|
10 |
* |
---|
11 |
* This program is free software {} you can redistribute it and/or modify |
---|
12 |
* it under the terms of the GNU General Public License as published by |
---|
13 |
* the Free Software Foundation {} either version 2 of the License, or |
---|
14 |
* (at your option) any later version. |
---|
15 |
* |
---|
16 |
* This program is distributed in the hope that it will be useful, |
---|
17 |
* but WITHOUT ANY WARRANTY {} without even the implied warranty of |
---|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 |
* GNU General Public License for more details. |
---|
20 |
* |
---|
21 |
* You should have received a copy of the GNU General Public License |
---|
22 |
* along with this program {} if not, write to the Free Software |
---|
23 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
24 |
* |
---|
25 |
* |
---|
26 |
* |
---|
27 |
*/ |
---|
28 |
|
---|
29 |
#include "Port.h" |
---|
30 |
#include <stdlib.h> |
---|
31 |
#include <assert.h> |
---|
32 |
|
---|
33 |
|
---|
34 |
namespace FreebobStreaming { |
---|
35 |
|
---|
36 |
IMPL_DEBUG_MODULE( Port, Port, DEBUG_LEVEL_NORMAL ); |
---|
37 |
|
---|
38 |
Port::Port(std::string name, enum E_BufferType type, unsigned int buffsize, |
---|
39 |
enum E_DataType datatype, enum E_PortType porttype, enum E_Direction direction) |
---|
40 |
: m_Name(name), |
---|
41 |
m_BufferType(type), |
---|
42 |
m_enabled(true), |
---|
43 |
m_buffersize(buffsize), |
---|
44 |
m_datatype(datatype), |
---|
45 |
m_porttype(porttype), |
---|
46 |
m_direction(direction), |
---|
47 |
m_buffer(0), |
---|
48 |
m_buffer_attached(false) |
---|
49 |
{ |
---|
50 |
|
---|
51 |
allocateInternalBuffer(); |
---|
52 |
} |
---|
53 |
|
---|
54 |
Port::Port(std::string name, enum E_BufferType type, unsigned int buffsize, |
---|
55 |
enum E_DataType datatype, void* externalbuffer, enum E_PortType porttype, |
---|
56 |
enum E_Direction direction) |
---|
57 |
: m_Name(name), |
---|
58 |
m_BufferType(type), |
---|
59 |
m_enabled(true), |
---|
60 |
m_buffersize(buffsize), |
---|
61 |
m_datatype(datatype), |
---|
62 |
m_porttype(porttype), |
---|
63 |
m_direction(direction), |
---|
64 |
m_buffer(externalbuffer), |
---|
65 |
m_buffer_attached(true) |
---|
66 |
{ |
---|
67 |
|
---|
68 |
} |
---|
69 |
|
---|
70 |
// int Port::attachBuffer(void *buff) { |
---|
71 |
// |
---|
72 |
// // if(m_buffer_attached) { |
---|
73 |
// // debugFatal("already has a buffer attached\n"); |
---|
74 |
// // return -1; |
---|
75 |
// // } |
---|
76 |
// // |
---|
77 |
// // freeInternalBuffer(); |
---|
78 |
// |
---|
79 |
// m_buffer=buff; |
---|
80 |
// |
---|
81 |
// m_buffer_attached=true; |
---|
82 |
// |
---|
83 |
// } |
---|
84 |
|
---|
85 |
// // detach the user buffer, allocates an internal buffer |
---|
86 |
// int Port::detachBuffer() { |
---|
87 |
// if(!m_buffer_attached) { |
---|
88 |
// debugWarning("does not have a buffer attached\n"); |
---|
89 |
// if(m_buffer) { |
---|
90 |
// return 0; |
---|
91 |
// } // if no buffer present, there should be one allocated |
---|
92 |
// } |
---|
93 |
// |
---|
94 |
// m_buffer_attached=false; |
---|
95 |
// |
---|
96 |
// return allocateInternalBuffer(); |
---|
97 |
// |
---|
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 |
|
---|
103 |
m_buffersize=newsize; |
---|
104 |
if(m_buffer_attached) { |
---|
105 |
return true; |
---|
106 |
} |
---|
107 |
|
---|
108 |
freeInternalBuffer(); |
---|
109 |
|
---|
110 |
return allocateInternalBuffer(); |
---|
111 |
|
---|
112 |
} |
---|
113 |
|
---|
114 |
|
---|
115 |
bool Port::allocateInternalBuffer() { |
---|
116 |
int event_size=getEventSize(); |
---|
117 |
|
---|
118 |
if(m_buffer_attached) { |
---|
119 |
debugWarning("has an external buffer attached, not allocating\n"); |
---|
120 |
return false; |
---|
121 |
} |
---|
122 |
|
---|
123 |
if(m_buffer) { |
---|
124 |
debugWarning("already has an internal buffer attached, re-allocating\n"); |
---|
125 |
freeInternalBuffer(); |
---|
126 |
} |
---|
127 |
|
---|
128 |
m_buffer=calloc(m_buffersize,event_size); |
---|
129 |
if (!m_buffer) { |
---|
130 |
debugFatal("could not allocate internal buffer\n"); |
---|
131 |
m_buffersize=0; |
---|
132 |
return false; |
---|
133 |
} |
---|
134 |
|
---|
135 |
return true; |
---|
136 |
} |
---|
137 |
|
---|
138 |
void Port::freeInternalBuffer() { |
---|
139 |
if(m_buffer_attached) { |
---|
140 |
debugWarning("has an external buffer attached, not free-ing\n"); |
---|
141 |
return; |
---|
142 |
} |
---|
143 |
if(m_buffer) { |
---|
144 |
free(m_buffer); |
---|
145 |
m_buffer=0; |
---|
146 |
} |
---|
147 |
} |
---|
148 |
|
---|
149 |
unsigned int Port::getEventSize() { |
---|
150 |
switch (m_datatype) { |
---|
151 |
case E_Float: |
---|
152 |
return sizeof(float); |
---|
153 |
case E_Int24: // 24 bit 2's complement, packed in a 32bit integer (LSB's) |
---|
154 |
return sizeof(uint32_t); |
---|
155 |
case E_Byte: |
---|
156 |
return sizeof(char); |
---|
157 |
default: |
---|
158 |
return 0; |
---|
159 |
} |
---|
160 |
} |
---|
161 |
|
---|
162 |
} |
---|