root/trunk/libffado/src/ffado.cpp

Revision 1498, 14.1 kB (checked in by ppalmers, 15 years ago)

Merge all changes from 2.0 branch into trunk (since r1361). This _should_ contain all forward merges done in the mean time. At this moment in time both branches should be in sync.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  * Copyright (C) 2005-2008 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 2 of the License, or
13  * (at your option) version 3 of the License.
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 "version.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 struct _ffado_device
79 {
80     DeviceManager * m_deviceManager;
81
82     ffado_options_t options;
83     ffado_device_info_t device_info;
84 };
85
86 ffado_device_t *ffado_streaming_init (ffado_device_info_t device_info, ffado_options_t options) {
87     unsigned int i=0;
88     setDebugLevel(options.verbose);
89
90     struct _ffado_device *dev = new struct _ffado_device;
91
92     printMessage("%s built %s %s\n", ffado_get_version(), __DATE__, __TIME__);
93
94 #if DEBUG_USE_MESSAGE_BUFFER
95     // ok
96 #else
97     printMessage("FFADO built without realtime-safe message buffer support. This can cause xruns and is not recommended.\n");
98 #endif
99
100     if(!dev) {
101         debugFatal( "Could not allocate streaming device\n" );
102         return 0;
103     }
104
105     memcpy((void *)&dev->options, (void *)&options, sizeof(dev->options));
106
107     dev->m_deviceManager = new DeviceManager();
108     if ( !dev->m_deviceManager ) {
109         debugFatal( "Could not allocate device manager\n" );
110         delete dev;
111         return 0;
112     }
113
114     dev->m_deviceManager->setVerboseLevel(dev->options.verbose);
115
116     if(dev->options.realtime) {
117         debugOutput(DEBUG_LEVEL_VERBOSE,
118                     "Starting with realtime scheduling, base priority %d\n",
119                     dev->options.packetizer_priority);
120     } else {
121         debugWarning("Realtime scheduling is not enabled. This will cause significant reliability issues.\n");
122     }
123     dev->m_deviceManager->setThreadParameters(dev->options.realtime, dev->options.packetizer_priority);
124
125     for (i = 0; i < device_info.nb_device_spec_strings; i++) {
126         char *s = device_info.device_spec_strings[i];
127         if ( !dev->m_deviceManager->addSpecString(s) ) {
128             debugFatal( "Could not add spec string %s to device manager\n", s );
129             delete dev->m_deviceManager;
130             delete dev;
131             return 0;
132         }
133     }
134     // create a processor manager to manage the actual stream
135     // processors
136     if ( !dev->m_deviceManager->setStreamingParams(dev->options.period_size,
137                                                    dev->options.sample_rate,
138                                                    dev->options.nb_buffers))
139     {
140         debugFatal( "Could not set streaming parameters of device manager\n" );
141         delete dev->m_deviceManager;
142         delete dev;
143         return 0;
144     }
145
146     // set slave mode option
147     bool slaveMode=(dev->options.slave_mode != 0);
148     debugOutput(DEBUG_LEVEL_VERBOSE, "setting slave mode to %d\n", slaveMode);
149     if(!dev->m_deviceManager->setOption("slaveMode", slaveMode)) {
150             debugWarning("Failed to set slave mode option\n");
151     }
152     // set snoop mode option
153     bool snoopMode=(dev->options.snoop_mode != 0);
154     debugOutput(DEBUG_LEVEL_VERBOSE, "setting snoop mode to %d\n", snoopMode);
155     if(!dev->m_deviceManager->setOption("snoopMode", snoopMode)) {
156             debugWarning("Failed to set snoop mode option\n");
157     }
158
159     if ( !dev->m_deviceManager->initialize() ) {
160         debugFatal( "Could not initialize device manager\n" );
161         delete dev->m_deviceManager;
162         delete dev;
163         return 0;
164     }
165     // discover the devices on the bus
166     if(!dev->m_deviceManager->discover()) {
167         debugFatal("Could not discover devices\n");
168         delete dev->m_deviceManager;
169         delete dev;
170         return 0;
171     }
172     // are there devices on the bus?
173     if(dev->m_deviceManager->getAvDeviceCount() == 0) {
174         debugFatal("There are no devices on the bus\n");
175         delete dev->m_deviceManager;
176         delete dev;
177         return 0;
178     }
179     // prepare here or there are no ports for jack
180     if(!dev->m_deviceManager->initStreaming()) {
181         debugFatal("Could not init the streaming system\n");
182         return 0;
183     }
184     // we are ready!
185     return dev;
186 }
187
188 int ffado_streaming_prepare(ffado_device_t *dev) {
189     debugOutput(DEBUG_LEVEL_VERBOSE, "Preparing...\n");
190     // prepare here or there are no ports for jack
191     if(!dev->m_deviceManager->prepareStreaming()) {
192         debugFatal("Could not prepare the streaming system\n");
193         return -1;
194     }
195     return 0;
196 }
197
198 void ffado_streaming_finish(ffado_device_t *dev) {
199     assert(dev);
200     if(!dev->m_deviceManager->finishStreaming()) {
201         debugError("Could not finish the streaming\n");
202     }
203     delete dev->m_deviceManager;
204     delete dev;
205     return;
206 }
207
208 int ffado_streaming_start(ffado_device_t *dev) {
209     debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Start -------------\n");
210     if(!dev->m_deviceManager->startStreaming()) {
211         debugFatal("Could not start the streaming system\n");
212         return -1;
213     }
214     return 0;
215 }
216
217 int ffado_streaming_stop(ffado_device_t *dev) {
218     debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Stop -------------\n");
219     if(!dev->m_deviceManager->stopStreaming()) {
220         debugFatal("Could not stop the streaming system\n");
221         return -1;
222     }
223     return 0;
224 }
225
226 int ffado_streaming_reset(ffado_device_t *dev) {
227     debugOutput(DEBUG_LEVEL_VERBOSE,"------------- Reset -------------\n");
228     if(!dev->m_deviceManager->resetStreaming()) {
229         debugFatal("Could not reset the streaming system\n");
230         return -1;
231     }
232     return 0;
233 }
234
235 ffado_wait_response
236 ffado_streaming_wait(ffado_device_t *dev) {
237     static int periods=0;
238     static int periods_print=0;
239     static int xruns=0;
240
241     periods++;
242     if(periods>periods_print) {
243         debugOutputShort(DEBUG_LEVEL_NORMAL, "\nffado_streaming_wait\n");
244         debugOutputShort(DEBUG_LEVEL_NORMAL, "============================================\n");
245         debugOutputShort(DEBUG_LEVEL_NORMAL, "Xruns: %d\n", xruns);
246         debugOutputShort(DEBUG_LEVEL_NORMAL, "============================================\n");
247         dev->m_deviceManager->showStreamingInfo();
248         debugOutputShort(DEBUG_LEVEL_NORMAL, "\n");
249         periods_print+=100;
250     }
251
252     enum DeviceManager::eWaitResult result;
253     result = dev->m_deviceManager->waitForPeriod();
254     if(result == DeviceManager::eWR_OK) {
255         return ffado_wait_ok;
256     } else if (result == DeviceManager::eWR_Xrun) {
257         debugOutput(DEBUG_LEVEL_NORMAL, "Handled XRUN\n");
258         xruns++;
259         return ffado_wait_xrun;
260     } else if (result == DeviceManager::eWR_Shutdown) {
261         debugWarning("Streaming system requests shutdown.\n");
262         return ffado_wait_shutdown;
263     } else {
264         debugError("Error condition while waiting (Unhandled XRUN)\n");
265         xruns++;
266         return ffado_wait_error;
267     }
268 }
269
270 int ffado_streaming_transfer_capture_buffers(ffado_device_t *dev) {
271     return dev->m_deviceManager->getStreamProcessorManager().transfer(Streaming::StreamProcessor::ePT_Receive);
272 }
273
274 int ffado_streaming_transfer_playback_buffers(ffado_device_t *dev) {
275     return dev->m_deviceManager->getStreamProcessorManager().transfer(Streaming::StreamProcessor::ePT_Transmit);
276 }
277
278 int ffado_streaming_transfer_buffers(ffado_device_t *dev) {
279     return dev->m_deviceManager->getStreamProcessorManager().transfer();
280 }
281
282 int ffado_streaming_get_nb_capture_streams(ffado_device_t *dev) {
283     return dev->m_deviceManager->getStreamProcessorManager().getPortCount(Streaming::Port::E_Capture);
284 }
285
286 int ffado_streaming_get_nb_playback_streams(ffado_device_t *dev) {
287     return dev->m_deviceManager->getStreamProcessorManager().getPortCount(Streaming::Port::E_Playback);
288 }
289
290 int ffado_streaming_get_capture_stream_name(ffado_device_t *dev, int i, char* buffer, size_t buffersize) {
291     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
292     if(!p) {
293         debugWarning("Could not get capture port at index %d\n",i);
294         return -1;
295     }
296
297     std::string name=p->getName();
298     if (!strncpy(buffer, name.c_str(), buffersize)) {
299         debugWarning("Could not copy name\n");
300         return -1;
301     } else return 0;
302 }
303
304 int ffado_streaming_get_playback_stream_name(ffado_device_t *dev, int i, char* buffer, size_t buffersize) {
305     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
306     if(!p) {
307         debugWarning("Could not get playback port at index %d\n",i);
308         return -1;
309     }
310
311     std::string name=p->getName();
312     if (!strncpy(buffer, name.c_str(), buffersize)) {
313         debugWarning("Could not copy name\n");
314         return -1;
315     } else return 0;
316 }
317
318 ffado_streaming_stream_type ffado_streaming_get_capture_stream_type(ffado_device_t *dev, int i) {
319     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
320     if(!p) {
321         debugWarning("Could not get capture port at index %d\n",i);
322         return ffado_stream_type_invalid;
323     }
324     switch(p->getPortType()) {
325     case Streaming::Port::E_Audio:
326         return ffado_stream_type_audio;
327     case Streaming::Port::E_Midi:
328         return ffado_stream_type_midi;
329     case Streaming::Port::E_Control:
330         return ffado_stream_type_control;
331     default:
332         return ffado_stream_type_unknown;
333     }
334 }
335
336 ffado_streaming_stream_type ffado_streaming_get_playback_stream_type(ffado_device_t *dev, int i) {
337     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
338     if(!p) {
339         debugWarning("Could not get playback port at index %d\n",i);
340         return ffado_stream_type_invalid;
341     }
342     switch(p->getPortType()) {
343     case Streaming::Port::E_Audio:
344         return ffado_stream_type_audio;
345     case Streaming::Port::E_Midi:
346         return ffado_stream_type_midi;
347     case Streaming::Port::E_Control:
348         return ffado_stream_type_control;
349     default:
350         return ffado_stream_type_unknown;
351     }
352 }
353
354 int ffado_streaming_set_audio_datatype(ffado_device_t *dev,
355     ffado_streaming_audio_datatype t) {
356     switch(t) {
357         case ffado_audio_datatype_int24:
358             if(!dev->m_deviceManager->getStreamProcessorManager().setAudioDataType(
359                Streaming::StreamProcessorManager::eADT_Int24)) {
360                 debugError("Could not set datatype\n");
361                 return -1;
362             }
363             break;
364         case ffado_audio_datatype_float:
365             if(!dev->m_deviceManager->getStreamProcessorManager().setAudioDataType(
366                Streaming::StreamProcessorManager::eADT_Float)) {
367                 debugError("Could not set datatype\n");
368                 return -1;
369             }
370             break;
371         default:
372             debugError("Invalid audio datatype\n");
373             return -1;
374     }
375     return 0;
376 }
377
378 ffado_streaming_audio_datatype ffado_streaming_get_audio_datatype(ffado_device_t *dev) {
379     switch(dev->m_deviceManager->getStreamProcessorManager().getAudioDataType()) {
380         case Streaming::StreamProcessorManager::eADT_Int24:
381             return ffado_audio_datatype_int24;
382         case Streaming::StreamProcessorManager::eADT_Float:
383             return ffado_audio_datatype_float;
384         default:
385             debugError("Invalid audio datatype\n");
386             return ffado_audio_datatype_error;
387     }
388 }
389
390 int ffado_streaming_stream_onoff(ffado_device_t *dev, int i,
391     int on, enum Streaming::Port::E_Direction direction) {
392     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, direction);
393     if(!p) {
394         debugWarning("Could not get %s port at index %d\n",
395             (direction==Streaming::Port::E_Playback?"Playback":"Capture"),i);
396         return -1;
397     }
398     if(on) {
399         p->enable();
400     } else {
401         p->disable();
402     }
403     return 0;
404 }
405
406 int ffado_streaming_playback_stream_onoff(ffado_device_t *dev, int number, int on) {
407     return ffado_streaming_stream_onoff(dev, number, on, Streaming::Port::E_Playback);
408 }
409
410 int ffado_streaming_capture_stream_onoff(ffado_device_t *dev, int number, int on) {
411     return ffado_streaming_stream_onoff(dev, number, on, Streaming::Port::E_Capture);
412 }
413
414 int ffado_streaming_set_capture_stream_buffer(ffado_device_t *dev, int i, char *buff) {
415     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Capture);
416     // use an assert here performancewise,
417     // it should already have failed before, if not correct
418     assert(p);
419     p->setBufferAddress((void *)buff);
420     return 0;
421 }
422
423 int ffado_streaming_set_playback_stream_buffer(ffado_device_t *dev, int i, char *buff) {
424     Streaming::Port *p = dev->m_deviceManager->getStreamProcessorManager().getPortByIndex(i, Streaming::Port::E_Playback);
425     // use an assert here performancewise,
426     // it should already have failed before, if not correct
427     assert(p);
428     p->setBufferAddress((void *)buff);
429     return 0;
430 }
Note: See TracBrowser for help on using the browser.