/xbmc/cores/dvdplayer/DVDPlayerAudio.h

http://github.com/xbmc/xbmc · C Header · 216 lines · 149 code · 42 blank · 25 comment · 2 complexity · c48006c1454ab11d8fa7fd9be4f263f9 MD5 · raw file

  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. #pragma once
  21. #include "threads/Thread.h"
  22. #include "DVDAudio.h"
  23. #include "DVDClock.h"
  24. #include "DVDMessageQueue.h"
  25. #include "DVDDemuxers/DVDDemuxUtils.h"
  26. #include "DVDStreamInfo.h"
  27. #include "utils/BitstreamStats.h"
  28. #include "cores/AudioEngine/Utils/AEAudioFormat.h"
  29. #include <list>
  30. #include <queue>
  31. class CDVDPlayer;
  32. class CDVDAudioCodec;
  33. class IAudioCallback;
  34. class CDVDAudioCodec;
  35. #define DECODE_FLAG_DROP 1
  36. #define DECODE_FLAG_RESYNC 2
  37. #define DECODE_FLAG_ERROR 4
  38. #define DECODE_FLAG_ABORT 8
  39. #define DECODE_FLAG_TIMEOUT 16
  40. typedef struct stDVDAudioFrame
  41. {
  42. uint8_t* data;
  43. double pts;
  44. double duration;
  45. unsigned int size;
  46. int channel_count;
  47. int encoded_channel_count;
  48. CAEChannelInfo channel_layout;
  49. enum AEDataFormat data_format;
  50. int bits_per_sample;
  51. int sample_rate;
  52. int encoded_sample_rate;
  53. bool passthrough;
  54. } DVDAudioFrame;
  55. class CPTSInputQueue
  56. {
  57. private:
  58. typedef std::list<std::pair<int64_t, double> >::iterator IT;
  59. std::list<std::pair<int64_t, double> > m_list;
  60. CCriticalSection m_sync;
  61. public:
  62. void Add(int64_t bytes, double pts);
  63. double Get(int64_t bytes, bool consume);
  64. void Flush();
  65. };
  66. class CDVDPlayerAudio : public CThread
  67. {
  68. public:
  69. CDVDPlayerAudio(CDVDClock* pClock, CDVDMessageQueue& parent);
  70. virtual ~CDVDPlayerAudio();
  71. bool OpenStream(CDVDStreamInfo &hints);
  72. void OpenStream(CDVDStreamInfo &hints, CDVDAudioCodec* codec);
  73. void CloseStream(bool bWaitForBuffers);
  74. void RegisterAudioCallback(IAudioCallback* pCallback) { m_dvdAudio.RegisterAudioCallback(pCallback); }
  75. void UnRegisterAudioCallback() { m_dvdAudio.UnRegisterAudioCallback(); }
  76. void SetSpeed(int speed);
  77. void Flush();
  78. // waits until all available data has been rendered
  79. void WaitForBuffers();
  80. bool AcceptsData() const { return !m_messageQueue.IsFull(); }
  81. bool HasData() const { return m_messageQueue.GetDataSize() > 0; }
  82. int GetLevel() const { return m_messageQueue.GetLevel(); }
  83. bool IsInited() const { return m_messageQueue.IsInited(); }
  84. void SendMessage(CDVDMsg* pMsg, int priority = 0) { m_messageQueue.Put(pMsg, priority); }
  85. //! Switch codec if needed. Called when the sample rate gotten from the
  86. //! codec changes, in which case we may want to switch passthrough on/off.
  87. bool SwitchCodecIfNeeded();
  88. void SetVolume(float fVolume) { m_dvdAudio.SetVolume(fVolume); }
  89. void SetDynamicRangeCompression(long drc) { m_dvdAudio.SetDynamicRangeCompression(drc); }
  90. float GetCurrentAttenuation() { return m_dvdAudio.GetCurrentAttenuation(); }
  91. std::string GetPlayerInfo();
  92. int GetAudioBitrate();
  93. // holds stream information for current playing stream
  94. CDVDStreamInfo m_streaminfo;
  95. CPTSOutputQueue m_ptsOutput;
  96. CPTSInputQueue m_ptsInput;
  97. double GetCurrentPts() { return m_dvdAudio.GetPlayingPts(); }
  98. bool IsStalled() { return m_stalled; }
  99. bool IsPassthrough() const;
  100. protected:
  101. virtual void OnStartup();
  102. virtual void OnExit();
  103. virtual void Process();
  104. int DecodeFrame(DVDAudioFrame &audioframe, bool bDropPacket);
  105. void UpdatePlayerInfo();
  106. CDVDMessageQueue m_messageQueue;
  107. CDVDMessageQueue& m_messageParent;
  108. double m_audioClock;
  109. // data for audio decoding
  110. struct PacketStatus
  111. {
  112. PacketStatus()
  113. {
  114. msg = NULL;
  115. Release();
  116. }
  117. ~PacketStatus()
  118. {
  119. Release();
  120. }
  121. CDVDMsgDemuxerPacket* msg;
  122. uint8_t* data;
  123. int size;
  124. double dts;
  125. void Attach(CDVDMsgDemuxerPacket* msg2)
  126. {
  127. if(msg) msg->Release();
  128. msg = msg2;
  129. msg->Acquire();
  130. DemuxPacket* p = msg->GetPacket();
  131. data = p->pData;
  132. size = p->iSize;
  133. dts = p->dts;
  134. }
  135. void Release()
  136. {
  137. if(msg) msg->Release();
  138. msg = NULL;
  139. data = NULL;
  140. size = 0;
  141. dts = DVD_NOPTS_VALUE;
  142. }
  143. } m_decode;
  144. CDVDAudio m_dvdAudio; // audio output device
  145. CDVDClock* m_pClock; // dvd master clock
  146. CDVDAudioCodec* m_pAudioCodec; // audio codec
  147. BitstreamStats m_audioStats;
  148. int m_speed;
  149. double m_droptime;
  150. bool m_stalled;
  151. bool m_started;
  152. double m_duration; // last packets duration
  153. bool m_silence;
  154. bool OutputPacket(DVDAudioFrame &audioframe);
  155. //SYNC_DISCON, SYNC_SKIPDUP, SYNC_RESAMPLE
  156. int m_synctype;
  157. int m_setsynctype;
  158. int m_prevsynctype; //so we can print to the log
  159. double m_error; //last average error
  160. int64_t m_errortime; //timestamp of last time we measured
  161. int64_t m_freq;
  162. void SetSyncType(bool passthrough);
  163. void HandleSyncError(double duration);
  164. double m_errorbuff; //place to store average errors
  165. int m_errorcount;//number of errors stored
  166. bool m_syncclock;
  167. double m_integral; //integral correction for resampler
  168. int m_skipdupcount; //counter for skip/duplicate synctype
  169. bool m_prevskipped;
  170. double m_maxspeedadjust;
  171. double m_resampleratio; //resample ratio when using SYNC_RESAMPLE, used for the codec info
  172. CCriticalSection m_info_section;
  173. std::string m_info;
  174. };