2
0

LiveTvMediaSourceProvider.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.LiveTv;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.MediaInfo;
  15. using Microsoft.Extensions.Logging;
  16. namespace Emby.Server.Implementations.LiveTv
  17. {
  18. public class LiveTvMediaSourceProvider : IMediaSourceProvider
  19. {
  20. // Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
  21. private const char StreamIdDelimiter = '_';
  22. private readonly ILiveTvManager _liveTvManager;
  23. private readonly ILogger<LiveTvMediaSourceProvider> _logger;
  24. private readonly IMediaSourceManager _mediaSourceManager;
  25. private readonly IServerApplicationHost _appHost;
  26. public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, ILogger<LiveTvMediaSourceProvider> logger, IMediaSourceManager mediaSourceManager, IServerApplicationHost appHost)
  27. {
  28. _liveTvManager = liveTvManager;
  29. _logger = logger;
  30. _mediaSourceManager = mediaSourceManager;
  31. _appHost = appHost;
  32. }
  33. public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
  34. {
  35. if (item.SourceType == SourceType.LiveTV)
  36. {
  37. var activeRecordingInfo = _liveTvManager.GetActiveRecordingInfo(item.Path);
  38. if (string.IsNullOrEmpty(item.Path) || activeRecordingInfo != null)
  39. {
  40. return GetMediaSourcesInternal(item, activeRecordingInfo, cancellationToken);
  41. }
  42. }
  43. return Task.FromResult(Enumerable.Empty<MediaSourceInfo>());
  44. }
  45. private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(BaseItem item, ActiveRecordingInfo activeRecordingInfo, CancellationToken cancellationToken)
  46. {
  47. IEnumerable<MediaSourceInfo> sources;
  48. var forceRequireOpening = false;
  49. try
  50. {
  51. if (activeRecordingInfo != null)
  52. {
  53. sources = await EmbyTV.EmbyTV.Current.GetRecordingStreamMediaSources(activeRecordingInfo, cancellationToken)
  54. .ConfigureAwait(false);
  55. }
  56. else
  57. {
  58. sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
  59. .ConfigureAwait(false);
  60. }
  61. }
  62. catch (NotImplementedException)
  63. {
  64. sources = _mediaSourceManager.GetStaticMediaSources(item, false);
  65. forceRequireOpening = true;
  66. }
  67. var list = sources.ToList();
  68. foreach (var source in list)
  69. {
  70. source.Type = MediaSourceType.Default;
  71. source.BufferMs ??= 1500;
  72. if (source.RequiresOpening || forceRequireOpening)
  73. {
  74. source.RequiresOpening = true;
  75. }
  76. if (source.RequiresOpening)
  77. {
  78. var openKeys = new List<string>
  79. {
  80. item.GetType().Name,
  81. item.Id.ToString("N", CultureInfo.InvariantCulture),
  82. source.Id ?? string.Empty
  83. };
  84. source.OpenToken = string.Join(StreamIdDelimiter, openKeys);
  85. }
  86. // Dummy this up so that direct play checks can still run
  87. if (string.IsNullOrEmpty(source.Path) && source.Protocol == MediaProtocol.Http)
  88. {
  89. source.Path = _appHost.GetSmartApiUrl(string.Empty);
  90. }
  91. }
  92. _logger.LogDebug("MediaSources: {@MediaSources}", list);
  93. return list;
  94. }
  95. /// <inheritdoc />
  96. public async Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
  97. {
  98. var keys = openToken.Split(StreamIdDelimiter, 3);
  99. var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
  100. var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
  101. var liveStream = info.Item2;
  102. return liveStream;
  103. }
  104. }
  105. }