From 961f48f5bcc8e4f1e0c3ac5e3c2757bbd14b6a9d Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Sun, 5 Apr 2020 12:45:01 -0400 Subject: [PATCH] Use ? and ?? where applicable --- MediaBrowser.Api/ApiEntryPoint.cs | 7 ++-- MediaBrowser.Api/BaseApiService.cs | 36 +++++++------------ MediaBrowser.Api/ItemUpdateService.cs | 1 - .../Library/LibraryStructureService.cs | 12 +++---- .../Playback/BaseStreamingService.cs | 19 ++-------- .../Playback/Hls/BaseHlsService.cs | 4 +-- .../Playback/Hls/DynamicHlsService.cs | 4 +-- MediaBrowser.Api/Playback/MediaInfoService.cs | 2 +- .../Playback/UniversalAudioService.cs | 2 +- MediaBrowser.Api/SearchService.cs | 10 ++---- MediaBrowser.Api/TranscodingJob.cs | 5 +-- MediaBrowser.Api/TvShowsService.cs | 9 +---- MediaBrowser.Api/VideosService.cs | 33 +++++------------ 13 files changed, 42 insertions(+), 102 deletions(-) diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index 4bd13df003..2215bb5c40 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -258,7 +258,7 @@ namespace MediaBrowser.Api public void ReportTranscodingProgress(TranscodingJob job, StreamState state, TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate) { - var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null; + var ticks = transcodingPosition?.Ticks; if (job != null) { @@ -561,10 +561,7 @@ namespace MediaBrowser.Api lock (job.ProcessLock) { - if (job.TranscodingThrottler != null) - { - job.TranscodingThrottler.Stop().GetAwaiter().GetResult(); - } + job.TranscodingThrottler?.Stop().GetAwaiter().GetResult(); var process = job.Process; diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 112ee8f79a..154177dca1 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -274,36 +274,26 @@ namespace MediaBrowser.Api private T GetItemFromSlugName(ILibraryManager libraryManager, string name, DtoOptions dtoOptions) where T : BaseItem, new() { - var result = libraryManager.GetItemList(new InternalItemsQuery + var result = (libraryManager.GetItemList(new InternalItemsQuery { Name = name.Replace(BaseItem.SlugChar, '&'), IncludeItemTypes = new[] { typeof(T).Name }, DtoOptions = dtoOptions + }).OfType().FirstOrDefault() ?? libraryManager.GetItemList(new InternalItemsQuery + { + Name = name.Replace(BaseItem.SlugChar, '/'), + IncludeItemTypes = new[] { typeof(T).Name }, + DtoOptions = dtoOptions + + }).OfType().FirstOrDefault()) ?? libraryManager.GetItemList(new InternalItemsQuery + { + Name = name.Replace(BaseItem.SlugChar, '?'), + IncludeItemTypes = new[] { typeof(T).Name }, + DtoOptions = dtoOptions + }).OfType().FirstOrDefault(); - if (result == null) - { - result = libraryManager.GetItemList(new InternalItemsQuery - { - Name = name.Replace(BaseItem.SlugChar, '/'), - IncludeItemTypes = new[] { typeof(T).Name }, - DtoOptions = dtoOptions - - }).OfType().FirstOrDefault(); - } - - if (result == null) - { - result = libraryManager.GetItemList(new InternalItemsQuery - { - Name = name.Replace(BaseItem.SlugChar, '?'), - IncludeItemTypes = new[] { typeof(T).Name }, - DtoOptions = dtoOptions - - }).OfType().FirstOrDefault(); - } - return result; } diff --git a/MediaBrowser.Api/ItemUpdateService.cs b/MediaBrowser.Api/ItemUpdateService.cs index 962e4ddf4b..2db6d717aa 100644 --- a/MediaBrowser.Api/ItemUpdateService.cs +++ b/MediaBrowser.Api/ItemUpdateService.cs @@ -391,7 +391,6 @@ namespace MediaBrowser.Api } return (SeriesStatus)Enum.Parse(typeof(SeriesStatus), item.Status, true); - } } } diff --git a/MediaBrowser.Api/Library/LibraryStructureService.cs b/MediaBrowser.Api/Library/LibraryStructureService.cs index c071b42f75..1e300814f6 100644 --- a/MediaBrowser.Api/Library/LibraryStructureService.cs +++ b/MediaBrowser.Api/Library/LibraryStructureService.cs @@ -327,15 +327,11 @@ namespace MediaBrowser.Api.Library try { - var mediaPath = request.PathInfo; - - if (mediaPath == null) + var mediaPath = request.PathInfo ?? new MediaPathInfo { - mediaPath = new MediaPathInfo - { - Path = request.Path - }; - } + Path = request.Path + }; + _libraryManager.AddMediaPath(request.Name, mediaPath); } finally diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index 5029ce0bb7..6e923768a2 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -248,14 +248,8 @@ namespace MediaBrowser.Api.Playback if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) { - if (string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) - { - logFilePrefix = "ffmpeg-remux"; - } - else - { - logFilePrefix = "ffmpeg-directstream"; - } + logFilePrefix = string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase) + ? "ffmpeg-remux" : "ffmpeg-directstream"; } var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt"); @@ -862,14 +856,7 @@ namespace MediaBrowser.Api.Playback { var caps = DeviceManager.GetCapabilities(state.Request.DeviceId); - if (caps != null) - { - state.DeviceProfile = caps.DeviceProfile; - } - else - { - state.DeviceProfile = DlnaManager.GetProfile(headers); - } + state.DeviceProfile = caps != null ? caps.DeviceProfile : DlnaManager.GetProfile(headers); } } diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs index af0097682a..566eaf86ea 100644 --- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs @@ -140,7 +140,7 @@ namespace MediaBrowser.Api.Playback.Hls if (isLive) { - job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType); + job ??= ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType); if (job != null) { @@ -156,7 +156,7 @@ namespace MediaBrowser.Api.Playback.Hls var playlistText = GetMasterPlaylistFileText(playlist, videoBitrate + audioBitrate, baselineStreamBitrate); - job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType); + job ??= ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType); if (job != null) { diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 8b8fcb05e6..20e18cc265 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -284,7 +284,7 @@ namespace MediaBrowser.Api.Playback.Hls //} Logger.LogDebug("returning {0} [general case]", segmentPath); - job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); + job ??= ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false); } @@ -934,7 +934,7 @@ namespace MediaBrowser.Api.Playback.Hls var framerate = state.VideoStream?.RealFrameRate; - if (framerate != null && framerate.HasValue) + if (framerate.HasValue) { // This is to make sure keyframe interval is limited to our segment, // as forcing keyframes is not enough. diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs index d74ec3ca63..5f8a86f477 100644 --- a/MediaBrowser.Api/Playback/MediaInfoService.cs +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -583,7 +583,7 @@ namespace MediaBrowser.Api.Playback private long? GetMaxBitrate(long? clientMaxBitrate, User user) { var maxBitrate = clientMaxBitrate; - var remoteClientMaxBitrate = user == null ? 0 : user.Policy.RemoteClientBitrateLimit; + var remoteClientMaxBitrate = user?.Policy.RemoteClientBitrateLimit ?? 0; if (remoteClientMaxBitrate <= 0) { diff --git a/MediaBrowser.Api/Playback/UniversalAudioService.cs b/MediaBrowser.Api/Playback/UniversalAudioService.cs index cbf981dfec..227a630eec 100644 --- a/MediaBrowser.Api/Playback/UniversalAudioService.cs +++ b/MediaBrowser.Api/Playback/UniversalAudioService.cs @@ -167,7 +167,7 @@ namespace MediaBrowser.Api.Playback AudioCodec = request.AudioCodec, Protocol = request.TranscodingProtocol, BreakOnNonKeyFrames = request.BreakOnNonKeyFrames, - MaxAudioChannels = request.TranscodingAudioChannels.HasValue ? request.TranscodingAudioChannels.Value.ToString(CultureInfo.InvariantCulture) : null + MaxAudioChannels = request.TranscodingAudioChannels?.ToString(CultureInfo.InvariantCulture) } }; diff --git a/MediaBrowser.Api/SearchService.cs b/MediaBrowser.Api/SearchService.cs index f1df01a096..c5b78828cb 100644 --- a/MediaBrowser.Api/SearchService.cs +++ b/MediaBrowser.Api/SearchService.cs @@ -279,7 +279,7 @@ namespace MediaBrowser.Api if (!item.ChannelId.Equals(Guid.Empty)) { var channel = _libraryManager.GetItemById(item.ChannelId); - result.ChannelName = channel == null ? null : channel.Name; + result.ChannelName = channel?.Name; } return result; @@ -316,12 +316,8 @@ namespace MediaBrowser.Api private void SetBackdropImageInfo(SearchHint hint, BaseItem item) { - var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null; - - if (itemWithImage == null) - { - itemWithImage = GetParentWithImage(item, ImageType.Backdrop); - } + var itemWithImage = (item.HasImage(ImageType.Backdrop) ? item : null) + ?? GetParentWithImage(item, ImageType.Backdrop); if (itemWithImage != null) { diff --git a/MediaBrowser.Api/TranscodingJob.cs b/MediaBrowser.Api/TranscodingJob.cs index 6d944d19ea..8c24e3ce18 100644 --- a/MediaBrowser.Api/TranscodingJob.cs +++ b/MediaBrowser.Api/TranscodingJob.cs @@ -92,10 +92,7 @@ namespace MediaBrowser.Api { lock (_timerLock) { - if (KillTimer != null) - { - KillTimer.Change(Timeout.Infinite, Timeout.Infinite); - } + KillTimer?.Change(Timeout.Infinite, Timeout.Infinite); } } diff --git a/MediaBrowser.Api/TvShowsService.cs b/MediaBrowser.Api/TvShowsService.cs index 1282bba8ae..cd8e8dfbe4 100644 --- a/MediaBrowser.Api/TvShowsService.cs +++ b/MediaBrowser.Api/TvShowsService.cs @@ -442,14 +442,7 @@ namespace MediaBrowser.Api var season = series.GetSeasons(user, dtoOptions).FirstOrDefault(i => i.IndexNumber == request.Season.Value); - if (season == null) - { - episodes = new List(); - } - else - { - episodes = ((Season)season).GetEpisodes(user, dtoOptions); - } + episodes = season == null ? new List() : ((Season)season).GetEpisodes(user, dtoOptions); } else { diff --git a/MediaBrowser.Api/VideosService.cs b/MediaBrowser.Api/VideosService.cs index 46b6d5a947..9d7dc2dfc6 100644 --- a/MediaBrowser.Api/VideosService.cs +++ b/MediaBrowser.Api/VideosService.cs @@ -138,32 +138,17 @@ namespace MediaBrowser.Api var videosWithVersions = items.Where(i => i.MediaSourceCount > 1) .ToList(); - var primaryVersion = videosWithVersions.FirstOrDefault(); + var primaryVersion = videosWithVersions.FirstOrDefault() ?? items.OrderBy(i => + { + return (i.Video3DFormat.HasValue || i.VideoType != Model.Entities.VideoType.VideoFile) ? 1 : 0; + }) + .ThenByDescending(i => + { + var stream = i.GetDefaultVideoStream(); - if (primaryVersion == null) - { - primaryVersion = items.OrderBy(i => - { - if (i.Video3DFormat.HasValue) - { - return 1; - } + return stream?.Width ?? 0; - if (i.VideoType != Model.Entities.VideoType.VideoFile) - { - return 1; - } - - return 0; - }) - .ThenByDescending(i => - { - var stream = i.GetDefaultVideoStream(); - - return stream == null || stream.Width == null ? 0 : stream.Width.Value; - - }).First(); - } + }).First(); var list = primaryVersion.LinkedAlternateVersions.ToList();