output_thread.c 8.53 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * 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 21
 */

#include "output_thread.h"
#include "output_api.h"
22
#include "output_internal.h"
23 24
#include "chunk.h"
#include "pipe.h"
25
#include "player_control.h"
26 27
#include "filter_plugin.h"
#include "filter/convert_filter_plugin.h"
28

29
#include <glib.h>
30

31
#include <assert.h>
32 33
#include <stdlib.h>
#include <errno.h>
34

35 36 37
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "output"

38 39 40 41 42 43 44
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);
}

45 46 47 48 49
static void
ao_open(struct audio_output *ao)
{
	bool success;
	GError *error = NULL;
50
	const struct audio_format *filter_audio_format;
51 52 53 54 55 56

	assert(!ao->open);
	assert(ao->fail_timer == NULL);
	assert(ao->pipe != NULL);
	assert(ao->chunk == NULL);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	/* open the filter */

	filter_audio_format = filter_open(ao->filter, &ao->in_audio_format,
					  &error);
	if (filter_audio_format == NULL) {
		g_warning("Failed to open filter for \"%s\" [%s]: %s",
			  ao->name, ao->plugin->name, error->message);
		g_error_free(error);

		ao->fail_timer = g_timer_new();
		return;
	}

	if (!ao->config_audio_format)
		ao->out_audio_format = *filter_audio_format;

73 74 75 76 77 78 79 80 81 82 83
	success = ao_plugin_open(ao->plugin, ao->data,
				 &ao->out_audio_format,
				 &error);

	assert(!ao->open);

	if (!success) {
		g_warning("Failed to open \"%s\" [%s]: %s",
			  ao->name, ao->plugin->name, error->message);
		g_error_free(error);

84
		filter_close(ao->filter);
85 86 87 88
		ao->fail_timer = g_timer_new();
		return;
	}

89
	convert_filter_set(ao->convert_filter, &ao->out_audio_format);
90 91 92 93 94 95

	g_mutex_lock(ao->mutex);
	ao->open = true;
	g_mutex_unlock(ao->mutex);

	g_debug("opened plugin=%s name=\"%s\" "
96
		"audio_format=%u:%u:%u:%u",
97 98 99
		ao->plugin->name, ao->name,
		ao->out_audio_format.sample_rate,
		ao->out_audio_format.bits,
100 101
		ao->out_audio_format.channels,
		ao->out_audio_format.reverse_endian);
102 103 104

	if (!audio_format_equals(&ao->in_audio_format,
				 &ao->out_audio_format))
105
		g_debug("converting from %u:%u:%u:%u",
106 107
			ao->in_audio_format.sample_rate,
			ao->in_audio_format.bits,
108 109
			ao->in_audio_format.channels,
			ao->in_audio_format.reverse_endian);
110 111
}

112 113 114 115 116
static void
ao_close(struct audio_output *ao)
{
	assert(ao->open);

117 118 119 120
	ao->pipe = NULL;

	g_mutex_lock(ao->mutex);
	ao->chunk = NULL;
121
	ao->open = false;
122 123
	g_mutex_unlock(ao->mutex);

124
	ao_plugin_close(ao->plugin, ao->data);
125
	filter_close(ao->filter);
126 127

	g_debug("closed plugin=%s name=\"%s\"", ao->plugin->name, ao->name);
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
static void
ao_reopen_filter(struct audio_output *ao)
{
	const struct audio_format *filter_audio_format;
	GError *error = NULL;

	filter_close(ao->filter);
	filter_audio_format = filter_open(ao->filter, &ao->in_audio_format,
					  &error);
	if (filter_audio_format == NULL) {
		g_warning("Failed to open filter for \"%s\" [%s]: %s",
			  ao->name, ao->plugin->name, error->message);
		g_error_free(error);

		/* this is a little code duplication fro ao_close(),
		   but we cannot call this function because we must
		   not call filter_close(ao->filter) again */

		ao->pipe = NULL;

		g_mutex_lock(ao->mutex);
		ao->chunk = NULL;
		ao->open = false;
		g_mutex_unlock(ao->mutex);

		ao_plugin_close(ao->plugin, ao->data);

		ao->fail_timer = g_timer_new();
		return;
	}

	convert_filter_set(ao->convert_filter, &ao->out_audio_format);
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static void
ao_reopen(struct audio_output *ao)
{
	if (!ao->config_audio_format) {
		if (ao->open) {
			const struct music_pipe *mp = ao->pipe;
			ao_close(ao);
			ao->pipe = mp;
		}

		/* no audio format is configured: copy in->out, let
		   the output's open() method determine the effective
		   out_audio_format */
		ao->out_audio_format = ao->in_audio_format;
	}

180 181 182 183 184
	if (ao->open)
		/* the audio format has changed, and all filters have
		   to be reconfigured */
		ao_reopen_filter(ao);
	else
185 186 187
		ao_open(ao);
}

188 189
static bool
ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk)
190
{
191 192
	const char *data = chunk->data;
	size_t size = chunk->length;
193
	GError *error = NULL;
194

195 196
	assert(ao != NULL);
	assert(ao->filter != NULL);
197 198
	assert(!music_chunk_is_empty(chunk));
	assert(music_chunk_check_format(chunk, &ao->in_audio_format));
199
	assert(size % audio_format_frame_size(&ao->in_audio_format) == 0);
200

201 202 203 204 205 206
	if (chunk->tag != NULL)
		ao_plugin_send_tag(ao->plugin, ao->data, chunk->tag);

	if (size == 0)
		return true;

207 208 209 210 211 212 213 214 215 216 217 218 219
	data = filter_filter(ao->filter, data, size, &size, &error);
	if (data == NULL) {
		g_warning("\"%s\" [%s] failed to filter: %s",
			  ao->name, ao->plugin->name, error->message);
		g_error_free(error);

		ao_plugin_cancel(ao->plugin, ao->data);
		ao_close(ao);

		/* don't automatically reopen this device for 10
		   seconds */
		ao->fail_timer = g_timer_new();
		return false;
220 221
	}

222
	while (size > 0 && ao->command == AO_COMMAND_NONE) {
223 224
		size_t nbytes;

225 226
		nbytes = ao_plugin_play(ao->plugin, ao->data, data, size,
					&error);
227 228
		if (nbytes == 0) {
			/* play()==0 means failure */
229 230 231 232
			g_warning("\"%s\" [%s] failed to play: %s",
				  ao->name, ao->plugin->name, error->message);
			g_error_free(error);

233 234
			ao_plugin_cancel(ao->plugin, ao->data);
			ao_close(ao);
235 236 237 238

			/* don't automatically reopen this device for
			   10 seconds */
			ao->fail_timer = g_timer_new();
239
			return false;
240 241 242 243 244 245 246
		}

		assert(nbytes <= size);
		assert(nbytes % audio_format_frame_size(&ao->out_audio_format) == 0);

		data += nbytes;
		size -= nbytes;
247 248
	}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	return true;
}

static void ao_play(struct audio_output *ao)
{
	bool success;
	const struct music_chunk *chunk;

	assert(ao->pipe != NULL);

	g_mutex_lock(ao->mutex);
	chunk = ao->chunk;
	if (chunk != NULL)
		/* continue the previous play() call */
		chunk = chunk->next;
	else
		chunk = music_pipe_peek(ao->pipe);
	ao->chunk_finished = false;

	while (chunk != NULL && ao->command == AO_COMMAND_NONE) {
		assert(!ao->chunk_finished);

		ao->chunk = chunk;
		g_mutex_unlock(ao->mutex);

		success = ao_play_chunk(ao, chunk);

		g_mutex_lock(ao->mutex);

		if (!success) {
			assert(ao->chunk == NULL);
			break;
		}

		assert(ao->chunk == chunk);
		chunk = chunk->next;
	}

	ao->chunk_finished = true;
	g_mutex_unlock(ao->mutex);
289

290
	notify_signal(&pc.notify);
291 292
}

293 294
static void ao_pause(struct audio_output *ao)
{
295 296 297
	bool ret;

	ao_plugin_cancel(ao->plugin, ao->data);
298
	ao->pause = true;
299 300 301 302 303 304 305 306 307
	ao_command_finished(ao);

	do {
		ret = ao_plugin_pause(ao->plugin, ao->data);
		if (!ret) {
			ao_close(ao);
			break;
		}
	} while (ao->command == AO_COMMAND_NONE);
308 309

	ao->pause = false;
310 311
}

312
static gpointer audio_output_task(gpointer arg)
313 314 315 316 317 318 319 320 321
{
	struct audio_output *ao = arg;

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

		case AO_COMMAND_OPEN:
322
			ao_open(ao);
323 324 325
			ao_command_finished(ao);
			break;

326 327 328 329 330
		case AO_COMMAND_REOPEN:
			ao_reopen(ao);
			ao_command_finished(ao);
			break;

331 332
		case AO_COMMAND_CLOSE:
			assert(ao->open);
333 334 335 336
			assert(ao->pipe != NULL);

			ao->pipe = NULL;
			ao->chunk = NULL;
337

338
			ao_plugin_cancel(ao->plugin, ao->data);
339
			ao_close(ao);
340
			filter_close(ao->filter);
341 342 343
			ao_command_finished(ao);
			break;

344 345
		case AO_COMMAND_PAUSE:
			ao_pause(ao);
346 347 348 349 350
			/* don't "break" here: this might cause
			   ao_play() to be called when command==CLOSE
			   ends the paused state - "continue" checks
			   the new command first */
			continue;
351

352
		case AO_COMMAND_CANCEL:
353
			ao->chunk = NULL;
354 355
			if (ao->open)
				ao_plugin_cancel(ao->plugin, ao->data);
356 357
			ao_command_finished(ao);

358 359 360 361 362
			/* the player thread will now clear our music
			   pipe - wait for a notify, to give it some
			   time */
			notify_wait(&ao->notify);
			continue;
363 364

		case AO_COMMAND_KILL:
365
			ao->chunk = NULL;
366 367 368 369
			ao_command_finished(ao);
			return NULL;
		}

370 371 372
		if (ao->open)
			ao_play(ao);

373 374 375 376 377 378
		notify_wait(&ao->notify);
	}
}

void audio_output_thread_start(struct audio_output *ao)
{
379
	GError *e = NULL;
380 381 382

	assert(ao->command == AO_COMMAND_NONE);

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