diff --git a/Packages/StreamVideo/Runtime/Core/DeviceManagers/StreamAudioDeviceManager.cs b/Packages/StreamVideo/Runtime/Core/DeviceManagers/StreamAudioDeviceManager.cs index ca93620c..a7ad92d9 100644 --- a/Packages/StreamVideo/Runtime/Core/DeviceManagers/StreamAudioDeviceManager.cs +++ b/Packages/StreamVideo/Runtime/Core/DeviceManagers/StreamAudioDeviceManager.cs @@ -159,6 +159,19 @@ protected override void OnUpdate() base.OnUpdate(); TrySyncMicrophoneAudioSourceReadPosWithMicrophoneWritePos(); + + // Check audio route every second on Android with native audio bindings +#if UNITY_ANDROID && STREAM_DEBUG_ENABLED + if (RtcSession.UseNativeAudioBindings) + { + var currentTime = Time.time; + if (currentTime - _lastAudioRouteCheckTime >= AudioRouteCheckInterval) + { + _lastAudioRouteCheckTime = currentTime; + NativeAudioDeviceManager.GetAudioRoute(); + } + } +#endif } protected override void OnDisposing() @@ -185,6 +198,9 @@ protected override void OnDisposing() private NativeAudioDeviceManager.AudioDeviceInfo[] _inputDevicesBuffer = new NativeAudioDeviceManager.AudioDeviceInfo[128]; + + private float _lastAudioRouteCheckTime; + private const float AudioRouteCheckInterval = 5.0f; private AudioSource GetOrCreateTargetAudioSource() { diff --git a/Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs b/Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs index acd052c7..6dfa06a2 100644 --- a/Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs +++ b/Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs @@ -139,5 +139,11 @@ Task GetCallAsync(StreamCallType callType, string callId, /// Call this resume audio playback if it was previously paused using . /// void ResumeAndroidAudioPlayback(); + + /// + /// Set Android Audio usage mode + /// + /// + void SetAndroidAudioUsageMode(AndroidAudioUsageMode usageMode); } } \ No newline at end of file diff --git a/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs b/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs index b4eb3a07..6cadc40d 100644 --- a/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs +++ b/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs @@ -33,6 +33,7 @@ using Unity.WebRTC; using UnityEngine; using Cache = StreamVideo.Core.State.Caches.Cache; +using AndroidAudioUsageMode = StreamVideo.Core.AndroidAudioUsageMode; namespace StreamVideo.Core { @@ -50,6 +51,24 @@ public enum DisconnectReason VideoServerDisconnected, } + + /// + /// Android audio usage modes for Oboe audio streams. + /// + public enum AndroidAudioUsageMode + { + /// + /// Media usage mode - suitable for media playback (e.g., music, video). + /// Use this for viewers/consumers in livestream scenarios. + /// + Media = 1, + + /// + /// Voice communication usage mode - optimized for voice calls. + /// Use this for broadcasters/hosts in livestream scenarios. + /// + VoiceCommunication = 2 + } public delegate void CallHandler(IStreamCall call); @@ -335,6 +354,17 @@ public void SetAudioProcessingModule(bool enabled, bool echoCancellationEnabled, public void ResumeAndroidAudioPlayback() => InternalLowLevelClient.RtcSession.ResumeAndroidAudioPlayback(); + public void SetAndroidAudioUsageMode(AndroidAudioUsageMode usageMode) + { +#if STREAM_NATIVE_AUDIO + WebRTC.SetAndroidAudioUsageMode((Unity.WebRTC.AndroidAudioUsageMode)usageMode); + _logs.Warning("Set audio usage mode to " + Enum.GetName(typeof(AndroidAudioUsageMode), usageMode)); +#else + throw new NotSupportedException( + $"{nameof(SetAndroidAudioUsageMode)} is only supported on Android platform."); +#endif + } + #region IStreamVideoClientEventsListener event Action IStreamVideoClientEventsListener.Destroyed diff --git a/Packages/StreamVideo/Runtime/Libs/DeviceManagers/AndroidAudioDeviceManager.cs b/Packages/StreamVideo/Runtime/Libs/DeviceManagers/AndroidAudioDeviceManager.cs index 8543a733..5cebaf94 100644 --- a/Packages/StreamVideo/Runtime/Libs/DeviceManagers/AndroidAudioDeviceManager.cs +++ b/Packages/StreamVideo/Runtime/Libs/DeviceManagers/AndroidAudioDeviceManager.cs @@ -22,6 +22,29 @@ public static void SetPreferredAudioRoute(NativeAudioDeviceManager.AudioRouting CallStatic("setAudioRoute", (int)audioRoute); } + /// + /// Gets the current audio route and prints it to Unity console. + /// + public static void GetAudioRoute() + { + var routeId = CallStatic("getAudioRoute"); + var routeName = GetRouteName(routeId); + Debug.Log($"[AndroidAudioDeviceManager] Current audio route: {routeName} (ID: {routeId})"); + } + + private static string GetRouteName(int routeId) + { + switch (routeId) + { + case 0: return "Earpiece"; + case 1: return "Speaker"; + case 2: return "Bluetooth"; + case 3: return "Wired Headset"; + case 4: return "USB Headset"; + default: return $"Unknown ({routeId})"; + } + } + /// /// For Android, the devices represent available audio routing options instead of physical devices. /// diff --git a/Packages/StreamVideo/Runtime/Libs/DeviceManagers/NativeAudioDeviceManager.cs b/Packages/StreamVideo/Runtime/Libs/DeviceManagers/NativeAudioDeviceManager.cs index 63f30f21..04ddddd6 100644 --- a/Packages/StreamVideo/Runtime/Libs/DeviceManagers/NativeAudioDeviceManager.cs +++ b/Packages/StreamVideo/Runtime/Libs/DeviceManagers/NativeAudioDeviceManager.cs @@ -76,6 +76,18 @@ public static void GetAudioInputDevices(ref AudioDeviceInfo[] result) AndroidAudioDeviceManager.GetAudioInputDevices(ref result); #else UnityEngine.Debug.LogWarning($"{nameof(GetAudioInputDevices)} is not supported on this platform: " + UnityEngine.Application.platform); +#endif + } + + /// + /// Gets the current audio route and prints it to Unity console. + /// + public static void GetAudioRoute() + { +#if UNITY_ANDROID + AndroidAudioDeviceManager.GetAudioRoute(); +#else + UnityEngine.Debug.LogWarning($"{nameof(GetAudioRoute)} is not supported on this platform: " + UnityEngine.Application.platform); #endif } } diff --git a/Packages/StreamVideo/Runtime/Libs/DeviceManagers/Plugins/Android/AndroidAudioManagerWrapper-debug.aar b/Packages/StreamVideo/Runtime/Libs/DeviceManagers/Plugins/Android/AndroidAudioManagerWrapper-debug.aar index dc591227..8cf8df0e 100644 Binary files a/Packages/StreamVideo/Runtime/Libs/DeviceManagers/Plugins/Android/AndroidAudioManagerWrapper-debug.aar and b/Packages/StreamVideo/Runtime/Libs/DeviceManagers/Plugins/Android/AndroidAudioManagerWrapper-debug.aar differ diff --git a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs index be9498af..8f9f3799 100644 --- a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs +++ b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs @@ -424,7 +424,7 @@ public void UnmuteAndroidAudioPlayback() public void SetAndroidAudioUsageMode(int usage) { - NativeMethods.SetAndroidAudioUsageMode(self, usage); + NativeMethods.SetAudioUsageMode(self, usage); } #endif diff --git a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs index d52e2881..08db4aa1 100644 --- a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs +++ b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs @@ -715,7 +715,6 @@ public enum NativeLoggingSeverity None, }; -#if UNITY_ANDROID && !UNITY_EDITOR /// /// Android audio usage modes for Oboe audio streams. /// @@ -733,7 +732,6 @@ public enum AndroidAudioUsageMode /// VoiceCommunication = 2 } -#endif /// /// Provides utilities and management functions for integrating WebRTC functionality. @@ -1889,7 +1887,7 @@ public static extern IntPtr CreateVideoRenderer( public static extern void GetAudioProcessingModuleConfig(IntPtr context, out bool enabled, out bool echoCancellationEnabled, out bool autoGainEnabled, out bool noiseSuppressionEnabled, out int noiseSuppressionLevel); [DllImport(WebRTC.Lib)] - public static extern void SetAndroidAudioUsageMode(IntPtr context, int usage); + public static extern void SetAudioUsageMode(IntPtr context, int usage); #endif }