root/branches/streaming-rework/src/freebob_streaming.cpp

Revision 412, 17.7 kB (checked in by pieterpalmers, 16 years ago)

- added some documentation
- added a lock()/unlock() to IAvDevice (see header)
- reimplemented test-freebob to the new C++ api
- started with support for AddressRangeMapping?, i.e. response

to reads/writes of the 1394 memory space on the host

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