2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2005-03-05 06:22:30 +01: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.
|
2005-03-05 06:22:30 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-29 14:32:32 +01:00
|
|
|
#include "AlsaOutputPlugin.hxx"
|
2017-10-26 09:06:40 +02:00
|
|
|
#include "lib/alsa/AllowedFormat.hxx"
|
2017-10-26 11:15:01 +02:00
|
|
|
#include "lib/alsa/HwSetup.hxx"
|
2017-01-24 23:10:35 +01:00
|
|
|
#include "lib/alsa/NonBlock.hxx"
|
2017-10-26 09:27:36 +02:00
|
|
|
#include "lib/alsa/PeriodBuffer.hxx"
|
2017-03-29 20:12:14 +02:00
|
|
|
#include "lib/alsa/Version.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
2013-04-09 01:24:32 +02:00
|
|
|
#include "pcm/PcmExport.hxx"
|
2017-06-08 22:23:26 +02:00
|
|
|
#include "thread/Mutex.hxx"
|
2017-04-28 21:45:47 +02:00
|
|
|
#include "thread/Cond.hxx"
|
2013-04-09 01:24:32 +02:00
|
|
|
#include "util/Manual.hxx"
|
2016-11-03 17:28:34 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2014-08-12 21:40:06 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2018-11-14 10:00:50 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2017-10-26 09:06:40 +02:00
|
|
|
#include "util/StringView.hxx"
|
2017-01-24 23:10:35 +01:00
|
|
|
#include "event/MultiSocketMonitor.hxx"
|
2017-11-12 17:22:28 +01:00
|
|
|
#include "event/DeferEvent.hxx"
|
2017-01-24 23:10:35 +01:00
|
|
|
#include "event/Call.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2008-10-26 21:58:37 +01:00
|
|
|
#include <alsa/asoundlib.h>
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
#include <boost/lockfree/spsc_queue.hpp>
|
|
|
|
|
2013-01-29 14:32:32 +01:00
|
|
|
#include <string>
|
2017-10-26 09:06:40 +02:00
|
|
|
#include <forward_list>
|
2013-01-29 14:32:32 +01:00
|
|
|
|
2008-09-08 20:42:39 +02:00
|
|
|
static const char default_device[] = "default";
|
|
|
|
|
2013-08-04 12:25:08 +02:00
|
|
|
static constexpr unsigned MPD_ALSA_BUFFER_TIME_US = 500000;
|
2008-12-01 22:37:05 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
class AlsaOutput final
|
2017-11-12 17:22:28 +01:00
|
|
|
: AudioOutput, MultiSocketMonitor {
|
|
|
|
|
|
|
|
DeferEvent defer_invalidate_sockets;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2013-04-09 01:24:32 +02:00
|
|
|
Manual<PcmExport> pcm_export;
|
2012-03-21 19:09:22 +01:00
|
|
|
|
2013-01-29 14:32:32 +01:00
|
|
|
/**
|
|
|
|
* The configured name of the ALSA device; empty for the
|
|
|
|
* default device
|
|
|
|
*/
|
2016-11-04 11:27:29 +01:00
|
|
|
const std::string device;
|
2008-10-14 17:21:49 +02:00
|
|
|
|
2016-02-26 18:41:52 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2012-03-21 10:36:19 +01:00
|
|
|
/**
|
2016-02-28 09:35:55 +01:00
|
|
|
* Enable DSD over PCM according to the DoP standard?
|
2012-03-21 10:36:19 +01:00
|
|
|
*
|
2014-08-31 16:12:26 +02:00
|
|
|
* @see http://dsd-guide.com/dop-open-standard
|
2012-03-21 10:36:19 +01:00
|
|
|
*/
|
2017-12-19 10:44:29 +01:00
|
|
|
bool dop_setting;
|
2016-02-26 18:41:52 +01:00
|
|
|
#endif
|
2012-03-21 10:36:19 +01:00
|
|
|
|
2009-01-25 13:13:24 +01:00
|
|
|
/** libasound's buffer_time setting (in microseconds) */
|
2016-11-04 11:27:29 +01:00
|
|
|
const unsigned buffer_time;
|
2009-01-25 13:13:24 +01:00
|
|
|
|
|
|
|
/** libasound's period_time setting (in microseconds) */
|
2016-11-04 11:27:29 +01:00
|
|
|
const unsigned period_time;
|
2009-01-25 13:13:24 +01:00
|
|
|
|
2008-10-14 17:21:49 +02:00
|
|
|
/** the mode flags passed to snd_pcm_open */
|
2016-10-28 22:56:27 +02:00
|
|
|
int mode = 0;
|
2008-10-14 17:21:49 +02:00
|
|
|
|
2017-10-26 09:06:40 +02:00
|
|
|
std::forward_list<Alsa::AllowedFormat> allowed_formats;
|
|
|
|
|
2017-12-19 10:44:29 +01:00
|
|
|
/**
|
|
|
|
* Protects #dop_setting and #allowed_formats.
|
|
|
|
*/
|
|
|
|
mutable Mutex attributes_mutex;
|
|
|
|
|
2009-01-25 13:13:24 +01:00
|
|
|
/** the libasound PCM device handle */
|
2009-01-25 13:05:16 +01:00
|
|
|
snd_pcm_t *pcm;
|
2009-01-25 13:13:24 +01:00
|
|
|
|
2017-10-26 08:17:21 +02:00
|
|
|
#ifndef NDEBUG
|
2012-03-22 01:01:11 +01:00
|
|
|
/**
|
|
|
|
* The size of one audio frame passed to method play().
|
|
|
|
*/
|
|
|
|
size_t in_frame_size;
|
2017-10-26 08:17:21 +02:00
|
|
|
#endif
|
2012-03-22 01:01:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of one audio frame passed to libasound.
|
|
|
|
*/
|
|
|
|
size_t out_frame_size;
|
2009-11-09 22:22:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of one period, in number of frames.
|
|
|
|
*/
|
|
|
|
snd_pcm_uframes_t period_frames;
|
|
|
|
|
2017-03-29 20:12:14 +02:00
|
|
|
/**
|
|
|
|
* Is this a buggy alsa-lib version, which needs a workaround
|
|
|
|
* for the snd_pcm_drain() bug always returning -EAGAIN? See
|
|
|
|
* alsa-lib commits fdc898d41135 and e4377b16454f for details.
|
|
|
|
* This bug was fixed in alsa-lib version 1.1.4.
|
|
|
|
*
|
|
|
|
* The workaround is to re-enable blocking mode for the
|
|
|
|
* snd_pcm_drain() call.
|
|
|
|
*/
|
|
|
|
bool work_around_drain_bug;
|
|
|
|
|
2009-11-09 22:22:31 +01:00
|
|
|
/**
|
2017-01-24 23:10:35 +01:00
|
|
|
* After Open(), has this output been activated by a Play()
|
|
|
|
* command?
|
2018-04-26 12:16:18 +02:00
|
|
|
*
|
2018-04-26 16:12:32 +02:00
|
|
|
* Protected by #mutex.
|
2009-11-09 22:22:31 +01:00
|
|
|
*/
|
2017-01-24 23:10:35 +01:00
|
|
|
bool active;
|
2013-01-29 14:32:32 +01:00
|
|
|
|
2013-11-04 23:26:24 +01:00
|
|
|
/**
|
2014-03-02 11:12:09 +01:00
|
|
|
* Do we need to call snd_pcm_prepare() before the next write?
|
|
|
|
* It means that we put the device to SND_PCM_STATE_SETUP by
|
|
|
|
* calling snd_pcm_drop().
|
|
|
|
*
|
|
|
|
* Without this flag, we could easily recover after a failed
|
|
|
|
* optimistic write (returning -EBADFD), but the Raspberry Pi
|
|
|
|
* audio driver is infamous for generating ugly artefacts from
|
|
|
|
* this.
|
2013-11-04 23:26:24 +01:00
|
|
|
*/
|
2014-03-02 11:12:09 +01:00
|
|
|
bool must_prepare;
|
2013-11-04 23:26:24 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
bool drain;
|
|
|
|
|
2013-02-04 14:35:53 +01:00
|
|
|
/**
|
|
|
|
* This buffer gets allocated after opening the ALSA device.
|
|
|
|
* It contains silence samples, enough to fill one period (see
|
|
|
|
* #period_frames).
|
|
|
|
*/
|
2013-12-14 22:17:19 +01:00
|
|
|
uint8_t *silence;
|
2013-02-04 14:35:53 +01:00
|
|
|
|
2018-11-11 07:34:08 +01:00
|
|
|
AlsaNonBlockPcm non_block;
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For copying data from OutputThread to IOThread.
|
|
|
|
*/
|
|
|
|
boost::lockfree::spsc_queue<uint8_t> *ring_buffer;
|
|
|
|
|
2017-10-26 09:27:36 +02:00
|
|
|
Alsa::PeriodBuffer period_buffer;
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-26 16:12:32 +02:00
|
|
|
* Protects #cond, #error, #active, #drain.
|
2017-01-24 23:10:35 +01:00
|
|
|
*/
|
|
|
|
mutable Mutex mutex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to wait when #ring_buffer is full. It will be
|
|
|
|
* signalled each time data is popped from the #ring_buffer,
|
|
|
|
* making space for more data.
|
|
|
|
*/
|
|
|
|
Cond cond;
|
|
|
|
|
|
|
|
std::exception_ptr error;
|
|
|
|
|
2017-01-24 22:20:09 +01:00
|
|
|
public:
|
2017-01-24 23:10:35 +01:00
|
|
|
AlsaOutput(EventLoop &loop, const ConfigBlock &block);
|
2013-01-29 14:32:32 +01:00
|
|
|
|
2017-10-26 08:18:05 +02:00
|
|
|
~AlsaOutput() noexcept {
|
2015-01-04 19:53:56 +01:00
|
|
|
/* free libasound's config cache */
|
|
|
|
snd_config_update_free_global();
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:54:01 +02:00
|
|
|
using MultiSocketMonitor::GetEventLoop;
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
gcc_pure
|
2017-06-03 21:33:44 +02:00
|
|
|
const char *GetDevice() const noexcept {
|
2015-01-04 19:53:56 +01:00
|
|
|
return device.empty() ? default_device : device.c_str();
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
static AudioOutput *Create(EventLoop &event_loop,
|
|
|
|
const ConfigBlock &block) {
|
|
|
|
return new AlsaOutput(event_loop, block);
|
|
|
|
}
|
2015-01-04 19:53:56 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
private:
|
2017-12-19 10:44:29 +01:00
|
|
|
const std::map<std::string, std::string> GetAttributes() const noexcept override;
|
|
|
|
void SetAttribute(std::string &&name, std::string &&value) override;
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Enable() override;
|
|
|
|
void Disable() noexcept override;
|
2015-01-04 19:53:56 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
2015-01-04 19:53:56 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
|
|
|
void Drain() override;
|
|
|
|
void Cancel() noexcept override;
|
2015-01-04 19:53:56 +01:00
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
/**
|
|
|
|
* Set up the snd_pcm_t object which was opened by the caller.
|
|
|
|
* Set up the configured settings and the audio format.
|
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*/
|
|
|
|
void Setup(AudioFormat &audio_format, PcmExport::Params ¶ms);
|
|
|
|
|
2016-02-26 18:41:52 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2016-11-03 17:28:34 +01:00
|
|
|
void SetupDop(AudioFormat audio_format,
|
|
|
|
PcmExport::Params ¶ms);
|
2016-02-26 18:41:52 +01:00
|
|
|
#endif
|
|
|
|
|
2017-10-26 09:06:40 +02:00
|
|
|
void SetupOrDop(AudioFormat &audio_format, PcmExport::Params ¶ms
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
, bool dop
|
|
|
|
#endif
|
|
|
|
);
|
2015-01-04 19:53:56 +01:00
|
|
|
|
2018-04-26 16:12:32 +02:00
|
|
|
gcc_pure
|
|
|
|
bool LockIsActive() const noexcept {
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
/**
|
|
|
|
* Activate the output by registering the sockets in the
|
|
|
|
* #EventLoop. Before calling this, filling the ring buffer
|
|
|
|
* has no effect; nothing will be played, and no code will be
|
|
|
|
* run on #EventLoop's thread.
|
2018-04-26 16:07:15 +02:00
|
|
|
*
|
|
|
|
* Caller must hold the mutex.
|
2017-11-14 21:02:53 +01:00
|
|
|
*
|
|
|
|
* @return true if Activate() was called, false if the mutex
|
|
|
|
* was never unlocked
|
2017-01-24 23:10:35 +01:00
|
|
|
*/
|
2018-04-26 16:07:15 +02:00
|
|
|
bool Activate() noexcept {
|
2017-01-24 23:10:35 +01:00
|
|
|
if (active)
|
2017-11-14 21:02:53 +01:00
|
|
|
return false;
|
2017-01-24 23:10:35 +01:00
|
|
|
|
2018-04-26 16:07:15 +02:00
|
|
|
active = true;
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2018-04-26 16:07:15 +02:00
|
|
|
defer_invalidate_sockets.Schedule();
|
2017-11-14 21:02:53 +01:00
|
|
|
return true;
|
2017-01-24 23:10:35 +01:00
|
|
|
}
|
|
|
|
|
2017-10-26 08:18:05 +02:00
|
|
|
int Recover(int err) noexcept;
|
2015-01-04 19:53:56 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-24 23:10:35 +01:00
|
|
|
* Drain all buffers. To be run in #EventLoop's thread.
|
|
|
|
*
|
2018-11-14 10:04:10 +01:00
|
|
|
* Throws on error.
|
|
|
|
*
|
2017-01-24 23:10:35 +01:00
|
|
|
* @return true if draining is complete, false if this method
|
|
|
|
* needs to be called again later
|
2015-01-04 19:53:56 +01:00
|
|
|
*/
|
2018-11-14 10:04:10 +01:00
|
|
|
bool DrainInternal();
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop playback immediately, dropping all buffers. To be run
|
|
|
|
* in #EventLoop's thread.
|
|
|
|
*/
|
2017-10-26 08:18:05 +02:00
|
|
|
void CancelInternal() noexcept;
|
2017-01-24 23:10:35 +01:00
|
|
|
|
2018-04-26 10:32:03 +02:00
|
|
|
/**
|
|
|
|
* @return false if no data was moved
|
|
|
|
*/
|
|
|
|
bool CopyRingToPeriodBuffer() noexcept {
|
2017-01-24 23:10:35 +01:00
|
|
|
if (period_buffer.IsFull())
|
2018-04-26 10:32:03 +02:00
|
|
|
return false;
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
size_t nbytes = ring_buffer->pop(period_buffer.GetTail(),
|
|
|
|
period_buffer.GetSpaceBytes());
|
|
|
|
if (nbytes == 0)
|
2018-04-26 10:32:03 +02:00
|
|
|
return false;
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
period_buffer.AppendBytes(nbytes);
|
|
|
|
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
/* notify the OutputThread that there is now
|
|
|
|
room in ring_buffer */
|
|
|
|
cond.signal();
|
2018-04-26 10:32:03 +02:00
|
|
|
|
|
|
|
return true;
|
2015-01-04 19:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-10-26 08:18:05 +02:00
|
|
|
snd_pcm_sframes_t WriteFromPeriodBuffer() noexcept {
|
2017-01-24 23:10:35 +01:00
|
|
|
assert(!period_buffer.IsEmpty());
|
|
|
|
|
|
|
|
auto frames_written = snd_pcm_writei(pcm, period_buffer.GetHead(),
|
|
|
|
period_buffer.GetFrames(out_frame_size));
|
|
|
|
if (frames_written > 0)
|
|
|
|
period_buffer.ConsumeFrames(frames_written,
|
|
|
|
out_frame_size);
|
|
|
|
|
|
|
|
return frames_written;
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:07:59 +01:00
|
|
|
void LockCaughtError() noexcept {
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
error = std::current_exception();
|
2018-04-26 16:14:27 +02:00
|
|
|
active = false;
|
2017-11-14 21:07:59 +01:00
|
|
|
cond.signal();
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
/* virtual methods from class MultiSocketMonitor */
|
2017-11-12 17:40:12 +01:00
|
|
|
std::chrono::steady_clock::duration PrepareSockets() noexcept override;
|
|
|
|
void DispatchSockets() noexcept override;
|
2009-01-25 13:05:16 +01:00
|
|
|
};
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain alsa_output_domain("alsa_output");
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2017-11-12 17:22:28 +01:00
|
|
|
AlsaOutput::AlsaOutput(EventLoop &_loop, const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(FLAG_ENABLE_DISABLE),
|
2017-11-12 17:22:28 +01:00
|
|
|
MultiSocketMonitor(_loop),
|
|
|
|
defer_invalidate_sockets(_loop, BIND_THIS_METHOD(InvalidateSockets)),
|
2016-11-04 11:27:29 +01:00
|
|
|
device(block.GetBlockValue("device", "")),
|
2016-02-26 18:41:52 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2017-10-26 09:06:40 +02:00
|
|
|
dop_setting(block.GetBlockValue("dop", false) ||
|
|
|
|
/* legacy name from MPD 0.18 and older: */
|
|
|
|
block.GetBlockValue("dsd_usb", false)),
|
2016-02-26 18:41:52 +01:00
|
|
|
#endif
|
2018-01-02 17:22:25 +01:00
|
|
|
buffer_time(block.GetPositiveValue("buffer_time",
|
|
|
|
MPD_ALSA_BUFFER_TIME_US)),
|
|
|
|
period_time(block.GetPositiveValue("period_time", 0u))
|
2016-11-04 11:27:29 +01:00
|
|
|
{
|
2008-10-14 22:37:27 +02:00
|
|
|
#ifdef SND_PCM_NO_AUTO_RESAMPLE
|
2015-01-21 22:13:44 +01:00
|
|
|
if (!block.GetBlockValue("auto_resample", true))
|
2014-08-26 06:52:39 +02:00
|
|
|
mode |= SND_PCM_NO_AUTO_RESAMPLE;
|
2008-10-14 22:37:27 +02:00
|
|
|
#endif
|
2008-10-14 17:21:49 +02:00
|
|
|
|
2008-10-14 22:37:27 +02:00
|
|
|
#ifdef SND_PCM_NO_AUTO_CHANNELS
|
2015-01-21 22:13:44 +01:00
|
|
|
if (!block.GetBlockValue("auto_channels", true))
|
2014-08-26 06:52:39 +02:00
|
|
|
mode |= SND_PCM_NO_AUTO_CHANNELS;
|
2008-10-14 22:37:27 +02:00
|
|
|
#endif
|
2008-10-14 17:21:49 +02:00
|
|
|
|
2008-10-14 22:37:27 +02:00
|
|
|
#ifdef SND_PCM_NO_AUTO_FORMAT
|
2015-01-21 22:13:44 +01:00
|
|
|
if (!block.GetBlockValue("auto_format", true))
|
2014-08-26 06:52:39 +02:00
|
|
|
mode |= SND_PCM_NO_AUTO_FORMAT;
|
2008-10-14 22:37:27 +02:00
|
|
|
#endif
|
2017-10-26 09:06:40 +02:00
|
|
|
|
|
|
|
const char *allowed_formats_string =
|
|
|
|
block.GetBlockValue("allowed_formats", nullptr);
|
|
|
|
if (allowed_formats_string != nullptr)
|
|
|
|
allowed_formats = Alsa::AllowedFormat::ParseList(allowed_formats_string);
|
2008-10-12 11:47:33 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 10:44:29 +01:00
|
|
|
const std::map<std::string, std::string>
|
|
|
|
AlsaOutput::GetAttributes() const noexcept
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(attributes_mutex);
|
|
|
|
|
|
|
|
return {
|
|
|
|
std::make_pair("allowed_formats",
|
|
|
|
Alsa::ToString(allowed_formats)),
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
std::make_pair("dop", dop_setting ? "1" : "0"),
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaOutput::SetAttribute(std::string &&name, std::string &&value)
|
|
|
|
{
|
|
|
|
if (name == "allowed_formats") {
|
|
|
|
const std::lock_guard<Mutex> lock(attributes_mutex);
|
|
|
|
allowed_formats = Alsa::AllowedFormat::ParseList({value.data(), value.length()});
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
} else if (name == "dop") {
|
|
|
|
const std::lock_guard<Mutex> lock(attributes_mutex);
|
|
|
|
if (value == "0")
|
|
|
|
dop_setting = false;
|
|
|
|
else if (value == "1")
|
|
|
|
dop_setting = true;
|
|
|
|
else
|
|
|
|
throw std::invalid_argument("Bad 'dop' value");
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
AudioOutput::SetAttribute(std::move(name), std::move(value));
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
2016-11-09 11:56:01 +01:00
|
|
|
AlsaOutput::Enable()
|
2012-03-21 19:09:22 +01:00
|
|
|
{
|
2015-01-04 19:53:56 +01:00
|
|
|
pcm_export.Construct();
|
2012-03-21 19:09:22 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
AlsaOutput::Disable() noexcept
|
2012-03-21 19:09:22 +01:00
|
|
|
{
|
2015-01-04 19:53:56 +01:00
|
|
|
pcm_export.Destruct();
|
2012-03-21 19:09:22 +01:00
|
|
|
}
|
|
|
|
|
2009-01-25 13:05:16 +01:00
|
|
|
static bool
|
2014-08-26 10:25:59 +02:00
|
|
|
alsa_test_default_device()
|
2005-03-12 04:10:09 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
snd_pcm_t *handle;
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2008-09-08 20:42:39 +02:00
|
|
|
int ret = snd_pcm_open(&handle, default_device,
|
2014-08-26 10:25:59 +02:00
|
|
|
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(alsa_output_domain,
|
|
|
|
"Error opening default ALSA device: %s",
|
|
|
|
snd_strerror(-ret));
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
snd_pcm_close(handle);
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 22:58:09 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for snd_pcm_sw_params().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
AlsaSetupSw(snd_pcm_t *pcm, snd_pcm_uframes_t start_threshold,
|
|
|
|
snd_pcm_uframes_t avail_min)
|
|
|
|
{
|
|
|
|
snd_pcm_sw_params_t *swparams;
|
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
|
|
|
|
|
|
|
int err = snd_pcm_sw_params_current(pcm, swparams);
|
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_sw_params_current() failed: %s",
|
|
|
|
snd_strerror(-err));
|
|
|
|
|
|
|
|
err = snd_pcm_sw_params_set_start_threshold(pcm, swparams,
|
|
|
|
start_threshold);
|
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_sw_params_set_start_threshold() failed: %s",
|
|
|
|
snd_strerror(-err));
|
|
|
|
|
|
|
|
err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min);
|
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_sw_params_set_avail_min() failed: %s",
|
|
|
|
snd_strerror(-err));
|
|
|
|
|
|
|
|
err = snd_pcm_sw_params(pcm, swparams);
|
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_sw_params() failed: %s",
|
|
|
|
snd_strerror(-err));
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
inline void
|
|
|
|
AlsaOutput::Setup(AudioFormat &audio_format,
|
|
|
|
PcmExport::Params ¶ms)
|
2017-01-24 22:48:48 +01:00
|
|
|
{
|
2017-10-26 12:13:00 +02:00
|
|
|
const auto hw_result = Alsa::SetupHw(pcm,
|
|
|
|
buffer_time, period_time,
|
|
|
|
audio_format, params);
|
2017-01-24 22:48:48 +01:00
|
|
|
|
2017-10-26 12:13:00 +02:00
|
|
|
FormatDebug(alsa_output_domain, "format=%s (%s)",
|
|
|
|
snd_pcm_format_name(hw_result.format),
|
|
|
|
snd_pcm_format_description(hw_result.format));
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain, "buffer_size=%u period_size=%u",
|
2017-10-26 12:13:00 +02:00
|
|
|
(unsigned)hw_result.buffer_size,
|
|
|
|
(unsigned)hw_result.period_size);
|
|
|
|
|
|
|
|
AlsaSetupSw(pcm, hw_result.buffer_size - hw_result.period_size,
|
|
|
|
hw_result.period_size);
|
2009-03-03 22:19:37 +01:00
|
|
|
|
2017-10-26 12:13:00 +02:00
|
|
|
auto alsa_period_size = hw_result.period_size;
|
2011-07-20 06:54:51 +02:00
|
|
|
if (alsa_period_size == 0)
|
|
|
|
/* this works around a SIGFPE bug that occurred when
|
|
|
|
an ALSA driver indicated period_size==0; this
|
|
|
|
caused a division by zero in alsa_play(). By using
|
|
|
|
the fallback "1", we make sure that this won't
|
|
|
|
happen again. */
|
|
|
|
alsa_period_size = 1;
|
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
period_frames = alsa_period_size;
|
2009-11-09 22:22:31 +01:00
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
silence = new uint8_t[snd_pcm_frames_to_bytes(pcm, alsa_period_size)];
|
2017-10-26 12:13:00 +02:00
|
|
|
snd_pcm_format_set_silence(hw_result.format, silence,
|
2017-01-24 22:48:48 +01:00
|
|
|
alsa_period_size * audio_format.channels);
|
2013-02-04 14:35:53 +01:00
|
|
|
|
2009-02-25 22:01:30 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 18:41:52 +01:00
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
|
2016-11-03 17:28:34 +01:00
|
|
|
inline void
|
2015-01-04 19:53:56 +01:00
|
|
|
AlsaOutput::SetupDop(const AudioFormat audio_format,
|
2016-11-03 17:28:34 +01:00
|
|
|
PcmExport::Params ¶ms)
|
2012-03-21 10:36:19 +01:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(audio_format.format == SampleFormat::DSD);
|
2012-03-21 10:36:19 +01:00
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
/* pass 24 bit to AlsaSetup() */
|
2012-03-21 10:36:19 +01:00
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
AudioFormat dop_format = audio_format;
|
|
|
|
dop_format.format = SampleFormat::S24_P32;
|
2012-03-21 10:36:19 +01:00
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
const AudioFormat check = dop_format;
|
2012-03-21 10:36:19 +01:00
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
Setup(dop_format, params);
|
2012-03-21 10:36:19 +01:00
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
/* if the device allows only 32 bit, shift all DoP
|
2012-03-27 01:05:33 +02:00
|
|
|
samples left by 8 bit and leave the lower 8 bit cleared;
|
|
|
|
the DSD-over-USB documentation does not specify whether
|
|
|
|
this is legal, but there is anecdotical evidence that this
|
|
|
|
is possible (and the only option for some devices) */
|
2016-02-27 07:34:58 +01:00
|
|
|
params.shift8 = dop_format.format == SampleFormat::S32;
|
2014-08-31 16:12:26 +02:00
|
|
|
if (dop_format.format == SampleFormat::S32)
|
|
|
|
dop_format.format = SampleFormat::S24_P32;
|
2012-03-27 01:05:33 +02:00
|
|
|
|
2014-08-31 16:12:26 +02:00
|
|
|
if (dop_format != check) {
|
2012-03-21 10:36:19 +01:00
|
|
|
/* no bit-perfect playback, which is required
|
|
|
|
for DSD over USB */
|
2015-01-04 19:53:56 +01:00
|
|
|
delete[] silence;
|
2016-11-03 17:28:34 +01:00
|
|
|
throw std::runtime_error("Failed to configure DSD-over-PCM");
|
2012-03-21 10:36:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 18:41:52 +01:00
|
|
|
#endif
|
|
|
|
|
2016-11-03 17:28:34 +01:00
|
|
|
inline void
|
2017-10-26 09:06:40 +02:00
|
|
|
AlsaOutput::SetupOrDop(AudioFormat &audio_format, PcmExport::Params ¶ms
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
, bool dop
|
|
|
|
#endif
|
|
|
|
)
|
2012-03-21 10:36:19 +01:00
|
|
|
{
|
2016-02-26 18:41:52 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2016-11-03 17:28:34 +01:00
|
|
|
std::exception_ptr dop_error;
|
|
|
|
if (dop && audio_format.format == SampleFormat::DSD) {
|
|
|
|
try {
|
|
|
|
params.dop = true;
|
2017-01-11 00:11:43 +01:00
|
|
|
SetupDop(audio_format, params);
|
2016-11-03 17:28:34 +01:00
|
|
|
return;
|
|
|
|
} catch (...) {
|
|
|
|
dop_error = std::current_exception();
|
2017-01-11 00:11:43 +01:00
|
|
|
params.dop = false;
|
2016-11-03 17:28:34 +01:00
|
|
|
}
|
2016-02-28 10:15:54 +01:00
|
|
|
}
|
2016-02-28 10:15:02 +01:00
|
|
|
|
2016-11-03 17:28:34 +01:00
|
|
|
try {
|
|
|
|
#endif
|
2017-01-24 23:06:33 +01:00
|
|
|
Setup(audio_format, params);
|
2016-02-28 10:15:02 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2016-11-03 17:28:34 +01:00
|
|
|
} catch (...) {
|
|
|
|
if (dop_error)
|
|
|
|
/* if DoP was attempted, prefer returning the
|
|
|
|
original DoP error instead of the fallback
|
|
|
|
error */
|
|
|
|
std::rethrow_exception(dop_error);
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2016-02-28 10:15:02 +01:00
|
|
|
#endif
|
2012-03-21 10:36:19 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 20:12:14 +02:00
|
|
|
static constexpr bool
|
|
|
|
MaybeDmix(snd_pcm_type_t type)
|
|
|
|
{
|
|
|
|
return type == SND_PCM_TYPE_DMIX || type == SND_PCM_TYPE_PLUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-15 23:05:45 +02:00
|
|
|
MaybeDmix(snd_pcm_t *pcm) noexcept
|
2017-03-29 20:12:14 +02:00
|
|
|
{
|
|
|
|
return MaybeDmix(snd_pcm_type(pcm));
|
|
|
|
}
|
|
|
|
|
2017-10-26 09:06:40 +02:00
|
|
|
static const Alsa::AllowedFormat &
|
|
|
|
BestMatch(const std::forward_list<Alsa::AllowedFormat> &haystack,
|
|
|
|
const AudioFormat &needle)
|
|
|
|
{
|
|
|
|
assert(!haystack.empty());
|
|
|
|
|
|
|
|
for (const auto &i : haystack)
|
|
|
|
if (needle.MatchMask(i.format))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return haystack.front();
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
2016-11-09 11:56:01 +01:00
|
|
|
AlsaOutput::Open(AudioFormat &audio_format)
|
2009-02-25 22:01:30 +01:00
|
|
|
{
|
2017-10-26 09:06:40 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2017-12-19 10:44:29 +01:00
|
|
|
bool dop;
|
2017-10-26 09:06:40 +02:00
|
|
|
#endif
|
|
|
|
|
2017-12-19 10:44:29 +01:00
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(attributes_mutex);
|
2017-10-26 09:06:40 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2017-12-19 10:44:29 +01:00
|
|
|
dop = dop_setting;
|
2017-10-26 09:06:40 +02:00
|
|
|
#endif
|
2017-12-19 10:44:29 +01:00
|
|
|
|
|
|
|
if (!allowed_formats.empty()) {
|
|
|
|
const auto &a = BestMatch(allowed_formats,
|
|
|
|
audio_format);
|
|
|
|
audio_format.ApplyMask(a.format);
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
dop = a.dop;
|
|
|
|
#endif
|
|
|
|
}
|
2017-10-26 09:06:40 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
int err = snd_pcm_open(&pcm, GetDevice(),
|
|
|
|
SND_PCM_STREAM_PLAYBACK, mode);
|
2016-11-03 17:28:34 +01:00
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("Failed to open ALSA device \"%s\": %s",
|
|
|
|
GetDevice(), snd_strerror(err));
|
2009-02-25 22:01:30 +01:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain, "opened %s type=%s",
|
2015-01-04 19:53:56 +01:00
|
|
|
snd_pcm_name(pcm),
|
|
|
|
snd_pcm_type_name(snd_pcm_type(pcm)));
|
2012-03-26 23:49:16 +02:00
|
|
|
|
2016-02-28 10:15:54 +01:00
|
|
|
PcmExport::Params params;
|
|
|
|
params.alsa_channel_order = true;
|
|
|
|
|
2016-11-03 17:28:34 +01:00
|
|
|
try {
|
2017-10-26 09:06:40 +02:00
|
|
|
SetupOrDop(audio_format, params
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
, dop
|
|
|
|
#endif
|
|
|
|
);
|
2016-11-03 17:28:34 +01:00
|
|
|
} catch (...) {
|
2015-01-04 19:53:56 +01:00
|
|
|
snd_pcm_close(pcm);
|
2016-11-03 17:28:34 +01:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Error opening ALSA device \"%s\"",
|
|
|
|
GetDevice()));
|
2009-02-25 22:01:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 20:12:14 +02:00
|
|
|
work_around_drain_bug = MaybeDmix(pcm) &&
|
|
|
|
GetRuntimeAlsaVersion() < MakeAlsaVersion(1, 1, 4);
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
snd_pcm_nonblock(pcm, 1);
|
|
|
|
|
2017-01-11 22:50:40 +01:00
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
if (params.dop)
|
|
|
|
FormatDebug(alsa_output_domain, "DoP (DSD over PCM) enabled");
|
|
|
|
#endif
|
|
|
|
|
2016-02-28 10:15:54 +01:00
|
|
|
pcm_export->Open(audio_format.format,
|
|
|
|
audio_format.channels,
|
|
|
|
params);
|
|
|
|
|
2017-10-26 08:17:21 +02:00
|
|
|
#ifndef NDEBUG
|
2015-01-04 19:53:56 +01:00
|
|
|
in_frame_size = audio_format.GetFrameSize();
|
2017-10-26 08:17:21 +02:00
|
|
|
#endif
|
2015-01-04 19:53:56 +01:00
|
|
|
out_frame_size = pcm_export->GetFrameSize(audio_format);
|
2009-02-25 22:01:30 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
drain = false;
|
|
|
|
|
|
|
|
size_t period_size = period_frames * out_frame_size;
|
|
|
|
ring_buffer = new boost::lockfree::spsc_queue<uint8_t>(period_size * 4);
|
|
|
|
|
|
|
|
period_buffer.Allocate(period_frames, out_frame_size);
|
|
|
|
|
|
|
|
active = false;
|
2015-01-04 19:53:56 +01:00
|
|
|
must_prepare = false;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
inline int
|
2017-10-26 08:18:05 +02:00
|
|
|
AlsaOutput::Recover(int err) noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (err == -EPIPE) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain,
|
2015-01-04 19:53:56 +01:00
|
|
|
"Underrun on ALSA device \"%s\"",
|
|
|
|
GetDevice());
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (err == -ESTRPIPE) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain,
|
|
|
|
"ALSA device \"%s\" was suspended",
|
2015-01-04 19:53:56 +01:00
|
|
|
GetDevice());
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
switch (snd_pcm_state(pcm)) {
|
2005-03-05 21:51:36 +01:00
|
|
|
case SND_PCM_STATE_PAUSED:
|
2015-01-04 19:53:56 +01:00
|
|
|
err = snd_pcm_pause(pcm, /* disable */ 0);
|
2005-03-05 21:51:36 +01:00
|
|
|
break;
|
|
|
|
case SND_PCM_STATE_SUSPENDED:
|
2015-01-04 19:53:56 +01:00
|
|
|
err = snd_pcm_resume(pcm);
|
2008-09-08 09:45:05 +02:00
|
|
|
if (err == -EAGAIN)
|
|
|
|
return 0;
|
|
|
|
/* fall-through to snd_pcm_prepare: */
|
2016-11-16 19:50:38 +01:00
|
|
|
#if GCC_CHECK_VERSION(7,0)
|
|
|
|
[[fallthrough]];
|
|
|
|
#endif
|
2017-01-14 20:48:55 +01:00
|
|
|
case SND_PCM_STATE_OPEN:
|
2005-03-05 15:01:13 +01:00
|
|
|
case SND_PCM_STATE_SETUP:
|
|
|
|
case SND_PCM_STATE_XRUN:
|
2017-01-24 23:10:35 +01:00
|
|
|
period_buffer.Rewind();
|
2015-01-04 19:53:56 +01:00
|
|
|
err = snd_pcm_prepare(pcm);
|
2005-03-05 21:51:36 +01:00
|
|
|
break;
|
2006-07-16 18:52:29 +02:00
|
|
|
case SND_PCM_STATE_DISCONNECTED:
|
|
|
|
break;
|
2008-04-12 06:07:44 +02:00
|
|
|
/* this is no error, so just keep running */
|
2017-01-14 20:48:55 +01:00
|
|
|
case SND_PCM_STATE_PREPARED:
|
2008-04-12 06:07:44 +02:00
|
|
|
case SND_PCM_STATE_RUNNING:
|
2017-01-14 20:48:55 +01:00
|
|
|
case SND_PCM_STATE_DRAINING:
|
2008-04-12 06:07:44 +02:00
|
|
|
err = 0;
|
|
|
|
break;
|
2018-05-03 11:59:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* this default case is just here to work around
|
|
|
|
-Wswitch due to SND_PCM_STATE_PRIVATE1 (libasound
|
|
|
|
1.1.6) */
|
|
|
|
break;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
inline bool
|
2018-11-14 10:04:10 +01:00
|
|
|
AlsaOutput::DrainInternal()
|
2009-10-29 15:59:40 +01:00
|
|
|
{
|
2017-01-24 23:10:35 +01:00
|
|
|
/* drain ring_buffer */
|
|
|
|
CopyRingToPeriodBuffer();
|
|
|
|
|
|
|
|
auto period_position = period_buffer.GetPeriodPosition(out_frame_size);
|
|
|
|
if (period_position > 0)
|
2009-11-09 22:22:31 +01:00
|
|
|
/* generate some silence to finish the partial
|
|
|
|
period */
|
2017-01-24 23:10:35 +01:00
|
|
|
period_buffer.FillWithSilence(silence, out_frame_size);
|
|
|
|
|
|
|
|
/* drain period_buffer */
|
|
|
|
if (!period_buffer.IsEmpty()) {
|
|
|
|
auto frames_written = WriteFromPeriodBuffer();
|
|
|
|
if (frames_written < 0 && errno != EAGAIN) {
|
|
|
|
CancelInternal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!period_buffer.IsEmpty())
|
|
|
|
/* need to call WriteFromPeriodBuffer() again
|
|
|
|
in the next iteration, so don't finish the
|
|
|
|
drain just yet */
|
|
|
|
return false;
|
2009-11-09 22:22:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
/* .. and finally drain the ALSA hardware buffer */
|
2017-03-29 20:12:14 +02:00
|
|
|
|
2018-11-14 10:00:50 +01:00
|
|
|
int result;
|
2017-03-29 20:12:14 +02:00
|
|
|
if (work_around_drain_bug) {
|
|
|
|
snd_pcm_nonblock(pcm, 0);
|
2018-11-14 10:00:50 +01:00
|
|
|
result = snd_pcm_drain(pcm);
|
2017-03-29 20:12:14 +02:00
|
|
|
snd_pcm_nonblock(pcm, 1);
|
2018-11-14 10:00:50 +01:00
|
|
|
} else
|
|
|
|
result = snd_pcm_drain(pcm);
|
2017-03-29 20:12:14 +02:00
|
|
|
|
2018-11-14 10:04:10 +01:00
|
|
|
if (result == 0)
|
|
|
|
return true;
|
|
|
|
else if (result == -EAGAIN)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
throw FormatRuntimeError("snd_pcm_drain() failed: %s",
|
|
|
|
snd_strerror(-result));
|
2017-01-24 23:10:35 +01:00
|
|
|
}
|
2009-11-09 22:22:31 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
2017-01-24 23:10:35 +01:00
|
|
|
AlsaOutput::Drain()
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
|
2018-04-26 16:17:14 +02:00
|
|
|
if (error)
|
|
|
|
std::rethrow_exception(error);
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
drain = true;
|
|
|
|
|
2018-04-26 16:07:15 +02:00
|
|
|
Activate();
|
2017-01-24 23:10:35 +01:00
|
|
|
|
2018-04-26 16:14:27 +02:00
|
|
|
while (drain && active)
|
2017-01-24 23:10:35 +01:00
|
|
|
cond.wait(mutex);
|
2018-04-26 16:14:27 +02:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
std::rethrow_exception(error);
|
2009-10-29 15:59:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
inline void
|
2017-10-26 08:18:05 +02:00
|
|
|
AlsaOutput::CancelInternal() noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2015-01-04 19:53:56 +01:00
|
|
|
must_prepare = true;
|
2005-12-12 04:22:27 +01:00
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
snd_pcm_drop(pcm);
|
2017-01-11 15:41:28 +01:00
|
|
|
|
|
|
|
pcm_export->Reset();
|
2017-01-24 23:10:35 +01:00
|
|
|
period_buffer.Clear();
|
2018-07-16 10:15:01 +02:00
|
|
|
ring_buffer->reset();
|
2018-04-26 12:28:10 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiSocketMonitor::Reset();
|
|
|
|
defer_invalidate_sockets.Cancel();
|
2017-01-24 23:10:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
AlsaOutput::Cancel() noexcept
|
2017-01-24 23:10:35 +01:00
|
|
|
{
|
2018-04-26 16:12:32 +02:00
|
|
|
if (!LockIsActive()) {
|
2017-01-24 23:10:35 +01:00
|
|
|
/* early cancel, quick code path without thread
|
|
|
|
synchronization */
|
|
|
|
|
|
|
|
pcm_export->Reset();
|
|
|
|
assert(period_buffer.IsEmpty());
|
2018-07-16 10:15:01 +02:00
|
|
|
ring_buffer->reset();
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:54:01 +02:00
|
|
|
BlockingCall(GetEventLoop(), [this](){
|
2017-01-24 23:10:35 +01:00
|
|
|
CancelInternal();
|
|
|
|
});
|
2005-12-12 04:22:27 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
AlsaOutput::Close() noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2017-01-24 23:10:35 +01:00
|
|
|
/* make sure the I/O thread isn't inside DispatchSockets() */
|
2018-04-26 15:54:01 +02:00
|
|
|
BlockingCall(GetEventLoop(), [this](){
|
2017-01-24 23:10:35 +01:00
|
|
|
MultiSocketMonitor::Reset();
|
2017-11-12 17:22:28 +01:00
|
|
|
defer_invalidate_sockets.Cancel();
|
2017-01-24 23:10:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
period_buffer.Free();
|
|
|
|
delete ring_buffer;
|
2015-01-04 19:53:56 +01:00
|
|
|
snd_pcm_close(pcm);
|
|
|
|
delete[] silence;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t
|
2017-01-24 23:10:35 +01:00
|
|
|
AlsaOutput::Play(const void *chunk, size_t size)
|
2017-01-11 21:31:12 +01:00
|
|
|
{
|
2017-01-24 23:10:35 +01:00
|
|
|
assert(size > 0);
|
|
|
|
assert(size % in_frame_size == 0);
|
2017-01-11 21:31:12 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
const auto e = pcm_export->Export({chunk, size});
|
|
|
|
if (e.size == 0)
|
|
|
|
/* the DoP (DSD over PCM) filter converts two frames
|
|
|
|
at a time and ignores the last odd frame; if there
|
|
|
|
was only one frame (e.g. the last frame in the
|
|
|
|
file), the result is empty; to avoid an endless
|
|
|
|
loop, bail out here, and pretend the one frame has
|
|
|
|
been played */
|
|
|
|
return size;
|
2017-01-11 21:31:12 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2017-01-11 21:31:12 +01:00
|
|
|
|
|
|
|
while (true) {
|
2017-01-24 23:10:35 +01:00
|
|
|
if (error)
|
|
|
|
std::rethrow_exception(error);
|
|
|
|
|
2017-02-10 22:23:00 +01:00
|
|
|
size_t bytes_written = ring_buffer->push((const uint8_t *)e.data,
|
|
|
|
e.size);
|
2017-01-24 23:10:35 +01:00
|
|
|
if (bytes_written > 0)
|
|
|
|
return pcm_export->CalcSourceSize(bytes_written);
|
|
|
|
|
|
|
|
/* now that the ring_buffer is full, we can activate
|
|
|
|
the socket handlers to trigger the first
|
|
|
|
snd_pcm_writei() */
|
2018-04-26 16:07:15 +02:00
|
|
|
if (Activate())
|
2017-11-14 21:02:53 +01:00
|
|
|
/* since everything may have changed while the
|
|
|
|
mutex was unlocked, we need to skip the
|
|
|
|
cond.wait() call below and check the new
|
|
|
|
status */
|
|
|
|
continue;
|
2017-02-15 11:23:44 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
/* wait for the DispatchSockets() to make room in the
|
|
|
|
ring_buffer */
|
|
|
|
cond.wait(mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 21:31:12 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
std::chrono::steady_clock::duration
|
2017-11-12 17:40:12 +01:00
|
|
|
AlsaOutput::PrepareSockets() noexcept
|
2017-01-24 23:10:35 +01:00
|
|
|
{
|
2018-04-26 16:14:27 +02:00
|
|
|
if (!LockIsActive()) {
|
2017-01-24 23:10:35 +01:00
|
|
|
ClearSocketList();
|
|
|
|
return std::chrono::steady_clock::duration(-1);
|
2017-01-11 21:31:12 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 21:06:17 +01:00
|
|
|
try {
|
2018-11-11 07:34:08 +01:00
|
|
|
return non_block.PrepareSockets(*this, pcm);
|
2017-11-14 21:06:17 +01:00
|
|
|
} catch (...) {
|
|
|
|
ClearSocketList();
|
|
|
|
LockCaughtError();
|
|
|
|
return std::chrono::steady_clock::duration(-1);
|
|
|
|
}
|
2017-01-11 21:31:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
void
|
2017-11-12 17:40:12 +01:00
|
|
|
AlsaOutput::DispatchSockets() noexcept
|
2017-01-24 23:10:35 +01:00
|
|
|
try {
|
2018-11-08 14:29:04 +01:00
|
|
|
non_block.DispatchSockets(*this, pcm);
|
|
|
|
|
2018-11-14 09:43:14 +01:00
|
|
|
if (must_prepare) {
|
|
|
|
must_prepare = false;
|
|
|
|
|
|
|
|
int err = snd_pcm_prepare(pcm);
|
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_prepare() failed: %s",
|
|
|
|
snd_strerror(-err));
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2018-04-26 16:12:32 +02:00
|
|
|
|
|
|
|
assert(active);
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
if (drain) {
|
|
|
|
{
|
|
|
|
ScopeUnlock unlock(mutex);
|
|
|
|
if (!DrainInternal())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MultiSocketMonitor::InvalidateSockets();
|
|
|
|
}
|
|
|
|
|
|
|
|
drain = false;
|
|
|
|
cond.signal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:01:11 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
CopyRingToPeriodBuffer();
|
|
|
|
|
2018-04-26 10:31:22 +02:00
|
|
|
if (period_buffer.IsEmpty()) {
|
|
|
|
if (snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED) {
|
|
|
|
/* at SND_PCM_STATE_PREPARED (not yet switched
|
|
|
|
to SND_PCM_STATE_RUNNING), we have no
|
|
|
|
pressure to fill the ALSA buffer, because
|
|
|
|
no xrun can possibly occur; and if no data
|
|
|
|
is available right now, we can easily wait
|
|
|
|
until some is available; so we just stop
|
|
|
|
monitoring the ALSA file descriptor, and
|
|
|
|
let it be reactivated by Play()/Activate()
|
|
|
|
whenever more data arrives */
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* avoid race condition: see if data has
|
|
|
|
arrived meanwhile before disabling the
|
|
|
|
event (but after clearing the "active"
|
|
|
|
flag) */
|
|
|
|
if (!CopyRingToPeriodBuffer()) {
|
|
|
|
MultiSocketMonitor::Reset();
|
|
|
|
defer_invalidate_sockets.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
/* insert some silence if the buffer has not enough
|
|
|
|
data yet, to avoid ALSA xrun */
|
|
|
|
period_buffer.FillWithSilence(silence, out_frame_size);
|
2018-04-26 10:31:22 +02:00
|
|
|
}
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
auto frames_written = WriteFromPeriodBuffer();
|
|
|
|
if (frames_written < 0) {
|
|
|
|
if (frames_written == -EAGAIN || frames_written == -EINTR)
|
|
|
|
/* try again in the next DispatchSockets()
|
|
|
|
call which is still scheduled */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Recover(frames_written) < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_writei() failed: %s",
|
|
|
|
snd_strerror(-frames_written));
|
|
|
|
|
|
|
|
/* recovered; try again in the next DispatchSockets()
|
|
|
|
call */
|
|
|
|
return;
|
|
|
|
}
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2017-01-24 23:10:35 +01:00
|
|
|
MultiSocketMonitor::Reset();
|
2017-11-14 21:07:59 +01:00
|
|
|
LockCaughtError();
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin alsa_output_plugin = {
|
2013-01-29 14:32:32 +01:00
|
|
|
"alsa",
|
|
|
|
alsa_test_default_device,
|
2017-08-09 16:58:44 +02:00
|
|
|
&AlsaOutput::Create,
|
2013-01-29 14:32:32 +01:00
|
|
|
&alsa_mixer_plugin,
|
2005-03-05 06:22:30 +01:00
|
|
|
};
|