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