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

Revision 325, 9.2 kB (checked in by wagi, 16 years ago)

2006-11-20 Daniel Wagner <wagi@newton.monom.org>

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