forked from dk307/HSPI_ChromecastSpeak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleChromecast.cs
201 lines (169 loc) · 8.82 KB
/
SimpleChromecast.cs
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
using NullGuard;
using SharpCaster;
using SharpCaster.Exceptions;
using SharpCaster.Models.ChromecastStatus;
using SharpCaster.Models.MediaStatus;
using SharpCaster.Models.Metadata;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Hspi.Chromecast
{
using static System.FormattableString;
/// <summary>
/// Class to play Url on Chromecast Device
/// </summary>
[NullGuard(ValidationFlags.Arguments | ValidationFlags.NonPublic)]
internal class SimpleChromecast
{
public SimpleChromecast(ILogger logger, ChromecastDevice device, Uri playUri, TimeSpan? duration, double? volume)
{
this.volume = volume;
this.duration = duration;
this.playUri = playUri;
this.device = device;
this.logger = logger;
}
public async Task Play(CancellationToken cancellationToken)
{
if (!Uri.TryCreate(Invariant($"https://{device.DeviceIP}/"), UriKind.Absolute, out Uri deviceUri))
{
throw new ChromecastException(Invariant($"Failed to create Uri for Chromecast {device.Name} on {device.DeviceIP}"));
}
using (var client = new ChromeCastClient(deviceUri))
{
await Connect(client, cancellationToken).ConfigureAwait(false);
var status = await LaunchApplication(client, cancellationToken).ConfigureAwait(false);
bool resetVolumeBack = false;
var currentVolume = status?.Volume;
if (volume.HasValue)
{
double chromecastVolume = volume.Value / 100;
if (currentVolume != null && ((currentVolume.Level != chromecastVolume) || currentVolume.Muted))
{
Trace.WriteLine(Invariant($"Setting Volume on Chromecast {device.Name} to {volume.Value}"));
status = await client.ReceiverChannel.SetVolume(chromecastVolume, false, cancellationToken).ConfigureAwait(false);
Trace.WriteLine(Invariant($"Finished Setting Volume on Chromecast {device.Name} to {volume.Value}"));
resetVolumeBack = true;
}
}
var defaultApplication = GetDefaultApplication(status);
client.MediaChannel.MessageReceived += MediaChannel_MessageReceived;
loadedMediaStatus = await LoadMedia(client, defaultApplication, cancellationToken).ConfigureAwait(false);
if (loadedMediaStatus == null)
{
throw new MediaLoadException(device.DeviceIP.ToString(), "Failed to load");
}
await playbackFinished.Task.WaitOnRequestCompletion(cancellationToken).ConfigureAwait(false);
client.MediaChannel.MessageReceived -= MediaChannel_MessageReceived;
// Restore the existing volume
if (resetVolumeBack)
{
Trace.WriteLine(Invariant($"Restoring Volume on Chromecast {device.Name}"));
await client.ReceiverChannel.SetVolume(currentVolume.Level, currentVolume.Muted, cancellationToken)
.ConfigureAwait(false);
Trace.WriteLine(Invariant($"Finished Restoring Volume on Chromecast {device.Name}"));
}
this.logger.LogInfo(Invariant($"Played Speech on Chromecast {device.Name}"));
Trace.WriteLine(Invariant($"Disconnecting Chromecast {device.Name}"));
await client.Disconnect(cancellationToken).ConfigureAwait(false);
Trace.WriteLine(Invariant($"Disconnected Chromecast {device.Name}"));
}
}
private void MediaChannel_MessageReceived(object sender, [AllowNull]MediaStatus mediaStatus)
{
if (loadedMediaStatus != null)
{
if (mediaStatus != null)
{
if ((mediaStatus.PlayerState == PlayerState.Idle) &&
(mediaStatus.MediaSessionId == loadedMediaStatus.MediaSessionId))
{
switch (mediaStatus.IdleReason)
{
case IdleReason.CANCELLED:
case IdleReason.ERROR:
case IdleReason.INTERRUPTED:
playbackFinished.SetException(new MediaLoadException(device.DeviceIP.ToString(),
mediaStatus.IdleReason.ToString()));
break;
case IdleReason.FINISHED:
playbackFinished.SetResult(true);
break;
}
}
}
}
}
private async Task<MediaStatus> LoadMedia(ChromeCastClient client, ChromecastApplication defaultApplication,
CancellationToken cancellationToken)
{
Trace.WriteLine(Invariant($"Loading Media [{playUri}] in on Chromecast {device.Name}"));
var metadata = new GenericMediaMetadata()
{
title = "Homeseer",
metadataType = SharpCaster.Models.Enums.MetadataType.GENERIC,
};
var mediaStatus = await client.MediaChannel.LoadMedia(defaultApplication, playUri, null, cancellationToken,
metadata: metadata,
duration: duration.HasValue ? duration.Value.TotalSeconds : 0D).ConfigureAwait(false);
Trace.WriteLine(Invariant($"Loaded Media [{playUri}] in on Chromecast {device.Name}"));
return mediaStatus;
}
private async Task<ChromecastStatus> LaunchApplication(ChromeCastClient client, CancellationToken cancellationToken)
{
Trace.WriteLine(Invariant($"Launching default app on Chromecast {device.Name}"));
ChromecastStatus status = await client.ReceiverChannel.GetChromecastStatus(cancellationToken).ConfigureAwait(false);
var defaultApplication = GetDefaultApplication(status);
//if (defaultApplication == null)
//{
status = await client.ReceiverChannel.LaunchApplication(defaultAppId, cancellationToken).ConfigureAwait(false);
defaultApplication = GetDefaultApplication(status);
//}
//else
//{
// Trace.WriteLine(Invariant($"Default app is already running on Chromecast {device.Name}"));
//}
if (defaultApplication == null)
{
throw new ChromecastDeviceException(device.Name, "No default app found inspite of launching it");
}
await client.ConnectionChannel.ConnectWithDestination(defaultApplication.TransportId, cancellationToken)
.ConfigureAwait(false);
Trace.WriteLine(Invariant($"Launched default app on Chromecast {device.Name}"));
return status;
}
private static ChromecastApplication GetDefaultApplication(ChromecastStatus status)
{
return status?.Applications?.FirstOrDefault((app) => { return app.AppId == defaultAppId; });
}
private async Task Connect(ChromeCastClient client, CancellationToken cancellationToken)
{
Trace.WriteLine(Invariant($"Connecting to Chromecast {device.Name} on {device.DeviceIP}"));
connectedSource = new TaskCompletionSource<bool>(cancellationToken);
client.ConnectedChanged += Client_ConnectedChanged;
await client.ConnectChromecast(cancellationToken).ConfigureAwait(false);
await connectedSource.Task.WaitOnRequestCompletion(cancellationToken).ConfigureAwait(false);
client.ConnectedChanged -= Client_ConnectedChanged;
Trace.WriteLine(Invariant($"Connected to Chromecast {device.Name} on {device.DeviceIP}"));
}
private void Client_ConnectedChanged(object sender, bool connected)
{
if (connected)
{
connectedSource.SetResult(true);
}
}
private TaskCompletionSource<bool> playbackFinished = new TaskCompletionSource<bool>();
private MediaStatus loadedMediaStatus;
private const string defaultAppId = "CC1AD845";
private readonly ILogger logger;
private readonly ChromecastDevice device;
private readonly Uri playUri;
private readonly TimeSpan? duration;
private readonly double? volume;
private TaskCompletionSource<bool> connectedSource;
};
}