forked from SteeBono/airplayreceiver
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAirPlayService.cs
More file actions
225 lines (197 loc) · 7.4 KB
/
Copy pathAirPlayService.cs
File metadata and controls
225 lines (197 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using AirPlay.Models.Configs;
using AirPlay.Services;
using AirPlay.Utils;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace AirPlay
{
public class AirPlayService : IHostedService, IDisposable
{
private readonly IAirPlayReceiver _airPlayReceiver;
private readonly DumpConfig _dConfig;
private AudioOutputService _audioOutput;
private VideoOutputService _videoOutput;
private List<byte> _audiobuf;
private readonly object _audioOutputLock = new object();
private readonly object _videoOutputLock = new object();
public AirPlayService(IAirPlayReceiver airPlayReceiver, IOptions<DumpConfig> dConfig)
{
_airPlayReceiver = airPlayReceiver ?? throw new ArgumentNullException(nameof(airPlayReceiver));
_dConfig = dConfig?.Value ?? throw new ArgumentNullException(nameof(dConfig));
}
private void RecreateAudioOutput()
{
lock (_audioOutputLock)
{
Console.WriteLine("Recreating audio output after unexpected stop...");
try
{
_audioOutput?.Dispose();
Thread.Sleep(200);
_audioOutput = new AudioOutputService();
_audioOutput.Initialize();
_audioOutput.PlaybackStoppedUnexpectedly += (s, e) => RecreateAudioOutput();
Console.WriteLine("Audio output recreated successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to recreate audio output: {ex.Message}");
}
}
}
public async Task StartAsync(CancellationToken cancellationToken)
{
#if DUMP
var bPath = _dConfig.Path;
var fPath = Path.Combine(bPath, "frames/");
var oPath = Path.Combine(bPath, "out/");
var pPath = Path.Combine(bPath, "pcm/");
if (!Directory.Exists(bPath))
{
Directory.CreateDirectory(bPath);
}
if (!Directory.Exists(fPath))
{
Directory.CreateDirectory(fPath);
}
if (!Directory.Exists(oPath))
{
Directory.CreateDirectory(oPath);
}
if (!Directory.Exists(pPath))
{
Directory.CreateDirectory(pPath);
}
#endif
// Initialize audio output on Windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
try
{
_audioOutput = new AudioOutputService();
_audioOutput.Initialize();
_audioOutput.PlaybackStoppedUnexpectedly += (s, e) => RecreateAudioOutput();
Console.WriteLine("Audio output initialized successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to initialize audio output: {ex.Message}");
Console.WriteLine("Continuing without audio output...");
}
}
// Initialize video output service (will be started/stopped per mirroring session)
_videoOutput = new VideoOutputService();
Console.WriteLine("Video output service ready (will auto-launch ffplay when mirroring starts)");
await _airPlayReceiver.StartListeners(cancellationToken);
await _airPlayReceiver.StartMdnsAsync().ConfigureAwait(false);
_airPlayReceiver.OnSetVolumeReceived += (s, e) =>
{
lock (_audioOutputLock)
{
_audioOutput?.SetVolume(e);
}
};
_airPlayReceiver.OnAudioFlushReceived += (s, e) =>
{
Console.WriteLine("Audio flush received - restarting audio output for new track");
lock (_audioOutputLock)
{
_audioOutput?.HandleFlush();
}
};
_airPlayReceiver.OnMirroringStartedReceived += (s, e) =>
{
Console.WriteLine("Mirroring started - launching video player...");
lock (_videoOutputLock)
{
_videoOutput?.StartMirroring();
}
};
_airPlayReceiver.OnMirroringStoppedReceived += (s, e) =>
{
Console.WriteLine("Mirroring stopped - closing video player...");
lock (_videoOutputLock)
{
_videoOutput?.StopMirroring();
}
};
// H264 VIDEO OUTPUT
_airPlayReceiver.OnH264DataReceived += (s, e) =>
{
// Send H264 data to video output pipe
lock (_videoOutputLock)
{
_videoOutput?.WriteFrame(e);
}
#if DUMP
using (FileStream writer = new FileStream($"{bPath}dump.h264", FileMode.Append))
{
writer.Write(e.Data, 0, e.Length);
}
#endif
};
_audiobuf = new List<byte>();
int pcmReceivedCount = 0;
_airPlayReceiver.OnPCMDataReceived += (s, e) =>
{
pcmReceivedCount++;
if (pcmReceivedCount <= 5 || pcmReceivedCount % 500 == 0)
{
bool allZeros = true;
int checkLen = Math.Min(e.Length, 100);
for (int i = 0; i < checkLen; i++)
{
if (e.Data[i] != 0) { allZeros = false; break; }
}
Console.WriteLine($"[DEBUG-PCM] OnPCMDataReceived #{pcmReceivedCount}: len={e.Length}, allZeros={allZeros}");
}
// Play audio through speakers
lock (_audioOutputLock)
{
_audioOutput?.AddSamples(e.Data, 0, e.Length);
}
#if DUMP
_audiobuf.AddRange(e.Data);
#endif
};
}
public Task StopAsync(CancellationToken cancellationToken)
{
_audioOutput?.Dispose();
_audioOutput = null;
_videoOutput?.Dispose();
_videoOutput = null;
#if DUMP
// DUMP WAV AUDIO
var bPath = _dConfig.Path;
using (var wr = new FileStream($"{bPath}dequeued.wav", FileMode.Create))
{
var header = Utilities.WriteWavHeader(2, 44100, 16, (uint)_audiobuf.Count);
wr.Write(header, 0, header.Length);
}
using (FileStream writer = new FileStream($"{bPath}dequeued.wav", FileMode.Append))
{
writer.Write(_audiobuf.ToArray(), 0, _audiobuf.Count);
}
#endif
return Task.CompletedTask;
}
public void Dispose()
{
_audioOutput?.Dispose();
_audioOutput = null;
_videoOutput?.Dispose();
_videoOutput = null;
if (_airPlayReceiver is IDisposable disposable)
{
disposable.Dispose();
}
}
}
}