LiveStreamHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Common.Extensions;
  10. using MediaBrowser.Controller.MediaEncoding;
  11. using MediaBrowser.Model.Dlna;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.MediaInfo;
  15. using MediaBrowser.Model.Serialization;
  16. using Microsoft.Extensions.Logging;
  17. namespace Emby.Server.Implementations.Library
  18. {
  19. public class LiveStreamHelper
  20. {
  21. private readonly IMediaEncoder _mediaEncoder;
  22. private readonly ILogger _logger;
  23. private IJsonSerializer _json;
  24. private IApplicationPaths _appPaths;
  25. public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger, IJsonSerializer json, IApplicationPaths appPaths)
  26. {
  27. _mediaEncoder = mediaEncoder;
  28. _logger = logger;
  29. _json = json;
  30. _appPaths = appPaths;
  31. }
  32. public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string cacheKey, bool addProbeDelay, CancellationToken cancellationToken)
  33. {
  34. var originalRuntime = mediaSource.RunTimeTicks;
  35. var now = DateTime.UtcNow;
  36. MediaInfo mediaInfo = null;
  37. var cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", cacheKey.GetMD5().ToString("N") + ".json");
  38. if (!string.IsNullOrEmpty(cacheKey))
  39. {
  40. try
  41. {
  42. mediaInfo = _json.DeserializeFromFile<MediaInfo>(cacheFilePath);
  43. //_logger.LogDebug("Found cached media info");
  44. }
  45. catch
  46. {
  47. }
  48. }
  49. if (mediaInfo == null)
  50. {
  51. if (addProbeDelay)
  52. {
  53. var delayMs = mediaSource.AnalyzeDurationMs ?? 0;
  54. delayMs = Math.Max(3000, delayMs);
  55. if (delayMs > 0)
  56. {
  57. _logger.LogInformation("Waiting {0}ms before probing the live stream", delayMs);
  58. await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
  59. }
  60. }
  61. mediaSource.AnalyzeDurationMs = 3000;
  62. mediaInfo = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
  63. {
  64. MediaSource = mediaSource,
  65. MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
  66. ExtractChapters = false
  67. }, cancellationToken).ConfigureAwait(false);
  68. if (cacheFilePath != null)
  69. {
  70. Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
  71. _json.SerializeToFile(mediaInfo, cacheFilePath);
  72. //_logger.LogDebug("Saved media info to {0}", cacheFilePath);
  73. }
  74. }
  75. var mediaStreams = mediaInfo.MediaStreams;
  76. if (!string.IsNullOrEmpty(cacheKey))
  77. {
  78. var newList = new List<MediaStream>();
  79. newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1));
  80. newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1));
  81. foreach (var stream in newList)
  82. {
  83. stream.Index = -1;
  84. stream.Language = null;
  85. }
  86. mediaStreams = newList;
  87. }
  88. _logger.LogInformation("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
  89. mediaSource.Bitrate = mediaInfo.Bitrate;
  90. mediaSource.Container = mediaInfo.Container;
  91. mediaSource.Formats = mediaInfo.Formats;
  92. mediaSource.MediaStreams = mediaStreams;
  93. mediaSource.RunTimeTicks = mediaInfo.RunTimeTicks;
  94. mediaSource.Size = mediaInfo.Size;
  95. mediaSource.Timestamp = mediaInfo.Timestamp;
  96. mediaSource.Video3DFormat = mediaInfo.Video3DFormat;
  97. mediaSource.VideoType = mediaInfo.VideoType;
  98. mediaSource.DefaultSubtitleStreamIndex = null;
  99. // Null this out so that it will be treated like a live stream
  100. if (!originalRuntime.HasValue)
  101. {
  102. mediaSource.RunTimeTicks = null;
  103. }
  104. var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
  105. if (audioStream == null || audioStream.Index == -1)
  106. {
  107. mediaSource.DefaultAudioStreamIndex = null;
  108. }
  109. else
  110. {
  111. mediaSource.DefaultAudioStreamIndex = audioStream.Index;
  112. }
  113. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
  114. if (videoStream != null)
  115. {
  116. if (!videoStream.BitRate.HasValue)
  117. {
  118. var width = videoStream.Width ?? 1920;
  119. if (width >= 3000)
  120. {
  121. videoStream.BitRate = 30000000;
  122. }
  123. else if (width >= 1900)
  124. {
  125. videoStream.BitRate = 20000000;
  126. }
  127. else if (width >= 1200)
  128. {
  129. videoStream.BitRate = 8000000;
  130. }
  131. else if (width >= 700)
  132. {
  133. videoStream.BitRate = 2000000;
  134. }
  135. }
  136. // This is coming up false and preventing stream copy
  137. videoStream.IsAVC = null;
  138. }
  139. mediaSource.AnalyzeDurationMs = 3000;
  140. // Try to estimate this
  141. mediaSource.InferTotalBitrate(true);
  142. }
  143. public Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, bool addProbeDelay, CancellationToken cancellationToken)
  144. {
  145. return AddMediaInfoWithProbe(mediaSource, isAudio, null, addProbeDelay, cancellationToken);
  146. }
  147. }
  148. }