/***************************************************************************
* Copyright (C) 2007 by Uriy Zhuravlev stalkerg@gmail.com *
* *
* Copyright (c) 2000-2001 Brad Hughes bhughes@trolltech.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QApplication>
#include <QSettings>
#include <QDir>
extern "C"
{
#ifdef HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#else
#include <soundcard.h>
#endif
}
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <qmmp/buffer.h>
#include <qmmp/visual.h>
#include "outputoss.h"
OutputOSS::OutputOSS(QObject * parent) : Output(parent), do_select(true), m_audio_fd(-1)
{
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
m_audio_device = settings.value("OSS/device","/dev/dsp").toString();
}
OutputOSS::~OutputOSS()
{
if (m_audio_fd >= 0)
{
ioctl(m_audio_fd, SNDCTL_DSP_RESET, 0);
close(m_audio_fd);
m_audio_fd = -1;
}
}
void OutputOSS::configure(quint32 freq, int chan, Qmmp::AudioFormat format)
{
int p;
switch (format)
{
case Qmmp::PCM_S16LE:
p = AFMT_S16_LE;
break;
case Qmmp::PCM_S8:
p = AFMT_S8;
break;
default:
qWarning("OutputOSS: unsupported audio format");
return;
}
int param = p;
if (ioctl(m_audio_fd, SNDCTL_DSP_SETFMT, &p) < 0)
{
qWarning("OutputOSS: ioctl SNDCTL_DSP_SETFMT failed: %s",strerror(errno));
return;
}
if(param != p)
{
qWarning("OutputOSS: unsupported audio format");
return;
}
param = chan;
if(ioctl(m_audio_fd, SNDCTL_DSP_CHANNELS, &chan) < 0)
{
qWarning("OutputOSS: ioctl SNDCTL_DSP_CHANNELS failed: %s", strerror(errno));
return;
}
if(param != chan)
{
qWarning("OutputOSS: unsupported %d-channel mode", param);
return;
}
uint param2 = freq;
if (ioctl(m_audio_fd, SNDCTL_DSP_SPEED, &freq) < 0)
{
qWarning("OutputOSS: ioctl SNDCTL_DSP_SPEED failed: %s", strerror(errno));
return;
}
if(param2 != freq)
{
qWarning("OutputOSS: unsupported sample rate");
return;
}
ioctl(m_audio_fd, SNDCTL_DSP_RESET, 0);
Output::configure(freq, chan, format);
}
void OutputOSS::post()
{
ioctl(m_audio_fd, SNDCTL_DSP_POST, 0);
}
void OutputOSS::sync()
{
ioctl(m_audio_fd, SNDCTL_DSP_SYNC, 0);
}
bool OutputOSS::initialize()
{
m_audio_fd = open(m_audio_device.toAscii(), O_WRONLY, 0);
if (m_audio_fd < 0)
{
qWarning("OSSOutput: failed to open output device '%s'", qPrintable(m_audio_device));
return false;
}
int flags;
if ((flags = fcntl(m_audio_fd, F_GETFL, 0)) > 0)
{
flags &= O_NDELAY;
fcntl(m_audio_fd, F_SETFL, flags);
}
fd_set afd;
FD_ZERO(&afd);
FD_SET(m_audio_fd, &afd);
struct timeval tv;
tv.tv_sec = 0l;
tv.tv_usec = 50000l;
do_select = (select(m_audio_fd + 1, 0, &afd, 0, &tv) > 0);
return true;
}
qint64 OutputOSS::latency()
{
//ulong used = 0;
/*if (ioctl(m_audio_fd, SNDCTL_DSP_GETODELAY, &used) == -1)
used = 0;*/
return 0;
}
qint64 OutputOSS::writeAudio(unsigned char *data, qint64 maxSize)
{
fd_set afd;