LiveStreamHelper.cs 3.7 KB

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