Example #1
0
 public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
 {
     _directStreamProvider = directStreamProvider;
     _outputHeaders = outputHeaders;
     _job = job;
     _logger = logger;
     _cancellationToken = cancellationToken;
 }
Example #2
0
        private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(targetFile));

            using (var output = _fileSystem.GetFileStream(targetFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
            {
                onStarted();

                _logger.LogInformation("Copying recording stream to file {0}", targetFile);

                // The media source is infinite so we need to handle stopping ourselves
                var durationToken = new CancellationTokenSource(duration);
                cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;

                await directStreamProvider.CopyToAsync(output, cancellationToken).ConfigureAwait(false);
            }

            _logger.LogInformation("Recording completed to file {0}", targetFile);
        }
        public async Task <Tuple <MediaSourceInfo, IDirectStreamProvider> > OpenMediaSource(string openToken, bool allowLiveStreamProbe, CancellationToken cancellationToken)
        {
            MediaSourceInfo stream  = null;
            const bool      isAudio = false;

            var keys          = openToken.Split(new[] { StreamIdDelimeter }, 3);
            var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
            IDirectStreamProvider directStreamProvider = null;

            if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase))
            {
                var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, cancellationToken).ConfigureAwait(false);

                stream = info.Item1;
                directStreamProvider = info.Item2;

                //allowLiveStreamProbe = false;
            }
            else
            {
                stream = await _liveTvManager.GetRecordingStream(keys[1], cancellationToken).ConfigureAwait(false);
            }

            try
            {
                if (!allowLiveStreamProbe || !stream.SupportsProbing || stream.MediaStreams.Any(i => i.Index != -1))
                {
                    AddMediaInfo(stream, isAudio, cancellationToken);
                }
                else
                {
                    await new LiveStreamHelper(_mediaEncoder, _logger).AddMediaInfoWithProbe(stream, isAudio, cancellationToken).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error probing live tv stream", ex);
            }

            _logger.Info("Live stream info: {0}", _jsonSerializer.SerializeToString(stream));
            return(new Tuple <MediaSourceInfo, IDirectStreamProvider>(stream, directStreamProvider));
        }
Example #4
0
        private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(targetFile));

            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
            using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                onStarted();

                _logger.LogInformation("Copying recording stream to file {0}", targetFile);

                // The media source is infinite so we need to handle stopping ourselves
                using var durationToken           = new CancellationTokenSource(duration);
                using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);

                await directStreamProvider.CopyToAsync(output, cancellationTokenSource.Token).ConfigureAwait(false);
            }

            _logger.LogInformation("Recording completed to file {0}", targetFile);
        }
        public async Task <Tuple <MediaSourceInfo, IDirectStreamProvider, bool> > OpenMediaSource(string openToken, CancellationToken cancellationToken)
        {
            MediaSourceInfo stream  = null;
            const bool      isAudio = false;

            var keys          = openToken.Split(new[] { StreamIdDelimeter }, 3);
            var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
            IDirectStreamProvider directStreamProvider = null;

            bool addProbeDelay       = false;
            bool allowLiveMediaProbe = false;

            var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, cancellationToken).ConfigureAwait(false);

            stream = info.Item1;
            directStreamProvider = info.Item2 as IDirectStreamProvider;
            addProbeDelay        = true;
            allowLiveMediaProbe  = directStreamProvider != null;

            try
            {
                if (stream.MediaStreams.Any(i => i.Index != -1) || !stream.SupportsProbing)
                {
                    AddMediaInfo(stream, isAudio, cancellationToken);
                }
                else
                {
                    var cacheKey = keys[1] + "-" + mediaSourceId;

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

            return(new Tuple <MediaSourceInfo, IDirectStreamProvider, bool>(stream, directStreamProvider, allowLiveMediaProbe));
        }