aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs
blob: 186bc499dbc7502034a308bb35ea10c429ac06a1 (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
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.Server.Implementations.LiveTv
{
    public class LiveTvMediaSourceProvider : IMediaSourceProvider
    {
        private readonly ILiveTvManager _liveTvManager;

        public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager)
        {
            _liveTvManager = liveTvManager;
        }

        public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
        {
            var channelItem = item as ILiveTvItem;

            if (channelItem != null)
            {
                var hasMetadata = (IHasMetadata)channelItem;

                if (string.IsNullOrWhiteSpace(hasMetadata.Path))
                {
                    return GetMediaSourcesInternal(channelItem, cancellationToken);
                }
            }

            return Task.FromResult<IEnumerable<MediaSourceInfo>>(new List<MediaSourceInfo>());
        }

        private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(ILiveTvItem item, CancellationToken cancellationToken)
        {
            var hasMediaSources = (IHasMediaSources)item;

            var sources = hasMediaSources.GetMediaSources(false)
                .ToList();

            foreach (var source in sources)
            {
                source.Type = MediaSourceType.Default;
                source.RequiresOpening = true;

                var openKeys = new List<string>();
                openKeys.Add(item.GetType().Name);
                openKeys.Add(item.Id.ToString("N"));
                source.OpenKey = string.Join("|", openKeys.ToArray());
            }

            return sources;
        }

        public async Task<MediaSourceInfo> OpenMediaSource(string openKey, CancellationToken cancellationToken)
        {
            var keys = openKey.Split(new[] { '|' }, 2);

            if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase))
            {
                return await _liveTvManager.GetChannelStream(keys[1], cancellationToken).ConfigureAwait(false);
            }

            return await _liveTvManager.GetRecordingStream(keys[1], cancellationToken).ConfigureAwait(false);
        }

        public Task CloseMediaSource(string closeKey, CancellationToken cancellationToken)
        {
            return _liveTvManager.CloseLiveStream(closeKey, cancellationToken);
        }
    }
}