|
| 1 | +using System.IO.Pipes; |
| 2 | +using HomeSpeaker.Server; |
| 3 | +using System.Diagnostics; |
| 4 | + |
| 5 | +namespace HomeSpeaker.Server2.Services; |
| 6 | + |
| 7 | +public class AirPlayReceiverService : BackgroundService |
| 8 | +{ |
| 9 | + private readonly ILogger<AirPlayReceiverService> logger; |
| 10 | + private readonly IMusicPlayer musicPlayer; private const string MetadataPipePath = "/tmp/airplay-shared/metadata"; |
| 11 | + private const string AirPlayStatePath = "/tmp/airplay-shared/state"; |
| 12 | + private const string AirPlayLogPath = "/tmp/airplay-shared/log"; |
| 13 | + private bool airplayActive = false; |
| 14 | + |
| 15 | + public AirPlayReceiverService(ILogger<AirPlayReceiverService> logger, IMusicPlayer musicPlayer) |
| 16 | + { |
| 17 | + this.logger = logger; |
| 18 | + this.musicPlayer = musicPlayer; |
| 19 | + } |
| 20 | + |
| 21 | + protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 22 | + { |
| 23 | + logger.LogInformation("AirPlay Receiver Service starting..."); |
| 24 | + |
| 25 | + // Monitor for AirPlay session events via shared state file |
| 26 | + _ = Task.Run(() => MonitorAirPlayEvents(stoppingToken), stoppingToken); |
| 27 | + |
| 28 | + // Monitor for metadata (song info, artwork, etc.) |
| 29 | + _ = Task.Run(() => MonitorAirPlayMetadata(stoppingToken), stoppingToken); |
| 30 | + |
| 31 | + // Keep service running |
| 32 | + await Task.Delay(Timeout.Infinite, stoppingToken); |
| 33 | + } |
| 34 | + |
| 35 | + private async Task MonitorAirPlayEvents(CancellationToken cancellationToken) |
| 36 | + { |
| 37 | + // Monitor the shared state file written by ShairportSync scripts |
| 38 | + while (!cancellationToken.IsCancellationRequested) |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + if (File.Exists(AirPlayStatePath)) |
| 43 | + { |
| 44 | + var stateContent = await File.ReadAllTextAsync(AirPlayStatePath, cancellationToken); |
| 45 | + bool currentlyActive = stateContent.Trim().Equals("ACTIVE", StringComparison.OrdinalIgnoreCase); |
| 46 | + |
| 47 | + if (currentlyActive && !airplayActive) |
| 48 | + { |
| 49 | + logger.LogInformation("AirPlay session started - pausing local playback"); |
| 50 | + musicPlayer.Stop(); // Pause local music when AirPlay starts |
| 51 | + airplayActive = true; |
| 52 | + } |
| 53 | + else if (!currentlyActive && airplayActive) |
| 54 | + { |
| 55 | + logger.LogInformation("AirPlay session ended - can resume local playback"); |
| 56 | + airplayActive = false; |
| 57 | + // Optionally auto-resume: musicPlayer.ResumePlay(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + await Task.Delay(1000, cancellationToken); |
| 62 | + } |
| 63 | + catch (Exception ex) |
| 64 | + { |
| 65 | + logger.LogError(ex, "Error monitoring AirPlay state file"); |
| 66 | + await Task.Delay(5000, cancellationToken); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private async Task MonitorAirPlayMetadata(CancellationToken cancellationToken) |
| 72 | + { |
| 73 | + while (!cancellationToken.IsCancellationRequested) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + if (File.Exists(MetadataPipePath)) |
| 78 | + { |
| 79 | + using var reader = new StreamReader(MetadataPipePath); |
| 80 | + var metadata = await reader.ReadToEndAsync(); |
| 81 | + |
| 82 | + if (!string.IsNullOrEmpty(metadata)) |
| 83 | + { |
| 84 | + logger.LogInformation("AirPlay metadata: {metadata}", metadata); |
| 85 | + // Parse metadata and update UI if needed |
| 86 | + // Could send events through your existing SendEvent mechanism |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + await Task.Delay(500, cancellationToken); |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + logger.LogError(ex, "Error reading AirPlay metadata"); |
| 95 | + await Task.Delay(2000, cancellationToken); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments