| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Collections.Immutable; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Jellyfin.Database.Implementations; |
| | 9 | | using Jellyfin.Database.Implementations.Entities; |
| | 10 | | using Jellyfin.Database.Implementations.Enums; |
| | 11 | | using Jellyfin.Extensions; |
| | 12 | | using MediaBrowser.Common.Extensions; |
| | 13 | | using MediaBrowser.Controller; |
| | 14 | | using MediaBrowser.Controller.Entities; |
| | 15 | | using MediaBrowser.Controller.Entities.Audio; |
| | 16 | | using MediaBrowser.Controller.Library; |
| | 17 | | using MediaBrowser.Controller.Providers; |
| | 18 | | using MediaBrowser.Model; |
| | 19 | | using MediaBrowser.Model.MediaSegments; |
| | 20 | | using Microsoft.EntityFrameworkCore; |
| | 21 | | using Microsoft.Extensions.Logging; |
| | 22 | |
|
| | 23 | | namespace Jellyfin.Server.Implementations.MediaSegments; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Manages media segments retrieval and storage. |
| | 27 | | /// </summary> |
| | 28 | | public class MediaSegmentManager : IMediaSegmentManager |
| | 29 | | { |
| | 30 | | private readonly ILogger<MediaSegmentManager> _logger; |
| | 31 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | 32 | | private readonly IMediaSegmentProvider[] _segmentProviders; |
| | 33 | | private readonly ILibraryManager _libraryManager; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Initializes a new instance of the <see cref="MediaSegmentManager"/> class. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="logger">Logger.</param> |
| | 39 | | /// <param name="dbProvider">EFCore Database factory.</param> |
| | 40 | | /// <param name="segmentProviders">List of all media segment providers.</param> |
| | 41 | | /// <param name="libraryManager">Library manager.</param> |
| | 42 | | public MediaSegmentManager( |
| | 43 | | ILogger<MediaSegmentManager> logger, |
| | 44 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | 45 | | IEnumerable<IMediaSegmentProvider> segmentProviders, |
| | 46 | | ILibraryManager libraryManager) |
| | 47 | | { |
| 21 | 48 | | _logger = logger; |
| 21 | 49 | | _dbProvider = dbProvider; |
| | 50 | |
|
| 21 | 51 | | _segmentProviders = segmentProviders |
| 21 | 52 | | .OrderBy(i => i is IHasOrder hasOrder ? hasOrder.Order : 0) |
| 21 | 53 | | .ToArray(); |
| 21 | 54 | | _libraryManager = libraryManager; |
| 21 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc/> |
| | 58 | | public async Task RunSegmentPluginProviders(BaseItem baseItem, bool overwrite, CancellationToken cancellationToken) |
| | 59 | | { |
| | 60 | | var libraryOptions = _libraryManager.GetLibraryOptions(baseItem); |
| | 61 | | var providers = _segmentProviders |
| | 62 | | .Where(e => !libraryOptions.DisabledMediaSegmentProviders.Contains(GetProviderId(e.Name))) |
| | 63 | | .OrderBy(i => |
| | 64 | | { |
| | 65 | | var index = libraryOptions.MediaSegmentProviderOrder.IndexOf(i.Name); |
| | 66 | | return index == -1 ? int.MaxValue : index; |
| | 67 | | }) |
| | 68 | | .ToList(); |
| | 69 | |
|
| | 70 | | if (providers.Count == 0) |
| | 71 | | { |
| | 72 | | _logger.LogDebug("Skipping media segment extraction as no providers are enabled for {MediaPath}", baseItem.P |
| | 73 | | return; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | using var db = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 77 | |
|
| | 78 | | if (!overwrite && (await db.MediaSegments.AnyAsync(e => e.ItemId.Equals(baseItem.Id), cancellationToken).Configu |
| | 79 | | { |
| | 80 | | _logger.LogDebug("Skip {MediaPath} as it already contains media segments", baseItem.Path); |
| | 81 | | return; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | _logger.LogDebug("Start media segment extraction for {MediaPath} with {CountProviders} providers enabled", baseI |
| | 85 | |
|
| | 86 | | await db.MediaSegments.Where(e => e.ItemId.Equals(baseItem.Id)).ExecuteDeleteAsync(cancellationToken).ConfigureA |
| | 87 | |
|
| | 88 | | // no need to recreate the request object every time. |
| | 89 | | var requestItem = new MediaSegmentGenerationRequest() { ItemId = baseItem.Id }; |
| | 90 | |
|
| | 91 | | foreach (var provider in providers) |
| | 92 | | { |
| | 93 | | if (!await provider.Supports(baseItem).ConfigureAwait(false)) |
| | 94 | | { |
| | 95 | | _logger.LogDebug("Media Segment provider {ProviderName} does not support item with path {MediaPath}", pr |
| | 96 | | continue; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | try |
| | 100 | | { |
| | 101 | | var segments = await provider.GetMediaSegments(requestItem, cancellationToken) |
| | 102 | | .ConfigureAwait(false); |
| | 103 | | if (segments.Count == 0) |
| | 104 | | { |
| | 105 | | _logger.LogDebug("Media Segment provider {ProviderName} did not find any segments for {MediaPath}", |
| | 106 | | continue; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | _logger.LogInformation("Media Segment provider {ProviderName} found {CountSegments} for {MediaPath}", pr |
| | 110 | | var providerId = GetProviderId(provider.Name); |
| | 111 | | foreach (var segment in segments) |
| | 112 | | { |
| | 113 | | segment.ItemId = baseItem.Id; |
| | 114 | | await CreateSegmentAsync(segment, providerId).ConfigureAwait(false); |
| | 115 | | } |
| | 116 | | } |
| | 117 | | catch (Exception ex) |
| | 118 | | { |
| | 119 | | _logger.LogError(ex, "Provider {ProviderName} failed to extract segments from {MediaPath}", provider.Nam |
| | 120 | | } |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| | 124 | | /// <inheritdoc /> |
| | 125 | | public async Task<MediaSegmentDto> CreateSegmentAsync(MediaSegmentDto mediaSegment, string segmentProviderId) |
| | 126 | | { |
| | 127 | | ArgumentOutOfRangeException.ThrowIfLessThan(mediaSegment.EndTicks, mediaSegment.StartTicks); |
| | 128 | |
|
| | 129 | | using var db = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 130 | | db.MediaSegments.Add(Map(mediaSegment, segmentProviderId)); |
| | 131 | | await db.SaveChangesAsync().ConfigureAwait(false); |
| | 132 | | return mediaSegment; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | /// <inheritdoc /> |
| | 136 | | public async Task DeleteSegmentAsync(Guid segmentId) |
| | 137 | | { |
| | 138 | | using var db = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 139 | | await db.MediaSegments.Where(e => e.Id.Equals(segmentId)).ExecuteDeleteAsync().ConfigureAwait(false); |
| | 140 | | } |
| | 141 | |
|
| | 142 | | /// <inheritdoc /> |
| | 143 | | public async Task<IEnumerable<MediaSegmentDto>> GetSegmentsAsync(Guid itemId, IEnumerable<MediaSegmentType>? typeFil |
| | 144 | | { |
| | 145 | | var baseItem = _libraryManager.GetItemById(itemId); |
| | 146 | |
|
| | 147 | | if (baseItem is null) |
| | 148 | | { |
| | 149 | | _logger.LogError("Tried to request segments for an invalid item"); |
| | 150 | | return []; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | return await GetSegmentsAsync(baseItem, typeFilter, filterByProvider).ConfigureAwait(false); |
| | 154 | | } |
| | 155 | |
|
| | 156 | | /// <inheritdoc /> |
| | 157 | | public async Task<IEnumerable<MediaSegmentDto>> GetSegmentsAsync(BaseItem item, IEnumerable<MediaSegmentType>? typeF |
| | 158 | | { |
| | 159 | | using var db = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 160 | |
|
| | 161 | | var query = db.MediaSegments |
| | 162 | | .Where(e => e.ItemId.Equals(item.Id)); |
| | 163 | |
|
| | 164 | | if (typeFilter is not null) |
| | 165 | | { |
| | 166 | | query = query.Where(e => typeFilter.Contains(e.Type)); |
| | 167 | | } |
| | 168 | |
|
| | 169 | | if (filterByProvider) |
| | 170 | | { |
| | 171 | | var libraryOptions = _libraryManager.GetLibraryOptions(item); |
| | 172 | | var providerIds = _segmentProviders |
| | 173 | | .Where(e => !libraryOptions.DisabledMediaSegmentProviders.Contains(GetProviderId(e.Name))) |
| | 174 | | .Select(f => GetProviderId(f.Name)) |
| | 175 | | .ToArray(); |
| | 176 | | if (providerIds.Length == 0) |
| | 177 | | { |
| | 178 | | return []; |
| | 179 | | } |
| | 180 | |
|
| | 181 | | query = query.Where(e => providerIds.Contains(e.SegmentProviderId)); |
| | 182 | | } |
| | 183 | |
|
| | 184 | | return query |
| | 185 | | .OrderBy(e => e.StartTicks) |
| | 186 | | .AsNoTracking() |
| | 187 | | .AsEnumerable() |
| | 188 | | .Select(Map) |
| | 189 | | .ToArray(); |
| | 190 | | } |
| | 191 | |
|
| | 192 | | private static MediaSegmentDto Map(MediaSegment segment) |
| | 193 | | { |
| 0 | 194 | | return new MediaSegmentDto() |
| 0 | 195 | | { |
| 0 | 196 | | Id = segment.Id, |
| 0 | 197 | | EndTicks = segment.EndTicks, |
| 0 | 198 | | ItemId = segment.ItemId, |
| 0 | 199 | | StartTicks = segment.StartTicks, |
| 0 | 200 | | Type = segment.Type |
| 0 | 201 | | }; |
| | 202 | | } |
| | 203 | |
|
| | 204 | | private static MediaSegment Map(MediaSegmentDto segment, string segmentProviderId) |
| | 205 | | { |
| 0 | 206 | | return new MediaSegment() |
| 0 | 207 | | { |
| 0 | 208 | | Id = segment.Id, |
| 0 | 209 | | EndTicks = segment.EndTicks, |
| 0 | 210 | | ItemId = segment.ItemId, |
| 0 | 211 | | StartTicks = segment.StartTicks, |
| 0 | 212 | | Type = segment.Type, |
| 0 | 213 | | SegmentProviderId = segmentProviderId |
| 0 | 214 | | }; |
| | 215 | | } |
| | 216 | |
|
| | 217 | | /// <inheritdoc /> |
| | 218 | | public bool HasSegments(Guid itemId) |
| | 219 | | { |
| 0 | 220 | | using var db = _dbProvider.CreateDbContext(); |
| 0 | 221 | | return db.MediaSegments.Any(e => e.ItemId.Equals(itemId)); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | /// <inheritdoc/> |
| | 225 | | public bool IsTypeSupported(BaseItem baseItem) |
| | 226 | | { |
| 0 | 227 | | return baseItem.MediaType is Data.Enums.MediaType.Video or Data.Enums.MediaType.Audio; |
| | 228 | | } |
| | 229 | |
|
| | 230 | | /// <inheritdoc/> |
| | 231 | | public IEnumerable<(string Name, string Id)> GetSupportedProviders(BaseItem item) |
| | 232 | | { |
| 0 | 233 | | if (item is not (Video or Audio)) |
| | 234 | | { |
| 0 | 235 | | return []; |
| | 236 | | } |
| | 237 | |
|
| 0 | 238 | | return _segmentProviders |
| 0 | 239 | | .Select(p => (p.Name, GetProviderId(p.Name))); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | private string GetProviderId(string name) |
| 0 | 243 | | => name.ToLowerInvariant() |
| 0 | 244 | | .GetMD5() |
| 0 | 245 | | .ToString("N", CultureInfo.InvariantCulture); |
| | 246 | | } |