Пример #1
0
        private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
                                 .GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

            _logger.LogInformation("Opened recording stream from tuner provider");

            Directory.CreateDirectory(Path.GetDirectoryName(targetFile));

            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
            await 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 if infinite so we need to handle stopping ourselves
            using var durationToken           = new CancellationTokenSource(duration);
            using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
            cancellationToken = linkedCancellationToken.Token;

            await _streamHelper.CopyUntilCancelled(
                await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
                output,
                IODefaults.CopyToBufferSize,
                cancellationToken).ConfigureAwait(false);

            _logger.LogInformation("Recording completed to file {0}", targetFile);
        }
Пример #2
0
        private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            var httpRequestOptions = new HttpRequestOptions
            {
                Url           = mediaSource.Path,
                BufferContent = false,

                // Some remote urls will expect a user agent to be supplied
                UserAgent = "Emby/3.0",

                // Shouldn't matter but may cause issues
                DecompressionMethod = CompressionMethods.None,
                CancellationToken   = cancellationToken
            };

            using (var response = await _httpClient.SendAsync(httpRequestOptions, HttpMethod.Get).ConfigureAwait(false))
            {
                _logger.LogInformation("Opened recording stream from tuner provider");

                Directory.CreateDirectory(Path.GetDirectoryName(targetFile));

                using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    onStarted();

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

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

                    await _streamHelper.CopyUntilCancelled(
                        response.Content,
                        output,
                        IODefaults.CopyToBufferSize,
                        cancellationTokenSource.Token).ConfigureAwait(false);
                }
            }

            _logger.LogInformation("Recording completed to file {0}", targetFile);
        }
Пример #3
0
        private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            var httpRequestOptions = new HttpRequestOptions
            {
                Url           = mediaSource.Path,
                BufferContent = false,

                // Some remote urls will expect a user agent to be supplied
                UserAgent = "Emby/3.0",

                // Shouldn't matter but may cause issues
                EnableHttpCompression = false
            };

            using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false))
            {
                _logger.Info("Opened recording stream from tuner provider");

                _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(targetFile));

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

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

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

                    await _streamHelper.CopyUntilCancelled(response.Content, output, 81920, cancellationToken).ConfigureAwait(false);
                }
            }

            _logger.Info("Recording completed to file {0}", targetFile);
        }