~ubuntu-branches/ubuntu/saucy/mpd/saucy

« back to all changes in this revision

Viewing changes to src/filter/chain_filter_plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "conf.h"
 
22
#include "filter/chain_filter_plugin.h"
 
23
#include "filter_plugin.h"
 
24
#include "filter_internal.h"
 
25
#include "filter_registry.h"
 
26
#include "audio_format.h"
 
27
 
 
28
#include <assert.h>
 
29
 
 
30
struct filter_chain {
 
31
        /** the base class */
 
32
        struct filter base;
 
33
 
 
34
        GSList *children;
 
35
};
 
36
 
 
37
static inline GQuark
 
38
filter_quark(void)
 
39
{
 
40
        return g_quark_from_static_string("filter");
 
41
}
 
42
 
 
43
static struct filter *
 
44
chain_filter_init(G_GNUC_UNUSED const struct config_param *param,
 
45
                  G_GNUC_UNUSED GError **error_r)
 
46
{
 
47
        struct filter_chain *chain = g_new(struct filter_chain, 1);
 
48
 
 
49
        filter_init(&chain->base, &chain_filter_plugin);
 
50
        chain->children = NULL;
 
51
 
 
52
        return &chain->base;
 
53
}
 
54
 
 
55
static void
 
56
chain_free_child(gpointer data, G_GNUC_UNUSED gpointer user_data)
 
57
{
 
58
        struct filter *filter = data;
 
59
 
 
60
        filter_free(filter);
 
61
}
 
62
 
 
63
static void
 
64
chain_filter_finish(struct filter *_filter)
 
65
{
 
66
        struct filter_chain *chain = (struct filter_chain *)_filter;
 
67
 
 
68
        g_slist_foreach(chain->children, chain_free_child, NULL);
 
69
        g_slist_free(chain->children);
 
70
 
 
71
        g_free(chain);
 
72
}
 
73
 
 
74
/**
 
75
 * Close all filters in the chain until #until is reached.  #until
 
76
 * itself is not closed.
 
77
 */
 
78
static void
 
79
chain_close_until(struct filter_chain *chain, const struct filter *until)
 
80
{
 
81
        GSList *i = chain->children;
 
82
        struct filter *filter;
 
83
 
 
84
        while (true) {
 
85
                /* this assertion fails if #until does not exist
 
86
                   (anymore) */
 
87
                assert(i != NULL);
 
88
 
 
89
                if (i->data == until)
 
90
                        /* don't close this filter */
 
91
                        break;
 
92
 
 
93
                /* close this filter */
 
94
                filter = i->data;
 
95
                filter_close(filter);
 
96
 
 
97
                i = g_slist_next(i);
 
98
        }
 
99
}
 
100
 
 
101
static const struct audio_format *
 
102
chain_open_child(struct filter *filter,
 
103
                 const struct audio_format *prev_audio_format,
 
104
                 GError **error_r)
 
105
{
 
106
        struct audio_format conv_audio_format = *prev_audio_format;
 
107
        const struct audio_format *next_audio_format;
 
108
 
 
109
        next_audio_format = filter_open(filter, &conv_audio_format, error_r);
 
110
        if (next_audio_format == NULL)
 
111
                return NULL;
 
112
 
 
113
        if (!audio_format_equals(&conv_audio_format, prev_audio_format)) {
 
114
                struct audio_format_string s;
 
115
 
 
116
                filter_close(filter);
 
117
                g_set_error(error_r, filter_quark(), 0,
 
118
                            "Audio format not supported by filter '%s': %s",
 
119
                            filter->plugin->name,
 
120
                            audio_format_to_string(prev_audio_format, &s));
 
121
                return NULL;
 
122
        }
 
123
 
 
124
        return next_audio_format;
 
125
}
 
126
 
 
127
static const struct audio_format *
 
128
chain_filter_open(struct filter *_filter, struct audio_format *in_audio_format,
 
129
                  GError **error_r)
 
130
{
 
131
        struct filter_chain *chain = (struct filter_chain *)_filter;
 
132
        const struct audio_format *audio_format = in_audio_format;
 
133
 
 
134
        for (GSList *i = chain->children; i != NULL; i = g_slist_next(i)) {
 
135
                struct filter *filter = i->data;
 
136
 
 
137
                audio_format = chain_open_child(filter, audio_format, error_r);
 
138
                if (audio_format == NULL) {
 
139
                        /* rollback, close all children */
 
140
                        chain_close_until(chain, filter);
 
141
                        return NULL;
 
142
                }
 
143
        }
 
144
 
 
145
        /* return the output format of the last filter */
 
146
        return audio_format;
 
147
}
 
148
 
 
149
static void
 
150
chain_close_child(gpointer data, G_GNUC_UNUSED gpointer user_data)
 
151
{
 
152
        struct filter *filter = data;
 
153
 
 
154
        filter_close(filter);
 
155
}
 
156
 
 
157
static void
 
158
chain_filter_close(struct filter *_filter)
 
159
{
 
160
        struct filter_chain *chain = (struct filter_chain *)_filter;
 
161
 
 
162
        g_slist_foreach(chain->children, chain_close_child, NULL);
 
163
}
 
164
 
 
165
static const void *
 
166
chain_filter_filter(struct filter *_filter,
 
167
                    const void *src, size_t src_size,
 
168
                    size_t *dest_size_r, GError **error_r)
 
169
{
 
170
        struct filter_chain *chain = (struct filter_chain *)_filter;
 
171
 
 
172
        for (GSList *i = chain->children; i != NULL; i = g_slist_next(i)) {
 
173
                struct filter *filter = i->data;
 
174
 
 
175
                /* feed the output of the previous filter as input
 
176
                   into the current one */
 
177
                src = filter_filter(filter, src, src_size, &src_size, error_r);
 
178
                if (src == NULL)
 
179
                        return NULL;
 
180
        }
 
181
 
 
182
        /* return the output of the last filter */
 
183
        *dest_size_r = src_size;
 
184
        return src;
 
185
}
 
186
 
 
187
const struct filter_plugin chain_filter_plugin = {
 
188
        .name = "chain",
 
189
        .init = chain_filter_init,
 
190
        .finish = chain_filter_finish,
 
191
        .open = chain_filter_open,
 
192
        .close = chain_filter_close,
 
193
        .filter = chain_filter_filter,
 
194
};
 
195
 
 
196
struct filter *
 
197
filter_chain_new(void)
 
198
{
 
199
        struct filter *filter = filter_new(&chain_filter_plugin, NULL, NULL);
 
200
        /* chain_filter_init() never fails */
 
201
        assert(filter != NULL);
 
202
 
 
203
        return filter;
 
204
}
 
205
 
 
206
void
 
207
filter_chain_append(struct filter *_chain, struct filter *filter)
 
208
{
 
209
        struct filter_chain *chain = (struct filter_chain *)_chain;
 
210
 
 
211
        chain->children = g_slist_append(chain->children, filter);
 
212
}
 
213