1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  
     | 
    
      /*
 *  Copyright (C) 2010-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */
#pragma once
#include "cores/AudioEngine/Engines/ActiveAE/ActiveAE.h"
#include "cores/AudioEngine/Engines/ActiveAE/ActiveAEBuffer.h"
#include "cores/AudioEngine/Interfaces/AEStream.h"
#include "cores/AudioEngine/Utils/AEAudioFormat.h"
#include "cores/AudioEngine/Utils/AELimiter.h"
#include "threads/Event.h"
#include <atomic>
#include <deque>
namespace ActiveAE
{
class CActiveAE;
class CSyncError
{
public:
  CSyncError()
  {
    Flush();
  }
  void Add(double error)
  {
    m_buffer += error;
    m_count++;
  }
  void Flush(std::chrono::milliseconds interval = std::chrono::milliseconds(100))
  {
    m_buffer = 0.0;
    m_lastError = 0.0;
    m_count  = 0;
    m_timer.Set(interval);
  }
  void SetErrorInterval(std::chrono::milliseconds interval = std::chrono::milliseconds(100))
  {
    m_buffer = 0.0;
    m_count = 0;
    m_timer.Set(interval);
  }
  bool Get(double& error, std::chrono::milliseconds interval = std::chrono::milliseconds(100))
  {
    if(m_timer.IsTimePast())
    {
      error = Get();
      Flush(interval);
      m_lastError = error;
      return true;
    }
    else
    {
      error = m_lastError;
      return false;
    }
  }
  double GetLastError(unsigned int &time)
  {
    time = m_timer.GetStartTime().time_since_epoch().count();
    return m_lastError;
  }
  void Correction(double correction)
  {
    m_lastError += correction;
  }
protected:
  double Get() const
  {
    if(m_count)
      return m_buffer / m_count;
    else
      return 0.0;
  }
  double m_buffer;
  double m_lastError;
  int m_count;
  XbmcThreads::EndTime<> m_timer;
};
class CActiveAEStreamBuffers
{
public:
  CActiveAEStreamBuffers(const AEAudioFormat& inputFormat, const AEAudioFormat& outputFormat, AEQuality quality);
  virtual ~CActiveAEStreamBuffers();
  bool Create(unsigned int totaltime, bool remap, bool upmix, bool normalize = true);
  void SetExtraData(int profile, enum AVMatrixEncoding matrix_encoding, enum AVAudioServiceType audio_service_type);
  bool ProcessBuffers();
  void ConfigureResampler(bool normalizelevels, bool stereoupmix, AEQuality quality);
  bool HasInputLevel(int level);
  float GetDelay();
  void Flush();
  void SetDrain(bool drain);
  bool IsDrained();
  void SetRR(double rr, double atempoThreshold);
  double GetRR();
  void FillBuffer();
  bool DoesNormalize();
  void ForceResampler(bool force);
  bool HasWork();
  std::unique_ptr<CActiveAEBufferPool> GetResampleBuffers();
  std::unique_ptr<CActiveAEBufferPool> GetAtempoBuffers();
  AEAudioFormat m_inputFormat;
  std::deque<CSampleBuffer*> m_outputSamples;
  std::deque<CSampleBuffer*> m_inputSamples;
protected:
  std::unique_ptr<CActiveAEBufferPoolResample> m_resampleBuffers;
  std::unique_ptr<CActiveAEBufferPoolAtempo> m_atempoBuffers;
private:
  CActiveAEStreamBuffers(const CActiveAEStreamBuffers&) = delete;
  CActiveAEStreamBuffers& operator=(const CActiveAEStreamBuffers&) = delete;
};
class CActiveAEStream : public IAEStream
{
protected:
  friend class CActiveAE;
  friend class CEngineStats;
  CActiveAEStream(AEAudioFormat *format, unsigned int streamid, CActiveAE *ae);
  ~CActiveAEStream() override = default;
  void FadingFinished();
  void IncFreeBuffers();
  void DecFreeBuffers();
  void ResetFreeBuffers();
  void InitRemapper();
  void RemapBuffer();
  double CalcResampleRatio(double error);
  std::chrono::milliseconds GetErrorInterval();
public:
  unsigned int GetSpace() override;
  unsigned int AddData(const uint8_t* const *data, unsigned int offset, unsigned int frames, ExtData *extData) override;
  double GetDelay() override;
  CAESyncInfo GetSyncInfo() override;
  bool IsBuffering() override;
  double GetCacheTime() override;
  double GetCacheTotal() override;
  double GetMaxDelay() override;
  void Pause() override;
  void Resume() override;
  void Drain(bool wait) override;
  bool IsDraining() override;
  bool IsDrained() override;
  void Flush() override;
  float GetVolume() override;
  float GetReplayGain() override;
  float GetAmplification() override;
  void SetVolume(float volume) override;
  void SetReplayGain(float factor) override;
  void SetAmplification(float amplify) override;
  void SetFFmpegInfo(int profile, enum AVMatrixEncoding matrix_encoding, enum AVAudioServiceType audio_service_type) override;
  unsigned int GetFrameSize() const override;
  unsigned int GetChannelCount() const override;
  unsigned int GetSampleRate() const override ;
  enum AEDataFormat GetDataFormat() const override;
  double GetResampleRatio() override;
  void SetResampleRatio(double ratio) override;
  void SetResampleMode(int mode) override;
  void RegisterAudioCallback(IAudioCallback* pCallback) override;
  void UnRegisterAudioCallback() override;
  void FadeVolume(float from, float to, unsigned int time) override;
  bool IsFading() override;
  void RegisterSlave(IAEStream *stream) override;
protected:
  CActiveAE *m_activeAE;
  unsigned int m_id;
  AEAudioFormat m_format;
  float m_streamVolume;
  float m_streamRgain;
  float m_streamAmplify;
  double m_streamResampleRatio;
  int m_streamResampleMode;
  unsigned int m_streamSpace;
  bool m_streamDraining;
  bool m_streamDrained;
  bool m_streamFading;
  int m_streamFreeBuffers;
  bool m_streamIsBuffering;
  bool m_streamIsFlushed;
  IAEStream *m_streamSlave;
  CCriticalSection m_streamLock;
  CCriticalSection m_statsLock;
  int m_leftoverBytes;
  CSampleBuffer *m_currentBuffer;
  std::unique_ptr<CSoundPacket> m_remapBuffer;
  std::unique_ptr<IAEResample> m_remapper;
  double m_lastPts;
  double m_lastPtsJump;
  std::chrono::milliseconds m_errorInterval{1000};
  // only accessed by engine
  std::unique_ptr<CActiveAEBufferPool> m_inputBuffers;
  std::unique_ptr<CActiveAEStreamBuffers> m_processingBuffers;
  std::deque<CSampleBuffer*> m_processingSamples;
  std::unique_ptr<CActiveAEDataProtocol> m_streamPort;
  CEvent m_inMsgEvent;
  bool m_drain;
  bool m_paused;
  bool m_started;
  CAELimiter m_limiter;
  float m_volume;
  float m_rgain;
  float m_amplify;
  float m_bufferedTime;
  int m_fadingSamples;
  float m_fadingBase;
  float m_fadingTarget;
  int m_fadingTime;
  int m_profile;
  int m_resampleMode;
  double m_resampleIntegral;
  double m_clockSpeed;
  enum AVMatrixEncoding m_matrixEncoding;
  enum AVAudioServiceType m_audioServiceType;
  bool m_forceResampler;
  IAEClockCallback *m_pClock;
  CSyncError m_syncError;
  double m_lastSyncError;
  CAESyncInfo::AESyncState m_syncState;
};
}
 
     |