WaapiPlaybackTransport.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #include "WaapiPlaybackTransport.h"
  16. #include "AkWaapiUtils.h"
  17. #include "IAudiokineticTools.h"
  18. #include "Async/Async.h"
  19. WaapiPlaybackTransport::~WaapiPlaybackTransport()
  20. {
  21. StopAndDestroyAll();
  22. }
  23. int32 WaapiPlaybackTransport::FindOrAdd(const FGuid& InItemID)
  24. {
  25. const FString itemIdStringField = InItemID.ToString(EGuidFormats::DigitsWithHyphensInBraces);
  26. TSharedPtr<FJsonObject> Result;
  27. int32 TransportID = -1;
  28. {
  29. FScopeLock AutoLock(&TransportItemsLock);
  30. if (TransportItems.Contains(InItemID))
  31. {
  32. return TransportItems[InItemID].TransportID;
  33. }
  34. }
  35. #if AK_SUPPORT_WAAPI
  36. auto WaapiClient = FAkWaapiClient::Get();
  37. if(!WaapiClient)
  38. {
  39. UE_LOG(LogAudiokineticTools, Log, TEXT("Unable to connect to Waapi"));
  40. return TransportID;
  41. }
  42. if (WaapiClient->Call(ak::wwise::core::transport::create, { { WwiseWaapiHelper::OBJECT, itemIdStringField } }, Result))
  43. {
  44. TransportID = Result->GetIntegerField(WwiseWaapiHelper::TRANSPORT);
  45. uint64 SubscriptionID = SubscribeToStateChanged(TransportID);
  46. {
  47. FScopeLock AutoLock(&TransportItemsLock);
  48. TransportItems.Add(InItemID, TransportInfo(TransportID, SubscriptionID));
  49. }
  50. }
  51. #endif
  52. return TransportID;
  53. }
  54. void WaapiPlaybackTransport::Remove(const FGuid& InItemID)
  55. {
  56. auto WaapiClient = FAkWaapiClient::Get();
  57. if (!WaapiClient)
  58. return;
  59. uint64 SubscriptionID;
  60. TransportInfo Item(0, 0);
  61. {
  62. FScopeLock AutoLock(&TransportItemsLock);
  63. if (!TransportItems.Contains(InItemID))
  64. {
  65. return;
  66. }
  67. Item = TransportItems[InItemID];
  68. }
  69. SubscriptionID = Item.SubscriptionID;
  70. TSharedRef<FJsonObject> Args = MakeShared<FJsonObject>();
  71. Args->SetNumberField(WwiseWaapiHelper::TRANSPORT, Item.TransportID);
  72. TSharedPtr<FJsonObject> Result;
  73. if (SubscriptionID != 0)
  74. {
  75. WaapiClient->Unsubscribe(SubscriptionID, Result);
  76. }
  77. #if AK_SUPPORT_WAAPI
  78. TSharedRef<FJsonObject> Options = MakeShared<FJsonObject>();
  79. if (WaapiClient->Call(ak::wwise::core::transport::destroy, Args, Options, Result))
  80. {
  81. {
  82. FScopeLock AutoLock(&TransportItemsLock);
  83. TransportItems.Remove(InItemID);
  84. }
  85. }
  86. #endif
  87. }
  88. void WaapiPlaybackTransport::TogglePlay(int32 InTransportID)
  89. {
  90. auto WaapiClient = FAkWaapiClient::Get();
  91. if (!WaapiClient)
  92. {
  93. UE_LOG(LogAudiokineticTools, Log, TEXT("Unable to connect to localhost"));
  94. return;
  95. }
  96. TSharedRef<FJsonObject> Args = MakeShared<FJsonObject>();
  97. Args->SetStringField(WwiseWaapiHelper::ACTION, WwiseWaapiHelper::PLAYSTOP);
  98. Args->SetNumberField(WwiseWaapiHelper::TRANSPORT, InTransportID);
  99. #if AK_SUPPORT_WAAPI
  100. TSharedPtr<FJsonObject> Result;
  101. TSharedRef<FJsonObject> Options = MakeShared<FJsonObject>();
  102. if (!WaapiClient->Call(ak::wwise::core::transport::executeAction, Args, Options, Result))
  103. {
  104. UE_LOG(LogAudiokineticTools, Log, TEXT("Failed to trigger playback"));
  105. }
  106. #endif
  107. }
  108. void WaapiPlaybackTransport::Stop(int32 InTransportID)
  109. {
  110. auto WaapiClient = FAkWaapiClient::Get();
  111. if (!WaapiClient)
  112. return;
  113. TSharedRef<FJsonObject> Args = MakeShared<FJsonObject>();
  114. Args->SetStringField(WwiseWaapiHelper::ACTION, WwiseWaapiHelper::STOP);
  115. Args->SetNumberField(WwiseWaapiHelper::TRANSPORT, InTransportID);
  116. #if AK_SUPPORT_WAAPI
  117. TSharedPtr<FJsonObject> Result;
  118. TSharedRef<FJsonObject> Options = MakeShared<FJsonObject>();
  119. if (!WaapiClient->Call(ak::wwise::core::transport::executeAction, Args, Options, Result))
  120. {
  121. UE_LOG(LogAudiokineticTools, Log, TEXT("Cannot stop event."));
  122. }
  123. #endif
  124. }
  125. void WaapiPlaybackTransport::StopAndDestroyAll()
  126. {
  127. FScopeLock AutoLock(&TransportItemsLock);
  128. for (auto Iter = TransportItems.CreateIterator(); Iter; ++Iter)
  129. {
  130. Stop(Iter.Value().TransportID);
  131. Remove(Iter.Key());
  132. }
  133. TransportItems.Empty();
  134. }
  135. bool WaapiPlaybackTransport::IsPlaying(const FGuid& InItemID)
  136. {
  137. if (!FAkWaapiClient::Get())
  138. {
  139. return false;
  140. }
  141. return TransportItems.Contains(InItemID);
  142. }
  143. uint64 WaapiPlaybackTransport::SubscribeToStateChanged(int32 TransportID)
  144. {
  145. auto WaapiClient = FAkWaapiClient::Get();
  146. if (!WaapiClient)
  147. return 0;
  148. auto WampEventCallback = WampEventCallback::CreateLambda(
  149. [this](uint64_t ID, TSharedPtr<FJsonObject> UEJsonObject)
  150. {
  151. AsyncTask(ENamedThreads::AnyThread, [this, UEJsonObject]
  152. {
  153. this->OnStateChanged(UEJsonObject);
  154. });
  155. });
  156. TSharedRef<FJsonObject> Options = MakeShared<FJsonObject>();
  157. Options->SetNumberField(WwiseWaapiHelper::TRANSPORT, TransportID);
  158. TSharedPtr<FJsonObject> OutJsonResult;
  159. uint64 SubscriptionID = 0;
  160. #if AK_SUPPORT_WAAPI
  161. WaapiClient->Subscribe(ak::wwise::core::transport::stateChanged, Options, WampEventCallback, SubscriptionID, OutJsonResult);
  162. #endif
  163. return SubscriptionID;
  164. }
  165. void WaapiPlaybackTransport::OnStateChanged(TSharedPtr<FJsonObject> InUEJsonObject)
  166. {
  167. const FString NewState = InUEJsonObject->GetStringField(WwiseWaapiHelper::STATE);
  168. FGuid ItemID;
  169. FGuid::Parse(InUEJsonObject->GetStringField(WwiseWaapiHelper::OBJECT), ItemID);
  170. const int32 TransportID = InUEJsonObject->GetNumberField(WwiseWaapiHelper::TRANSPORT);
  171. if (NewState == WwiseWaapiHelper::STOPPED)
  172. {
  173. Remove(ItemID);
  174. }
  175. else if (NewState == WwiseWaapiHelper::PLAYING)
  176. {
  177. {
  178. FScopeLock AutoLock(&TransportItemsLock);
  179. if (!TransportItems.Contains(ItemID))
  180. {
  181. TransportItems.Add(ItemID, TransportInfo(TransportID, 0));
  182. }
  183. }
  184. }
  185. }