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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: Olivier Lapicque */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */
#pragma once
#include "openmpt/all/BuildSettings.hpp"
#include "SoundDevice.hpp"
#include "SoundDeviceBase.hpp"
#include "mpt/base/detect.hpp"
#include "openmpt/logging/Logger.hpp"
#if MPT_OS_LINUX || MPT_OS_MACOSX_OR_IOS || MPT_OS_FREEBSD
// we use c++11 in native support library
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <thread>
#endif // MPT_OS_LINUX || MPT_OS_MACOSX_OR_IOS || MPT_OS_FREEBSD
#if MPT_OS_WINDOWS
#include <mmreg.h>
#include <windows.h>
#endif // MPT_OS_WINDOWS
OPENMPT_NAMESPACE_BEGIN
namespace SoundDevice
{
#if MPT_OS_WINDOWS
bool FillWaveFormatExtensible(WAVEFORMATEXTENSIBLE &WaveFormat, const SoundDevice::Settings &m_Settings);
#endif // MPT_OS_WINDOWS
#if MPT_OS_WINDOWS
class CSoundDeviceWithThread;
class CPriorityBooster
{
private:
SoundDevice::SysInfo m_SysInfo;
bool m_BoostPriority;
int m_Priority;
DWORD task_idx;
HANDLE hTask;
int oldPriority;
public:
CPriorityBooster(SoundDevice::SysInfo sysInfo, bool boostPriority, const mpt::winstring &priorityClass, int priority);
~CPriorityBooster();
};
class CAudioThread
{
friend class CPeriodicWaker;
private:
CSoundDeviceWithThread &m_SoundDevice;
mpt::winstring m_MMCSSClass;
double m_WakeupInterval;
HANDLE m_hAudioWakeUp;
HANDLE m_hPlayThread;
HANDLE m_hAudioThreadTerminateRequest;
HANDLE m_hAudioThreadGoneIdle;
HANDLE m_hHardwareWakeupEvent;
DWORD m_dwPlayThreadId;
LONG m_AudioThreadActive;
static DWORD WINAPI AudioThreadWrapper(LPVOID user);
DWORD AudioThread();
bool IsActive();
public:
CAudioThread(CSoundDeviceWithThread &SoundDevice);
CAudioThread(const CAudioThread &) = delete;
CAudioThread &operator=(const CAudioThread &) = delete;
~CAudioThread();
void Activate();
void Deactivate();
void SetWakeupEvent(HANDLE ev);
void SetWakeupInterval(double seconds);
};
class CSoundDeviceWithThread
: public SoundDevice::Base
{
friend class CAudioThread;
protected:
CAudioThread m_AudioThread;
private:
void FillAudioBufferLocked();
protected:
void SetWakeupEvent(HANDLE ev);
void SetWakeupInterval(double seconds);
public:
CSoundDeviceWithThread(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo);
virtual ~CSoundDeviceWithThread();
bool InternalStart();
void InternalStop();
virtual void StartFromSoundThread() = 0;
virtual void StopFromSoundThread() = 0;
};
#endif // MPT_OS_WINDOWS
#if MPT_OS_LINUX || MPT_OS_MACOSX_OR_IOS || MPT_OS_FREEBSD
class semaphore
{
private:
unsigned int count;
unsigned int waiters_count;
std::mutex mutex;
std::condition_variable count_nonzero;
public:
semaphore(unsigned int initial_count = 0)
: count(initial_count)
, waiters_count(0)
{
return;
}
~semaphore()
{
return;
}
void wait()
{
std::unique_lock<std::mutex> l(mutex);
waiters_count++;
while(count == 0)
{
count_nonzero.wait(l);
}
waiters_count--;
count--;
}
void post()
{
std::unique_lock<std::mutex> l(mutex);
if(waiters_count > 0)
{
count_nonzero.notify_one();
}
count++;
}
void lock()
{
wait();
}
void unlock()
{
post();
}
};
class ThreadPriorityGuardImpl;
class ThreadPriorityGuard
{
private:
std::unique_ptr<ThreadPriorityGuardImpl> impl;
public:
ThreadPriorityGuard(ILogger &logger, bool active, bool realtime, int niceness, int rt_priority);
~ThreadPriorityGuard();
};
class ThreadBase
: public SoundDevice::Base
{
private:
semaphore m_ThreadStarted;
std::atomic<bool> m_ThreadStopRequest;
std::thread m_Thread;
private:
static void ThreadProcStatic(ThreadBase *this_);
void ThreadProc();
public:
ThreadBase(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo);
virtual ~ThreadBase();
bool InternalStart();
void InternalStop();
virtual void InternalStartFromSoundThread() = 0;
virtual void InternalWaitFromSoundThread() = 0;
virtual void InternalStopFromSoundThread() = 0;
};
#endif // MPT_OS_LINUX || MPT_OS_MACOSX_OR_IOS || MPT_OS_FREEBSD
} // namespace SoundDevice
OPENMPT_NAMESPACE_END
|