/// <summary>
        /// Gets the audio stream information.
        /// </summary>
        /// <param name="serverId">The server identifier.</param>
        /// <param name="options">The options.</param>
        /// <returns>Task&lt;StreamInfo&gt;.</returns>
        public async Task <StreamInfo> GetAudioStreamInfo(string serverId, AudioOptions options)
        {
            var streamBuilder = new StreamBuilder();

            var localItem = await _localAssetManager.GetLocalItem(serverId, options.ItemId);

            if (localItem != null)
            {
                var localMediaSource = localItem.Item.MediaSources[0];

                // Use the local media source, unless a specific server media source was requested
                if (string.IsNullOrWhiteSpace(options.MediaSourceId) ||
                    string.Equals(localMediaSource.Id, options.MediaSourceId,
                                  StringComparison.OrdinalIgnoreCase))
                {
                    // Finally, check to make sure the local file is actually available at this time
                    var fileExists = await _localAssetManager.FileExists(localMediaSource.Path).ConfigureAwait(false);

                    if (fileExists)
                    {
                        options.MediaSources = localItem.Item.MediaSources;

                        var result = streamBuilder.BuildAudioItem(options);
                        result.PlayMethod = PlayMethod.DirectPlay;
                        return(result);
                    }
                }
            }

            return(streamBuilder.BuildAudioItem(options));
        }
Ejemplo n.º 2
0
        private async Task SyncData(IApiClient apiClient,
                                    ServerInfo serverInfo,
                                    bool syncUserItemAccess,
                                    CancellationToken cancellationToken)
        {
            var localIds = await _localAssetManager.GetServerItemIds(serverInfo.Id).ConfigureAwait(false);

            var result = await apiClient.SyncData(new SyncDataRequest
            {
                TargetId       = apiClient.DeviceId,
                LocalItemIds   = localIds,
                OfflineUserIds = serverInfo.Users.Select(i => i.Id).ToList()
            }).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            foreach (var itemIdToRemove in result.ItemIdsToRemove)
            {
                try
                {
                    await RemoveItem(serverInfo.Id, itemIdToRemove).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error deleting item from device. Id: {0}", ex, itemIdToRemove);
                }
            }

            if (syncUserItemAccess)
            {
                foreach (var item in result.ItemUserAccess)
                {
                    var itemid = item.Key;

                    var localItem = await _localAssetManager.GetLocalItem(serverInfo.Id, itemid).ConfigureAwait(false);

                    if (!localItem.UserIdsWithAccess.SequenceEqual(item.Value, StringComparer.OrdinalIgnoreCase))
                    {
                        localItem.UserIdsWithAccess = item.Value;
                        await _localAssetManager.AddOrUpdate(localItem).ConfigureAwait(false);
                    }
                }
            }
        }