LiveStreamHelper.cs 6.4 KB

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