3 * Copyright (C) 2010-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
25 #include "cores/AudioEngine/Interfaces/AESink.h"
26 #include "cores/AudioEngine/Utils/AEDeviceInfo.h"
29 #define ALSA_PCM_NEW_HW_PARAMS_API
30 #include <alsa/asoundlib.h>
32 #include "threads/CriticalSection.h"
34 class CAESinkALSA : public IAESink
37 virtual const char *GetName() { return "ALSA"; }
40 virtual ~CAESinkALSA();
42 virtual bool Initialize (AEAudioFormat &format, std::string &device);
43 virtual void Deinitialize();
46 virtual double GetDelay ();
47 virtual double GetCacheTotal ();
48 virtual unsigned int AddPackets (uint8_t *data, unsigned int frames, bool hasAudio, bool blocking = false);
49 virtual void Drain ();
51 static void EnumerateDevicesEx(AEDeviceInfoList &list, bool force = false);
53 CAEChannelInfo GetChannelLayout(AEAudioFormat format, unsigned int minChannels, unsigned int maxChannels);
54 void GetAESParams(const AEAudioFormat format, std::string& params);
55 void HandleError(const char* name, int err);
57 std::string m_initDevice;
58 AEAudioFormat m_initFormat;
59 AEAudioFormat m_format;
60 unsigned int m_bufferSize;
61 double m_formatSampleRateMul;
69 unsigned int sampleRate;
70 unsigned int periodSize;
71 unsigned int frameSize;
72 unsigned int channels;
76 static snd_pcm_format_t AEFormatToALSAFormat(const enum AEDataFormat format);
78 bool InitializeHW(const ALSAConfig &inconfig, ALSAConfig &outconfig);
79 bool InitializeSW(const ALSAConfig &inconfig);
81 static void AppendParams(std::string &device, const std::string ¶ms);
82 static bool TryDevice(const std::string &name, snd_pcm_t **pcmp, snd_config_t *lconf);
83 static bool TryDeviceWithParams(const std::string &name, const std::string ¶ms, snd_pcm_t **pcmp, snd_config_t *lconf);
84 static bool OpenPCMDevice(const std::string &name, const std::string ¶ms, int channels, snd_pcm_t **pcmp, snd_config_t *lconf);
86 static AEDeviceType AEDeviceTypeFromName(const std::string &name);
87 static std::string GetParamFromName(const std::string &name, const std::string ¶m);
88 static void EnumerateDevice(AEDeviceInfoList &list, const std::string &device, const std::string &description, snd_config_t *config);
89 static bool SoundDeviceExists(const std::string& device);
90 static bool GetELD(snd_hctl_t *hctl, int device, CAEDeviceInfo& info, bool& badHDMI);
92 static void sndLibErrorHandler(const char *file, int line, const char *function, int err, const char *fmt, ...);