protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.

            m_MediaResourceManager.Clear();
            m_MediaResourceManager.LoadHandlers();

            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("media-servers");

            CommonLog.Info("=== Media Server is starting ===");

            var appSettings = ConfigurationManager.AppSettings;

            string allAvailableChannels = "";

            if (appSettings.AllKeys.Contains("AvailableChannels"))
            {
                allAvailableChannels = appSettings["AvailableChannels"];
            }
            if (allAvailableChannels.Length > 0)
            {
                m_MediaResourceManager.SetAvailableChannelNames(allAvailableChannels);
            }

            string remoteValidationURL = "";

            if (appSettings.AllKeys.Contains("RemoteValidationURL"))
            {
                remoteValidationURL = appSettings["RemoteValidationURL"];
            }

            var mediaServerSettings = (NameValueCollection)ConfigurationManager.GetSection("media-servers");
            var allKeys             = mediaServerSettings.AllKeys;

            foreach (var key in allKeys)
            {
                string                json        = mediaServerSettings[key];
                ServerSetting         setting     = JsonConvert.DeserializeObject <ServerSetting>(json);
                HttpSourceMediaServer mediaServer = new HttpSourceMediaServer(key, m_MediaResourceManager, CommonLog.GetLogger(),
                                                                              setting.InputIp, setting.InputPort, setting.OutputIp, setting.OutputPort, setting.InputWhitelist, setting.CertFile, setting.CertKey);
                mediaServer.InputQueueSize         = setting.InputQueueSize;
                mediaServer.InputBufferSize        = setting.InputBufferSize;
                mediaServer.OutputQueueSize        = setting.OutputQueueSize;
                mediaServer.OutputBufferSize       = setting.OutputBufferSize;
                mediaServer.OutputSocketBufferSize = setting.OutputSocketBufferSize;
                mediaServer.SetClientValidator(new MediaClientValidator(CommonLog.GetLogger(), remoteValidationURL));
            }

            var servers = m_MediaResourceManager.GetServerList();

            foreach (var item in servers)
            {
                if (item.Start())
                {
                    CommonLog.Info("Media Server is working on port " + item.InputPort
                                   + " (input) and port " + item.OutputPort + " (output) ... ");
                }
            }
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                m_MediaResourceManager.LoadHandlers();

                var appSettings = ConfigurationManager.AppSettings;

                string allAvailableChannels = "";
                if (appSettings.AllKeys.Contains("AvailableChannels"))
                {
                    allAvailableChannels = appSettings["AvailableChannels"];
                }
                if (allAvailableChannels.Length > 0)
                {
                    m_MediaResourceManager.SetAvailableChannelNames(allAvailableChannels);
                }

                string remoteValidationURL = "";
                if (appSettings.AllKeys.Contains("RemoteValidationURL"))
                {
                    remoteValidationURL = appSettings["RemoteValidationURL"];
                }

                Dictionary <string, int> channelInputQueueLengths = null;
                var channelInputQueueLengthSetting = ConfigurationManager.GetSection("channel-input-queue-lengths") as NameValueCollection;
                if (channelInputQueueLengthSetting != null)
                {
                    channelInputQueueLengths = new Dictionary <string, int>();
                    var channelKeys = channelInputQueueLengthSetting.AllKeys;
                    foreach (var key in channelKeys)
                    {
                        int len = 0;
                        if (Int32.TryParse(channelInputQueueLengthSetting[key], out len))
                        {
                            if (!channelInputQueueLengths.ContainsKey(key))
                            {
                                channelInputQueueLengths.Add(key, len);
                            }
                        }
                    }
                }

                var mediaServerSettings = (NameValueCollection)ConfigurationManager.GetSection("media-servers");
                var allKeys             = mediaServerSettings.AllKeys;

                foreach (var key in allKeys)
                {
                    string                json        = mediaServerSettings[key];
                    ServerSetting         setting     = JsonConvert.DeserializeObject <ServerSetting>(json);
                    HttpSourceMediaServer mediaServer = new HttpSourceMediaServer(key, m_MediaResourceManager, CommonLog.GetLogger(),
                                                                                  setting.InputIp, setting.InputPort, setting.OutputIp, setting.OutputPort, setting.InputWhitelist, setting.CertFile, setting.CertKey, channelInputQueueLengths);
                    mediaServer.InputQueueSize         = setting.InputQueueSize;
                    mediaServer.InputBufferSize        = setting.InputBufferSize;
                    mediaServer.OutputQueueSize        = setting.OutputQueueSize;
                    mediaServer.OutputBufferSize       = setting.OutputBufferSize;
                    mediaServer.OutputSocketBufferSize = setting.OutputSocketBufferSize;
                    mediaServer.SetClientValidator(new MediaClientValidator(CommonLog.GetLogger(), remoteValidationURL));
                }

                if (allAvailableChannels.Length > 0)
                {
                    LogMsg("Available Channel Names: " + allAvailableChannels);
                }
                else
                {
                    LogMsg("Any channel name would be accepted");
                }

                if (remoteValidationURL.Length > 0)
                {
                    LogMsg("Remote Validation URL: " + remoteValidationURL);
                }
                else
                {
                    LogMsg("Remote validation has not been set (Any connection would be accepted)");
                }

                var startedServerCount = 0;
                var servers            = m_MediaResourceManager.GetServerList();
                foreach (var server in servers)
                {
                    if (server.Start())
                    {
                        startedServerCount++;
                        if (startedServerCount == 1) // just show the first server's whitelist ...
                        {
                            List <string> list = server.GetSourceWhitelist();
                            listWhitelist.Items.Clear();
                            foreach (var item in list)
                            {
                                listWhitelist.Items.Add(item.ToString());
                            }
                        }
                        LogMsg(server.ServerName + " is working on port " + server.InputPort + " (input) and port " + server.OutputPort + " (output) ... ");
                    }
                    else
                    {
                        LogMsg("Failed to start " + server.ServerName + "! ports: " + server.InputPort + " , " + server.OutputPort);
                    }
                }

                timerRefreshInfo.Enabled = true;
                timerRefreshInfo.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.

            m_MediaResourceManager.Clear();
            m_MediaResourceManager.LoadHandlers();

            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("media-servers");

            try
            {
                ConfigurationManager.RefreshSection("channel-input-queue-lengths");
            }
            catch { }

            CommonLog.Info("=== Media Server is starting ===");

            var appSettings = ConfigurationManager.AppSettings;

            string allAvailableChannels = "";

            if (appSettings.AllKeys.Contains("AvailableChannels"))
            {
                allAvailableChannels = appSettings["AvailableChannels"];
            }
            if (allAvailableChannels.Length > 0)
            {
                m_MediaResourceManager.SetAvailableChannelNames(allAvailableChannels);
            }

            string remoteValidationURL = "";

            if (appSettings.AllKeys.Contains("RemoteValidationURL"))
            {
                remoteValidationURL = appSettings["RemoteValidationURL"];
            }

            Dictionary <string, int> channelInputQueueLengths = null;

            try
            {
                var channelInputQueueLengthSetting = ConfigurationManager.GetSection("channel-input-queue-lengths") as NameValueCollection;
                if (channelInputQueueLengthSetting != null)
                {
                    channelInputQueueLengths = new Dictionary <string, int>();
                    var channelKeys = channelInputQueueLengthSetting.AllKeys;
                    foreach (var key in channelKeys)
                    {
                        int len = 0;
                        if (Int32.TryParse(channelInputQueueLengthSetting[key], out len))
                        {
                            if (!channelInputQueueLengths.ContainsKey(key))
                            {
                                channelInputQueueLengths.Add(key, len);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonLog.Error("Failed to load channel-input-queue-lengths: ");
                CommonLog.Error(ex.ToString());
            }

            var mediaServerSettings = (NameValueCollection)ConfigurationManager.GetSection("media-servers");
            var allKeys             = mediaServerSettings.AllKeys;

            foreach (var key in allKeys)
            {
                string                json        = mediaServerSettings[key];
                ServerSetting         setting     = JsonConvert.DeserializeObject <ServerSetting>(json);
                HttpSourceMediaServer mediaServer = new HttpSourceMediaServer(key, m_MediaResourceManager, CommonLog.GetLogger(),
                                                                              setting.InputIp, setting.InputPort, setting.OutputIp, setting.OutputPort, setting.InputWhitelist, setting.CertFile, setting.CertKey, channelInputQueueLengths);
                mediaServer.InputQueueSize         = setting.InputQueueSize;
                mediaServer.InputBufferSize        = setting.InputBufferSize;
                mediaServer.OutputQueueSize        = setting.OutputQueueSize;
                mediaServer.OutputBufferSize       = setting.OutputBufferSize;
                mediaServer.OutputSocketBufferSize = setting.OutputSocketBufferSize;
                mediaServer.SetClientValidator(new MediaClientValidator(CommonLog.GetLogger(), remoteValidationURL));
            }

            var servers = m_MediaResourceManager.GetServerList();

            foreach (var item in servers)
            {
                if (item.Start())
                {
                    CommonLog.Info("Media Server is working on port " + item.InputPort
                                   + " (input) and port " + item.OutputPort + " (output) ... ");
                }
            }
        }