aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs
blob: 5b0ea686d1c9a74230afd31e188d234059ef1c93 (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
using System;
using Jellyfin.MediaEncoding.Hls.Cache;
using Jellyfin.MediaEncoding.Hls.Extractors;
using Jellyfin.MediaEncoding.Hls.Playlist;
using Microsoft.Extensions.DependencyInjection;

namespace Jellyfin.MediaEncoding.Hls.Extensions;

/// <summary>
/// Extensions for the <see cref="IServiceCollection"/> interface.
/// </summary>
public static class MediaEncodingHlsServiceCollectionExtensions
{
    /// <summary>
    /// Adds the hls playlist generators to the <see cref="IServiceCollection"/>.
    /// </summary>
    /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param>
    /// <returns>The updated service collection.</returns>
    public static IServiceCollection AddHlsPlaylistGenerator(this IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingletonWithDecorator(typeof(FfProbeKeyframeExtractor));
        serviceCollection.AddSingletonWithDecorator(typeof(MatroskaKeyframeExtractor));
        serviceCollection.AddSingleton<IDynamicHlsPlaylistGenerator, DynamicHlsPlaylistGenerator>();
        return serviceCollection;
    }

    private static void AddSingletonWithDecorator(this IServiceCollection serviceCollection, Type type)
    {
        serviceCollection.AddSingleton<IKeyframeExtractor>(serviceProvider =>
        {
            var extractor = ActivatorUtilities.CreateInstance(serviceProvider, type);
            var decorator = ActivatorUtilities.CreateInstance<CacheDecorator>(serviceProvider, extractor);
            return decorator;
        });
    }
}