VoiceSDKImpl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using Meta.WitAi;
  22. using Meta.WitAi.Configuration;
  23. using Meta.WitAi.Events;
  24. using Meta.WitAi.Interfaces;
  25. using Meta.WitAi.Requests;
  26. using Oculus.Voice.Core.Bindings.Android;
  27. using Oculus.Voice.Interfaces;
  28. using Debug = UnityEngine.Debug;
  29. namespace Oculus.Voice.Bindings.Android
  30. {
  31. // TODO: Fix VoiceSDKImpl to work with IVoiceRequest
  32. public class VoiceSDKImpl : BaseAndroidConnectionImpl<VoiceSDKBinding>,
  33. IPlatformVoiceService, IVCBindingEvents
  34. {
  35. private bool _isServiceAvailable = true;
  36. public Action OnServiceNotAvailableEvent;
  37. private IVoiceService _baseVoiceService;
  38. private bool _isActive;
  39. public VoiceSDKImpl(IVoiceService baseVoiceService) : base(
  40. "com.oculus.assistant.api.unity.immersivevoicecommands.UnityIVCServiceFragment")
  41. {
  42. _baseVoiceService = baseVoiceService;
  43. }
  44. public bool PlatformSupportsWit => service.PlatformSupportsWit && _isServiceAvailable;
  45. public bool Active => service.Active && _isActive;
  46. public bool IsRequestActive => service.IsRequestActive;
  47. public bool MicActive => service.MicActive;
  48. public void SetRuntimeConfiguration(WitRuntimeConfiguration configuration)
  49. {
  50. service.SetRuntimeConfiguration(configuration);
  51. }
  52. private VoiceSDKListenerBinding eventBinding;
  53. public ITranscriptionProvider TranscriptionProvider { get; set; }
  54. public bool CanActivateAudio()
  55. {
  56. return true;
  57. }
  58. public bool CanSend()
  59. {
  60. return true;
  61. }
  62. public override void Connect(string version)
  63. {
  64. base.Connect(version);
  65. eventBinding = new VoiceSDKListenerBinding(this, this);
  66. eventBinding.VoiceEvents.OnStoppedListening.AddListener(OnStoppedListening);
  67. service.SetListener(eventBinding);
  68. service.Connect();
  69. Debug.Log(
  70. $"Platform integration initialization complete. Platform integrations are {(PlatformSupportsWit ? "active" : "inactive")}");
  71. }
  72. public override void Disconnect()
  73. {
  74. base.Disconnect();
  75. if (null != eventBinding)
  76. {
  77. eventBinding.VoiceEvents.OnStoppedListening.RemoveListener(OnStoppedListening);
  78. }
  79. }
  80. private void OnStoppedListening()
  81. {
  82. _isActive = false;
  83. }
  84. public VoiceServiceRequest Activate(string text, WitRequestOptions requestOptions,
  85. VoiceServiceRequestEvents requestEvents)
  86. {
  87. eventBinding.VoiceEvents.OnRequestOptionSetup?.Invoke(requestOptions);
  88. service.Activate(text, requestOptions);
  89. return null;
  90. }
  91. public VoiceServiceRequest Activate(WitRequestOptions requestOptions,
  92. VoiceServiceRequestEvents requestEvents)
  93. {
  94. if (_isActive) return null;
  95. _isActive = true;
  96. eventBinding.VoiceEvents.OnRequestOptionSetup?.Invoke(requestOptions);
  97. service.Activate(requestOptions);
  98. return null;
  99. }
  100. public VoiceServiceRequest ActivateImmediately(WitRequestOptions requestOptions,
  101. VoiceServiceRequestEvents requestEvents)
  102. {
  103. if (_isActive) return null;
  104. _isActive = true;
  105. eventBinding.VoiceEvents.OnRequestOptionSetup?.Invoke(requestOptions);
  106. service.ActivateImmediately(requestOptions);
  107. return null;
  108. }
  109. public void Deactivate()
  110. {
  111. _isActive = false;
  112. service.Deactivate();
  113. }
  114. public void DeactivateAndAbortRequest()
  115. {
  116. _isActive = false;
  117. service.Deactivate();
  118. }
  119. public void DeactivateAndAbortRequest(VoiceServiceRequest request)
  120. {
  121. }
  122. public void OnServiceNotAvailable(string error, string message)
  123. {
  124. _isActive = false;
  125. _isServiceAvailable = false;
  126. OnServiceNotAvailableEvent?.Invoke();
  127. }
  128. public VoiceEvents VoiceEvents
  129. {
  130. get => _baseVoiceService.VoiceEvents;
  131. set => _baseVoiceService.VoiceEvents = value;
  132. }
  133. public TelemetryEvents TelemetryEvents
  134. {
  135. get => _baseVoiceService.TelemetryEvents;
  136. set => _baseVoiceService.TelemetryEvents = value;
  137. }
  138. }
  139. }