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

Revision 248, 9.5 kB (checked in by pieterpalmers, 17 years ago)

- add API version check

  • 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 const int
68 freebob_get_api_version() {
69     return FREEBOB_API_VERSION;
70 }
71
72 freebob_handle_t
73 freebob_new_handle( int port )
74 {
75     freebob_handle_t handle = new struct freebob_handle;
76     if (! handle ) {
77         debugFatal( "Could not allocate memory for new handle\n" );
78         return 0;
79     }
80
81     handle->m_deviceManager = new DeviceManager();
82     if ( !handle->m_deviceManager ) {
83         debugFatal( "Could not allocate device manager\n" );
84         delete handle;
85         return 0;
86     }
87     if ( !handle->m_deviceManager->initialize( port ) ) {
88         debugFatal( "Could not initialize device manager\n" );
89         delete handle->m_deviceManager;
90         delete handle;
91         return 0;
92     }
93     return handle;
94 }
95
96 int
97 freebob_destroy_handle( freebob_handle_t freebob_handle )
98 {
99     delete freebob_handle->m_deviceManager;
100     delete freebob_handle;
101     return 0;
102 }
103
104 int
105 freebob_discover_devices( freebob_handle_t freebob_handle, int verbose )
106 {
107     return freebob_handle->m_deviceManager->discover(verbose)? 0 : -1;
108 }
109
110 freebob_connection_info_t*
111 freebob_get_connection_info( freebob_handle_t freebob_handle,
112                              int node_id,
113                              enum freebob_direction direction )
114 {
115     xmlDocPtr doc;
116     doc = freebob_handle->m_deviceManager->getXmlDescription();
117     if ( !doc ) {
118         debugFatal( "Could not get XML description\n" );
119         return 0;
120     }
121
122     return freebob_xmlparse_get_connection_info( doc, node_id, direction );
123 }
124
125 freebob_supported_stream_format_info_t*
126 freebob_get_supported_stream_format_info( freebob_handle_t freebob_handle,
127                                           int node_id,
128                                           enum freebob_direction direction )
129 {
130     xmlDocPtr doc;
131     doc = freebob_handle->m_deviceManager->getXmlDescription();
132     if ( !doc ) {
133         debugFatal( "Could not get XML description\n" );
134         return 0;
135     }
136
137     return freebob_xmlparse_get_stream_formats( doc, node_id, direction );
138 }
139
140 int
141 freebob_node_is_valid_freebob_device( freebob_handle_t freebob_handle, int node_id )
142 {
143     return freebob_handle->m_deviceManager->isValidNode( node_id );
144 }
145
146 int
147 freebob_get_nb_devices_on_bus( freebob_handle_t freebob_handle )
148 {
149     return freebob_handle->m_deviceManager->getNbDevices();
150 }
151
152 int
153 freebob_get_device_node_id( freebob_handle_t freebob_handle, int device_nr )
154 {
155     return freebob_handle->m_deviceManager->getDeviceNodeId(device_nr);
156 }
157
158 int
159 freebob_set_samplerate( freebob_handle_t freebob_handle, int node_id, int samplerate )
160 {
161     IAvDevice* avDevice = freebob_handle->m_deviceManager->getAvDevice( node_id );
162     if ( avDevice ) {
163         if ( avDevice->setSamplingFrequency( parseSampleRate( samplerate ) ) ) {
164             return freebob_handle->m_deviceManager->discover(0)? 1 : 0;
165         } else {
166             return -1;
167         }
168     }
169     return -1;
170 }
171
172 void
173 freebob_free_connection_info( freebob_connection_info_t* connection_info )
174 {
175     if ( !connection_info ) {
176         return;
177     }
178
179     for ( int i = 0; i < connection_info->nb_connections; ++i ) {
180         freebob_free_connection_spec( connection_info->connections[i] );
181     }
182
183     free( connection_info->connections );
184     free( connection_info );
185 }
186
187 void
188 freebob_free_connection_spec( freebob_connection_spec_t* connection_spec )
189 {
190     if ( !connection_spec ) {
191         return;
192     }
193
194     freebob_free_stream_info( connection_spec->stream_info );
195     free( connection_spec );
196 }
197
198 void freebob_free_stream_info( freebob_stream_info_t* stream_info )
199 {
200     if ( !stream_info ) {
201         return;
202     }
203
204     for ( int i = 0; i < stream_info->nb_streams; ++i ) {
205         freebob_free_stream_spec( stream_info->streams[i] );
206     }
207
208     free(stream_info->streams);
209     free(stream_info);
210 }
211
212 void freebob_free_stream_spec( freebob_stream_spec_t* stream_spec )
213 {
214     if ( !stream_spec ) {
215         return;
216     }
217
218     free( stream_spec );
219 }
220
221 void
222 freebob_free_supported_stream_format_info( freebob_supported_stream_format_info_t* stream_info )
223 {
224     if ( !stream_info ) {
225         return;
226     }
227
228     for ( int i = 0; i < stream_info->nb_formats; ++i ) {
229         freebob_free_supported_stream_format_spec( stream_info->formats[i] );
230     }
231
232     free(stream_info->formats);
233     free(stream_info);
234 }
235
236 void
237 freebob_free_supported_stream_format_spec( freebob_supported_stream_format_spec_t* stream_spec )
238 {
239     if ( !stream_spec ) {
240         return;
241     }
242
243     free( stream_spec );
244 }
245
246 void
247 freebob_print_connection_info( freebob_connection_info_t* connection_info )
248 {
249     if ( !connection_info ) {
250         fprintf( stderr, "connection_info==NULL\n" );
251         return;
252     }
253
254     printf( "Direction:              %d (%s)\n\n", connection_info->direction,
255             connection_info->direction? "playback" : "capture" );
256
257     puts( "Connection Info" );
258     puts( "===============\n" );
259
260     printf("Number of connections:  %d\n\n",
261            connection_info->nb_connections );
262
263     for ( int i = 0; i < connection_info->nb_connections; ++i) {
264         freebob_connection_spec_t* connection_spec
265             = connection_info->connections[i];
266
267
268         if ( connection_spec ) {
269             printf( "  Connection %2d\n", i );
270             printf( "  -------------\n" );
271             printf( "    [%2d] Id:         %d\n", i, connection_spec->id );
272             printf( "    [%2d] Port:       %d\n", i, connection_spec->port );
273             printf( "    [%2d] Node:       %d\n", i, connection_spec->node );
274             printf( "    [%2d] Plug:       %d\n", i, connection_spec->plug );
275             printf( "    [%2d] Dimension:  %d\n", i, connection_spec->dimension );
276             printf( "    [%2d] Samplerate: %d\n", i, connection_spec->samplerate );
277             printf( "    [%2d] IsoChannel: %d\n", i, connection_spec->iso_channel );
278             printf( "    [%2d] IsMaster:   %d\n", i, connection_spec->is_master );
279
280             if ( connection_info->connections[i]->stream_info ) {
281                 printf("    [%2d] Number of stream infos: %d\n\n",
282                        i, connection_spec->stream_info->nb_streams );
283
284                 printf("    StreamId  Position Location Format Type DPort Name\n");
285                 printf("    --------------------------------------------------\n");
286
287                 for ( int j = 0;
288                       j < connection_spec->stream_info->nb_streams;
289                       ++j )
290                 {
291                     freebob_stream_spec_t* stream_spec
292                         = connection_spec->stream_info->streams[j];
293
294                     printf("    [%2d]:[%2d] "
295                            "0x%02x     0x%02x     0x%02x   0x%02x 0x%02x  %s\n",
296                            i, j,
297                            stream_spec->position,
298                            stream_spec->location,
299                            stream_spec->format,
300                            stream_spec->type,
301                            stream_spec->destination_port,
302                            stream_spec->name );
303                 }
304             }
305         }
306         printf( "\n" );
307     }
308
309     return;
310 }
311
312 void
313 freebob_print_supported_stream_format_info( freebob_supported_stream_format_info_t* stream_info )
314 {
315     if ( !stream_info ) {
316         fprintf( stderr, "stream_info==NULL\n" );
317         return;
318     }
319
320     printf( "Direction:              %d (%s)\n\n", stream_info->direction,
321             stream_info->direction? "playback" : "capture" );
322
323     printf( "Samplerate AudioChannels MidiChannels\n" );
324     printf( "-------------------------------------\n" );
325     for ( int i = 0; i < stream_info->nb_formats; ++i) {
326         freebob_supported_stream_format_spec_t* format_spec
327             = stream_info->formats[i];
328
329         if ( format_spec ) {
330             printf( "%05d      %02d            %02d\n",
331                     format_spec->samplerate,
332                     format_spec->nb_audio_channels,
333                     format_spec->nb_midi_channels );
334         }
335     }
336
337     return;
338 }
339
340 /* debug function */
341 void
342 freebob_print_xml_description( freebob_handle_t freebob_handle,
343                              int node_id,
344                              enum freebob_direction direction )
345 {
346     xmlDocPtr doc;
347     doc = freebob_handle->m_deviceManager->getXmlDescription();
348     if ( !doc ) {
349         debugFatal( "Could not get XML description\n" );
350         return;
351     }
352
353     xmlChar* xmlbuff;
354     int buffersize;
355     xmlDocDumpFormatMemory( doc, &xmlbuff, &buffersize, 1 );
356
357         printf("%s\n",(char *)xmlbuff);
358
359         xmlFree(xmlbuff);
360         xmlFree(doc);
361     return;
362 }
363
364 void freebob_sleep_after_avc_command( int time )
365 {
366     AVCCommand::setSleepAfterAVCCommand( time );
367 }
Note: See TracBrowser for help on using the browser.