LiveTvMediaSourceProvider.cs 5.2 KB

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