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 "Interfaces/AESink.h"
26 #include "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();
44 virtual bool IsCompatible(const AEAudioFormat &format, const std::string &device);
47 virtual double GetDelay ();
48 virtual double GetCacheTime ();
49 virtual double GetCacheTotal ();
50 virtual unsigned int AddPackets (uint8_t *data, unsigned int frames, bool hasAudio, bool blocking = false);
51 virtual void Drain ();
52 virtual bool SoftSuspend();
53 virtual bool SoftResume();
55 static void EnumerateDevicesEx(AEDeviceInfoList &list, bool force = false);
57 CAEChannelInfo GetChannelLayout(AEAudioFormat format);
58 void GetAESParams(const AEAudioFormat format, std::string& params);
59 void HandleError(const char* name, int err);
61 std::string m_initDevice;
62 AEAudioFormat m_initFormat;
63 AEAudioFormat m_format;
64 unsigned int m_bufferSize;
65 double m_formatSampleRateMul;
71 static snd_pcm_format_t AEFormatToALSAFormat(const enum AEDataFormat format);
73 bool InitializeHW(AEAudioFormat &format);
74 bool InitializeSW(AEAudioFormat &format);
76 static void AppendParams(std::string &device, const std::string ¶ms);
77 static bool TryDevice(const std::string &name, snd_pcm_t **pcmp, snd_config_t *lconf);
78 static bool TryDeviceWithParams(const std::string &name, const std::string ¶ms, snd_pcm_t **pcmp, snd_config_t *lconf);
79 static bool OpenPCMDevice(const std::string &name, const std::string ¶ms, int channels, snd_pcm_t **pcmp, snd_config_t *lconf);
81 static AEDeviceType AEDeviceTypeFromName(const std::string &name);
82 static std::string GetParamFromName(const std::string &name, const std::string ¶m);
83 static void EnumerateDevice(AEDeviceInfoList &list, const std::string &device, const std::string &description, snd_config_t *config);
84 static bool SoundDeviceExists(const std::string& device);
85 static bool GetELD(snd_hctl_t *hctl, int device, CAEDeviceInfo& info, bool& badHDMI);
87 static void sndLibErrorHandler(const char *file, int line, const char *function, int err, const char *fmt, ...);