output/oss: move functions into the struct
This commit is contained in:
parent
674d14879f
commit
c150fd9a1c
@ -20,6 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "OssOutputPlugin.hxx"
|
#include "OssOutputPlugin.hxx"
|
||||||
#include "../OutputAPI.hxx"
|
#include "../OutputAPI.hxx"
|
||||||
|
#include "../Wrapper.hxx"
|
||||||
#include "mixer/MixerList.hxx"
|
#include "mixer/MixerList.hxx"
|
||||||
#include "system/fd_util.h"
|
#include "system/fd_util.h"
|
||||||
#include "util/ConstBuffer.hxx"
|
#include "util/ConstBuffer.hxx"
|
||||||
@ -86,6 +87,41 @@ struct OssOutput {
|
|||||||
bool Initialize(const config_param ¶m, Error &error_r) {
|
bool Initialize(const config_param ¶m, Error &error_r) {
|
||||||
return base.Configure(param, error_r);
|
return base.Configure(param, error_r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static OssOutput *Create(const config_param ¶m, Error &error);
|
||||||
|
|
||||||
|
#ifdef AFMT_S24_PACKED
|
||||||
|
bool Enable(gcc_unused Error &error) {
|
||||||
|
pcm_export.Construct();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Disable() {
|
||||||
|
pcm_export.Destruct();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool Open(AudioFormat &audio_format, Error &error);
|
||||||
|
|
||||||
|
void Close() {
|
||||||
|
DoClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Play(const void *chunk, size_t size, Error &error);
|
||||||
|
void Cancel();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Sets up the OSS device which was opened before.
|
||||||
|
*/
|
||||||
|
bool Setup(AudioFormat &audio_format, Error &error);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reopen the device with the saved audio_format, without any probing.
|
||||||
|
*/
|
||||||
|
bool Reopen(Error &error);
|
||||||
|
|
||||||
|
void DoClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr Domain oss_output_domain("oss_output");
|
static constexpr Domain oss_output_domain("oss_output");
|
||||||
@ -147,7 +183,7 @@ oss_output_test_default_device(void)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static AudioOutput *
|
static OssOutput *
|
||||||
oss_open_default(Error &error)
|
oss_open_default(Error &error)
|
||||||
{
|
{
|
||||||
int err[ARRAY_SIZE(default_devices)];
|
int err[ARRAY_SIZE(default_devices)];
|
||||||
@ -164,7 +200,7 @@ oss_open_default(Error &error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
od->device = default_devices[i];
|
od->device = default_devices[i];
|
||||||
return &od->base;
|
return od;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,8 +233,8 @@ oss_open_default(Error &error)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static AudioOutput *
|
inline OssOutput *
|
||||||
oss_output_init(const config_param ¶m, Error &error)
|
OssOutput::Create(const config_param ¶m, Error &error)
|
||||||
{
|
{
|
||||||
const char *device = param.GetBlockValue("device");
|
const char *device = param.GetBlockValue("device");
|
||||||
if (device != NULL) {
|
if (device != NULL) {
|
||||||
@ -209,47 +245,18 @@ oss_output_init(const config_param ¶m, Error &error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
od->device = device;
|
od->device = device;
|
||||||
return &od->base;
|
return od;
|
||||||
}
|
}
|
||||||
|
|
||||||
return oss_open_default(error);
|
return oss_open_default(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void
|
||||||
oss_output_finish(AudioOutput *ao)
|
OssOutput::DoClose()
|
||||||
{
|
{
|
||||||
OssOutput *od = (OssOutput *)ao;
|
if (fd >= 0)
|
||||||
|
close(fd);
|
||||||
delete od;
|
fd = -1;
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef AFMT_S24_PACKED
|
|
||||||
|
|
||||||
static bool
|
|
||||||
oss_output_enable(AudioOutput *ao, gcc_unused Error &error)
|
|
||||||
{
|
|
||||||
OssOutput *od = (OssOutput *)ao;
|
|
||||||
|
|
||||||
od->pcm_export.Construct();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
oss_output_disable(AudioOutput *ao)
|
|
||||||
{
|
|
||||||
OssOutput *od = (OssOutput *)ao;
|
|
||||||
|
|
||||||
od->pcm_export.Destruct();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
|
||||||
oss_close(OssOutput *od)
|
|
||||||
{
|
|
||||||
if (od->fd >= 0)
|
|
||||||
close(od->fd);
|
|
||||||
od->fd = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -609,18 +616,14 @@ oss_setup_sample_format(int fd, AudioFormat &audio_format,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
inline bool
|
||||||
* Sets up the OSS device which was opened before.
|
OssOutput::Setup(AudioFormat &_audio_format, Error &error)
|
||||||
*/
|
|
||||||
static bool
|
|
||||||
oss_setup(OssOutput *od, AudioFormat &audio_format,
|
|
||||||
Error &error)
|
|
||||||
{
|
{
|
||||||
return oss_setup_channels(od->fd, audio_format, error) &&
|
return oss_setup_channels(fd, _audio_format, error) &&
|
||||||
oss_setup_sample_rate(od->fd, audio_format, error) &&
|
oss_setup_sample_rate(fd, _audio_format, error) &&
|
||||||
oss_setup_sample_format(od->fd, audio_format, &od->oss_format,
|
oss_setup_sample_format(fd, _audio_format, &oss_format,
|
||||||
#ifdef AFMT_S24_PACKED
|
#ifdef AFMT_S24_PACKED
|
||||||
od->pcm_export,
|
pcm_export,
|
||||||
#endif
|
#endif
|
||||||
error);
|
error);
|
||||||
}
|
}
|
||||||
@ -628,46 +631,46 @@ oss_setup(OssOutput *od, AudioFormat &audio_format,
|
|||||||
/**
|
/**
|
||||||
* Reopen the device with the saved audio_format, without any probing.
|
* Reopen the device with the saved audio_format, without any probing.
|
||||||
*/
|
*/
|
||||||
static bool
|
inline bool
|
||||||
oss_reopen(OssOutput *od, Error &error)
|
OssOutput::Reopen(Error &error)
|
||||||
{
|
{
|
||||||
assert(od->fd < 0);
|
assert(fd < 0);
|
||||||
|
|
||||||
od->fd = open_cloexec(od->device, O_WRONLY, 0);
|
fd = open_cloexec(device, O_WRONLY, 0);
|
||||||
if (od->fd < 0) {
|
if (fd < 0) {
|
||||||
error.FormatErrno("Error opening OSS device \"%s\"",
|
error.FormatErrno("Error opening OSS device \"%s\"",
|
||||||
od->device);
|
device);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum oss_setup_result result;
|
enum oss_setup_result result;
|
||||||
|
|
||||||
const char *const msg1 = "Failed to set channel count";
|
const char *const msg1 = "Failed to set channel count";
|
||||||
result = oss_try_ioctl(od->fd, SNDCTL_DSP_CHANNELS,
|
result = oss_try_ioctl(fd, SNDCTL_DSP_CHANNELS,
|
||||||
od->audio_format.channels, msg1, error);
|
audio_format.channels, msg1, error);
|
||||||
if (result != SUCCESS) {
|
if (result != SUCCESS) {
|
||||||
oss_close(od);
|
DoClose();
|
||||||
if (result == UNSUPPORTED)
|
if (result == UNSUPPORTED)
|
||||||
error.Set(oss_output_domain, msg1);
|
error.Set(oss_output_domain, msg1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *const msg2 = "Failed to set sample rate";
|
const char *const msg2 = "Failed to set sample rate";
|
||||||
result = oss_try_ioctl(od->fd, SNDCTL_DSP_SPEED,
|
result = oss_try_ioctl(fd, SNDCTL_DSP_SPEED,
|
||||||
od->audio_format.sample_rate, msg2, error);
|
audio_format.sample_rate, msg2, error);
|
||||||
if (result != SUCCESS) {
|
if (result != SUCCESS) {
|
||||||
oss_close(od);
|
DoClose();
|
||||||
if (result == UNSUPPORTED)
|
if (result == UNSUPPORTED)
|
||||||
error.Set(oss_output_domain, msg2);
|
error.Set(oss_output_domain, msg2);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *const msg3 = "Failed to set sample format";
|
const char *const msg3 = "Failed to set sample format";
|
||||||
result = oss_try_ioctl(od->fd, SNDCTL_DSP_SAMPLESIZE,
|
result = oss_try_ioctl(fd, SNDCTL_DSP_SAMPLESIZE,
|
||||||
od->oss_format,
|
oss_format,
|
||||||
msg3, error);
|
msg3, error);
|
||||||
if (result != SUCCESS) {
|
if (result != SUCCESS) {
|
||||||
oss_close(od);
|
DoClose();
|
||||||
if (result == UNSUPPORTED)
|
if (result == UNSUPPORTED)
|
||||||
error.Set(oss_output_domain, msg3);
|
error.Set(oss_output_domain, msg3);
|
||||||
return false;
|
return false;
|
||||||
@ -676,62 +679,47 @@ oss_reopen(OssOutput *od, Error &error)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
inline bool
|
||||||
oss_output_open(AudioOutput *ao, AudioFormat &audio_format,
|
OssOutput::Open(AudioFormat &_audio_format, Error &error)
|
||||||
Error &error)
|
|
||||||
{
|
{
|
||||||
OssOutput *od = (OssOutput *)ao;
|
fd = open_cloexec(device, O_WRONLY, 0);
|
||||||
|
if (fd < 0) {
|
||||||
od->fd = open_cloexec(od->device, O_WRONLY, 0);
|
|
||||||
if (od->fd < 0) {
|
|
||||||
error.FormatErrno("Error opening OSS device \"%s\"",
|
error.FormatErrno("Error opening OSS device \"%s\"",
|
||||||
od->device);
|
device);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!oss_setup(od, audio_format, error)) {
|
if (!Setup(_audio_format, error)) {
|
||||||
oss_close(od);
|
DoClose();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
od->audio_format = audio_format;
|
audio_format = _audio_format;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
inline void
|
||||||
oss_output_close(AudioOutput *ao)
|
OssOutput::Cancel()
|
||||||
{
|
{
|
||||||
OssOutput *od = (OssOutput *)ao;
|
if (fd >= 0) {
|
||||||
|
ioctl(fd, SNDCTL_DSP_RESET, 0);
|
||||||
oss_close(od);
|
DoClose();
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
oss_output_cancel(AudioOutput *ao)
|
|
||||||
{
|
|
||||||
OssOutput *od = (OssOutput *)ao;
|
|
||||||
|
|
||||||
if (od->fd >= 0) {
|
|
||||||
ioctl(od->fd, SNDCTL_DSP_RESET, 0);
|
|
||||||
oss_close(od);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
inline size_t
|
||||||
oss_output_play(AudioOutput *ao, const void *chunk, size_t size,
|
OssOutput::Play(const void *chunk, size_t size, Error &error)
|
||||||
Error &error)
|
|
||||||
{
|
{
|
||||||
OssOutput *od = (OssOutput *)ao;
|
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
||||||
/* reopen the device since it was closed by dropBufferedAudio */
|
/* reopen the device since it was closed by dropBufferedAudio */
|
||||||
if (od->fd < 0 && !oss_reopen(od, error))
|
if (fd < 0 && !Reopen(error))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
#ifdef AFMT_S24_PACKED
|
#ifdef AFMT_S24_PACKED
|
||||||
const auto e = od->pcm_export->Export({chunk, size});
|
const auto e = pcm_export->Export({chunk, size});
|
||||||
chunk = e.data;
|
chunk = e.data;
|
||||||
size = e.size;
|
size = e.size;
|
||||||
#endif
|
#endif
|
||||||
@ -739,40 +727,42 @@ oss_output_play(AudioOutput *ao, const void *chunk, size_t size,
|
|||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
ret = write(od->fd, chunk, size);
|
ret = write(fd, chunk, size);
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
#ifdef AFMT_S24_PACKED
|
#ifdef AFMT_S24_PACKED
|
||||||
ret = od->pcm_export->CalcSourceSize(ret);
|
ret = pcm_export->CalcSourceSize(ret);
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret < 0 && errno != EINTR) {
|
if (ret < 0 && errno != EINTR) {
|
||||||
error.FormatErrno("Write error on %s", od->device);
|
error.FormatErrno("Write error on %s", device);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef AudioOutputWrapper<OssOutput> Wrapper;
|
||||||
|
|
||||||
const struct AudioOutputPlugin oss_output_plugin = {
|
const struct AudioOutputPlugin oss_output_plugin = {
|
||||||
"oss",
|
"oss",
|
||||||
oss_output_test_default_device,
|
oss_output_test_default_device,
|
||||||
oss_output_init,
|
&Wrapper::Init,
|
||||||
oss_output_finish,
|
&Wrapper::Finish,
|
||||||
#ifdef AFMT_S24_PACKED
|
#ifdef AFMT_S24_PACKED
|
||||||
oss_output_enable,
|
&Wrapper::Enable,
|
||||||
oss_output_disable,
|
&Wrapper::Disable,
|
||||||
#else
|
#else
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
#endif
|
#endif
|
||||||
oss_output_open,
|
&Wrapper::Open,
|
||||||
oss_output_close,
|
&Wrapper::Close,
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
oss_output_play,
|
&Wrapper::Play,
|
||||||
nullptr,
|
nullptr,
|
||||||
oss_output_cancel,
|
&Wrapper::Cancel,
|
||||||
nullptr,
|
nullptr,
|
||||||
|
|
||||||
&oss_mixer_plugin,
|
&oss_mixer_plugin,
|
||||||
|
Loading…
Reference in New Issue
Block a user