configure.ac: Move inotify to "Miscellaneous Libraries".
[mpd-mk.git] / test / run_encoder.c
blobb8820572ae537929419b7d56d75273e51dd7dd1b
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
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.
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.
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.
20 #include "config.h"
21 #include "encoder_list.h"
22 #include "encoder_plugin.h"
23 #include "audio_format.h"
24 #include "audio_parser.h"
25 #include "conf.h"
27 #include <glib.h>
29 #include <stddef.h>
30 #include <unistd.h>
32 static void
33 encoder_to_stdout(struct encoder *encoder)
35 size_t length;
36 static char buffer[32768];
38 while ((length = encoder_read(encoder, buffer, sizeof(buffer))) > 0)
39 write(1, buffer, length);
42 int main(int argc, char **argv)
44 GError *error = NULL;
45 struct audio_format audio_format;
46 bool ret;
47 const char *encoder_name;
48 const struct encoder_plugin *plugin;
49 struct encoder *encoder;
50 struct config_param *param;
51 static char buffer[32768];
52 ssize_t nbytes;
54 /* parse command line */
56 if (argc > 3) {
57 g_printerr("Usage: run_encoder [ENCODER] [FORMAT] <IN >OUT\n");
58 return 1;
61 if (argc > 1)
62 encoder_name = argv[1];
63 else
64 encoder_name = "vorbis";
66 audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
68 /* create the encoder */
70 plugin = encoder_plugin_get(encoder_name);
71 if (plugin == NULL) {
72 g_printerr("No such encoder: %s\n", encoder_name);
73 return 1;
76 param = config_new_param(NULL, -1);
77 config_add_block_param(param, "quality", "5.0", -1, NULL);
79 encoder = encoder_init(plugin, param, &error);
80 if (encoder == NULL) {
81 g_printerr("Failed to initialize encoder: %s\n",
82 error->message);
83 g_error_free(error);
84 return 1;
87 /* open the encoder */
89 if (argc > 2) {
90 ret = audio_format_parse(&audio_format, argv[2],
91 false, &error);
92 if (!ret) {
93 g_printerr("Failed to parse audio format: %s\n",
94 error->message);
95 g_error_free(error);
96 return 1;
100 ret = encoder_open(encoder, &audio_format, &error);
101 if (encoder == NULL) {
102 g_printerr("Failed to open encoder: %s\n",
103 error->message);
104 g_error_free(error);
105 return 1;
108 /* do it */
110 while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) {
111 ret = encoder_write(encoder, buffer, nbytes, &error);
112 if (!ret) {
113 g_printerr("encoder_write() failed: %s\n",
114 error->message);
115 g_error_free(error);
116 return 1;
119 encoder_to_stdout(encoder);
122 ret = encoder_flush(encoder, &error);
123 if (!ret) {
124 g_printerr("encoder_flush() failed: %s\n",
125 error->message);
126 g_error_free(error);
127 return 1;
130 encoder_to_stdout(encoder);