root/branches/freebob-syt-experiments/src/freebob.cpp

Revision 186, 9.1 kB (checked in by wagi, 18 years ago)

2006-05-03 Daniel Wagner <wagi@monom.org>

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