kodi
AESinkALSA.h
1 /*
2  * Copyright (C) 2010-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "cores/AudioEngine/Interfaces/AESink.h"
12 #include "cores/AudioEngine/Utils/AEDeviceInfo.h"
13 #include "threads/CriticalSection.h"
14 
15 #include <stdint.h>
16 
17 #include <alsa/asoundlib.h>
18 
19 #define AE_MIN_PERIODSIZE 256
20 
21 class CAESinkALSA : public IAESink
22 {
23 public:
24  const char *GetName() override { return "ALSA"; }
25 
26  CAESinkALSA();
27  ~CAESinkALSA() override;
28 
29  static void Register();
30  static std::unique_ptr<IAESink> Create(std::string& device, AEAudioFormat& desiredFormat);
31  static void EnumerateDevicesEx(AEDeviceInfoList &list, bool force = false);
32  static void Cleanup();
33 
34  bool Initialize(AEAudioFormat &format, std::string &device) override;
35  void Deinitialize() override;
36 
37  virtual void Stop ();
38  void GetDelay(AEDelayStatus& status) override;
39  double GetCacheTotal() override;
40  unsigned int AddPackets(uint8_t **data, unsigned int frames, unsigned int offset) override;
41  void Drain() override;
42 
43 private:
44  CAEChannelInfo GetChannelLayoutRaw(const AEAudioFormat& format);
45  CAEChannelInfo GetChannelLayoutLegacy(const AEAudioFormat& format, unsigned int minChannels, unsigned int maxChannels);
46  CAEChannelInfo GetChannelLayout(const AEAudioFormat& format, unsigned int channels);
47 
48  static AEChannel ALSAChannelToAEChannel(unsigned int alsaChannel);
49  static unsigned int AEChannelToALSAChannel(AEChannel aeChannel);
50  static CAEChannelInfo ALSAchmapToAEChannelMap(snd_pcm_chmap_t* alsaMap);
51  static snd_pcm_chmap_t* AEChannelMapToALSAchmap(const CAEChannelInfo& info);
52  static snd_pcm_chmap_t* CopyALSAchmap(snd_pcm_chmap_t* alsaMap);
53  static std::string ALSAchmapToString(snd_pcm_chmap_t* alsaMap);
54  static CAEChannelInfo GetAlternateLayoutForm(const CAEChannelInfo& info);
55  snd_pcm_chmap_t* SelectALSAChannelMap(const CAEChannelInfo& info);
56 
57  void GetAESParams(const AEAudioFormat& format, std::string& params);
58  void HandleError(const char* name, int err);
59 
60  std::string m_initDevice;
61  AEAudioFormat m_initFormat;
62  AEAudioFormat m_format;
63  unsigned int m_bufferSize = 0;
64  double m_formatSampleRateMul = 0.0;
65  bool m_passthrough = false;
66  std::string m_device;
67  snd_pcm_t *m_pcm;
68  int m_timeout = 0;
69  // support fragmentation, e.g. looping in the sink to get a certain amount of data onto the device
70  bool m_fragmented = false;
71  unsigned int m_originalPeriodSize = AE_MIN_PERIODSIZE;
72 
73  struct ALSAConfig
74  {
75  unsigned int sampleRate;
76  unsigned int periodSize;
77  unsigned int frameSize;
78  unsigned int channels;
79  AEDataFormat format;
80  };
81 
82  static snd_pcm_format_t AEFormatToALSAFormat(const enum AEDataFormat format);
83 
84  bool InitializeHW(const ALSAConfig &inconfig, ALSAConfig &outconfig);
85  bool InitializeSW(const ALSAConfig &inconfig);
86 
87  static void AppendParams(std::string &device, const std::string &params);
88  static bool TryDevice(const std::string &name, snd_pcm_t **pcmp, snd_config_t *lconf);
89  static bool TryDeviceWithParams(const std::string &name, const std::string &params, snd_pcm_t **pcmp, snd_config_t *lconf);
90  static bool OpenPCMDevice(const std::string &name, const std::string &params, int channels, snd_pcm_t **pcmp, snd_config_t *lconf);
91 
92  static AEDeviceType AEDeviceTypeFromName(const std::string &name);
93  static std::string GetParamFromName(const std::string &name, const std::string &param);
94  static void EnumerateDevice(AEDeviceInfoList &list, const std::string &device, const std::string &description, snd_config_t *config);
95  static bool SoundDeviceExists(const std::string& device);
96  static bool GetELD(snd_hctl_t *hctl, int device, CAEDeviceInfo& info, bool& badHDMI);
97 
98  static void sndLibErrorHandler(const char *file, int line, const char *function, int err, const char *fmt, ...);
99 };
100 
Definition: deflate.c:123
This classt provides the details of what the audio output hardware is capable of. ...
Definition: AEDeviceInfo.h:31
Definition: AESink.h:18
The audio format structure that fully defines a stream&#39;s audio information.
Definition: AEAudioFormat.h:19
unsigned int AddPackets(uint8_t **data, unsigned int frames, unsigned int offset) override
Adds packets to be sent out, this routine MUST block or sleep.
Definition: AESinkALSA.cpp:903
Definition: AEUtil.h:27
Definition: AEChannelInfo.h:19
Definition: AESinkALSA.h:21
void GetDelay(AEDelayStatus &status) override
Return a timestamped status structure with delay and sink info.
Definition: AESinkALSA.cpp:879