LiveTvMediaSourceProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 StreamIdDelimeter = '_';
  21. private const string StreamIdDelimeterString = "_";
  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<IEnumerable<MediaSourceInfo>>(Array.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. var serverUrl = await _appHost.GetLocalApiUrl(cancellationToken).ConfigureAwait(false);
  69. foreach (var source in list)
  70. {
  71. source.Type = MediaSourceType.Default;
  72. source.BufferMs ??= 1500;
  73. if (source.RequiresOpening || forceRequireOpening)
  74. {
  75. source.RequiresOpening = true;
  76. }
  77. if (source.RequiresOpening)
  78. {
  79. var openKeys = new List<string>
  80. {
  81. item.GetType().Name,
  82. item.Id.ToString("N", CultureInfo.InvariantCulture),
  83. source.Id ?? string.Empty
  84. };
  85. source.OpenToken = string.Join(StreamIdDelimeterString, openKeys);
  86. }
  87. // Dummy this up so that direct play checks can still run
  88. if (string.IsNullOrEmpty(source.Path) && source.Protocol == MediaProtocol.Http)
  89. {
  90. source.Path = serverUrl;
  91. }
  92. }
  93. _logger.LogDebug("MediaSources: {@MediaSources}", list);
  94. return list;
  95. }
  96. /// <inheritdoc />
  97. public async Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
  98. {
  99. var keys = openToken.Split(new[] { StreamIdDelimeter }, 3);
  100. var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
  101. var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
  102. var liveStream = info.Item2;
  103. return liveStream;
  104. }
  105. }
  106. }