forked from SteeBono/airplayreceiver
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAirPlayReceiver.cs
More file actions
167 lines (141 loc) · 6.13 KB
/
Copy pathAirPlayReceiver.cs
File metadata and controls
167 lines (141 loc) · 6.13 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using AirPlay.Listeners;
using AirPlay.Models;
using AirPlay.Models.Configs;
using Makaretu.Dns;
using Microsoft.Extensions.Options;
namespace AirPlay
{
public class AirPlayReceiver : IRtspReceiver, IAirPlayReceiver, IDisposable
{
public event EventHandler<decimal> OnSetVolumeReceived;
public event EventHandler<H264Data> OnH264DataReceived;
public event EventHandler<PcmData> OnPCMDataReceived;
public event EventHandler OnAudioFlushReceived;
public event EventHandler OnMirroringStartedReceived;
public event EventHandler OnMirroringStoppedReceived;
public const string AirPlayType = "_airplay._tcp";
public const string AirTunesType = "_raop._tcp";
private MulticastService _mdns = null;
private AirTunesListener _airTunesListener = null;
private readonly string _instance;
private readonly ushort _airTunesPort;
private readonly ushort _airPlayPort;
private readonly string _deviceId;
public AirPlayReceiver(IOptions<AirPlayReceiverConfig> aprConfig, IOptions<DumpConfig> dumpConfig)
{
_airTunesPort = aprConfig?.Value?.AirTunesPort ?? 5000;
_airPlayPort = aprConfig?.Value?.AirPlayPort ?? 7000;
_deviceId = aprConfig?.Value?.DeviceMacAddress ?? "11:22:33:44:55:66";
_instance = aprConfig?.Value?.Instance ?? throw new ArgumentNullException("apr.instance");
var dConfig = dumpConfig?.Value ?? throw new ArgumentNullException(nameof(dumpConfig));
_airTunesListener = new AirTunesListener(this, _airTunesPort, _airPlayPort, dConfig);
}
public async Task StartListeners(CancellationToken cancellationToken)
{
await _airTunesListener.StartAsync(cancellationToken).ConfigureAwait(false);
}
public Task StartMdnsAsync()
{
if (string.IsNullOrWhiteSpace(_deviceId))
{
throw new ArgumentNullException(_deviceId);
}
var rDeviceId = new Regex("^(([0-9a-fA-F][0-9a-fA-F]):){5}([0-9a-fA-F][0-9a-fA-F])$");
var mDeviceId = rDeviceId.Match(_deviceId);
if (!mDeviceId.Success)
{
throw new ArgumentException("Device id must be a mac address", _deviceId);
}
var deviceIdInstance = string.Join(string.Empty, mDeviceId.Groups[2].Captures) + mDeviceId.Groups[3].Value;
_mdns = new MulticastService();
var sd = new ServiceDiscovery(_mdns);
foreach (var ip in MulticastService.GetIPAddresses())
{
Console.WriteLine($"IP address {ip}");
}
_mdns.NetworkInterfaceDiscovered += (s, e) =>
{
foreach (var nic in e.NetworkInterfaces)
{
Console.WriteLine($"NIC '{nic.Name}'");
}
};
// Internally 'ServiceProfile' create the SRV record
var airTunes = new ServiceProfile($"{deviceIdInstance}@{_instance}", AirTunesType, _airTunesPort);
airTunes.AddProperty("ch", "2");
airTunes.AddProperty("cn", "0,1,2,3");
airTunes.AddProperty("et", "0,3,5");
airTunes.AddProperty("md", "0,1,2");
airTunes.AddProperty("sr", "44100");
airTunes.AddProperty("ss", "16");
airTunes.AddProperty("da", "true");
airTunes.AddProperty("sv", "false");
airTunes.AddProperty("ft", "0x5A7FFFF7,0x1E"); // 0x4A7FFFF7, 0xE
airTunes.AddProperty("am", "AppleTV5,3");
airTunes.AddProperty("pk", "29fbb183a58b466e05b9ab667b3c429d18a6b785637333d3f0f3a34baa89f45e");
airTunes.AddProperty("sf", "0x4");
airTunes.AddProperty("tp", "UDP");
airTunes.AddProperty("vn", "65537");
airTunes.AddProperty("vs", "220.68");
airTunes.AddProperty("vv", "2");
/*
* ch 2 audio channels: stereo
* cn 0,1,2,3 audio codecs
* et 0,3,5 supported encryption types
* md 0,1,2 supported metadata types
* pw false does the speaker require a password?
* sr 44100 audio sample rate: 44100 Hz
* ss 16 audio sample size: 16-bit
*/
// Internally 'ServiceProfile' create the SRV record
var airPlay = new ServiceProfile(_instance, AirPlayType, _airPlayPort);
airPlay.AddProperty("deviceid", _deviceId);
airPlay.AddProperty("features", "0x5A7FFFF7,0x1E"); // 0x4A7FFFF7
airPlay.AddProperty("flags", "0x4");
airPlay.AddProperty("model", "AppleTV5,3");
airPlay.AddProperty("pk", "29fbb183a58b466e05b9ab667b3c429d18a6b785637333d3f0f3a34baa89f45e");
airPlay.AddProperty("pi", "aa072a95-0318-4ec3-b042-4992495877d3");
airPlay.AddProperty("srcvers", "220.68");
airPlay.AddProperty("vv", "2");
sd.Advertise(airTunes);
sd.Advertise(airPlay);
_mdns.Start();
return Task.CompletedTask;
}
public void OnSetVolume(decimal volume)
{
OnSetVolumeReceived?.Invoke(this, volume);
}
public void OnData(H264Data data)
{
OnH264DataReceived?.Invoke(this, data);
}
public void OnPCMData(PcmData data)
{
OnPCMDataReceived?.Invoke(this, data);
}
public void OnAudioFlush()
{
OnAudioFlushReceived?.Invoke(this, EventArgs.Empty);
}
public void OnMirroringStarted()
{
OnMirroringStartedReceived?.Invoke(this, EventArgs.Empty);
}
public void OnMirroringStopped()
{
OnMirroringStoppedReceived?.Invoke(this, EventArgs.Empty);
}
public void Dispose()
{
_mdns?.Stop();
}
}
}