FIX: [droid] set "remote as keyboard" default to true
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDPlayerAudio.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #pragma once
22 #include "threads/Thread.h"
23
24 #include "DVDAudio.h"
25 #include "DVDClock.h"
26 #include "DVDMessageQueue.h"
27 #include "DVDDemuxers/DVDDemuxUtils.h"
28 #include "DVDStreamInfo.h"
29 #include "utils/BitstreamStats.h"
30
31 #include "cores/AudioEngine/Utils/AEAudioFormat.h"
32
33 #include <list>
34 #include <queue>
35
36 class CDVDPlayer;
37 class CDVDAudioCodec;
38 class IAudioCallback;
39 class CDVDAudioCodec;
40
41 #define DECODE_FLAG_DROP    1
42 #define DECODE_FLAG_RESYNC  2
43 #define DECODE_FLAG_ERROR   4
44 #define DECODE_FLAG_ABORT   8
45 #define DECODE_FLAG_TIMEOUT 16
46
47 class CPTSInputQueue
48 {
49 private:
50   typedef std::list<std::pair<int64_t, double> >::iterator IT;
51   std::list<std::pair<int64_t, double> > m_list;
52   CCriticalSection m_sync;
53 public:
54   void   Add(int64_t bytes, double pts);
55   double Get(int64_t bytes, bool consume);
56   void   Flush();
57 };
58
59 class CDVDErrorAverage
60 {
61 public:
62   CDVDErrorAverage()
63   {
64     Flush();
65   }
66   void    Add(double error)
67   {
68     m_buffer += error;
69     m_count++;
70   }
71
72   void    Flush()
73   {
74     m_buffer = 0.0f;
75     m_count  = 0;
76     m_timer.Set(2000);
77   }
78
79   double  Get()
80   {
81     if(m_count)
82       return m_buffer / m_count;
83     else
84       return 0.0;
85   }
86
87   bool    Get(double& error)
88   {
89     if(m_timer.IsTimePast())
90     {
91       error = Get();
92       Flush();
93       return true;
94     }
95     else
96       return false;
97   }
98
99   double               m_buffer; //place to store average errors
100   int                  m_count;  //number of errors stored
101   XbmcThreads::EndTime m_timer;
102 };
103
104 class CDVDPlayerAudio : public CThread
105 {
106 public:
107   CDVDPlayerAudio(CDVDClock* pClock, CDVDMessageQueue& parent);
108   virtual ~CDVDPlayerAudio();
109
110   bool OpenStream(CDVDStreamInfo &hints);
111   void OpenStream(CDVDStreamInfo &hints, CDVDAudioCodec* codec);
112   void CloseStream(bool bWaitForBuffers);
113
114   void RegisterAudioCallback(IAudioCallback* pCallback) { m_dvdAudio.RegisterAudioCallback(pCallback); }
115   void UnRegisterAudioCallback()                        { m_dvdAudio.UnRegisterAudioCallback(); }
116
117   void SetSpeed(int speed);
118   void Flush();
119
120   // waits until all available data has been rendered
121   void WaitForBuffers();
122   bool AcceptsData() const                              { return !m_messageQueue.IsFull(); }
123   bool HasData() const                                  { return m_messageQueue.GetDataSize() > 0; }
124   int  GetLevel() const                                 { return m_messageQueue.GetLevel(); }
125   bool IsInited() const                                 { return m_messageQueue.IsInited(); }
126   void SendMessage(CDVDMsg* pMsg, int priority = 0)     { m_messageQueue.Put(pMsg, priority); }
127
128   //! Switch codec if needed. Called when the sample rate gotten from the
129   //! codec changes, in which case we may want to switch passthrough on/off.
130   bool SwitchCodecIfNeeded();
131
132   void SetVolume(float fVolume)                         { m_dvdAudio.SetVolume(fVolume); }
133   void SetDynamicRangeCompression(long drc)             { m_dvdAudio.SetDynamicRangeCompression(drc); }
134   float GetCurrentAttenuation()                         { return m_dvdAudio.GetCurrentAttenuation(); }
135
136   std::string GetPlayerInfo();
137   int GetAudioBitrate();
138
139   // holds stream information for current playing stream
140   CDVDStreamInfo m_streaminfo;
141
142   CPTSOutputQueue m_ptsOutput;
143   CPTSInputQueue  m_ptsInput;
144
145   double GetCurrentPts()                            { return m_dvdAudio.GetPlayingPts(); }
146
147   bool IsStalled()                                  { return m_stalled;  }
148   bool IsPassthrough() const;
149 protected:
150
151   virtual void OnStartup();
152   virtual void OnExit();
153   virtual void Process();
154
155   int DecodeFrame(DVDAudioFrame &audioframe);
156
157   void UpdatePlayerInfo();
158
159   CDVDMessageQueue m_messageQueue;
160   CDVDMessageQueue& m_messageParent;
161
162   double m_audioClock;
163
164   // data for audio decoding
165   struct PacketStatus
166   {
167     PacketStatus()
168     {
169         msg = NULL;
170         Release();
171     }
172
173    ~PacketStatus()
174     {
175         Release();
176     }
177
178     CDVDMsgDemuxerPacket*  msg;
179     uint8_t*               data;
180     int                    size;
181     double                 dts;
182
183     void Attach(CDVDMsgDemuxerPacket* msg2)
184     {
185       if(msg) msg->Release();
186       msg = msg2;
187       msg->Acquire();
188       DemuxPacket* p = msg->GetPacket();
189       data = p->pData;
190       size = p->iSize;
191       dts = p->dts;
192
193     }
194     void Release()
195     {
196       if(msg) msg->Release();
197       msg  = NULL;
198       data = NULL;
199       size = 0;
200       dts  = DVD_NOPTS_VALUE;
201     }
202   } m_decode;
203
204   CDVDAudio m_dvdAudio; // audio output device
205   CDVDClock* m_pClock; // dvd master clock
206   CDVDAudioCodec* m_pAudioCodec; // audio codec
207   BitstreamStats m_audioStats;
208
209   int     m_speed;
210   bool    m_stalled;
211   bool    m_started;
212   bool    m_silence;
213
214   bool OutputPacket(DVDAudioFrame &audioframe);
215
216   //SYNC_DISCON, SYNC_SKIPDUP, SYNC_RESAMPLE
217   int    m_synctype;
218   int    m_setsynctype;
219   int    m_prevsynctype; //so we can print to the log
220
221   double m_error;    //last average error
222
223   void   SetSyncType(bool passthrough);
224   void   HandleSyncError(double duration);
225   CDVDErrorAverage m_errors;
226   bool   m_syncclock;
227
228   double m_integral; //integral correction for resampler
229   bool   m_prevskipped;
230   double m_maxspeedadjust;
231   double m_resampleratio; //resample ratio when using SYNC_RESAMPLE, used for the codec info
232
233
234   CCriticalSection m_info_section;
235   std::string      m_info;
236 };
237