2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-09-09 10:02:34 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-09-09 10:02:34 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Internal.hxx"
|
2013-04-17 01:12:05 +02:00
|
|
|
#include "OutputPlugin.hxx"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Domain.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerControl.hxx"
|
2013-01-10 10:44:04 +01:00
|
|
|
#include "notify.hxx"
|
2014-01-24 16:31:52 +01:00
|
|
|
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2016-12-27 22:47:20 +01:00
|
|
|
/** after a failure, wait this duration before
|
2013-10-20 13:52:26 +02:00
|
|
|
automatically reopening the device */
|
2016-12-27 22:47:20 +01:00
|
|
|
static constexpr PeriodClock::Duration REOPEN_AFTER = std::chrono::seconds(10);
|
2009-02-28 20:43:23 +01:00
|
|
|
|
2008-12-27 20:53:52 +01:00
|
|
|
struct notify audio_output_client_notify;
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::WaitForCommand()
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
while (!IsCommandFinished()) {
|
|
|
|
mutex.unlock();
|
2013-01-10 10:44:04 +01:00
|
|
|
audio_output_client_notify.Wait();
|
2014-01-28 11:39:12 +01:00
|
|
|
mutex.lock();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2014-12-24 22:11:57 +01:00
|
|
|
AudioOutput::CommandAsync(Command cmd)
|
2008-09-24 07:20:26 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(IsCommandFinished());
|
|
|
|
|
|
|
|
command = cmd;
|
|
|
|
cond.signal();
|
2008-09-24 07:20:26 +02:00
|
|
|
}
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2014-12-24 22:11:57 +01:00
|
|
|
AudioOutput::CommandWait(Command cmd)
|
2008-09-24 07:23:19 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
CommandAsync(cmd);
|
|
|
|
WaitForCommand();
|
2008-09-24 07:23:19 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2014-12-24 22:11:57 +01:00
|
|
|
AudioOutput::LockCommandWait(Command cmd)
|
2011-01-10 21:54:43 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
CommandWait(cmd);
|
2011-01-10 21:54:43 +01:00
|
|
|
}
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
void
|
2016-12-14 08:41:22 +01:00
|
|
|
AudioOutput::EnableAsync()
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
if (!thread.IsDefined()) {
|
|
|
|
if (plugin.enable == nullptr) {
|
2009-10-23 10:55:52 +02:00
|
|
|
/* 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 */
|
2014-01-28 11:39:12 +01:00
|
|
|
really_enabled = true;
|
2009-10-23 10:55:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
StartThread();
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2016-12-14 08:41:22 +01:00
|
|
|
CommandAsync(Command::ENABLE);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-12-14 08:41:22 +01:00
|
|
|
AudioOutput::DisableAsync()
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
if (!thread.IsDefined()) {
|
|
|
|
if (plugin.disable == nullptr)
|
|
|
|
really_enabled = false;
|
2009-10-23 10:55:52 +02:00
|
|
|
else
|
|
|
|
/* if there's no thread yet, the device cannot
|
|
|
|
be enabled */
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(!really_enabled);
|
2009-10-23 10:55:52 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-14 08:41:22 +01:00
|
|
|
CommandAsync(Command::DISABLE);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
inline bool
|
|
|
|
AudioOutput::Open(const AudioFormat audio_format, const MusicPipe &mp)
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(audio_format.IsValid());
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
fail_timer.Reset();
|
2008-10-29 22:32:50 +01:00
|
|
|
|
2016-12-21 17:00:29 +01:00
|
|
|
if (open && audio_format == request.audio_format) {
|
2016-12-20 16:37:36 +01:00
|
|
|
assert(request.pipe == &mp || (always_on && pause));
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2016-12-21 13:01:38 +01:00
|
|
|
if (!pause)
|
|
|
|
/* already open, already the right parameters
|
|
|
|
- nothing needs to be done */
|
|
|
|
return true;
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 17:00:29 +01:00
|
|
|
request.audio_format = audio_format;
|
2016-12-20 23:19:12 +01:00
|
|
|
request.pipe = ∓
|
2009-03-09 19:25:26 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (!thread.IsDefined())
|
|
|
|
StartThread();
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2016-12-13 20:07:00 +01:00
|
|
|
CommandWait(Command::OPEN);
|
2014-01-28 11:39:12 +01:00
|
|
|
const bool open2 = open;
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (open2 && mixer != nullptr) {
|
2016-09-09 12:52:51 +02:00
|
|
|
try {
|
|
|
|
mixer_open(mixer);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
FormatError(e, "Failed to open mixer for '%s'", name);
|
|
|
|
}
|
2009-10-20 22:10:56 +02:00
|
|
|
}
|
2009-03-26 18:23:23 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
return open2;
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::CloseWait()
|
2009-10-29 22:39:42 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
2010-11-07 15:30:18 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (mixer != nullptr)
|
|
|
|
mixer_auto_close(mixer);
|
2009-10-29 22:39:42 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(!open || !fail_timer.IsDefined());
|
2009-10-29 22:39:42 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (open)
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandWait(Command::CLOSE);
|
2013-11-24 20:20:57 +01:00
|
|
|
else
|
2014-01-28 11:39:12 +01:00
|
|
|
fail_timer.Reset();
|
2009-10-29 22:39:42 +01:00
|
|
|
}
|
|
|
|
|
2009-03-07 19:55:57 +01:00
|
|
|
bool
|
2014-01-28 11:39:12 +01:00
|
|
|
AudioOutput::LockUpdate(const AudioFormat audio_format,
|
|
|
|
const MusicPipe &mp)
|
2008-10-29 22:17:44 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2009-10-29 17:06:40 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (enabled && really_enabled) {
|
2015-06-20 15:37:19 +02:00
|
|
|
if (!fail_timer.IsDefined() ||
|
|
|
|
fail_timer.Check(REOPEN_AFTER * 1000)) {
|
2014-01-28 11:39:12 +01:00
|
|
|
return Open(audio_format, mp);
|
2009-10-29 17:06:40 +01:00
|
|
|
}
|
2014-01-28 11:39:12 +01:00
|
|
|
} else if (IsOpen())
|
|
|
|
CloseWait();
|
2009-03-07 19:55:57 +01:00
|
|
|
|
|
|
|
return false;
|
2008-10-29 22:17:44 +01:00
|
|
|
}
|
|
|
|
|
2009-02-16 00:43:06 +01:00
|
|
|
void
|
2014-01-28 11:39:12 +01:00
|
|
|
AudioOutput::LockPlay()
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
2011-09-01 07:13:21 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (IsOpen() && !in_playback_loop && !woken_for_play) {
|
|
|
|
woken_for_play = true;
|
|
|
|
cond.signal();
|
2013-11-06 23:47:30 +01:00
|
|
|
}
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::LockPauseAsync()
|
2008-09-29 16:43:55 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
if (mixer != nullptr && plugin.pause == nullptr)
|
2009-04-21 22:17:52 +02:00
|
|
|
/* the device has no pause mode: close the mixer,
|
|
|
|
unless its "global" flag is set (checked by
|
|
|
|
mixer_auto_close()) */
|
2014-01-28 11:39:12 +01:00
|
|
|
mixer_auto_close(mixer);
|
2009-04-21 22:17:52 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2013-04-17 01:19:25 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
|
|
|
if (IsOpen())
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandAsync(Command::PAUSE);
|
2008-09-29 16:43:55 +02:00
|
|
|
}
|
|
|
|
|
2009-11-09 22:16:26 +01:00
|
|
|
void
|
2014-01-28 11:39:12 +01:00
|
|
|
AudioOutput::LockDrainAsync()
|
2009-11-09 22:16:26 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2013-04-17 01:19:25 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(allow_play);
|
|
|
|
if (IsOpen())
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandAsync(Command::DRAIN);
|
2009-11-09 22:16:26 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::LockCancelAsync()
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2011-09-01 07:13:21 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
if (IsOpen()) {
|
|
|
|
allow_play = false;
|
2014-12-24 22:11:57 +01:00
|
|
|
CommandAsync(Command::CANCEL);
|
2011-09-01 07:13:21 +02:00
|
|
|
}
|
2011-09-01 07:53:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-28 11:39:12 +01:00
|
|
|
AudioOutput::LockAllowPlay()
|
2011-09-01 07:53:42 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
2011-09-01 07:53:42 +02:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
allow_play = true;
|
|
|
|
if (IsOpen())
|
|
|
|
cond.signal();
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:29:33 +01:00
|
|
|
void
|
2014-01-28 11:39:12 +01:00
|
|
|
AudioOutput::LockRelease()
|
2010-03-03 20:29:33 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
if (always_on)
|
|
|
|
LockPauseAsync();
|
2010-03-03 20:29:33 +01:00
|
|
|
else
|
2014-01-28 11:39:12 +01:00
|
|
|
LockCloseWait();
|
2010-03-03 20:29:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::LockCloseWait()
|
2010-11-04 21:51:02 +01:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(!open || !fail_timer.IsDefined());
|
2010-11-04 21:51:02 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
CloseWait();
|
2010-11-04 21:51:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
|
|
|
AudioOutput::StopThread()
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
assert(thread.IsDefined());
|
|
|
|
assert(allow_play);
|
2009-01-07 23:55:13 +01:00
|
|
|
|
2014-12-24 22:11:57 +01:00
|
|
|
LockCommandWait(Command::KILL);
|
2014-01-28 11:39:12 +01:00
|
|
|
thread.Join();
|
|
|
|
}
|
2009-02-28 20:43:23 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
void
|
2016-12-14 08:15:33 +01:00
|
|
|
AudioOutput::BeginDestroy()
|
2014-01-28 11:39:12 +01:00
|
|
|
{
|
2016-12-14 08:29:09 +01:00
|
|
|
if (mixer != nullptr)
|
|
|
|
mixer_auto_close(mixer);
|
2014-01-28 11:39:12 +01:00
|
|
|
|
2016-12-14 08:15:33 +01:00
|
|
|
if (thread.IsDefined()) {
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
CommandAsync(Command::KILL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioOutput::FinishDestroy()
|
|
|
|
{
|
2014-01-28 11:39:12 +01:00
|
|
|
if (thread.IsDefined())
|
2016-12-14 08:15:33 +01:00
|
|
|
thread.Join();
|
2009-01-07 23:55:13 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
audio_output_free(this);
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|