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 |
/* freebob_streaming.c |
---|
30 |
* |
---|
31 |
* Implementation of the FreeBob Streaming API |
---|
32 |
* |
---|
33 |
*/ |
---|
34 |
|
---|
35 |
#include "libfreebob/freebob_streaming.h" |
---|
36 |
#include "../devicemanager.h" |
---|
37 |
#include "../iavdevice.h" |
---|
38 |
|
---|
39 |
#include "IsoHandler.h" |
---|
40 |
#include "IsoStream.h" |
---|
41 |
#include "StreamProcessor.h" |
---|
42 |
#include "StreamProcessorManager.h" |
---|
43 |
#include "AmdtpStreamProcessor.h" |
---|
44 |
#include "IsoHandlerManager.h" |
---|
45 |
#include "StreamRunner.h" |
---|
46 |
#include "FreebobPosixThread.h" |
---|
47 |
#include "AmdtpPort.h" |
---|
48 |
|
---|
49 |
#include <assert.h> |
---|
50 |
|
---|
51 |
#include <string.h> |
---|
52 |
#include <string> |
---|
53 |
|
---|
54 |
/** |
---|
55 |
* Device structure |
---|
56 |
*/ |
---|
57 |
|
---|
58 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
59 |
|
---|
60 |
using namespace FreebobStreaming; |
---|
61 |
|
---|
62 |
struct _freebob_device |
---|
63 |
{ |
---|
64 |
DeviceManager * m_deviceManager; |
---|
65 |
StreamProcessorManager *processorManager; |
---|
66 |
|
---|
67 |
freebob_options_t options; |
---|
68 |
freebob_device_info_t device_info; |
---|
69 |
}; |
---|
70 |
|
---|
71 |
freebob_device_t *freebob_streaming_init (freebob_device_info_t *device_info, freebob_options_t options) { |
---|
72 |
int i=0; |
---|
73 |
|
---|
74 |
struct _freebob_device *dev = new struct _freebob_device; |
---|
75 |
|
---|
76 |
debugFatal("%s built %s %s\n", freebob_get_version(), __DATE__, __TIME__); |
---|
77 |
|
---|
78 |
if(!dev) { |
---|
79 |
debugFatal( "Could not allocate streaming device\n" ); |
---|
80 |
return 0; |
---|
81 |
} |
---|
82 |
|
---|
83 |
memcpy((void *)&dev->options, (void *)&options, sizeof(dev->options)); |
---|
84 |
memcpy((void *)&dev->device_info, (void *)device_info, sizeof(dev->device_info)); |
---|
85 |
|
---|
86 |
dev->m_deviceManager = new DeviceManager(); |
---|
87 |
if ( !dev->m_deviceManager ) { |
---|
88 |
debugFatal( "Could not allocate device manager\n" ); |
---|
89 |
delete dev; |
---|
90 |
return 0; |
---|
91 |
} |
---|
92 |
if ( !dev->m_deviceManager->initialize( dev->options.port ) ) { |
---|
93 |
debugFatal( "Could not initialize device manager\n" ); |
---|
94 |
delete dev->m_deviceManager; |
---|
95 |
delete dev; |
---|
96 |
return 0; |
---|
97 |
} |
---|
98 |
|
---|
99 |
// create a processor manager to manage the actual stream |
---|
100 |
// processors |
---|
101 |
dev->processorManager = new StreamProcessorManager(dev->options.period_size,dev->options.nb_buffers); |
---|
102 |
if(!dev->processorManager) { |
---|
103 |
debugFatal("Could not create StreamProcessorManager\n"); |
---|
104 |
delete dev->m_deviceManager; |
---|
105 |
delete dev; |
---|
106 |
return 0; |
---|
107 |
} |
---|
108 |
|
---|
109 |
dev->processorManager->setThreadParameters(dev->options.realtime, dev->options.packetizer_priority); |
---|
110 |
|
---|
111 |
dev->processorManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
112 |
if(!dev->processorManager->init()) { |
---|
113 |
debugFatal("Could not init StreamProcessorManager\n"); |
---|
114 |
delete dev->processorManager; |
---|
115 |
delete dev->m_deviceManager; |
---|
116 |
delete dev; |
---|
117 |
return 0; |
---|
118 |
} |
---|
119 |
|
---|
120 |
// discover the devices on the bus |
---|
121 |
if(!dev->m_deviceManager->discover(DEBUG_LEVEL_VERBOSE)) { |
---|
122 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Could not discover devices\n"); |
---|
123 |
return 0; |
---|
124 |
} |
---|
125 |
|
---|
126 |
// iterate over the found devices |
---|
127 |
// add the stream processors of the devices to the managers |
---|
128 |
for(i=0;i<dev->m_deviceManager->getAvDeviceCount();i++) { |
---|
129 |
IAvDevice *device=dev->m_deviceManager->getAvDeviceByIndex(i); |
---|
130 |
assert(device); |
---|
131 |
|
---|
132 |
// prepare the device |
---|
133 |
device->prepare(); |
---|
134 |
|
---|
135 |
int j=0; |
---|
136 |
for(j=0; j<device->getStreamCount();j++) { |
---|
137 |
StreamProcessor *streamproc=device->getStreamProcessorByIndex(j); |
---|
138 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Registering stream processor %d of device %d with processormanager\n",j,i); |
---|
139 |
if (!dev->processorManager->registerProcessor(streamproc)) { |
---|
140 |
debugWarning("Could not register stream processor (%p) with the Processor manager\n",streamproc); |
---|
141 |
} |
---|
142 |
} |
---|
143 |
} |
---|
144 |
|
---|
145 |
// we are ready! |
---|
146 |
|
---|
147 |
debugOutputShort(DEBUG_LEVEL_VERBOSE, "\n\n"); |
---|
148 |
return dev; |
---|
149 |
|
---|
150 |
} |
---|
151 |
|
---|
152 |
int freebob_streaming_prepare(freebob_device_t *dev) { |
---|
153 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Preparing...\n"); |
---|
154 |
|
---|
155 |
dev->processorManager->prepare(); |
---|
156 |
|
---|
157 |
return true; |
---|
158 |
} |
---|
159 |
|
---|
160 |
void freebob_streaming_finish(freebob_device_t *dev) { |
---|
161 |
|
---|
162 |
assert(dev); |
---|
163 |
|
---|
164 |
delete dev->processorManager; |
---|
165 |
delete dev->m_deviceManager; |
---|
166 |
delete dev; |
---|
167 |
|
---|
168 |
return; |
---|
169 |
} |
---|
170 |
|
---|
171 |
int freebob_streaming_start(freebob_device_t *dev) { |
---|
172 |
int i=0; |
---|
173 |
debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Start -------------\n"); |
---|
174 |
|
---|
175 |
|
---|
176 |
// create the connections for all devices |
---|
177 |
// iterate over the found devices |
---|
178 |
// add the stream processors of the devices to the managers |
---|
179 |
for(i=0;i<dev->m_deviceManager->getAvDeviceCount();i++) { |
---|
180 |
IAvDevice *device=dev->m_deviceManager->getAvDeviceByIndex(i); |
---|
181 |
assert(device); |
---|
182 |
|
---|
183 |
int j=0; |
---|
184 |
for(j=0; j<device->getStreamCount();j++) { |
---|
185 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Starting stream %d of device %d\n",j,i); |
---|
186 |
// start the stream |
---|
187 |
device->startStreamByIndex(j); |
---|
188 |
} |
---|
189 |
} |
---|
190 |
|
---|
191 |
dev->processorManager->start(); |
---|
192 |
|
---|
193 |
return 0; |
---|
194 |
} |
---|
195 |
|
---|
196 |
int freebob_streaming_stop(freebob_device_t *dev) { |
---|
197 |
int i; |
---|
198 |
debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Stop -------------\n"); |
---|
199 |
|
---|
200 |
dev->processorManager->stop(); |
---|
201 |
|
---|
202 |
// create the connections for all devices |
---|
203 |
// iterate over the found devices |
---|
204 |
// add the stream processors of the devices to the managers |
---|
205 |
for(i=0;i<dev->m_deviceManager->getAvDeviceCount();i++) { |
---|
206 |
IAvDevice *device=dev->m_deviceManager->getAvDeviceByIndex(i); |
---|
207 |
assert(device); |
---|
208 |
|
---|
209 |
|
---|
210 |
int j=0; |
---|
211 |
for(j=0; j<device->getStreamCount();j++) { |
---|
212 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Stopping stream %d of device %d\n",j,i); |
---|
213 |
// stop the stream |
---|
214 |
device->stopStreamByIndex(j); |
---|
215 |
} |
---|
216 |
} |
---|
217 |
|
---|
218 |
return 0; |
---|
219 |
} |
---|
220 |
|
---|
221 |
int freebob_streaming_reset(freebob_device_t *dev) { |
---|
222 |
debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Reset -------------\n"); |
---|
223 |
|
---|
224 |
// dev->processorManager->reset(); |
---|
225 |
|
---|
226 |
return 0; |
---|
227 |
} |
---|
228 |
|
---|
229 |
int freebob_streaming_wait(freebob_device_t *dev) { |
---|
230 |
static int periods=0; |
---|
231 |
static int periods_print=0; |
---|
232 |
static int xruns=0; |
---|
233 |
|
---|
234 |
periods++; |
---|
235 |
if(periods>periods_print) { |
---|
236 |
debugOutput(DEBUG_LEVEL_VERBOSE, "\n"); |
---|
237 |
debugOutput(DEBUG_LEVEL_VERBOSE, "============================================\n"); |
---|
238 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Xruns: %d\n",xruns); |
---|
239 |
debugOutput(DEBUG_LEVEL_VERBOSE, "============================================\n"); |
---|
240 |
dev->processorManager->dumpInfo(); |
---|
241 |
// debugOutput(DEBUG_LEVEL_VERBOSE, "--------------------------------------------\n"); |
---|
242 |
/* quadlet_t *addr=(quadlet_t*)(dev->processorManager->getPortByIndex(0, Port::E_Capture)->getBufferAddress()); |
---|
243 |
if (addr) hexDumpQuadlets(addr,10);*/ |
---|
244 |
debugOutput(DEBUG_LEVEL_VERBOSE, "\n"); |
---|
245 |
periods_print+=100; |
---|
246 |
} |
---|
247 |
if(dev->processorManager->waitForPeriod()) { |
---|
248 |
return dev->options.period_size; |
---|
249 |
} else { |
---|
250 |
debugWarning("XRUN detected\n"); |
---|
251 |
// do xrun recovery |
---|
252 |
|
---|
253 |
dev->processorManager->handleXrun(); |
---|
254 |
xruns++; |
---|
255 |
return -1; |
---|
256 |
} |
---|
257 |
} |
---|
258 |
|
---|
259 |
int freebob_streaming_transfer_capture_buffers(freebob_device_t *dev) { |
---|
260 |
return dev->processorManager->transfer(StreamProcessor::E_Receive); |
---|
261 |
} |
---|
262 |
|
---|
263 |
int freebob_streaming_transfer_playback_buffers(freebob_device_t *dev) { |
---|
264 |
return dev->processorManager->transfer(StreamProcessor::E_Transmit); |
---|
265 |
} |
---|
266 |
|
---|
267 |
int freebob_streaming_transfer_buffers(freebob_device_t *dev) { |
---|
268 |
return dev->processorManager->transfer(); |
---|
269 |
} |
---|
270 |
|
---|
271 |
|
---|
272 |
int freebob_streaming_write(freebob_device_t *dev, int i, freebob_sample_t *buffer, int nsamples) { |
---|
273 |
// debugFatal("Not implemented\n"); |
---|
274 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Playback); |
---|
275 |
// use an assert here performancewise, |
---|
276 |
// it should already have failed before, if not correct |
---|
277 |
assert(p); |
---|
278 |
|
---|
279 |
return p->writeEvents((void *)buffer, nsamples); |
---|
280 |
} |
---|
281 |
|
---|
282 |
int freebob_streaming_read(freebob_device_t *dev, int i, freebob_sample_t *buffer, int nsamples) { |
---|
283 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Capture); |
---|
284 |
// use an assert here performancewise, |
---|
285 |
// it should already have failed before, if not correct |
---|
286 |
assert(p); |
---|
287 |
|
---|
288 |
return p->readEvents((void *)buffer, nsamples); |
---|
289 |
} |
---|
290 |
|
---|
291 |
pthread_t freebob_streaming_get_packetizer_thread(freebob_device_t *dev) { |
---|
292 |
// debugFatal("Not implemented\n"); |
---|
293 |
return 0; |
---|
294 |
} |
---|
295 |
|
---|
296 |
|
---|
297 |
int freebob_streaming_get_nb_capture_streams(freebob_device_t *dev) { |
---|
298 |
return dev->processorManager->getPortCount(Port::E_Capture); |
---|
299 |
} |
---|
300 |
|
---|
301 |
int freebob_streaming_get_nb_playback_streams(freebob_device_t *dev) { |
---|
302 |
return dev->processorManager->getPortCount(Port::E_Playback); |
---|
303 |
} |
---|
304 |
|
---|
305 |
int freebob_streaming_get_capture_stream_name(freebob_device_t *dev, int i, char* buffer, size_t buffersize) { |
---|
306 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Capture); |
---|
307 |
if(!p) { |
---|
308 |
debugWarning("Could not get capture port at index %d\n",i); |
---|
309 |
return -1; |
---|
310 |
} |
---|
311 |
|
---|
312 |
std::string name=p->getName(); |
---|
313 |
if (!strncpy(buffer, name.c_str(), buffersize)) { |
---|
314 |
debugWarning("Could not copy name\n"); |
---|
315 |
return -1; |
---|
316 |
} else return 0; |
---|
317 |
} |
---|
318 |
|
---|
319 |
int freebob_streaming_get_playback_stream_name(freebob_device_t *dev, int i, char* buffer, size_t buffersize) { |
---|
320 |
|
---|
321 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Playback); |
---|
322 |
if(!p) { |
---|
323 |
debugWarning("Could not get playback port at index %d\n",i); |
---|
324 |
return -1; |
---|
325 |
} |
---|
326 |
|
---|
327 |
std::string name=p->getName(); |
---|
328 |
if (!strncpy(buffer, name.c_str(), buffersize)) { |
---|
329 |
debugWarning("Could not copy name\n"); |
---|
330 |
return -1; |
---|
331 |
} else return 0; |
---|
332 |
} |
---|
333 |
|
---|
334 |
freebob_streaming_stream_type freebob_streaming_get_capture_stream_type(freebob_device_t *dev, int i) { |
---|
335 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Capture); |
---|
336 |
if(!p) { |
---|
337 |
debugWarning("Could not get capture port at index %d\n",i); |
---|
338 |
return freebob_stream_type_invalid; |
---|
339 |
} |
---|
340 |
switch(p->getPortType()) { |
---|
341 |
case Port::E_Audio: |
---|
342 |
return freebob_stream_type_audio; |
---|
343 |
case Port::E_Midi: |
---|
344 |
return freebob_stream_type_midi; |
---|
345 |
case Port::E_Control: |
---|
346 |
return freebob_stream_type_control; |
---|
347 |
default: |
---|
348 |
return freebob_stream_type_unknown; |
---|
349 |
} |
---|
350 |
} |
---|
351 |
|
---|
352 |
freebob_streaming_stream_type freebob_streaming_get_playback_stream_type(freebob_device_t *dev, int i) { |
---|
353 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Playback); |
---|
354 |
if(!p) { |
---|
355 |
debugWarning("Could not get playback port at index %d\n",i); |
---|
356 |
return freebob_stream_type_invalid; |
---|
357 |
} |
---|
358 |
switch(p->getPortType()) { |
---|
359 |
case Port::E_Audio: |
---|
360 |
return freebob_stream_type_audio; |
---|
361 |
case Port::E_Midi: |
---|
362 |
return freebob_stream_type_midi; |
---|
363 |
case Port::E_Control: |
---|
364 |
return freebob_stream_type_control; |
---|
365 |
default: |
---|
366 |
return freebob_stream_type_unknown; |
---|
367 |
} |
---|
368 |
} |
---|
369 |
|
---|
370 |
int freebob_streaming_set_stream_buffer_type(freebob_device_t *dev, int i, |
---|
371 |
freebob_streaming_buffer_type t, enum Port::E_Direction direction) { |
---|
372 |
|
---|
373 |
Port *p=dev->processorManager->getPortByIndex(i, direction); |
---|
374 |
if(!p) { |
---|
375 |
debugWarning("Could not get %s port at index %d\n", |
---|
376 |
(direction==Port::E_Playback?"Playback":"Capture"),i); |
---|
377 |
return -1; |
---|
378 |
} |
---|
379 |
|
---|
380 |
switch(t) { |
---|
381 |
case freebob_buffer_type_int24: |
---|
382 |
if (!p->setDataType(Port::E_Int24)) { |
---|
383 |
debugWarning("%s: Could not set data type to Int24\n",p->getName().c_str()); |
---|
384 |
return -1; |
---|
385 |
} |
---|
386 |
if (!p->setBufferType(Port::E_PointerBuffer)) { |
---|
387 |
debugWarning("%s: Could not set buffer type to Pointerbuffer\n",p->getName().c_str()); |
---|
388 |
return -1; |
---|
389 |
} |
---|
390 |
break; |
---|
391 |
case freebob_buffer_type_float: |
---|
392 |
if (!p->setDataType(Port::E_Float)) { |
---|
393 |
debugWarning("%s: Could not set data type to Float\n",p->getName().c_str()); |
---|
394 |
return -1; |
---|
395 |
} |
---|
396 |
if (!p->setBufferType(Port::E_PointerBuffer)) { |
---|
397 |
debugWarning("%s: Could not set buffer type to Pointerbuffer\n",p->getName().c_str()); |
---|
398 |
return -1; |
---|
399 |
} |
---|
400 |
break; |
---|
401 |
case freebob_buffer_type_midi: |
---|
402 |
if (!p->setDataType(Port::E_MidiEvent)) { |
---|
403 |
debugWarning("%s: Could not set data type to MidiEvent\n",p->getName().c_str()); |
---|
404 |
return -1; |
---|
405 |
} |
---|
406 |
if (!p->setBufferType(Port::E_RingBuffer)) { |
---|
407 |
debugWarning("%s: Could not set buffer type to Ringbuffer\n",p->getName().c_str()); |
---|
408 |
return -1; |
---|
409 |
} |
---|
410 |
break; |
---|
411 |
default: |
---|
412 |
debugWarning("%s: Unsupported buffer type\n",p->getName().c_str()); |
---|
413 |
return -1; |
---|
414 |
} |
---|
415 |
return 0; |
---|
416 |
|
---|
417 |
} |
---|
418 |
|
---|
419 |
int freebob_streaming_set_playback_buffer_type(freebob_device_t *dev, int i, freebob_streaming_buffer_type t) { |
---|
420 |
return freebob_streaming_set_stream_buffer_type(dev, i, t, Port::E_Playback); |
---|
421 |
} |
---|
422 |
|
---|
423 |
int freebob_streaming_set_capture_buffer_type(freebob_device_t *dev, int i, freebob_streaming_buffer_type t) { |
---|
424 |
return freebob_streaming_set_stream_buffer_type(dev, i, t, Port::E_Capture); |
---|
425 |
} |
---|
426 |
|
---|
427 |
int freebob_streaming_stream_onoff(freebob_device_t *dev, int i, |
---|
428 |
int on, enum Port::E_Direction direction) { |
---|
429 |
Port *p=dev->processorManager->getPortByIndex(i, direction); |
---|
430 |
if(!p) { |
---|
431 |
debugWarning("Could not get %s port at index %d\n", |
---|
432 |
(direction==Port::E_Playback?"Playback":"Capture"),i); |
---|
433 |
return -1; |
---|
434 |
} |
---|
435 |
if(on) { |
---|
436 |
p->enable(); |
---|
437 |
} else { |
---|
438 |
p->disable(); |
---|
439 |
} |
---|
440 |
return 0; |
---|
441 |
} |
---|
442 |
|
---|
443 |
int freebob_streaming_playback_stream_onoff(freebob_device_t *dev, int number, int on) { |
---|
444 |
return freebob_streaming_stream_onoff(dev, number, on, Port::E_Playback); |
---|
445 |
} |
---|
446 |
|
---|
447 |
int freebob_streaming_capture_stream_onoff(freebob_device_t *dev, int number, int on) { |
---|
448 |
return freebob_streaming_stream_onoff(dev, number, on, Port::E_Capture); |
---|
449 |
} |
---|
450 |
|
---|
451 |
// TODO: the way port buffers are set in the C api doesn't satisfy me |
---|
452 |
int freebob_streaming_set_capture_stream_buffer(freebob_device_t *dev, int i, char *buff) { |
---|
453 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Capture); |
---|
454 |
|
---|
455 |
// use an assert here performancewise, |
---|
456 |
// it should already have failed before, if not correct |
---|
457 |
assert(p); |
---|
458 |
|
---|
459 |
p->setExternalBufferAddress((void *)buff); |
---|
460 |
|
---|
461 |
return 0; |
---|
462 |
|
---|
463 |
} |
---|
464 |
|
---|
465 |
int freebob_streaming_set_playback_stream_buffer(freebob_device_t *dev, int i, char *buff) { |
---|
466 |
Port *p=dev->processorManager->getPortByIndex(i, Port::E_Playback); |
---|
467 |
// use an assert here performancewise, |
---|
468 |
// it should already have failed before, if not correct |
---|
469 |
assert(p); |
---|
470 |
|
---|
471 |
p->setExternalBufferAddress((void *)buff); |
---|
472 |
|
---|
473 |
return 0; |
---|
474 |
} |
---|
475 |
|
---|