Skip to content

Commit 891c5ec

Browse files
authored
Merge pull request #200 from GetStream/feature/uni-149-expose-audio-usage-mode-in-the-video-client
Feature/uni 149 expose audio usage mode in the video client
2 parents 1876bc6 + 926137e commit 891c5ec

File tree

8 files changed

+89
-4
lines changed

8 files changed

+89
-4
lines changed

Packages/StreamVideo/Runtime/Core/DeviceManagers/StreamAudioDeviceManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ protected override void OnUpdate()
159159
base.OnUpdate();
160160

161161
TrySyncMicrophoneAudioSourceReadPosWithMicrophoneWritePos();
162+
163+
// Check audio route every second on Android with native audio bindings
164+
#if UNITY_ANDROID && STREAM_DEBUG_ENABLED
165+
if (RtcSession.UseNativeAudioBindings)
166+
{
167+
var currentTime = Time.time;
168+
if (currentTime - _lastAudioRouteCheckTime >= AudioRouteCheckInterval)
169+
{
170+
_lastAudioRouteCheckTime = currentTime;
171+
NativeAudioDeviceManager.GetAudioRoute();
172+
}
173+
}
174+
#endif
162175
}
163176

164177
protected override void OnDisposing()
@@ -185,6 +198,9 @@ protected override void OnDisposing()
185198

186199
private NativeAudioDeviceManager.AudioDeviceInfo[] _inputDevicesBuffer
187200
= new NativeAudioDeviceManager.AudioDeviceInfo[128];
201+
202+
private float _lastAudioRouteCheckTime;
203+
private const float AudioRouteCheckInterval = 5.0f;
188204

189205
private AudioSource GetOrCreateTargetAudioSource()
190206
{

Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,11 @@ Task<IStreamCall> GetCallAsync(StreamCallType callType, string callId,
139139
/// Call this resume audio playback if it was previously paused using <see cref="PauseAndroidAudioPlayback"/>.
140140
/// </summary>
141141
void ResumeAndroidAudioPlayback();
142+
143+
/// <summary>
144+
/// Set Android Audio usage mode
145+
/// </summary>
146+
/// <param name="usageMode"></param>
147+
void SetAndroidAudioUsageMode(AndroidAudioUsageMode usageMode);
142148
}
143149
}

Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
using Unity.WebRTC;
3434
using UnityEngine;
3535
using Cache = StreamVideo.Core.State.Caches.Cache;
36+
using AndroidAudioUsageMode = StreamVideo.Core.AndroidAudioUsageMode;
3637

3738
namespace StreamVideo.Core
3839
{
@@ -50,6 +51,24 @@ public enum DisconnectReason
5051

5152
VideoServerDisconnected,
5253
}
54+
55+
/// <summary>
56+
/// Android audio usage modes for Oboe audio streams.
57+
/// </summary>
58+
public enum AndroidAudioUsageMode
59+
{
60+
/// <summary>
61+
/// Media usage mode - suitable for media playback (e.g., music, video).
62+
/// Use this for viewers/consumers in livestream scenarios.
63+
/// </summary>
64+
Media = 1,
65+
66+
/// <summary>
67+
/// Voice communication usage mode - optimized for voice calls.
68+
/// Use this for broadcasters/hosts in livestream scenarios.
69+
/// </summary>
70+
VoiceCommunication = 2
71+
}
5372

5473
public delegate void CallHandler(IStreamCall call);
5574

@@ -335,6 +354,17 @@ public void SetAudioProcessingModule(bool enabled, bool echoCancellationEnabled,
335354

336355
public void ResumeAndroidAudioPlayback() => InternalLowLevelClient.RtcSession.ResumeAndroidAudioPlayback();
337356

357+
public void SetAndroidAudioUsageMode(AndroidAudioUsageMode usageMode)
358+
{
359+
#if STREAM_NATIVE_AUDIO
360+
WebRTC.SetAndroidAudioUsageMode((Unity.WebRTC.AndroidAudioUsageMode)usageMode);
361+
_logs.Warning("Set audio usage mode to " + Enum.GetName(typeof(AndroidAudioUsageMode), usageMode));
362+
#else
363+
throw new NotSupportedException(
364+
$"{nameof(SetAndroidAudioUsageMode)} is only supported on Android platform.");
365+
#endif
366+
}
367+
338368
#region IStreamVideoClientEventsListener
339369

340370
event Action IStreamVideoClientEventsListener.Destroyed

Packages/StreamVideo/Runtime/Libs/DeviceManagers/AndroidAudioDeviceManager.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ public static void SetPreferredAudioRoute(NativeAudioDeviceManager.AudioRouting
2222
CallStatic("setAudioRoute", (int)audioRoute);
2323
}
2424

25+
/// <summary>
26+
/// Gets the current audio route and prints it to Unity console.
27+
/// </summary>
28+
public static void GetAudioRoute()
29+
{
30+
var routeId = CallStatic<int>("getAudioRoute");
31+
var routeName = GetRouteName(routeId);
32+
Debug.Log($"[AndroidAudioDeviceManager] Current audio route: {routeName} (ID: {routeId})");
33+
}
34+
35+
private static string GetRouteName(int routeId)
36+
{
37+
switch (routeId)
38+
{
39+
case 0: return "Earpiece";
40+
case 1: return "Speaker";
41+
case 2: return "Bluetooth";
42+
case 3: return "Wired Headset";
43+
case 4: return "USB Headset";
44+
default: return $"Unknown ({routeId})";
45+
}
46+
}
47+
2548
/// <summary>
2649
/// For Android, the devices represent available audio routing options instead of physical devices.
2750
/// </summary>

Packages/StreamVideo/Runtime/Libs/DeviceManagers/NativeAudioDeviceManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public static void GetAudioInputDevices(ref AudioDeviceInfo[] result)
7676
AndroidAudioDeviceManager.GetAudioInputDevices(ref result);
7777
#else
7878
UnityEngine.Debug.LogWarning($"{nameof(GetAudioInputDevices)} is not supported on this platform: " + UnityEngine.Application.platform);
79+
#endif
80+
}
81+
82+
/// <summary>
83+
/// Gets the current audio route and prints it to Unity console.
84+
/// </summary>
85+
public static void GetAudioRoute()
86+
{
87+
#if UNITY_ANDROID
88+
AndroidAudioDeviceManager.GetAudioRoute();
89+
#else
90+
UnityEngine.Debug.LogWarning($"{nameof(GetAudioRoute)} is not supported on this platform: " + UnityEngine.Application.platform);
7991
#endif
8092
}
8193
}

Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public void UnmuteAndroidAudioPlayback()
424424

425425
public void SetAndroidAudioUsageMode(int usage)
426426
{
427-
NativeMethods.SetAndroidAudioUsageMode(self, usage);
427+
NativeMethods.SetAudioUsageMode(self, usage);
428428
}
429429
#endif
430430

Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ public enum NativeLoggingSeverity
715715
None,
716716
};
717717

718-
#if UNITY_ANDROID && !UNITY_EDITOR
719718
/// <summary>
720719
/// Android audio usage modes for Oboe audio streams.
721720
/// </summary>
@@ -733,7 +732,6 @@ public enum AndroidAudioUsageMode
733732
/// </summary>
734733
VoiceCommunication = 2
735734
}
736-
#endif
737735

738736
/// <summary>
739737
/// Provides utilities and management functions for integrating WebRTC functionality.
@@ -1889,7 +1887,7 @@ public static extern IntPtr CreateVideoRenderer(
18891887
public static extern void GetAudioProcessingModuleConfig(IntPtr context, out bool enabled, out bool echoCancellationEnabled, out bool autoGainEnabled, out bool noiseSuppressionEnabled, out int noiseSuppressionLevel);
18901888

18911889
[DllImport(WebRTC.Lib)]
1892-
public static extern void SetAndroidAudioUsageMode(IntPtr context, int usage);
1890+
public static extern void SetAudioUsageMode(IntPtr context, int usage);
18931891
#endif
18941892

18951893
}

0 commit comments

Comments
 (0)