2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 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"
|
2019-06-16 12:11:44 +02:00
|
|
|
#include "pcm/Export.hxx"
|
2019-06-28 14:35:56 +02:00
|
|
|
#include "system/PeriodClock.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"
|
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
|
|
|
|
2019-06-28 14:51:27 +02:00
|
|
|
/**
|
|
|
|
* This timer is used to re-schedule the #MultiSocketMonitor
|
|
|
|
* after it had been disabled to wait for the next Play() call
|
|
|
|
* to deliver more data. This timer is necessary to start
|
|
|
|
* generating silence if Play() doesn't get called soon enough
|
|
|
|
* to avoid the xrun.
|
|
|
|
*/
|
|
|
|
TimerEvent silence_timer;
|
|
|
|
|
2019-06-28 14:35:56 +02:00
|
|
|
PeriodClock throttle_silence_log;
|
|
|
|
|
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
|
|
|
|
2012-03-22 01:01:11 +01:00
|
|
|
/**
|
|
|
|
* The size of one audio frame passed to method play().
|
|
|
|
*/
|
|
|
|
size_t in_frame_size;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2019-06-28 14:51:27 +02:00
|
|
|
std::chrono::steady_clock::duration effective_period_duration;
|
|
|
|
|
2018-11-14 11:06:59 +01:00
|
|
|
/**
|
|
|
|
* If snd_pcm_avail() goes above this value and no more data
|
|
|
|
* is available in the #ring_buffer, we need to play some
|
|
|
|
* silence.
|
|
|
|
*/
|
|
|
|
snd_pcm_sframes_t max_avail_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
|
|
|
/**
|
2019-06-28 17:03:26 +02:00
|
|
|
* After Open() or Cancel(), 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
|
|
|
|
2019-06-28 15:30:26 +02:00
|
|
|
/**
|
|
|
|
* Is this output waiting for more data?
|
|
|
|
*
|
|
|
|
* Protected by #mutex.
|
|
|
|
*/
|
|
|
|
bool waiting;
|
|
|
|
|
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
|
|
|
|
2018-11-16 12:49:31 +01:00
|
|
|
/**
|
|
|
|
* Has snd_pcm_writei() been called successfully at least once
|
|
|
|
* since the PCM was prepared?
|
|
|
|
*
|
|
|
|
* This is necessary to work around a kernel bug which causes
|
|
|
|
* snd_pcm_drain() to return -EAGAIN forever in non-blocking
|
|
|
|
* mode if snd_pcm_writei() was never called.
|
|
|
|
*/
|
|
|
|
bool written;
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2019-06-28 15:30:26 +02:00
|
|
|
* Protects #cond, #error, #active, #waiting, #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
|
|
|
|
2020-02-01 04:37:53 +01:00
|
|
|
~AlsaOutput() noexcept override {
|
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.
|
|
|
|
*
|
2019-07-03 22:10:26 +02:00
|
|
|
* Throws on error.
|
2017-01-24 23:06:33 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:30:26 +02:00
|
|
|
gcc_pure
|
|
|
|
bool LockIsActiveAndNotWaiting() const noexcept {
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
return active && !waiting;
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-28 15:30:26 +02:00
|
|
|
if (active && !waiting)
|
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;
|
2019-06-28 15:30:26 +02:00
|
|
|
waiting = false;
|
2018-04-26 16:07:15 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-04 12:34:39 +02:00
|
|
|
/**
|
|
|
|
* Wait until there is some space available in the ring buffer.
|
|
|
|
*
|
|
|
|
* Caller must not lock the mutex.
|
|
|
|
*
|
|
|
|
* Throws on error.
|
|
|
|
*
|
|
|
|
* @return the number of frames available for writing
|
|
|
|
*/
|
|
|
|
size_t LockWaitWriteAvailable();
|
|
|
|
|
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 */
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
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 {
|
2019-06-27 21:22:52 +02:00
|
|
|
assert(period_buffer.IsFull());
|
2019-06-27 21:52:07 +02:00
|
|
|
assert(period_buffer.GetFrames(out_frame_size) > 0);
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
auto frames_written = snd_pcm_writei(pcm, period_buffer.GetHead(),
|
|
|
|
period_buffer.GetFrames(out_frame_size));
|
2018-11-16 12:49:31 +01:00
|
|
|
if (frames_written > 0) {
|
|
|
|
written = true;
|
2017-01-24 23:10:35 +01:00
|
|
|
period_buffer.ConsumeFrames(frames_written,
|
|
|
|
out_frame_size);
|
2018-11-16 12:49:31 +01:00
|
|
|
}
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
return frames_written;
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:07:59 +01:00
|
|
|
void LockCaughtError() noexcept {
|
2018-11-13 20:23:22 +01:00
|
|
|
period_buffer.Clear();
|
|
|
|
|
2017-11-14 21:07:59 +01:00
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
error = std::current_exception();
|
2018-04-26 16:14:27 +02:00
|
|
|
active = false;
|
2019-06-28 15:30:26 +02:00
|
|
|
waiting = false;
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2017-11-14 21:07:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:51:27 +02:00
|
|
|
/**
|
|
|
|
* Callback for @silence_timer
|
|
|
|
*/
|
|
|
|
void OnSilenceTimer() noexcept {
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
assert(active);
|
|
|
|
waiting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiSocketMonitor::InvalidateSockets();
|
|
|
|
}
|
|
|
|
|
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)),
|
2019-06-28 14:51:27 +02:00
|
|
|
silence_timer(_loop, BIND_THIS_METHOD(OnSilenceTimer)),
|
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;
|
2019-06-28 14:51:27 +02:00
|
|
|
effective_period_duration = audio_format.FramesToTime<decltype(effective_period_duration)>(period_frames);
|
2009-11-09 22:22:31 +01:00
|
|
|
|
2019-06-28 18:04:45 +02:00
|
|
|
/* generate silence if there's less than one period of data
|
2018-11-14 11:06:59 +01:00
|
|
|
in the ALSA-PCM buffer */
|
|
|
|
max_avail_frames = hw_result.buffer_size - hw_result.period_size;
|
|
|
|
|
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 {
|
2019-06-17 22:20:28 +02:00
|
|
|
params.dsd_mode = PcmExport::DsdMode::DOP;
|
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();
|
2019-06-17 22:20:28 +02:00
|
|
|
params.dsd_mode = PcmExport::DsdMode::NONE;
|
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
|
2019-06-17 22:20:28 +02:00
|
|
|
if (params.dsd_mode == PcmExport::DsdMode::DOP)
|
2017-01-11 22:50:40 +01:00
|
|
|
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);
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
in_frame_size = audio_format.GetFrameSize();
|
2019-06-26 15:45:44 +02:00
|
|
|
out_frame_size = pcm_export->GetOutputFrameSize();
|
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;
|
2019-06-28 15:30:26 +02:00
|
|
|
waiting = false;
|
2015-01-04 19:53:56 +01:00
|
|
|
must_prepare = false;
|
2018-11-16 12:49:31 +01:00
|
|
|
written = false;
|
2018-11-14 13:20:54 +01:00
|
|
|
error = {};
|
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();
|
2018-11-16 12:49:31 +01:00
|
|
|
written = false;
|
2015-01-04 19:53:56 +01:00
|
|
|
err = snd_pcm_prepare(pcm);
|
2005-03-05 21:51:36 +01:00
|
|
|
break;
|
2019-07-04 12:46:15 +02:00
|
|
|
|
2006-07-16 18:52:29 +02:00
|
|
|
case SND_PCM_STATE_DISCONNECTED:
|
2019-07-04 12:46:15 +02:00
|
|
|
case SND_PCM_STATE_DRAINING:
|
|
|
|
/* can't play in this state; throw the error */
|
2006-07-16 18:52:29 +02:00
|
|
|
break;
|
2019-07-04 12:46:15 +02:00
|
|
|
|
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:
|
2019-07-04 12:46:15 +02:00
|
|
|
/* the state is ok, but the error was unexpected;
|
|
|
|
throw it */
|
2008-04-12 06:07:44 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
/* drain period_buffer */
|
2019-06-27 21:53:28 +02:00
|
|
|
if (!period_buffer.IsCleared()) {
|
|
|
|
if (!period_buffer.IsFull())
|
|
|
|
/* generate some silence to finish the partial
|
|
|
|
period */
|
|
|
|
period_buffer.FillWithSilence(silence, out_frame_size);
|
|
|
|
|
|
|
|
/* drain period_buffer */
|
|
|
|
if (!period_buffer.IsDrained()) {
|
|
|
|
auto frames_written = WriteFromPeriodBuffer();
|
|
|
|
if (frames_written < 0) {
|
|
|
|
if (frames_written == -EAGAIN)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
throw FormatRuntimeError("snd_pcm_writei() failed: %s",
|
|
|
|
snd_strerror(-frames_written));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to call CopyRingToPeriodBuffer() and
|
|
|
|
WriteFromPeriodBuffer() again in the next
|
|
|
|
iteration, so don't finish the drain just
|
|
|
|
yet */
|
|
|
|
return false;
|
2017-01-24 23:10:35 +01:00
|
|
|
}
|
2009-11-09 22:22:31 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:49:31 +01:00
|
|
|
if (!written)
|
|
|
|
/* if nothing has ever been written to the PCM, we
|
|
|
|
don't need to drain it */
|
|
|
|
return true;
|
|
|
|
|
2019-06-28 08:55:23 +02:00
|
|
|
switch (snd_pcm_state(pcm)) {
|
|
|
|
case SND_PCM_STATE_PREPARED:
|
|
|
|
case SND_PCM_STATE_RUNNING:
|
|
|
|
/* these states require a call to snd_pcm_drain() */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_DRAINING:
|
|
|
|
/* already draining, but not yet finished; this is
|
|
|
|
probably a spurious epoll event, and we should wait
|
|
|
|
for the next one */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* all other states cannot be drained, and we're
|
|
|
|
done */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2019-04-25 18:53:38 +02:00
|
|
|
std::unique_lock<Mutex> lock(mutex);
|
2017-01-24 23:10:35 +01:00
|
|
|
|
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
|
|
|
|
2019-05-07 20:01:45 +02:00
|
|
|
cond.wait(lock, [this]{ return !drain || !active; });
|
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
|
|
|
{
|
2018-11-14 11:49:48 +01:00
|
|
|
/* this method doesn't need to lock the mutex because while it
|
|
|
|
runs, the calling thread is blocked inside Cancel() */
|
|
|
|
|
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
|
|
|
|
2018-11-14 11:49:48 +01:00
|
|
|
active = false;
|
2019-06-28 15:30:26 +02:00
|
|
|
waiting = false;
|
2018-04-26 12:28:10 +02:00
|
|
|
|
|
|
|
MultiSocketMonitor::Reset();
|
|
|
|
defer_invalidate_sockets.Cancel();
|
2019-06-28 14:51:27 +02:00
|
|
|
silence_timer.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();
|
2019-06-27 21:50:26 +02:00
|
|
|
assert(period_buffer.IsCleared());
|
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();
|
2019-06-28 14:51:27 +02:00
|
|
|
silence_timer.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
|
2019-07-04 12:34:39 +02:00
|
|
|
AlsaOutput::LockWaitWriteAvailable()
|
2017-01-11 21:31:12 +01:00
|
|
|
{
|
2019-07-04 12:34:39 +02:00
|
|
|
const size_t out_block_size = pcm_export->GetOutputBlockSize();
|
|
|
|
const size_t min_available = 2 * out_block_size;
|
2017-01-11 21:31:12 +01:00
|
|
|
|
2019-04-25 18:53:38 +02:00
|
|
|
std::unique_lock<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);
|
|
|
|
|
2019-07-04 12:34:39 +02:00
|
|
|
size_t write_available = ring_buffer->write_available();
|
|
|
|
if (write_available >= min_available) {
|
|
|
|
/* reserve room for one extra block, just in
|
|
|
|
case PcmExport::Export() has some partial
|
|
|
|
block data in its internal buffer */
|
|
|
|
write_available -= out_block_size;
|
|
|
|
|
|
|
|
return write_available / out_frame_size;
|
|
|
|
}
|
2017-01-24 23:10:35 +01:00
|
|
|
|
|
|
|
/* 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 */
|
2019-04-25 18:53:38 +02:00
|
|
|
cond.wait(lock);
|
2017-01-24 23:10:35 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 21:31:12 +01:00
|
|
|
|
2019-07-04 12:34:39 +02:00
|
|
|
size_t
|
|
|
|
AlsaOutput::Play(const void *chunk, size_t size)
|
|
|
|
{
|
|
|
|
assert(size > 0);
|
|
|
|
assert(size % in_frame_size == 0);
|
|
|
|
|
|
|
|
const size_t max_frames = LockWaitWriteAvailable();
|
|
|
|
const size_t max_size = max_frames * in_frame_size;
|
|
|
|
if (size > max_size)
|
|
|
|
size = max_size;
|
|
|
|
|
|
|
|
const auto e = pcm_export->Export({chunk, size});
|
|
|
|
if (e.empty())
|
|
|
|
return size;
|
|
|
|
|
|
|
|
size_t bytes_written = ring_buffer->push((const uint8_t *)e.data,
|
|
|
|
e.size);
|
|
|
|
assert(bytes_written == e.size);
|
|
|
|
(void)bytes_written;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-06-28 15:30:26 +02:00
|
|
|
if (!LockIsActiveAndNotWaiting()) {
|
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;
|
2018-11-16 12:49:31 +01:00
|
|
|
written = false;
|
2018-11-14 09:43:14 +01:00
|
|
|
|
|
|
|
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;
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2017-01-24 23:10:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:01:11 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
CopyRingToPeriodBuffer();
|
|
|
|
|
2019-06-27 15:40:07 +02:00
|
|
|
if (!period_buffer.IsFull()) {
|
2018-11-14 11:06:59 +01:00
|
|
|
if (snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED ||
|
|
|
|
snd_pcm_avail(pcm) <= max_avail_frames) {
|
2018-04-26 10:31:22 +02:00
|
|
|
/* 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 */
|
2018-11-14 11:06:59 +01:00
|
|
|
/* the same applies when there is still enough
|
|
|
|
data in the ALSA-PCM buffer (determined by
|
2019-06-28 14:39:52 +02:00
|
|
|
snd_pcm_avail()); this can happen at the
|
2018-11-14 11:06:59 +01:00
|
|
|
start of playback, when our ring_buffer is
|
|
|
|
smaller than the ALSA-PCM buffer */
|
2018-04-26 10:31:22 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2019-06-28 15:30:26 +02:00
|
|
|
waiting = true;
|
2019-04-25 18:33:09 +02:00
|
|
|
cond.notify_one();
|
2018-04-26 10:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* avoid race condition: see if data has
|
|
|
|
arrived meanwhile before disabling the
|
2019-06-28 15:30:26 +02:00
|
|
|
event (but after setting the "waiting"
|
2018-04-26 10:31:22 +02:00
|
|
|
flag) */
|
|
|
|
if (!CopyRingToPeriodBuffer()) {
|
|
|
|
MultiSocketMonitor::Reset();
|
|
|
|
defer_invalidate_sockets.Cancel();
|
2019-06-28 14:51:27 +02:00
|
|
|
|
|
|
|
/* just in case Play() doesn't get
|
|
|
|
called soon enough, schedule a
|
|
|
|
timer which generates silence
|
|
|
|
before the xrun occurs */
|
|
|
|
/* the timer fires in half of a
|
|
|
|
period; this short duration may
|
|
|
|
produce a few more wakeups than
|
|
|
|
necessary, but should be small
|
|
|
|
enough to avoid the xrun */
|
|
|
|
silence_timer.Schedule(effective_period_duration / 2);
|
2018-04-26 10:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:35:56 +02:00
|
|
|
if (throttle_silence_log.CheckUpdate(std::chrono::seconds(5)))
|
|
|
|
FormatWarning(alsa_output_domain, "Decoder is too slow; playing silence to avoid xrun");
|
|
|
|
|
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
|
|
|
};
|