output_thread.c 9.55 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
static bool
ao_enable(struct audio_output *ao)
{
	GError *error = NULL;

	if (ao->really_enabled)
		return true;

	if (!ao_plugin_enable(ao->plugin, ao->data, &error)) {
		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
ao_close(struct audio_output *ao);

static void
ao_disable(struct audio_output *ao)
{
	if (ao->open)
		ao_close(ao);

	if (ao->really_enabled) {
		ao->really_enabled = false;
		ao_plugin_disable(ao->plugin, ao->data);
	}
}

79 80 81 82 83
static void
ao_open(struct audio_output *ao)
{
	bool success;
	GError *error = NULL;
84
	const struct audio_format *filter_audio_format;
85 86 87 88 89 90

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

91 92 93 94 95 96
	/* enable the device (just in case the last enable has failed) */

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

97 98 99 100 101 102 103 104 105 106 107 108 109
	/* 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;
	}

110 111 112
	ao->out_audio_format = *filter_audio_format;
	audio_format_mask_apply(&ao->out_audio_format,
				&ao->config_audio_format);
113

114 115 116 117 118 119 120 121 122 123 124
	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);

125
		filter_close(ao->filter);
126 127 128 129
		ao->fail_timer = g_timer_new();
		return;
	}

130
	convert_filter_set(ao->convert_filter, &ao->out_audio_format);
131 132 133 134 135 136

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

	g_debug("opened plugin=%s name=\"%s\" "
137
		"audio_format=%u:%u:%u:%u",
138 139 140
		ao->plugin->name, ao->name,
		ao->out_audio_format.sample_rate,
		ao->out_audio_format.bits,
141 142
		ao->out_audio_format.channels,
		ao->out_audio_format.reverse_endian);
143 144 145

	if (!audio_format_equals(&ao->in_audio_format,
				 &ao->out_audio_format))
146
		g_debug("converting from %u:%u:%u:%u",
147 148
			ao->in_audio_format.sample_rate,
			ao->in_audio_format.bits,
149 150
			ao->in_audio_format.channels,
			ao->in_audio_format.reverse_endian);
151 152
}

153 154 155 156 157
static void
ao_close(struct audio_output *ao)
{
	assert(ao->open);

158 159 160 161
	ao->pipe = NULL;

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

165
	ao_plugin_close(ao->plugin, ao->data);
166
	filter_close(ao->filter);
167 168

	g_debug("closed plugin=%s name=\"%s\"", ao->plugin->name, ao->name);
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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);
}

205 206 207
static void
ao_reopen(struct audio_output *ao)
{
208
	if (!audio_format_fully_defined(&ao->config_audio_format)) {
209 210 211 212 213 214 215 216 217 218
		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;
219 220
		audio_format_mask_apply(&ao->out_audio_format,
					&ao->config_audio_format);
221 222
	}

223 224 225 226 227
	if (ao->open)
		/* the audio format has changed, and all filters have
		   to be reconfigured */
		ao_reopen_filter(ao);
	else
228 229 230
		ao_open(ao);
}

231 232
static bool
ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk)
233
{
234 235
	const char *data = chunk->data;
	size_t size = chunk->length;
236
	GError *error = NULL;
237

238 239
	assert(ao != NULL);
	assert(ao->filter != NULL);
240 241
	assert(!music_chunk_is_empty(chunk));
	assert(music_chunk_check_format(chunk, &ao->in_audio_format));
242
	assert(size % audio_format_frame_size(&ao->in_audio_format) == 0);
243

244 245 246 247 248 249
	if (chunk->tag != NULL)
		ao_plugin_send_tag(ao->plugin, ao->data, chunk->tag);

	if (size == 0)
		return true;

250 251 252 253 254 255 256 257 258 259 260 261 262
	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;
263 264
	}

265
	while (size > 0 && ao->command == AO_COMMAND_NONE) {
266 267
		size_t nbytes;

268 269
		nbytes = ao_plugin_play(ao->plugin, ao->data, data, size,
					&error);
270 271
		if (nbytes == 0) {
			/* play()==0 means failure */
272 273 274 275
			g_warning("\"%s\" [%s] failed to play: %s",
				  ao->name, ao->plugin->name, error->message);
			g_error_free(error);

276 277
			ao_plugin_cancel(ao->plugin, ao->data);
			ao_close(ao);
278 279 280 281

			/* don't automatically reopen this device for
			   10 seconds */
			ao->fail_timer = g_timer_new();
282
			return false;
283 284 285 286 287 288 289
		}

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

		data += nbytes;
		size -= nbytes;
290 291
	}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	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);
332

333
	notify_signal(&pc.notify);
334 335
}

336 337
static void ao_pause(struct audio_output *ao)
{
338 339 340
	bool ret;

	ao_plugin_cancel(ao->plugin, ao->data);
341
	ao->pause = true;
342 343 344 345 346 347 348 349 350
	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);
351 352

	ao->pause = false;
353 354
}

355
static gpointer audio_output_task(gpointer arg)
356 357 358 359 360 361 362 363
{
	struct audio_output *ao = arg;

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

364 365 366 367 368 369 370 371 372 373
		case AO_COMMAND_ENABLE:
			ao_enable(ao);
			ao_command_finished(ao);
			break;

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

374
		case AO_COMMAND_OPEN:
375
			ao_open(ao);
376 377 378
			ao_command_finished(ao);
			break;

379 380 381 382 383
		case AO_COMMAND_REOPEN:
			ao_reopen(ao);
			ao_command_finished(ao);
			break;

384 385
		case AO_COMMAND_CLOSE:
			assert(ao->open);
386 387 388 389
			assert(ao->pipe != NULL);

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

391
			ao_plugin_cancel(ao->plugin, ao->data);
392
			ao_close(ao);
393
			filter_close(ao->filter);
394 395 396
			ao_command_finished(ao);
			break;

397 398
		case AO_COMMAND_PAUSE:
			ao_pause(ao);
399 400 401 402 403
			/* 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;
404

405
		case AO_COMMAND_CANCEL:
406
			ao->chunk = NULL;
407 408
			if (ao->open)
				ao_plugin_cancel(ao->plugin, ao->data);
409 410
			ao_command_finished(ao);

411 412 413 414 415
			/* the player thread will now clear our music
			   pipe - wait for a notify, to give it some
			   time */
			notify_wait(&ao->notify);
			continue;
416 417

		case AO_COMMAND_KILL:
418
			ao->chunk = NULL;
419 420 421 422
			ao_command_finished(ao);
			return NULL;
		}

423 424 425
		if (ao->open)
			ao_play(ao);

426 427 428 429 430 431
		notify_wait(&ao->notify);
	}
}

void audio_output_thread_start(struct audio_output *ao)
{
432
	GError *e = NULL;
433 434 435

	assert(ao->command == AO_COMMAND_NONE);

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