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/AEAudioFormat.h"
43 #define DECODE_FLAG_DROP 1
44 #define DECODE_FLAG_RESYNC 2
45 #define DECODE_FLAG_ERROR 4
46 #define DECODE_FLAG_ABORT 8
47 #define DECODE_FLAG_TIMEOUT 16
49 typedef struct stDVDAudioFrame
57 int encoded_channel_count;
58 CAEChannelInfo channel_layout;
59 enum AEDataFormat data_format;
62 int encoded_sample_rate;
69 typedef std::list<std::pair<int64_t, double> >::iterator IT;
70 std::list<std::pair<int64_t, double> > m_list;
71 CCriticalSection m_sync;
73 void Add(int64_t bytes, double pts);
74 double Get(int64_t bytes, bool consume);
78 class CDVDPlayerAudio : public CThread
81 CDVDPlayerAudio(CDVDClock* pClock, CDVDMessageQueue& parent);
82 virtual ~CDVDPlayerAudio();
84 bool OpenStream(CDVDStreamInfo &hints);
85 void OpenStream(CDVDStreamInfo &hints, CDVDAudioCodec* codec);
86 void CloseStream(bool bWaitForBuffers);
88 void RegisterAudioCallback(IAudioCallback* pCallback) { m_dvdAudio.RegisterAudioCallback(pCallback); }
89 void UnRegisterAudioCallback() { m_dvdAudio.UnRegisterAudioCallback(); }
91 void SetSpeed(int speed);
94 // waits until all available data has been rendered
95 void WaitForBuffers();
96 bool AcceptsData() const { return !m_messageQueue.IsFull(); }
97 bool HasData() const { return m_messageQueue.GetDataSize() > 0; }
98 int GetLevel() const { return m_messageQueue.GetLevel(); }
99 bool IsInited() const { return m_messageQueue.IsInited(); }
100 void SendMessage(CDVDMsg* pMsg, int priority = 0) { m_messageQueue.Put(pMsg, priority); }
102 //! Switch codec if needed. Called when the sample rate gotten from the
103 //! codec changes, in which case we may want to switch passthrough on/off.
104 bool SwitchCodecIfNeeded();
106 void SetVolume(float fVolume) { m_dvdAudio.SetVolume(fVolume); }
107 void SetDynamicRangeCompression(long drc) { m_dvdAudio.SetDynamicRangeCompression(drc); }
108 float GetCurrentAttenuation() { return m_dvdAudio.GetCurrentAttenuation(); }
110 std::string GetPlayerInfo();
111 int GetAudioBitrate();
113 // holds stream information for current playing stream
114 CDVDStreamInfo m_streaminfo;
116 CPTSOutputQueue m_ptsOutput;
117 CPTSInputQueue m_ptsInput;
119 double GetCurrentPts() { return m_dvdAudio.GetPlayingPts(); }
121 bool IsStalled() { return m_stalled; }
122 bool IsPassthrough() const;
125 virtual void OnStartup();
126 virtual void OnExit();
127 virtual void Process();
129 int DecodeFrame(DVDAudioFrame &audioframe, bool bDropPacket);
131 CDVDMessageQueue m_messageQueue;
132 CDVDMessageQueue& m_messageParent;
136 // data for audio decoding
150 CDVDMsgDemuxerPacket* msg;
155 void Attach(CDVDMsgDemuxerPacket* msg2)
157 if(msg) msg->Release();
160 DemuxPacket* p = msg->GetPacket();
168 if(msg) msg->Release();
172 dts = DVD_NOPTS_VALUE;
176 CDVDAudio m_dvdAudio; // audio output device
177 CDVDClock* m_pClock; // dvd master clock
178 CDVDAudioCodec* m_pAudioCodec; // audio codec
179 BitstreamStats m_audioStats;
185 double m_duration; // last packets duration
188 bool OutputPacket(DVDAudioFrame &audioframe);
190 //SYNC_DISCON, SYNC_SKIPDUP, SYNC_RESAMPLE
193 int m_prevsynctype; //so we can print to the log
195 double m_error; //last average error
197 int64_t m_errortime; //timestamp of last time we measured
200 void SetSyncType(bool passthrough);
201 void HandleSyncError(double duration);
202 double m_errorbuff; //place to store average errors
203 int m_errorcount;//number of errors stored
206 double m_integral; //integral correction for resampler
207 int m_skipdupcount; //counter for skip/duplicate synctype
209 double m_maxspeedadjust;
210 double m_resampleratio; //resample ratio when using SYNC_RESAMPLE, used for the codec info