Add system mute control via setMute action
Introduce a setMute protocol action and an NAudio-backed AudioControlService so the Android client can toggle the Windows render endpoint mute state over WebSocket. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -46,7 +46,7 @@ public sealed class SyncMessageTests
|
|||||||
public void ReplaceAllAppendsSubmittedText()
|
public void ReplaceAllAppendsSubmittedText()
|
||||||
{
|
{
|
||||||
var keyboard = new RecordingKeyboardInjectionService();
|
var keyboard = new RecordingKeyboardInjectionService();
|
||||||
var handler = new SyncMessageHandler(keyboard);
|
var handler = new SyncMessageHandler(keyboard, new RecordingAudioControlService());
|
||||||
|
|
||||||
handler.Handle(new SyncMessage
|
handler.Handle(new SyncMessage
|
||||||
{
|
{
|
||||||
@@ -59,6 +59,33 @@ public sealed class SyncMessageTests
|
|||||||
keyboard.Calls);
|
keyboard.Calls);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void DeserializeAndroidSetMuteMessage()
|
||||||
|
{
|
||||||
|
var message = JsonSerializer.Deserialize<SyncMessage>(
|
||||||
|
"""{"action":"setMute","muted":true}""",
|
||||||
|
new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
||||||
|
|
||||||
|
Assert.IsNotNull(message);
|
||||||
|
Assert.AreEqual(SyncAction.SetMute, message.Action);
|
||||||
|
Assert.AreEqual(true, message.Muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void SetMuteForwardsToAudioService()
|
||||||
|
{
|
||||||
|
var audio = new RecordingAudioControlService();
|
||||||
|
var handler = new SyncMessageHandler(new RecordingKeyboardInjectionService(), audio);
|
||||||
|
|
||||||
|
handler.Handle(new SyncMessage
|
||||||
|
{
|
||||||
|
Action = SyncAction.SetMute,
|
||||||
|
Muted = true
|
||||||
|
});
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(new[] { "mute:True" }, audio.Calls);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void DiscoveryResponseContainsServiceEndpoint()
|
public void DiscoveryResponseContainsServiceEndpoint()
|
||||||
{
|
{
|
||||||
@@ -77,7 +104,7 @@ public sealed class SyncMessageTests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void WebSocketServiceInfoContainsDeviceName()
|
public void WebSocketServiceInfoContainsDeviceName()
|
||||||
{
|
{
|
||||||
using var server = new WebSocketServerService(new SyncMessageHandler(new RecordingKeyboardInjectionService()), "Desktop-WIN11");
|
using var server = new WebSocketServerService(new SyncMessageHandler(new RecordingKeyboardInjectionService(), new RecordingAudioControlService()), "Desktop-WIN11");
|
||||||
|
|
||||||
using var document = JsonDocument.Parse(server.BuildServiceInfoJson());
|
using var document = JsonDocument.Parse(server.BuildServiceInfoJson());
|
||||||
var root = document.RootElement;
|
var root = document.RootElement;
|
||||||
@@ -112,4 +139,16 @@ public sealed class SyncMessageTests
|
|||||||
Calls.Add("enter");
|
Calls.Add("enter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sealed class RecordingAudioControlService : IAudioControlService
|
||||||
|
{
|
||||||
|
public List<string> Calls { get; } = [];
|
||||||
|
|
||||||
|
public bool IsMuted => Calls.Count > 0 && Calls[^1] == "mute:True";
|
||||||
|
|
||||||
|
public void SetMute(bool muted)
|
||||||
|
{
|
||||||
|
Calls.Add($"mute:{muted}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ public enum SyncAction
|
|||||||
ReplaceAll,
|
ReplaceAll,
|
||||||
Backspace,
|
Backspace,
|
||||||
Enter,
|
Enter,
|
||||||
Ping
|
Ping,
|
||||||
|
SetMute
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public sealed class SyncActionJsonConverter : JsonConverter<SyncAction>
|
|||||||
"backspace" => SyncAction.Backspace,
|
"backspace" => SyncAction.Backspace,
|
||||||
"enter" => SyncAction.Enter,
|
"enter" => SyncAction.Enter,
|
||||||
"ping" => SyncAction.Ping,
|
"ping" => SyncAction.Ping,
|
||||||
|
"setMute" => SyncAction.SetMute,
|
||||||
_ => throw new JsonException($"Unsupported sync action: {value}")
|
_ => throw new JsonException($"Unsupported sync action: {value}")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -35,6 +36,7 @@ public sealed class SyncActionJsonConverter : JsonConverter<SyncAction>
|
|||||||
SyncAction.Backspace => "backspace",
|
SyncAction.Backspace => "backspace",
|
||||||
SyncAction.Enter => "enter",
|
SyncAction.Enter => "enter",
|
||||||
SyncAction.Ping => "ping",
|
SyncAction.Ping => "ping",
|
||||||
|
SyncAction.SetMute => "setMute",
|
||||||
_ => throw new JsonException($"Unsupported sync action: {value}")
|
_ => throw new JsonException($"Unsupported sync action: {value}")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,4 +9,7 @@ public sealed record SyncMessage
|
|||||||
|
|
||||||
[JsonPropertyName("text")]
|
[JsonPropertyName("text")]
|
||||||
public string? Text { get; init; }
|
public string? Text { get; init; }
|
||||||
|
|
||||||
|
[JsonPropertyName("muted")]
|
||||||
|
public bool? Muted { get; init; }
|
||||||
}
|
}
|
||||||
|
|||||||
52
WirelessTextSyncer.Windows/Services/AudioControlService.cs
Normal file
52
WirelessTextSyncer.Windows/Services/AudioControlService.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using NAudio.CoreAudioApi;
|
||||||
|
|
||||||
|
namespace WirelessTextSyncer.Windows.Services;
|
||||||
|
|
||||||
|
public sealed class AudioControlService : IAudioControlService, IDisposable
|
||||||
|
{
|
||||||
|
private readonly MMDeviceEnumerator enumerator = new();
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public bool IsMuted
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
|
||||||
|
return device.AudioEndpointVolume.Mute;
|
||||||
|
}
|
||||||
|
catch (COMException exception)
|
||||||
|
{
|
||||||
|
AppLogger.Error("Failed to read audio mute state.", exception);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetMute(bool muted)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
|
||||||
|
device.AudioEndpointVolume.Mute = muted;
|
||||||
|
AppLogger.Info($"Audio mute set to {muted}.");
|
||||||
|
}
|
||||||
|
catch (COMException exception)
|
||||||
|
{
|
||||||
|
AppLogger.Error($"Failed to set audio mute to {muted}.", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
|
enumerator.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace WirelessTextSyncer.Windows.Services;
|
||||||
|
|
||||||
|
public interface IAudioControlService
|
||||||
|
{
|
||||||
|
bool IsMuted { get; }
|
||||||
|
|
||||||
|
void SetMute(bool muted);
|
||||||
|
}
|
||||||
@@ -5,11 +5,13 @@ namespace WirelessTextSyncer.Windows.Services;
|
|||||||
public sealed class SyncMessageHandler
|
public sealed class SyncMessageHandler
|
||||||
{
|
{
|
||||||
private readonly IKeyboardInjectionService keyboard;
|
private readonly IKeyboardInjectionService keyboard;
|
||||||
|
private readonly IAudioControlService audio;
|
||||||
private string remoteText = string.Empty;
|
private string remoteText = string.Empty;
|
||||||
|
|
||||||
public SyncMessageHandler(IKeyboardInjectionService keyboard)
|
public SyncMessageHandler(IKeyboardInjectionService keyboard, IAudioControlService audio)
|
||||||
{
|
{
|
||||||
this.keyboard = keyboard;
|
this.keyboard = keyboard;
|
||||||
|
this.audio = audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(SyncMessage message)
|
public void Handle(SyncMessage message)
|
||||||
@@ -38,11 +40,21 @@ public sealed class SyncMessageHandler
|
|||||||
break;
|
break;
|
||||||
case SyncAction.Ping:
|
case SyncAction.Ping:
|
||||||
break;
|
break;
|
||||||
|
case SyncAction.SetMute:
|
||||||
|
HandleSetMute(message);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
|
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void HandleSetMute(SyncMessage message)
|
||||||
|
{
|
||||||
|
var muted = message.Muted ?? false;
|
||||||
|
AppLogger.Info($"SetMute received muted={muted}.");
|
||||||
|
audio.SetMute(muted);
|
||||||
|
}
|
||||||
|
|
||||||
private void AppendSubmittedText(string text)
|
private void AppendSubmittedText(string text)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(text))
|
if (string.IsNullOrEmpty(text))
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace WirelessTextSyncer.Windows.Tray;
|
|||||||
public sealed class TrayApplicationContext : ApplicationContext
|
public sealed class TrayApplicationContext : ApplicationContext
|
||||||
{
|
{
|
||||||
private readonly KeyboardInjectionService keyboard;
|
private readonly KeyboardInjectionService keyboard;
|
||||||
|
private readonly AudioControlService audio;
|
||||||
private readonly WebSocketServerService server;
|
private readonly WebSocketServerService server;
|
||||||
private readonly DiscoveryResponderService discovery;
|
private readonly DiscoveryResponderService discovery;
|
||||||
private readonly NotifyIcon notifyIcon;
|
private readonly NotifyIcon notifyIcon;
|
||||||
@@ -21,7 +22,8 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
AppLogger.Info("Tray application starting.");
|
AppLogger.Info("Tray application starting.");
|
||||||
uiContext = SynchronizationContext.Current ?? new WindowsFormsSynchronizationContext();
|
uiContext = SynchronizationContext.Current ?? new WindowsFormsSynchronizationContext();
|
||||||
keyboard = new KeyboardInjectionService();
|
keyboard = new KeyboardInjectionService();
|
||||||
var handler = new SyncMessageHandler(keyboard);
|
audio = new AudioControlService();
|
||||||
|
var handler = new SyncMessageHandler(keyboard, audio);
|
||||||
|
|
||||||
server = new WebSocketServerService(handler);
|
server = new WebSocketServerService(handler);
|
||||||
server.StatusChanged += (_, _) => uiContext.Post(_ => UpdateTrayStatus(), null);
|
server.StatusChanged += (_, _) => uiContext.Post(_ => UpdateTrayStatus(), null);
|
||||||
@@ -62,6 +64,7 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
connectedIcon.Dispose();
|
connectedIcon.Dispose();
|
||||||
waitIcon.Dispose();
|
waitIcon.Dispose();
|
||||||
discovery.Dispose();
|
discovery.Dispose();
|
||||||
|
audio.Dispose();
|
||||||
server.Dispose();
|
server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Fleck" Version="1.2.0" />
|
<PackageReference Include="Fleck" Version="1.2.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
|
||||||
|
<PackageReference Include="NAudio" Version="2.2.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user