~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to test/run_filter.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 "audio_parser.h"
 
23
#include "audio_format.h"
 
24
#include "filter_plugin.h"
 
25
#include "pcm_volume.h"
 
26
#include "idle.h"
 
27
#include "mixer_control.h"
 
28
#include "playlist.h"
 
29
#include "stdbin.h"
 
30
 
 
31
#include <glib.h>
 
32
 
 
33
#include <assert.h>
 
34
#include <string.h>
 
35
#include <errno.h>
 
36
#include <unistd.h>
 
37
 
 
38
struct playlist g_playlist;
 
39
 
 
40
void
 
41
idle_add(G_GNUC_UNUSED unsigned flags)
 
42
{
 
43
}
 
44
 
 
45
bool
 
46
mixer_set_volume(G_GNUC_UNUSED struct mixer *mixer,
 
47
                 G_GNUC_UNUSED unsigned volume, G_GNUC_UNUSED GError **error_r)
 
48
{
 
49
        return true;
 
50
}
 
51
 
 
52
static void
 
53
my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
 
54
            const gchar *message, G_GNUC_UNUSED gpointer user_data)
 
55
{
 
56
        if (log_domain != NULL)
 
57
                g_printerr("%s: %s\n", log_domain, message);
 
58
        else
 
59
                g_printerr("%s\n", message);
 
60
}
 
61
 
 
62
static const struct config_param *
 
63
find_named_config_block(const char *block, const char *name)
 
64
{
 
65
        const struct config_param *param = NULL;
 
66
 
 
67
        while ((param = config_get_next_param(block, param)) != NULL) {
 
68
                const char *current_name =
 
69
                        config_get_block_string(param, "name", NULL);
 
70
                if (current_name != NULL && strcmp(current_name, name) == 0)
 
71
                        return param;
 
72
        }
 
73
 
 
74
        return NULL;
 
75
}
 
76
 
 
77
static struct filter *
 
78
load_filter(const char *name)
 
79
{
 
80
        const struct config_param *param;
 
81
        struct filter *filter;
 
82
        GError *error = NULL;
 
83
 
 
84
        param = find_named_config_block("filter", name);
 
85
        if (param == NULL) {
 
86
                g_printerr("No such configured filter: %s\n", name);
 
87
                return false;
 
88
        }
 
89
 
 
90
        filter = filter_configured_new(param, &error);
 
91
        if (filter == NULL) {
 
92
                g_printerr("Failed to load filter: %s\n", error->message);
 
93
                g_error_free(error);
 
94
                return NULL;
 
95
        }
 
96
 
 
97
        return filter;
 
98
}
 
99
 
 
100
int main(int argc, char **argv)
 
101
{
 
102
        struct audio_format audio_format;
 
103
        struct audio_format_string af_string;
 
104
        bool success;
 
105
        GError *error = NULL;
 
106
        struct filter *filter;
 
107
        const struct audio_format *out_audio_format;
 
108
        char buffer[4096];
 
109
        size_t frame_size;
 
110
 
 
111
        if (argc < 3 || argc > 4) {
 
112
                g_printerr("Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
 
113
                return 1;
 
114
        }
 
115
 
 
116
        audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
 
117
 
 
118
        /* initialize GLib */
 
119
 
 
120
        g_thread_init(NULL);
 
121
        g_log_set_default_handler(my_log_func, NULL);
 
122
 
 
123
        /* read configuration file (mpd.conf) */
 
124
 
 
125
        config_global_init();
 
126
        success = config_read_file(argv[1], &error);
 
127
        if (!success) {
 
128
                g_printerr("%s:", error->message);
 
129
                g_error_free(error);
 
130
                return 1;
 
131
        }
 
132
 
 
133
        /* parse the audio format */
 
134
 
 
135
        if (argc > 3) {
 
136
                success = audio_format_parse(&audio_format, argv[3],
 
137
                                             false, &error);
 
138
                if (!success) {
 
139
                        g_printerr("Failed to parse audio format: %s\n",
 
140
                                   error->message);
 
141
                        g_error_free(error);
 
142
                        return 1;
 
143
                }
 
144
        }
 
145
 
 
146
        /* initialize the filter */
 
147
 
 
148
        filter = load_filter(argv[2]);
 
149
        if (filter == NULL)
 
150
                return 1;
 
151
 
 
152
        /* open the filter */
 
153
 
 
154
        out_audio_format = filter_open(filter, &audio_format, &error);
 
155
        if (out_audio_format == NULL) {
 
156
                g_printerr("Failed to open filter: %s\n", error->message);
 
157
                g_error_free(error);
 
158
                filter_free(filter);
 
159
                return 1;
 
160
        }
 
161
 
 
162
        g_printerr("audio_format=%s\n",
 
163
                   audio_format_to_string(out_audio_format, &af_string));
 
164
 
 
165
        frame_size = audio_format_frame_size(&audio_format);
 
166
 
 
167
        /* play */
 
168
 
 
169
        while (true) {
 
170
                ssize_t nbytes;
 
171
                size_t length;
 
172
                const void *dest;
 
173
 
 
174
                nbytes = read(0, buffer, sizeof(buffer));
 
175
                if (nbytes <= 0)
 
176
                        break;
 
177
 
 
178
                dest = filter_filter(filter, buffer, (size_t)nbytes,
 
179
                                     &length, &error);
 
180
                if (dest == NULL) {
 
181
                        g_printerr("Filter failed: %s\n", error->message);
 
182
                        filter_close(filter);
 
183
                        filter_free(filter);
 
184
                        return 1;
 
185
                }
 
186
 
 
187
                nbytes = write(1, dest, length);
 
188
                if (nbytes < 0) {
 
189
                        g_printerr("Failed to write: %s\n", strerror(errno));
 
190
                        filter_close(filter);
 
191
                        filter_free(filter);
 
192
                        return 1;
 
193
                }
 
194
        }
 
195
 
 
196
        /* cleanup and exit */
 
197
 
 
198
        filter_close(filter);
 
199
        filter_free(filter);
 
200
 
 
201
        config_global_finish();
 
202
 
 
203
        return 0;
 
204
}