2020-10-21 18:05:20 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-10-22 17:37:19 +08:00
|
|
|
using System.Diagnostics;
|
2020-10-21 18:05:20 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
2020-10-22 12:41:54 +08:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-10-22 16:29:43 +08:00
|
|
|
using Newtonsoft.Json;
|
2020-10-22 12:41:54 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
2020-10-22 13:54:27 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2020-10-22 12:41:54 +08:00
|
|
|
using osu.Game.Online.API;
|
2020-10-22 18:17:19 +08:00
|
|
|
using osu.Game.Replays.Legacy;
|
2020-10-22 16:29:38 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2020-10-22 13:54:27 +08:00
|
|
|
using osu.Game.Rulesets.Replays;
|
|
|
|
using osu.Game.Rulesets.Replays.Types;
|
2020-10-21 18:05:20 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Spectator
|
|
|
|
{
|
2020-10-22 12:41:54 +08:00
|
|
|
public class SpectatorStreamingClient : Component, ISpectatorClient
|
2020-10-21 18:05:20 +08:00
|
|
|
{
|
2020-10-22 12:41:54 +08:00
|
|
|
private HubConnection connection;
|
2020-10-21 18:05:20 +08:00
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
private readonly List<int> watchingUsers = new List<int>();
|
|
|
|
|
|
|
|
public IBindableList<int> PlayingUsers => playingUsers;
|
|
|
|
|
|
|
|
private readonly BindableList<int> playingUsers = new BindableList<int>();
|
2020-10-21 18:05:20 +08:00
|
|
|
|
2020-10-22 12:41:54 +08:00
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
private bool isConnected;
|
|
|
|
|
2020-10-22 12:41:54 +08:00
|
|
|
[Resolved]
|
2020-10-22 14:03:43 +08:00
|
|
|
private IAPIProvider api { get; set; }
|
2020-10-22 12:41:54 +08:00
|
|
|
|
2020-10-22 13:54:27 +08:00
|
|
|
[Resolved]
|
|
|
|
private IBindable<WorkingBeatmap> beatmap { get; set; }
|
|
|
|
|
2020-10-22 16:29:38 +08:00
|
|
|
[Resolved]
|
|
|
|
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
|
|
|
|
|
|
|
|
private readonly SpectatorState currentState = new SpectatorState();
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
private bool isPlaying;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called whenever new frames arrive from the server.
|
|
|
|
/// </summary>
|
|
|
|
public event Action<int, FrameDataBundle> OnNewFrames;
|
2020-10-22 17:10:27 +08:00
|
|
|
|
2020-10-22 12:41:54 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2020-10-21 18:05:20 +08:00
|
|
|
{
|
2020-10-22 12:41:54 +08:00
|
|
|
apiState.BindTo(api.State);
|
|
|
|
apiState.BindValueChanged(apiStateChanged, true);
|
|
|
|
}
|
2020-10-21 18:05:20 +08:00
|
|
|
|
2020-10-22 12:41:54 +08:00
|
|
|
private void apiStateChanged(ValueChangedEvent<APIState> state)
|
|
|
|
{
|
|
|
|
switch (state.NewValue)
|
|
|
|
{
|
|
|
|
case APIState.Failing:
|
|
|
|
case APIState.Offline:
|
|
|
|
connection?.StopAsync();
|
|
|
|
connection = null;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case APIState.Online:
|
2020-10-22 14:27:04 +08:00
|
|
|
Task.Run(connect);
|
2020-10-22 12:41:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
private const string endpoint = "http://localhost:5009/spectator";
|
|
|
|
#else
|
|
|
|
private const string endpoint = "https://spectator.ppy.sh/spectator";
|
|
|
|
#endif
|
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
private async Task connect()
|
2020-10-22 12:41:54 +08:00
|
|
|
{
|
2020-10-22 14:27:04 +08:00
|
|
|
if (connection != null)
|
|
|
|
return;
|
|
|
|
|
2020-10-22 12:41:54 +08:00
|
|
|
connection = new HubConnectionBuilder()
|
|
|
|
.WithUrl(endpoint, options =>
|
|
|
|
{
|
|
|
|
options.Headers.Add("Authorization", $"Bearer {api.AccessToken}");
|
|
|
|
})
|
2020-10-22 16:29:43 +08:00
|
|
|
.AddNewtonsoftJsonProtocol(options => { options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; })
|
2020-10-22 12:41:54 +08:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
// until strong typed client support is added, each method must be manually bound (see https://github.com/dotnet/aspnetcore/issues/15198)
|
2020-10-22 17:37:19 +08:00
|
|
|
connection.On<int, SpectatorState>(nameof(ISpectatorClient.UserBeganPlaying), ((ISpectatorClient)this).UserBeganPlaying);
|
|
|
|
connection.On<int, FrameDataBundle>(nameof(ISpectatorClient.UserSentFrames), ((ISpectatorClient)this).UserSentFrames);
|
|
|
|
connection.On<int, SpectatorState>(nameof(ISpectatorClient.UserFinishedPlaying), ((ISpectatorClient)this).UserFinishedPlaying);
|
2020-10-22 12:41:54 +08:00
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
connection.Closed += async ex =>
|
|
|
|
{
|
|
|
|
isConnected = false;
|
2020-10-22 17:37:19 +08:00
|
|
|
playingUsers.Clear();
|
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
if (ex != null) await tryUntilConnected();
|
|
|
|
};
|
|
|
|
|
|
|
|
await tryUntilConnected();
|
|
|
|
|
|
|
|
async Task tryUntilConnected()
|
|
|
|
{
|
|
|
|
while (api.State.Value == APIState.Online)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// reconnect on any failure
|
|
|
|
await connection.StartAsync();
|
|
|
|
|
|
|
|
// success
|
|
|
|
isConnected = true;
|
2020-10-22 17:37:19 +08:00
|
|
|
|
|
|
|
// resubscribe to watched users
|
|
|
|
var users = watchingUsers.ToArray();
|
|
|
|
watchingUsers.Clear();
|
|
|
|
foreach (var userId in users)
|
|
|
|
WatchUser(userId);
|
|
|
|
|
|
|
|
// re-send state in case it wasn't received
|
|
|
|
if (isPlaying)
|
|
|
|
beginPlaying();
|
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
await Task.Delay(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 18:05:20 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
Task ISpectatorClient.UserBeganPlaying(int userId, SpectatorState state)
|
2020-10-21 18:05:20 +08:00
|
|
|
{
|
2020-10-22 17:37:19 +08:00
|
|
|
if (!playingUsers.Contains(userId))
|
|
|
|
playingUsers.Add(userId);
|
2020-10-21 18:05:20 +08:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
Task ISpectatorClient.UserFinishedPlaying(int userId, SpectatorState state)
|
2020-10-21 18:05:20 +08:00
|
|
|
{
|
2020-10-22 17:37:19 +08:00
|
|
|
playingUsers.Remove(userId);
|
2020-10-21 18:05:20 +08:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
Task ISpectatorClient.UserSentFrames(int userId, FrameDataBundle data)
|
2020-10-21 18:05:20 +08:00
|
|
|
{
|
2020-10-22 17:37:19 +08:00
|
|
|
OnNewFrames?.Invoke(userId, data);
|
2020-10-21 18:05:20 +08:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:29:43 +08:00
|
|
|
public void BeginPlaying()
|
2020-10-22 14:27:04 +08:00
|
|
|
{
|
|
|
|
if (!isConnected) return;
|
2020-10-21 18:05:20 +08:00
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
if (isPlaying)
|
|
|
|
throw new InvalidOperationException($"Cannot invoke {nameof(BeginPlaying)} when already playing");
|
|
|
|
|
|
|
|
isPlaying = true;
|
|
|
|
|
2020-10-22 16:29:43 +08:00
|
|
|
// transfer state at point of beginning play
|
|
|
|
currentState.BeatmapID = beatmap.Value.BeatmapInfo.OnlineBeatmapID;
|
2020-10-22 16:38:16 +08:00
|
|
|
currentState.Mods = mods.Value.Select(m => new APIMod(m));
|
2020-10-22 16:29:43 +08:00
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
beginPlaying();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void beginPlaying()
|
|
|
|
{
|
|
|
|
Debug.Assert(isPlaying);
|
|
|
|
|
2020-10-22 16:29:43 +08:00
|
|
|
connection.SendAsync(nameof(ISpectatorServer.BeginPlaySession), currentState);
|
2020-10-22 14:27:04 +08:00
|
|
|
}
|
2020-10-21 18:05:20 +08:00
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
public void SendFrames(FrameDataBundle data)
|
|
|
|
{
|
|
|
|
if (!isConnected) return;
|
|
|
|
|
2020-10-22 18:17:19 +08:00
|
|
|
lastSend = connection.SendAsync(nameof(ISpectatorServer.SendFrameData), data);
|
2020-10-22 14:27:04 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 16:29:38 +08:00
|
|
|
public void EndPlaying()
|
2020-10-22 14:27:04 +08:00
|
|
|
{
|
|
|
|
if (!isConnected) return;
|
2020-10-21 18:05:20 +08:00
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
if (!isPlaying)
|
|
|
|
throw new InvalidOperationException($"Cannot invoke {nameof(EndPlaying)} when not playing");
|
|
|
|
|
|
|
|
isPlaying = false;
|
2020-10-22 16:29:38 +08:00
|
|
|
connection.SendAsync(nameof(ISpectatorServer.EndPlaySession), currentState);
|
2020-10-22 14:27:04 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 17:37:19 +08:00
|
|
|
public void WatchUser(int userId)
|
2020-10-22 14:27:04 +08:00
|
|
|
{
|
2020-10-22 17:37:19 +08:00
|
|
|
if (watchingUsers.Contains(userId))
|
|
|
|
return;
|
|
|
|
|
|
|
|
watchingUsers.Add(userId);
|
2020-10-22 18:17:19 +08:00
|
|
|
|
|
|
|
if (!isConnected) return;
|
|
|
|
|
2020-10-22 14:27:04 +08:00
|
|
|
connection.SendAsync(nameof(ISpectatorServer.StartWatchingUser), userId);
|
|
|
|
}
|
2020-10-22 13:54:27 +08:00
|
|
|
|
2020-10-22 18:17:19 +08:00
|
|
|
public void StopWatchingUser(int userId)
|
|
|
|
{
|
|
|
|
watchingUsers.Remove(userId);
|
|
|
|
|
|
|
|
if (!isConnected) return;
|
|
|
|
|
|
|
|
connection.SendAsync(nameof(ISpectatorServer.EndWatchingUser), userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly Queue<LegacyReplayFrame> pendingFrames = new Queue<LegacyReplayFrame>();
|
|
|
|
|
|
|
|
private double lastSendTime;
|
|
|
|
|
|
|
|
private Task lastSend;
|
|
|
|
|
|
|
|
private const double time_between_sends = 200;
|
|
|
|
|
|
|
|
private const int max_pending_frames = 30;
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (pendingFrames.Count > 0 && Time.Current - lastSendTime > time_between_sends)
|
|
|
|
purgePendingFrames();
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:54:27 +08:00
|
|
|
public void HandleFrame(ReplayFrame frame)
|
|
|
|
{
|
|
|
|
if (frame is IConvertibleReplayFrame convertible)
|
2020-10-22 18:17:19 +08:00
|
|
|
pendingFrames.Enqueue(convertible.ToLegacy(beatmap.Value.Beatmap));
|
|
|
|
|
|
|
|
if (pendingFrames.Count > max_pending_frames)
|
|
|
|
purgePendingFrames();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void purgePendingFrames()
|
|
|
|
{
|
|
|
|
if (lastSend?.IsCompleted == false)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var frames = pendingFrames.ToArray();
|
|
|
|
|
|
|
|
pendingFrames.Clear();
|
|
|
|
|
|
|
|
SendFrames(new FrameDataBundle(frames));
|
|
|
|
|
|
|
|
lastSendTime = Time.Current;
|
2020-10-22 13:54:27 +08:00
|
|
|
}
|
2020-10-21 18:05:20 +08:00
|
|
|
}
|
|
|
|
}
|