output_thread.c 10.9 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
#include "config.h"
21 22
#include "output_thread.h"
#include "output_api.h"
23
#include "output_internal.h"
24 25
#include "chunk.h"
#include "pipe.h"
26
#include "player_control.h"
27 28
#include "filter_plugin.h"
#include "filter/convert_filter_plugin.h"
29

30
#include <glib.h>
31

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

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

39 40 41 42
static void ao_command_finished(struct audio_output *ao)
{
	assert(ao->command != AO_COMMAND_NONE);
	ao->command = AO_COMMAND_NONE;
43 44

	g_mutex_unlock(ao->mutex);
45
	notify_signal(&audio_output_client_notify);
46
	g_mutex_lock(ao->mutex);
47 48
}

49 50 51 52
static bool
ao_enable(struct audio_output *ao)
{
	GError *error = NULL;
53
	bool success;
54 55 56 57

	if (ao->really_enabled)
		return true;

58 59 60 61
	g_mutex_unlock(ao->mutex);
	success = ao_plugin_enable(ao->plugin, ao->data, &error);
	g_mutex_lock(ao->mutex);
	if (!success) {
62 63 64 65 66 67 68 69 70 71 72
		g_warning("Failed to enable \"%s\" [%s]: %s\n",
			  ao->name, ao->plugin->name, error->message);
		g_error_free(error);
		return false;
	}

	ao->really_enabled = true;
	return true;
}

static void
73
ao_close(struct audio_output *ao, bool drain);
74 75 76 77 78

static void
ao_disable(struct audio_output *ao)
{
	if (ao->open)
79
		ao_close(ao, false);
80 81 82

	if (ao->really_enabled) {
		ao->really_enabled = false;
83 84

		g_mutex_unlock(ao->mutex);
85
		ao_plugin_disable(ao->plugin, ao->data);
86
		g_mutex_lock(ao->mutex);
87 88 89
	}
}

90 91 92 93 94
static void
ao_open(struct audio_output *ao)
{
	bool success;
	GError *error = NULL;
95
	const struct audio_format *filter_audio_format;
96
	struct audio_format_string af_string;
97 98 99 100 101 102

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

103 104 105 106 107 108
	/* enable the device (just in case the last enable has failed) */

	if (!ao_enable(ao))
		/* still no luck */
		return;

109 110 111 112 113 114 115 116 117 118 119 120 121
	/* 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;
	}

122 123 124
	ao->out_audio_format = *filter_audio_format;
	audio_format_mask_apply(&ao->out_audio_format,
				&ao->config_audio_format);
125

126
	g_mutex_unlock(ao->mutex);
127 128 129
	success = ao_plugin_open(ao->plugin, ao->data,
				 &ao->out_audio_format,
				 &error);
130
	g_mutex_lock(ao->mutex);
131 132 133 134 135 136 137 138

	assert(!ao->open);

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

139
		filter_close(ao->filter);
140 141 142 143
		ao->fail_timer = g_timer_new();
		return;
	}

144
	convert_filter_set(ao->convert_filter, &ao->out_audio_format);
145 146 147 148

	ao->open = true;

	g_debug("opened plugin=%s name=\"%s\" "
149
		"audio_format=%s",
150
		ao->plugin->name, ao->name,
151
		audio_format_to_string(&ao->out_audio_format, &af_string));
152 153 154

	if (!audio_format_equals(&ao->in_audio_format,
				 &ao->out_audio_format))
155 156 157
		g_debug("converting from %s",
			audio_format_to_string(&ao->in_audio_format,
					       &af_string));
158 159
}

160
static void
161
ao_close(struct audio_output *ao, bool drain)
162 163 164
{
	assert(ao->open);

165 166 167
	ao->pipe = NULL;

	ao->chunk = NULL;
168
	ao->open = false;
169

170 171
	g_mutex_unlock(ao->mutex);

172 173 174 175 176
	if (drain)
		ao_plugin_drain(ao->plugin, ao->data);
	else
		ao_plugin_cancel(ao->plugin, ao->data);

177
	ao_plugin_close(ao->plugin, ao->data);
178
	filter_close(ao->filter);
179

180 181
	g_mutex_lock(ao->mutex);

182
	g_debug("closed plugin=%s name=\"%s\"", ao->plugin->name, ao->name);
183 184
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
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;

		ao->chunk = NULL;
		ao->open = false;
207
		ao->fail_timer = g_timer_new();
208

209
		g_mutex_unlock(ao->mutex);
210
		ao_plugin_close(ao->plugin, ao->data);
211
		g_mutex_lock(ao->mutex);
212 213 214 215 216 217 218

		return;
	}

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

219 220 221
static void
ao_reopen(struct audio_output *ao)
{
222
	if (!audio_format_fully_defined(&ao->config_audio_format)) {
223 224
		if (ao->open) {
			const struct music_pipe *mp = ao->pipe;
225
			ao_close(ao, true);
226 227 228 229 230 231 232
			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;
233 234
		audio_format_mask_apply(&ao->out_audio_format,
					&ao->config_audio_format);
235 236
	}

237 238 239 240 241
	if (ao->open)
		/* the audio format has changed, and all filters have
		   to be reconfigured */
		ao_reopen_filter(ao);
	else
242 243 244
		ao_open(ao);
}

245 246
static bool
ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk)
247
{
248 249
	const char *data = chunk->data;
	size_t size = chunk->length;
250
	GError *error = NULL;
251

252 253
	assert(ao != NULL);
	assert(ao->filter != NULL);
254 255
	assert(!music_chunk_is_empty(chunk));
	assert(music_chunk_check_format(chunk, &ao->in_audio_format));
256
	assert(size % audio_format_frame_size(&ao->in_audio_format) == 0);
257

258 259
	if (chunk->tag != NULL) {
		g_mutex_unlock(ao->mutex);
260
		ao_plugin_send_tag(ao->plugin, ao->data, chunk->tag);
261 262
		g_mutex_lock(ao->mutex);
	}
263 264 265 266

	if (size == 0)
		return true;

267 268 269 270 271 272
	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);

273
		ao_close(ao, false);
274 275 276 277 278

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

281
	while (size > 0 && ao->command == AO_COMMAND_NONE) {
282 283
		size_t nbytes;

284
		g_mutex_unlock(ao->mutex);
285 286
		nbytes = ao_plugin_play(ao->plugin, ao->data, data, size,
					&error);
287
		g_mutex_lock(ao->mutex);
288 289
		if (nbytes == 0) {
			/* play()==0 means failure */
290 291 292 293
			g_warning("\"%s\" [%s] failed to play: %s",
				  ao->name, ao->plugin->name, error->message);
			g_error_free(error);

294
			ao_close(ao, false);
295 296 297 298

			/* don't automatically reopen this device for
			   10 seconds */
			ao->fail_timer = g_timer_new();
299
			return false;
300 301 302 303 304 305 306
		}

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

		data += nbytes;
		size -= nbytes;
307 308
	}

309 310 311
	return true;
}

312 313 314 315 316 317 318 319 320 321
static const struct music_chunk *
ao_next_chunk(struct audio_output *ao)
{
	return ao->chunk != NULL
		/* continue the previous play() call */
		? ao->chunk->next
		/* get the first chunk from the pipe */
		: music_pipe_peek(ao->pipe);
}

322 323 324 325 326 327 328 329 330 331
/**
 * Plays all remaining chunks, until the tail of the pipe has been
 * reached (and no more chunks are queued), or until a command is
 * received.
 *
 * @return true if at least one chunk has been available, false if the
 * tail of the pipe was already reached
 */
static bool
ao_play(struct audio_output *ao)
332 333 334 335 336 337
{
	bool success;
	const struct music_chunk *chunk;

	assert(ao->pipe != NULL);

338
	chunk = ao_next_chunk(ao);
339 340 341
	if (chunk == NULL)
		/* no chunk available */
		return false;
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	ao->chunk_finished = false;

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

		ao->chunk = chunk;

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

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

	ao->chunk_finished = true;
361

362
	g_mutex_unlock(ao->mutex);
363
	player_lock_signal();
364
	g_mutex_lock(ao->mutex);
365 366

	return true;
367 368
}

369 370
static void ao_pause(struct audio_output *ao)
{
371 372
	bool ret;

373
	g_mutex_unlock(ao->mutex);
374
	ao_plugin_cancel(ao->plugin, ao->data);
375 376
	g_mutex_lock(ao->mutex);

377
	ao->pause = true;
378 379 380
	ao_command_finished(ao);

	do {
381
		g_mutex_unlock(ao->mutex);
382
		ret = ao_plugin_pause(ao->plugin, ao->data);
383 384
		g_mutex_lock(ao->mutex);

385
		if (!ret) {
386
			ao_close(ao, false);
387 388 389
			break;
		}
	} while (ao->command == AO_COMMAND_NONE);
390 391

	ao->pause = false;
392 393
}

394
static gpointer audio_output_task(gpointer arg)
395 396 397
{
	struct audio_output *ao = arg;

398 399
	g_mutex_lock(ao->mutex);

400 401 402 403 404
	while (1) {
		switch (ao->command) {
		case AO_COMMAND_NONE:
			break;

405 406 407 408 409 410 411 412 413 414
		case AO_COMMAND_ENABLE:
			ao_enable(ao);
			ao_command_finished(ao);
			break;

		case AO_COMMAND_DISABLE:
			ao_disable(ao);
			ao_command_finished(ao);
			break;

415
		case AO_COMMAND_OPEN:
416
			ao_open(ao);
417 418 419
			ao_command_finished(ao);
			break;

420 421 422 423 424
		case AO_COMMAND_REOPEN:
			ao_reopen(ao);
			ao_command_finished(ao);
			break;

425 426
		case AO_COMMAND_CLOSE:
			assert(ao->open);
427 428
			assert(ao->pipe != NULL);

429
			ao_close(ao, false);
430 431 432
			ao_command_finished(ao);
			break;

433
		case AO_COMMAND_PAUSE:
434 435 436 437 438 439 440 441 442
			if (!ao->open) {
				/* the output has failed after
				   audio_output_all_pause() has
				   submitted the PAUSE command; bail
				   out */
				ao_command_finished(ao);
				break;
			}

443
			ao_pause(ao);
444 445 446 447 448
			/* 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;
449

450 451 452 453 454 455 456 457 458 459 460 461 462
		case AO_COMMAND_DRAIN:
			if (ao->open) {
				assert(ao->chunk == NULL);
				assert(music_pipe_peek(ao->pipe) == NULL);

				g_mutex_unlock(ao->mutex);
				ao_plugin_drain(ao->plugin, ao->data);
				g_mutex_lock(ao->mutex);
			}

			ao_command_finished(ao);
			continue;

463
		case AO_COMMAND_CANCEL:
464
			ao->chunk = NULL;
465 466
			if (ao->open)
				ao_plugin_cancel(ao->plugin, ao->data);
467 468
			ao_command_finished(ao);

469 470 471
			/* the player thread will now clear our music
			   pipe - wait for a notify, to give it some
			   time */
472 473
			if (ao->command == AO_COMMAND_NONE)
				g_cond_wait(ao->cond, ao->mutex);
474
			continue;
475 476

		case AO_COMMAND_KILL:
477
			ao->chunk = NULL;
478
			ao_command_finished(ao);
479
			g_mutex_unlock(ao->mutex);
480 481 482
			return NULL;
		}

483 484 485 486
		if (ao->open && ao_play(ao))
			/* don't wait for an event if there are more
			   chunks in the pipe */
			continue;
487

488 489
		if (ao->command == AO_COMMAND_NONE)
			g_cond_wait(ao->cond, ao->mutex);
490 491 492 493 494
	}
}

void audio_output_thread_start(struct audio_output *ao)
{
495
	GError *e = NULL;
496 497 498

	assert(ao->command == AO_COMMAND_NONE);

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