コード例 #1
0
ファイル: MediaStreamer.cs プロジェクト: jindal1979/Flyleaf
        public int Open(string url, StreamType streamType = StreamType.TORRENT, bool isMagnetLink = true)
        {
            this.streamType = streamType;
            status          = Status.OPENING;
            Initialize();

            if (streamType == StreamType.FILE)
            {
                fsStream = new FileStream(url, FileMode.Open, FileAccess.Read);
                fileSize = fsStream.Length;

                Torrent torrent = new Torrent("whatever");
                torrent.file         = new Torrent.TorrentFile();
                torrent.file.paths   = new List <string>();
                torrent.file.lengths = new List <long>();

                torrent.file.paths.Add("MediaFile1.mp4");
                torrent.file.paths.Add("MediaFile2.mp4");
                torrent.file.lengths.Add(123123894);
                torrent.file.lengths.Add(123123897);

                MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(torrent));

                status = Status.OPENED;
            }
            else if (streamType == StreamType.TORRENT)
            {
                BitSwarm.OptionsStruct opt = BitSwarm.GetDefaultsOptions();
                opt.PieceTimeout = 4300;
                //opt.LogStats            = true;
                //opt.Verbosity           = 2;

                try
                {
                    if (isMagnetLink)
                    {
                        tsStream = new BitSwarm(new Uri(url), opt);
                    }
                    else
                    {
                        tsStream = new BitSwarm(url, opt);
                    }
                } catch (Exception e) { Log($"[MS] BitSwarm Failed Opening Url {e.Message}\r\n{e.StackTrace}"); Initialize(); status = Status.FAILED; return(-1); }

                tsStream.MetadataReceived += MetadataReceived;
                tsStream.StatsUpdated     += Stats;

                tsStream.Start();
                sortedPaths = null;
                status      = Status.OPENED;
            }

            return(0);
        }
コード例 #2
0
ファイル: frmMain.cs プロジェクト: SuRGeoNix/BitSwarm
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
            {
                output.Text = "";
                listBox1.Items.Clear();
                button2.Enabled = false;

                try
                {
                    opt = new Options();

                    opt.FolderComplete = downPath.Text;

                    opt.MaxThreads        = int.Parse(maxCon.Text);
                    opt.MinThreads        = int.Parse(maxThreads.Text);
                    opt.SleepModeLimit    = int.Parse(sleepLimit.Text);
                    opt.PeersFromTracker  = int.Parse(peersFromTrackers.Text);
                    opt.ConnectionTimeout = int.Parse(conTimeout.Text);
                    opt.HandshakeTimeout  = int.Parse(handTimeout.Text);
                    opt.PieceTimeout      = int.Parse(pieceTimeout.Text);
                    opt.MetadataTimeout   = int.Parse(metaTimeout.Text);

                    opt.Verbosity  = 0;
                    opt.LogDHT     = false;
                    opt.LogStats   = false;
                    opt.LogTracker = false;
                    opt.LogPeer    = false;

                    output.Text  = "Started at " + DateTime.Now.ToString("G", DateTimeFormatInfo.InvariantInfo) + "\r\n";
                    button1.Text = "Stop";

                    bitSwarm = new BitSwarm(opt);

                    bitSwarm.StatsUpdated     += BitSwarm_StatsUpdated;
                    bitSwarm.MetadataReceived += BitSwarm_MetadataReceived;
                    bitSwarm.StatusChanged    += BitSwarm_StatusChanged;

                    bitSwarm.Open(input.Text);
                    bitSwarm.Start();
                }
                catch (Exception e1)
                {
                    output.Text    += e1.Message + "\r\n" + e1.StackTrace;
                    button1.Text    = "Start";
                    button2.Enabled = false;
                }
            }
            else
            {
                bitSwarm.Dispose();
                button1.Text = "Start";
            }
        }
コード例 #3
0
ファイル: TorrentStreamer.cs プロジェクト: huerd/Flyleaf
        public int Open(string input)
        {
            Initialize();

            try
            {
                bitSwarm.Open(input);
                Log("Starting");
                bitSwarm.Start();
            }
            catch (Exception e)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(e.Message, "completed or is invalid"))
                {
                    MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(bitSwarm.torrent));
                    return(0);
                }

                Initialize();
                return(-1);
            }

            return(0);
        }
コード例 #4
0
ファイル: TorrentDownloader.cs プロジェクト: phuctv95/try-it
        public void StartDownloading(string magnetLinkOrTorrentFile, string savingLocation, Action <string> writeLog,
                                     Action <IList <string> > onMetadataReceived, Action <TorrentDownloadStats> onDownloadingProgress, Action onDownloadFinished)
        {
            CreateProcessingFoldersIfNotExist();
            var opt = new Options
            {
                FolderComplete   = savingLocation,
                FolderIncomplete = Path.Combine(Directory.GetCurrentDirectory(), IncompleteFolderName),
                FolderTorrents   = Path.Combine(Directory.GetCurrentDirectory(), TorrentsFolderName),
                FolderSessions   = Path.Combine(Directory.GetCurrentDirectory(), SessionsFolderName),
            };

            _bitSwarm = new BitSwarm(opt);

            _bitSwarm.MetadataReceived += (source, e) => OnMetadataReceived(e, writeLog, onMetadataReceived);
            _bitSwarm.StatsUpdated     += (source, e) => OnStatsUpdated(e, writeLog, onDownloadingProgress);
            _bitSwarm.StatusChanged    += (source, e) => OnStatusChanged(e, writeLog, onDownloadFinished);
            _bitSwarm.OnFinishing      += (source, e) => writeLog($"[OnFinishing] Finishing...");

            _bitSwarm.Open(magnetLinkOrTorrentFile);
            _downloadedFiles.Clear();
            _bitSwarm.Start();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            // Initialize LibVLC
            Core.Initialize();
            libVLC      = new LibVLC();
            libVLC.Log += (s, e) => WriteLine($"LibVLC -> {e.FormattedLog}");

            // Initialize BitSwarm
            Options opt = new Options();

            opt.BlockRequests = 2;      // To avoid timeouts for large byte ranges
            opt.PieceTimeout  = 1000;   // It will reset the requested blocks after 1sec of timeout (this means that we might get them twice or more) | More drop bytes vs Faster streaming (should be set only during open/seek)
            opt.PieceRetries  = 5;      // To avoid disconnecting the peer (reset pieces on first retry but keep the peer alive)
            //opt.Verbosity = 2;
            //opt.LogStats = true;

            bitSwarm = new BitSwarm(opt);
            bitSwarm.MetadataReceived += BitSwarm_MetadataReceived;
            bitSwarm.StatsUpdated     += BitSwarm_StatsUpdated;
            bitSwarm.Open(TORRENT_MAGNET_OR_HASH);
            bitSwarm.Start();

            ReadKey();
        }
コード例 #6
0
        public int SetMediaFile(string fileName)
        {
            Log($"{streamType.ToString()}: File Selected {fileName}");

            if (streamType == StreamType.TORRENT)
            {
                Pause();

                FileName     = fileName;
                fileIndex    = torrent.file.paths.IndexOf(fileName);
                FileSize     = torrent.file.lengths[fileIndex];
                fileDistance = 0;
                for (int i = 0; i < fileIndex; i++)
                {
                    fileDistance += torrent.file.lengths[i];
                }
                fileLastPiece = FilePosToPiece(FileSize);

                if (sortedPaths == null)
                {
                    sortedPaths = Utils.GetMoviesSorted(torrent.file.paths);
                }
                downloadNextStarted = false;
                fileIndexNext       = -1;

                FolderComplete = torrent.file.paths.Count == 1 ? config.DownloadPath : torrent.data.folder;

                if (torrent.data.files[fileIndex] != null)
                {
                    if (!torrent.data.files[fileIndex].Created)
                    {
                        bitSwarm.IncludeFiles(new List <string>()
                        {
                            fileName
                        });
                        if (!bitSwarm.isRunning)
                        {
                            bitSwarm.Start();
                        }
                    }
                }

                // Both complete & incomplete files are missing!
                else
                {
                    if (File.Exists(Path.Combine(FolderComplete, FileName)))
                    {
                        status = Status.OPENED;
                        if (!DownloadNext())
                        {
                            bitSwarm.Pause();
                        }
                        player.UrlType = MediaRouter.InputType.TorrentFile;
                        return(player.decoder.Open(Path.Combine(FolderComplete, FileName)));
                    }
                    else
                    {
                        return(-5); // File Missing?!
                    }
                }

                player.UrlType = MediaRouter.InputType.TorrentPart;
                status         = Status.BUFFERING;
                player.renderer.NewMessage(OSDMessage.Type.Buffering, $"Buffering ...", null, 30000);

                Log($"[BB OPENING]");
                int ret = decoder.Open(null, "", "", "", DecoderRequestsBuffer, FileSize);
                if (ret != 0)
                {
                    return(ret);
                }

                Log($"[DD OPENING]");
                ret = player.decoder.Open(null, "", "", "", DecoderRequests, FileSize);
                if (ret != 0)
                {
                    return(ret);
                }

                status = Status.OPENED;
                DownloadNext();

                return(ret);
            }
            else
            {
                Log($"[BB OPENING]");
                int ret = decoder.Open(null, "", "", "", DecoderRequestsBuffer, FileSize);
                if (ret != 0)
                {
                    return(ret);
                }

                Log($"[DD OPENING]");
                ret = player.decoder.Open(null, "", "", "", DecoderRequests, FileSize);
                if (ret != 0)
                {
                    return(ret);
                }

                return(0);
            }
        }
コード例 #7
0
        public int Open(string url, StreamType streamType = StreamType.TORRENT)
        {
            this.streamType = streamType;
            status          = Status.OPENING;
            Initialize();

            if (streamType == StreamType.FILE)
            {
                fsStream = new FileStream(url, FileMode.Open, FileAccess.Read);
                FileSize = fsStream.Length;

                Torrent torrent = new Torrent(null);
                torrent.file         = new Torrent.TorrentFile();
                torrent.file.paths   = new List <string>();
                torrent.file.lengths = new List <long>();

                torrent.file.paths.Add("MediaFile1.mp4");
                torrent.file.paths.Add("MediaFile2.mp4");
                torrent.file.lengths.Add(123123894);
                torrent.file.lengths.Add(123123897);

                MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(torrent));

                status = Status.OPENED;
            }
            else if (streamType == StreamType.TORRENT)
            {
                ParseSettingsToBitSwarm(); // Resets Options (Note: can be changed during a sessions)
                bitSwarmOpt.PieceTimeout = config.TimeoutOpen;
                bitSwarmOpt.PieceRetries = config.RetriesOpen;

                // Testing
                //bitSwarmOpt.SleepModeLimit  = config.SleepMode;
                //bitSwarmOpt.MinThreads      = config.MinThreads;
                //bitSwarmOpt.MaxThreads      = config.MaxThreads;
                //bitSwarmOpt.BlockRequests   = config.BlockRequests;

                //bitSwarmOpt.TrackersPath    = @"c:\root\trackers2.txt";

                //bitSwarmOpt.FolderComplete      = @"c:\root\_f01";
                //bitSwarmOpt.FolderIncomplete    = @"c:\root\_f01";
                //bitSwarmOpt.FolderSessions      = @"c:\root\_f01";
                //bitSwarmOpt.FolderTorrents      = @"c:\root\_f01";

                //bitSwarmOpt.LogPeer = true;
                //bitSwarmOpt.LogTracker = true;
                //bitSwarmOpt.LogStats = true;
                //bitSwarmOpt.Verbosity = 4;

                bitSwarm = new BitSwarm(bitSwarmOpt);

                bitSwarm.MetadataReceived += MetadataReceived;
                bitSwarm.OnFinishing      += DownloadCompleted;
                bitSwarm.StatsUpdated     += Stats;

                try
                {
                    //seemsCompleted = false;
                    bitSwarm.Open(url);
                }
                catch (Exception e)
                {
                    Log($"BitSwarm Failed Opening Url {e.Message} [Will consider completed]");

                    if (System.Text.RegularExpressions.Regex.IsMatch(e.Message, "completed or is invalid"))
                    {
                        //seemsCompleted = true;
                        sortedPaths = null;
                        status      = Status.OPENED;

                        MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(bitSwarm.torrent));

                        return(0);
                    }

                    Initialize();
                    status = Status.FAILED;
                    return(-1);
                }

                bitSwarm.Start();
                sortedPaths = null;
                status      = Status.OPENED;
            }

            return(0);
        }
コード例 #8
0
        private static void Run(Options userOptions)
        {
            try
            {
                Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Initializing ...");

                if (userOptions.Input == "config")
                {
                    BitSwarmOptions.CreateConfig(new BitSwarmOptions());
                    Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Config {BitSwarmOptions.ConfigFile} created.");
                    return;
                }
                else if ((bitSwarmOptions = BitSwarmOptions.LoadConfig()) != null)
                {
                    Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Config {BitSwarmOptions.ConfigFile} loaded.");
                }

                if (bitSwarmOptions == null)
                {
                    bitSwarmOptions = new BitSwarmOptions();
                }
                if (!Options.ParseOptionsToBitSwarm(userOptions, ref bitSwarmOptions))
                {
                    return;
                }

                // BitSwarm [Create | Subscribe | Open | Start]
                bitSwarm = new BitSwarm(bitSwarmOptions);

                bitSwarm.MetadataReceived += BitSwarm_MetadataReceived;     // Receives torrent data [on torrent file will fire directly, on magnetlink will fire on metadata received]
                bitSwarm.StatsUpdated     += BitSwarm_StatsUpdated;         // Stats refresh every 2 seconds
                bitSwarm.StatusChanged    += BitSwarm_StatusChanged;        // Paused/Stopped or Finished

                bitSwarm.Open(userOptions.Input);
                bitSwarm.Start();

                // Stats | Torrent | Peers Views [Until Stop or Finish]
                ConsoleKeyInfo cki;
                Console.TreatControlCAsInput = true;
                prevHeight = Console.WindowHeight;

                while (!sessionFinished)
                {
                    try
                    {
                        cki = Console.ReadKey();

                        if (sessionFinished)
                        {
                            break;
                        }
                        if ((cki.Modifiers & ConsoleModifiers.Control) != 0 && cki.Key == ConsoleKey.C)
                        {
                            break;
                        }

                        lock (lockRefresh)
                            switch (cki.Key)
                            {
                            case ConsoleKey.D1:
                                view = View.Stats;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpStats());
                                PrintMenu();
                                break;

                            case ConsoleKey.D2:
                                view = View.Torrent;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpTorrent());
                                PrintMenu();

                                break;

                            case ConsoleKey.D3:
                                view = View.Torrent;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpPeers());
                                PrintMenu();

                                break;

                            case ConsoleKey.D4:
                                view = View.Peers;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpPeers());
                                PrintMenu();

                                break;

                            default:
                                break;
                            }
                    } catch (Exception) { }
                }

                // Dispose (force) BitSwarm
                if (bitSwarm != null)
                {
                    bitSwarm.Dispose(true);
                }
            } catch (Exception e) { Console.WriteLine($"[ERROR] {e.Message}"); }
        }