OutputControl.cxx 7.38 KB
Newer Older
1

2
/*
3
 * Copyright (C) 2003-2013 The Music Player Daemon Project
4
 * http://www.musicpd.org
5 6 7 8 9 10 11 12 13 14
 *
 * 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.
15 16 17 18
 *
 * 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.
19 20
 */

21
#include "config.h"
22 23
#include "OutputControl.hxx"
#include "OutputThread.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25
#include "OutputInternal.hxx"
#include "OutputPlugin.hxx"
26
#include "OutputError.hxx"
27 28
#include "MixerPlugin.hxx"
#include "MixerControl.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "notify.hxx"
30
#include "filter/ReplayGainFilterPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "FilterPlugin.hxx"
32
#include "util/Error.hxx"
33
#include "Log.hxx"
34

35 36
#include <glib.h>

37 38
#include <assert.h>
#include <stdlib.h>
39

40 41 42
/** after a failure, wait this number of seconds before
    automatically reopening the device */
static constexpr unsigned REOPEN_AFTER = 10;
43

44
struct notify audio_output_client_notify;
45

46 47 48 49 50
/**
 * Waits for command completion.
 *
 * @param ao the #audio_output instance; must be locked
 */
51 52 53
static void ao_command_wait(struct audio_output *ao)
{
	while (ao->command != AO_COMMAND_NONE) {
54
		ao->mutex.unlock();
Max Kellermann's avatar
Max Kellermann committed
55
		audio_output_client_notify.Wait();
56
		ao->mutex.lock();
57 58 59
	}
}

60 61 62 63 64 65
/**
 * Sends a command to the #audio_output object, but does not wait for
 * completion.
 *
 * @param ao the #audio_output instance; must be locked
 */
66 67
static void ao_command_async(struct audio_output *ao,
			     enum audio_output_command cmd)
68 69 70
{
	assert(ao->command == AO_COMMAND_NONE);
	ao->command = cmd;
71
	ao->cond.signal();
72
}
73

74 75 76 77 78 79
/**
 * Sends a command to the #audio_output object and waits for
 * completion.
 *
 * @param ao the #audio_output instance; must be locked
 */
80 81
static void
ao_command(struct audio_output *ao, enum audio_output_command cmd)
82
{
83 84
	ao_command_async(ao, cmd);
	ao_command_wait(ao);
85 86
}

87 88 89 90 91 92 93
/**
 * Lock the #audio_output object and execute the command
 * synchronously.
 */
static void
ao_lock_command(struct audio_output *ao, enum audio_output_command cmd)
{
94
	const ScopeLock protect(ao->mutex);
95 96 97
	ao_command(ao, cmd);
}

98 99
void
audio_output_set_replay_gain_mode(struct audio_output *ao,
100
				  ReplayGainMode mode)
101
{
102
	if (ao->replay_gain_filter != nullptr)
103 104 105
		replay_gain_filter_set_mode(ao->replay_gain_filter, mode);
}

106 107 108
void
audio_output_enable(struct audio_output *ao)
{
109
	if (!ao->thread.IsDefined()) {
110
		if (ao->plugin->enable == nullptr) {
111 112 113 114 115 116 117 118 119 120
			/* don't bother to start the thread now if the
			   device doesn't even have a enable() method;
			   just assign the variable and we're done */
			ao->really_enabled = true;
			return;
		}

		audio_output_thread_start(ao);
	}

121
	ao_lock_command(ao, AO_COMMAND_ENABLE);
122 123 124 125 126
}

void
audio_output_disable(struct audio_output *ao)
{
127
	if (!ao->thread.IsDefined()) {
128
		if (ao->plugin->disable == nullptr)
129 130 131 132 133 134 135 136 137
			ao->really_enabled = false;
		else
			/* if there's no thread yet, the device cannot
			   be enabled */
			assert(!ao->really_enabled);

		return;
	}

138
	ao_lock_command(ao, AO_COMMAND_DISABLE);
139 140
}

141 142 143
/**
 * Object must be locked (and unlocked) by the caller.
 */
144
static bool
145
audio_output_open(struct audio_output *ao,
146
		  const AudioFormat audio_format,
147
		  const MusicPipe &mp)
148
{
149 150
	bool open;

151
	assert(ao != nullptr);
152
	assert(ao->allow_play);
153
	assert(audio_format.IsValid());
154

155
	if (ao->fail_timer != nullptr) {
156
		g_timer_destroy(ao->fail_timer);
157
		ao->fail_timer = nullptr;
158
	}
159

160
	if (ao->open && audio_format == ao->in_audio_format) {
161
		assert(ao->pipe == &mp ||
162
		       (ao->always_on && ao->pause));
163

164
		if (ao->pause) {
165
			ao->chunk = nullptr;
166
			ao->pipe = &mp;
167

168 169 170 171 172 173 174 175 176 177
			/* unpause with the CANCEL command; this is a
			   hack, but suits well for forcing the thread
			   to leave the ao_pause() thread, and we need
			   to flush the device buffer anyway */

			/* we're not using audio_output_cancel() here,
			   because that function is asynchronous */
			ao_command(ao, AO_COMMAND_CANCEL);
		}

178
		return true;
179 180
	}

181
	ao->in_audio_format = audio_format;
182
	ao->chunk = nullptr;
183

184
	ao->pipe = &mp;
185

186
	if (!ao->thread.IsDefined())
187
		audio_output_thread_start(ao);
188

189
	ao_command(ao, ao->open ? AO_COMMAND_REOPEN : AO_COMMAND_OPEN);
190
	open = ao->open;
191

192
	if (open && ao->mixer != nullptr) {
193 194
		Error error;
		if (!mixer_open(ao->mixer, error))
195 196 197
			FormatWarning(output_domain,
				      "Failed to open mixer for '%s'",
				      ao->name);
198
	}
199 200

	return open;
201 202
}

203 204 205 206 207 208 209
/**
 * Same as audio_output_close(), but expects the lock to be held by
 * the caller.
 */
static void
audio_output_close_locked(struct audio_output *ao)
{
210
	assert(ao != nullptr);
211
	assert(ao->allow_play);
212

213
	if (ao->mixer != nullptr)
214 215
		mixer_auto_close(ao->mixer);

216
	assert(!ao->open || ao->fail_timer == nullptr);
217 218 219

	if (ao->open)
		ao_command(ao, AO_COMMAND_CLOSE);
220
	else if (ao->fail_timer != nullptr) {
221
		g_timer_destroy(ao->fail_timer);
222
		ao->fail_timer = nullptr;
223 224 225
	}
}

226
bool
227
audio_output_update(struct audio_output *ao,
228
		    const AudioFormat audio_format,
229
		    const MusicPipe &mp)
230
{
231
	const ScopeLock protect(ao->mutex);
232

233
	if (ao->enabled && ao->really_enabled) {
234 235
		if (ao->fail_timer == nullptr ||
		    g_timer_elapsed(ao->fail_timer, nullptr) > REOPEN_AFTER) {
236
			return audio_output_open(ao, audio_format, mp);
237
		}
238
	} else if (audio_output_is_open(ao))
239
		audio_output_close_locked(ao);
240 241

	return false;
242 243
}

244
void
245
audio_output_play(struct audio_output *ao)
246
{
247
	const ScopeLock protect(ao->mutex);
248

249 250
	assert(ao->allow_play);

251 252 253
	if (audio_output_is_open(ao) && !ao->in_playback_loop &&
	    !ao->woken_for_play) {
		ao->woken_for_play = true;
254
		ao->cond.signal();
255
	}
256 257
}

258
void audio_output_pause(struct audio_output *ao)
259
{
260
	if (ao->mixer != nullptr && ao->plugin->pause == nullptr)
261 262 263 264 265
		/* the device has no pause mode: close the mixer,
		   unless its "global" flag is set (checked by
		   mixer_auto_close()) */
		mixer_auto_close(ao->mixer);

266 267
	const ScopeLock protect(ao->mutex);

268
	assert(ao->allow_play);
269 270
	if (audio_output_is_open(ao))
		ao_command_async(ao, AO_COMMAND_PAUSE);
271 272
}

273 274 275
void
audio_output_drain_async(struct audio_output *ao)
{
276 277
	const ScopeLock protect(ao->mutex);

278
	assert(ao->allow_play);
279 280 281 282
	if (audio_output_is_open(ao))
		ao_command_async(ao, AO_COMMAND_DRAIN);
}

283
void audio_output_cancel(struct audio_output *ao)
284
{
285
	const ScopeLock protect(ao->mutex);
286 287 288

	if (audio_output_is_open(ao)) {
		ao->allow_play = false;
289
		ao_command_async(ao, AO_COMMAND_CANCEL);
290
	}
291 292 293 294 295
}

void
audio_output_allow_play(struct audio_output *ao)
{
296
	const ScopeLock protect(ao->mutex);
297 298 299

	ao->allow_play = true;
	if (audio_output_is_open(ao))
300
		ao->cond.signal();
301 302
}

303 304 305 306 307 308 309 310 311
void
audio_output_release(struct audio_output *ao)
{
	if (ao->always_on)
		audio_output_pause(ao);
	else
		audio_output_close(ao);
}

312 313
void audio_output_close(struct audio_output *ao)
{
314 315
	assert(ao != nullptr);
	assert(!ao->open || ao->fail_timer == nullptr);
316

317
	const ScopeLock protect(ao->mutex);
318 319 320
	audio_output_close_locked(ao);
}

321
void audio_output_finish(struct audio_output *ao)
322
{
323
	audio_output_close(ao);
324

325
	assert(ao->fail_timer == nullptr);
326

327
	if (ao->thread.IsDefined()) {
328
		assert(ao->allow_play);
329
		ao_lock_command(ao, AO_COMMAND_KILL);
330
		ao->thread.Join();
331 332
	}

333
	audio_output_free(ao);
334
}