コード例 #1
0
ファイル: Program.cs プロジェクト: icedream/restreamer
        static int Main(string[] args)
        {
            // Parse arguments
            Queue<string> arguments = new Queue<string>(args);
            while (arguments.Count > 0)
            {
                string name = arguments.Dequeue();
                switch (name)
                {
                    case "--no-logo":
                    case "--no-version":
                    case "-v-":
                        showversion = false;
                        break;

                    case "--config-file":
                    case "-c":
                        configfile = arguments.Dequeue();
                        break;

                    case "--help":
                    case "-h":
                        if (showversion)
                        {
                            Version();
                            Console.WriteLine();
                        }
                        Usage();
                        return 0;

                    case "--version":
                    case "-v":
                        Version();
                        return 0;
                }
            }

            if (showversion)
            {
                Version();
                Console.WriteLine();
            }

            if (string.IsNullOrEmpty(configfile))
            {
                Console.Error.WriteLine("You need to input a configuration XML file with the -c/--config-file parameter.");
                return -1;
            }

            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(configfile);
            }
            catch (XmlException e)
            {
                Console.Error.WriteLine("Error in XML configuration file:");
                Console.Error.WriteLine("  {0}", e.Message);
                Console.Error.WriteLine("in line {0}, pos {1}.", e.LineNumber, e.LinePosition);
                return -2;
            }

            // Init sources
            foreach (var sourceNode in doc.SelectNodes("//restreamer/source").Cast<XmlNode>())
            {
                LivestreamReceiver recv = new LivestreamReceiver(sourceNode.Attributes["uri"].Value);
                receivers.Add(recv);

                // Init targets
                foreach (var targetNode in sourceNode.SelectNodes("child::target").Cast<XmlNode>())
                {
                    RestreamTarget targ = new RestreamTarget(recv, new Uri(targetNode.Attributes["uri"].Value));
                    targ.Disconnected += delegate(object sender, EventArgs e)
                    {
                        Console.WriteLine("Connection error, waiting 5 seconds before reconnecting...");
                        System.Threading.Thread.Sleep(5000);
                        //((RestreamTarget)sender).Stop();
                        ((RestreamTarget)sender).Start();
                    };
                    targets.Add(targ);
                }
            }

            foreach (var receiver in receivers)
            {
                Console.WriteLine("Starting receiver: {0}", receiver.SourceUri);
                receiver.Start();
            }
            foreach (var target in targets)
            {
                Console.WriteLine("Starting broadcaster: {0}:{1} (Type: {2}, Mountpoint: {3})", target.ServerHost, target.ServerPort, target.Type, target.Mountpoint);
                target.Start();
            }

            Console.WriteLine("Initialization finished.");
            Console.TreatControlCAsInput = true;
            while (true)
            {
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.C && key.Modifiers == ConsoleModifiers.Control)
                    break;
            }

            foreach (var target in targets)
            {
                Console.WriteLine("Stopping broadcaster: {0}:{1} (Type: {2}, Mountpoint: {3})", target.ServerHost, target.ServerPort, target.Type, target.Mountpoint);
                target.Stop();
            }
            foreach (var receiver in receivers)
            {
                Console.WriteLine("Stopping receiver: {0}", receiver.SourceUri);
                receiver.Stop();
            }

            return 0;
        }
コード例 #2
0
        public RestreamTarget(LivestreamReceiver receiver, Uri uri)
        {
            Receiver       = receiver;
            UserAgent      = string.Format("Restreamer/{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
            ServerUsername = "******";
            ServerPassword = "******";
            ServerHost     = "127.0.0.1";
            ServerPort     = 8000;
            Mountpoint     = "/"; // shoutcast
            Type           = RestreamType.Icecast;
            RelayMetadata  = true;

            if (!string.IsNullOrEmpty(uri.Host))
            {
                ServerHost = uri.Host;
            }

            if (!uri.IsDefaultPort)
            {
                ServerPort = (ushort)uri.Port;
            }

            if (!string.IsNullOrEmpty(uri.UserInfo))
            {
                if (uri.UserInfo.Contains(':'))
                {
                    // Username:Password
                    ServerUsername = uri.UserInfo.Substring(0, uri.UserInfo.IndexOf(':'));
                    ServerPassword = uri.UserInfo.Substring(uri.UserInfo.IndexOf(':') + 1);
                }
                else
                {
                    // Default: password only. Quite weird for URIs, but I'm too lazy to make an alternative way.
                    ServerPassword = uri.UserInfo;
                }
            }

            if (!string.IsNullOrEmpty(uri.AbsolutePath))
            {
                Mountpoint = uri.AbsolutePath;
            }

            var q = HttpUtility.ParseQueryString(uri.Query.TrimStart('?'));

            foreach (string k in q.AllKeys)
            {
                string v = q[k];
                switch (k.ToLower())
                {
                case "relay-metadata":
                    RelayMetadata = bool.Parse(v);
                    break;

                case "user-agent":
                    UserAgent = v;
                    break;

                case "password":     // same as password@host, but looks better to me.
                    ServerPassword = v;
                    break;
                }
            }

            switch (uri.Scheme.ToLower())
            {
            case "shout":
            case "sc":
                Type = RestreamType.Shoutcast;
                break;

            default:
                Type = RestreamType.Icecast;
                break;
            }

            receiver.DataReceived    += new EventHandler <LivestreamDataEventArgs>(receiver_DataReceived);
            receiver.MetadataChanged += new EventHandler <LivestreamMetadataEventArgs>(receiver_MetadataChanged);
        }
コード例 #3
0
 internal LivestreamMetadataEventArgs(LivestreamReceiver receiver, Dictionary <string, string> metadata)
 {
     this.Receiver = receiver;
     this.Metadata = metadata;
 }
コード例 #4
0
 internal LivestreamDataEventArgs(LivestreamReceiver receiver, byte[] data)
 {
     this.Receiver = receiver;
     this.Data     = data;
 }
コード例 #5
0
        static int Main(string[] args)
        {
            // Parse arguments
            Queue <string> arguments = new Queue <string>(args);

            while (arguments.Count > 0)
            {
                string name = arguments.Dequeue();
                switch (name)
                {
                case "--no-logo":
                case "--no-version":
                case "-v-":
                    showversion = false;
                    break;

                case "--config-file":
                case "-c":
                    configfile = arguments.Dequeue();
                    break;

                case "--help":
                case "-h":
                    if (showversion)
                    {
                        Version();
                        Console.WriteLine();
                    }
                    Usage();
                    return(0);

                case "--version":
                case "-v":
                    Version();
                    return(0);
                }
            }

            if (showversion)
            {
                Version();
                Console.WriteLine();
            }

            if (string.IsNullOrEmpty(configfile))
            {
                Console.Error.WriteLine("You need to input a configuration XML file with the -c/--config-file parameter.");
                return(-1);
            }

            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(configfile);
            }
            catch (XmlException e)
            {
                Console.Error.WriteLine("Error in XML configuration file:");
                Console.Error.WriteLine("  {0}", e.Message);
                Console.Error.WriteLine("in line {0}, pos {1}.", e.LineNumber, e.LinePosition);
                return(-2);
            }

            // Init sources
            foreach (var sourceNode in doc.SelectNodes("//restreamer/source").Cast <XmlNode>())
            {
                LivestreamReceiver recv = new LivestreamReceiver(sourceNode.Attributes["uri"].Value);
                receivers.Add(recv);

                // Init targets
                foreach (var targetNode in sourceNode.SelectNodes("child::target").Cast <XmlNode>())
                {
                    RestreamTarget targ = new RestreamTarget(recv, new Uri(targetNode.Attributes["uri"].Value));
                    targ.Disconnected += delegate(object sender, EventArgs e)
                    {
                        Console.WriteLine("Connection error, waiting 5 seconds before reconnecting...");
                        System.Threading.Thread.Sleep(5000);
                        //((RestreamTarget)sender).Stop();
                        ((RestreamTarget)sender).Start();
                    };
                    targets.Add(targ);
                }
            }

            foreach (var receiver in receivers)
            {
                Console.WriteLine("Starting receiver: {0}", receiver.SourceUri);
                receiver.Start();
            }
            foreach (var target in targets)
            {
                Console.WriteLine("Starting broadcaster: {0}:{1} (Type: {2}, Mountpoint: {3})", target.ServerHost, target.ServerPort, target.Type, target.Mountpoint);
                target.Start();
            }

            Console.WriteLine("Initialization finished.");
            Console.TreatControlCAsInput = true;
            while (true)
            {
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.C && key.Modifiers == ConsoleModifiers.Control)
                {
                    break;
                }
            }

            foreach (var target in targets)
            {
                Console.WriteLine("Stopping broadcaster: {0}:{1} (Type: {2}, Mountpoint: {3})", target.ServerHost, target.ServerPort, target.Type, target.Mountpoint);
                target.Stop();
            }
            foreach (var receiver in receivers)
            {
                Console.WriteLine("Stopping receiver: {0}", receiver.SourceUri);
                receiver.Stop();
            }

            return(0);
        }
コード例 #6
0
 internal LivestreamMetadataEventArgs(LivestreamReceiver receiver, Dictionary<string, string> metadata)
 {
     this.Receiver = receiver;
     this.Metadata = metadata;
 }
コード例 #7
0
 internal LivestreamDataEventArgs(LivestreamReceiver receiver, byte[] data)
 {
     this.Receiver = receiver;
     this.Data = data;
 }