aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/openmpt/sounddevice/SoundDeviceASIO.hpp
blob: 6448532807605381d5ef8d188e3e304fed9cac20 (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: Olivier Lapicque */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */


#pragma once

#include "openmpt/all/BuildSettings.hpp"

#ifdef MPT_WITH_ASIO

#include "SoundDevice.hpp"
#include "SoundDeviceBase.hpp"

#include "mpt/string/types.hpp"
#include "openmpt/base/Types.hpp"
#include "openmpt/logging/Logger.hpp"

#include <atomic>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

#include <cassert>

#include <ASIOModern/ASIO.hpp>
#include <ASIOModern/ASIOSystemWindows.hpp>

#if defined(MODPLUG_TRACKER)
#if !defined(MPT_BUILD_WINESUPPORT)
#include "../mptrack/ExceptionHandler.h"
#endif  // !MPT_BUILD_WINESUPPORT
#endif  // MODPLUG_TRACKER

#endif  // MPT_WITH_ASIO

OPENMPT_NAMESPACE_BEGIN

namespace SoundDevice
{

#ifdef MPT_WITH_ASIO


class ASIOException
	: public std::runtime_error
{
public:
	ASIOException(const std::string &msg)
		: std::runtime_error(msg)
	{
		return;
	}
};

class CASIODevice
	: public SoundDevice::Base
	, private ASIO::Driver::CallbackHandler
{

	friend class TemporaryASIODriverOpener;

protected:
	std::unique_ptr<ASIO::Windows::IBufferSwitchDispatcher> m_DeferredBufferSwitchDispatcher;
	std::unique_ptr<ASIO::Driver> m_Driver;

#if defined(MODPLUG_TRACKER) && !defined(MPT_BUILD_WINESUPPORT)
	using CrashContext = ExceptionHandler::Context;
	using CrashContextGuard = ExceptionHandler::ContextSetter;
#else   // !(MODPLUG_TRACKER && !MPT_BUILD_WINESUPPORT)
	struct CrashContext
	{
		void SetDescription(mpt::ustring)
		{
			return;
		}
	};
	struct CrashContextGuard
	{
		CrashContextGuard(CrashContext *)
		{
			return;
		}
	};
#endif  // MODPLUG_TRACKER && !MPT_BUILD_WINESUPPORT
	CrashContext m_Ectx;

	class ASIODriverWithContext
	{
	private:
		ASIO::Driver *m_Driver;
		CrashContextGuard m_Guard;

	public:
		ASIODriverWithContext(ASIO::Driver *driver, CrashContext *ectx)
			: m_Driver(driver)
			, m_Guard(ectx)
		{
			assert(driver);
			assert(ectx);
		}
		ASIODriverWithContext(const ASIODriverWithContext &) = delete;
		ASIODriverWithContext &operator=(const ASIODriverWithContext &) = delete;
		ASIO::Driver *operator->()
		{
			return m_Driver;
		}
	};

	ASIODriverWithContext AsioDriver()
	{
		assert(m_Driver);
		return ASIODriverWithContext{m_Driver.get(), &m_Ectx};
	}

	double m_BufferLatency;
	ASIO::Long m_nAsioBufferLen;
	std::vector<ASIO::BufferInfo> m_BufferInfo;
	bool m_BuffersCreated;
	std::vector<ASIO::ChannelInfo> m_ChannelInfo;
	std::vector<double> m_SampleBufferDouble;
	std::vector<float> m_SampleBufferFloat;
	std::vector<int16> m_SampleBufferInt16;
	std::vector<int24> m_SampleBufferInt24;
	std::vector<int32> m_SampleBufferInt32;
	std::vector<double> m_SampleInputBufferDouble;
	std::vector<float> m_SampleInputBufferFloat;
	std::vector<int16> m_SampleInputBufferInt16;
	std::vector<int24> m_SampleInputBufferInt24;
	std::vector<int32> m_SampleInputBufferInt32;
	bool m_CanOutputReady;

	bool m_DeviceRunning;
	uint64 m_TotalFramesWritten;
	bool m_DeferredProcessing;
	ASIO::BufferIndex m_BufferIndex;
	std::atomic<bool> m_RenderSilence;
	std::atomic<bool> m_RenderingSilence;

	int64 m_StreamPositionOffset;

	using AsioRequests = uint8;
	struct AsioRequest
	{
		enum AsioRequestEnum : AsioRequests
		{
			LatenciesChanged = 1 << 0,
		};
	};
	std::atomic<AsioRequests> m_AsioRequest;

	using AsioFeatures = uint16;
	struct AsioFeature
	{
		enum AsioFeatureEnum : AsioFeatures
		{
			ResetRequest = 1 << 0,
			ResyncRequest = 1 << 1,
			BufferSizeChange = 1 << 2,
			Overload = 1 << 3,
			SampleRateChange = 1 << 4,
			DeferredProcess = 1 << 5,
		};
	};
	mutable std::atomic<AsioFeatures> m_UsedFeatures;
	static mpt::ustring AsioFeaturesToString(AsioFeatures features);

	mutable std::atomic<uint32> m_DebugRealtimeThreadID;

	void SetRenderSilence(bool silence, bool wait = false);

public:
	CASIODevice(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo);
	~CASIODevice();

private:
	void InitMembers();
	bool HandleRequests();  // return true if any work has been done
	void UpdateLatency();

	void InternalStopImpl(bool force);

public:
	bool InternalOpen();
	bool InternalClose();
	void InternalFillAudioBuffer();
	bool InternalStart();
	void InternalStop();
	bool InternalIsOpen() const { return m_BuffersCreated; }

	bool InternalIsPlayingSilence() const;
	void InternalStopAndAvoidPlayingSilence();
	void InternalEndPlayingSilence();

	bool OnIdle() { return HandleRequests(); }

	SoundDevice::Caps InternalGetDeviceCaps();
	SoundDevice::DynamicCaps GetDeviceDynamicCaps(const std::vector<uint32> &baseSampleRates);

	bool OpenDriverSettings();

	bool DebugIsFragileDevice() const;
	bool DebugInRealtimeCallback() const;

	SoundDevice::Statistics GetStatistics() const;

public:
	static std::unique_ptr<SoundDevice::BackendInitializer> BackendInitializer() { return std::make_unique<SoundDevice::BackendInitializer>(); }
	static std::vector<SoundDevice::Info> EnumerateDevices(ILogger &logger, SoundDevice::SysInfo sysInfo);

protected:
	void OpenDriver();
	void CloseDriver();
	bool IsDriverOpen() const { return (m_Driver != nullptr); }

	bool InternalHasTimeInfo() const;

	SoundDevice::BufferAttributes InternalGetEffectiveBufferAttributes() const;

protected:
	void FillAsioBuffer(bool useSource = true);

private:
	// CallbackHandler

	void MessageResetRequest() noexcept override;
	bool MessageBufferSizeChange(ASIO::Long newSize) noexcept override;
	bool MessageResyncRequest() noexcept override;
	void MessageLatenciesChanged() noexcept override;
	ASIO::Long MessageMMCCommand(ASIO::Long value, const void *message, const ASIO::Double *opt) noexcept override;
	void MessageOverload() noexcept override;

	ASIO::Long MessageUnknown(ASIO::MessageSelector selector, ASIO::Long value, const void *message, const ASIO::Double *opt) noexcept override;

	void RealtimeSampleRateDidChange(ASIO::SampleRate sRate) noexcept override;
	void RealtimeRequestDeferredProcessing(bool value) noexcept override;
	void RealtimeTimeInfo(ASIO::Time time) noexcept override;
	void RealtimeBufferSwitch(ASIO::BufferIndex bufferIndex) noexcept override;

	void RealtimeBufferSwitchImpl(ASIO::BufferIndex bufferIndex) noexcept;

private:
	void ExceptionHandler(const char *func);
};

#endif  // MPT_WITH_ASIO

}  // namespace SoundDevice

OPENMPT_NAMESPACE_END