]> git.tuebingen.mpg.de Git - paraslash.git/blob - buffer_tree.h
Merge topic branch t/sf_float into pu
[paraslash.git] / buffer_tree.h
1 /* Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /**
4  * \file buffer_tree.h Buffer tree management.
5  *
6  * Buffer trees and buffer tree nodes.
7  *
8  * The buffer tree API offers an efficient method for managing the data flow
9  * from a producer (e.g. the network receiver) to the consumer(s) (e.g. a sound
10  * card).
11  *
12  * A buffer tree consists of buffer tree nodes which are linked together via
13  * parent/child relationships. Data buffers are propagated down without copying.
14  *
15  * Each data buffer starts its way from the root of the buffer tree. At each
16  * node the data is investigated and possibly changed. New data is then fed to
17  * each child. There are no file descriptors, no processes/threads and no calls
18  * to read() or write().
19  *
20  * Whenever a node in the buffer tree creates output, either by creating a new
21  * buffer or by pushing down buffers received from its parent, references to
22  * that buffer are created for all children of the node. The code avoids to
23  * copy buffer contents when possible.
24  *
25  * Communication between nodes is possible via the btr_exec_up() function. For
26  * example, in para_audiod the alsa writer asks all parent nodes for the number
27  * of channels and the sample rate of the current audio file.
28  *
29  * Buffer pools - An alternative to malloc/free buffer management.
30  *
31  * Non-leaf nodes usually create output to be processed by their child nodes.
32  * The data must be fed through the output channel(s) of the node in order to
33  * make that data available to each child.
34  *
35  * The easiest way to do so is to malloc() a buffer, fill it, and then call
36  * btr_add_output(). This adds references to that buffer to all children. The
37  * buffer is automatically freed if no buffer tree node is using it any more.
38  *
39  * This approach is simple but has some drawbacks. For example the data source
40  * represented by the root node does not know in advance how much data will be
41  * available. Therefore the allocated buffer will either be larger than
42  * necessary or too small so that multiple buffers have to be used.
43  *
44  * While this could be worked around by using a large buffer and calling
45  * realloc() afterwards to shrink the buffer according to how much has been
46  * read, there is a second problem which comes from the alignment constraints
47  * of some filters, mainly the decoders like mp3dec. These need a minimal
48  * amount of data to proceed, and most of them even need this amount as one
49  * contiguous buffer, i.e. not spread out over two or more buffers.
50  *
51  * Although the buffer tree code handles this case just fine, it can be
52  * expensive because two or more buffers must be merged by copying buffer
53  * contents around in order to satisfy the constraint.
54  *
55  * This is where buffer pools come into play. Buffer pools try to satisfy
56  * alignment constraints without copying buffer content whenever possible. To
57  * avoid spreading out the input data over the address space like in the
58  * malloc/free approach, a fixed large contiguous buffer (the area) is used
59  * instead. A buffer pool consists basically of an area and two pointers, the
60  * read head and the write head.
61  *
62  * Once a buffer pool has been created, its node, e.g. a receiver, obtains the
63  * current value of the write head and writes new data to this location. Then
64  * it calls btr_add_output_pool() to tell much data it has written. This
65  * advances the write head accordingly, and it also creates references to the
66  * newly written part of the area for the children of the node to consume.
67  *
68  * Child nodes consume data by working through their input queue, which is a
69  * list of buffer references. Once the content of a buffer is no longer needed
70  * by a child node, the child calls btr_consume() to indicate the amount of
71  * data which can be dropped from the child's point of view. If no reference
72  * to some region of the buffer pool area remains, the read head of the buffer
73  * pool advances, making space available for the receiver node to fill.
74  *
75  * No matter if malloc() or a buffer pool is used, the buffer tree code takes
76  * care of alignment constraints imposed by the consumers. In the buffer pool
77  * case, automatic merging of references to contiguous buffers is performed.
78  * memcpy is only used if a constraint can not be satisfied by using the
79  * remaining part of the area only. This only happens when the end of the area
80  * is reached.
81  */
82
83 struct btr_node;
84 struct btr_pool;
85
86 /**
87  * The three different types of buffer tree nodes.
88  *
89  * Usually, there is exactly one node in the buffer tree, the root node, which
90  * has no parent. Every node different from the root node has exactly one
91  * parent.  The root node represents a data source. Root nodes are thus used by
92  * the receivers of paraslash. Also, reading from stdin is realized as the root
93  * node of a buffer tree.
94  *
95  * Each node may have arbitrary many children, including none. Nodes with no
96  * children are called leaf nodes. They represent a data sink, like the alsa or
97  * the file writer.
98  *
99  * Hence there are three different types of buffer tree nodes: The root node
100  * and the leaf nodes and nodes which have both a parent and at least one
101  * child. Such a node is called an internal node.
102  *
103  * Internal nodes represent filters through which data buffers flow, possibly
104  * while being altered on their way to the children of the node. Examples of
105  * internal nodes are audio file decoders (mp3dec, oggdec, ...), but also the
106  * check for a wav header is implemented as an internal buffer tree node.
107  */
108 enum btr_node_type {
109         /* This node has no parent. */
110         BTR_NT_ROOT,
111         /* Node has parent and at least one child. */
112         BTR_NT_INTERNAL,
113         /* Node has no children. */
114         BTR_NT_LEAF,
115 };
116
117 /**
118  * Per node handler used for inter node communication.
119  *
120  * Each node in the buffer tree may optionally provide a command handler for
121  * execution of commands by other nodes of the tree.
122  *
123  * It is dependent on the node in question which commands are supported and how
124  * they work. In any case, the input for the command handler is some string and
125  * its output is also a string which is returned via the \a result pointer of
126  * the handler.
127  *
128  * This mechanism is used in para_audiod e.g. by the alsa writer which needs to
129  * know the sample rate of its input known to e.g. the mp3dec node further up
130  * in the buffer tree.
131  */
132 typedef int (*btr_command_handler)(const struct btr_node *btrn,
133                 const char *command, char **result);
134
135 /**
136  * Structure for creating new buffer tree nodes.
137  *
138  * btr_new_node() takes a pointer to such a structure.
139  *
140  * There are four different combinations of \a parent and child:
141  *
142  * 1. both \p NULL. This creates a new buffer tree with a single isolated node.
143  *
144  * 2. \a parent != \p NULL, \a child == NULL. This creates a new leaf node by
145  * adding the new node to the list of children of the given parent node.
146  *
147  * 3. \a parent == NULL, \a child != NULL. The new node becomes the new root of
148  * the buffer tree. The child must be old root.
149  *
150  * 4. both != NULL. This creates a new internal node. \a child must be child of
151  * p. This mode of operation is currently not needed and is thus not yet
152  * implemented.
153  */
154 struct btr_node_description {
155         /** Name of the new node. */
156         const char *name;
157         /** Parent of the new node. */
158         struct btr_node *parent;
159         /** Child of the new node. */
160         struct btr_node *child;
161         /** Used for inter node communication. Optional. */
162         btr_command_handler handler;
163         /** Points usually to the struct that contains the node pointer. */
164         void *context;
165 };
166
167 size_t btr_pool_size(const struct btr_pool *btrp);
168 struct btr_pool *btr_pool_new(const char *name, size_t area_size);
169 void btr_pool_free(struct btr_pool *btrp);
170 size_t btr_pool_get_buffer(const struct btr_pool *btrp, char **result);
171 int btr_pool_get_buffers(const struct btr_pool *btrp, struct iovec iov[2]);
172 void btr_add_output_pool(struct btr_pool *btrp, size_t size,
173         struct btr_node *btrn);
174 size_t btr_pool_unused(const struct btr_pool *btrp);
175 void btr_copy(const void *src, size_t n, struct btr_pool *btrp,
176         struct btr_node *btrn);
177 struct btr_node *btr_new_node(struct btr_node_description *bnd);
178 void btr_remove_node(struct btr_node **btrnp);
179 void btr_add_output(char *buf, size_t size, struct btr_node *btrn);
180 void btr_add_output_dont_free(const char *buf, size_t size, struct btr_node *btrn);
181 size_t btr_get_input_queue_size(const struct btr_node *btrn);
182 size_t btr_get_output_queue_size(const struct btr_node *btrn);
183 bool btr_no_parent(const struct btr_node *btrn);
184 size_t btr_next_buffer(const struct btr_node *btrn, char **bufp);
185 size_t btr_next_buffer_omit(const struct btr_node *btrn, size_t omit,
186                 char **bufp);
187 void btr_consume(struct btr_node *btrn, size_t numbytes);
188 int btr_exec_up(const struct btr_node *btrn, const char *command, char **value_result);
189 void btr_splice_out_node(struct btr_node **btrnp);
190 void btr_pushdown(struct btr_node *btrn);
191 void *btr_context(const struct btr_node *btrn);
192 void btr_merge(struct btr_node *btrn, size_t dest_size);
193 void btr_log_tree(const struct btr_node *btrn, int ll);
194 void btr_pushdown_one(struct btr_node *btrn);
195 bool btr_inplace_ok(const struct btr_node *btrn);
196 int btr_node_status(const struct btr_node *btrn, size_t min_iqs,
197                 enum btr_node_type type);
198 void btr_get_node_start(const struct btr_node *btrn, struct timeval *tv);
199 struct btr_node *btr_search_node(const char *name, struct btr_node *root);
200 void btr_drain(struct btr_node *btrn);
201 struct btr_node *btr_parent(const struct btr_node *btrn);