Merge pull request #2703 from ace20022/cores_revised
[vuplus_xbmc] / xbmc / cores / AudioEngine / Sinks / AESinkALSA.h
1 #pragma once
2 /*
3  *      Copyright (C) 2010-2013 Team XBMC
4  *      http://xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
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/>.
19  *
20  */
21
22 #include "system.h"
23 #ifdef HAS_ALSA
24
25 #include "Interfaces/AESink.h"
26 #include "Utils/AEDeviceInfo.h"
27 #include <stdint.h>
28
29 #define ALSA_PCM_NEW_HW_PARAMS_API
30 #include <alsa/asoundlib.h>
31
32 #include "threads/CriticalSection.h"
33
34 class CAESinkALSA : public IAESink
35 {
36 public:
37   virtual const char *GetName() { return "ALSA"; }
38
39   CAESinkALSA();
40   virtual ~CAESinkALSA();
41
42   virtual bool Initialize  (AEAudioFormat &format, std::string &device);
43   virtual void Deinitialize();
44   virtual bool IsCompatible(const AEAudioFormat &format, const std::string &device);
45
46   virtual void         Stop            ();
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();
54
55   static void EnumerateDevicesEx(AEDeviceInfoList &list, bool force = false);
56 private:
57   CAEChannelInfo GetChannelLayout(AEAudioFormat format);
58   void           GetAESParams(const AEAudioFormat format, std::string& params);
59   void           HandleError(const char* name, int err);
60
61   std::string       m_initDevice;
62   AEAudioFormat     m_initFormat;
63   AEAudioFormat     m_format;
64   unsigned int      m_bufferSize;
65   double            m_formatSampleRateMul;
66   bool              m_passthrough;
67   std::string       m_device;
68   snd_pcm_t        *m_pcm;
69   int               m_timeout;
70
71   static snd_pcm_format_t AEFormatToALSAFormat(const enum AEDataFormat format);
72
73   bool InitializeHW(AEAudioFormat &format);
74   bool InitializeSW(AEAudioFormat &format);
75
76   static void AppendParams(std::string &device, const std::string &params);
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 &params, snd_pcm_t **pcmp, snd_config_t *lconf);
79   static bool OpenPCMDevice(const std::string &name, const std::string &params, int channels, snd_pcm_t **pcmp, snd_config_t *lconf);
80
81   static AEDeviceType AEDeviceTypeFromName(const std::string &name);
82   static std::string GetParamFromName(const std::string &name, const std::string &param);
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);
86
87   static void sndLibErrorHandler(const char *file, int line, const char *function, int err, const char *fmt, ...);
88 };
89 #endif
90