2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
22 #include "threads/Thread.h"
26 #include "DVDMessageQueue.h"
27 #include "DVDDemuxers/DVDDemuxUtils.h"
28 #include "DVDStreamInfo.h"
29 #include "utils/BitstreamStats.h"
31 #include "cores/AudioEngine/Utils/AEAudioFormat.h"
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
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;
54 void Add(int64_t bytes, double pts);
55 double Get(int64_t bytes, bool consume);
59 class CDVDErrorAverage
66 void Add(double error)
82 return m_buffer / m_count;
87 bool Get(double& error)
89 if(m_timer.IsTimePast())
99 double m_buffer; //place to store average errors
100 int m_count; //number of errors stored
101 XbmcThreads::EndTime m_timer;
104 class CDVDPlayerAudio : public CThread
107 CDVDPlayerAudio(CDVDClock* pClock, CDVDMessageQueue& parent);
108 virtual ~CDVDPlayerAudio();
110 bool OpenStream(CDVDStreamInfo &hints);
111 void OpenStream(CDVDStreamInfo &hints, CDVDAudioCodec* codec);
112 void CloseStream(bool bWaitForBuffers);
114 void RegisterAudioCallback(IAudioCallback* pCallback) { m_dvdAudio.RegisterAudioCallback(pCallback); }
115 void UnRegisterAudioCallback() { m_dvdAudio.UnRegisterAudioCallback(); }
117 void SetSpeed(int speed);
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); }
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();
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(); }
136 std::string GetPlayerInfo();
137 int GetAudioBitrate();
139 // holds stream information for current playing stream
140 CDVDStreamInfo m_streaminfo;
142 CPTSOutputQueue m_ptsOutput;
143 CPTSInputQueue m_ptsInput;
145 double GetCurrentPts() { return m_dvdAudio.GetPlayingPts(); }
147 bool IsStalled() { return m_stalled; }
148 bool IsPassthrough() const;
151 virtual void OnStartup();
152 virtual void OnExit();
153 virtual void Process();
155 int DecodeFrame(DVDAudioFrame &audioframe);
157 void UpdatePlayerInfo();
159 CDVDMessageQueue m_messageQueue;
160 CDVDMessageQueue& m_messageParent;
164 // data for audio decoding
178 CDVDMsgDemuxerPacket* msg;
183 void Attach(CDVDMsgDemuxerPacket* msg2)
185 if(msg) msg->Release();
188 DemuxPacket* p = msg->GetPacket();
196 if(msg) msg->Release();
200 dts = DVD_NOPTS_VALUE;
204 CDVDAudio m_dvdAudio; // audio output device
205 CDVDClock* m_pClock; // dvd master clock
206 CDVDAudioCodec* m_pAudioCodec; // audio codec
207 BitstreamStats m_audioStats;
214 bool OutputPacket(DVDAudioFrame &audioframe);
216 //SYNC_DISCON, SYNC_SKIPDUP, SYNC_RESAMPLE
219 int m_prevsynctype; //so we can print to the log
221 double m_error; //last average error
223 void SetSyncType(bool passthrough);
224 void HandleSyncError(double duration);
225 CDVDErrorAverage m_errors;
228 double m_integral; //integral correction for resampler
230 double m_maxspeedadjust;
231 double m_resampleratio; //resample ratio when using SYNC_RESAMPLE, used for the codec info
234 CCriticalSection m_info_section;