2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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-01-24 23:10:35 +01:00
|
|
|
#include "lib/alsa/NonBlock.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2015-01-04 19:53:56 +01:00
|
|
|
#include "../Wrapper.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"
|
2016-02-28 09:54:20 +01:00
|
|
|
#include "system/ByteOrder.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-01-24 23:10:35 +01:00
|
|
|
#include "event/MultiSocketMonitor.hxx"
|
|
|
|
#include "event/DeferredMonitor.hxx"
|
|
|
|
#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>
|
|
|
|
|
2014-08-23 16:12:18 +02:00
|
|
|
#if SND_LIB_VERSION >= 0x1001c
|
|
|
|
/* alsa-lib supports DSD since version 1.0.27.1 */
|
|
|
|
#define HAVE_ALSA_DSD
|
|
|
|
#endif
|
|
|
|
|
2016-02-26 18:31:16 +01:00
|
|
|
#if SND_LIB_VERSION >= 0x1001d
|
|
|
|
/* alsa-lib supports DSD_U32 since version 1.0.29 */
|
|
|
|
#define HAVE_ALSA_DSD_U32
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2014-08-26 10:25:59 +02:00
|
|
|
static constexpr unsigned MPD_ALSA_RETRY_NR = 5;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
class AlsaOutput final
|
|
|
|
: MultiSocketMonitor, DeferredMonitor {
|
|
|
|
|
2017-01-24 22:20:09 +01:00
|
|
|
friend struct AudioOutputWrapper<AlsaOutput>;
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
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
|
|
|
*/
|
2016-11-04 11:27:29 +01:00
|
|
|
const bool dop;
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2017-01-24 23:10:35 +01:00
|
|
|
* After Open(), has this output been activated by a Play()
|
|
|
|
* command?
|
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
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
/**
|
|
|
|
* For PrepareAlsaPcmSockets().
|
|
|
|
*/
|
|
|
|
ReusableArray<pollfd> pfd_buffer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For copying data from OutputThread to IOThread.
|
|
|
|
*/
|
|
|
|
boost::lockfree::spsc_queue<uint8_t> *ring_buffer;
|
|
|
|
|
|
|
|
class PeriodBuffer {
|
|
|
|
size_t capacity, head, tail;
|
|
|
|
|
|
|
|
uint8_t *buffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PeriodBuffer() = default;
|
|
|
|
PeriodBuffer(const PeriodBuffer &) = delete;
|
|
|
|
PeriodBuffer &operator=(const PeriodBuffer &) = delete;
|
|
|
|
|
|
|
|
void Allocate(size_t n_frames, size_t frame_size) {
|
|
|
|
capacity = n_frames * frame_size;
|
|
|
|
|
|
|
|
/* reserve space for one more (partial) frame,
|
|
|
|
to be able to fill the buffer with silence,
|
|
|
|
after moving an unfinished frame to the
|
|
|
|
end */
|
|
|
|
buffer = new uint8_t[capacity + frame_size - 1];
|
|
|
|
head = tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free() {
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
return head == tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFull() const {
|
|
|
|
return tail >= capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *GetTail() {
|
|
|
|
return buffer + tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetSpaceBytes() const {
|
|
|
|
assert(tail <= capacity);
|
|
|
|
|
|
|
|
return capacity - tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendBytes(size_t n) {
|
|
|
|
assert(n <= capacity);
|
|
|
|
assert(tail <= capacity - n);
|
|
|
|
|
|
|
|
tail += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FillWithSilence(const uint8_t *_silence,
|
|
|
|
const size_t frame_size) {
|
|
|
|
size_t partial_frame = tail % frame_size;
|
|
|
|
auto *dest = GetTail() - partial_frame;
|
|
|
|
|
|
|
|
/* move the partial frame to the end */
|
|
|
|
std::copy(dest, GetTail(), buffer + capacity);
|
|
|
|
|
|
|
|
size_t silence_size = capacity - tail - partial_frame;
|
|
|
|
std::copy_n(_silence, silence_size, dest);
|
|
|
|
|
|
|
|
tail = capacity + partial_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *GetHead() const {
|
|
|
|
return buffer + head;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_uframes_t GetFrames(size_t frame_size) const {
|
|
|
|
return (tail - head) / frame_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumeBytes(size_t n) {
|
|
|
|
head += n;
|
|
|
|
|
|
|
|
assert(head <= capacity);
|
|
|
|
|
|
|
|
if (head >= capacity) {
|
|
|
|
tail -= head;
|
|
|
|
/* copy the partial frame (if any)
|
|
|
|
back to the beginning */
|
|
|
|
std::copy_n(GetHead(), tail, buffer);
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumeFrames(snd_pcm_uframes_t n, size_t frame_size) {
|
|
|
|
ConsumeBytes(n * frame_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_uframes_t GetPeriodPosition(size_t frame_size) const {
|
|
|
|
return head / frame_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind() {
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
head = tail = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PeriodBuffer period_buffer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protects #cond, #error, #drain.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
~AlsaOutput() {
|
|
|
|
/* free libasound's config cache */
|
|
|
|
snd_config_update_free_global();
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
const char *GetDevice() {
|
|
|
|
return device.empty() ? default_device : device.c_str();
|
|
|
|
}
|
|
|
|
|
2017-01-25 09:47:43 +01:00
|
|
|
static AlsaOutput *Create(EventLoop &event_loop,
|
|
|
|
const ConfigBlock &block);
|
2015-01-04 19:53:56 +01:00
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
void Enable();
|
2015-01-04 19:53:56 +01:00
|
|
|
void Disable();
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
void Open(AudioFormat &audio_format);
|
2015-01-04 19:53:56 +01:00
|
|
|
void Close();
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
size_t Play(const void *chunk, size_t size);
|
2015-01-04 19:53:56 +01:00
|
|
|
void Drain();
|
|
|
|
void Cancel();
|
|
|
|
|
|
|
|
private:
|
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
|
|
|
|
|
2016-11-03 17:28:34 +01:00
|
|
|
void SetupOrDop(AudioFormat &audio_format, PcmExport::Params ¶ms);
|
2015-01-04 19:53:56 +01:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
void Activate() {
|
|
|
|
if (active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
active = true;
|
|
|
|
DeferredMonitor::Schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for Activate() which unlocks our mutex. Call this
|
|
|
|
* if you're holding the mutex.
|
|
|
|
*/
|
|
|
|
void UnlockActivate() {
|
|
|
|
if (active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
Activate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearRingBuffer() {
|
|
|
|
std::array<uint8_t, 1024> buffer;
|
|
|
|
while (ring_buffer->pop(&buffer.front(), buffer.size())) {}
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
int Recover(int err);
|
|
|
|
|
|
|
|
/**
|
2017-01-24 23:10:35 +01:00
|
|
|
* Drain all buffers. To be run in #EventLoop's thread.
|
|
|
|
*
|
|
|
|
* @return true if draining is complete, false if this method
|
|
|
|
* needs to be called again later
|
2015-01-04 19:53:56 +01:00
|
|
|
*/
|
2017-01-24 23:10:35 +01:00
|
|
|
bool DrainInternal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop playback immediately, dropping all buffers. To be run
|
|
|
|
* in #EventLoop's thread.
|
|
|
|
*/
|
|
|
|
void CancelInternal();
|
|
|
|
|
|
|
|
void CopyRingToPeriodBuffer() {
|
|
|
|
if (period_buffer.IsFull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t nbytes = ring_buffer->pop(period_buffer.GetTail(),
|
|
|
|
period_buffer.GetSpaceBytes());
|
|
|
|
if (nbytes == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
period_buffer.AppendBytes(nbytes);
|
|
|
|
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
/* notify the OutputThread that there is now
|
|
|
|
room in ring_buffer */
|
|
|
|
cond.signal();
|
2015-01-04 19:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
snd_pcm_sframes_t WriteFromPeriodBuffer() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LockHasError() const {
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
return !!error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual methods from class DeferredMonitor */
|
|
|
|
virtual void RunDeferred() override {
|
|
|
|
InvalidateSockets();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual methods from class MultiSocketMonitor */
|
|
|
|
virtual std::chrono::steady_clock::duration PrepareSockets() override;
|
|
|
|
virtual void DispatchSockets() 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-01-24 23:10:35 +01:00
|
|
|
AlsaOutput::AlsaOutput(EventLoop &loop, const ConfigBlock &block)
|
|
|
|
:MultiSocketMonitor(loop), DeferredMonitor(loop),
|
|
|
|
base(alsa_output_plugin, block),
|
2016-11-04 11:27:29 +01:00
|
|
|
device(block.GetBlockValue("device", "")),
|
2016-02-26 18:41:52 +01:00
|
|
|
#ifdef ENABLE_DSD
|
2016-11-04 11:27:29 +01:00
|
|
|
dop(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
|
2016-11-04 11:27:29 +01:00
|
|
|
buffer_time(block.GetBlockValue("buffer_time",
|
|
|
|
MPD_ALSA_BUFFER_TIME_US)),
|
|
|
|
period_time(block.GetBlockValue("period_time", 0u))
|
|
|
|
{
|
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
|
2008-10-12 11:47:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
inline AlsaOutput *
|
2017-02-10 21:21:15 +01:00
|
|
|
AlsaOutput::Create(EventLoop &event_loop, const ConfigBlock &block)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2017-02-10 21:21:15 +01:00
|
|
|
return new AlsaOutput(event_loop, block);
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
inline void
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
inline void
|
|
|
|
AlsaOutput::Disable()
|
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
|
|
|
}
|
|
|
|
|
2014-08-26 09:37:30 +02:00
|
|
|
/**
|
|
|
|
* Convert MPD's #SampleFormat enum to libasound's snd_pcm_format_t
|
|
|
|
* enum. Returns SND_PCM_FORMAT_UNKNOWN if there is no according ALSA
|
|
|
|
* PCM format.
|
|
|
|
*/
|
2016-02-28 09:44:15 +01:00
|
|
|
gcc_const
|
2009-01-25 13:05:16 +01:00
|
|
|
static snd_pcm_format_t
|
2016-02-28 09:44:15 +01:00
|
|
|
ToAlsaPcmFormat(SampleFormat sample_format)
|
2008-09-08 20:42:51 +02:00
|
|
|
{
|
2010-01-16 17:45:56 +01:00
|
|
|
switch (sample_format) {
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::UNDEFINED:
|
2014-08-23 16:12:18 +02:00
|
|
|
return SND_PCM_FORMAT_UNKNOWN;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::DSD:
|
2014-08-23 16:12:18 +02:00
|
|
|
#ifdef HAVE_ALSA_DSD
|
|
|
|
return SND_PCM_FORMAT_DSD_U8;
|
|
|
|
#else
|
2011-10-08 14:37:54 +02:00
|
|
|
return SND_PCM_FORMAT_UNKNOWN;
|
2014-08-23 16:12:18 +02:00
|
|
|
#endif
|
2011-10-08 14:37:54 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S8:
|
2009-11-10 17:11:34 +01:00
|
|
|
return SND_PCM_FORMAT_S8;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2009-11-10 17:11:34 +01:00
|
|
|
return SND_PCM_FORMAT_S16;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
2009-11-10 17:11:34 +01:00
|
|
|
return SND_PCM_FORMAT_S24;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S32:
|
2009-11-10 17:11:34 +01:00
|
|
|
return SND_PCM_FORMAT_S32;
|
2011-10-08 10:25:06 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::FLOAT:
|
2011-10-08 10:25:06 +02:00
|
|
|
return SND_PCM_FORMAT_FLOAT;
|
2008-09-08 20:42:51 +02:00
|
|
|
}
|
2011-10-08 14:37:54 +02:00
|
|
|
|
|
|
|
assert(false);
|
2013-08-03 21:34:17 +02:00
|
|
|
gcc_unreachable();
|
2008-09-08 20:42:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 09:37:30 +02:00
|
|
|
/**
|
|
|
|
* Determine the byte-swapped PCM format. Returns
|
|
|
|
* SND_PCM_FORMAT_UNKNOWN if the format cannot be byte-swapped.
|
|
|
|
*/
|
2009-07-19 17:43:08 +02:00
|
|
|
static snd_pcm_format_t
|
2016-02-28 09:44:15 +01:00
|
|
|
ByteSwapAlsaPcmFormat(snd_pcm_format_t fmt)
|
2009-07-19 17:43:08 +02:00
|
|
|
{
|
2014-08-26 10:25:59 +02:00
|
|
|
switch (fmt) {
|
2009-07-19 17:43:08 +02:00
|
|
|
case SND_PCM_FORMAT_S16_LE: return SND_PCM_FORMAT_S16_BE;
|
|
|
|
case SND_PCM_FORMAT_S24_LE: return SND_PCM_FORMAT_S24_BE;
|
|
|
|
case SND_PCM_FORMAT_S32_LE: return SND_PCM_FORMAT_S32_BE;
|
|
|
|
case SND_PCM_FORMAT_S16_BE: return SND_PCM_FORMAT_S16_LE;
|
|
|
|
case SND_PCM_FORMAT_S24_BE: return SND_PCM_FORMAT_S24_LE;
|
2010-01-16 18:35:10 +01:00
|
|
|
|
|
|
|
case SND_PCM_FORMAT_S24_3BE:
|
|
|
|
return SND_PCM_FORMAT_S24_3LE;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_S24_3LE:
|
|
|
|
return SND_PCM_FORMAT_S24_3BE;
|
|
|
|
|
2009-07-19 17:43:08 +02:00
|
|
|
case SND_PCM_FORMAT_S32_BE: return SND_PCM_FORMAT_S32_LE;
|
2016-02-26 18:31:16 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_ALSA_DSD_U32
|
|
|
|
case SND_PCM_FORMAT_DSD_U16_LE:
|
|
|
|
return SND_PCM_FORMAT_DSD_U16_BE;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_DSD_U16_BE:
|
|
|
|
return SND_PCM_FORMAT_DSD_U16_LE;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_DSD_U32_LE:
|
|
|
|
return SND_PCM_FORMAT_DSD_U32_BE;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_DSD_U32_BE:
|
|
|
|
return SND_PCM_FORMAT_DSD_U32_LE;
|
|
|
|
#endif
|
|
|
|
|
2009-07-19 17:43:08 +02:00
|
|
|
default: return SND_PCM_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2010-01-16 17:14:30 +01:00
|
|
|
|
2014-08-26 09:37:30 +02:00
|
|
|
/**
|
|
|
|
* Check if there is a "packed" version of the give PCM format.
|
|
|
|
* Returns SND_PCM_FORMAT_UNKNOWN if not.
|
|
|
|
*/
|
2012-03-21 23:59:31 +01:00
|
|
|
static snd_pcm_format_t
|
2016-02-28 09:44:15 +01:00
|
|
|
PackAlsaPcmFormat(snd_pcm_format_t fmt)
|
2012-03-21 23:59:31 +01:00
|
|
|
{
|
|
|
|
switch (fmt) {
|
|
|
|
case SND_PCM_FORMAT_S24_LE:
|
|
|
|
return SND_PCM_FORMAT_S24_3LE;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_S24_BE:
|
|
|
|
return SND_PCM_FORMAT_S24_3BE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return SND_PCM_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 09:37:30 +02:00
|
|
|
/**
|
|
|
|
* Attempts to configure the specified sample format. On failure,
|
|
|
|
* fall back to the packed version.
|
|
|
|
*/
|
2012-03-21 23:59:31 +01:00
|
|
|
static int
|
2016-02-28 09:44:15 +01:00
|
|
|
AlsaTryFormatOrPacked(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
|
|
|
snd_pcm_format_t fmt, PcmExport::Params ¶ms)
|
2012-03-21 23:59:31 +01:00
|
|
|
{
|
|
|
|
int err = snd_pcm_hw_params_set_format(pcm, hwparams, fmt);
|
|
|
|
if (err == 0)
|
2016-02-27 07:35:35 +01:00
|
|
|
params.pack24 = false;
|
2012-03-21 23:59:31 +01:00
|
|
|
|
|
|
|
if (err != -EINVAL)
|
|
|
|
return err;
|
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
fmt = PackAlsaPcmFormat(fmt);
|
2012-03-21 23:59:31 +01:00
|
|
|
if (fmt == SND_PCM_FORMAT_UNKNOWN)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_format(pcm, hwparams, fmt);
|
|
|
|
if (err == 0)
|
2016-02-27 07:35:35 +01:00
|
|
|
params.pack24 = true;
|
2012-03-21 23:59:31 +01:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-01-16 17:41:28 +01:00
|
|
|
/**
|
2012-03-22 00:23:07 +01:00
|
|
|
* Attempts to configure the specified sample format, and tries the
|
|
|
|
* reversed host byte order if was not supported.
|
2010-01-16 17:41:28 +01:00
|
|
|
*/
|
|
|
|
static int
|
2016-02-28 09:44:15 +01:00
|
|
|
AlsaTryFormatOrByteSwap(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
|
|
|
snd_pcm_format_t fmt,
|
|
|
|
PcmExport::Params ¶ms)
|
2010-01-16 17:41:28 +01:00
|
|
|
{
|
2016-02-28 09:44:15 +01:00
|
|
|
int err = AlsaTryFormatOrPacked(pcm, hwparams, fmt, params);
|
2012-03-22 00:24:56 +01:00
|
|
|
if (err == 0)
|
2016-02-27 07:35:35 +01:00
|
|
|
params.reverse_endian = false;
|
2010-01-16 17:41:28 +01:00
|
|
|
|
2012-03-22 00:23:07 +01:00
|
|
|
if (err != -EINVAL)
|
|
|
|
return err;
|
2010-01-16 17:41:28 +01:00
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
fmt = ByteSwapAlsaPcmFormat(fmt);
|
|
|
|
if (fmt == SND_PCM_FORMAT_UNKNOWN)
|
2010-01-16 17:41:28 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
err = AlsaTryFormatOrPacked(pcm, hwparams, fmt, params);
|
2012-03-22 00:24:56 +01:00
|
|
|
if (err == 0)
|
2016-02-27 07:35:35 +01:00
|
|
|
params.reverse_endian = true;
|
2010-01-16 18:08:13 +01:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-02-28 09:54:20 +01:00
|
|
|
/**
|
|
|
|
* Attempts to configure the specified sample format. On DSD_U8
|
2017-01-11 22:33:52 +01:00
|
|
|
* failure, attempt to switch to DSD_U32 or DSD_U16.
|
2016-02-28 09:54:20 +01:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
AlsaTryFormatDsd(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
|
|
|
snd_pcm_format_t fmt, PcmExport::Params ¶ms)
|
|
|
|
{
|
|
|
|
int err = AlsaTryFormatOrByteSwap(pcm, hwparams, fmt, params);
|
|
|
|
|
|
|
|
#if defined(ENABLE_DSD) && defined(HAVE_ALSA_DSD_U32)
|
2017-01-11 22:33:52 +01:00
|
|
|
if (err == 0) {
|
|
|
|
params.dsd_u16 = false;
|
2016-02-28 09:54:20 +01:00
|
|
|
params.dsd_u32 = false;
|
2017-01-11 22:33:52 +01:00
|
|
|
}
|
2016-02-28 09:54:20 +01:00
|
|
|
|
|
|
|
if (err == -EINVAL && fmt == SND_PCM_FORMAT_DSD_U8) {
|
|
|
|
/* attempt to switch to DSD_U32 */
|
|
|
|
fmt = IsLittleEndian()
|
|
|
|
? SND_PCM_FORMAT_DSD_U32_LE
|
|
|
|
: SND_PCM_FORMAT_DSD_U32_BE;
|
|
|
|
err = AlsaTryFormatOrByteSwap(pcm, hwparams, fmt, params);
|
|
|
|
if (err == 0)
|
|
|
|
params.dsd_u32 = true;
|
2017-01-11 22:33:52 +01:00
|
|
|
else
|
|
|
|
fmt = SND_PCM_FORMAT_DSD_U8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == -EINVAL && fmt == SND_PCM_FORMAT_DSD_U8) {
|
|
|
|
/* attempt to switch to DSD_U16 */
|
|
|
|
fmt = IsLittleEndian()
|
|
|
|
? SND_PCM_FORMAT_DSD_U16_LE
|
|
|
|
: SND_PCM_FORMAT_DSD_U16_BE;
|
|
|
|
err = AlsaTryFormatOrByteSwap(pcm, hwparams, fmt, params);
|
|
|
|
if (err == 0)
|
|
|
|
params.dsd_u16 = true;
|
|
|
|
else
|
|
|
|
fmt = SND_PCM_FORMAT_DSD_U8;
|
2016-02-28 09:54:20 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
static int
|
|
|
|
AlsaTryFormat(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
|
|
|
SampleFormat sample_format,
|
|
|
|
PcmExport::Params ¶ms)
|
|
|
|
{
|
|
|
|
snd_pcm_format_t alsa_format = ToAlsaPcmFormat(sample_format);
|
|
|
|
if (alsa_format == SND_PCM_FORMAT_UNKNOWN)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-02-28 09:54:20 +01:00
|
|
|
return AlsaTryFormatDsd(pcm, hwparams, alsa_format, params);
|
2016-02-28 09:44:15 +01:00
|
|
|
}
|
|
|
|
|
2009-02-25 22:01:30 +01:00
|
|
|
/**
|
2010-01-16 17:14:30 +01:00
|
|
|
* Configure a sample format, and probe other formats if that fails.
|
2009-02-25 22:01:30 +01:00
|
|
|
*/
|
2010-01-16 17:14:30 +01:00
|
|
|
static int
|
2016-02-28 09:44:15 +01:00
|
|
|
AlsaSetupFormat(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
|
|
|
AudioFormat &audio_format,
|
|
|
|
PcmExport::Params ¶ms)
|
2005-03-05 06:22:30 +01:00
|
|
|
{
|
2010-01-16 18:08:13 +01:00
|
|
|
/* try the input format first */
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
int err = AlsaTryFormat(pcm, hwparams, audio_format.format, params);
|
2010-01-16 17:14:30 +01:00
|
|
|
|
2010-01-16 18:08:13 +01:00
|
|
|
/* if unsupported by the hardware, try other formats */
|
2010-01-16 17:14:30 +01:00
|
|
|
|
2014-08-26 10:25:59 +02:00
|
|
|
static constexpr SampleFormat probe_formats[] = {
|
2013-08-03 21:00:50 +02:00
|
|
|
SampleFormat::S24_P32,
|
|
|
|
SampleFormat::S32,
|
|
|
|
SampleFormat::S16,
|
|
|
|
SampleFormat::S8,
|
|
|
|
SampleFormat::UNDEFINED,
|
2010-01-16 18:08:13 +01:00
|
|
|
};
|
2009-03-02 16:41:38 +01:00
|
|
|
|
2012-03-22 00:29:56 +01:00
|
|
|
for (unsigned i = 0;
|
2013-08-03 21:00:50 +02:00
|
|
|
err == -EINVAL && probe_formats[i] != SampleFormat::UNDEFINED;
|
2012-03-22 00:29:56 +01:00
|
|
|
++i) {
|
2013-08-03 21:00:50 +02:00
|
|
|
const SampleFormat mpd_format = probe_formats[i];
|
|
|
|
if (mpd_format == audio_format.format)
|
2010-01-16 18:08:13 +01:00
|
|
|
continue;
|
2010-01-16 17:41:28 +01:00
|
|
|
|
2016-02-28 09:44:15 +01:00
|
|
|
err = AlsaTryFormat(pcm, hwparams, mpd_format, params);
|
2012-03-22 00:24:56 +01:00
|
|
|
if (err == 0)
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = mpd_format;
|
2009-07-19 17:43:08 +02:00
|
|
|
}
|
2008-10-12 12:02:55 +02:00
|
|
|
|
2012-03-22 00:29:56 +01:00
|
|
|
return err;
|
2010-01-16 17:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-24 22:48:48 +01:00
|
|
|
* Wrapper for snd_pcm_hw_params().
|
2016-11-03 17:28:34 +01:00
|
|
|
*
|
2017-01-24 22:48:48 +01:00
|
|
|
* @param buffer_time the configured buffer time, or 0 if not configured
|
|
|
|
* @param period_time the configured period time, or 0 if not configured
|
|
|
|
* @param audio_format an #AudioFormat to be configured (or modified)
|
|
|
|
* by this function
|
|
|
|
* @param params to be modified by this function
|
2010-01-16 17:14:30 +01:00
|
|
|
*/
|
2016-11-03 17:28:34 +01:00
|
|
|
static void
|
2017-01-24 22:48:48 +01:00
|
|
|
AlsaSetupHw(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
|
|
|
|
unsigned buffer_time, unsigned period_time,
|
|
|
|
AudioFormat &audio_format, PcmExport::Params ¶ms)
|
2010-01-16 17:14:30 +01:00
|
|
|
{
|
|
|
|
int err;
|
2014-08-30 00:26:48 +02:00
|
|
|
unsigned retry = MPD_ALSA_RETRY_NR;
|
2017-01-24 22:48:48 +01:00
|
|
|
unsigned int period_time_ro = period_time;
|
2010-01-16 17:14:30 +01:00
|
|
|
|
|
|
|
configure_hw:
|
|
|
|
/* configure HW params */
|
2017-01-24 22:48:48 +01:00
|
|
|
err = snd_pcm_hw_params_any(pcm, hwparams);
|
2010-01-16 17:14:30 +01:00
|
|
|
if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params_any() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2010-01-16 17:14:30 +01:00
|
|
|
|
2017-01-24 22:48:48 +01:00
|
|
|
err = snd_pcm_hw_params_set_access(pcm, hwparams,
|
2016-02-28 09:30:59 +01:00
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params_set_access() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2010-01-16 17:14:30 +01:00
|
|
|
|
2017-01-24 22:48:48 +01:00
|
|
|
err = AlsaSetupFormat(pcm, hwparams, audio_format, params);
|
2016-11-03 17:28:34 +01:00
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("Failed to configure format %s: %s",
|
|
|
|
sample_format_to_string(audio_format.format),
|
|
|
|
snd_strerror(-err));
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2017-01-24 22:48:48 +01:00
|
|
|
unsigned int channels = audio_format.channels;
|
|
|
|
err = snd_pcm_hw_params_set_channels_near(pcm, hwparams,
|
2006-07-20 18:02:40 +02:00
|
|
|
&channels);
|
2016-11-03 17:28:34 +01:00
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("Failed to configure %i channels: %s",
|
|
|
|
(int)audio_format.channels,
|
|
|
|
snd_strerror(-err));
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.channels = (int8_t)channels;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2017-01-10 23:48:26 +01:00
|
|
|
const unsigned requested_sample_rate =
|
|
|
|
params.CalcOutputSampleRate(audio_format.sample_rate);
|
|
|
|
unsigned output_sample_rate = requested_sample_rate;
|
|
|
|
|
2017-01-24 22:48:48 +01:00
|
|
|
err = snd_pcm_hw_params_set_rate_near(pcm, hwparams,
|
2017-01-10 23:48:26 +01:00
|
|
|
&output_sample_rate, nullptr);
|
2016-11-03 17:28:34 +01:00
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("Failed to configure sample rate %u Hz: %s",
|
2017-01-10 23:48:26 +01:00
|
|
|
requested_sample_rate,
|
2016-11-03 17:28:34 +01:00
|
|
|
snd_strerror(-err));
|
2016-11-04 10:35:03 +01:00
|
|
|
|
2017-01-10 23:48:26 +01:00
|
|
|
if (output_sample_rate == 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("Failed to configure sample rate %u Hz",
|
|
|
|
audio_format.sample_rate);
|
2016-11-04 10:35:03 +01:00
|
|
|
|
2017-01-10 23:48:26 +01:00
|
|
|
if (output_sample_rate != requested_sample_rate)
|
|
|
|
audio_format.sample_rate = params.CalcInputSampleRate(output_sample_rate);
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2010-11-05 08:25:21 +01:00
|
|
|
snd_pcm_uframes_t buffer_size_min, buffer_size_max;
|
|
|
|
snd_pcm_hw_params_get_buffer_size_min(hwparams, &buffer_size_min);
|
|
|
|
snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size_max);
|
|
|
|
unsigned buffer_time_min, buffer_time_max;
|
|
|
|
snd_pcm_hw_params_get_buffer_time_min(hwparams, &buffer_time_min, 0);
|
|
|
|
snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time_max, 0);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain, "buffer: size=%u..%u time=%u..%u",
|
|
|
|
(unsigned)buffer_size_min, (unsigned)buffer_size_max,
|
|
|
|
buffer_time_min, buffer_time_max);
|
2010-11-05 08:25:21 +01:00
|
|
|
|
|
|
|
snd_pcm_uframes_t period_size_min, period_size_max;
|
|
|
|
snd_pcm_hw_params_get_period_size_min(hwparams, &period_size_min, 0);
|
|
|
|
snd_pcm_hw_params_get_period_size_max(hwparams, &period_size_max, 0);
|
|
|
|
unsigned period_time_min, period_time_max;
|
|
|
|
snd_pcm_hw_params_get_period_time_min(hwparams, &period_time_min, 0);
|
|
|
|
snd_pcm_hw_params_get_period_time_max(hwparams, &period_time_max, 0);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain, "period: size=%u..%u time=%u..%u",
|
|
|
|
(unsigned)period_size_min, (unsigned)period_size_max,
|
|
|
|
period_time_min, period_time_max);
|
2010-11-05 08:25:21 +01:00
|
|
|
|
2017-01-24 22:48:48 +01:00
|
|
|
if (buffer_time > 0) {
|
|
|
|
err = snd_pcm_hw_params_set_buffer_time_near(pcm, hwparams,
|
2013-10-28 23:58:17 +01:00
|
|
|
&buffer_time, nullptr);
|
2008-10-11 12:52:48 +02:00
|
|
|
if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params_set_buffer_time_near() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2009-03-08 04:11:30 +01:00
|
|
|
} else {
|
|
|
|
err = snd_pcm_hw_params_get_buffer_time(hwparams, &buffer_time,
|
2013-10-28 23:58:17 +01:00
|
|
|
nullptr);
|
2009-03-08 04:11:30 +01:00
|
|
|
if (err < 0)
|
|
|
|
buffer_time = 0;
|
2008-10-11 12:52:48 +02:00
|
|
|
}
|
2006-07-16 18:52:06 +02:00
|
|
|
|
2009-03-08 03:55:01 +01:00
|
|
|
if (period_time_ro == 0 && buffer_time >= 10000) {
|
|
|
|
period_time_ro = period_time = buffer_time / 4;
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain,
|
|
|
|
"default period_time = buffer_time/4 = %u/4 = %u",
|
|
|
|
buffer_time, period_time);
|
2009-03-08 03:55:01 +01:00
|
|
|
}
|
|
|
|
|
2008-10-11 12:52:48 +02:00
|
|
|
if (period_time_ro > 0) {
|
|
|
|
period_time = period_time_ro;
|
2017-01-24 22:48:48 +01:00
|
|
|
err = snd_pcm_hw_params_set_period_time_near(pcm, hwparams,
|
2013-10-28 23:58:17 +01:00
|
|
|
&period_time, nullptr);
|
2008-10-11 12:52:48 +02:00
|
|
|
if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params_set_period_time_near() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2008-10-11 12:52:48 +02:00
|
|
|
}
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2017-01-24 22:48:48 +01:00
|
|
|
err = snd_pcm_hw_params(pcm, hwparams);
|
2008-10-11 12:52:48 +02:00
|
|
|
if (err == -EPIPE && --retry > 0 && period_time_ro > 0) {
|
2006-08-12 20:20:55 +02:00
|
|
|
period_time_ro = period_time_ro >> 1;
|
2006-07-24 03:38:51 +02:00
|
|
|
goto configure_hw;
|
|
|
|
} else if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2006-07-24 03:38:51 +02:00
|
|
|
if (retry != MPD_ALSA_RETRY_NR)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(alsa_output_domain,
|
|
|
|
"ALSA period_time set to %d", period_time);
|
2017-01-24 22:48:48 +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
|
|
|
{
|
|
|
|
snd_pcm_hw_params_t *hwparams;
|
|
|
|
snd_pcm_hw_params_alloca(&hwparams);
|
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
AlsaSetupHw(pcm, hwparams,
|
|
|
|
buffer_time, period_time,
|
2017-01-24 22:48:48 +01:00
|
|
|
audio_format, params);
|
|
|
|
|
|
|
|
snd_pcm_format_t format;
|
|
|
|
if (snd_pcm_hw_params_get_format(hwparams, &format) == 0)
|
|
|
|
FormatDebug(alsa_output_domain,
|
|
|
|
"format=%s (%s)", snd_pcm_format_name(format),
|
|
|
|
snd_pcm_format_description(format));
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2013-01-29 14:32:32 +01:00
|
|
|
snd_pcm_uframes_t alsa_buffer_size;
|
2017-01-24 22:48:48 +01:00
|
|
|
int err = snd_pcm_hw_params_get_buffer_size(hwparams, &alsa_buffer_size);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params_get_buffer_size() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2013-01-29 14:32:32 +01:00
|
|
|
snd_pcm_uframes_t alsa_period_size;
|
2006-07-17 02:15:52 +02:00
|
|
|
err = snd_pcm_hw_params_get_period_size(hwparams, &alsa_period_size,
|
2013-10-28 23:58:17 +01:00
|
|
|
nullptr);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (err < 0)
|
2016-11-03 17:28:34 +01:00
|
|
|
throw FormatRuntimeError("snd_pcm_hw_params_get_period_size() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2017-01-24 23:06:33 +01:00
|
|
|
AlsaSetupSw(pcm, alsa_buffer_size - alsa_period_size,
|
2017-01-24 22:58:09 +01:00
|
|
|
alsa_period_size);
|
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",
|
|
|
|
(unsigned)alsa_buffer_size, (unsigned)alsa_period_size);
|
2009-03-03 22:19:37 +01:00
|
|
|
|
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)];
|
|
|
|
snd_pcm_format_set_silence(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
|
|
|
{
|
2015-01-04 19:53:56 +01:00
|
|
|
assert(dop);
|
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
|
|
|
|
AlsaOutput::SetupOrDop(AudioFormat &audio_format, PcmExport::Params ¶ms)
|
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
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
inline void
|
|
|
|
AlsaOutput::Open(AudioFormat &audio_format)
|
2009-02-25 22:01:30 +01: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 {
|
|
|
|
SetupOrDop(audio_format, params);
|
|
|
|
} 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-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);
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
in_frame_size = audio_format.GetFrameSize();
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* reserve space for one more (partial) frame, to be able to
|
|
|
|
fill the buffer with silence, after moving an unfinished
|
|
|
|
frame to the end */
|
|
|
|
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
|
|
|
|
AlsaOutput::Recover(int err)
|
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;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
inline bool
|
|
|
|
AlsaOutput::DrainInternal()
|
2009-10-29 15:59:40 +01:00
|
|
|
{
|
2017-01-24 23:10:35 +01:00
|
|
|
if (snd_pcm_state(pcm) != SND_PCM_STATE_RUNNING) {
|
|
|
|
CancelInternal();
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-09 22:22:31 +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 */
|
|
|
|
return snd_pcm_drain(pcm) != -EAGAIN;
|
|
|
|
}
|
2009-11-09 22:22:31 +01:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
inline void
|
|
|
|
AlsaOutput::Drain()
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
|
|
|
|
drain = true;
|
|
|
|
|
|
|
|
UnlockActivate();
|
|
|
|
|
|
|
|
while (drain && !error)
|
|
|
|
cond.wait(mutex);
|
2009-10-29 15:59:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
inline void
|
2017-01-24 23:10:35 +01:00
|
|
|
AlsaOutput::CancelInternal()
|
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();
|
|
|
|
ClearRingBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
AlsaOutput::Cancel()
|
|
|
|
{
|
|
|
|
if (!active) {
|
|
|
|
/* early cancel, quick code path without thread
|
|
|
|
synchronization */
|
|
|
|
|
|
|
|
pcm_export->Reset();
|
|
|
|
assert(period_buffer.IsEmpty());
|
|
|
|
ClearRingBuffer();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockingCall(MultiSocketMonitor::GetEventLoop(), [this](){
|
|
|
|
CancelInternal();
|
|
|
|
});
|
2005-12-12 04:22:27 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
inline void
|
|
|
|
AlsaOutput::Close()
|
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() */
|
|
|
|
BlockingCall(MultiSocketMonitor::GetEventLoop(), [this](){
|
|
|
|
MultiSocketMonitor::Reset();
|
|
|
|
DeferredMonitor::Cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
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-01-11 21:31:12 +01:00
|
|
|
inline 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() */
|
|
|
|
UnlockActivate();
|
|
|
|
|
2017-02-15 11:23:44 +01:00
|
|
|
/* check the error again, because a new one may have
|
|
|
|
been set while our mutex was unlocked in
|
|
|
|
UnlockActivate() */
|
|
|
|
if (error)
|
|
|
|
std::rethrow_exception(error);
|
|
|
|
|
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
|
|
|
|
AlsaOutput::PrepareSockets()
|
|
|
|
{
|
|
|
|
if (LockHasError()) {
|
|
|
|
ClearSocketList();
|
|
|
|
return std::chrono::steady_clock::duration(-1);
|
2017-01-11 21:31:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
return PrepareAlsaPcmSockets(*this, pcm, pfd_buffer);
|
2017-01-11 21:31:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
void
|
|
|
|
AlsaOutput::DispatchSockets()
|
|
|
|
try {
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
if (drain) {
|
|
|
|
{
|
|
|
|
ScopeUnlock unlock(mutex);
|
|
|
|
if (!DrainInternal())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MultiSocketMonitor::InvalidateSockets();
|
|
|
|
}
|
|
|
|
|
|
|
|
drain = false;
|
|
|
|
cond.signal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:01:11 +01:00
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
if (must_prepare) {
|
|
|
|
must_prepare = false;
|
2014-03-02 11:12:09 +01:00
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
int err = snd_pcm_prepare(pcm);
|
2016-11-03 17:28:34 +01:00
|
|
|
if (err < 0)
|
|
|
|
throw FormatRuntimeError("snd_pcm_prepare() failed: %s",
|
|
|
|
snd_strerror(-err));
|
2014-03-02 11:12:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
CopyRingToPeriodBuffer();
|
|
|
|
|
|
|
|
if (period_buffer.IsEmpty())
|
|
|
|
/* insert some silence if the buffer has not enough
|
|
|
|
data yet, to avoid ALSA xrun */
|
|
|
|
period_buffer.FillWithSilence(silence, out_frame_size);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} catch (const std::runtime_error &) {
|
|
|
|
MultiSocketMonitor::Reset();
|
2014-08-31 15:01:12 +02:00
|
|
|
|
2017-01-24 23:10:35 +01:00
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
error = std::current_exception();
|
|
|
|
cond.signal();
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:53:56 +01:00
|
|
|
typedef AudioOutputWrapper<AlsaOutput> Wrapper;
|
|
|
|
|
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,
|
2015-01-04 19:53:56 +01:00
|
|
|
&Wrapper::Init,
|
|
|
|
&Wrapper::Finish,
|
|
|
|
&Wrapper::Enable,
|
|
|
|
&Wrapper::Disable,
|
|
|
|
&Wrapper::Open,
|
|
|
|
&Wrapper::Close,
|
2013-01-29 14:32:32 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2015-01-04 19:53:56 +01:00
|
|
|
&Wrapper::Play,
|
|
|
|
&Wrapper::Drain,
|
|
|
|
&Wrapper::Cancel,
|
2013-01-29 14:32:32 +01:00
|
|
|
nullptr,
|
|
|
|
|
|
|
|
&alsa_mixer_plugin,
|
2005-03-05 06:22:30 +01:00
|
|
|
};
|