Merge pull request #3014 from bfg1981/master
[vuplus_xbmc] / xbmc / cores / AudioEngine / Engines / ActiveAE / ActiveAESink.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 "threads/Event.h"
23 #include "threads/Thread.h"
24 #include "utils/ActorProtocol.h"
25 #include "Interfaces/AE.h"
26 #include "Interfaces/AESink.h"
27 #include "AESinkFactory.h"
28 #include "ActiveAEResample.h"
29 #include "Utils/AEConvert.h"
30
31 namespace ActiveAE
32 {
33 using namespace Actor;
34
35 class CEngineStats;
36
37 struct SinkConfig
38 {
39   AEAudioFormat format;
40   CEngineStats *stats;
41 };
42
43 class CSinkControlProtocol : public Protocol
44 {
45 public:
46   CSinkControlProtocol(std::string name, CEvent* inEvent, CEvent *outEvent) : Protocol(name, inEvent, outEvent) {};
47   enum OutSignal
48   {
49     CONFIGURE,
50     UNCONFIGURE,
51     SILENCEMODE,
52     VOLUME,
53     TIMEOUT,
54   };
55   enum InSignal
56   {
57     ACC,
58     ERR,
59     STATS,
60   };
61 };
62
63 class CSinkDataProtocol : public Protocol
64 {
65 public:
66   CSinkDataProtocol(std::string name, CEvent* inEvent, CEvent *outEvent) : Protocol(name, inEvent, outEvent) {};
67   enum OutSignal
68   {
69     SAMPLE = 0,
70     DRAIN,
71   };
72   enum InSignal
73   {
74     RETURNSAMPLE,
75     ACC,
76   };
77 };
78
79 class CActiveAESink : private CThread
80 {
81 public:
82   CActiveAESink(CEvent *inMsgEvent);
83   void EnumerateSinkList();
84   void EnumerateOutputDevices(AEDeviceList &devices, bool passthrough);
85   std::string GetDefaultDevice(bool passthrough);
86   void Start();
87   void Dispose();
88   bool IsCompatible(const AEAudioFormat format, const std::string &device);
89   bool HasVolume();
90   CSinkControlProtocol m_controlPort;
91   CSinkDataProtocol m_dataPort;
92
93 protected:
94   void Process();
95   void StateMachine(int signal, Protocol *port, Message *msg);
96   void PrintSinks();
97   void GetDeviceFriendlyName(std::string &device);
98   void OpenSink();
99   void ReturnBuffers();
100
101   unsigned int OutputSamples(CSampleBuffer* samples);
102   void ConvertInit(CSampleBuffer* samples);
103   inline void EnsureConvertBuffer(CSampleBuffer* samples);
104   inline uint8_t* Convert(CSampleBuffer* samples);
105
106   void GenerateNoise();
107
108   CEvent m_outMsgEvent;
109   CEvent *m_inMsgEvent;
110   int m_state;
111   bool m_bStateMachineSelfTrigger;
112   int m_extTimeout;
113   bool m_extError;
114   bool m_extSilence;
115   int m_extCycleCounter;
116
117   CSampleBuffer m_sampleOfSilence;
118   CSampleBuffer m_sampleOfNoise;
119   uint8_t *m_convertBuffer;
120   int m_convertBufferSampleSize;
121   CAEConvert::AEConvertFrFn m_convertFn;
122   enum
123   {
124     CHECK_CONVERT,
125     NEED_CONVERT,
126     NEED_BYTESWAP,
127     SKIP_CONVERT,
128   } m_convertState;
129
130   std::string m_deviceFriendlyName;
131   AESinkInfoList m_sinkInfoList;
132   IAESink *m_sink;
133   AEAudioFormat m_sinkFormat, m_requestedFormat;
134   CEngineStats *m_stats;
135   float m_volume;
136 };
137
138 }