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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Security;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Globalization;
namespace MediaBrowser.Server.Implementations.Intros
{
public class DefaultIntroProvider : IIntroProvider
{
private readonly ISecurityManager _security;
private readonly ILocalizationManager _localization;
private readonly IConfigurationManager _serverConfig;
private readonly ILibraryManager _libraryManager;
private readonly IFileSystem _fileSystem;
private readonly IMediaSourceManager _mediaSourceManager;
public DefaultIntroProvider(ISecurityManager security, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
{
_security = security;
_localization = localization;
_serverConfig = serverConfig;
_libraryManager = libraryManager;
_fileSystem = fileSystem;
_mediaSourceManager = mediaSourceManager;
}
public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
{
var config = GetOptions();
if (item is Movie)
{
if (!config.EnableIntrosForMovies)
{
return new List<IntroInfo>();
}
}
else if (item is Episode)
{
if (!config.EnableIntrosForEpisodes)
{
return new List<IntroInfo>();
}
}
else
{
return new List<IntroInfo>();
}
var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
? null
: _localization.GetRatingLevel(item.OfficialRating);
var candidates = new List<ItemWithTrailer>();
var trailerTypes = new List<TrailerType>();
var sourceTypes = new List<SourceType>();
if (config.EnableIntrosFromMoviesInLibrary)
{
trailerTypes.Add(TrailerType.LocalTrailer);
sourceTypes.Add(SourceType.Library);
}
if (IsSupporter)
{
if (config.EnableIntrosFromUpcomingTrailers)
{
trailerTypes.Add(TrailerType.ComingSoonToTheaters);
sourceTypes.Clear();
}
if (config.EnableIntrosFromUpcomingDvdMovies)
{
trailerTypes.Add(TrailerType.ComingSoonToDvd);
sourceTypes.Clear();
}
if (config.EnableIntrosFromUpcomingStreamingMovies)
{
trailerTypes.Add(TrailerType.ComingSoonToStreaming);
sourceTypes.Clear();
}
if (config.EnableIntrosFromSimilarMovies)
{
trailerTypes.Add(TrailerType.Archive);
sourceTypes.Clear();
}
}
if (trailerTypes.Count > 0)
{
var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(Trailer).Name },
TrailerTypes = trailerTypes.ToArray(),
SimilarTo = item,
IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
// Account for duplicates by imdb id, since the database doesn't support this yet
Limit = config.TrailerLimit * 2,
SourceTypes = sourceTypes.ToArray()
}).Where(i => string.IsNullOrWhiteSpace(i.GetProviderId(MetadataProviders.Imdb)) || !string.Equals(i.GetProviderId(MetadataProviders.Imdb), item.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase)).Take(config.TrailerLimit);
candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
{
Item = i,
Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
LibraryManager = _libraryManager
}));
}
return GetResult(item, candidates, config);
}
private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config)
{
var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
GetCustomIntros(config) :
new List<IntroInfo>();
var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
GetMediaInfoIntros(config, item) :
new List<IntroInfo>();
// Avoid implicitly captured closure
return candidates.Select(i => i.IntroInfo)
.Concat(customIntros.Take(1))
.Concat(mediaInfoIntros);
}
private CinemaModeConfiguration GetOptions()
{
return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
}
private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
{
try
{
return GetCustomIntroFiles(options, true, false)
.OrderBy(i => Guid.NewGuid())
.Select(i => new IntroInfo
{
Path = i
}).ToList();
}
catch (IOException)
{
return new List<IntroInfo>();
}
}
private IEnumerable<IntroInfo> GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
{
try
{
var hasMediaSources = item as IHasMediaSources;
if (hasMediaSources == null)
{
return new List<IntroInfo>();
}
var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
.FirstOrDefault();
if (mediaSource == null)
{
return new List<IntroInfo>();
}
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
var allIntros = GetCustomIntroFiles(options, false, true)
.OrderBy(i => Guid.NewGuid())
.Select(i => new IntroInfo
{
Path = i
}).ToList();
var returnResult = new List<IntroInfo>();
if (videoStream != null)
{
returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
}
if (audioStream != null)
{
returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
}
returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
}
catch (IOException)
{
return new List<IntroInfo>();
}
}
private IEnumerable<IntroInfo> GetMediaInfoIntrosByVideoStream(List<IntroInfo> allIntros, MediaStream stream)
{
var codec = stream.Codec;
if (string.IsNullOrWhiteSpace(codec))
{
return new List<IntroInfo>();
}
return allIntros
.Where(i => IsMatch(i.Path, codec))
.OrderBy(i => Guid.NewGuid());
}
private IEnumerable<IntroInfo> GetMediaInfoIntrosByAudioStream(List<IntroInfo> allIntros, MediaStream stream)
{
var codec = stream.Codec;
if (string.IsNullOrWhiteSpace(codec))
{
return new List<IntroInfo>();
}
return allIntros
.Where(i => IsAudioMatch(i.Path, stream))
.OrderBy(i => Guid.NewGuid());
}
private IEnumerable<IntroInfo> GetMediaInfoIntrosByTags(List<IntroInfo> allIntros, List<string> tags)
{
return allIntros
.Where(i => tags.Any(t => IsMatch(i.Path, t)))
.OrderBy(i => Guid.NewGuid());
}
private bool IsMatch(string file, string attribute)
{
var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
filename = Normalize(filename);
if (string.IsNullOrWhiteSpace(filename))
{
return false;
}
attribute = Normalize(attribute);
if (string.IsNullOrWhiteSpace(attribute))
{
return false;
}
return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
}
private string Normalize(string value)
{
return value;
}
private bool IsAudioMatch(string path, MediaStream stream)
{
if (!string.IsNullOrWhiteSpace(stream.Codec))
{
if (IsMatch(path, stream.Codec))
{
return true;
}
}
if (!string.IsNullOrWhiteSpace(stream.Profile))
{
if (IsMatch(path, stream.Profile))
{
return true;
}
}
return false;
}
private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
{
var list = new List<string>();
if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
{
list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
.Where(_libraryManager.IsVideoFile));
}
if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
{
list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
.Where(_libraryManager.IsVideoFile));
}
return list.Distinct(StringComparer.OrdinalIgnoreCase);
}
public IEnumerable<string> GetAllIntroFiles()
{
return GetCustomIntroFiles(GetOptions(), true, true);
}
private bool IsSupporter
{
get { return _security.IsMBSupporter; }
}
public string Name
{
get { return "Default"; }
}
internal class ItemWithTrailer
{
internal BaseItem Item;
internal ItemWithTrailerType Type;
internal ILibraryManager LibraryManager;
public IntroInfo IntroInfo
{
get
{
var id = Item.Id;
if (Type == ItemWithTrailerType.ItemWithTrailer)
{
var hasTrailers = Item as IHasTrailers;
if (hasTrailers != null)
{
id = hasTrailers.LocalTrailerIds.FirstOrDefault();
}
}
return new IntroInfo
{
ItemId = id
};
}
}
}
internal enum ItemWithTrailerType
{
ChannelTrailer,
ItemWithTrailer
}
}
public class CinemaModeConfigurationFactory : IConfigurationFactory
{
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return new[]
{
new ConfigurationStore
{
ConfigurationType = typeof(CinemaModeConfiguration),
Key = "cinemamode"
}
};
}
}
}
|