Exemplo n.º 1
0
        public async Task Start_WithMock_PauseResume_Success()
        {
            string mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();
            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.TargetDir = outDir;
            hc.Start();

            await Task.Delay(9000);

            hc.Stop();

            await Task.Delay(2000);

            var name = hc.TargetFileName;

            hc                = new SingleSourceHTTPDownloader(hc.Id);
            hc.TargetDir      = outDir;
            hc.TargetFileName = name;
            hc.Finished      += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.Resume();

            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            string hash1 = mockServer.GetHash(mockId);

            Assert.AreEqual(hash1, GetFileHash(hc.TargetFile));
        }
Exemplo n.º 2
0
        public async Task DownloadAsync_With2Url_Success(string segmentedExt, string expectedExt)
        {
            var success = false;
            var cs      = new CancellationTokenSource();

            var n = 10;

            mockServer = new MockServer.MockServer();
            var pl1 = CreateMockPlaylist(n, mockServer, segmentedExt);
            var pl2 = CreateMockPlaylist(n, mockServer, segmentedExt);

            mockServer.StartAsync();

            string configDir = Path.GetTempPath();
            string id        = Guid.NewGuid().ToString();

            string tempDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(tempDir);

            Console.WriteLine(tempDir);

            var hc = new MultiSourceHLSDownloader(
                new MultiSourceHLSDownloadInfo
            {
                VideoUri = pl1.Url,
                AudioUri = pl2.Url
            },
                mediaProcessor: new FakeMediaProcessor());

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir      = tempDir;
            hc.TargetFileName = "Sample";
            hc.Start();
            try
            {
                await Task.Delay(5 * 60 * 1000, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);
            Console.WriteLine(hc.Duration);
            Assert.NotZero(hc.Duration);

            long size2 = hc.FileSize;

            Assert.AreEqual(pl1.Size + pl2.Size, size2);
            Assert.IsTrue(expectedExt.Equals(Path.GetExtension(hc.TargetFileName),
                                             StringComparison.InvariantCultureIgnoreCase));
        }
Exemplo n.º 3
0
        public async Task Start_WithMock_DualSource_Success()
        {
            var mockId1 = Guid.NewGuid().ToString();
            var mockId2 = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId1, start: 20, end: 30);
            mockServer.AddMockHandler(mockId2);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();

            var hc = new DualSourceHTTPDownloader(
                new DualSourceHTTPDownloadInfo
            {
                Uri1 = $"http://127.0.0.1:39000/{mockId1}",
                Uri2 = $"http://127.0.0.1:39000/{mockId2}"
            },
                mediaProcessor: new FakeMediaProcessor());

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            hc.Start();
            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            Console.WriteLine(hc.FileSize);
            Assert.NotZero(hc.FileSize);

            //mockServer.Stop();
            string hash1 = mockServer.GetHash(mockId1);
            string hash2 = mockServer.GetHash(mockId2);

            Assert.AreEqual(hash1, GetFileHash(Path.Combine(Config.DataDir, hc.Id, "1_" + hc.Id)));
            Assert.AreEqual(hash2, GetFileHash(Path.Combine(Config.DataDir, hc.Id, "2_" + hc.Id)));
        }
Exemplo n.º 4
0
        public async Task Start_WithMockAndAttachment_Success()
        {
            var name    = "filename.zip";
            var headers = new Dictionary <string, string>
            {
                ["Content-Disposition"] = $"attachment; filename=\"{name}\"",
                ["Content-Type"]        = "application/zip"
            };

            var mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId, headers: headers);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();

            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            hc.Start();
            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            string hash1 = mockServer.GetHash(mockId);

            Assert.AreEqual(GetFileHash(hc.TargetFile), hash1);
            Assert.AreEqual(hc.TargetFileName, name);
        }
Exemplo n.º 5
0
        public async Task Start_OnDropout_Fails()
        {
            string mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = true;

            var cs = new CancellationTokenSource();

            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                Console.WriteLine("Test: download failed");
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            hc.Start();
            try
            {
                await Task.Delay(3000, cs.Token);
            }
            catch { }
            mockServer.Stop();

            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsFalse(success);
            Assert.IsFalse(hc.IsCancelled);
            var path = Path.Combine(Config.DataDir, hc.Id);

            Assert.DoesNotThrow(() => Directory.Delete(path, true));
        }
Exemplo n.º 6
0
        public async Task Start_WithMock_NonResumableNoContentLength_Success()
        {
            string mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            var ret = mockServer.AddMockHandler(mockId, start: 10, end: 20);

            mockServer.NonResumable     = true;
            mockServer.HasContentLength = false;
            mockServer.StartAsync();

            Console.WriteLine("Actual file size: {0}", ret.Size);

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();

            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            Console.WriteLine("Out path: {0}", Path.Combine(Config.DataDir, hc.Id));

            hc.Start();
            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);
            Assert.IsTrue(hc.FileSize > 0);
            Assert.AreEqual(hc.FileSize, ret.Size);
            var hash1 = mockServer.GetHash(mockId);

            Assert.AreEqual(hash1, GetFileHash(hc.TargetFile));
        }
Exemplo n.º 7
0
        public async Task DownloadAsyncWithMockSuccess(string segmentedExt, string expectedExt)
        {
            var n      = 20;
            var random = new Random();

            mockServer = new MockServer.MockServer();
            var size    = 0L;
            var mockIds = new string[n];
            var streams = new StringBuilder();

            for (var i = 0; i < n; i++)
            {
                var mockId = Guid.NewGuid().ToString() + segmentedExt;
                size      += mockServer.AddMockHandler(mockId, start: 1, end: random.Next(1, 4)).Size;
                mockIds[i] = mockId;
                streams.Append(
                    $@"#EXTINF:57.590867
                    {mockServer.BaseUrl}{mockId}
                    ");
            }

            var playlist =
                $@"
                #EXTM3U
                #EXT-X-VERSION:3
                #EXT-X-TARGETDURATION:64
                {streams}
                #EXT-X-ENDLIST
                ";
            var pid = Guid.NewGuid().ToString();

            mockServer.AddMockHandler(pid, contents: Encoding.UTF8.GetBytes(playlist));
            mockServer.StartAsync();

            var url = $"{mockServer.BaseUrl}{pid}";

            string configDir = Path.GetTempPath();
            string id        = Guid.NewGuid().ToString();

            string tempDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(tempDir);

            Console.WriteLine(tempDir);

            var success = false;
            var cs      = new CancellationTokenSource();

            var hc = new MultiSourceHLSDownloader(new MultiSourceHLSDownloadInfo
            {
                VideoUri = url
            },
                                                  mediaProcessor: new FakeMediaProcessor());

            hc.Finished += (a, b) =>
            {
                Console.Write("Success");
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                Console.Write("Failed");
                success = false;
                cs.Cancel();
            };
            hc.TargetDir      = tempDir;
            hc.TargetFileName = "Sample";
            hc.Start();
            try
            {
                await Task.Delay(2 * 60 * 1000, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            long size2 = hc.FileSize;

            Console.WriteLine(size2 + " " + size);

            Console.WriteLine(hc.Duration);
            Assert.NotZero(hc.Duration);
            Assert.AreEqual(size2, size);
            Assert.IsTrue(expectedExt.Equals(Path.GetExtension(hc.TargetFileName),
                                             StringComparison.InvariantCultureIgnoreCase));
        }
Exemplo n.º 8
0
        public async Task DownloadAsync_With2Url_PauseResume_Success(string segmentedExt, string expectedExt)
        {
            var success = false;
            var cs      = new CancellationTokenSource();

            var n = 20;

            mockServer = new MockServer.MockServer();
            var pl1 = CreateMockPlaylist(n, mockServer, segmentedExt);
            var pl2 = CreateMockPlaylist(n, mockServer, segmentedExt);

            mockServer.StartAsync();

            string configDir = Path.GetTempPath();
            string id        = Guid.NewGuid().ToString();

            string tempDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(tempDir);

            var hc = new MultiSourceHLSDownloader(
                new MultiSourceHLSDownloadInfo
            {
                VideoUri = pl1.Url,
                AudioUri = pl2.Url
            },
                mediaProcessor: new FakeMediaProcessor());

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir      = tempDir;
            hc.TargetFileName = "Sample";
            hc.Start();

            await Task.Delay(10000);

            hc.Stop();
            Log.Information("Stopped --");
            await Task.Delay(2000);

            var name = hc.TargetFileName;

            cs = new CancellationTokenSource();
            hc = new MultiSourceHLSDownloader(hc.Id,
                                              mediaProcessor: new FakeMediaProcessor());
            hc.TargetDir      = tempDir;
            hc.TargetFileName = name;
            hc.Finished      += (a, b) =>
            {
                Log.Debug("Finished2");
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.Resume();

            try
            {
                await Task.Delay(15 * 60 * 1000, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            long size2 = hc.FileSize;

            //foreach (var f in Directory.EnumerateFiles(Path.Combine(Config.DataDir, hc.Id)))
            //{
            //    size2 += new FileInfo(f).Length;
            //}
            Log.Debug(size2 + " " + (pl1.Size + pl2.Size));
            Assert.AreEqual(pl1.Size + pl2.Size, size2);

            Log.Debug(expectedExt + " " + Path.GetExtension(hc.TargetFileName));

            Assert.IsTrue(expectedExt.Equals(Path.GetExtension(hc.TargetFileName),
                                             StringComparison.InvariantCultureIgnoreCase));
        }
Exemplo n.º 9
0
        public async Task DownloadAsyncWithMockPauseResumeSuccess()
        {
            var success = false;
            var cs      = new CancellationTokenSource();

            var n = 10;

            mockServer = new MockServer.MockServer();
            var pl = CreateMockPlaylist(n, mockServer);

            mockServer.StartAsync();

            string configDir = Path.GetTempPath();
            string id        = Guid.NewGuid().ToString();

            string tempDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(tempDir);

            Console.WriteLine(tempDir);

            var hc = new MultiSourceHLSDownloader(new MultiSourceHLSDownloadInfo
            {
                VideoUri = pl.Url
            },
                                                  mediaProcessor: new FakeMediaProcessor());

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir      = tempDir;
            hc.TargetFileName = "Sample";
            hc.Start();

            await Task.Delay(2000);

            hc.Stop();
            Log.Information("Stopped --");
            await Task.Delay(2000);

            var name = hc.TargetFileName;

            cs = new CancellationTokenSource();
            hc = new MultiSourceHLSDownloader(hc.Id,
                                              mediaProcessor: new FakeMediaProcessor());
            hc.TargetDir      = tempDir;
            hc.TargetFileName = name;
            hc.Finished      += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.Resume();

            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            long size2 = hc.FileSize;

            Console.WriteLine(size2 + " " + pl.Size);

            Console.WriteLine(hc.Duration);
            Assert.NotZero(hc.Duration);
            Assert.AreEqual(size2, pl.Size);
        }