2014-01-27 08:20:25 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2014-01-27 08:20:25 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "MultipleOutputs.hxx"
|
2014-01-28 11:42:54 +01:00
|
|
|
#include "Internal.hxx"
|
|
|
|
#include "Domain.hxx"
|
2014-01-27 08:20:25 +01:00
|
|
|
#include "MusicBuffer.hxx"
|
|
|
|
#include "MusicPipe.hxx"
|
|
|
|
#include "MusicChunk.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "config/Block.hxx"
|
2014-01-27 08:20:25 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
|
|
|
#include "config/ConfigOption.hxx"
|
|
|
|
#include "notify.hxx"
|
2016-11-24 14:04:40 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2014-01-27 08:20:25 +01:00
|
|
|
|
2016-10-28 21:11:52 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-02-05 23:20:33 +01:00
|
|
|
MultipleOutputs::MultipleOutputs(MixerListener &_mixer_listener)
|
2016-04-21 13:27:45 +02:00
|
|
|
:mixer_listener(_mixer_listener)
|
2014-01-27 08:20:25 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MultipleOutputs::~MultipleOutputs()
|
|
|
|
{
|
2016-12-14 08:15:33 +01:00
|
|
|
/* parallel destruction */
|
2016-12-14 08:29:09 +01:00
|
|
|
for (auto i : outputs)
|
2016-12-14 08:15:33 +01:00
|
|
|
i->BeginDestroy();
|
|
|
|
for (auto i : outputs)
|
|
|
|
i->FinishDestroy();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
static AudioOutput *
|
2016-11-25 13:54:55 +01:00
|
|
|
LoadOutput(EventLoop &event_loop,
|
|
|
|
const ReplayGainConfig &replay_gain_config,
|
|
|
|
MixerListener &mixer_listener,
|
2016-12-14 12:14:57 +01:00
|
|
|
AudioOutputClient &client, const ConfigBlock &block)
|
2016-10-28 21:11:52 +02:00
|
|
|
try {
|
2016-11-25 13:54:55 +01:00
|
|
|
return audio_output_new(event_loop, replay_gain_config, block,
|
2016-11-09 11:56:01 +01:00
|
|
|
mixer_listener,
|
2016-12-14 12:14:57 +01:00
|
|
|
client);
|
2016-10-28 21:11:52 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
if (block.line > 0)
|
2016-11-24 14:04:40 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to configure output in line %i",
|
|
|
|
block.line));
|
2016-10-28 21:11:52 +02:00
|
|
|
else
|
2016-11-24 14:04:40 +01:00
|
|
|
throw;
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-25 13:54:55 +01:00
|
|
|
MultipleOutputs::Configure(EventLoop &event_loop,
|
|
|
|
const ReplayGainConfig &replay_gain_config,
|
2016-12-14 12:14:57 +01:00
|
|
|
AudioOutputClient &client)
|
2014-01-27 08:20:25 +01:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
for (const auto *param = config_get_block(ConfigBlockOption::AUDIO_OUTPUT);
|
2014-02-19 23:57:55 +01:00
|
|
|
param != nullptr; param = param->next) {
|
2016-11-25 13:54:55 +01:00
|
|
|
auto output = LoadOutput(event_loop, replay_gain_config,
|
|
|
|
mixer_listener,
|
2016-12-14 12:14:57 +01:00
|
|
|
client, *param);
|
2014-01-27 08:20:25 +01:00
|
|
|
if (FindByName(output->name) != nullptr)
|
2016-11-24 14:04:40 +01:00
|
|
|
throw FormatRuntimeError("output devices with identical "
|
|
|
|
"names: %s", output->name);
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
outputs.push_back(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputs.empty()) {
|
|
|
|
/* auto-detect device */
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigBlock empty;
|
2016-11-25 13:54:55 +01:00
|
|
|
auto output = LoadOutput(event_loop, replay_gain_config,
|
|
|
|
mixer_listener,
|
2016-12-14 12:14:57 +01:00
|
|
|
client, empty);
|
2014-01-27 08:20:25 +01:00
|
|
|
outputs.push_back(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput *
|
2014-01-27 08:20:25 +01:00
|
|
|
MultipleOutputs::FindByName(const char *name) const
|
|
|
|
{
|
|
|
|
for (auto i : outputs)
|
|
|
|
if (strcmp(i->name, name) == 0)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::EnableDisable()
|
|
|
|
{
|
2016-12-14 08:41:22 +01:00
|
|
|
/* parallel execution */
|
|
|
|
|
|
|
|
for (auto ao : outputs) {
|
|
|
|
const ScopeLock lock(ao->mutex);
|
|
|
|
ao->EnableDisableAsync();
|
|
|
|
}
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
for (auto ao : outputs) {
|
2016-12-14 07:54:05 +01:00
|
|
|
const ScopeLock lock(ao->mutex);
|
2016-12-14 08:41:22 +01:00
|
|
|
ao->WaitForCommand();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MultipleOutputs::AllFinished() const
|
|
|
|
{
|
|
|
|
for (auto ao : outputs) {
|
|
|
|
const ScopeLock protect(ao->mutex);
|
2014-01-28 11:39:12 +01:00
|
|
|
if (ao->IsOpen() && !ao->IsCommandFinished())
|
2014-01-27 08:20:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::WaitAll()
|
|
|
|
{
|
|
|
|
while (!AllFinished())
|
|
|
|
audio_output_client_notify.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::AllowPlay()
|
|
|
|
{
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockAllowPlay();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
audio_output_reset_reopen(AudioOutput *ao)
|
2014-01-27 08:20:25 +01:00
|
|
|
{
|
|
|
|
const ScopeLock protect(ao->mutex);
|
|
|
|
|
|
|
|
ao->fail_timer.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::ResetReopen()
|
|
|
|
{
|
|
|
|
for (auto ao : outputs)
|
|
|
|
audio_output_reset_reopen(ao);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MultipleOutputs::Update()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (!input_audio_format.IsDefined())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ret = ao->LockUpdate(input_audio_format, *pipe)
|
2014-01-27 08:20:25 +01:00
|
|
|
|| ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::SetReplayGainMode(ReplayGainMode mode)
|
|
|
|
{
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->SetReplayGainMode(mode);
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
void
|
|
|
|
MultipleOutputs::Play(MusicChunk *chunk)
|
2014-01-27 08:20:25 +01:00
|
|
|
{
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
assert(pipe != nullptr);
|
|
|
|
assert(chunk != nullptr);
|
|
|
|
assert(chunk->CheckFormat(input_audio_format));
|
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
if (!Update())
|
2014-01-27 08:20:25 +01:00
|
|
|
/* TODO: obtain real error */
|
2016-11-09 12:31:23 +01:00
|
|
|
throw std::runtime_error("Failed to open audio output");
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
pipe->Push(chunk);
|
|
|
|
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockPlay();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
void
|
2014-01-27 08:20:25 +01:00
|
|
|
MultipleOutputs::Open(const AudioFormat audio_format,
|
2016-11-09 12:31:23 +01:00
|
|
|
MusicBuffer &_buffer)
|
2014-01-27 08:20:25 +01:00
|
|
|
{
|
|
|
|
bool ret = false, enabled = false;
|
|
|
|
|
|
|
|
assert(buffer == nullptr || buffer == &_buffer);
|
|
|
|
assert((pipe == nullptr) == (buffer == nullptr));
|
|
|
|
|
|
|
|
buffer = &_buffer;
|
|
|
|
|
|
|
|
/* the audio format must be the same as existing chunks in the
|
|
|
|
pipe */
|
|
|
|
assert(pipe == nullptr || pipe->CheckFormat(audio_format));
|
|
|
|
|
|
|
|
if (pipe == nullptr)
|
|
|
|
pipe = new MusicPipe();
|
|
|
|
else
|
|
|
|
/* if the pipe hasn't been cleared, the the audio
|
|
|
|
format must not have changed */
|
|
|
|
assert(pipe->IsEmpty() || audio_format == input_audio_format);
|
|
|
|
|
|
|
|
input_audio_format = audio_format;
|
|
|
|
|
|
|
|
ResetReopen();
|
|
|
|
EnableDisable();
|
|
|
|
Update();
|
|
|
|
|
|
|
|
for (auto ao : outputs) {
|
|
|
|
if (ao->enabled)
|
|
|
|
enabled = true;
|
|
|
|
|
|
|
|
if (ao->open)
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:31:23 +01:00
|
|
|
if (!enabled) {
|
2014-01-27 08:20:25 +01:00
|
|
|
/* close all devices if there was an error */
|
|
|
|
Close();
|
2016-11-09 12:31:23 +01:00
|
|
|
throw std::runtime_error("All audio outputs are disabled");
|
|
|
|
} else if (!ret) {
|
|
|
|
/* close all devices if there was an error */
|
|
|
|
Close();
|
|
|
|
/* TODO: obtain real error */
|
|
|
|
throw std::runtime_error("Failed to open audio output");
|
|
|
|
}
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-12 15:56:41 +02:00
|
|
|
MultipleOutputs::IsChunkConsumed(const MusicChunk *chunk) const
|
2014-01-27 08:20:25 +01:00
|
|
|
{
|
2016-12-11 21:59:28 +01:00
|
|
|
for (auto ao : outputs)
|
|
|
|
if (!ao->LockIsChunkConsumed(*chunk))
|
2014-01-27 08:20:25 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2016-12-12 15:24:38 +01:00
|
|
|
MultipleOutputs::ClearTailChunk(const MusicChunk *chunk,
|
2014-01-27 08:20:25 +01:00
|
|
|
bool *locked)
|
|
|
|
{
|
|
|
|
assert(chunk->next == nullptr);
|
|
|
|
assert(pipe->Contains(chunk));
|
|
|
|
|
|
|
|
for (unsigned i = 0, n = outputs.size(); i != n; ++i) {
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput *ao = outputs[i];
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
/* this mutex will be unlocked by the caller when it's
|
|
|
|
ready */
|
|
|
|
ao->mutex.lock();
|
|
|
|
locked[i] = ao->open;
|
|
|
|
|
|
|
|
if (!locked[i]) {
|
|
|
|
ao->mutex.unlock();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-22 14:46:59 +01:00
|
|
|
ao->ClearTailChunk(*chunk);
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
MultipleOutputs::Check()
|
|
|
|
{
|
2014-08-12 15:56:41 +02:00
|
|
|
const MusicChunk *chunk;
|
2014-01-27 08:20:25 +01:00
|
|
|
bool is_tail;
|
2014-08-12 15:56:41 +02:00
|
|
|
MusicChunk *shifted;
|
2014-01-27 08:20:25 +01:00
|
|
|
bool locked[outputs.size()];
|
|
|
|
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
assert(pipe != nullptr);
|
|
|
|
|
|
|
|
while ((chunk = pipe->Peek()) != nullptr) {
|
|
|
|
assert(!pipe->IsEmpty());
|
|
|
|
|
|
|
|
if (!IsChunkConsumed(chunk))
|
|
|
|
/* at least one output is not finished playing
|
|
|
|
this chunk */
|
|
|
|
return pipe->GetSize();
|
|
|
|
|
2014-08-29 13:15:33 +02:00
|
|
|
if (chunk->length > 0 && !chunk->time.IsNegative())
|
2014-01-27 08:20:25 +01:00
|
|
|
/* only update elapsed_time if the chunk
|
|
|
|
provides a defined value */
|
2014-08-29 13:15:33 +02:00
|
|
|
elapsed_time = chunk->time;
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
is_tail = chunk->next == nullptr;
|
|
|
|
if (is_tail)
|
|
|
|
/* this is the tail of the pipe - clear the
|
|
|
|
chunk reference in all outputs */
|
|
|
|
ClearTailChunk(chunk, locked);
|
|
|
|
|
|
|
|
/* remove the chunk from the pipe */
|
|
|
|
shifted = pipe->Shift();
|
|
|
|
assert(shifted == chunk);
|
|
|
|
|
|
|
|
if (is_tail)
|
|
|
|
/* unlock all audio outputs which were locked
|
|
|
|
by clear_tail_chunk() */
|
|
|
|
for (unsigned i = 0, n = outputs.size(); i != n; ++i)
|
|
|
|
if (locked[i])
|
|
|
|
outputs[i]->mutex.unlock();
|
|
|
|
|
|
|
|
/* return the chunk to the buffer */
|
|
|
|
buffer->Return(shifted);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::Pause()
|
|
|
|
{
|
|
|
|
Update();
|
|
|
|
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockPauseAsync();
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
WaitAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::Drain()
|
|
|
|
{
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockDrainAsync();
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
WaitAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::Cancel()
|
|
|
|
{
|
|
|
|
/* send the cancel() command to all audio outputs */
|
|
|
|
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockCancelAsync();
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
WaitAll();
|
|
|
|
|
|
|
|
/* clear the music pipe and return all chunks to the buffer */
|
|
|
|
|
|
|
|
if (pipe != nullptr)
|
|
|
|
pipe->Clear(*buffer);
|
|
|
|
|
|
|
|
/* the audio outputs are now waiting for a signal, to
|
|
|
|
synchronize the cleared music pipe */
|
|
|
|
|
|
|
|
AllowPlay();
|
|
|
|
|
|
|
|
/* invalidate elapsed_time */
|
|
|
|
|
2014-08-29 13:13:08 +02:00
|
|
|
elapsed_time = SignedSongTime::Negative();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::Close()
|
|
|
|
{
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockCloseWait();
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
if (pipe != nullptr) {
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
|
|
|
|
pipe->Clear(*buffer);
|
|
|
|
delete pipe;
|
|
|
|
pipe = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = nullptr;
|
|
|
|
|
|
|
|
input_audio_format.Clear();
|
|
|
|
|
2014-08-29 13:13:08 +02:00
|
|
|
elapsed_time = SignedSongTime::Negative();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::Release()
|
|
|
|
{
|
|
|
|
for (auto ao : outputs)
|
2014-01-28 11:39:12 +01:00
|
|
|
ao->LockRelease();
|
2014-01-27 08:20:25 +01:00
|
|
|
|
|
|
|
if (pipe != nullptr) {
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
|
|
|
|
pipe->Clear(*buffer);
|
|
|
|
delete pipe;
|
|
|
|
pipe = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = nullptr;
|
|
|
|
|
|
|
|
input_audio_format.Clear();
|
|
|
|
|
2014-08-29 13:13:08 +02:00
|
|
|
elapsed_time = SignedSongTime::Negative();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultipleOutputs::SongBorder()
|
|
|
|
{
|
|
|
|
/* clear the elapsed_time pointer at the beginning of a new
|
|
|
|
song */
|
2014-08-29 13:13:08 +02:00
|
|
|
elapsed_time = SignedSongTime::zero();
|
2014-01-27 08:20:25 +01:00
|
|
|
}
|