Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
{
Expand Down
6 changes: 6 additions & 0 deletions Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,11 @@ Task<IStreamCall> GetCallAsync(StreamCallType callType, string callId,
/// Call this resume audio playback if it was previously paused using <see cref="PauseAndroidAudioPlayback"/>.
/// </summary>
void ResumeAndroidAudioPlayback();

/// <summary>
/// Set Android Audio usage mode
/// </summary>
/// <param name="usageMode"></param>
void SetAndroidAudioUsageMode(AndroidAudioUsageMode usageMode);
}
}
30 changes: 30 additions & 0 deletions Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Unity.WebRTC;
using UnityEngine;
using Cache = StreamVideo.Core.State.Caches.Cache;
using AndroidAudioUsageMode = StreamVideo.Core.AndroidAudioUsageMode;

namespace StreamVideo.Core
{
Expand All @@ -50,6 +51,24 @@ public enum DisconnectReason

VideoServerDisconnected,
}

/// <summary>
/// Android audio usage modes for Oboe audio streams.
/// </summary>
public enum AndroidAudioUsageMode
{
/// <summary>
/// Media usage mode - suitable for media playback (e.g., music, video).
/// Use this for viewers/consumers in livestream scenarios.
/// </summary>
Media = 1,

/// <summary>
/// Voice communication usage mode - optimized for voice calls.
/// Use this for broadcasters/hosts in livestream scenarios.
/// </summary>
VoiceCommunication = 2
}

public delegate void CallHandler(IStreamCall call);

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ public static void SetPreferredAudioRoute(NativeAudioDeviceManager.AudioRouting
CallStatic("setAudioRoute", (int)audioRoute);
}

/// <summary>
/// Gets the current audio route and prints it to Unity console.
/// </summary>
public static void GetAudioRoute()
{
var routeId = CallStatic<int>("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})";
}
}

/// <summary>
/// For Android, the devices represent available audio routing options instead of physical devices.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary>
/// Gets the current audio route and prints it to Unity console.
/// </summary>
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
}
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public void UnmuteAndroidAudioPlayback()

public void SetAndroidAudioUsageMode(int usage)
{
NativeMethods.SetAndroidAudioUsageMode(self, usage);
NativeMethods.SetAudioUsageMode(self, usage);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,6 @@ public enum NativeLoggingSeverity
None,
};

#if UNITY_ANDROID && !UNITY_EDITOR
/// <summary>
/// Android audio usage modes for Oboe audio streams.
/// </summary>
Expand All @@ -733,7 +732,6 @@ public enum AndroidAudioUsageMode
/// </summary>
VoiceCommunication = 2
}
#endif

/// <summary>
/// Provides utilities and management functions for integrating WebRTC functionality.
Expand Down Expand Up @@ -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

}
Expand Down
Loading