xbmc
ActiveAESink.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/AESinkFactory.h"
12 #include "cores/AudioEngine/Engines/ActiveAE/ActiveAEBuffer.h"
13 #include "cores/AudioEngine/Interfaces/AE.h"
14 #include "cores/AudioEngine/Interfaces/AESink.h"
15 #include "threads/Event.h"
16 #include "threads/SystemClock.h"
17 #include "threads/Thread.h"
18 #include "utils/ActorProtocol.h"
19 
20 #include <memory>
21 #include <utility>
22 
23 class CAEBitstreamPacker;
24 
25 namespace ActiveAE
26 {
27 using namespace Actor;
28 
29 class CEngineStats;
30 
31 struct SinkConfig
32 {
33  AEAudioFormat format;
34  CEngineStats *stats;
35  const std::string *device;
36 };
37 
38 struct SinkReply
39 {
40  AEAudioFormat format;
41  float cacheTotal;
42  float latency;
43  bool hasVolume;
44 };
45 
47 {
48 public:
49  CSinkControlProtocol(std::string name, CEvent* inEvent, CEvent* outEvent)
50  : Protocol(std::move(name), inEvent, outEvent)
51  {
52  }
53  enum OutSignal
54  {
55  CONFIGURE,
56  UNCONFIGURE,
57  STREAMING,
58  APPFOCUSED,
59  VOLUME,
60  FLUSH,
61  TIMEOUT,
62  SETSILENCETIMEOUT,
63  SETNOISETYPE,
64  };
65  enum InSignal
66  {
67  ACC,
68  ERR,
69  STATS,
70  };
71 };
72 
74 {
75 public:
76  CSinkDataProtocol(std::string name, CEvent* inEvent, CEvent* outEvent)
77  : Protocol(std::move(name), inEvent, outEvent)
78  {
79  }
80  enum OutSignal
81  {
82  SAMPLE = 0,
83  DRAIN,
84  };
85  enum InSignal
86  {
87  RETURNSAMPLE,
88  ACC,
89  };
90 };
91 
92 class CActiveAESink : private CThread
93 {
94 public:
95  explicit CActiveAESink(CEvent *inMsgEvent);
96  ~CActiveAESink();
97 
98  void EnumerateSinkList(bool force, std::string driver);
99  void EnumerateOutputDevices(AEDeviceList &devices, bool passthrough);
100  std::string ValidateOuputDevice(const std::string& device, bool passthrough) const;
101  void Start();
102  void Dispose();
103  AEDeviceType GetDeviceType(const std::string &device);
104  bool HasPassthroughDevice();
105  bool SupportsFormat(const std::string &device, AEAudioFormat &format);
106  bool DeviceExist(std::string driver, const std::string& device);
107  bool NeedIecPack() const { return m_needIecPack; }
108  CSinkControlProtocol m_controlPort;
109  CSinkDataProtocol m_dataPort;
110 
111 protected:
112  void Process() override;
113  void StateMachine(int signal, Protocol *port, Message *msg);
114  void PrintSinks(std::string& driver);
115  void GetDeviceFriendlyName(const std::string& device);
116  void OpenSink();
117  void ReturnBuffers();
118  void SetSilenceTimer();
119  bool NeedIECPacking();
120 
121  unsigned int OutputSamples(CSampleBuffer* samples);
122  void SwapInit(CSampleBuffer* samples);
123 
124  void GenerateNoise();
125 
126  CEvent m_outMsgEvent;
127  CEvent *m_inMsgEvent;
128  int m_state;
129  bool m_bStateMachineSelfTrigger;
130  std::chrono::milliseconds m_extTimeout;
131  std::chrono::minutes m_silenceTimeOut{std::chrono::minutes::zero()};
132  bool m_extError;
133  std::chrono::milliseconds m_extSilenceTimeout;
134  bool m_extAppFocused;
135  bool m_extStreaming;
136  XbmcThreads::EndTime<> m_extSilenceTimer;
137 
138  CSampleBuffer m_sampleOfSilence;
139  enum
140  {
141  CHECK_SWAP,
142  NEED_CONVERT,
143  NEED_BYTESWAP,
144  SKIP_SWAP,
145  } m_swapState;
146 
147  std::vector<uint8_t> m_mergeBuffer;
148 
149  std::string m_deviceFriendlyName;
150  std::string m_device;
151  std::vector<AE::AESinkInfo> m_sinkInfoList;
152  std::unique_ptr<IAESink> m_sink;
153  AEAudioFormat m_sinkFormat, m_requestedFormat;
154  CEngineStats *m_stats;
155  float m_volume;
156  int m_sinkLatency;
157  std::unique_ptr<CAEBitstreamPacker> m_packer;
158  bool m_needIecPack{false};
159  bool m_streamNoise;
160 };
161 
162 }
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: Thread.h:44
Definition: ActorProtocol.h:73
Definition: ActiveAESink.h:92
Definition: SystemClock.h:31
Definition: ActiveAESink.h:31
Definition: ActiveAESink.h:38
Definition: AEBitstreamPacker.h:20
Definition: ActiveAESink.h:46
Definition: Application.h:69
Definition: ActorProtocol.h:21
Definition: ActorProtocol.h:45
Definition: ActiveAESink.h:73
The audio format structure that fully defines a stream&#39;s audio information.
Definition: AEAudioFormat.h:19
Definition: ActiveAE.h:182
Definition: ActiveAEBuffer.h:45