mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-22 18:14:42 +01:00
update live stream generation
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
@@ -8,13 +7,14 @@ using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.MediaInfo;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Server.Implementations.LiveTv;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Library
|
||||
{
|
||||
@@ -23,16 +23,18 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
private readonly IItemRepository _itemRepo;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
|
||||
private IMediaSourceProvider[] _providers;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public MediaSourceManager(IItemRepository itemRepo, IUserManager userManager, ILibraryManager libraryManager, ILogger logger)
|
||||
public MediaSourceManager(IItemRepository itemRepo, IUserManager userManager, ILibraryManager libraryManager, ILogger logger, IJsonSerializer jsonSerializer)
|
||||
{
|
||||
_itemRepo = itemRepo;
|
||||
_userManager = userManager;
|
||||
_libraryManager = libraryManager;
|
||||
_logger = logger;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
}
|
||||
|
||||
public void AddParts(IEnumerable<IMediaSourceProvider> providers)
|
||||
@@ -317,13 +319,13 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
private readonly ConcurrentDictionary<string, LiveStreamInfo> _openStreams = new ConcurrentDictionary<string, LiveStreamInfo>();
|
||||
private readonly SemaphoreSlim _liveStreamSemaphore = new SemaphoreSlim(1, 1);
|
||||
|
||||
public async Task<MediaSourceInfo> OpenLiveStream(string openToken, bool enableAutoClose, CancellationToken cancellationToken)
|
||||
public async Task<LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, bool enableAutoClose, CancellationToken cancellationToken)
|
||||
{
|
||||
await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
var tuple = GetProvider(openToken);
|
||||
var tuple = GetProvider(request.OpenToken);
|
||||
var provider = tuple.Item1;
|
||||
|
||||
var mediaSource = await provider.OpenMediaSource(tuple.Item2, cancellationToken).ConfigureAwait(false);
|
||||
@@ -344,12 +346,19 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
StartCloseTimer();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(mediaSource.TranscodingUrl))
|
||||
var json = _jsonSerializer.SerializeToString(mediaSource);
|
||||
var clone = _jsonSerializer.DeserializeFromString<MediaSourceInfo>(json);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.UserId))
|
||||
{
|
||||
mediaSource.TranscodingUrl += "&LiveStreamId=" + mediaSource.LiveStreamId;
|
||||
var user = _userManager.GetUserById(request.UserId);
|
||||
SetUserProperties(clone, user);
|
||||
}
|
||||
|
||||
return mediaSource;
|
||||
return new LiveStreamResponse
|
||||
{
|
||||
MediaSource = clone
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -14,6 +14,7 @@ using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Controller.Security;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.Devices;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Events;
|
||||
using MediaBrowser.Model.Library;
|
||||
@@ -304,13 +305,21 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<MediaSourceInfo> GetMediaSource(BaseItem item, string mediaSourceId)
|
||||
{
|
||||
var sources = await _mediaSourceManager.GetPlayackMediaSources(item.Id.ToString("N"), false, CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return sources.FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the now playing item id.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="info">The information.</param>
|
||||
/// <param name="libraryItem">The library item.</param>
|
||||
private void UpdateNowPlayingItem(SessionInfo session, PlaybackProgressInfo info, BaseItem libraryItem)
|
||||
private async Task UpdateNowPlayingItem(SessionInfo session, PlaybackProgressInfo info, BaseItem libraryItem)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(info.MediaSourceId))
|
||||
{
|
||||
@@ -319,29 +328,27 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(info.ItemId) && info.Item == null && libraryItem != null)
|
||||
{
|
||||
var runtimeTicks = libraryItem.RunTimeTicks;
|
||||
|
||||
if (!string.Equals(info.ItemId, info.MediaSourceId) &&
|
||||
!string.IsNullOrWhiteSpace(info.MediaSourceId))
|
||||
{
|
||||
var runtimeItem = _libraryManager.GetItemById(new Guid(info.MediaSourceId)) ??
|
||||
_libraryManager.GetItemById(info.ItemId);
|
||||
|
||||
runtimeTicks = runtimeItem.RunTimeTicks;
|
||||
}
|
||||
|
||||
var current = session.NowPlayingItem;
|
||||
|
||||
if (current == null || !string.Equals(current.Id, info.ItemId, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
info.Item = GetItemInfo(libraryItem, libraryItem, info.MediaSourceId);
|
||||
var runtimeTicks = libraryItem.RunTimeTicks;
|
||||
|
||||
var mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId).ConfigureAwait(false);
|
||||
|
||||
if (mediaSource != null)
|
||||
{
|
||||
runtimeTicks = mediaSource.RunTimeTicks;
|
||||
}
|
||||
|
||||
info.Item = GetItemInfo(libraryItem, libraryItem, mediaSource);
|
||||
|
||||
info.Item.RunTimeTicks = runtimeTicks;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.Item = current;
|
||||
}
|
||||
|
||||
info.Item.RunTimeTicks = runtimeTicks;
|
||||
}
|
||||
|
||||
session.NowPlayingItem = info.Item;
|
||||
@@ -432,6 +439,12 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
|
||||
device = device ?? _deviceManager.GetDevice(deviceId);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
var userIdString = userId.HasValue ? userId.Value.ToString("N") : null;
|
||||
device = await _deviceManager.RegisterDevice(deviceId, deviceName, appName, appVersion, userIdString).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (device != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(device.CustomName))
|
||||
@@ -570,7 +583,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
? null
|
||||
: _libraryManager.GetItemById(new Guid(info.ItemId));
|
||||
|
||||
UpdateNowPlayingItem(session, info, libraryItem);
|
||||
await UpdateNowPlayingItem(session, info, libraryItem).ConfigureAwait(false);
|
||||
|
||||
if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
|
||||
{
|
||||
@@ -652,7 +665,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
? null
|
||||
: _libraryManager.GetItemById(new Guid(info.ItemId));
|
||||
|
||||
UpdateNowPlayingItem(session, info, libraryItem);
|
||||
await UpdateNowPlayingItem(session, info, libraryItem).ConfigureAwait(false);
|
||||
|
||||
var users = GetUsers(session);
|
||||
|
||||
@@ -731,7 +744,9 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
|
||||
if (current == null || !string.Equals(current.Id, info.ItemId, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
info.Item = GetItemInfo(libraryItem, libraryItem, info.MediaSourceId);
|
||||
var mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId).ConfigureAwait(false);
|
||||
|
||||
info.Item = GetItemInfo(libraryItem, libraryItem, mediaSource);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1439,10 +1454,10 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="chapterOwner">The chapter owner.</param>
|
||||
/// <param name="mediaSourceId">The media source identifier.</param>
|
||||
/// <param name="mediaSource">The media source.</param>
|
||||
/// <returns>BaseItemInfo.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
private BaseItemInfo GetItemInfo(BaseItem item, BaseItem chapterOwner, string mediaSourceId)
|
||||
private BaseItemInfo GetItemInfo(BaseItem item, BaseItem chapterOwner, MediaSourceInfo mediaSource)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
@@ -1593,9 +1608,9 @@ namespace MediaBrowser.Server.Implementations.Session
|
||||
info.Chapters = _dtoService.GetChapterInfoDtos(chapterOwner).ToList();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(mediaSourceId))
|
||||
if (mediaSource != null)
|
||||
{
|
||||
info.MediaStreams = _mediaSourceManager.GetMediaStreams(mediaSourceId).ToList();
|
||||
info.MediaStreams = mediaSource.MediaStreams;
|
||||
}
|
||||
|
||||
return info;
|
||||
|
||||
Reference in New Issue
Block a user