Skip to content

Commit 194dace

Browse files
committed
Implement methods to mute audio track locally
1 parent f2f9171 commit 194dace

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

Packages/StreamVideo/Runtime/Core/StatefulModels/Tracks/StreamAudioTrack.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,81 @@ public StreamAudioTrack(AudioStreamTrack track)
1313
{
1414

1515
}
16+
17+
/// <summary>
18+
/// Mutes this audio track locally without affecting other users.
19+
/// </summary>
20+
/// <remarks>
21+
/// `MuteLocally` mutes this audio track on the local device only.
22+
/// Other users in the call will not be affected. This is useful for temporarily
23+
/// stopping playback of incoming audio without disconnecting the track.
24+
/// This method is only available on Android platform.
25+
/// </remarks>
26+
/// <example>
27+
/// <code lang="cs"><![CDATA[
28+
/// audioStreamTrack.MuteLocally();
29+
/// ]]></code>
30+
/// </example>
31+
/// <seealso cref="UnmuteLocally"/>
32+
/// <seealso cref="IsLocallyMuted"/>
33+
public void MuteLocally()
34+
{
35+
#if UNITY_ANDROID && !UNITY_EDITOR
36+
Track.MuteLocally();
37+
#else
38+
throw new NotSupportedException($"The {nameof(MuteLocally)} method is currently only supported on Android platform.");
39+
#endif
40+
}
41+
42+
/// <summary>
43+
/// Unmutes this audio track locally.
44+
/// </summary>
45+
/// <remarks>
46+
/// `UnmuteLocally` unmutes a previously muted audio track on the local device.
47+
/// This method is only available on Android platform.
48+
/// </remarks>
49+
/// <example>
50+
/// <code lang="cs"><![CDATA[
51+
/// audioStreamTrack.UnmuteLocally();
52+
/// ]]></code>
53+
/// </example>
54+
/// <seealso cref="MuteLocally"/>
55+
/// <seealso cref="IsLocallyMuted"/>
56+
public void UnmuteLocally()
57+
{
58+
#if UNITY_ANDROID && !UNITY_EDITOR
59+
Track.UnmuteLocally();
60+
#else
61+
throw new NotSupportedException($"The {nameof(UnmuteLocally)} method is currently only supported on Android platform.");
62+
#endif
63+
}
64+
65+
/// <summary>
66+
/// Checks if this audio track is locally muted.
67+
/// </summary>
68+
/// <remarks>
69+
/// `IsLocallyMuted` returns true if the track is currently muted on the local device.
70+
/// This method is only available on Android platform.
71+
/// </remarks>
72+
/// <returns>True if the track is locally muted, false otherwise.</returns>
73+
/// <example>
74+
/// <code lang="cs"><![CDATA[
75+
/// if (audioStreamTrack.IsLocallyMuted())
76+
/// {
77+
/// Debug.Log("Track is muted locally");
78+
/// }
79+
/// ]]></code>
80+
/// </example>
81+
/// <seealso cref="MuteLocally"/>
82+
/// <seealso cref="UnmuteLocally"/>
83+
public bool IsLocallyMuted()
84+
{
85+
#if UNITY_ANDROID && !UNITY_EDITOR
86+
return Track.IsLocallyMuted();
87+
#else
88+
throw new NotSupportedException($"The {nameof(IsLocallyMuted)} method is currently only supported on Android platform.");
89+
#endif
90+
}
1691

1792
public void SetAudioSourceTarget(AudioSource audioSource)
1893
{

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,80 @@ internal void RemoveSink(AudioStreamRenderer renderer)
290290
GetSelfOrThrow(), renderer.self);
291291
}
292292

293+
#if UNITY_ANDROID && !UNITY_EDITOR
294+
/// <summary>
295+
/// Mutes this audio track locally without affecting other users.
296+
/// </summary>
297+
/// <remarks>
298+
/// `MuteLocally` mutes this audio track on the local device only.
299+
/// Other users in the call will not be affected. This is useful for temporarily
300+
/// stopping playback of incoming audio without disconnecting the track.
301+
/// This method is only available on Android platform.
302+
/// </remarks>
303+
/// <example>
304+
/// <code lang="cs"><![CDATA[
305+
/// audioStreamTrack.MuteLocally();
306+
/// ]]></code>
307+
/// </example>
308+
/// <seealso cref="UnmuteLocally"/>
309+
/// <seealso cref="IsLocallyMuted"/>
310+
public void MuteLocally()
311+
{
312+
if (_streamRenderer == null)
313+
throw new InvalidOperationException("MuteLocally is only available for receiver side audio tracks.");
314+
315+
NativeMethods.AudioTrackSinkMute(_streamRenderer.self);
316+
}
317+
318+
/// <summary>
319+
/// Unmutes this audio track locally.
320+
/// </summary>
321+
/// <remarks>
322+
/// `UnmuteLocally` unmutes a previously muted audio track on the local device.
323+
/// This method is only available on Android platform.
324+
/// </remarks>
325+
/// <example>
326+
/// <code lang="cs"><![CDATA[
327+
/// audioStreamTrack.UnmuteLocally();
328+
/// ]]></code>
329+
/// </example>
330+
/// <seealso cref="MuteLocally"/>
331+
/// <seealso cref="IsLocallyMuted"/>
332+
public void UnmuteLocally()
333+
{
334+
if (_streamRenderer == null)
335+
throw new InvalidOperationException("UnmuteLocally is only available for receiver side audio tracks.");
336+
337+
NativeMethods.AudioTrackSinkUnmute(_streamRenderer.self);
338+
}
339+
340+
/// <summary>
341+
/// Checks if this audio track is locally muted.
342+
/// </summary>
343+
/// <remarks>
344+
/// `IsLocallyMuted` returns true if the track is currently muted on the local device.
345+
/// This method is only available on Android platform.
346+
/// </remarks>
347+
/// <returns>True if the track is locally muted, false otherwise.</returns>
348+
/// <example>
349+
/// <code lang="cs"><![CDATA[
350+
/// if (audioStreamTrack.IsLocallyMuted())
351+
/// {
352+
/// Debug.Log("Track is muted locally");
353+
/// }
354+
/// ]]></code>
355+
/// </example>
356+
/// <seealso cref="MuteLocally"/>
357+
/// <seealso cref="UnmuteLocally"/>
358+
public bool IsLocallyMuted()
359+
{
360+
if (_streamRenderer == null)
361+
throw new InvalidOperationException("IsLocallyMuted is only available for receiver side audio tracks.");
362+
363+
return NativeMethods.AudioTrackSinkIsMuted(_streamRenderer.self);
364+
}
365+
#endif
366+
293367
/// <summary>
294368
/// Disposes of AudioStreamTrack.
295369
/// </summary>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,15 @@ public static extern void RegisterDebugLog(DelegateDebugLog func, [MarshalAs(Unm
17361736
[DllImport(WebRTC.Lib)]
17371737
public static extern void AudioTrackSinkProcessAudio(
17381738
IntPtr sink, float[] data, int length, int channels, int sampleRate);
1739+
#if UNITY_ANDROID && !UNITY_EDITOR
1740+
[DllImport(WebRTC.Lib)]
1741+
public static extern void AudioTrackSinkMute(IntPtr sink);
1742+
[DllImport(WebRTC.Lib)]
1743+
public static extern void AudioTrackSinkUnmute(IntPtr sink);
1744+
[DllImport(WebRTC.Lib)]
1745+
[return: MarshalAs(UnmanagedType.U1)]
1746+
public static extern bool AudioTrackSinkIsMuted(IntPtr sink);
1747+
#endif
17391748
[DllImport(WebRTC.Lib)]
17401749
[return: MarshalAs(UnmanagedType.U1)]
17411750
public static extern bool MediaStreamAddTrack(IntPtr stream, IntPtr track);

0 commit comments

Comments
 (0)