mp3_afh.c: Fix off by one bug
[paraslash.git] / write.h
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file write.h writer-related structures */
20
21 /** the list of supported writers */
22 enum writer_enum {WRITER_ENUM};
23
24 /**
25  * decbribes one running instance of a writer
26  */
27 struct writer_node {
28         /** points to the writer structure associated with this node */
29         struct writer *writer; /* FIXME: Should better be only the number */
30         /** writer-specific data */
31         void *private_data;
32         /** send that many bytes in one go */
33         int chunk_bytes;
34         /** pointer to the group this node belongs to */
35         struct writer_node_group *wng;
36         /** the writer-specific configuration of this node */
37         void *conf;
38         /** how much of the wng's buffer is already written */
39         size_t written;
40 };
41
42 /** describes one supported writer */
43 struct writer {
44         /**
45          * the init function of the writer
46          *
47          * It must fill in all other function pointers of the given
48          * writer structure.
49          *
50          */
51         void (*init)(struct writer *w);
52         /**
53          *
54          *
55          * the command line parser of the writer
56          *
57          * It should check whether the command line options given by \a options are
58          * valid.  On success, it should return a pointer to the writer-specific
59          * configuration data determined by \a options.  Note that this might be called
60          * more than once with different values of \a options.
61          *
62          */
63                 void * (*parse_config)(const char *options);
64         /**
65          *
66          * open one instance of this writer
67          *
68          * This function should perform any work necessary to write the incoming
69          * stream. If To this aim, it may allocate its private data structure and store
70          * a pointer to that structure via the given writer_node paramenter.
71          */
72         int (*open)(struct writer_node *);
73         /**
74          *
75          * write a chunk of audio data
76          *
77          * This is called from the writer node group task's pre_select(). It
78          * may use the sched pointer to add any file descriptors or to decrease
79          * the select timeout. It must return positive on success and negative
80          * on errors.
81          */
82         int (*pre_select)(struct sched *s, struct writer_node *wn);
83         /**
84          * Called from the post_select function of the wng task. It must keep
85          * track of the the number of bytes consumed from the wng's buffer via
86          * the wn->written variable (which may be modified by the wng handling
87          * functions). This function must return positive on success and
88          * negative on errors.
89          */
90         int (*post_select)(struct sched *s, struct writer_node *wn);
91         /**
92          * close one instance of the writer
93          *
94          * This function is assumed to succeed.
95          */
96         void (*close)(struct writer_node *);
97         /**
98          * shutdown the writer
99          *
100          * This is a optional function pointer used for cleaning
101          * up.
102          */
103         void (*shutdown)(struct writer_node *);
104 };
105
106 /**
107  * describes a set of writer nodes that all write the same stream.
108  */
109 struct writer_node_group {
110         /** number of nodes belonging to this group */
111         unsigned num_writers;
112         /** array of pointers to the corresponding writer nodes */
113         struct writer_node *writer_nodes;
114         /** the maximum of the chunk_bytes values of the writer nodes in this group */
115         size_t max_chunk_bytes;
116         /** non-zero if end of file was encountered by the feeding task */
117         int *input_eof;
118         /** non-zero if end of file was encountered */
119         int eof;
120         /** current output buffer */
121         char *buf;
122         /** number of bytes loaded in the output buffer */
123         size_t *loaded;
124         /** number of audio channels of the current stream */
125         unsigned int *channels;
126         /** sample rate of the current stream */
127         unsigned int *samplerate;
128         /** the task associated to this group */
129         struct task task;
130 };
131
132 /** loop over each writer node in a writer group */
133 #define FOR_EACH_WRITER_NODE(i, wng) for (i = 0; i < (wng)->num_writers; i++)
134 /** loop over each supported writer */
135 #define FOR_EACH_WRITER(i) for (i = 0; i < NUM_SUPPORTED_WRITERS; i++)
136
137 /** declare the init functions of all supported writers */
138 DECLARE_WRITER_INITS;
139
140 /** array containing the name of each writer */
141 extern const char *writer_names[];
142
143 /** the writer structure for each supported writer */
144 extern struct writer writers[NUM_SUPPORTED_WRITERS];