From 477a182efd632d83f8e2c481011fdfc96d82aa0f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 15 May 2015 22:36:47 -0400 Subject: [PATCH] adjust live tv stream selection --- .../LiveTv/ILiveTvManager.cs | 3 ++- .../LiveTv/LiveTvManager.cs | 21 ++++++++++++++----- .../LiveTv/LiveTvMediaSourceProvider.cs | 8 ++++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs index 4ee0565f9b..53eb18e7a8 100644 --- a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs +++ b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs @@ -158,9 +158,10 @@ namespace MediaBrowser.Controller.LiveTv /// Gets the channel stream. /// /// The identifier. + /// The media source identifier. /// The cancellation token. /// Task{StreamResponseInfo}. - Task GetChannelStream(string id, CancellationToken cancellationToken); + Task GetChannelStream(string id, string mediaSourceId, CancellationToken cancellationToken); /// /// Gets the program. diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index f60a219875..48ad3505a1 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -330,12 +330,12 @@ namespace MediaBrowser.Server.Implementations.LiveTv public async Task GetRecordingStream(string id, CancellationToken cancellationToken) { - return await GetLiveStream(id, false, cancellationToken).ConfigureAwait(false); + return await GetLiveStream(id, null, false, cancellationToken).ConfigureAwait(false); } - public async Task GetChannelStream(string id, CancellationToken cancellationToken) + public async Task GetChannelStream(string id, string mediaSourceId, CancellationToken cancellationToken) { - return await GetLiveStream(id, true, cancellationToken).ConfigureAwait(false); + return await GetLiveStream(id, mediaSourceId, true, cancellationToken).ConfigureAwait(false); } public async Task> GetRecordingMediaSources(string id, CancellationToken cancellationToken) @@ -364,7 +364,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv return _services.FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase)); } - private async Task GetLiveStream(string id, bool isChannel, CancellationToken cancellationToken) + private async Task GetLiveStream(string id, string mediaSourceId, bool isChannel, CancellationToken cancellationToken) { await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); @@ -379,7 +379,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv isVideo = channel.ChannelType == ChannelType.TV; var service = GetService(channel); _logger.Info("Opening channel stream from {0}, external channel Id: {1}", service.Name, channel.ExternalId); - info = await service.GetChannelStream(channel.ExternalId, null, cancellationToken).ConfigureAwait(false); + info = await service.GetChannelStream(channel.ExternalId, mediaSourceId, cancellationToken).ConfigureAwait(false); info.RequiresClosing = true; if (info.RequiresClosing) @@ -519,6 +519,17 @@ namespace MediaBrowser.Server.Implementations.LiveTv stream.Index = -1; } } + + // Set the total bitrate if not already supplied + if (!mediaSource.Bitrate.HasValue) + { + var total = mediaSource.MediaStreams.Select(i => i.BitRate ?? 0).Sum(); + + if (total > 0) + { + mediaSource.Bitrate = total; + } + } } private async Task GetChannel(ChannelInfo channelInfo, string serviceName, CancellationToken cancellationToken) diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs index 38c93a6964..46c186b0ac 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs @@ -84,6 +84,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv var openKeys = new List(); openKeys.Add(item.GetType().Name); openKeys.Add(item.Id.ToString("N")); + openKeys.Add(source.Id ?? string.Empty); source.OpenToken = string.Join("|", openKeys.ToArray()); } @@ -95,13 +96,14 @@ namespace MediaBrowser.Server.Implementations.LiveTv public async Task OpenMediaSource(string openToken, CancellationToken cancellationToken) { MediaSourceInfo stream; - var isAudio = false; + const bool isAudio = false; - var keys = openToken.Split(new[] { '|' }, 2); + var keys = openToken.Split(new[] { '|' }, 3); + var mediaSourceId = keys.Length >= 3 ? keys[2] : null; if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase)) { - stream = await _liveTvManager.GetChannelStream(keys[1], cancellationToken).ConfigureAwait(false); + stream = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, cancellationToken).ConfigureAwait(false); } else {