// 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.IO;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Video;
using osu.Game.Graphics;

namespace osu.Game.Tournament.Components
{
    public class TourneyVideo : CompositeDrawable
    {
        private readonly VideoSprite video;

        public TourneyVideo(Stream stream)
        {
            if (stream == null)
            {
                InternalChild = new Box
                {
                    Colour = ColourInfo.GradientVertical(OsuColour.Gray(0.3f), OsuColour.Gray(0.6f)),
                    RelativeSizeAxes = Axes.Both,
                };
            }
            else
                InternalChild = video = new VideoSprite(stream)
                {
                    RelativeSizeAxes = Axes.Both,
                    FillMode = FillMode.Fit,
                };
        }

        public bool Loop
        {
            set
            {
                if (video != null)
                    video.Loop = value;
            }
        }
    }
}

// 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.IO;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Video;
using osu.Framework.Timing;
using osu.Game.Graphics;

namespace osu.Game.Tournament.Components
{
    public class TourneyVideo : CompositeDrawable
    {
        private readonly VideoSprite video;

        private ManualClock manualClock;
        private IFrameBasedClock sourceClock;

        public TourneyVideo(Stream stream)
        {
            if (stream == null)
            {
                InternalChild = new Box
                {
                    Colour = ColourInfo.GradientVertical(OsuColour.Gray(0.3f), OsuColour.Gray(0.6f)),
                    RelativeSizeAxes = Axes.Both,
                };
            }
            else
                InternalChild = video = new VideoSprite(stream)
                {
                    RelativeSizeAxes = Axes.Both,
                    FillMode = FillMode.Fit,
                };
        }

        public bool Loop
        {
            set
            {
                if (video != null)
                    video.Loop = value;
            }
        }

        protected override void LoadComplete()
        {
            base.LoadComplete();

            sourceClock = Clock;
            Clock = new FramedClock(manualClock = new ManualClock());
        }

        protected override void Update()
        {
            base.Update();

            // we want to avoid seeking as much as possible, because we care about performance, not sync.
            // to avoid seeking completely, we only increment out local clock when in an updating state.
            manualClock.CurrentTime += sourceClock.ElapsedFrameTime;
        }
    }
}

Fix tournament videos stuttering when changing scenes
Fix tournament videos stuttering when changing scenes
