kodi
AESinkAUDIOTRACK.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/Interfaces/AESink.h"
12 #include "cores/AudioEngine/Utils/AEDeviceInfo.h"
13 #include "cores/AudioEngine/Utils/AEUtil.h"
14 #include "threads/CriticalSection.h"
15 #include "threads/SystemClock.h"
16 #include "threads/Thread.h"
17 
18 #include <deque>
19 #include <set>
20 
21 #include <androidjni/AudioTrack.h>
22 
23 class CAESinkAUDIOTRACK : public IAESink
24 {
25 public:
26  const char* GetName() override { return "AUDIOTRACK"; }
27 
29  ~CAESinkAUDIOTRACK() override;
30 
31  bool Initialize(AEAudioFormat& format, std::string& device) override;
32  void Deinitialize() override;
33  bool IsInitialized();
34 
35  void GetDelay(AEDelayStatus& status) override;
36  double GetLatency() override;
37  double GetCacheTotal() override;
38  unsigned int AddPackets(uint8_t** data, unsigned int frames, unsigned int offset) override;
39  void AddPause(unsigned int millis) override;
40  void Drain() override;
41  static void EnumerateDevicesEx(AEDeviceInfoList &list, bool force = false);
42  static void Register();
43  static std::unique_ptr<IAESink> Create(std::string& device, AEAudioFormat& desiredFormat);
44 
45 protected:
46  static jni::CJNIAudioTrack *CreateAudioTrack(int stream, int sampleRate, int channelMask, int encoding, int bufferSize);
47  static bool IsSupported(int sampleRateInHz, int channelConfig, int audioFormat);
48  static bool VerifySinkConfiguration(int sampleRate,
49  int channelMask,
50  int encoding,
51  bool isRaw = false);
52  static void UpdateAvailablePCMCapabilities();
53  static void UpdateAvailablePassthroughCapabilities(bool isRaw = false);
54 
55  int AudioTrackWrite(char* audioData, int offsetInBytes, int sizeInBytes);
56  int AudioTrackWrite(char* audioData, int sizeInBytes, int64_t timestamp);
57 
58 private:
59  jni::CJNIAudioTrack *m_at_jni;
60  int m_jniAudioFormat;
61 
62  double m_duration_written;
63  unsigned int m_min_buffer_size;
64  uint64_t m_headPos;
65  uint64_t m_headPosOld;
66  uint32_t m_stuckCounter;
67  uint64_t m_timestampPos = 0;
68  // Moving Average computes the weighted average delay over
69  // a fixed size of delay values - current size: 20 values
70  double GetMovingAverageDelay(double newestdelay);
71 
72  // We maintain our linear weighted average delay counter in here
73  // The n-th value (timely oldest value) is weighted with 1/n
74  // the newest value gets a weight of 1
75  std::deque<double> m_linearmovingaverage;
76 
77  static CAEDeviceInfo m_info;
78  static CAEDeviceInfo m_info_raw;
79  static CAEDeviceInfo m_info_iec;
80  static bool m_hasIEC;
81  static std::set<unsigned int> m_sink_sampleRates;
82  static bool m_sinkSupportsFloat;
83  static bool m_sinkSupportsMultiChannelFloat;
84  static bool m_passthrough_use_eac3;
85 
86  AEAudioFormat m_format;
87  int16_t *m_alignedS16;
88  unsigned int m_sink_frameSize;
89  unsigned int m_sink_sampleRate;
90  bool m_passthrough;
91  double m_audiotrackbuffer_sec;
92  double m_audiotrackbuffer_sec_orig;
93  int m_encoding;
94  double m_pause_ms = 0.0;
95  double m_delay = 0.0;
96  double m_hw_delay = 0.0;
97  CJNIAudioTimestamp m_timestamp;
98  XbmcThreads::EndTime<> m_stampTimer;
99  bool m_superviseAudioDelay = false;
100 
101  std::vector<float> m_floatbuf;
102  std::vector<int16_t> m_shortbuf;
103  std::vector<char> m_charbuf;
104 };
This classt provides the details of what the audio output hardware is capable of. ...
Definition: AEDeviceInfo.h:31
Definition: SystemClock.h:31
Definition: AESink.h:18
The audio format structure that fully defines a stream&#39;s audio information.
Definition: AEAudioFormat.h:19
void GetDelay(AEDelayStatus &status) override
Return a timestamped status structure with delay and sink info.
Definition: AESinkAUDIOTRACK.cpp:642
Definition: AEUtil.h:27
unsigned int AddPackets(uint8_t **data, unsigned int frames, unsigned int offset) override
Adds packets to be sent out, this routine MUST block or sleep.
Definition: AESinkAUDIOTRACK.cpp:811
void AddPause(unsigned int millis) override
instruct the sink to add a pause
Definition: AESinkAUDIOTRACK.cpp:969
Definition: AESinkAUDIOTRACK.h:23