Пример #1
0
        public async Task <ActionResult <LiveStreamResponse> > OpenLiveStream(
            [FromQuery] string?openToken,
            [FromQuery] Guid?userId,
            [FromQuery] string?playSessionId,
            [FromQuery] int?maxStreamingBitrate,
            [FromQuery] long?startTimeTicks,
            [FromQuery] int?audioStreamIndex,
            [FromQuery] int?subtitleStreamIndex,
            [FromQuery] int?maxAudioChannels,
            [FromQuery] Guid?itemId,
            [FromBody] OpenLiveStreamDto?openLiveStreamDto,
            [FromQuery] bool?enableDirectPlay,
            [FromQuery] bool?enableDirectStream)
        {
            var request = new LiveStreamRequest
            {
                OpenToken           = openToken ?? openLiveStreamDto?.OpenToken,
                UserId              = userId ?? openLiveStreamDto?.UserId ?? Guid.Empty,
                PlaySessionId       = playSessionId ?? openLiveStreamDto?.PlaySessionId,
                MaxStreamingBitrate = maxStreamingBitrate ?? openLiveStreamDto?.MaxStreamingBitrate,
                StartTimeTicks      = startTimeTicks ?? openLiveStreamDto?.StartTimeTicks,
                AudioStreamIndex    = audioStreamIndex ?? openLiveStreamDto?.AudioStreamIndex,
                SubtitleStreamIndex = subtitleStreamIndex ?? openLiveStreamDto?.SubtitleStreamIndex,
                MaxAudioChannels    = maxAudioChannels ?? openLiveStreamDto?.MaxAudioChannels,
                ItemId              = itemId ?? openLiveStreamDto?.ItemId ?? Guid.Empty,
                DeviceProfile       = openLiveStreamDto?.DeviceProfile,
                EnableDirectPlay    = enableDirectPlay ?? openLiveStreamDto?.EnableDirectPlay ?? true,
                EnableDirectStream  = enableDirectStream ?? openLiveStreamDto?.EnableDirectStream ?? true,
                DirectPlayProtocols = openLiveStreamDto?.DirectPlayProtocols ?? new[] { MediaProtocol.Http }
            };

            return(await _mediaInfoHelper.OpenMediaSource(Request, request).ConfigureAwait(false));
        }
Пример #2
0
        public async Task <LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, bool enableAutoClose, CancellationToken cancellationToken)
        {
            enableAutoClose = false;
            await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                var tuple    = GetProvider(request.OpenToken);
                var provider = tuple.Item1;

                var mediaSourceTuple = await provider.OpenMediaSource(tuple.Item2, cancellationToken).ConfigureAwait(false);

                var mediaSource = mediaSourceTuple.Item1;

                if (string.IsNullOrWhiteSpace(mediaSource.LiveStreamId))
                {
                    throw new InvalidOperationException(string.Format("{0} returned null LiveStreamId", provider.GetType().Name));
                }

                SetKeyProperties(provider, mediaSource);

                var info = new LiveStreamInfo
                {
                    Date             = DateTime.UtcNow,
                    EnableCloseTimer = enableAutoClose,
                    Id                   = mediaSource.LiveStreamId,
                    MediaSource          = mediaSource,
                    DirectStreamProvider = mediaSourceTuple.Item2
                };

                _openStreams[mediaSource.LiveStreamId] = info;

                if (enableAutoClose)
                {
                    StartCloseTimer();
                }

                var json = _jsonSerializer.SerializeToString(mediaSource);
                _logger.Debug("Live stream opened: " + json);
                var clone = _jsonSerializer.DeserializeFromString <MediaSourceInfo>(json);

                if (!string.IsNullOrWhiteSpace(request.UserId))
                {
                    var user = _userManager.GetUserById(request.UserId);
                    var item = string.IsNullOrWhiteSpace(request.ItemId)
                        ? null
                        : _libraryManager.GetItemById(request.ItemId);
                    SetUserProperties(item, clone, user);
                }

                return(new LiveStreamResponse
                {
                    MediaSource = clone
                });
            }
            finally
            {
                _liveStreamSemaphore.Release();
            }
        }
Пример #3
0
        public async Task <LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, CancellationToken cancellationToken)
        {
            await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                var tuple    = GetProvider(request.OpenToken);
                var provider = tuple.Item1;

                var mediaSourceTuple = await provider.OpenMediaSource(tuple.Item2, cancellationToken).ConfigureAwait(false);

                var mediaSource = mediaSourceTuple.Item1;

                // Validate that this is actually possible
                if (mediaSource.SupportsDirectStream)
                {
                    mediaSource.SupportsDirectStream = SupportsDirectStream(mediaSource.Path, mediaSource.Protocol);
                }

                if (string.IsNullOrEmpty(mediaSource.LiveStreamId))
                {
                    throw new InvalidOperationException(string.Format("{0} returned null LiveStreamId", provider.GetType().Name));
                }

                SetKeyProperties(provider, mediaSource);

                var info = new LiveStreamInfo
                {
                    Id                      = mediaSource.LiveStreamId,
                    MediaSource             = mediaSource,
                    DirectStreamProvider    = mediaSourceTuple.Item2,
                    AllowLiveMediaInfoProbe = mediaSourceTuple.Item3
                };

                _openStreams[mediaSource.LiveStreamId] = info;

                var json = _jsonSerializer.SerializeToString(mediaSource);
                _logger.Info("Live stream opened: " + json);
                var clone = _jsonSerializer.DeserializeFromString <MediaSourceInfo>(json);

                if (!string.IsNullOrEmpty(request.UserId))
                {
                    var user = _userManager.GetUserById(request.UserId);
                    var item = string.IsNullOrEmpty(request.ItemId)
                        ? null
                        : _libraryManager.GetItemById(request.ItemId);
                    SetDefaultAudioAndSubtitleStreamIndexes(item, clone, user);
                }

                return(new LiveStreamResponse
                {
                    MediaSource = clone
                });
            }
            finally
            {
                _liveStreamSemaphore.Release();
            }
        }
Пример #4
0
        private async Task <LiveStreamResponse> OpenMediaSource(LiveStreamRequest request)
        {
            var authInfo = _authContext.GetAuthorizationInfo(Request);

            var result = await _mediaSourceManager.OpenLiveStream(request, CancellationToken.None).ConfigureAwait(false);

            var profile = request.DeviceProfile;

            if (profile == null)
            {
                var caps = _deviceManager.GetCapabilities(authInfo.DeviceId);
                if (caps != null)
                {
                    profile = caps.DeviceProfile;
                }
            }

            if (profile != null)
            {
                var item = _libraryManager.GetItemById(request.ItemId);

                SetDeviceSpecificData(
                    item,
                    result.MediaSource,
                    profile,
                    authInfo,
                    request.MaxStreamingBitrate,
                    request.StartTimeTicks ?? 0,
                    result.MediaSource.Id,
                    request.AudioStreamIndex,
                    request.SubtitleStreamIndex,
                    request.MaxAudioChannels,
                    request.PlaySessionId,
                    request.UserId,
                    request.EnableDirectPlay,
                    request.EnableDirectStream,
                    true,
                    true,
                    true);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(result.MediaSource.TranscodingUrl))
                {
                    result.MediaSource.TranscodingUrl += "&LiveStreamId=" + result.MediaSource.LiveStreamId;
                }
            }

            // here was a check if (result.MediaSource != null) but Rider said it will never be null
            NormalizeMediaSourceContainer(result.MediaSource, profile !, DlnaProfileType.Video);

            return(result);
        }
Пример #5
0
        public async Task <LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, CancellationToken cancellationToken)
        {
            var result = await OpenLiveStreamInternal(request, cancellationToken).ConfigureAwait(false);

            return(result.Item1);
        }
Пример #6
0
        public async Task <Tuple <LiveStreamResponse, IDirectStreamProvider> > OpenLiveStreamInternal(LiveStreamRequest request, CancellationToken cancellationToken)
        {
            await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            MediaSourceInfo mediaSource;
            ILiveStream     liveStream;

            try
            {
                var tuple    = GetProvider(request.OpenToken);
                var provider = tuple.Item1;

                var currentLiveStreams = _openStreams.Values.ToList();

                liveStream = await provider.OpenMediaSource(tuple.Item2, currentLiveStreams, cancellationToken).ConfigureAwait(false);

                mediaSource = liveStream.MediaSource;

                // Validate that this is actually possible
                if (mediaSource.SupportsDirectStream)
                {
                    mediaSource.SupportsDirectStream = SupportsDirectStream(mediaSource.Path, mediaSource.Protocol);
                }

                SetKeyProperties(provider, mediaSource);

                _openStreams[mediaSource.LiveStreamId] = liveStream;
            }
            finally
            {
                _liveStreamSemaphore.Release();
            }

            // TODO: Don't hardcode this
            var isAudio = false;

            try
            {
                if (mediaSource.MediaStreams.Any(i => i.Index != -1) || !mediaSource.SupportsProbing)
                {
                    AddMediaInfo(mediaSource, isAudio);
                }
                else
                {
                    // hack - these two values were taken from LiveTVMediaSourceProvider
                    var cacheKey = request.OpenToken;

                    await new LiveStreamHelper(_mediaEncoder(), _logger, _jsonSerializer, _appPaths).AddMediaInfoWithProbe(mediaSource, isAudio, cacheKey, true, cancellationToken).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error probing live tv stream", ex);
                AddMediaInfo(mediaSource, isAudio);
            }

            var json = _jsonSerializer.SerializeToString(mediaSource);

            _logger.Info("Live stream opened: " + json);
            var clone = _jsonSerializer.DeserializeFromString <MediaSourceInfo>(json);

            if (!request.UserId.Equals(Guid.Empty))
            {
                var user = _userManager.GetUserById(request.UserId);
                var item = request.ItemId.Equals(Guid.Empty)
                    ? null
                    : _libraryManager.GetItemById(request.ItemId);
                SetDefaultAudioAndSubtitleStreamIndexes(item, clone, user);
            }

            return(new Tuple <LiveStreamResponse, IDirectStreamProvider>(new LiveStreamResponse
            {
                MediaSource = clone
            }, liveStream as IDirectStreamProvider));
        }
Пример #7
0
        public async Task <Tuple <LiveStreamResponse, IDirectStreamProvider> > OpenLiveStreamInternal(LiveStreamRequest request, CancellationToken cancellationToken)
        {
            await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            MediaSourceInfo mediaSource;
            ILiveStream     liveStream;

            try
            {
                var(provider, keyId) = GetProvider(request.OpenToken);

                var currentLiveStreams = _openStreams.Values.ToList();

                liveStream = await provider.OpenMediaSource(keyId, currentLiveStreams, cancellationToken).ConfigureAwait(false);

                mediaSource = liveStream.MediaSource;

                // Validate that this is actually possible
                if (mediaSource.SupportsDirectStream)
                {
                    mediaSource.SupportsDirectStream = SupportsDirectStream(mediaSource.Path, mediaSource.Protocol);
                }

                SetKeyProperties(provider, mediaSource);

                _openStreams[mediaSource.LiveStreamId] = liveStream;
            }
            finally
            {
                _liveStreamSemaphore.Release();
            }

            try
            {
                if (mediaSource.MediaStreams.Any(i => i.Index != -1) || !mediaSource.SupportsProbing)
                {
                    AddMediaInfo(mediaSource);
                }
                else
                {
                    // hack - these two values were taken from LiveTVMediaSourceProvider
                    string cacheKey = request.OpenToken;

                    await new LiveStreamHelper(_mediaEncoder, _logger, _appPaths)
                    .AddMediaInfoWithProbe(mediaSource, false, cacheKey, true, cancellationToken)
                    .ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error probing live tv stream");
                AddMediaInfo(mediaSource);
            }

            // TODO: @bond Fix
            var json = JsonSerializer.SerializeToUtf8Bytes(mediaSource, _jsonOptions);

            _logger.LogInformation("Live stream opened: {@MediaSource}", mediaSource);
            var clone = JsonSerializer.Deserialize <MediaSourceInfo>(json, _jsonOptions);

            if (!request.UserId.Equals(default))