forked from ozsun88/SynchronousAudioRouter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsarclient.h
More file actions
139 lines (120 loc) · 4.15 KB
/
sarclient.h
File metadata and controls
139 lines (120 loc) · 4.15 KB
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
// SynchronousAudioRouter
// Copyright (C) 2015 Mackenzie Straight
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SynchronousAudioRouter. If not, see <http://www.gnu.org/licenses/>.
#ifndef _SAR_ASIO_SARCLIENT_H
#define _SAR_ASIO_SARCLIENT_H
#include "config.h"
#include "sar.h"
namespace Sar {
struct BufferConfig
{
int periodFrameSize;
int sampleRate;
int sampleSize;
std::array<std::vector<std::vector<void *>>, 2> asioBuffers;
};
struct SarClient: public std::enable_shared_from_this<SarClient>
{
SarClient(
const DriverConfig& driverConfig,
const BufferConfig& bufferConfig);
void tick(long bufferIndex);
bool start();
void stop();
void updateSampleRateOnTick()
{
_updateSampleRateOnTick = true;
}
private:
struct NotificationHandle
{
NotificationHandle(): generation(0), handle(nullptr) {}
~NotificationHandle()
{
if (handle) {
CloseHandle(handle);
}
}
ULONG generation;
HANDLE handle;
};
struct HandleQueueCompletion: OVERLAPPED
{
SarHandleQueueResponse responses[32];
};
struct ATL_NO_VTABLE NotificationClient:
public CComObjectRootEx<CComMultiThreadModel>,
public IMMNotificationClient
{
BEGIN_COM_MAP(NotificationClient)
COM_INTERFACE_ENTRY(IMMNotificationClient)
END_COM_MAP()
DECLARE_NO_REGISTRY()
void setClient(std::weak_ptr<SarClient> client)
{
_client = client;
}
virtual HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(
_In_ LPCWSTR pwstrDeviceId,
_In_ DWORD dwNewState) override;
virtual HRESULT STDMETHODCALLTYPE OnDeviceAdded(
_In_ LPCWSTR pwstrDeviceId) override;
virtual HRESULT STDMETHODCALLTYPE OnDeviceRemoved(
_In_ LPCWSTR pwstrDeviceId) override;
virtual HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(
_In_ EDataFlow flow,
_In_ ERole role,
_In_ LPCWSTR pwstrDefaultDeviceId) override;
virtual HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(
_In_ LPCWSTR pwstrDeviceId,
_In_ const PROPERTYKEY key) override;
private:
std::weak_ptr<SarClient> _client;
};
bool openControlDevice();
bool openMmNotificationClient();
bool setBufferLayout();
bool createEndpoints();
bool enableRegistryFilter();
void updateNotificationHandles();
void processNotificationHandleUpdates(int updateCount);
void demux(
void *muxBufferFirst, size_t firstSize,
void *muxBufferSecond, size_t secondSize,
void **targetBuffers, int ntargets, int nsources,
size_t targetSize, int sampleSize);
void mux(
void *muxBufferFirst, size_t firstSize,
void *muxBufferSecond, size_t secondSize,
void **targetBuffers, int ntargets, int nsources,
size_t targetSize, int sampleSize);
DriverConfig _driverConfig;
BufferConfig _bufferConfig;
std::vector<NotificationHandle> _notificationHandles;
HANDLE _device;
HANDLE _completionPort;
void *_sharedBuffer;
DWORD _sharedBufferSize;
volatile SarEndpointRegisters *_registers;
HandleQueueCompletion _handleQueueCompletion;
bool _handleQueueStarted;
CComPtr<IMMDeviceEnumerator> _mmEnumerator;
CComObject<NotificationClient> *_mmNotificationClient = nullptr;
bool _mmNotificationClientRegistered = false;
std::atomic<bool> _updateSampleRateOnTick = false;
std::mutex _registersLock;
};
} // namespace Sar
#endif // _SAR_ASIO_SARCLIENT_H