XT-Audio
XtService.hpp
Go to the documentation of this file.
1 #ifndef XT_API_SERVICE_HPP
2 #define XT_API_SERVICE_HPP
3 
6 #include <xt/cpp/Core.hpp>
7 #include <xt/cpp/Error.hpp>
8 #include <xt/cpp/Forward.hpp>
9 
10 #include <xt/api/Enums.hpp>
11 #include <xt/api/Structs.hpp>
12 #include <xt/api/XtDevice.hpp>
13 #include <xt/api/XtDeviceList.hpp>
14 
15 #include <memory>
16 #include <optional>
19 namespace Xt {
20 
21 using namespace Detail;
22 
23 class Service final
24 {
26  friend class Platform;
28  XtService const* const _s;
29  Service(XtService const* s): _s(s) { }
30 public:
31  ServiceCaps GetCapabilities() const;
32  std::unique_ptr<Device> OpenDevice(std::string const& id) const;
33  std::optional<std::string> GetDefaultDeviceId(bool output) const;
34  std::unique_ptr<DeviceList> OpenDeviceList(EnumFlags flags) const;
35  std::unique_ptr<Stream> AggregateStream(AggregateStreamParams const& params, void* user);
36 };
37 
38 inline std::unique_ptr<Device>
39 Service::OpenDevice(std::string const& id) const
40 {
41  XtDevice* device;
42  Detail::HandleError(XtServiceOpenDevice(_s, id.c_str(), &device));
43  return std::unique_ptr<Device>(new Device(device));
44 }
45 
46 inline std::unique_ptr<DeviceList>
48 {
49  XtDeviceList* list;
50  auto coreFlags = static_cast<XtEnumFlags>(flags);
51  Detail::HandleError(XtServiceOpenDeviceList(_s, coreFlags, &list));
52  return std::unique_ptr<DeviceList>(new DeviceList(list));
53 }
54 
55 inline ServiceCaps
57 {
58  auto coreCapabilities = Detail::HandleAssert(XtServiceGetCapabilities(_s));
59  return static_cast<ServiceCaps>(coreCapabilities);
60 }
61 
62 inline std::optional<std::string>
63 Service::GetDefaultDeviceId(bool output) const
64 {
65  XtBool valid;
66  int32_t size = 0;
67  Detail::HandleError(XtServiceGetDefaultDeviceId(_s, output, &valid, nullptr, &size));
68  if(!valid) return std::optional<std::string>(std::nullopt);
69  std::vector<char> buffer(static_cast<size_t>(size));
70  Detail::HandleError(XtServiceGetDefaultDeviceId(_s, output, &valid, buffer.data(), &size));
71  if(!valid) return std::optional<std::string>(std::nullopt);
72  return std::optional<std::string>(std::string(buffer.data()));
73 }
74 
75 inline std::unique_ptr<Stream>
77 {
78  XtStream* stream = nullptr;
79  std::vector<XtAggregateDeviceParams> ds(params.count);
80  for(int32_t i = 0; i < params.count; i++)
81  {
82  ds[i].device = params.devices[i].device->_d;
83  ds[i].bufferSize = params.devices[i].bufferSize;
84  ds[i].channels = *reinterpret_cast<XtChannels const*>(&params.devices[i].channels);
85  }
86  XtAggregateStreamParams coreParams = { 0 };
87  coreParams.devices = ds.data();
88  coreParams.count = params.count;
89  coreParams.master = params.master->_d;
90  coreParams.stream.onBuffer = Detail::ForwardOnBuffer;
91  coreParams.stream.interleaved = params.stream.interleaved;
92  coreParams.mix = *reinterpret_cast<XtMix const*>(&params.mix);
93  coreParams.stream.onXRun = params.stream.onXRun == nullptr? nullptr: Detail::ForwardOnXRun;
94  coreParams.stream.onRunning = params.stream.onRunning == nullptr? nullptr: Detail::ForwardOnRunning;
95  std::unique_ptr<Stream> result(new Stream(params.stream, user));
96  Detail::HandleError(XtServiceAggregateStream(_s, &coreParams, result.get(), &stream));
97  result->_s = stream;
98  return result;
99 }
100 
101 } // namespace Xt
102 #endif // XT_API_SERVICE_HPP
Mix mix
Definition: Structs.hpp:130
Definition: Structs.hpp:125
EnumFlags
Definition: Enums.hpp:13
std::unique_ptr< DeviceList > OpenDeviceList(EnumFlags flags) const
Definition: XtService.hpp:47
ServiceCaps GetCapabilities() const
Definition: XtService.hpp:56
class Device * device
Definition: Structs.hpp:117
std::unique_ptr< Device > OpenDevice(std::string const &id) const
Definition: XtService.hpp:39
Channels channels
Definition: Structs.hpp:118
StreamParams stream
Definition: Structs.hpp:127
std::unique_ptr< Stream > AggregateStream(AggregateStreamParams const &params, void *user)
Definition: XtService.hpp:76
AggregateDeviceParams * devices
Definition: Structs.hpp:128
OnXRun onXRun
Definition: Structs.hpp:98
double bufferSize
Definition: Structs.hpp:119
std::optional< std::string > GetDefaultDeviceId(bool output) const
Definition: XtService.hpp:63
Definition: XtDeviceList.hpp:17
Definition: XtService.hpp:23
int32_t count
Definition: Structs.hpp:129
Definition: XtPlatform.hpp:19
bool interleaved
Definition: Structs.hpp:96
Device const * master
Definition: Structs.hpp:131
ServiceCaps
Definition: Enums.hpp:14
Definition: Callbacks.hpp:10
OnRunning onRunning
Definition: Structs.hpp:99
Definition: XtStream.hpp:18
Definition: XtDevice.hpp:20