1
0

WineSoundDeviceStub.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * WineSoundDeviceStub.cpp
  3. * -----------------------
  4. * Purpose: Stub sound device driver class connection to WineSupport Wrapper.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #if MPT_COMPILER_MSVC
  11. #pragma warning(disable:4800) // 'T' : forcing value to bool 'true' or 'false' (performance warning)
  12. #endif // MPT_COMPILER_MSVC
  13. #include "WineSoundDeviceStub.h"
  14. #include "openmpt/sounddevice/SoundDevice.hpp"
  15. #include "../common/misc_util.h"
  16. #include "MPTrackWine.h"
  17. #include "wine/NativeSoundDeviceMarshalling.h"
  18. OPENMPT_NAMESPACE_BEGIN
  19. namespace SoundDevice {
  20. static mpt::ustring GetTypePrefix()
  21. {
  22. return U_("Wine");
  23. }
  24. static SoundDevice::Info AddTypePrefix(SoundDevice::Info info)
  25. {
  26. info.type = GetTypePrefix() + U_("-") + info.type;
  27. info.apiPath.insert(info.apiPath.begin(), U_("Wine"));
  28. return info;
  29. }
  30. static SoundDevice::Info RemoveTypePrefix(SoundDevice::Info info)
  31. {
  32. info.type = info.type.substr(GetTypePrefix().length() + 1);
  33. info.apiPath.erase(info.apiPath.begin());
  34. return info;
  35. }
  36. std::vector<SoundDevice::Info> SoundDeviceStub::EnumerateDevices(ILogger &logger, SoundDevice::SysInfo sysInfo)
  37. {
  38. ComponentHandle<ComponentWineWrapper> WineWrapper;
  39. if(!IsComponentAvailable(WineWrapper))
  40. {
  41. return std::vector<SoundDevice::Info>();
  42. }
  43. MPT_UNREFERENCED_PARAMETER(logger);
  44. MPT_UNREFERENCED_PARAMETER(sysInfo); // we do not want to pass this to the native layer because it would actually be totally wrong
  45. std::vector<SoundDevice::Info> result = json_cast<std::vector<SoundDevice::Info> >(WineWrapper->SoundDevice_EnumerateDevices());
  46. for(auto &info : result)
  47. {
  48. info = AddTypePrefix(info);
  49. }
  50. return result;
  51. }
  52. SoundDeviceStub::SoundDeviceStub(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo)
  53. : impl(nullptr)
  54. {
  55. MPT_UNREFERENCED_PARAMETER(logger);
  56. MPT_UNREFERENCED_PARAMETER(sysInfo); // we do not want to pass this to the native layer because it would actually be totally wrong
  57. info = RemoveTypePrefix(info);
  58. impl = w->OpenMPT_Wine_Wrapper_SoundDevice_Construct(json_cast<std::string>(info).c_str());
  59. }
  60. SoundDeviceStub::~SoundDeviceStub() {
  61. if(impl)
  62. {
  63. w->OpenMPT_Wine_Wrapper_SoundDevice_Destruct(impl);
  64. impl = nullptr;
  65. }
  66. }
  67. static void __cdecl SoundDevice_MessageReceiver_SoundDeviceMessage(void * inst, uintptr_t level, const char * message)
  68. {
  69. SoundDevice::IMessageReceiver * mr = (SoundDevice::IMessageReceiver*)inst;
  70. if(!mr)
  71. {
  72. return;
  73. }
  74. mr->SoundDeviceMessage((LogLevel)level, mpt::ToUnicode(mpt::Charset::UTF8, message ? message : ""));
  75. }
  76. void SoundDeviceStub::SetMessageReceiver(SoundDevice::IMessageReceiver *receiver) {
  77. OpenMPT_Wine_Wrapper_SoundDevice_IMessageReceiver messageReceiver = {};
  78. messageReceiver.inst = receiver;
  79. messageReceiver.SoundDeviceMessageFunc = &SoundDevice_MessageReceiver_SoundDeviceMessage;
  80. return w->OpenMPT_Wine_Wrapper_SoundDevice_SetMessageReceiver(impl, &messageReceiver);
  81. }
  82. static void __cdecl SoundCallbackGetReferenceClockNowNanosecondsFunc( void * inst, uint64_t * result ) {
  83. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  84. if(!callback)
  85. {
  86. *result = 0;
  87. return;
  88. }
  89. *result = callback->SoundCallbackGetReferenceClockNowNanoseconds();
  90. }
  91. static void __cdecl SoundCallbackPreStartFunc( void * inst ) {
  92. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  93. if(!callback)
  94. {
  95. return;
  96. }
  97. callback->SoundCallbackPreStart();
  98. }
  99. static void __cdecl SoundCallbackPostStopFunc( void * inst ) {
  100. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  101. if(!callback)
  102. {
  103. return;
  104. }
  105. callback->SoundCallbackPostStop();
  106. }
  107. static void __cdecl SoundCallbackIsLockedByCurrentThreadFunc( void * inst, uintptr_t * result ) {
  108. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  109. if(!callback)
  110. {
  111. *result = 0;
  112. return;
  113. }
  114. *result = callback->SoundCallbackIsLockedByCurrentThread();
  115. }
  116. static void __cdecl SoundCallbackLockFunc( void * inst ) {
  117. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  118. if(!callback)
  119. {
  120. return;
  121. }
  122. callback->SoundCallbackLock();
  123. }
  124. static void __cdecl SoundCallbackLockedGetReferenceClockNowNanosecondsFunc( void * inst, uint64_t * result ) {
  125. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  126. if(!callback)
  127. {
  128. *result = 0;
  129. return;
  130. }
  131. *result = callback->SoundCallbackLockedGetReferenceClockNowNanoseconds();
  132. }
  133. static void __cdecl SoundCallbackLockedProcessPrepareFunc( void * inst, const OpenMPT_SoundDevice_TimeInfo * timeInfo ) {
  134. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  135. SoundDevice::TimeInfo ti = C::decode(*timeInfo);
  136. if(!callback)
  137. {
  138. return;
  139. }
  140. callback->SoundCallbackLockedProcessPrepare(ti);
  141. }
  142. static void __cdecl SoundCallbackLockedProcessUint8Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, uint8_t * buffer, const uint8_t * inputBuffer ) {
  143. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  144. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  145. if(!callback)
  146. {
  147. return;
  148. }
  149. callback->SoundCallbackLockedProcess(bf, numFrames, buffer, inputBuffer);
  150. }
  151. static void __cdecl SoundCallbackLockedProcessInt8Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int8_t * buffer, const int8_t * inputBuffer ) {
  152. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  153. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  154. if(!callback)
  155. {
  156. return;
  157. }
  158. callback->SoundCallbackLockedProcess(bf, numFrames, buffer, inputBuffer);
  159. }
  160. static void __cdecl SoundCallbackLockedProcessInt16Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int16_t * buffer, const int16_t * inputBuffer ) {
  161. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  162. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  163. if(!callback)
  164. {
  165. return;
  166. }
  167. callback->SoundCallbackLockedProcess(bf, numFrames, buffer, inputBuffer);
  168. }
  169. static void __cdecl SoundCallbackLockedProcessInt24Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, OpenMPT_int24 * buffer, const OpenMPT_int24 * inputBuffer ) {
  170. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  171. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  172. if(!callback)
  173. {
  174. return;
  175. }
  176. callback->SoundCallbackLockedProcess(bf, numFrames, static_cast<int24*>(buffer), static_cast<const int24*>(inputBuffer));
  177. }
  178. static void __cdecl SoundCallbackLockedProcessInt32Func( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, int32_t * buffer, const int32_t * inputBuffer ) {
  179. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  180. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  181. if(!callback)
  182. {
  183. return;
  184. }
  185. callback->SoundCallbackLockedProcess(bf, numFrames, buffer, inputBuffer);
  186. }
  187. static void __cdecl SoundCallbackLockedProcessFloatFunc( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, float * buffer, const float * inputBuffer ) {
  188. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  189. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  190. if(!callback)
  191. {
  192. return;
  193. }
  194. callback->SoundCallbackLockedProcess(bf, numFrames, buffer, inputBuffer);
  195. }
  196. static void __cdecl SoundCallbackLockedProcessDoubleFunc( void * inst, const OpenMPT_SoundDevice_BufferFormat * bufferFormat, uintptr_t numFrames, double * buffer, const double * inputBuffer ) {
  197. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  198. SoundDevice::BufferFormat bf = C::decode(*bufferFormat);
  199. if(!callback)
  200. {
  201. return;
  202. }
  203. callback->SoundCallbackLockedProcess(bf, numFrames, buffer, inputBuffer);
  204. }
  205. static void __cdecl SoundCallbackLockedProcessDoneFunc( void * inst, const OpenMPT_SoundDevice_TimeInfo * timeInfo ) {
  206. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  207. SoundDevice::TimeInfo ti = C::decode(*timeInfo);
  208. if(!callback)
  209. {
  210. return;
  211. }
  212. callback->SoundCallbackLockedProcessDone(ti);
  213. }
  214. static void __cdecl SoundCallbackUnlockFunc( void * inst ) {
  215. SoundDevice::ICallback * callback = ((SoundDevice::ICallback*)inst);
  216. if(!callback)
  217. {
  218. return;
  219. }
  220. callback->SoundCallbackUnlock();
  221. }
  222. void SoundDeviceStub::SetCallback(SoundDevice::ICallback *icallback) {
  223. OpenMPT_Wine_Wrapper_SoundDevice_ICallback callback = {};
  224. callback.inst = icallback;
  225. callback.SoundCallbackGetReferenceClockNowNanosecondsFunc = &SoundCallbackGetReferenceClockNowNanosecondsFunc;
  226. callback.SoundCallbackPreStartFunc = &SoundCallbackPreStartFunc;
  227. callback.SoundCallbackPostStopFunc = &SoundCallbackPostStopFunc;
  228. callback.SoundCallbackIsLockedByCurrentThreadFunc = &SoundCallbackIsLockedByCurrentThreadFunc;
  229. callback.SoundCallbackLockFunc = &SoundCallbackLockFunc;
  230. callback.SoundCallbackLockedGetReferenceClockNowNanosecondsFunc = &SoundCallbackLockedGetReferenceClockNowNanosecondsFunc;
  231. callback.SoundCallbackLockedProcessPrepareFunc = &SoundCallbackLockedProcessPrepareFunc;
  232. callback.SoundCallbackLockedProcessUint8Func = &SoundCallbackLockedProcessUint8Func;
  233. callback.SoundCallbackLockedProcessInt8Func = &SoundCallbackLockedProcessInt8Func;
  234. callback.SoundCallbackLockedProcessInt16Func = &SoundCallbackLockedProcessInt16Func;
  235. callback.SoundCallbackLockedProcessInt24Func = &SoundCallbackLockedProcessInt24Func;
  236. callback.SoundCallbackLockedProcessInt32Func = &SoundCallbackLockedProcessInt32Func;
  237. callback.SoundCallbackLockedProcessFloatFunc = &SoundCallbackLockedProcessFloatFunc;
  238. callback.SoundCallbackLockedProcessDoubleFunc = &SoundCallbackLockedProcessDoubleFunc;
  239. callback.SoundCallbackLockedProcessDoneFunc = &SoundCallbackLockedProcessDoneFunc;
  240. callback.SoundCallbackUnlockFunc = &SoundCallbackUnlockFunc;
  241. return w->OpenMPT_Wine_Wrapper_SoundDevice_SetCallback(impl, &callback);
  242. }
  243. SoundDevice::Info SoundDeviceStub::GetDeviceInfo() const {
  244. SoundDevice::Info info = json_cast<SoundDevice::Info>(w->result_as_string(w->OpenMPT_Wine_Wrapper_SoundDevice_GetDeviceInfo(impl)));
  245. info = AddTypePrefix(info);
  246. return info;
  247. }
  248. SoundDevice::Caps SoundDeviceStub::GetDeviceCaps() const {
  249. return json_cast<SoundDevice::Caps>(w->result_as_string(w->OpenMPT_Wine_Wrapper_SoundDevice_GetDeviceCaps(impl)));
  250. }
  251. SoundDevice::DynamicCaps SoundDeviceStub::GetDeviceDynamicCaps(const std::vector<uint32> &baseSampleRates) {
  252. return json_cast<SoundDevice::DynamicCaps>(w->result_as_string(w->OpenMPT_Wine_Wrapper_SoundDevice_GetDeviceDynamicCaps(impl, json_cast<std::string>(baseSampleRates).c_str())));
  253. }
  254. bool SoundDeviceStub::Init(const SoundDevice::AppInfo &appInfo) {
  255. return w->OpenMPT_Wine_Wrapper_SoundDevice_Init(impl, json_cast<std::string>(appInfo).c_str());
  256. }
  257. bool SoundDeviceStub::Open(const SoundDevice::Settings &settings) {
  258. return w->OpenMPT_Wine_Wrapper_SoundDevice_Open(impl, json_cast<std::string>(settings).c_str());
  259. }
  260. bool SoundDeviceStub::Close() {
  261. return w->OpenMPT_Wine_Wrapper_SoundDevice_Close(impl);
  262. }
  263. bool SoundDeviceStub::Start() {
  264. return w->OpenMPT_Wine_Wrapper_SoundDevice_Start(impl);
  265. }
  266. void SoundDeviceStub::Stop() {
  267. return w->OpenMPT_Wine_Wrapper_SoundDevice_Stop(impl);
  268. }
  269. FlagSet<RequestFlags> SoundDeviceStub::GetRequestFlags() const {
  270. uint32_t result = 0;
  271. w->OpenMPT_Wine_Wrapper_SoundDevice_GetRequestFlags(impl, &result);
  272. return FlagSet<RequestFlags>(result);
  273. }
  274. bool SoundDeviceStub::IsInited() const {
  275. return w->OpenMPT_Wine_Wrapper_SoundDevice_IsInited(impl);
  276. }
  277. bool SoundDeviceStub::IsOpen() const {
  278. return w->OpenMPT_Wine_Wrapper_SoundDevice_IsOpen(impl);
  279. }
  280. bool SoundDeviceStub::IsAvailable() const {
  281. return w->OpenMPT_Wine_Wrapper_SoundDevice_IsAvailable(impl);
  282. }
  283. bool SoundDeviceStub::IsPlaying() const {
  284. return w->OpenMPT_Wine_Wrapper_SoundDevice_IsPlaying(impl);
  285. }
  286. bool SoundDeviceStub::IsPlayingSilence() const {
  287. return w->OpenMPT_Wine_Wrapper_SoundDevice_IsPlayingSilence(impl);
  288. }
  289. void SoundDeviceStub::StopAndAvoidPlayingSilence() {
  290. return w->OpenMPT_Wine_Wrapper_SoundDevice_StopAndAvoidPlayingSilence(impl);
  291. }
  292. void SoundDeviceStub::EndPlayingSilence() {
  293. return w->OpenMPT_Wine_Wrapper_SoundDevice_EndPlayingSilence(impl);
  294. }
  295. bool SoundDeviceStub::OnIdle() {
  296. return w->OpenMPT_Wine_Wrapper_SoundDevice_OnIdle(impl);
  297. }
  298. SoundDevice::Settings SoundDeviceStub::GetSettings() const {
  299. return json_cast<SoundDevice::Settings>(w->result_as_string(w->OpenMPT_Wine_Wrapper_SoundDevice_GetSettings(impl)));
  300. }
  301. SampleFormat SoundDeviceStub::GetActualSampleFormat() const {
  302. int32_t result = 0;
  303. w->OpenMPT_Wine_Wrapper_SoundDevice_GetActualSampleFormat(impl, &result);
  304. return SampleFormat::FromInt(result);
  305. }
  306. SoundDevice::BufferAttributes SoundDeviceStub::GetEffectiveBufferAttributes() const {
  307. OpenMPT_SoundDevice_BufferAttributes result;
  308. w->OpenMPT_Wine_Wrapper_SoundDevice_GetEffectiveBufferAttributes(impl, &result);
  309. return C::decode(result);
  310. }
  311. SoundDevice::TimeInfo SoundDeviceStub::GetTimeInfo() const {
  312. OpenMPT_SoundDevice_TimeInfo result;
  313. w->OpenMPT_Wine_Wrapper_SoundDevice_GetTimeInfo(impl, &result);
  314. return C::decode(result);
  315. }
  316. SoundDevice::StreamPosition SoundDeviceStub::GetStreamPosition() const {
  317. OpenMPT_SoundDevice_StreamPosition result;
  318. w->OpenMPT_Wine_Wrapper_SoundDevice_GetStreamPosition(impl, &result);
  319. return C::decode(result);
  320. }
  321. bool SoundDeviceStub::DebugIsFragileDevice() const {
  322. return w->OpenMPT_Wine_Wrapper_SoundDevice_DebugIsFragileDevice(impl);
  323. }
  324. bool SoundDeviceStub::DebugInRealtimeCallback() const {
  325. return w->OpenMPT_Wine_Wrapper_SoundDevice_DebugInRealtimeCallback(impl);
  326. }
  327. SoundDevice::Statistics SoundDeviceStub::GetStatistics() const {
  328. return json_cast<SoundDevice::Statistics>(w->result_as_string(w->OpenMPT_Wine_Wrapper_SoundDevice_GetStatistics(impl)));
  329. }
  330. bool SoundDeviceStub::OpenDriverSettings() {
  331. return w->OpenMPT_Wine_Wrapper_SoundDevice_OpenDriverSettings(impl);
  332. }
  333. } // namespace SoundDevice
  334. OPENMPT_NAMESPACE_END