Remove some unused error codes.
[paraslash.git] / buffer_tree.h
1 /**
2  * \file buffer_tree.h Buffer tree management.
3  *
4  * \par Buffer trees and buffer tree nodes.
5  * The buffer tree API offers a more powerful method than standard unix pipes
6  * for managing the data flow from the producer of the data (e.g. the network
7  * receiver) to its consumer(s) (e.g. a sound card).
8  *
9  * A buffer tree consists of buffer tree nodes linked via certain parent/child
10  * relationships.
11  *
12  * Each data buffer starts its way from the root of the buffer tree. At each
13  * node the data is investigated and possibly changed. New data is then fed to
14  * each child.  Everything happens within one single-treaded process. There are
15  * no file descriptors and no calls to read() or write().
16  *
17  * Whenever a node in the buffer tree creates output, either by creating a new
18  * buffer or by pushing down buffers received from its parent, references to
19  * that buffer are created for all children of the node. The buffer tree code
20  * tries hard to avoid to copy buffer contents, but is forced to do so in case
21  * there are alignment constraints.
22  *
23  * Communication between nodes is possible via the btr_exec_up() function.
24  * For example, in para_audiod the alsa writer asks all parent nodes
25  * for for the number of channels and the sample rate of the current
26  * audio file.
27  *
28  * Buffer pools - An alternative to malloc/free buffer management.
29  *
30  * Non-leaf nodes usually create output to be processed by their children.  The
31  * data must be fed through the output channel(s) of the node in order to make
32  * that data available to each child.
33  *
34  * The easiest way to do so is to malloc() a buffer, fill it, and then call
35  * btr_add_output(). This adds references to that buffer to all children. The
36  * buffer is automatically freed if no buffer tree node is using it any more.
37  *
38  * This approach, while being simple, has some drawbacks, especially affecting
39  * the root nodes of the buffer tree. Often the data source which is
40  * represented by a root node does not know in advance how much data will be
41  * available.  Therefore the allocated buffer is either larger than what can
42  * currently be read, or is 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)(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(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(struct btr_pool *btrp, char **result);
171 void btr_add_output_pool(struct btr_pool *btrp, size_t size,
172         struct btr_node *btrn);
173 size_t btr_pool_unused(struct btr_pool *btrp);
174 void btr_copy(const void *src, size_t n, struct btr_pool *btrp,
175         struct btr_node *btrn);
176
177 struct btr_node *btr_new_node(struct btr_node_description *bnd);
178 void btr_remove_node(struct btr_node *btrn);
179 void btr_free_node(struct btr_node *btrn);
180 void btr_add_output(char *buf, size_t size, struct btr_node *btrn);
181 size_t btr_get_input_queue_size(struct btr_node *btrn);
182 bool btr_no_parent(struct btr_node *btrn);
183 size_t btr_next_buffer(struct btr_node *btrn, char **bufp);
184 void btr_consume(struct btr_node *btrn, size_t numbytes);
185 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result);
186 void btr_splice_out_node(struct btr_node *btrn);
187 void btr_pushdown(struct btr_node *btrn);
188 void *btr_context(struct btr_node *btrn);
189 void btr_merge(struct btr_node *btrn, size_t dest_size);
190 bool btr_eof(struct btr_node *btrn);
191 void btr_log_tree(struct btr_node *btrn, int loglevel);
192 int btr_pushdown_one(struct btr_node *btrn);
193 bool btr_inplace_ok(struct btr_node *btrn);
194 int btr_node_status(struct btr_node *btrn, size_t min_iqs,
195                 enum btr_node_type type);
196 void btr_get_node_start(struct btr_node *btrn, struct timeval *tv);
197 struct btr_node *btr_search_node(const char *name, struct btr_node *root);