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

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

- moved files around to the place they belong
- fixed all compile warnings

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