Exemplo n.º 1
0
        public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
        {
            try
            {
                if (_directStreamProvider != null)
                {
                    await _directStreamProvider.CopyToAsync(outputStream, _cancellationToken).ConfigureAwait(false);

                    return;
                }

                var eofCount = 0;

                using (var inputStream = GetInputStream())
                {
                    if (StartPosition > 0)
                    {
                        inputStream.Position = StartPosition;
                    }

                    while (eofCount < 15 || !AllowEndOfFile)
                    {
                        var bytesRead = await CopyToAsyncInternal(inputStream, outputStream, BufferSize, _cancellationToken).ConfigureAwait(false);

                        //var position = fs.Position;
                        //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);

                        if (bytesRead == 0)
                        {
                            if (_job == null || _job.HasExited)
                            {
                                eofCount++;
                            }
                            await Task.Delay(100, _cancellationToken).ConfigureAwait(false);
                        }
                        else
                        {
                            eofCount = 0;
                        }
                    }
                }
            }
            finally
            {
                if (_job != null)
                {
                    ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
                }
            }
        }
Exemplo n.º 2
0
        public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
        {
            if (_directStreamProvider != null)
            {
                await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);

                return;
            }

            var eofCount = 0;

            // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
            var allowAsyncFileRead = _environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows;

            using (var inputStream = GetInputStream(allowAsyncFileRead))
            {
                if (StartPosition > 0)
                {
                    inputStream.Position = StartPosition;
                }

                var emptyReadLimit = AllowEndOfFile ? 20 : 100;

                while (eofCount < emptyReadLimit)
                {
                    int bytesRead;
                    if (allowAsyncFileRead)
                    {
                        bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
                    }

                    //var position = fs.Position;
                    //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);

                    if (bytesRead == 0)
                    {
                        eofCount++;
                        await Task.Delay(100, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        eofCount = 0;
                    }
                }
            }
        }
Exemplo n.º 3
0
        private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            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 directStreamProvider.CopyToAsync(output, cancellationToken).ConfigureAwait(false);
            }

            _logger.Info("Recording completed to file {0}", targetFile);
        }
Exemplo n.º 4
0
        private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            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 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);
        }
Exemplo n.º 5
0
        private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));

            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
            using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, AsyncFile.UseAsyncIO))
            {
                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);
        }
Exemplo n.º 6
0
        public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
        {
            if (_directStreamProvider != null)
            {
                await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);

                return;
            }

            var fileOptions = FileOptions.SequentialScan;

            // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                fileOptions |= FileOptions.Asynchronous;
            }

            using (var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, fileOptions))
            {
                var emptyReadLimit = AllowEndOfFile ? 20 : 100;
                var eofCount       = 0;
                while (eofCount < emptyReadLimit)
                {
                    int bytesRead;
                    bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);

                    if (bytesRead == 0)
                    {
                        eofCount++;
                        await Task.Delay(100, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        eofCount = 0;
                    }
                }
            }
        }
Exemplo n.º 7
0
        public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
        {
            cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationToken).Token;

            try
            {
                if (_directStreamProvider != null)
                {
                    await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);

                    return;
                }

                var eofCount = 0;

                // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
                var allowAsyncFileRead = OperatingSystem.Id != OperatingSystemId.Windows;

                using (var inputStream = GetInputStream(allowAsyncFileRead))
                {
                    if (StartPosition > 0)
                    {
                        inputStream.Position = StartPosition;
                    }

                    while (eofCount < 20 || !AllowEndOfFile)
                    {
                        int bytesRead;
                        if (allowAsyncFileRead)
                        {
                            bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
                        }
                        else
                        {
                            bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
                        }

                        //var position = fs.Position;
                        //_logger.LogDebug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);

                        if (bytesRead == 0)
                        {
                            if (_job == null || _job.HasExited)
                            {
                                eofCount++;
                            }
                            await Task.Delay(100, cancellationToken).ConfigureAwait(false);
                        }
                        else
                        {
                            eofCount = 0;
                        }
                    }
                }
            }
            finally
            {
                if (_job != null)
                {
                    ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
                }
            }
        }