output_thread.c 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* the Music Player Daemon (MPD)
 * Copyright (C) 2008 Max Kellermann <max@duempel.org>
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "output_thread.h"
#include "output_api.h"
21
#include "output_internal.h"
22

23
#include <glib.h>
24
#include <assert.h>
25 26
#include <stdlib.h>
#include <errno.h>
27

28 29 30 31 32 33
enum {
	/** after a failure, wait this number of seconds before
	    automatically reopening the device */
	REOPEN_AFTER = 10,
};

34 35 36 37 38 39 40 41 42 43
static void ao_command_finished(struct audio_output *ao)
{
	assert(ao->command != AO_COMMAND_NONE);
	ao->command = AO_COMMAND_NONE;
	notify_signal(&audio_output_client_notify);
}

static void convertAudioFormat(struct audio_output *audioOutput,
			       const char **chunkArgPtr, size_t *sizeArgPtr)
{
Max Kellermann's avatar
Max Kellermann committed
44 45 46
	size_t size = pcm_convert_size(&(audioOutput->inAudioFormat),
				       *sizeArgPtr,
				       &(audioOutput->outAudioFormat));
47 48 49 50

	if (size > audioOutput->convBufferLen) {
		if (audioOutput->convBuffer != NULL)
			free(audioOutput->convBuffer);
51
		audioOutput->convBuffer = g_malloc(size);
52 53 54
		audioOutput->convBufferLen = size;
	}

Max Kellermann's avatar
Max Kellermann committed
55 56 57 58 59
	*sizeArgPtr = pcm_convert(&(audioOutput->inAudioFormat),
				  *chunkArgPtr, *sizeArgPtr,
				  &(audioOutput->outAudioFormat),
				  audioOutput->convBuffer,
				  &audioOutput->convState);
60 61 62 63 64 65 66 67

	*chunkArgPtr = audioOutput->convBuffer;
}

static void ao_play(struct audio_output *ao)
{
	const char *data = ao->args.play.data;
	size_t size = ao->args.play.size;
68
	bool ret;
69

70 71
	assert(size > 0);

72
	if (!audio_format_equals(&ao->inAudioFormat, &ao->outAudioFormat)) {
73 74
		convertAudioFormat(ao, &data, &size);

75 76 77 78 79 80 81 82
		/* under certain circumstances, pcm_convert() may
		   return an empty buffer - this condition should be
		   investigated further, but for now, do this check as
		   a workaround: */
		if (size == 0)
			return;
	}

83 84
	ret = ao->plugin->play(ao->data, data, size);
	if (!ret) {
85
		ao->plugin->cancel(ao->data);
86
		ao->plugin->close(ao->data);
87
		pcm_convert_deinit(&ao->convState);
88 89 90
		ao->open = false;
	}

91 92 93
	ao_command_finished(ao);
}

94 95
static void ao_pause(struct audio_output *ao)
{
96 97
	ao->plugin->cancel(ao->data);

98 99 100 101 102 103 104
	if (ao->plugin->pause != NULL) {
		/* pause is supported */
		ao_command_finished(ao);
		ao->plugin->pause(ao->data);
	} else {
		/* pause is not supported - simply close the device */
		ao->plugin->close(ao->data);
105
		ao->open = false;
106 107 108 109
		ao_command_finished(ao);
	}
}

110
static gpointer audio_output_task(gpointer arg)
111 112
{
	struct audio_output *ao = arg;
113
	bool ret;
114 115 116 117 118 119 120 121

	while (1) {
		switch (ao->command) {
		case AO_COMMAND_NONE:
			break;

		case AO_COMMAND_OPEN:
			assert(!ao->open);
122 123

			pcm_convert_init(&ao->convState);
124 125
			ret = ao->plugin->open(ao->data,
					       &ao->outAudioFormat);
126 127

			assert(!ao->open);
128
			if (ret == true)
129
				ao->open = true;
130 131
			else
				ao->reopen_after = time(NULL) + REOPEN_AFTER;
132

133 134 135 136 137
			ao_command_finished(ao);
			break;

		case AO_COMMAND_CLOSE:
			assert(ao->open);
138
			ao->plugin->cancel(ao->data);
139
			ao->plugin->close(ao->data);
140

141
			ao->open = false;
142 143 144 145 146 147 148
			ao_command_finished(ao);
			break;

		case AO_COMMAND_PLAY:
			ao_play(ao);
			break;

149 150 151 152
		case AO_COMMAND_PAUSE:
			ao_pause(ao);
			break;

153
		case AO_COMMAND_CANCEL:
154
			ao->plugin->cancel(ao->data);
155 156 157 158
			ao_command_finished(ao);
			break;

		case AO_COMMAND_SEND_TAG:
159
			ao->plugin->send_tag(ao->data, ao->args.tag);
160 161 162 163 164 165 166 167 168 169 170 171 172 173
			ao_command_finished(ao);
			break;

		case AO_COMMAND_KILL:
			ao_command_finished(ao);
			return NULL;
		}

		notify_wait(&ao->notify);
	}
}

void audio_output_thread_start(struct audio_output *ao)
{
174
	GError *e = NULL;
175 176 177

	assert(ao->command == AO_COMMAND_NONE);

178
	if (!(ao->thread = g_thread_create(audio_output_task, ao, true, &e)))
179
		g_error("Failed to spawn output task: %s\n", e->message);
180
}