/// <summary> /// Add a source to hashtables, and possibly raise a SourceRestored event /// </summary> /// <param name="ssrc"></param> /// <param name="cname"></param> public void Add(uint ssrc, string cname) { String cnamekey = cname; // + " " + name; lock (this) { if (!AddedFlags.ContainsKey(ssrc)) { AddedFlags.Add(ssrc, "true"); } if (ActiveStreams.ContainsKey(cnamekey)) { ActiveStreams[cnamekey] = ssrc; } else { ActiveStreams.Add(cnamekey, ssrc); } DateTime previousAddForCname = DateTime.Now; if (RestartTimes.ContainsKey(cnamekey)) { Debug.WriteLine("found previous restart time"); previousAddForCname = (DateTime)RestartTimes[cnamekey]; RestartTimes.Remove(cnamekey); } RestartTimes.Add(cnamekey, DateTime.Now); //Debug.WriteLine("SSRCManager.Add: " + cname +" "+Convert.ToString(ssrc)); //if it was in our running set, update the RtpStream and raise event. if (RunningSet.ContainsKey(cnamekey)) { Debug.WriteLine("found item in running set:" + cnamekey); uint old_ssrc = Convert.ToUInt32(RunningSet[cnamekey]); RunningSet[cnamekey] = ssrc; //Don't raise source restored if the last add came less then 3 seconds ago. if (DateTime.Now - previousAddForCname > new TimeSpan(0, 0, 0, 3, 0)) { //Debug.WriteLine("time qualifies stream for source restored:" + cname); if ((raiseSourceRestored) && (OnSourceRestored != null)) { //Debug.WriteLine("Ready to enqueue sourceRestored"); SourceData sd = new SourceData(); sd.old_ssrc = old_ssrc; sd.new_ssrc = ssrc; sd.cname = cnamekey; ThreadPool.QueueUserWorkItem(new WaitCallback(queueSourceRestored), sd); } } } } }
/// <summary> /// Clear all the hashtables /// </summary> public void Clear() { lock (this) { ActiveStreams.Clear(); RunningSet.Clear(); RestartTimes.Clear(); AddedFlags.Clear(); } }
/// <summary> /// given a cname, return the last time it was restarted /// </summary> /// <param name="cname"></param> /// <returns></returns> public DateTime GetRestartTime(string cname) { if (RestartTimes.Contains(cname)) { return((DateTime)RestartTimes[cname]); } else { return(DateTime.Now); } }
public void SetRestartTime(string cname) { if (RestartTimes.ContainsKey(cname)) { RestartTimes[cname] = DateTime.Now; } else { eventLog.WriteEntry("Restart Time does not exist for cname: " + cname, EventLogEntryType.Error, 1003); //shouldn't happen? } }
public void TryRestart() { DateTime now = DateTime.Now; if (!RestartTimes.Any() || (now.Day == _lastRestartDateTime.Day)) { return; // no restart times defined or there had already been start/restart today } var restartTime = RestartTimes.First(); // TODO: support multiple restart times if (now.IsTimePast(restartTime)) { _reportProcessStatus($"-+ Restarting process '{ProcessName}' PID={ProcessId}."); TryStop(isForced: true); Start(isForced: true); _lastRestartDateTime = DateTime.Now; } }
/// <summary> /// verify that a ssrc seems stable before raising OnSourceRestored /// This was to work around a pathological analog video driver issue /// which caused sources to toggle on and off. We think the issue has /// been fixed now, so the wait time has been removed. /// We'll leave it on the thread pool since the restart does take some /// time to complete. /// </summary> /// <param name="o"></param> private void queueSourceRestored(object o) { string cname = ((SourceData)o).cname; uint old_ssrc = ((SourceData)o).old_ssrc; uint new_ssrc = ((SourceData)o).new_ssrc; if ((!RunningSet.Contains(cname)) || (!RestartTimes.Contains(cname))) { return; } if (Convert.ToUInt32(RunningSet[cname]) != new_ssrc) { return; } if ((raiseSourceRestored) && (OnSourceRestored != null)) { StreamRestoredEventArgs ea = new StreamRestoredEventArgs(new_ssrc, old_ssrc, cname, payload); OnSourceRestored(ea); //raise event } }