Ejemplo n.º 1
0
        async void _device_PlaybackProgress(object sender, PlaybackProgressEventArgs e)
        {
            try
            {
                var info = StreamParams.ParseFromUrl(e.MediaInfo.Url, _libraryManager, _mediaSourceManager);

                if (info.Item != null)
                {
                    var progress = GetProgressInfo(e.MediaInfo, info);

                    await _sessionManager.OnPlaybackProgress(progress).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error reporting progress", ex);
            }
        }
Ejemplo n.º 2
0
        private async Task Seek(long newPosition)
        {
            var media = _device.CurrentMediaInfo;

            if (media != null)
            {
                var info = StreamParams.ParseFromUrl(media.Url, _libraryManager, _mediaSourceManager);

                if (info.Item != null && !EnableClientSideSeek(info))
                {
                    var user    = _session.UserId.HasValue ? _userManager.GetUserById(_session.UserId.Value) : null;
                    var newItem = CreatePlaylistItem(info.Item, user, newPosition, info.MediaSourceId, info.AudioStreamIndex, info.SubtitleStreamIndex);

                    await _device.SetAvTransport(newItem.StreamUrl, GetDlnaHeaders(newItem), newItem.Didl).ConfigureAwait(false);

                    return;
                }
                await SeekAfterTransportChange(newPosition).ConfigureAwait(false);
            }
        }
Ejemplo n.º 3
0
        private async void OnDeviceMediaChanged(object sender, MediaChangedEventArgs e)
        {
            if (_disposed || string.IsNullOrEmpty(e.OldMediaInfo.Url))
            {
                return;
            }

            try
            {
                var streamInfo = StreamParams.ParseFromUrl(e.OldMediaInfo.Url, _libraryManager, _mediaSourceManager);
                if (streamInfo.Item != null)
                {
                    var positionTicks = GetProgressPositionTicks(streamInfo);

                    await ReportPlaybackStopped(streamInfo, positionTicks).ConfigureAwait(false);
                }

                streamInfo = StreamParams.ParseFromUrl(e.NewMediaInfo.Url, _libraryManager, _mediaSourceManager);
                if (streamInfo.Item == null)
                {
                    return;
                }

                var newItemProgress = GetProgressInfo(streamInfo);

                await _sessionManager.OnPlaybackStart(newItemProgress).ConfigureAwait(false);

                // Send a message to the DLNA device to notify what is the next track in the playlist.
                var currentItemIndex = _playlist.FindIndex(item => item.StreamInfo.ItemId == streamInfo.ItemId);
                if (currentItemIndex >= 0)
                {
                    _currentPlaylistIndex = currentItemIndex;
                }

                await SendNextTrackMessage(currentItemIndex, CancellationToken.None);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error reporting progress");
            }
        }
Ejemplo n.º 4
0
        async void _device_MediaChanged(object sender, MediaChangedEventArgs e)
        {
            try
            {
                var streamInfo = StreamParams.ParseFromUrl(e.OldMediaInfo.Url, _libraryManager);
                var progress   = GetProgressInfo(e.OldMediaInfo, streamInfo);

                var positionTicks = progress.PositionTicks;

                ReportPlaybackStopped(e.OldMediaInfo, streamInfo, positionTicks);

                streamInfo = StreamParams.ParseFromUrl(e.NewMediaInfo.Url, _libraryManager);
                progress   = GetProgressInfo(e.NewMediaInfo, streamInfo);

                await _sessionManager.OnPlaybackStart(progress).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error reporting progress", ex);
            }
        }
Ejemplo n.º 5
0
        async void _device_PlaybackStopped(object sender, PlaybackStoppedEventArgs e)
        {
            try
            {
                var streamInfo = StreamParams.ParseFromUrl(e.MediaInfo.Url, _libraryManager);
                var progress   = GetProgressInfo(e.MediaInfo, streamInfo);

                var positionTicks = progress.PositionTicks;

                ReportPlaybackStopped(e.MediaInfo, streamInfo, positionTicks);

                var duration = streamInfo.MediaSource == null ?
                               (_device.Duration == null ? (long?)null : _device.Duration.Value.Ticks) :
                               streamInfo.MediaSource.RunTimeTicks;

                var playedToCompletion = (positionTicks.HasValue && positionTicks.Value == 0);

                if (!playedToCompletion && duration.HasValue && positionTicks.HasValue)
                {
                    double percent = positionTicks.Value;
                    percent /= duration.Value;

                    playedToCompletion = Math.Abs(1 - percent) <= .1;
                }

                if (playedToCompletion)
                {
                    await SetPlaylistIndex(_currentPlaylistIndex + 1).ConfigureAwait(false);
                }
                else
                {
                    Playlist.Clear();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error reporting playback stopped", ex);
            }
        }
Ejemplo n.º 6
0
        private async void OnDevicePlaybackStart(object sender, PlaybackStartEventArgs e)
        {
            if (_disposed)
            {
                return;
            }

            try
            {
                var info = StreamParams.ParseFromUrl(e.MediaInfo.Url, _libraryManager, _mediaSourceManager);

                if (info.Item != null)
                {
                    var progress = GetProgressInfo(info);

                    await _sessionManager.OnPlaybackStart(progress).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error reporting progress");
            }
        }
Ejemplo n.º 7
0
        async void _device_MediaChanged(object sender, MediaChangedEventArgs e)
        {
            if (_disposed)
            {
                return;
            }

            try
            {
                var streamInfo = await StreamParams.ParseFromUrl(e.OldMediaInfo.Url, _libraryManager, _mediaSourceManager).ConfigureAwait(false);

                if (streamInfo.Item != null)
                {
                    var progress = GetProgressInfo(e.OldMediaInfo, streamInfo);

                    var positionTicks = progress.PositionTicks;

                    ReportPlaybackStopped(e.OldMediaInfo, streamInfo, positionTicks);
                }

                streamInfo = await StreamParams.ParseFromUrl(e.NewMediaInfo.Url, _libraryManager, _mediaSourceManager).ConfigureAwait(false);

                if (streamInfo.Item == null)
                {
                    return;
                }

                var newItemProgress = GetProgressInfo(e.NewMediaInfo, streamInfo);

                await _sessionManager.OnPlaybackStart(newItemProgress).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error reporting progress", ex);
            }
        }
Ejemplo n.º 8
0
        private async Task SetSubtitleStreamIndex(int?newIndex)
        {
            var media = _device.CurrentMediaInfo;

            if (media != null)
            {
                var info = StreamParams.ParseFromUrl(media.Url, _libraryManager, _mediaSourceManager);

                if (info.Item != null)
                {
                    var newPosition = GetProgressPositionTicks(media, info) ?? 0;

                    var user    = !_session.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(_session.UserId) : null;
                    var newItem = CreatePlaylistItem(info.Item, user, newPosition, info.MediaSourceId, info.AudioStreamIndex, newIndex);

                    await _device.SetAvTransport(newItem.StreamUrl, GetDlnaHeaders(newItem), newItem.Didl, CancellationToken.None).ConfigureAwait(false);

                    if (EnableClientSideSeek(newItem.StreamInfo) && newPosition > 0)
                    {
                        await SeekAfterTransportChange(newPosition, CancellationToken.None).ConfigureAwait(false);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        private async Task Seek(long newPosition)
        {
            var media = _device.CurrentMediaInfo;

            if (media != null)
            {
                var info = StreamParams.ParseFromUrl(media.Url, _libraryManager);

                if (info.Item != null && !info.IsDirectStream)
                {
                    var user    = _session.UserId.HasValue ? _userManager.GetUserById(_session.UserId.Value) : null;
                    var newItem = CreatePlaylistItem(info.Item, user, newPosition, GetServerAddress(), info.MediaSourceId, info.AudioStreamIndex, info.SubtitleStreamIndex);

                    await _device.SetAvTransport(newItem.StreamUrl, GetDlnaHeaders(newItem), newItem.Didl).ConfigureAwait(false);

                    if (newItem.StreamInfo.IsDirectStream)
                    {
                        await _device.Seek(TimeSpan.FromTicks(newPosition)).ConfigureAwait(false);
                    }
                    return;
                }
                await _device.Seek(TimeSpan.FromTicks(newPosition)).ConfigureAwait(false);
            }
        }
Ejemplo n.º 10
0
        private PlaybackStartInfo GetProgressInfo(uBaseObject mediaInfo)
        {
            var info = StreamParams.ParseFromUrl(mediaInfo.Url, _libraryManager);

            return(GetProgressInfo(mediaInfo, info));
        }