root/trunk/libffado/src/ffado.cpp

Revision 750, 15.2 kB (checked in by ppalmers, 13 years ago)

Code refactoring. Tries to simplify things and tries to put all code where it belongs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2007 by Daniel Wagner
3  * Copyright (C) 2005-2007 by Pieter Palmers
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 3 of the License, or
13  * (at your option) any later version.
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 /*
26  * Implementation of the FFADO external C API
27  */
28
29 #include "config.h"
30
31 #include "../libffado/ffado.h"
32 #include "libstreaming/generic/StreamProcessor.h"
33 #include "libstreaming/generic/Port.h"
34
35 #include "debugmodule/debugmodule.h"
36 #include "fbtypes.h"
37 #include "devicemanager.h"
38 #include "ffadodevice.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <assert.h>
44 #include <string>
45
46 DECLARE_GLOBAL_DEBUG_MODULE;
47 IMPL_GLOBAL_DEBUG_MODULE( FFADO, DEBUG_LEVEL_VERBOSE );
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 // this is very much nescessary, as otherwise the
54 // message buffer thread doesn't get killed when the
55 // library is dlclose()'d
56
57 static void exitfunc(void) __attribute__((destructor));
58
59 static void exitfunc(void)
60 {
61     delete DebugModuleManager::instance();
62
63 }
64 #ifdef __cplusplus
65 }
66 #endif
67
68 const char*
69 ffado_get_version() {
70     return PACKAGE_STRING;
71 }
72
73 int
74 ffado_get_api_version() {
75     return FFADO_API_VERSION;
76 }
77
78 #warning this should be cleaned up
79 #include "libavc/general/avc_generic.h"
80 void ffado_sleep_after_avc_command( int time )
81 {
82     AVC::AVCCommand::setSleepAfterAVCCommand( time );
83 }
84
85 struct _ffado_device
86 {
87     DeviceManager * m_deviceManager;
88
89     ffado_options_t options;
90     ffado_device_info_t device_info;
91 };
92
93 ffado_device_t *ffado_streaming_init (ffado_device_info_t device_info, ffado_options_t options) {
94     unsigned int i=0;
95     setDebugLevel(options.verbose);
96
97     struct _ffado_device *dev = new struct _ffado_device;
98
99     debugWarning("%s built %s %s\n", ffado_get_version(), __DATE__, __TIME__);
100
101     if(!dev) {
102         debugFatal( "Could not allocate streaming device\n" );
103         return 0;
104     }
105
106     memcpy((void *)&dev->options, (void *)&options, sizeof(dev->options));
107
108     dev->m_deviceManager = new DeviceManager();
109     if ( !dev->m_deviceManager ) {
110         debugFatal( "Could not allocate device manager\n" );
111         delete dev;
112         return 0;
113     }
114
115     dev->m_deviceManager->setVerboseLevel(dev->options.verbose);
116     dev->m_deviceManager->setThreadParameters(dev->options.realtime, dev->options.packetizer_priority);
117
118     for (i = 0; i < device_info.nb_device_spec_strings; i++) {
119         char *s = device_info.device_spec_strings[i];
120         if ( !dev->m_deviceManager->addSpecString(s) ) {
121             debugFatal( "Could not add spec string %s to device manager\n", s );
122             delete dev->m_deviceManager;
123             delete dev;
124             return 0;
125         }
126     }
127     // create a processor manager to manage the actual stream
128     // processors
129     if ( !dev->m_deviceManager->setStreamingParams(dev->options.period_size,
130                                                    dev->options.sample_rate,
131                                                    dev->options.nb_buffers))
132     {
133         debugFatal( "Could not set streaming parameters of device manager\n" );
134         delete dev->m_deviceManager;
135         delete dev;
136         return 0;
137     }
138
139     // set slave mode option
140     bool slaveMode=(dev->options.slave_mode != 0);
141     debugOutput(DEBUG_LEVEL_VERBOSE, "setting slave mode to %d\n", slaveMode);
142     if(!dev->m_deviceManager->setOption("slaveMode", slaveMode)) {
143             debugWarning("Failed to set slave mode option\n");
144     }
145     // set snoop mode option
146     bool snoopMode=(dev->options.snoop_mode != 0);
147     debugOutput(DEBUG_LEVEL_VERBOSE, "setting snoop mode to %d\n", snoopMode);
148     if(!dev->m_deviceManager->setOption("snoopMode", snoopMode)) {
149             debugWarning("Failed to set snoop mode option\n");
150     }
151
152     if ( !dev->m_deviceManager->initialize() ) {
153         debugFatal( "Could not initialize device manager\n" );
154         delete dev->m_deviceManager;
155         delete dev;
156         return 0;
157     }
158     // discover the devices on the bus
159     if(!dev->m_deviceManager->discover()) {
160         debugFatal("Could not discover devices\n");
161         delete dev->m_deviceManager;
162         delete dev;
163         return 0;
164     }
165     // are there devices on the bus?
166     if(dev->m_deviceManager->getAvDeviceCount() == 0) {
167         debugFatal("There are no devices on the bus\n");
168         delete dev->m_deviceManager;
169         delete dev;
170         return 0;
171     }
172     // prepare here or there are no ports for jack
173     if(!dev->m_deviceManager->initStreaming()) {
174         debugFatal("Could not init the streaming system\n");
175         return 0;
176     }
177     // we are ready!
178     return dev;
179 }
180
181 int ffado_streaming_prepare(ffado_device_t *dev) {
182     debugOutput(DEBUG_LEVEL_VERBOSE, "Preparing...\n");
183     // prepare here or there are no ports for jack
184     if(!dev->m_deviceManager->prepareStreaming()) {
185         debugFatal("Could not prepare the streaming system\n");
186         return 0;
187     }
188     return 0;
189 }
190
191 void ffado_streaming_finish(ffado_device_t *dev) {
192     assert(dev);
193     if(!dev->m_deviceManager->finishStreaming()) {
194         debugError("Could not finish the streaming\n");
195     }
196     delete dev->m_deviceManager;
197     delete dev;
198     return;
199 }
200
201 int ffado_streaming_start(ffado_device_t *dev) {
202     debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Start -------------\n");
203     if(!dev->m_deviceManager->startStreaming()) {
204         debugFatal("Could not start the streaming system\n");
205         return -1;
206     }
207     return 0;
208 }
209
210 int ffado_streaming_stop(ffado_device_t *dev) {
211     debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Stop -------------\n");
212     if(!dev->m_deviceManager->stopStreaming()) {
213         debugFatal("Could not stop the streaming system\n");
214         return -1;
215     }
216     return 0;
217 }
218
219 int ffado_streaming_reset(ffado_device_t *dev) {
220     debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Reset -------------\n");
221     if(!dev->m_deviceManager->resetStreaming()) {
222         debugFatal("Could not reset the streaming system\n");
223         return -1;
224     }
225     return 0;
226 }
227
228 int ffado_streaming_wait(ffado_device_t *dev) {
229     static int periods=0;
230     static int periods_print=0;
231     static int xruns=0;
232
233     periods++;
234     if(periods>periods_print) {
235         debugOutputShort(DEBUG_LEVEL_VERBOSE, "\nffado_streaming_wait\n");
236         debugOutputShort(DEBUG_LEVEL_VERBOSE, "============================================\n");
237         debugOutputShort(DEBUG_LEVEL_VERBOSE, "Xruns: %d\n", xruns);
238         debugOutputShort(DEBUG_LEVEL_VERBOSE, "============================================\n");
239         dev->m_deviceManager->showStreamingInfo();
240         debugOutputShort(DEBUG_LEVEL_VERBOSE, "\n");
241         periods_print+=100;
242     }
243
244     if(dev->m_deviceManager->waitForPeriod()) {
245         return dev->options.period_size;
246     } else {
247         xruns++;
248         return -1;
249     }
250 }
251
252 int ffado_streaming_transfer_capture_buffers(ffado_device_t *dev) {
253     return dev->m_deviceManager->getStreamProcessorManager().transfer(Streaming::StreamProcessor::ePT_Receive);
254 }
255
256 int ffado_streaming_transfer_playback_buffers(ffado_device_t *dev) {
257     return dev->m_deviceManager->getStreamProcessorManager().transfer(Streaming::StreamProcessor::ePT_Transmit);
258 }
259
260 int ffado_streaming_transfer_buffers(ffado_device_t *dev) {
261     return dev->m_deviceManager->getStreamProcessorManager().transfer();
262 }
263
264
265 int ffado_streaming_write(ffado_device_t *dev, int i, ffado_sample_t *buffer, int nsamples) {
266     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
267     // use an assert here performancewise,
268     // it should already have failed before, if not correct
269     assert(p);
270
271     return p->writeEvents((void *)buffer, nsamples);
272 }
273
274 int ffado_streaming_read(ffado_device_t *dev, int i, ffado_sample_t *buffer, int nsamples) {
275     Streaming::Port *p=dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
276     // use an assert here performancewise,
277     // it should already have failed before, if not correct
278     assert(p);
279
280     return p->readEvents((void *)buffer, nsamples);
281 }
282
283 int ffado_streaming_get_nb_capture_streams(ffado_device_t *dev) {
284     return dev->m_deviceManager->getStreamProcessorManager().getPortCount(Streaming::Port::E_Capture);
285 }
286
287 int ffado_streaming_get_nb_playback_streams(ffado_device_t *dev) {
288     return dev->m_deviceManager->getStreamProcessorManager().getPortCount(Streaming::Port::E_Playback);
289 }
290
291 int ffado_streaming_get_capture_stream_name(ffado_device_t *dev, int i, char* buffer, size_t buffersize) {
292     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
293     if(!p) {
294         debugWarning("Could not get capture port at index %d\n",i);
295         return -1;
296     }
297
298     std::string name=p->getName();
299     if (!strncpy(buffer, name.c_str(), buffersize)) {
300         debugWarning("Could not copy name\n");
301         return -1;
302     } else return 0;
303 }
304
305 int ffado_streaming_get_playback_stream_name(ffado_device_t *dev, int i, char* buffer, size_t buffersize) {
306     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
307     if(!p) {
308         debugWarning("Could not get playback 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 ffado_streaming_stream_type ffado_streaming_get_capture_stream_type(ffado_device_t *dev, int i) {
320     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
321     if(!p) {
322         debugWarning("Could not get capture port at index %d\n",i);
323         return ffado_stream_type_invalid;
324     }
325     switch(p->getPortType()) {
326     case Streaming::Port::E_Audio:
327         return ffado_stream_type_audio;
328     case Streaming::Port::E_Midi:
329         return ffado_stream_type_midi;
330     case Streaming::Port::E_Control:
331         return ffado_stream_type_control;
332     default:
333         return ffado_stream_type_unknown;
334     }
335 }
336
337 ffado_streaming_stream_type ffado_streaming_get_playback_stream_type(ffado_device_t *dev, int i) {
338     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
339     if(!p) {
340         debugWarning("Could not get playback port at index %d\n",i);
341         return ffado_stream_type_invalid;
342     }
343     switch(p->getPortType()) {
344     case Streaming::Port::E_Audio:
345         return ffado_stream_type_audio;
346     case Streaming::Port::E_Midi:
347         return ffado_stream_type_midi;
348     case Streaming::Port::E_Control:
349         return ffado_stream_type_control;
350     default:
351         return ffado_stream_type_unknown;
352     }
353 }
354
355 int ffado_streaming_set_stream_buffer_type(ffado_device_t *dev, int i,
356     ffado_streaming_buffer_type t, enum Streaming::Port::E_Direction direction) {
357
358     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, direction);
359     if(!p) {
360         debugWarning("Could not get %s port at index %d\n",
361             (direction==Streaming::Port::E_Playback?"Playback":"Capture"),i);
362         return -1;
363     }
364
365     switch(t) {
366     case ffado_buffer_type_int24:
367         if (!p->setDataType(Streaming::Port::E_Int24)) {
368             debugWarning("%s: Could not set data type to Int24\n",p->getName().c_str());
369             return -1;
370         }
371         if (!p->setBufferType(Streaming::Port::E_PointerBuffer)) {
372             debugWarning("%s: Could not set buffer type to Pointerbuffer\n",p->getName().c_str());
373             return -1;
374         }
375         break;
376     case ffado_buffer_type_float:
377         if (!p->setDataType(Streaming::Port::E_Float)) {
378             debugWarning("%s: Could not set data type to Float\n",p->getName().c_str());
379             return -1;
380         }
381         if (!p->setBufferType(Streaming::Port::E_PointerBuffer)) {
382             debugWarning("%s: Could not set buffer type to Pointerbuffer\n",p->getName().c_str());
383             return -1;
384         }
385         break;
386     case ffado_buffer_type_midi:
387         if (!p->setDataType(Streaming::Port::E_MidiEvent)) {
388             debugWarning("%s: Could not set data type to MidiEvent\n",p->getName().c_str());
389             return -1;
390         }
391         if (!p->setBufferType(Streaming::Port::E_RingBuffer)) {
392             debugWarning("%s: Could not set buffer type to Ringbuffer\n",p->getName().c_str());
393             return -1;
394         }
395         break;
396     default:
397         debugWarning("%s: Unsupported buffer type\n",p->getName().c_str());
398         return -1;
399     }
400     return 0;
401
402 }
403
404 int ffado_streaming_set_playback_buffer_type(ffado_device_t *dev, int i, ffado_streaming_buffer_type t) {
405     return ffado_streaming_set_stream_buffer_type(dev, i, t, Streaming::Port::E_Playback);
406 }
407
408 int ffado_streaming_set_capture_buffer_type(ffado_device_t *dev, int i, ffado_streaming_buffer_type t) {
409     return ffado_streaming_set_stream_buffer_type(dev, i, t, Streaming::Port::E_Capture);
410 }
411
412 int ffado_streaming_stream_onoff(ffado_device_t *dev, int i,
413     int on, enum Streaming::Port::E_Direction direction) {
414     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, direction);
415     if(!p) {
416         debugWarning("Could not get %s port at index %d\n",
417             (direction==Streaming::Port::E_Playback?"Playback":"Capture"),i);
418         return -1;
419     }
420     if(on) {
421         p->enable();
422     } else {
423         p->disable();
424     }
425     return 0;
426 }
427
428 int ffado_streaming_playback_stream_onoff(ffado_device_t *dev, int number, int on) {
429     return ffado_streaming_stream_onoff(dev, number, on, Streaming::Port::E_Playback);
430 }
431
432 int ffado_streaming_capture_stream_onoff(ffado_device_t *dev, int number, int on) {
433     return ffado_streaming_stream_onoff(dev, number, on, Streaming::Port::E_Capture);
434 }
435
436 // TODO: the way port buffers are set in the C api doesn't satisfy me
437 int ffado_streaming_set_capture_stream_buffer(ffado_device_t *dev, int i, char *buff) {
438         Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
439
440         // use an assert here performancewise,
441         // it should already have failed before, if not correct
442         assert(p);
443
444         p->useExternalBuffer(true);
445         p->setExternalBufferAddress((void *)buff);
446
447         return 0;
448
449 }
450
451 int ffado_streaming_set_playback_stream_buffer(ffado_device_t *dev, int i, char *buff) {
452         Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
453         // use an assert here performancewise,
454         // it should already have failed before, if not correct
455         assert(p);
456
457         p->useExternalBuffer(true);
458         p->setExternalBufferAddress((void *)buff);
459
460         return 0;
461 }
Note: See TracBrowser for help on using the browser.