null_output_plugin.c 2.72 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "null_output_plugin.h"
22 23
#include "output_api.h"
#include "timer.h"
24

25 26
#include <glib.h>

27 28
#include <assert.h>

29
struct null_data {
30 31
	struct audio_output base;

32 33
	bool sync;

34
	struct timer *timer;
35 36
};

37 38
static struct audio_output *
null_init(const struct config_param *param, GError **error_r)
39
{
40
	struct null_data *nd = g_new(struct null_data, 1);
Max Kellermann's avatar
Max Kellermann committed
41

42 43 44 45 46
	if (!ao_base_init(&nd->base, &null_output_plugin, param, error_r)) {
		g_free(nd);
		return NULL;
	}

47
	nd->sync = config_get_block_bool(param, "sync", true);
Max Kellermann's avatar
Max Kellermann committed
48

49
	return &nd->base;
50 51
}

52
static void
53
null_finish(struct audio_output *ao)
54
{
55
	struct null_data *nd = (struct null_data *)ao;
56

57
	ao_base_finish(&nd->base);
58 59 60
	g_free(nd);
}

61
static bool
62
null_open(struct audio_output *ao, struct audio_format *audio_format,
63
	  G_GNUC_UNUSED GError **error)
64
{
65
	struct null_data *nd = (struct null_data *)ao;
66

67 68
	if (nd->sync)
		nd->timer = timer_new(audio_format);
Max Kellermann's avatar
Max Kellermann committed
69

70
	return true;
71 72
}

Max Kellermann's avatar
Max Kellermann committed
73
static void
74
null_close(struct audio_output *ao)
75
{
76
	struct null_data *nd = (struct null_data *)ao;
77

78
	if (nd->sync)
79
		timer_free(nd->timer);
80 81
}

82 83 84 85 86 87 88 89 90 91
static unsigned
null_delay(struct audio_output *ao)
{
	struct null_data *nd = (struct null_data *)ao;

	return nd->sync && nd->timer->started
		? timer_delay(nd->timer)
		: 0;
}

92
static size_t
93
null_play(struct audio_output *ao, G_GNUC_UNUSED const void *chunk, size_t size,
94
	  G_GNUC_UNUSED GError **error)
95
{
96
	struct null_data *nd = (struct null_data *)ao;
97
	struct timer *timer = nd->timer;
98

99
	if (!nd->sync)
100
		return size;
101

102 103 104
	if (!timer->started)
		timer_start(timer);
	timer_add(timer, size);
105

106
	return size;
107 108
}

Max Kellermann's avatar
Max Kellermann committed
109
static void
110
null_cancel(struct audio_output *ao)
111
{
112
	struct null_data *nd = (struct null_data *)ao;
113

114 115 116
	if (!nd->sync)
		return;

117
	timer_reset(nd->timer);
118 119
}

Max Kellermann's avatar
Max Kellermann committed
120
const struct audio_output_plugin null_output_plugin = {
121
	.name = "null",
Max Kellermann's avatar
Max Kellermann committed
122
	.init = null_init,
123
	.finish = null_finish,
Max Kellermann's avatar
Max Kellermann committed
124 125
	.open = null_open,
	.close = null_close,
126
	.delay = null_delay,
Max Kellermann's avatar
Max Kellermann committed
127 128
	.play = null_play,
	.cancel = null_cancel,
129
};