aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Library/UserDataManager.cs
blob: dfa80483ec0ce4b178f6e2dcfbccd57d7d84e104 (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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Connectivity;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.Controller.Library
{
    /// <summary>
    /// Class UserDataManager
    /// </summary>
    public class UserDataManager : BaseManager<Kernel>
    {
        #region Events
        /// <summary>
        /// Occurs when [playback start].
        /// </summary>
        public event EventHandler<PlaybackProgressEventArgs> PlaybackStart;
        /// <summary>
        /// Occurs when [playback progress].
        /// </summary>
        public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
        /// <summary>
        /// Occurs when [playback stopped].
        /// </summary>
        public event EventHandler<PlaybackProgressEventArgs> PlaybackStopped;
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="UserDataManager" /> class.
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        public UserDataManager(Kernel kernel)
            : base(kernel)
        {

        }

        /// <summary>
        /// Used to report that playback has started for an item
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="item">The item.</param>
        /// <param name="clientType">Type of the client.</param>
        /// <param name="deviceName">Name of the device.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        public void OnPlaybackStart(User user, BaseItem item, ClientType clientType, string deviceName)
        {
            if (user == null)
            {
                throw new ArgumentNullException();
            }
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            Kernel.UserManager.UpdateNowPlayingItemId(user, clientType, deviceName, item);

            // Nothing to save here
            // Fire events to inform plugins
            EventHelper.QueueEventIfNotNull(PlaybackStart, this, new PlaybackProgressEventArgs
            {
                Argument = item,
                User = user
            });
        }

        /// <summary>
        /// Used to report playback progress for an item
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="item">The item.</param>
        /// <param name="positionTicks">The position ticks.</param>
        /// <param name="clientType">Type of the client.</param>
        /// <param name="deviceName">Name of the device.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public async Task OnPlaybackProgress(User user, BaseItem item, long? positionTicks, ClientType clientType, string deviceName)
        {
            if (user == null)
            {
                throw new ArgumentNullException();
            }
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            Kernel.UserManager.UpdateNowPlayingItemId(user, clientType, deviceName, item, positionTicks);

            if (positionTicks.HasValue)
            {
                var data = item.GetUserData(user, true);

                UpdatePlayState(item, data, positionTicks.Value, false);
                await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
            }

            EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
            {
                Argument = item,
                User = user,
                PlaybackPositionTicks = positionTicks
            });
        }

        /// <summary>
        /// Used to report that playback has ended for an item
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="item">The item.</param>
        /// <param name="positionTicks">The position ticks.</param>
        /// <param name="clientType">Type of the client.</param>
        /// <param name="deviceName">Name of the device.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public async Task OnPlaybackStopped(User user, BaseItem item, long? positionTicks, ClientType clientType, string deviceName)
        {
            if (user == null)
            {
                throw new ArgumentNullException();
            }
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            Kernel.UserManager.RemoveNowPlayingItemId(user, clientType, deviceName, item);
            
            var data = item.GetUserData(user, true);

            if (positionTicks.HasValue)
            {
                UpdatePlayState(item, data, positionTicks.Value, true);
            }
            else
            {
                // If the client isn't able to report this, then we'll just have to make an assumption
                data.PlayCount++;
                data.Played = true;
            }

            await SaveUserDataForItem(user, item, data).ConfigureAwait(false);

            EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs
            {
                Argument = item,
                User = user,
                PlaybackPositionTicks = positionTicks
            });
        }

        /// <summary>
        /// Updates playstate position for an item but does not save
        /// </summary>
        /// <param name="item">The item</param>
        /// <param name="data">User data for the item</param>
        /// <param name="positionTicks">The current playback position</param>
        /// <param name="incrementPlayCount">Whether or not to increment playcount</param>
        private void UpdatePlayState(BaseItem item, UserItemData data, long positionTicks, bool incrementPlayCount)
        {
            // If a position has been reported, and if we know the duration
            if (positionTicks > 0 && item.RunTimeTicks.HasValue && item.RunTimeTicks > 0)
            {
                var pctIn = Decimal.Divide(positionTicks, item.RunTimeTicks.Value) * 100;

                // Don't track in very beginning
                if (pctIn < Kernel.Configuration.MinResumePct)
                {
                    positionTicks = 0;
                    incrementPlayCount = false;
                }

                // If we're at the end, assume completed
                else if (pctIn > Kernel.Configuration.MaxResumePct || positionTicks >= item.RunTimeTicks.Value)
                {
                    positionTicks = 0;
                    data.Played = true;
                }

                else
                {
                    // Enforce MinResumeDuration
                    var durationSeconds = TimeSpan.FromTicks(item.RunTimeTicks.Value).TotalSeconds;

                    if (durationSeconds < Kernel.Configuration.MinResumeDurationSeconds)
                    {
                        positionTicks = 0;
                        data.Played = true;
                    }
                }
            }

            data.PlaybackPositionTicks = positionTicks;

            if (incrementPlayCount)
            {
                data.PlayCount++;
                data.LastPlayedDate = DateTime.UtcNow;
            }
        }

        /// <summary>
        /// Saves user data for an item
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="item">The item.</param>
        /// <param name="data">The data.</param>
        public Task SaveUserDataForItem(User user, BaseItem item, UserItemData data)
        {
            item.AddOrUpdateUserData(user, data);

            return Kernel.UserDataRepository.SaveUserData(item, CancellationToken.None);
        }
    }
}