2009-03-10 15:46:55 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-10 15:46:55 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-10 15:46:55 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2011-08-28 17:29:09 +02:00
|
|
|
#include "io_thread.h"
|
2009-03-10 15:46:55 +01:00
|
|
|
#include "output_plugin.h"
|
|
|
|
#include "output_internal.h"
|
|
|
|
#include "output_control.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "audio_parser.h"
|
2009-07-06 10:01:47 +02:00
|
|
|
#include "filter_registry.h"
|
|
|
|
#include "pcm_convert.h"
|
2009-10-21 10:30:42 +02:00
|
|
|
#include "event_pipe.h"
|
2010-02-14 17:04:39 +01:00
|
|
|
#include "idle.h"
|
2010-04-25 13:42:23 +02:00
|
|
|
#include "playlist.h"
|
2009-11-03 21:08:48 +01:00
|
|
|
#include "player_control.h"
|
2010-05-20 07:17:20 +02:00
|
|
|
#include "stdbin.h"
|
2009-03-10 15:46:55 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-08-28 17:29:09 +02:00
|
|
|
#include <stdlib.h>
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2010-04-25 13:42:23 +02:00
|
|
|
struct playlist g_playlist;
|
|
|
|
|
2010-02-14 17:04:39 +01:00
|
|
|
void
|
|
|
|
idle_add(G_GNUC_UNUSED unsigned flags)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-10-21 10:30:42 +02:00
|
|
|
void
|
|
|
|
event_pipe_emit(G_GNUC_UNUSED enum pipe_event event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
void pcm_convert_init(G_GNUC_UNUSED struct pcm_convert_state *state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-06 10:01:47 +02:00
|
|
|
void pcm_convert_deinit(G_GNUC_UNUSED struct pcm_convert_state *state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *
|
|
|
|
pcm_convert(G_GNUC_UNUSED struct pcm_convert_state *state,
|
|
|
|
G_GNUC_UNUSED const struct audio_format *src_format,
|
|
|
|
G_GNUC_UNUSED const void *src, G_GNUC_UNUSED size_t src_size,
|
|
|
|
G_GNUC_UNUSED const struct audio_format *dest_format,
|
2009-07-23 12:01:03 +02:00
|
|
|
G_GNUC_UNUSED size_t *dest_size_r,
|
|
|
|
GError **error_r)
|
2009-07-06 10:01:47 +02:00
|
|
|
{
|
2009-07-23 12:01:03 +02:00
|
|
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
|
|
|
"Not implemented");
|
2009-07-06 10:01:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct filter_plugin *
|
|
|
|
filter_plugin_by_name(G_GNUC_UNUSED const char *name)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
static const struct config_param *
|
|
|
|
find_named_config_block(const char *block, const char *name)
|
|
|
|
{
|
|
|
|
const struct config_param *param = NULL;
|
|
|
|
|
|
|
|
while ((param = config_get_next_param(block, param)) != NULL) {
|
|
|
|
const char *current_name =
|
|
|
|
config_get_block_string(param, "name", NULL);
|
|
|
|
if (current_name != NULL && strcmp(current_name, name) == 0)
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static struct audio_output *
|
|
|
|
load_audio_output(const char *name)
|
2009-03-10 15:46:55 +01:00
|
|
|
{
|
|
|
|
const struct config_param *param;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
param = find_named_config_block(CONF_AUDIO_OUTPUT, name);
|
|
|
|
if (param == NULL) {
|
|
|
|
g_printerr("No such configured audio output: %s\n", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
static struct player_control dummy_player_control;
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output *ao =
|
|
|
|
audio_output_new(param, &dummy_player_control, &error);
|
|
|
|
if (ao == NULL) {
|
2009-03-10 15:46:55 +01:00
|
|
|
g_printerr("%s\n", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return ao;
|
2009-03-10 15:46:55 +01:00
|
|
|
}
|
|
|
|
|
2011-08-30 22:17:46 +02:00
|
|
|
static bool
|
|
|
|
run_output(struct audio_output *ao, struct audio_format *audio_format)
|
|
|
|
{
|
|
|
|
/* open the audio output */
|
|
|
|
|
|
|
|
GError *error = NULL;
|
2011-12-24 16:42:23 +01:00
|
|
|
if (!ao_plugin_enable(ao, &error)) {
|
|
|
|
g_printerr("Failed to enable audio output: %s\n",
|
|
|
|
error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
if (!ao_plugin_open(ao, audio_format, &error)) {
|
2011-12-24 16:42:23 +01:00
|
|
|
ao_plugin_disable(ao);
|
2011-08-30 22:17:46 +02:00
|
|
|
g_printerr("Failed to open audio output: %s\n",
|
|
|
|
error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct audio_format_string af_string;
|
|
|
|
g_printerr("audio_format=%s\n",
|
|
|
|
audio_format_to_string(audio_format, &af_string));
|
|
|
|
|
|
|
|
size_t frame_size = audio_format_frame_size(audio_format);
|
|
|
|
|
|
|
|
/* 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) {
|
2011-09-16 23:31:48 +02:00
|
|
|
size_t consumed = ao_plugin_play(ao,
|
2011-08-30 22:17:46 +02:00
|
|
|
buffer, play_length,
|
|
|
|
&error);
|
|
|
|
if (consumed == 0) {
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_plugin_close(ao);
|
2011-12-24 16:42:23 +01:00
|
|
|
ao_plugin_disable(ao);
|
2011-08-30 22:17:46 +02:00
|
|
|
g_printerr("Failed to play: %s\n",
|
|
|
|
error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(consumed <= length);
|
|
|
|
assert(consumed % frame_size == 0);
|
|
|
|
|
|
|
|
length -= consumed;
|
|
|
|
memmove(buffer, buffer + consumed, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_plugin_close(ao);
|
2011-12-24 16:42:23 +01:00
|
|
|
ao_plugin_disable(ao);
|
2011-08-30 22:17:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2009-07-19 17:24:43 +02:00
|
|
|
struct audio_format audio_format;
|
2009-03-10 15:46:55 +01:00
|
|
|
bool success;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (argc < 3 || argc > 4) {
|
|
|
|
g_printerr("Usage: run_output CONFIG NAME [FORMAT] <IN\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
|
2009-07-19 17:24:43 +02:00
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
g_thread_init(NULL);
|
|
|
|
|
|
|
|
/* read configuration file (mpd.conf) */
|
|
|
|
|
|
|
|
config_global_init();
|
2009-09-24 21:40:07 +02:00
|
|
|
success = config_read_file(argv[1], &error);
|
|
|
|
if (!success) {
|
|
|
|
g_printerr("%s:", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2011-08-28 17:29:09 +02:00
|
|
|
io_thread_init();
|
|
|
|
if (!io_thread_start(&error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
/* initialize the audio output */
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output *ao = load_audio_output(argv[2]);
|
|
|
|
if (ao == NULL)
|
2009-03-10 15:46:55 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* parse the audio format */
|
|
|
|
|
|
|
|
if (argc > 3) {
|
2009-10-21 23:01:04 +02:00
|
|
|
success = audio_format_parse(&audio_format, argv[3],
|
|
|
|
false, &error);
|
2009-03-10 15:46:55 +01:00
|
|
|
if (!success) {
|
|
|
|
g_printerr("Failed to parse audio format: %s\n",
|
|
|
|
error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-30 22:17:46 +02:00
|
|
|
/* do it */
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
success = run_output(ao, &audio_format);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
|
|
|
/* cleanup and exit */
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
audio_output_free(ao);
|
2009-03-10 15:46:55 +01:00
|
|
|
|
2011-08-28 17:29:09 +02:00
|
|
|
io_thread_deinit();
|
|
|
|
|
2009-03-10 15:46:55 +01:00
|
|
|
config_global_finish();
|
|
|
|
|
2011-08-30 22:17:46 +02:00
|
|
|
return success ? EXIT_SUCCESS : EXIT_FAILURE;
|
2009-03-10 15:46:55 +01:00
|
|
|
}
|