aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI/Playback/PlaybackManager.cs
blob: 5ad1c9f5573061c07311c7c46fe939d7816a28dd (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Net;
using MediaBrowser.UI.Configuration;
using MediaBrowser.UI.Controller;
using MediaBrowser.UI.Playback.InternalPlayer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MediaBrowser.UI.Playback
{
    /// <summary>
    /// Class PlaybackManager
    /// </summary>
    public class PlaybackManager : BaseManager<UIKernel>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PlaybackManager" /> class.
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        public PlaybackManager(UIKernel kernel)
            : base(kernel)
        {

        }

        #region PlaybackStarted Event
        /// <summary>
        /// Occurs when [playback started].
        /// </summary>
        public event EventHandler<PlaybackEventArgs> PlaybackStarted;

        /// <summary>
        /// Called when [playback started].
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="options">The options.</param>
        /// <param name="playerConfiguration">The player configuration.</param>
        private void OnPlaybackStarted(BaseMediaPlayer player, PlayOptions options, PlayerConfiguration playerConfiguration)
        {
            EventHelper.QueueEventIfNotNull(PlaybackStarted, this, new PlaybackEventArgs
            {
                Options = options,
                Player = player,
                PlayerConfiguration = playerConfiguration
            });
        }
        #endregion

        #region PlaybackCompleted Event
        /// <summary>
        /// Occurs when [playback completed].
        /// </summary>
        public event EventHandler<PlaybackStopEventArgs> PlaybackCompleted;

        /// <summary>
        /// Called when [playback completed].
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="items">The items.</param>
        internal void OnPlaybackCompleted(BaseMediaPlayer player, List<DtoBaseItem> items)
        {
            EventHelper.QueueEventIfNotNull(PlaybackCompleted, this, new PlaybackStopEventArgs
            {
                Items = items,
                Player = player
            });
        }
        #endregion

        /// <summary>
        /// Gets the active players.
        /// </summary>
        /// <value>The active players.</value>
        public IEnumerable<BaseMediaPlayer> ActivePlayers
        {
            get
            {
                return Kernel.MediaPlayers.Where(m => m.PlayState != PlayState.Idle);
            }
        }

        /// <summary>
        /// Gets the active internal players.
        /// </summary>
        /// <value>The active internal players.</value>
        public IEnumerable<BaseMediaPlayer> ActiveInternalPlayers
        {
            get { return ActivePlayers.Where(p => p is BaseInternalMediaPlayer); }
        }

        /// <summary>
        /// Plays the specified items.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.InvalidOperationException"></exception>
        public async Task Play(PlayOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (options.Items == null || options.Items.Count == 0)
            {
                throw new ArgumentNullException("options");
            }

            var player = GetPlayer(options.Items);

            if (player != null)
            {
                await StopAllPlayback();

                await Play(player.Item1, options, player.Item2);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }

        /// <summary>
        /// Plays the specified player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="options">The options.</param>
        /// <param name="playerConfiguration">The player configuration.</param>
        /// <returns>Task.</returns>
        private async Task Play(BaseMediaPlayer player, PlayOptions options, PlayerConfiguration playerConfiguration)
        {
            if (options.Shuffle)
            {
                options.Items = options.Items.Shuffle().ToList();
            }

            var firstItem = options.Items[0];

            if (options.StartPositionTicks == 0 && player.SupportsMultiFilePlayback && firstItem.IsVideo && firstItem.LocationType == LocationType.FileSystem)
            {
                try
                {
                    var intros = await UIKernel.Instance.ApiClient.GetIntrosAsync(firstItem.Id, App.Instance.CurrentUser.Id);

                    options.Items.InsertRange(0, intros.Select(GetPlayableItem));
                }
                catch (HttpException ex)
                {
                    Logger.ErrorException("Error retrieving intros", ex);
                }
            }

            await player.Play(options, playerConfiguration);

            OnPlaybackStarted(player, options, playerConfiguration);
        }

        /// <summary>
        /// Gets the playable item.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>DtoBaseItem.</returns>
        public DtoBaseItem GetPlayableItem(string path)
        {
            return new DtoBaseItem
            {
                Path = path,
                Name = Path.GetFileName(path),
                Type = "Video",
                VideoType = VideoType.VideoFile,
                IsFolder = false
            };
        }

        /// <summary>
        /// Gets the playable item.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="name">The name.</param>
        /// <returns>DtoBaseItem.</returns>
        public DtoBaseItem GetPlayableItem(Uri uri, string name)
        {
            return new DtoBaseItem
            {
                Path = uri.ToString(),
                Name = name,
                Type = "Video",
                VideoType = VideoType.VideoFile,
                IsFolder = false,
                LocationType = LocationType.Remote
            };
        }

        /// <summary>
        /// Stops all playback.
        /// </summary>
        /// <returns>Task.</returns>
        public async Task StopAllPlayback()
        {
            var tasks = Kernel.MediaPlayers.Where(p => p.PlayState == PlayState.Playing || p.PlayState == PlayState.Paused).Select(p => p.Stop());

            await Task.WhenAll(tasks);
        }

        /// <summary>
        /// Gets the player.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <returns>BaseMediaPlayer.</returns>
        private Tuple<BaseMediaPlayer, PlayerConfiguration> GetPlayer(List<DtoBaseItem> items)
        {
            var player = GetConfiguredPlayer(items);

            if (player != null)
            {
                return player;
            }

            // If there's no explicit configuration just find the first matching player
            var mediaPlayer = Kernel.MediaPlayers.OfType<BaseInternalMediaPlayer>().FirstOrDefault(p => items.All(p.CanPlay));

            if (mediaPlayer != null)
            {
                return new Tuple<BaseMediaPlayer, PlayerConfiguration>(mediaPlayer, null);
            }

            return null;
        }

        /// <summary>
        /// Gets the configured player.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <returns>BaseMediaPlayer.</returns>
        private Tuple<BaseMediaPlayer, PlayerConfiguration> GetConfiguredPlayer(List<DtoBaseItem> items)
        {
            if (UIKernel.Instance.Configuration.MediaPlayers == null)
            {
                return null;
            }

            return UIKernel.Instance.Configuration.MediaPlayers.Where(p => IsConfiguredToPlay(p, items))
                           .Select(p => new Tuple<BaseMediaPlayer, PlayerConfiguration>(UIKernel.Instance.MediaPlayers.FirstOrDefault(m => m.Name.Equals(p.PlayerName, StringComparison.OrdinalIgnoreCase)), p))
                           .FirstOrDefault(p => p.Item1 != null);
        }

        /// <summary>
        /// Determines whether [is configured to play] [the specified configuration].
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="items">The items.</param>
        /// <returns><c>true</c> if [is configured to play] [the specified configuration]; otherwise, <c>false</c>.</returns>
        private bool IsConfiguredToPlay(PlayerConfiguration configuration, List<DtoBaseItem> items)
        {
            if (configuration.ItemTypes != null && configuration.ItemTypes.Length > 0)
            {
                if (items.Any(i => !configuration.ItemTypes.Contains(i.Type, StringComparer.OrdinalIgnoreCase)))
                {
                    return false;
                }
            }

            if (configuration.FileExtensions != null && configuration.FileExtensions.Length > 0)
            {
                if (items.Any(i => !configuration.FileExtensions.Select(ext => ext.TrimStart('.')).Contains((Path.GetExtension(i.Path) ?? string.Empty).TrimStart('.'), StringComparer.OrdinalIgnoreCase)))
                {
                    return false;
                }
            }

            if (configuration.VideoTypes != null && configuration.VideoTypes.Length > 0)
            {
                if (items.Any(i => i.VideoType.HasValue && !configuration.VideoTypes.Contains(i.VideoType.Value)))
                {
                    return false;
                }
            }

            if (configuration.VideoFormats != null && configuration.VideoFormats.Length > 0)
            {
                if (items.Any(i => i.VideoFormat.HasValue && !configuration.VideoFormats.Contains(i.VideoFormat.Value)))
                {
                    return false;
                }
            }

            return true;
        }
    }
}