LiveStreamHelper.cs 3.5 KB

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