aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/SyncPlay/GroupStates/PlayingGroupState.cs
blob: 42c7779c197485c144b5ccec5bb7df6485761827 (plain)
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
using System.Linq;
using System;
using System.Threading;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Session;
using MediaBrowser.Model.SyncPlay;

namespace MediaBrowser.Controller.SyncPlay
{
    /// <summary>
    /// Class PlayingGroupState.
    /// </summary>
    /// <remarks>
    /// Class is not thread-safe, external locking is required when accessing methods.
    /// </remarks>
    public class PlayingGroupState : SyncPlayAbstractState
    {
        /// <inheritdoc />
        public override GroupState GetGroupState()
        {
            return GroupState.Playing;
        }

        /// <inheritdoc />
        public override bool HandleRequest(ISyncPlayStateContext context, bool newState, PlayGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
        {
            GroupInfo group = context.GetGroup();

            if (newState)
            {
                // Pick a suitable time that accounts for latency
                var delay = Math.Max(group.GetHighestPing() * 2, group.DefaultPing);

                // Unpause group and set starting point in future
                // Clients will start playback at LastActivity (datetime) from PositionTicks (playback position)
                // The added delay does not guarantee, of course, that the command will be received in time
                // Playback synchronization will mainly happen client side
                group.LastActivity = DateTime.UtcNow.AddMilliseconds(
                    delay
                );

                var command = context.NewSyncPlayCommand(SendCommandType.Play);
                context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
            }
            else
            {
                // Client got lost, sending current state
                var command = context.NewSyncPlayCommand(SendCommandType.Play);
                context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
            }

            return true;
        }

        /// <inheritdoc />
        public override bool HandleRequest(ISyncPlayStateContext context, bool newState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
        {
            // Change state
            var pausedState = new PausedGroupState();
            context.SetState(pausedState);
            return pausedState.HandleRequest(context, true, request, session, cancellationToken);
        }

        /// <inheritdoc />
        public override bool HandleRequest(ISyncPlayStateContext context, bool newState, SeekGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
        {
            // Change state
            var pausedState = new PausedGroupState();
            context.SetState(pausedState);
            return pausedState.HandleRequest(context, true, request, session, cancellationToken);
        }

        /// <inheritdoc />
        public override bool HandleRequest(ISyncPlayStateContext context, bool newState, BufferGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
        {
            // Change state
            var pausedState = new PausedGroupState();
            context.SetState(pausedState);
            return pausedState.HandleRequest(context, true, request, session, cancellationToken);
        }

        /// <inheritdoc />
        public override bool HandleRequest(ISyncPlayStateContext context, bool newState, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
        {
            // Group was not waiting, make sure client has latest state
            var command = context.NewSyncPlayCommand(SendCommandType.Play);
            context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);

            return true;
        }
    }
}