/// <summary> /// Check if a method times out and starts to reinitialise AtmoWin if needed. /// </summary> /// <param name="method">Method that needs checking for a timeout.</param> /// <param name="timeout">Timeout in ms.</param> /// <returns>true if not timed out and false if timed out.</returns> private bool TimeoutHandler(System.Action method, int timeout = timeoutComInterface) { try { #if DEBUG method(); return(true); #else long timeoutStart = Win32API.GetTickCount(); var tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; var task = Task.Factory.StartNew(() => method(), token); if (!task.Wait(timeout, token)) { // Stacktrace is needed so we can output the name of the method that timed out. StackTrace trace = new StackTrace(); Log.Error("AtmoWinHandler - {0} timed out after {1}ms!", trace.GetFrame(1).GetMethod().Name, Win32API.GetTickCount() - timeoutStart); ReInitialise(); return(false); } return(true); #endif } catch (AggregateException ex) { StackTrace trace = new StackTrace(); Log.Error("AtmoWinHandler - Error with {0}!", trace.GetFrame(1).GetMethod().Name); foreach (var innerEx in ex.InnerExceptions) { Log.Error("AtmoWinHandler - Exception: {0}", innerEx.Message); } ReInitialise(); return(false); } }
public void UICapture(object sender, EventArgs args) { if (!coreObject.IsConnected() || !coreObject.IsAtmoLightOn() || coreObject.GetCurrentEffect() != ContentEffect.MediaPortalLiveMode) { return; } // Low CPU setting. // Skip frame if LowCPUTime has not yet passed since last frame. if (settings.LowCPU) { if ((Win32API.GetTickCount() - lastFrame) < settings.LowCPUTime) { return; } else { lastFrame = Win32API.GetTickCount(); } } Rectangle rectangleDestination = new Rectangle(0, 0, coreObject.GetCaptureWidth(), coreObject.GetCaptureHeight()); try { if (surfaceDestination == null) { surfaceDestination = SharpDX.Direct3D9.Surface.CreateRenderTarget(SkinContext.Device, coreObject.GetCaptureWidth(), coreObject.GetCaptureHeight(), SharpDX.Direct3D9.Format.A8R8G8B8, SharpDX.Direct3D9.MultisampleType.None, 0, true); } // Use the Player Surface if video is playing. // This results in lower time to calculate aswell as blackbar removal if (ServiceRegistration.Get <IPlayerContextManager>().IsVideoContextActive) { player = ServiceRegistration.Get <IPlayerContextManager>().PrimaryPlayerContext.CurrentPlayer as ISharpDXVideoPlayer; // player.Texture can be null even if VideoContext is active, e.g. at the start of a video if (player.Texture != null) { surfaceSource = player.Texture.GetSurfaceLevel(0); } } else { surfaceSource = SkinContext.Device.GetRenderTarget(0); } if (surfaceSource == null) { return; } surfaceSource.Device.StretchRectangle(surfaceSource, null, surfaceDestination, rectangleDestination, SharpDX.Direct3D9.TextureFilter.None); DataStream stream = SharpDX.Direct3D9.Surface.ToStream(surfaceDestination, SharpDX.Direct3D9.ImageFileFormat.Bmp); coreObject.CalculateBitmap(stream); stream.Close(); stream.Dispose(); } catch (Exception ex) { surfaceDestination.Dispose(); surfaceDestination = null; Log.Error("Error in UICapture."); Log.Error("Exception: {0}", ex.Message); } }