Change keyframe motion to use a single timer for all objects. This is required

to prevent slippage between objects that are meant to move synchronously or
keep their relative positions/rotations.
avinationmerge
Melanie 2013-01-30 15:54:14 +01:00
parent 2a558c7346
commit d1ebb0a8f9
1 changed files with 246 additions and 253 deletions

View File

@ -22,6 +22,77 @@ using log4net;
namespace OpenSim.Region.Framework.Scenes
{
public static class KeyframeTimer
{
private static Timer m_timer;
private static Dictionary<KeyframeMotion, object> m_motions = new Dictionary<KeyframeMotion, object>();
private static object m_lockObject = new object();
private static object m_timerLock = new object();
public const double timerInterval = 50.0;
static KeyframeTimer()
{
m_timer = new Timer();
m_timer.Interval = timerInterval;
m_timer.AutoReset = true;
m_timer.Elapsed += OnTimer;
m_timer.Start();
}
private static void OnTimer(object sender, ElapsedEventArgs ea)
{
if (!Monitor.TryEnter(m_timerLock))
return;
try
{
List<KeyframeMotion> motions;
lock (m_lockObject)
{
motions = new List<KeyframeMotion>(m_motions.Keys);
}
foreach (KeyframeMotion m in motions)
{
try
{
m.OnTimer();
}
catch (Exception inner)
{
// Don't stop processing
}
}
}
catch (Exception e)
{
// Keep running no matter what
}
finally
{
Monitor.Exit(m_timerLock);
}
}
public static void Add(KeyframeMotion motion)
{
lock (m_lockObject)
{
m_motions[motion] = null;
}
}
public static void Remove(KeyframeMotion motion)
{
lock (m_lockObject)
{
m_motions.Remove(motion);
}
}
}
[Serializable]
public class KeyframeMotion
{
@ -63,18 +134,6 @@ namespace OpenSim.Region.Framework.Scenes
private Keyframe[] m_keyframes;
[NonSerialized()]
protected Timer m_timer = null;
// timer lock
[NonSerialized()]
private object m_onTimerLock;
// timer overrun detect
// prevents overlap or timer events threads frozen on the lock
[NonSerialized()]
private bool m_inOnTimer;
// skip timer events.
//timer.stop doesn't assure there aren't event threads still being fired
[NonSerialized()]
@ -102,7 +161,7 @@ namespace OpenSim.Region.Framework.Scenes
private int m_iterations = 0;
private const double timerInterval = 50.0;
private int m_skipLoops = 0;
public DataFormat Data
{
@ -139,31 +198,16 @@ namespace OpenSim.Region.Framework.Scenes
private void StartTimer()
{
if (m_timer == null)
return;
KeyframeTimer.Add(this);
m_timerStopped = false;
m_timer.Start();
}
private void StopTimer()
{
if (m_timer == null || m_timerStopped)
return;
m_timerStopped = true;
m_timer.Stop();
KeyframeTimer.Remove(this);
}
private void RemoveTimer()
{
if (m_timer == null)
return;
m_timerStopped = true;
m_timer.Stop();
m_timer.Elapsed -= OnTimer;
m_timer = null;
}
public static KeyframeMotion FromData(SceneObjectGroup grp, Byte[] data)
{
KeyframeMotion newMotion = null;
@ -180,9 +224,7 @@ namespace OpenSim.Region.Framework.Scenes
if (grp != null && grp.IsSelected)
newMotion.m_selected = true;
newMotion.m_onTimerLock = new object();
newMotion.m_timerStopped = false;
newMotion.m_inOnTimer = false;
newMotion.m_isCrossing = false;
newMotion.m_waitingCrossing = false;
}
@ -196,8 +238,6 @@ namespace OpenSim.Region.Framework.Scenes
public void UpdateSceneObject(SceneObjectGroup grp)
{
// lock (m_onTimerLock)
{
m_isCrossing = false;
m_waitingCrossing = false;
StopTimer();
@ -227,7 +267,6 @@ namespace OpenSim.Region.Framework.Scenes
if (m_running)
Start();
}
}
public KeyframeMotion(SceneObjectGroup grp, PlayMode mode, DataFormat data)
{
@ -241,9 +280,7 @@ namespace OpenSim.Region.Framework.Scenes
m_baseRotation = grp.GroupRotation;
}
m_onTimerLock = new object();
m_timerStopped = true;
m_inOnTimer = false;
m_isCrossing = false;
m_waitingCrossing = false;
}
@ -296,7 +333,7 @@ namespace OpenSim.Region.Framework.Scenes
public void Delete()
{
m_running = false;
RemoveTimer();
StopTimer();
m_isCrossing = false;
m_waitingCrossing = false;
m_frames.Clear();
@ -309,27 +346,13 @@ namespace OpenSim.Region.Framework.Scenes
m_waitingCrossing = false;
if (m_keyframes != null && m_group != null && m_keyframes.Length > 0)
{
if (m_timer == null)
{
m_timer = new Timer();
m_timer.Interval = timerInterval;
m_timer.AutoReset = true;
m_timer.Elapsed += OnTimer;
}
else
{
StopTimer();
m_timer.Interval = timerInterval;
}
m_inOnTimer = false;
StartTimer();
m_running = true;
}
else
{
m_running = false;
RemoveTimer();
StopTimer();
}
}
@ -339,7 +362,7 @@ namespace OpenSim.Region.Framework.Scenes
m_isCrossing = false;
m_waitingCrossing = false;
RemoveTimer();
StopTimer();
m_basePosition = m_group.AbsolutePosition;
m_baseRotation = m_group.GroupRotation;
@ -354,7 +377,7 @@ namespace OpenSim.Region.Framework.Scenes
public void Pause()
{
m_running = false;
RemoveTimer();
StopTimer();
m_group.RootPart.Velocity = Vector3.Zero;
m_group.RootPart.AngularVelocity = Vector3.Zero;
@ -377,15 +400,11 @@ namespace OpenSim.Region.Framework.Scenes
int start = 0;
int end = m_keyframes.Length;
// if (m_mode == PlayMode.PingPong && m_keyframes.Length > 1)
// end = m_keyframes.Length - 1;
if (direction < 0)
{
start = m_keyframes.Length - 1;
end = -1;
// if (m_mode == PlayMode.PingPong && m_keyframes.Length > 1)
// end = 0;
}
for (int i = start; i != end ; i += direction)
@ -463,39 +482,30 @@ namespace OpenSim.Region.Framework.Scenes
}
}
protected void OnTimer(object sender, ElapsedEventArgs e)
public void OnTimer()
{
if (m_timerStopped) // trap events still in air even after a timer.stop
return;
if (m_inOnTimer) // don't let overruns to happen
if (m_skipLoops > 0)
{
m_log.Warn("[KeyFrame]: timer overrun");
m_skipLoops--;
return;
}
if (m_timerStopped) // trap events still in air even after a timer.stop
return;
if (m_group == null)
return;
lock (m_onTimerLock)
{
m_inOnTimer = true;
bool update = false;
try
{
if (m_selected)
{
if (m_group.RootPart.Velocity != Vector3.Zero)
{
m_group.RootPart.Velocity = Vector3.Zero;
m_group.SendGroupRootTerseUpdate();
// m_group.RootPart.ScheduleTerseUpdate();
}
m_inOnTimer = false;
return;
}
@ -510,10 +520,8 @@ namespace OpenSim.Region.Framework.Scenes
if (!m_isCrossing)
{
StopTimer();
m_timer.Interval = timerInterval;
StartTimer();
}
m_inOnTimer = false;
return;
}
@ -524,7 +532,6 @@ namespace OpenSim.Region.Framework.Scenes
if (m_frames.Count == 0)
{
Stop();
m_inOnTimer = false;
Scene scene = m_group.Scene;
IScriptModule[] scriptModules = scene.RequestModuleInterfaces<IScriptModule>();
@ -539,16 +546,16 @@ namespace OpenSim.Region.Framework.Scenes
}
m_currentFrame = m_frames[0];
m_currentFrame.TimeMS += (int)timerInterval;
m_currentFrame.TimeMS += (int)KeyframeTimer.timerInterval;
//force a update on a keyframe transition
update = true;
}
m_currentFrame.TimeMS -= (int)timerInterval;
m_currentFrame.TimeMS -= (int)KeyframeTimer.timerInterval;
// Do the frame processing
double steps = (double)m_currentFrame.TimeMS / timerInterval;
double steps = (double)m_currentFrame.TimeMS / KeyframeTimer.timerInterval;
if (steps <= 0.0)
{
@ -639,23 +646,8 @@ namespace OpenSim.Region.Framework.Scenes
}
if (update)
{
m_group.SendGroupRootTerseUpdate();
// m_group.RootPart.ScheduleTerseUpdate();
}
catch ( Exception ex)
{
// still happening sometimes
// lets try to see where
m_log.Warn("[KeyFrame]: timer overrun" + ex.Message);
}
finally
{
// make sure we do not let this frozen
m_inOnTimer = false;
}
}
}
@ -706,9 +698,10 @@ namespace OpenSim.Region.Framework.Scenes
m_group.SendGroupRootTerseUpdate();
// m_group.RootPart.ScheduleTerseUpdate();
if (m_running && m_timer != null)
if (m_running)
{
m_timer.Interval = 60000;
StopTimer();
m_skipLoops = 1200; // 60 seconds
StartTimer();
}
}