root/trunk/libfreebob/libfreebob/freebob_streaming.h

Revision 185, 13.8 kB (checked in by wagi, 17 years ago)

CVS-SVN migration developer public sync patch

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  *   FreeBob Streaming API
3  *   FreeBob = Firewire (pro-)audio for linux
4  *
5  *   http://freebob.sf.net
6  *
7  *   Copyright (C) 2005 Pieter Palmers <pieterpalmers@users.sourceforge.net>
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *
24  *
25  */
26
27 /* freebob_streaming.h
28  *
29  * Specification for the FreeBob Streaming API
30  *
31  */
32 #ifndef __FREEBOB_STREAMING_H__
33 #define __FREEBOB_STREAMING_H__
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #define FREEBOB_STREAMING_MAX_URL_LENGTH 2048
40
41 #define FREEBOB_IGNORE_CAPTURE          (1<<0)
42 #define FREEBOB_IGNORE_PLAYBACK         (1<<1)
43
44 #include <stdlib.h>
45
46 /* The basic operation of the API is as follows:
47  *
48  * freebob_streaming_init()
49  * freebob_streaming_start()
50  * while(running) {
51  *   retval = freebob_streaming_wait();
52  *   if (retval == -1) {
53  *     freebob_streaming_reset();
54  *     continue;
55  *   }
56  *
57  *   freebob_streaming_transfer_buffers(dev);
58  *
59  *   for(all channels) {
60  *     switch (channel_type) {
61  *     case audio:
62  *       bytesread=freebob_streaming_read(audioinbuffer[channel]);
63  *       byteswritten=freebob_streaming_write(audiooutbuffer[channel]);
64  *     case midi:
65  *       bytesread=freebob_streaming_read(midiinbuffer[channel]);
66  *       byteswritten=freebob_streaming_write(midioutbuffer[channel]);
67  *     }
68  *   }
69  * }
70  * freebob_streaming_stop();
71  * freebob_streaming_finish();
72  *
73  */
74
75 typedef struct _freebob_device freebob_device_t;
76
77 /**
78  * The sample format used by the freebob streaming API
79  */
80
81 typedef unsigned int freebob_sample_t;
82
83
84 typedef unsigned int freebob_nframes_t;
85
86
87 typedef struct freebob_device_info {
88         /* TODO: How is the device specification done? */
89 //      char xml_location[FREEBOB_STREAMING_MAX_URL_LENGTH]; // can be an osc url or an XML filename
90 //      freebob_device_info_location_type location_type;
91 } freebob_device_info_t;
92
93 /**
94  * Structure to pass the options to the freebob streaming code.
95  */
96 typedef struct freebob_options {
97         /* driver related setup */
98         int sample_rate;                /* this is acutally dictated by the device
99                                                          * you can specify a value here or -1 to autodetect
100                                                          */
101
102         /* buffer setup */
103         int period_size;        /* one period is the amount of frames that
104                                  * has to be sent or received in order for
105                                  * a period boundary to be signalled.
106                                  * (unit: frames)
107                                  */
108         int nb_buffers; /* the size of the frame buffer (in periods) */
109
110         /* RAW1394 related settings */
111         int iso_buffers;
112         int iso_prebuffers;
113         int iso_irq_interval;
114        
115         /* packetizer thread options */
116         int realtime;
117         int packetizer_priority;
118        
119         /* libfreebob related setup */
120         int node_id;
121         int port;
122        
123
124         /* direction map */
125         int directions;
126
127 } freebob_options_t;
128
129 /**
130  * The types of streams supported by the API
131  *
132  * A freebob_audio type stream is a stream that consists of successive samples.
133  * The format is a 24bit UINT in host byte order, aligned as the 24LSB's of the
134  * 32bit UINT of the read/write buffer.
135  * The wait operation looks at this type of streams only.
136  *
137  * A freebob_midi type stream is a stream of midi bytes. The bytes are 8bit UINT,
138  * aligned as the first 8LSB's of the 32bit UINT of the read/write buffer.
139  *
140  */
141 typedef enum {
142         freebob_stream_type_invalid                      =   -1,
143         freebob_stream_type_unknown                      =   0,
144         freebob_stream_type_audio                        =   1,
145         freebob_stream_type_midi                         =   2,
146 } freebob_streaming_stream_type;
147
148 /**
149  *
150  * Buffer types known to the API
151  *
152  */
153 typedef enum {
154         freebob_buffer_type_per_stream          =   -1, // use this to use the per-stream read functions
155         freebob_buffer_type_uint24           =   0,
156         freebob_buffer_type_float            =   1,
157 //      freebob_buffer_type_uint32           =   2,
158 //      freebob_buffer_type_double           =   3,
159 //      ...
160 } freebob_streaming_buffer_type;
161
162 /**
163  * Initializes the streaming from/to a FreeBob device. A FreeBob device
164  * is a virtual device composed of several BeBoB or compatible devices,
165  * linked together in one sync domain.
166  *
167  * This prepares all IEEE1394 related stuff and sets up all buffering.
168  * It elects a sync master if nescessary.
169  *
170  * @param device_info provides a way to specify the virtual device
171  * @param options options regarding buffers, ieee1394 setup, ...
172  *
173  * @return Opaque device handle if successful.  If this is NULL, the
174  * init operation failed.
175  *
176  */
177 freebob_device_t *freebob_streaming_init (freebob_device_info_t *device_info,
178                                         freebob_options_t options);
179
180 /**
181  * Finishes the FreeBob streaming. Cleans up all internal data structures
182  * and terminates connections.
183  *
184  * @param dev the freebob device to be closed.
185  */
186 void freebob_streaming_finish(freebob_device_t *dev);
187
188 /**
189  * Returns the amount of capture channels available
190  *
191  * @param dev the freebob device
192  *
193  * @return the number of capture streams present & active on the device.
194  *         can be 0. returns -1 upon error.
195  */
196 int freebob_streaming_get_nb_capture_streams(freebob_device_t *dev);
197
198 /**
199  * Returns the amount of playack channels available
200  *
201  * @param dev the freebob device
202  *
203  * @return the number of playback streams present & active on the device.
204  *         can be 0. returns -1 upon error.
205  */
206 int freebob_streaming_get_nb_playback_streams(freebob_device_t *dev);
207
208 /**
209  * Copies the capture channel name into the specified buffer
210  *
211  * @param dev the freebob device
212  * @param number the stream number
213  * @param buffer the buffer to copy the name into. has to be allocated.
214  * @param buffersize the size of the buffer
215  *
216  * @return the number of characters copied into the buffer
217  */
218 int freebob_streaming_get_capture_stream_name(freebob_device_t *dev, int number, char* buffer, size_t buffersize);
219
220 /**
221  * Copies the playback channel name into the specified buffer
222  *
223  * @param dev the freebob device
224  * @param number the stream number
225  * @param buffer the buffer to copy the name into. has to be allocated.
226  * @param buffersize the size of the buffer
227  *
228  * @return the number of characters copied into the buffer
229  */
230 int freebob_streaming_get_playback_stream_name(freebob_device_t *dev, int number, char* buffer, size_t buffersize);
231
232 /**
233  * Returns the type of a capture channel
234  *
235  * @param dev the freebob device
236  * @param number the stream number
237  *
238  * @return the channel type
239  */
240 freebob_streaming_stream_type freebob_streaming_get_capture_stream_type(freebob_device_t *dev, int number);
241
242 /**
243  * Returns the type of a playback channel
244  *
245  * @param dev the freebob device
246  * @param number the stream number
247  *
248  * @return the channel type
249  */
250 freebob_streaming_stream_type freebob_streaming_get_playback_stream_type(freebob_device_t *dev, int number);
251 /*
252  *
253  * Note: buffer handling will change in order to allow setting the sample type for *_read and *_write
254  * and separately indicate if you want to use a user buffer or a managed buffer.
255  *
256  */
257 /**
258  * Sets the decode buffer for the stream. This allows for zero-copy decoding.
259  * The call to freebob_streaming_transfer_buffers will decode one period of the stream to
260  * this buffer. Make sure it is large enough.
261  *
262  * @param dev the freebob device
263  * @param number the stream number
264  * @param buff a pointer to the sample buffer, make sure it is large enough
265  *             i.e. sizeof(your_sample_type)*period_size
266  * @param t   the type of the buffer. this determines sample type and the decode function used.
267  *
268  * @return -1 on error, 0 on success
269  */
270 int freebob_streaming_set_capture_stream_buffer(freebob_device_t *dev, int number, char *buff, freebob_streaming_buffer_type t);
271
272 /**
273  * Sets the encode buffer for the stream. This allows for zero-copy encoding (directly to the events).
274  * The call to freebob_streaming_transfer_buffers will encode one period of the stream from
275  * this buffer to the event buffer.
276  *
277  * @param dev the freebob device
278  * @param number the stream number
279  * @param buff a pointer to the sample buffer
280  * @param t   the type of the buffer. this determines sample type and the decode function used.
281  *
282  * @return -1 on error, 0 on success
283  */
284 int freebob_streaming_set_playback_stream_buffer(freebob_device_t *dev, int number, char *buff, freebob_streaming_buffer_type t);
285
286
287 /**
288  * Starts the streaming operation. This initiates the connections to the FreeBob devices and
289  * starts the packet handling thread(s). This has to be called before any I/O can occur.
290  *
291  * @param dev the freebob device
292  *
293  * @return 0 on success, -1 on failure.
294  */
295 int freebob_streaming_start(freebob_device_t *dev);
296
297 /**
298  * Stops the streaming operation. This closes the connections to the FreeBob devices and
299  * stops the packet handling thread(s).
300  *
301  * @param dev the freebob device
302  *
303  * @return 0 on success, -1 on failure.
304  */
305 int freebob_streaming_stop(freebob_device_t *dev);
306
307 /**
308  * Resets the streaming as if it was stopped and restarted. The difference is that the connections
309  * are not nescessarily broken and restored.
310  *
311  * All buffers are reset in the initial state and all data in them is lost.
312  *
313  * @param dev the freebob device
314  *
315  * @return 0 on success, -1 on failure.
316  */
317 int freebob_streaming_reset(freebob_device_t *dev);
318
319 /**
320  * Waits until there is at least one period of data available on all capture connections and
321  * room for one period of data on all playback connections
322  *
323  * @param dev the freebob device
324  *
325  * @return The number of frames ready. -1 when a problem occurred.
326  */
327 int freebob_streaming_wait(freebob_device_t *dev);
328
329 /**
330  * Reads from a specific channel to a supplied buffer.
331  *
332  * @param dev the freebob device
333  * @param number the stream number
334  * @param buffer the buffer to copy the samples into
335  * @param nsamples the number of samples to be read. the buffer has to be big enough for this amount of samples.
336  *
337  * @return the amount of samples actually read. -1 on error (xrun).
338  */
339 int freebob_streaming_read(freebob_device_t *dev, int number, freebob_sample_t *buffer, int nsamples);
340
341 /**
342  * Write to a specific channel from a supplied buffer.
343  *
344  * @param dev the freebob device
345  * @param number the stream number
346  * @param buffer the buffer to copy the samples from
347  * @param nsamples the number of samples to be written.
348  *
349  * @return the amount of samples actually written. -1 on error.
350  */
351 int freebob_streaming_write(freebob_device_t *dev, int number, freebob_sample_t *buffer, int nsamples);
352
353 /**
354  * Transfer & decode the events from the packet buffer to the sample buffers
355  *
356  * This should be called after the wait call returns, before reading/writing the sample buffers
357  * with freebob_streaming_[read|write].
358  *
359  * The purpose is to allow more precise timing information. freebob_streaming_wait returns as soon as the
360  * period boundary is crossed, and can therefore be used to determine the time instant of this crossing (e.g. jack DLL).
361  *
362  * The actual decoding work is done in this function and can therefore be omitted in this timing calculation.
363  * Note that you HAVE to call this function in order for the buffers not to overflow, and only call it when
364  * freebob_streaming_wait doesn't indicate a buffer xrun (xrun handler resets buffer).
365  *
366  * If user supplied playback buffers are specified with freebob_streaming_set_playback_buffers
367  * their contents should be valid before calling this function.
368  * If user supplied capture buffers are specified with freebob_streaming_set_capture_buffers
369  * their contents are updated in this function.
370  *
371  * Use either freebob_streaming_transfer_buffers to transfer all buffers at once, or use
372  * freebob_streaming_transfer_playback_buffers and freebob_streaming_transfer_capture_buffers
373  * to have more control. Don't use both.
374  *
375  * @param dev the freebob device
376  * @return  -1 on error.
377  */
378  
379 int freebob_streaming_transfer_buffers(freebob_device_t *dev);
380
381 /**
382  * Transfer & encode the events from the sample buffers to the packet buffer
383  *
384  * This should be called after the wait call returns, after writing the sample buffers
385  * with freebob_streaming_write.
386  *
387  * If user supplied playback buffers are specified with freebob_streaming_set_playback_buffers
388  * their contents should be valid before calling this function.
389  *
390  * Use either freebob_streaming_transfer_buffers to transfer all buffers at once, or use
391  * freebob_streaming_transfer_playback_buffers and freebob_streaming_transfer_capture_buffers
392  * to have more control. Don't use both.
393  *
394  * @param dev the freebob device
395  * @return  -1 on error.
396  */
397  
398 int freebob_streaming_transfer_playback_buffers(freebob_device_t *dev);
399
400 /**
401  * Transfer & decode the events from the packet buffer to the sample buffers
402  *
403  * This should be called after the wait call returns, before reading the sample buffers
404  * with freebob_streaming_read.
405  *
406  * If user supplied capture buffers are specified with freebob_streaming_set_capture_buffers
407  * their contents are updated in this function.
408  *
409  * Use either freebob_streaming_transfer_buffers to transfer all buffers at once, or use
410  * freebob_streaming_transfer_playback_buffers and freebob_streaming_transfer_capture_buffers
411  * to have more control. Don't use both.
412  *
413  * @param dev the freebob device
414  * @return  -1 on error.
415  */
416
417 int freebob_streaming_transfer_capture_buffers(freebob_device_t *dev);
418
419 /**
420  * Returns the packetizer thread to allow RT enabling by the host.
421  *
422  * @param dev the freebob device
423  *
424  * @return the thread.
425  */
426 pthread_t freebob_streaming_get_packetizer_thread(freebob_device_t *dev);
427
428
429 #ifdef __cplusplus
430 }
431 #endif
432
433 #endif
Note: See TracBrowser for help on using the browser.