LiveTvMediaSourceProvider.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.MediaInfo;
  14. using Microsoft.Extensions.Logging;
  15. namespace Emby.Server.Implementations.LiveTv
  16. {
  17. public class LiveTvMediaSourceProvider : IMediaSourceProvider
  18. {
  19. // Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
  20. private const char StreamIdDelimiter = '_';
  21. private readonly ILiveTvManager _liveTvManager;
  22. private readonly ILogger<LiveTvMediaSourceProvider> _logger;
  23. private readonly IMediaSourceManager _mediaSourceManager;
  24. private readonly IServerApplicationHost _appHost;
  25. public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, ILogger<LiveTvMediaSourceProvider> logger, IMediaSourceManager mediaSourceManager, IServerApplicationHost appHost)
  26. {
  27. _liveTvManager = liveTvManager;
  28. _logger = logger;
  29. _mediaSourceManager = mediaSourceManager;
  30. _appHost = appHost;
  31. }
  32. public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
  33. {
  34. if (item.SourceType == SourceType.LiveTV)
  35. {
  36. var activeRecordingInfo = _liveTvManager.GetActiveRecordingInfo(item.Path);
  37. if (string.IsNullOrEmpty(item.Path) || activeRecordingInfo != null)
  38. {
  39. return GetMediaSourcesInternal(item, activeRecordingInfo, cancellationToken);
  40. }
  41. }
  42. return Task.FromResult(Enumerable.Empty<MediaSourceInfo>());
  43. }
  44. private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(BaseItem item, ActiveRecordingInfo activeRecordingInfo, CancellationToken cancellationToken)
  45. {
  46. IEnumerable<MediaSourceInfo> sources;
  47. var forceRequireOpening = false;
  48. try
  49. {
  50. if (activeRecordingInfo != null)
  51. {
  52. sources = await EmbyTV.EmbyTV.Current.GetRecordingStreamMediaSources(activeRecordingInfo, cancellationToken)
  53. .ConfigureAwait(false);
  54. }
  55. else
  56. {
  57. sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
  58. .ConfigureAwait(false);
  59. }
  60. }
  61. catch (NotImplementedException)
  62. {
  63. sources = _mediaSourceManager.GetStaticMediaSources(item, false);
  64. forceRequireOpening = true;
  65. }
  66. var list = sources.ToList();
  67. var serverUrl = await _appHost.GetLocalApiUrl(cancellationToken).ConfigureAwait(false);
  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 = serverUrl;
  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. }