Beispiel #1
0
        private async Task CopyTo(MediaBrowser.Model.Net.ISocket udpClient, string file, TaskCompletionSource <bool> openTaskCompletionSource, CancellationToken cancellationToken)
        {
            var bufferSize = 81920;

            byte[] buffer = new byte[bufferSize];
            int    read;
            var    resolved = false;

            using (var source = _socketFactory.CreateNetworkStream(udpClient, false))
                using (var fileStream = FileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
                {
                    var currentCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token).Token;

                    while ((read = await source.ReadAsync(buffer, 0, buffer.Length, currentCancellationToken).ConfigureAwait(false)) != 0)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        currentCancellationToken = cancellationToken;

                        read -= RtpHeaderBytes;

                        if (read > 0)
                        {
                            fileStream.Write(buffer, RtpHeaderBytes, read);
                        }

                        if (!resolved)
                        {
                            resolved   = true;
                            DateOpened = DateTime.UtcNow;
                            Resolve(openTaskCompletionSource);
                        }
                    }
                }
        }
Beispiel #2
0
        private Task StartStreaming(MediaBrowser.Model.Net.ISocket udpClient, HdHomerunManager hdHomerunManager, IPAddress remoteAddress, TaskCompletionSource <bool> openTaskCompletionSource, CancellationToken cancellationToken)
        {
            return(Task.Run(async() =>
            {
                using (udpClient)
                    using (hdHomerunManager)
                    {
                        try
                        {
                            await CopyTo(udpClient, TempFilePath, openTaskCompletionSource, cancellationToken).ConfigureAwait(false);
                        }
                        catch (OperationCanceledException ex)
                        {
                            Logger.LogInformation("HDHR UDP stream cancelled or timed out from {0}", remoteAddress);
                            openTaskCompletionSource.TrySetException(ex);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex, "Error opening live stream:");
                            openTaskCompletionSource.TrySetException(ex);
                        }

                        EnableStreamSharing = false;
                    }

                await DeleteTempFiles(new List <string> {
                    TempFilePath
                }).ConfigureAwait(false);
            }));
        }
        private async Task CopyTo(MediaBrowser.Model.Net.ISocket udpClient, string file, TaskCompletionSource <bool> openTaskCompletionSource, CancellationToken cancellationToken)
        {
            byte[] buffer = ArrayPool <byte> .Shared.Rent(StreamDefaults.DefaultCopyToBufferSize);

            try
            {
                using (var source = _socketFactory.CreateNetworkStream(udpClient, false))
                    using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        var currentCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token).Token;
                        int read;
                        var resolved = false;
                        while ((read = await source.ReadAsync(buffer, 0, buffer.Length, currentCancellationToken).ConfigureAwait(false)) != 0)
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            currentCancellationToken = cancellationToken;

                            read -= RtpHeaderBytes;

                            if (read > 0)
                            {
                                await fileStream.WriteAsync(buffer, RtpHeaderBytes, read).ConfigureAwait(false);
                            }

                            if (!resolved)
                            {
                                resolved   = true;
                                DateOpened = DateTime.UtcNow;
                                openTaskCompletionSource.TrySetResult(true);
                            }
                        }
                    }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
 public UdpClientStream(MediaBrowser.Model.Net.ISocket udpClient) : base()
 {
     _udpClient = udpClient;
 }