LiveStreamHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Model.Dlna;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. namespace Emby.Server.Implementations.LiveTv
  12. {
  13. public class LiveStreamHelper
  14. {
  15. private readonly IMediaEncoder _mediaEncoder;
  16. private readonly ILogger _logger;
  17. const int AnalyzeDurationMs = 2000;
  18. public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger)
  19. {
  20. _mediaEncoder = mediaEncoder;
  21. _logger = logger;
  22. }
  23. public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
  24. {
  25. var originalRuntime = mediaSource.RunTimeTicks;
  26. var now = DateTime.UtcNow;
  27. var allowVideoStreamCopy = mediaSource.MediaStreams.Any(i => i.Type == MediaStreamType.Video && i.AllowStreamCopy);
  28. var info = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
  29. {
  30. InputPath = mediaSource.Path,
  31. Protocol = mediaSource.Protocol,
  32. MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
  33. ExtractChapters = false,
  34. AnalyzeDurationMs = AnalyzeDurationMs
  35. }, cancellationToken).ConfigureAwait(false);
  36. _logger.Info("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
  37. mediaSource.Bitrate = info.Bitrate;
  38. mediaSource.Container = info.Container;
  39. mediaSource.Formats = info.Formats;
  40. mediaSource.MediaStreams = info.MediaStreams;
  41. mediaSource.RunTimeTicks = info.RunTimeTicks;
  42. mediaSource.Size = info.Size;
  43. mediaSource.Timestamp = info.Timestamp;
  44. mediaSource.Video3DFormat = info.Video3DFormat;
  45. mediaSource.VideoType = info.VideoType;
  46. mediaSource.DefaultSubtitleStreamIndex = null;
  47. // Null this out so that it will be treated like a live stream
  48. if (!originalRuntime.HasValue)
  49. {
  50. mediaSource.RunTimeTicks = null;
  51. }
  52. var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
  53. if (audioStream == null || audioStream.Index == -1)
  54. {
  55. mediaSource.DefaultAudioStreamIndex = null;
  56. }
  57. else
  58. {
  59. mediaSource.DefaultAudioStreamIndex = audioStream.Index;
  60. }
  61. var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
  62. if (videoStream != null)
  63. {
  64. videoStream.AllowStreamCopy = allowVideoStreamCopy;
  65. if (!videoStream.BitRate.HasValue)
  66. {
  67. var width = videoStream.Width ?? 1920;
  68. if (width >= 3000)
  69. {
  70. videoStream.BitRate = 25000000;
  71. }
  72. else if (width >= 1900)
  73. {
  74. videoStream.BitRate = 15000000;
  75. }
  76. else if (width >= 1200)
  77. {
  78. videoStream.BitRate = 4000000;
  79. }
  80. else if (width >= 700)
  81. {
  82. videoStream.BitRate = 1500000;
  83. }
  84. }
  85. // This is coming up false and preventing stream copy
  86. videoStream.IsAVC = null;
  87. }
  88. // Try to estimate this
  89. mediaSource.InferTotalBitrate(true);
  90. mediaSource.AnalyzeDurationMs = AnalyzeDurationMs;
  91. }
  92. }
  93. }