all: clean up cAudio header, separate private stuff

This commit is contained in:
Stefan Seyfried
2013-11-02 20:06:29 +01:00
parent 69527185a9
commit 1166d49486
15 changed files with 366 additions and 594 deletions

View File

@@ -4,7 +4,7 @@
#include <sys/ioctl.h>
#include <unistd.h>
#include <hardware/aud/aud_inf.h>
#include <hardware/tddevices.h>
#include <avs/avs_inf.h>
#define AUDIO_DEVICE "/dev/" DEVICE_NAME_AUDIO
@@ -17,60 +17,63 @@
cAudio * audioDecoder = NULL;
typedef struct audio_pdata
{
int fd;
int clipfd;
int mixer_fd;
int mixer_num;
} audio_pdata;
#define P ((audio_pdata *)pdata)
cAudio::cAudio(void *, void *, void *)
{
fd = -1;
clipfd = -1;
mixer_fd = -1;
openDevice();
Muted = false;
pdata = calloc(1, sizeof(audio_pdata));
P->clipfd = -1;
P->mixer_fd = -1;
int fd = open(AUDIO_DEVICE, O_RDWR);
if (fd < 0)
lt_info("%s: open failed (%m)\n", __func__);
fcntl(fd, F_SETFD, FD_CLOEXEC);
P->fd = fd;
SetMute(true);
muted = false;
}
cAudio::~cAudio(void)
{
closeDevice();
if (P->fd >= 0)
close(P->fd);
if (P->clipfd >= 0)
close(P->clipfd);
if (P->mixer_fd >= 0)
close(P->mixer_fd);
free(pdata);
}
void cAudio::openDevice(void)
int cAudio::mute(void)
{
if (fd < 0)
{
if ((fd = open(AUDIO_DEVICE, O_RDWR)) < 0)
lt_info("openDevice: open failed (%m)\n");
fcntl(fd, F_SETFD, FD_CLOEXEC);
do_mute(true, false);
}
else
lt_info("openDevice: already open (fd = %d)\n", fd);
return SetMute(true);
}
void cAudio::closeDevice(void)
int cAudio::unmute(void)
{
if (fd >= 0)
close(fd);
fd = -1;
if (clipfd >= 0)
close(clipfd);
clipfd = -1;
if (mixer_fd >= 0)
close(mixer_fd);
mixer_fd = -1;
return SetMute(false);
}
int cAudio::do_mute(bool enable, bool remember)
int cAudio::SetMute(bool enable)
{
lt_debug("%s(%d, %d)\n", __FUNCTION__, enable, remember);
lt_debug("%s(%d)\n", __func__, enable);
int avsfd;
int ret;
if (remember)
Muted = enable;
ret = ioctl(fd, MPEG_AUD_SET_MUTE, enable);
muted = enable;
ret = ioctl(P->fd, MPEG_AUD_SET_MUTE, enable);
if (ret < 0)
lt_info("%s(%d) failed (%m)\n", __FUNCTION__, (int)enable);
/* are we using alternative DSP / mixer? */
if (clipfd != -1 || mixer_fd != -1)
setVolume(volume,volume); /* considers "Muted" variable, "remember"
if (P->clipfd != -1 || P->mixer_fd != -1)
setVolume(volume,volume); /* considers "muted" variable, "remember"
is basically always true in this context */
avsfd = open("/dev/stb/tdsystem", O_RDONLY);
if (avsfd >= 0)
@@ -103,14 +106,14 @@ int cAudio::setVolume(unsigned int left, unsigned int right)
int vr = map_volume(right);
volume = (left + right) / 2;
int v = map_volume(volume);
if (clipfd != -1 && mixer_fd != -1) {
if (P->clipfd != -1 && P->mixer_fd != -1) {
int tmp = 0;
/* not sure if left / right is correct here, but it is always the same anyways ;-) */
if (! Muted)
if (! muted)
tmp = left << 8 | right;
ret = ioctl(mixer_fd, MIXER_WRITE(mixer_num), &tmp);
ret = ioctl(P->mixer_fd, MIXER_WRITE(P->mixer_num), &tmp);
if (ret == -1)
lt_info("%s: MIXER_WRITE(%d),%04x: %m\n", __func__, mixer_num, tmp);
lt_info("%s: MIXER_WRITE(%d),%04x: %m\n", __func__, P->mixer_num, tmp);
return ret;
}
// if (settings.volume_type == CControld::TYPE_OST || forcetype == (int)CControld::TYPE_OST)
@@ -122,7 +125,7 @@ int cAudio::setVolume(unsigned int left, unsigned int right)
vol.rearright = vr;
vol.center = v;
vol.lfe = v;
ret = ioctl(fd, MPEG_AUD_SET_VOL, &vol);
ret = ioctl(P->fd, MPEG_AUD_SET_VOL, &vol);
if (ret < 0)
lt_info("setVolume MPEG_AUD_SET_VOL failed (%m)\n");
return ret;
@@ -146,17 +149,17 @@ int cAudio::setVolume(unsigned int left, unsigned int right)
int cAudio::Start(void)
{
int ret;
ret = ioctl(fd, MPEG_AUD_PLAY);
int fd = P->fd;
int ret = ioctl(fd, MPEG_AUD_PLAY);
/* this seems to be not strictly necessary since neutrino
re-mutes all the time, but is certainly more correct */
ioctl(fd, MPEG_AUD_SET_MUTE, Muted);
ioctl(fd, MPEG_AUD_SET_MUTE, muted);
return ret;
}
int cAudio::Stop(void)
{
return ioctl(fd, MPEG_AUD_STOP);
return ioctl(P->fd, MPEG_AUD_STOP);
}
bool cAudio::Pause(bool /*Pcm*/)
@@ -167,6 +170,7 @@ bool cAudio::Pause(bool /*Pcm*/)
void cAudio::SetSyncMode(AVSYNC_TYPE Mode)
{
lt_debug("%s %d\n", __FUNCTION__, Mode);
int fd = P->fd;
switch (Mode)
{
case 0:
@@ -180,19 +184,25 @@ void cAudio::SetSyncMode(AVSYNC_TYPE Mode)
void cAudio::SetStreamType(AUDIO_FORMAT type)
{
int bypass_disable;
lt_debug("%s %d\n", __FUNCTION__, type);
StreamType = type;
int fd = P->fd;
if (type != AUDIO_FMT_DOLBY_DIGITAL && type != AUDIO_FMT_MPEG && type != AUDIO_FMT_MPG1)
lt_info("%s unhandled AUDIO_FORMAT %d\n", __func__, type);
if (StreamType != AUDIO_FMT_DOLBY_DIGITAL && StreamType != AUDIO_FMT_MPEG && StreamType != AUDIO_FMT_MPG1)
lt_info("%s unhandled AUDIO_FORMAT %d\n", __FUNCTION__, StreamType);
bool bypass_disable = (type != AUDIO_FMT_DOLBY_DIGITAL);
if (bypass_disable)
ioctl(fd, MPEG_AUD_SET_MODE, AUD_MODE_MPEG);
else {
/* dvb2001 does always set AUD_MODE_DTS before setting AUD_MODE_AC3,
this might be some workaround, so we do the same... */
ioctl(fd, MPEG_AUD_SET_MODE, AUD_MODE_DTS);
ioctl(fd, MPEG_AUD_SET_MODE, AUD_MODE_AC3);
}
//setBypassMode(bypass_disable);
bypass_disable = (StreamType != AUDIO_FMT_DOLBY_DIGITAL);
setBypassMode(bypass_disable);
if (StreamType == AUDIO_FMT_MPEG)
if (type == AUDIO_FMT_MPEG)
ioctl(fd, MPEG_AUD_SET_STREAM_TYPE, AUD_STREAM_TYPE_PES);
if (StreamType == AUDIO_FMT_MPG1)
if (type == AUDIO_FMT_MPG1)
ioctl(fd, MPEG_AUD_SET_STREAM_TYPE, AUD_STREAM_TYPE_MPEG1);
};
@@ -209,12 +219,12 @@ int cAudio::PrepareClipPlay(int ch, int srate, int bits, int little_endian)
const char *dsp_dev = getenv("DSP_DEVICE");
const char *mix_dev = getenv("MIX_DEVICE");
lt_debug("%s ch %d srate %d bits %d le %d\n", __FUNCTION__, ch, srate, bits, little_endian);
if (clipfd >= 0) {
lt_info("%s: clipfd already opened (%d)\n", __FUNCTION__, clipfd);
if (P->clipfd >= 0) {
lt_info("%s: clipfd already opened (%d)\n", __func__, P->clipfd);
return -1;
}
mixer_num = -1;
mixer_fd = -1;
P->mixer_num = -1;
P->mixer_fd = -1;
/* a different DSP device can be given with DSP_DEVICE and MIX_DEVICE
* if this device cannot be opened, we fall back to the internal TD OSS device
* Example:
@@ -232,42 +242,42 @@ int cAudio::PrepareClipPlay(int ch, int srate, int bits, int little_endian)
}
lt_info("%s: dsp_dev %s mix_dev %s\n", __func__, dsp_dev, mix_dev); /* NULL mix_dev is ok */
/* the tdoss dsp driver seems to work only on the second open(). really. */
clipfd = open(dsp_dev, O_WRONLY);
close(clipfd);
clipfd = open(dsp_dev, O_WRONLY);
if (clipfd < 0) {
P->clipfd = open(dsp_dev, O_WRONLY);
close(P->clipfd);
P->clipfd = open(dsp_dev, O_WRONLY);
if (P->clipfd < 0) {
lt_info("%s open %s: %m\n", dsp_dev, __FUNCTION__);
return -1;
}
fcntl(clipfd, F_SETFD, FD_CLOEXEC);
fcntl(P->clipfd, F_SETFD, FD_CLOEXEC);
/* no idea if we ever get little_endian == 0 */
if (little_endian)
fmt = AFMT_S16_BE;
else
fmt = AFMT_S16_LE;
if (ioctl(clipfd, SNDCTL_DSP_SETFMT, &fmt))
if (ioctl(P->clipfd, SNDCTL_DSP_SETFMT, &fmt))
perror("SNDCTL_DSP_SETFMT");
if (ioctl(clipfd, SNDCTL_DSP_CHANNELS, &ch))
if (ioctl(P->clipfd, SNDCTL_DSP_CHANNELS, &ch))
perror("SNDCTL_DSP_CHANNELS");
if (ioctl(clipfd, SNDCTL_DSP_SPEED, &srate))
if (ioctl(P->clipfd, SNDCTL_DSP_SPEED, &srate))
perror("SNDCTL_DSP_SPEED");
if (ioctl(clipfd, SNDCTL_DSP_RESET))
if (ioctl(P->clipfd, SNDCTL_DSP_RESET))
perror("SNDCTL_DSP_RESET");
if (!mix_dev)
return 0;
mixer_fd = open(mix_dev, O_RDWR);
if (mixer_fd < 0) {
P->mixer_fd = open(mix_dev, O_RDWR);
if (P->mixer_fd < 0) {
lt_info("%s: open mixer %s failed (%m)\n", __func__, mix_dev);
/* not a real error */
return 0;
}
if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
if (ioctl(P->mixer_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
lt_info("%s: SOUND_MIXER_READ_DEVMASK %m\n", __func__);
devmask = 0;
}
if (ioctl(mixer_fd, SOUND_MIXER_READ_STEREODEVS, &stereo) == -1) {
if (ioctl(P->mixer_fd, SOUND_MIXER_READ_STEREODEVS, &stereo) == -1) {
lt_info("%s: SOUND_MIXER_READ_STEREODEVS %m\n", __func__);
stereo = 0;
}
@@ -275,8 +285,8 @@ int cAudio::PrepareClipPlay(int ch, int srate, int bits, int little_endian)
if (usable == 0) {
lt_info("%s: devmask: %08x stereo: %08x, no usable dev :-(\n",
__func__, devmask, stereo);
close(mixer_fd);
mixer_fd = -1;
close(P->mixer_fd);
P->mixer_fd = -1;
return 0; /* TODO: should we treat this as error? */
}
/* __builtin_popcount needs GCC, it counts the set bits... */
@@ -287,14 +297,14 @@ int cAudio::PrepareClipPlay(int ch, int srate, int bits, int little_endian)
__func__, devmask, stereo, __func__);
const char *tmp = getenv("MIX_NUMBER");
if (tmp)
mixer_num = atoi(tmp);
P->mixer_num = atoi(tmp);
lt_info("%s: mixer_num is %d -> device %08x\n",
__func__, (mixer_num >= 0) ? (1 << mixer_num) : 0);
__func__, P->mixer_num, (P->mixer_num >= 0) ? (1 << P->mixer_num) : 0);
/* no error checking, you'd better know what you are doing... */
} else {
mixer_num = 0;
P->mixer_num = 0;
while (!(usable & 0x01)) {
mixer_num++;
P->mixer_num++;
usable >>= 1;
}
}
@@ -307,11 +317,11 @@ int cAudio::WriteClip(unsigned char *buffer, int size)
{
int ret;
// lt_debug("cAudio::%s\n", __FUNCTION__);
if (clipfd <= 0) {
if (P->clipfd <= 0) {
lt_info("%s: clipfd not yet opened\n", __FUNCTION__);
return -1;
}
ret = write(clipfd, buffer, size);
ret = write(P->clipfd, buffer, size);
if (ret < 0)
lt_info("%s: write error (%m)\n", __FUNCTION__);
return ret;
@@ -320,15 +330,15 @@ int cAudio::WriteClip(unsigned char *buffer, int size)
int cAudio::StopClip()
{
lt_debug("%s\n", __FUNCTION__);
if (clipfd <= 0) {
if (P->clipfd <= 0) {
lt_info("%s: clipfd not yet opened\n", __FUNCTION__);
return -1;
}
close(clipfd);
clipfd = -1;
if (mixer_fd >= 0)
close(mixer_fd);
mixer_fd = -1;
close(P->clipfd);
P->clipfd = -1;
if (P->mixer_fd >= 0)
close(P->mixer_fd);
P->mixer_fd = -1;
setVolume(volume, volume);
return 0;
};
@@ -340,6 +350,7 @@ void cAudio::getAudioInfo(int &type, int &layer, int &freq, int &bitrate, int &m
static const int freq_mpg[] = {44100, 48000, 32000, 0};
static const int freq_ac3[] = {48000, 44100, 32000, 0};
scratchl2 i;
int fd = P->fd;
if (ioctl(fd, MPEG_AUD_GET_DECTYP, &atype) < 0)
perror("cAudio::getAudioInfo MPEG_AUD_GET_DECTYP");
if (ioctl(fd, MPEG_AUD_GET_STATUS, &i) < 0)
@@ -385,6 +396,11 @@ void cAudio::SetSpdifDD(bool enable)
lt_debug("%s %d\n", __FUNCTION__, enable);
};
void cAudio::SetHdmiDD(bool enable)
{
lt_debug("%s %d\n", __func__, enable);
};
void cAudio::ScheduleMute(bool On)
{
lt_debug("%s %d\n", __FUNCTION__, On);
@@ -395,6 +411,7 @@ void cAudio::EnableAnalogOut(bool enable)
lt_debug("%s %d\n", __FUNCTION__, enable);
};
#if 0
void cAudio::setBypassMode(bool disable)
{
lt_debug("%s %d\n", __FUNCTION__, disable);
@@ -412,3 +429,4 @@ void cAudio::setBypassMode(bool disable)
/* all those ioctl aways return "invalid argument", but they seem to
work anyway, so there's no use in checking the return value */
}
#endif