root/branches/libfreebob-2.0/src/freebob.cpp

Revision 242, 9.4 kB (checked in by pieterpalmers, 17 years ago)

- made the bounce device actually work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* freebob.cpp
2  * Copyright (C) 2005 Pieter Palmers, Daniel Wagner
3  *
4  * This file is part of FreeBoB
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include "config.h"
23
24 #include "libfreebob/freebob.h"
25 #include "libfreebob/xmlparser.h"
26
27 #include "debugmodule/debugmodule.h"
28 #include "fbtypes.h"
29 #include "devicemanager.h"
30 #include "iavdevice.h"
31
32 #include "libfreebobavc/avc_generic.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <libxml/xmlmemory.h>
38 #include <libxml/parser.h>
39
40 DECLARE_GLOBAL_DEBUG_MODULE;
41 IMPL_GLOBAL_DEBUG_MODULE( FreeBob, DEBUG_LEVEL_VERBOSE );
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 // this is very much nescessary, as otherwise the
48 // message buffer thread doesn't get killed when the
49 // library is dlclose()'d
50
51 static void exitfunc(void) __attribute__((destructor));
52
53 static void exitfunc(void)
54 {
55     delete DebugModuleManager::instance();
56
57 }
58 #ifdef __cplusplus
59 }
60 #endif
61
62 const char*
63 freebob_get_version() {
64     return PACKAGE_STRING;
65 }
66
67 freebob_handle_t
68 freebob_new_handle( int port )
69 {
70     freebob_handle_t handle = new struct freebob_handle;
71     if (! handle ) {
72         debugFatal( "Could not allocate memory for new handle\n" );
73         return 0;
74     }
75
76     handle->m_deviceManager = new DeviceManager();
77     if ( !handle->m_deviceManager ) {
78         debugFatal( "Could not allocate device manager\n" );
79         delete handle;
80         return 0;
81     }
82     if ( !handle->m_deviceManager->initialize( port ) ) {
83         debugFatal( "Could not initialize device manager\n" );
84         delete handle->m_deviceManager;
85         delete handle;
86         return 0;
87     }
88     return handle;
89 }
90
91 int
92 freebob_destroy_handle( freebob_handle_t freebob_handle )
93 {
94     delete freebob_handle->m_deviceManager;
95     delete freebob_handle;
96     return 0;
97 }
98
99 int
100 freebob_discover_devices( freebob_handle_t freebob_handle, int verbose )
101 {
102     return freebob_handle->m_deviceManager->discover(verbose)? 0 : -1;
103 }
104
105 freebob_connection_info_t*
106 freebob_get_connection_info( freebob_handle_t freebob_handle,
107                              int node_id,
108                              enum freebob_direction direction )
109 {
110     xmlDocPtr doc;
111     doc = freebob_handle->m_deviceManager->getXmlDescription();
112     if ( !doc ) {
113         debugFatal( "Could not get XML description\n" );
114         return 0;
115     }
116
117     return freebob_xmlparse_get_connection_info( doc, node_id, direction );
118 }
119
120 freebob_supported_stream_format_info_t*
121 freebob_get_supported_stream_format_info( freebob_handle_t freebob_handle,
122                                           int node_id,
123                                           enum freebob_direction direction )
124 {
125     xmlDocPtr doc;
126     doc = freebob_handle->m_deviceManager->getXmlDescription();
127     if ( !doc ) {
128         debugFatal( "Could not get XML description\n" );
129         return 0;
130     }
131
132     return freebob_xmlparse_get_stream_formats( doc, node_id, direction );
133 }
134
135 int
136 freebob_node_is_valid_freebob_device( freebob_handle_t freebob_handle, int node_id )
137 {
138     return freebob_handle->m_deviceManager->isValidNode( node_id );
139 }
140
141 int
142 freebob_get_nb_devices_on_bus( freebob_handle_t freebob_handle )
143 {
144     return freebob_handle->m_deviceManager->getNbDevices();
145 }
146
147 int
148 freebob_get_device_node_id( freebob_handle_t freebob_handle, int device_nr )
149 {
150     return freebob_handle->m_deviceManager->getDeviceNodeId(device_nr);
151 }
152
153 int
154 freebob_set_samplerate( freebob_handle_t freebob_handle, int node_id, int samplerate )
155 {
156     IAvDevice* avDevice = freebob_handle->m_deviceManager->getAvDevice( node_id );
157     if ( avDevice ) {
158         if ( avDevice->setSamplingFrequency( parseSampleRate( samplerate ) ) ) {
159             return freebob_handle->m_deviceManager->discover(0)? 1 : 0;
160         } else {
161             return -1;
162         }
163     }
164     return -1;
165 }
166
167 void
168 freebob_free_connection_info( freebob_connection_info_t* connection_info )
169 {
170     if ( !connection_info ) {
171         return;
172     }
173
174     for ( int i = 0; i < connection_info->nb_connections; ++i ) {
175         freebob_free_connection_spec( connection_info->connections[i] );
176     }
177
178     free( connection_info->connections );
179     free( connection_info );
180 }
181
182 void
183 freebob_free_connection_spec( freebob_connection_spec_t* connection_spec )
184 {
185     if ( !connection_spec ) {
186         return;
187     }
188
189     freebob_free_stream_info( connection_spec->stream_info );
190     free( connection_spec );
191 }
192
193 void freebob_free_stream_info( freebob_stream_info_t* stream_info )
194 {
195     if ( !stream_info ) {
196         return;
197     }
198
199     for ( int i = 0; i < stream_info->nb_streams; ++i ) {
200         freebob_free_stream_spec( stream_info->streams[i] );
201     }
202
203     free(stream_info->streams);
204     free(stream_info);
205 }
206
207 void freebob_free_stream_spec( freebob_stream_spec_t* stream_spec )
208 {
209     if ( !stream_spec ) {
210         return;
211     }
212
213     free( stream_spec );
214 }
215
216 void
217 freebob_free_supported_stream_format_info( freebob_supported_stream_format_info_t* stream_info )
218 {
219     if ( !stream_info ) {
220         return;
221     }
222
223     for ( int i = 0; i < stream_info->nb_formats; ++i ) {
224         freebob_free_supported_stream_format_spec( stream_info->formats[i] );
225     }
226
227     free(stream_info->formats);
228     free(stream_info);
229 }
230
231 void
232 freebob_free_supported_stream_format_spec( freebob_supported_stream_format_spec_t* stream_spec )
233 {
234     if ( !stream_spec ) {
235         return;
236     }
237
238     free( stream_spec );
239 }
240
241 void
242 freebob_print_connection_info( freebob_connection_info_t* connection_info )
243 {
244     if ( !connection_info ) {
245         fprintf( stderr, "connection_info==NULL\n" );
246         return;
247     }
248
249     printf( "Direction:              %d (%s)\n\n", connection_info->direction,
250             connection_info->direction? "playback" : "capture" );
251
252     puts( "Connection Info" );
253     puts( "===============\n" );
254
255     printf("Number of connections:  %d\n\n",
256            connection_info->nb_connections );
257
258     for ( int i = 0; i < connection_info->nb_connections; ++i) {
259         freebob_connection_spec_t* connection_spec
260             = connection_info->connections[i];
261
262
263         if ( connection_spec ) {
264             printf( "  Connection %2d\n", i );
265             printf( "  -------------\n" );
266             printf( "    [%2d] Id:         %d\n", i, connection_spec->id );
267             printf( "    [%2d] Port:       %d\n", i, connection_spec->port );
268             printf( "    [%2d] Node:       %d\n", i, connection_spec->node );
269             printf( "    [%2d] Plug:       %d\n", i, connection_spec->plug );
270             printf( "    [%2d] Dimension:  %d\n", i, connection_spec->dimension );
271             printf( "    [%2d] Samplerate: %d\n", i, connection_spec->samplerate );
272             printf( "    [%2d] IsoChannel: %d\n", i, connection_spec->iso_channel );
273             printf( "    [%2d] IsMaster:   %d\n", i, connection_spec->is_master );
274
275             if ( connection_info->connections[i]->stream_info ) {
276                 printf("    [%2d] Number of stream infos: %d\n\n",
277                        i, connection_spec->stream_info->nb_streams );
278
279                 printf("    StreamId  Position Location Format Type DPort Name\n");
280                 printf("    --------------------------------------------------\n");
281
282                 for ( int j = 0;
283                       j < connection_spec->stream_info->nb_streams;
284                       ++j )
285                 {
286                     freebob_stream_spec_t* stream_spec
287                         = connection_spec->stream_info->streams[j];
288
289                     printf("    [%2d]:[%2d] "
290                            "0x%02x     0x%02x     0x%02x   0x%02x 0x%02x  %s\n",
291                            i, j,
292                            stream_spec->position,
293                            stream_spec->location,
294                            stream_spec->format,
295                            stream_spec->type,
296                            stream_spec->destination_port,
297                            stream_spec->name );
298                 }
299             }
300         }
301         printf( "\n" );
302     }
303
304     return;
305 }
306
307 void
308 freebob_print_supported_stream_format_info( freebob_supported_stream_format_info_t* stream_info )
309 {
310     if ( !stream_info ) {
311         fprintf( stderr, "stream_info==NULL\n" );
312         return;
313     }
314
315     printf( "Direction:              %d (%s)\n\n", stream_info->direction,
316             stream_info->direction? "playback" : "capture" );
317
318     printf( "Samplerate AudioChannels MidiChannels\n" );
319     printf( "-------------------------------------\n" );
320     for ( int i = 0; i < stream_info->nb_formats; ++i) {
321         freebob_supported_stream_format_spec_t* format_spec
322             = stream_info->formats[i];
323
324         if ( format_spec ) {
325             printf( "%05d      %02d            %02d\n",
326                     format_spec->samplerate,
327                     format_spec->nb_audio_channels,
328                     format_spec->nb_midi_channels );
329         }
330     }
331
332     return;
333 }
334
335 /* debug function */
336 void
337 freebob_print_xml_description( freebob_handle_t freebob_handle,
338                              int node_id,
339                              enum freebob_direction direction )
340 {
341     xmlDocPtr doc;
342     doc = freebob_handle->m_deviceManager->getXmlDescription();
343     if ( !doc ) {
344         debugFatal( "Could not get XML description\n" );
345         return;
346     }
347
348     xmlChar* xmlbuff;
349     int buffersize;
350     xmlDocDumpFormatMemory( doc, &xmlbuff, &buffersize, 1 );
351
352         printf("%s\n",(char *)xmlbuff);
353
354         xmlFree(xmlbuff);
355         xmlFree(doc);
356     return;
357 }
358
359 void freebob_sleep_after_avc_command( int time )
360 {
361     AVCCommand::setSleepAfterAVCCommand( time );
362 }
Note: See TracBrowser for help on using the browser.