2021-04-15 10:12:52 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2023-08-02 09:26:36 +00:00
|
|
|
using osu.Framework.Logging;
|
2021-04-15 10:12:52 +00:00
|
|
|
using osu.Framework.Timing;
|
2022-08-24 05:50:10 +00:00
|
|
|
using osu.Game.Screens.Play;
|
2021-04-15 10:12:52 +00:00
|
|
|
|
2021-05-03 04:38:53 +00:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
2021-04-15 10:12:52 +00:00
|
|
|
{
|
2021-04-15 10:35:57 +00:00
|
|
|
/// <summary>
|
2022-08-24 06:07:04 +00:00
|
|
|
/// A clock which catches up using rate adjustment.
|
2021-04-15 10:35:57 +00:00
|
|
|
/// </summary>
|
2022-08-24 06:07:04 +00:00
|
|
|
public class SpectatorPlayerClock : IFrameBasedClock, IAdjustableClock
|
2021-04-15 10:12:52 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-04-15 10:35:57 +00:00
|
|
|
/// The catch up rate.
|
2021-04-15 10:12:52 +00:00
|
|
|
/// </summary>
|
2022-08-24 06:37:14 +00:00
|
|
|
private const double catchup_rate = 2;
|
2021-04-15 10:12:52 +00:00
|
|
|
|
2022-08-24 05:50:10 +00:00
|
|
|
private readonly GameplayClockContainer masterClock;
|
2021-04-15 10:12:52 +00:00
|
|
|
|
|
|
|
public double CurrentTime { get; private set; }
|
|
|
|
|
2022-08-24 06:17:56 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether this clock is waiting on frames to continue playback.
|
|
|
|
/// </summary>
|
2022-08-24 06:40:47 +00:00
|
|
|
public bool WaitingOnFrames { get; set; } = true;
|
2022-08-24 06:17:56 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether this clock is behind the master clock and running at a higher rate to catch up to it.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Of note, this will be false if this clock is *ahead* of the master clock.
|
|
|
|
/// </remarks>
|
|
|
|
public bool IsCatchingUp { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether this spectator clock should be running.
|
|
|
|
/// Use instead of <see cref="Start"/> / <see cref="Stop"/> to control time.
|
|
|
|
/// </summary>
|
|
|
|
public bool IsRunning { get; set; }
|
2021-04-15 10:12:52 +00:00
|
|
|
|
2022-08-24 06:07:04 +00:00
|
|
|
public SpectatorPlayerClock(GameplayClockContainer masterClock)
|
2022-08-22 12:52:43 +00:00
|
|
|
{
|
2022-08-24 05:50:10 +00:00
|
|
|
this.masterClock = masterClock;
|
2022-08-22 12:52:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 10:12:52 +00:00
|
|
|
public void Reset() => CurrentTime = 0;
|
|
|
|
|
2022-08-24 06:17:56 +00:00
|
|
|
public void Start()
|
2022-05-12 21:07:11 +00:00
|
|
|
{
|
2022-08-24 06:17:56 +00:00
|
|
|
// Our running state should only be managed by SpectatorSyncManager via IsRunning.
|
2022-05-12 21:07:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 06:17:56 +00:00
|
|
|
public void Stop()
|
2022-05-12 21:07:11 +00:00
|
|
|
{
|
2022-08-24 06:17:56 +00:00
|
|
|
// Our running state should only be managed by an SpectatorSyncManager via IsRunning.
|
2022-05-12 21:07:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 07:23:59 +00:00
|
|
|
public bool Seek(double position)
|
|
|
|
{
|
2023-08-02 09:26:36 +00:00
|
|
|
Logger.Log($"{nameof(SpectatorPlayerClock)} seeked to {position}");
|
2021-06-11 07:23:59 +00:00
|
|
|
CurrentTime = position;
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-15 10:12:52 +00:00
|
|
|
|
|
|
|
public void ResetSpeedAdjustments()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-24 06:38:25 +00:00
|
|
|
public double Rate
|
2021-04-15 10:12:52 +00:00
|
|
|
{
|
2022-08-24 06:38:25 +00:00
|
|
|
get => IsCatchingUp ? catchup_rate : 1;
|
|
|
|
set => throw new NotImplementedException();
|
2021-04-15 10:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ProcessFrame()
|
|
|
|
{
|
|
|
|
if (IsRunning)
|
|
|
|
{
|
2022-08-24 07:33:07 +00:00
|
|
|
// When in catch-up mode, the source is usually not running.
|
|
|
|
// In such a case, its elapsed time may be zero, which would cause catch-up to get stuck.
|
|
|
|
// To avoid this, use a constant 16ms elapsed time for now. Probably not too correct, but this whole logic isn't too correct anyway.
|
2022-08-25 12:33:33 +00:00
|
|
|
// Clamping is required to ensure that player clocks don't get too far ahead if ProcessFrame is run multiple times.
|
|
|
|
double elapsedSource = masterClock.ElapsedFrameTime != 0 ? masterClock.ElapsedFrameTime : Math.Clamp(masterClock.CurrentTime - CurrentTime, 0, 16);
|
2021-04-15 10:12:52 +00:00
|
|
|
double elapsed = elapsedSource * Rate;
|
|
|
|
|
|
|
|
CurrentTime += elapsed;
|
|
|
|
ElapsedFrameTime = elapsed;
|
2022-08-24 05:50:10 +00:00
|
|
|
FramesPerSecond = masterClock.FramesPerSecond;
|
2021-04-15 10:12:52 +00:00
|
|
|
}
|
2022-08-24 06:37:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ElapsedFrameTime = 0;
|
|
|
|
FramesPerSecond = 0;
|
|
|
|
}
|
2021-04-15 10:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public double ElapsedFrameTime { get; private set; }
|
|
|
|
|
|
|
|
public double FramesPerSecond { get; private set; }
|
|
|
|
|
|
|
|
public FrameTimeInfo TimeInfo => new FrameTimeInfo { Elapsed = ElapsedFrameTime, Current = CurrentTime };
|
|
|
|
}
|
|
|
|
}
|