/// <summary> /// Starts an ezstream process for the given channel id /// </summary> /// <param name="channelId">Channel id for the channel which should have an ezstram started</param> private void StartEzstreamProcess(int channelId) { _logger.AddEntry("Start starting ezstream process for channel with id: [" + channelId + "]"); if (!IsChannelStreamRunning(channelId)) //Check if the channel already has a running stream { //Start set up the process //Path to ezstream executable string ezPath = FilePath.ITUEzStreamPath.GetPath(); //Create the arguments to ezstream string xmlFilePath; xmlFilePath = FilePath.ITUChannelConfigPath.GetPath() + channelId.ToString() + ".xml"; string arguments = "-c " + xmlFilePath; //Start set up process-info ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + ezPath + " " + arguments); //Default is true, it should be false for ezstream startInfo.UseShellExecute = false; //It should not create a new window for the ezstream process startInfo.CreateNoWindow = true; //Create the process for ezstream EzProcess p = new EzProcess(channelId); p.StartInfo = startInfo; _logger.AddEntry("Starting process for channel with id: [" + channelId + "]"); try { p.Start(); } catch (Exception e) { _logger.AddEntry("Exception when trying to start ezprocess for channel with id: [" + channelId + "]. EzStream path: [" + ezPath + "] - XmlFilePath: [" + xmlFilePath + "] - Arguments: [" + arguments + "]. Exception: " + e); throw e; } _logger.AddEntry("Process started for channel with id: [" + channelId + "]"); //Start asynchronous assignment of process id to the newly created process Task t = new Task(() => AssignProcessId(p)); t.Start(); //Add this process to the dictionary with running channels runningChannelIds.Add(channelId, p); } else // Channel already has a running ezstream { _logger.AddEntry("Channel with id: [" + channelId + "] is already running"); throw new ChannelRunningException("Channel with id: [" + channelId + "] is already running"); } }
/// <summary> /// Sets the RealProcessId property for the given EzProcess /// This is used because the default EzProcess.id is the id of "cmd" and not the "ezstream" process /// </summary> /// <param name="p">Process which should have assigned its RealProcessId property</param> private void AssignProcessId(EzProcess p) { _logger.AddEntry("Start assign process id for channel with id: [" + p.ChannelId + "]"); Thread.Sleep(1000); _logger.AddEntry("Sleep has finished for channel with id: [" + p.ChannelId + "]"); //Loop through all windows processes names "ezstream" bool sucess = false; foreach (Process process in Process.GetProcessesByName("ezstream")) { if (!ezstreamProcessIds.Contains(process.Id)) //If the windows process has an id which is not in the list of already running process ids { //Set the RealProcessId p.RealProcessId = process.Id; //Add the id to the list of running processes ids ezstreamProcessIds.Add(process.Id); sucess = true; _logger.AddEntry("Process for channel with id: [" + p.ChannelId + "] has been assigned process id: [" + p.RealProcessId + "]"); break; } } if (!sucess) { _logger.AddEntry("Unable to find process id for channel with id: [" + p.ChannelId + "]"); } }