kodi
CoreAudioDevice.h
1 /*
2  * Copyright (C) 2011-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/Sinks/osx/CoreAudioStream.h"
12 #include "threads/SystemClock.h"
13 
14 #include <list>
15 #include <string>
16 #include <vector>
17 
18 #include <CoreAudio/CoreAudio.h>
19 
20 typedef std::vector<UInt32> CoreAudioDataSourceList;
21 typedef std::list<AudioDeviceID> CoreAudioDeviceList;
22 
24 
26 {
27 public:
28  CCoreAudioDevice() = default;
29  explicit CCoreAudioDevice(AudioDeviceID deviceId);
30  virtual ~CCoreAudioDevice();
31 
32  bool Open(AudioDeviceID deviceId);
33  void Close();
34 
35  void Start();
36  void Stop();
37  void RemoveObjectListenerProc(AudioObjectPropertyListenerProc callback, void *pClientData);
38  bool SetObjectListenerProc(AudioObjectPropertyListenerProc callback, void *pClientData);
39 
40  AudioDeviceID GetId() const {return m_DeviceId;}
41  std::string GetName() const;
42  bool IsDigital() const;
43  UInt32 GetTransportType() const;
44  UInt32 GetTotalOutputChannels() const;
45  UInt32 GetNumChannelsOfStream(UInt32 streamIdx) const;
46  bool GetStreams(AudioStreamIdList *pList);
47  bool IsRunning();
48  bool SetHogStatus(bool hog);
49  pid_t GetHogStatus();
50  bool SetMixingSupport(UInt32 mix);
51  bool GetMixingSupport();
52  bool SetCurrentVolume(Float32 vol);
53  bool GetPreferredChannelLayout(CCoreAudioChannelLayout &layout) const;
54  bool GetPreferredChannelLayoutForStereo(CCoreAudioChannelLayout &layout) const;
55  bool GetDataSources(CoreAudioDataSourceList *pList) const;
56  bool GetDataSource(UInt32 &dataSourceId) const;
57  bool SetDataSource(UInt32 &dataSourceId);
58  std::string GetDataSourceName(UInt32 dataSourceId) const;
59  std::string GetCurrentDataSourceName() const;
60  Float64 GetNominalSampleRate();
61  bool SetNominalSampleRate(Float64 sampleRate);
62  UInt32 GetNumLatencyFrames();
63  UInt32 GetBufferSize();
64  bool SetBufferSize(UInt32 size);
65 
66  static void RegisterDeviceChangedCB(bool bRegister, AudioObjectPropertyListenerProc callback, void *ref);
67  static void RegisterDefaultOutputDeviceChangedCB(bool bRegister, AudioObjectPropertyListenerProc callback, void *ref);
68  // suppresses the default output device changed callback for given time in ms
69  static void SuppressDefaultOutputDeviceCB(unsigned int suppressTimeMs)
70  {
71  m_callbackSuppressTimer.Set(std::chrono::milliseconds(suppressTimeMs));
72  }
73 
74  bool AddIOProc(AudioDeviceIOProc ioProc, void* pCallbackData);
75  bool RemoveIOProc();
76 protected:
77  bool m_Started = false;
78  AudioDeviceID m_DeviceId = 0;
79  int m_MixerRestore = -1;
80  AudioDeviceIOProc m_IoProc = nullptr;
81  AudioObjectPropertyListenerProc m_ObjectListenerProc = nullptr;
82 
83  Float64 m_SampleRateRestore = 0.0;
84  pid_t m_HogPid = -1;
85  unsigned int m_frameSize = 0;
86  unsigned int m_OutputBufferIndex = 0;
87  unsigned int m_BufferSizeRestore = 0;
88 
89  static XbmcThreads::EndTime<> m_callbackSuppressTimer;
90  static AudioObjectPropertyListenerProc m_defaultOutputDeviceChangedCB;
91  static OSStatus defaultOutputDeviceChanged(AudioObjectID inObjectID,
92  UInt32 inNumberAddresses,
93  const AudioObjectPropertyAddress inAddresses[],
94  void* inClientData);
95 
96 };
Definition: SystemClock.h:31
Definition: CoreAudioDevice.h:25
Definition: CoreAudioChannelLayout.h:58