/// <summary> /// Get playback info. /// </summary> /// <param name="id">Item id.</param> /// <param name="userId">User Id.</param> /// <param name="mediaSourceId">Media source id.</param> /// <param name="liveStreamId">Live stream id.</param> /// <returns>A <see cref="Task"/> containing the <see cref="PlaybackInfoResponse"/>.</returns> public async Task <PlaybackInfoResponse> GetPlaybackInfo( Guid id, Guid?userId, string?mediaSourceId = null, string?liveStreamId = null) { var user = userId.HasValue && !userId.Equals(Guid.Empty) ? _userManager.GetUserById(userId.Value) : null; var item = _libraryManager.GetItemById(id); var result = new PlaybackInfoResponse(); MediaSourceInfo[] mediaSources; if (string.IsNullOrWhiteSpace(liveStreamId)) { // TODO (moved from MediaBrowser.Api) handle supportedLiveMediaTypes? var mediaSourcesList = await _mediaSourceManager.GetPlaybackMediaSources(item, user, true, true, CancellationToken.None).ConfigureAwait(false); if (string.IsNullOrWhiteSpace(mediaSourceId)) { mediaSources = mediaSourcesList.ToArray(); } else { mediaSources = mediaSourcesList .Where(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)) .ToArray(); } } else { var mediaSource = await _mediaSourceManager.GetLiveStream(liveStreamId, CancellationToken.None).ConfigureAwait(false); mediaSources = new[] { mediaSource }; } if (mediaSources.Length == 0) { result.MediaSources = Array.Empty <MediaSourceInfo>(); result.ErrorCode ??= PlaybackErrorCode.NoCompatibleStream; } else { // Since we're going to be setting properties on MediaSourceInfos that come out of _mediaSourceManager, we should clone it // Should we move this directly into MediaSourceManager? var mediaSourcesClone = JsonSerializer.Deserialize <MediaSourceInfo[]>(JsonSerializer.SerializeToUtf8Bytes(mediaSources)); if (mediaSourcesClone != null) { result.MediaSources = mediaSourcesClone; } result.PlaySessionId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); } return(result); }
private async Task <PlaybackInfoResponse> GetPlaybackInfo(Guid id, Guid userId, string[] supportedLiveMediaTypes, string mediaSourceId = null, string liveStreamId = null) { var user = _userManager.GetUserById(userId); var item = _libraryManager.GetItemById(id); var result = new PlaybackInfoResponse(); if (string.IsNullOrWhiteSpace(liveStreamId)) { IEnumerable <MediaSourceInfo> mediaSources; try { // TODO handle supportedLiveMediaTypes ? mediaSources = await _mediaSourceManager.GetPlayackMediaSources(item, user, true, false, CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { mediaSources = new List <MediaSourceInfo>(); _logger.LogError(ex, "Could not find media sources for item id {id}", id); // TODO PlaybackException ?? //result.ErrorCode = ex.ErrorCode; } result.MediaSources = mediaSources.ToArray(); if (!string.IsNullOrWhiteSpace(mediaSourceId)) { result.MediaSources = result.MediaSources .Where(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)) .ToArray(); } } else { var mediaSource = await _mediaSourceManager.GetLiveStream(liveStreamId, CancellationToken.None).ConfigureAwait(false); result.MediaSources = new MediaSourceInfo[] { mediaSource }; } if (result.MediaSources.Length == 0) { if (!result.ErrorCode.HasValue) { result.ErrorCode = PlaybackErrorCode.NoCompatibleStream; } } else { result.MediaSources = Clone(result.MediaSources); result.PlaySessionId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); } return(result); }
private async Task <PlaybackInfoResponse> GetPlaybackInfo(string id, string userId, string[] supportedLiveMediaTypes, string mediaSourceId = null, string liveStreamId = null) { var result = new PlaybackInfoResponse(); if (string.IsNullOrWhiteSpace(liveStreamId)) { IEnumerable <MediaSourceInfo> mediaSources; try { mediaSources = await _mediaSourceManager.GetPlayackMediaSources(id, userId, true, supportedLiveMediaTypes, CancellationToken.None).ConfigureAwait(false); } catch (PlaybackException ex) { mediaSources = new List <MediaSourceInfo>(); result.ErrorCode = ex.ErrorCode; } result.MediaSources = mediaSources.ToList(); if (!string.IsNullOrWhiteSpace(mediaSourceId)) { result.MediaSources = result.MediaSources .Where(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)) .ToList(); } } else { var mediaSource = await _mediaSourceManager.GetLiveStream(liveStreamId, CancellationToken.None).ConfigureAwait(false); result.MediaSources = new List <MediaSourceInfo> { mediaSource }; } if (result.MediaSources.Count == 0) { if (!result.ErrorCode.HasValue) { result.ErrorCode = PlaybackErrorCode.NoCompatibleStream; } } else { result.MediaSources = Clone(result.MediaSources); result.PlaySessionId = Guid.NewGuid().ToString("N"); } return(result); }