~ubuntu-dev/ubuntu/lucid/mpd/lucid-201002101854

« back to all changes in this revision

Viewing changes to test/run_encoder.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-10-23 12:38:20 UTC
  • mfrom: (1.1.12 upstream) (2.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091023123820-rxt21lmekscxbkbt
Tags: 0.15.4-1ubuntu1
* Merge from debian unstable, Ubuntu remaining changes:
  - debian/control:
    + Don't build-depends 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.
* Also fixes LP: #332332.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2009 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 "encoder_list.h"
 
21
#include "encoder_plugin.h"
 
22
#include "audio_format.h"
 
23
#include "audio_parser.h"
 
24
#include "conf.h"
 
25
 
 
26
#include <glib.h>
 
27
 
 
28
#include <stddef.h>
 
29
#include <unistd.h>
 
30
 
 
31
static void
 
32
encoder_to_stdout(struct encoder *encoder)
 
33
{
 
34
        size_t length;
 
35
        static char buffer[32768];
 
36
 
 
37
        while ((length = encoder_read(encoder, buffer, sizeof(buffer))) > 0)
 
38
                write(1, buffer, length);
 
39
}
 
40
 
 
41
int main(int argc, char **argv)
 
42
{
 
43
        GError *error = NULL;
 
44
        struct audio_format audio_format = {
 
45
                .sample_rate = 44100,
 
46
                .bits = 16,
 
47
                .channels = 2,
 
48
        };
 
49
        bool ret;
 
50
        const char *encoder_name;
 
51
        const struct encoder_plugin *plugin;
 
52
        struct encoder *encoder;
 
53
        struct config_param *param;
 
54
        static char buffer[32768];
 
55
        ssize_t nbytes;
 
56
 
 
57
        /* parse command line */
 
58
 
 
59
        if (argc > 3) {
 
60
                g_printerr("Usage: run_encoder [ENCODER] [FORMAT] <IN >OUT\n");
 
61
                return 1;
 
62
        }
 
63
 
 
64
        if (argc > 1)
 
65
                encoder_name = argv[1];
 
66
        else
 
67
                encoder_name = "vorbis";
 
68
 
 
69
        /* create the encoder */
 
70
 
 
71
        plugin = encoder_plugin_get(encoder_name);
 
72
        if (plugin == NULL) {
 
73
                g_printerr("No such encoder: %s\n", encoder_name);
 
74
                return 1;
 
75
        }
 
76
 
 
77
        param = config_new_param(NULL, -1);
 
78
        config_add_block_param(param, "quality", "5.0", -1);
 
79
 
 
80
        encoder = encoder_init(plugin, param, &error);
 
81
        if (encoder == NULL) {
 
82
                g_printerr("Failed to initialize encoder: %s\n",
 
83
                           error->message);
 
84
                g_error_free(error);
 
85
                return 1;
 
86
        }
 
87
 
 
88
        /* open the encoder */
 
89
 
 
90
        if (argc > 2) {
 
91
                ret = audio_format_parse(&audio_format, argv[2], &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;
 
97
                }
 
98
        }
 
99
 
 
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;
 
106
        }
 
107
 
 
108
        /* do it */
 
109
 
 
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;
 
117
                }
 
118
 
 
119
                encoder_to_stdout(encoder);
 
120
        }
 
121
 
 
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;
 
128
        }
 
129
 
 
130
        encoder_to_stdout(encoder);
 
131
}