1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "output/Internal.hxx"
#include "output/OutputPlugin.hxx"
#include "config/ConfigData.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
#include "Idle.hxx"
#include "Main.hxx"
#include "event/Loop.hxx"
#include "ScopeIOThread.hxx"
#include "fs/Path.hxx"
#include "AudioParser.hxx"
#include "pcm/PcmConvert.hxx"
#include "filter/FilterRegistry.hxx"
#include "PlayerControl.hxx"
#include "stdbin.h"
#include "util/Error.hxx"
#include "Log.hxx"
#ifdef HAVE_GLIB
#include <glib.h>
#endif
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
const struct filter_plugin *
filter_plugin_by_name(gcc_unused const char *name)
{
assert(false);
return NULL;
}
PlayerControl::PlayerControl(PlayerListener &_listener,
MultipleOutputs &_outputs,
unsigned _buffer_chunks,
unsigned _buffered_before_play)
:listener(_listener), outputs(_outputs),
buffer_chunks(_buffer_chunks),
buffered_before_play(_buffered_before_play) {}
PlayerControl::~PlayerControl() {}
static AudioOutput *
load_audio_output(EventLoop &event_loop, const char *name)
{
const config_param *param =
config_find_block(CONF_AUDIO_OUTPUT, "name", name);
if (param == NULL) {
fprintf(stderr, "No such configured audio output: %s\n", name);
return nullptr;
}
static struct PlayerControl dummy_player_control(*(PlayerListener *)nullptr,
*(MultipleOutputs *)nullptr,
32, 4);
Error error;
AudioOutput *ao =
audio_output_new(event_loop, *param,
*(MixerListener *)nullptr,
dummy_player_control,
error);
if (ao == nullptr)
LogError(error);
return ao;
}
static bool
run_output(AudioOutput *ao, AudioFormat audio_format)
{
/* open the audio output */
Error error;
if (!ao_plugin_enable(ao, error)) {
LogError(error, "Failed to enable audio output");
return false;
}
if (!ao_plugin_open(ao, audio_format, error)) {
ao_plugin_disable(ao);
LogError(error, "Failed to open audio output");
return false;
}
struct audio_format_string af_string;
fprintf(stderr, "audio_format=%s\n",
audio_format_to_string(audio_format, &af_string));
size_t frame_size = audio_format.GetFrameSize();
/* play */
size_t length = 0;
char buffer[4096];
while (true) {
if (length < sizeof(buffer)) {
ssize_t nbytes = read(0, buffer + length,
sizeof(buffer) - length);
if (nbytes <= 0)
break;
length += (size_t)nbytes;
}
size_t play_length = (length / frame_size) * frame_size;
if (play_length > 0) {
size_t consumed = ao_plugin_play(ao,
buffer, play_length,
error);
if (consumed == 0) {
ao_plugin_close(ao);
ao_plugin_disable(ao);
LogError(error, "Failed to play");
return false;
}
assert(consumed <= length);
assert(consumed % frame_size == 0);
length -= consumed;
memmove(buffer, buffer + consumed, length);
}
}
ao_plugin_close(ao);
ao_plugin_disable(ao);
return true;
}
int main(int argc, char **argv)
{
Error error;
if (argc < 3 || argc > 4) {
fprintf(stderr, "Usage: run_output CONFIG NAME [FORMAT] <IN\n");
return EXIT_FAILURE;
}
const Path config_path = Path::FromFS(argv[1]);
AudioFormat audio_format(44100, SampleFormat::S16, 2);
#ifdef HAVE_GLIB
#if !GLIB_CHECK_VERSION(2,32,0)
g_thread_init(NULL);
#endif
#endif
/* read configuration file (mpd.conf) */
config_global_init();
if (!ReadConfigFile(config_path, error)) {
LogError(error);
return EXIT_FAILURE;
}
EventLoop event_loop;
const ScopeIOThread io_thread;
/* initialize the audio output */
AudioOutput *ao = load_audio_output(event_loop, argv[2]);
if (ao == NULL)
return 1;
/* parse the audio format */
if (argc > 3) {
if (!audio_format_parse(audio_format, argv[3], false, error)) {
LogError(error, "Failed to parse audio format");
return EXIT_FAILURE;
}
}
/* do it */
bool success = run_output(ao, audio_format);
/* cleanup and exit */
audio_output_free(ao);
config_global_finish();
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
|