public void Stop() { DebugTrace.TraceVerbose("Stop called by: \r\n" + new StackTrace(1, true).ToString()); if (playerThread != null) { if (playerThread.IsAlive) { running = false; isRecording = false; DebugTrace.TraceInfo("Camera stop request sent. Waiting ..."); playerThread.Join(500); if (playerThread.IsAlive) { DebugTrace.TraceWarning("Killing camera thread"); playerThread.Abort(); playerThread.Join(); } } playerThread = null; } }
private void PlayerWorkerThread(object state) { DebugTrace.TraceInfo(string.Format("Camera started. Buffer size: {0}", playbackBufferSize)); BufferedFrameInfo newFrame; lock (syncRoot) { playbackBuffer.Clear(); newFrame = new BufferedFrameInfo() { FirstIntegratedFrameIndex = -1 }; integratedFramesSoFar = 0; } lastBufferedImage = new int[height, width]; isRecording = false; Profiler.ResetAndStartTimer(100); while (running) { currentFrameIndex++; long startTicks = Profiler.GetTicks(); if (currentFrameIndex > allImagesPixels.Count - 1) { Profiler.StopTimer(100); double msForAllImages = Profiler.GetElapsedMillisecondsForTimer(100); double fps = allImagesPixels.Count * 1000.0 / msForAllImages; DebugTrace.TraceVerbose(string.Format("Camera running at {0} fps", fps.ToString("#0.00"))); currentFrameIndex = 0; Profiler.ResetAndStartTimer(100); } if (playbackBufferSize > 0) { lock (syncRoot) { while (playbackBuffer.Count > playbackBufferSize) { playbackBuffer.Dequeue(); } if (newFrame.FirstIntegratedFrameIndex == -1) { newFrame.FirstIntegratedFrameIndex = currentFrameIndex; newFrame.LastIntegratedFrameIndex = -1; } if (integration > 1) { integratedFramesSoFar++; if (integratedFramesSoFar >= integration) { newFrame.LastIntegratedFrameIndex = currentFrameIndex; newFrame.IsIntegratedFrame = true; } } else { newFrame.LastIntegratedFrameIndex = newFrame.FirstIntegratedFrameIndex; newFrame.IsIntegratedFrame = false; } if (newFrame.LastIntegratedFrameIndex != -1) { currentFrameNo++; newFrame.CurrentFrameNo = currentFrameNo; playbackBuffer.Enqueue(newFrame); newFrame = new BufferedFrameInfo() { FirstIntegratedFrameIndex = -1 }; integratedFramesSoFar = 0; } } } else { // No buffering if (newFrame.FirstIntegratedFrameIndex == -1) { newFrame.FirstIntegratedFrameIndex = currentFrameIndex; newFrame.LastIntegratedFrameIndex = -1; } if (integration > 1) { integratedFramesSoFar++; if (integratedFramesSoFar >= integration) { newFrame.LastIntegratedFrameIndex = currentFrameIndex; newFrame.IsIntegratedFrame = true; } } else { newFrame.LastIntegratedFrameIndex = newFrame.FirstIntegratedFrameIndex; newFrame.IsIntegratedFrame = false; } if (newFrame.LastIntegratedFrameIndex != -1) { currentFrameNo++; newFrame.CurrentFrameNo = currentFrameNo; currentFrame = newFrame; newFrame = new BufferedFrameInfo() { FirstIntegratedFrameIndex = -1 }; integratedFramesSoFar = 0; } if (isRecording) { int[,] pixels = GetCurrentImageFromBufferedFrame(currentFrame); aviTools.AddAviVideoFrame(pixels); } } long endTicks = Profiler.GetTicks(); int msThisFrame = (int)Profiler.TranslateTicksToMilliseconds(endTicks - startTicks); if (msThisFrame < frameWaitTime) { Thread.Sleep(frameWaitTime - msThisFrame); } if (stopRecordingRequested) { stopRecordingRequested = false; isRecording = false; } } DebugTrace.TraceInfo("Camera stopped"); }
private void BufferVideoFrames(IWin32Window ownerForm, string bitmapFilesLocation) { frmLoadingImages frmLoading = OpenProgressForm(ownerForm); try { string[] files = Directory.GetFiles(bitmapFilesLocation, "*.bmp"); allFiles.AddRange(files); allFiles.Sort(); if (allFiles.Count > 0) { width = 0; height = 0; // TODO: Use .NET 4 multithreaded loading for (int i = 0; i < allFiles.Count; i++) { var bmp = new Bitmap(allFiles[i]); if (width == 0 && height == 0) { width = bmp.Width; height = bmp.Height; } else if (width != bmp.Width || height != bmp.Height) { throw new ApplicationException("All bitmaps must be the same size."); } int[,] pixels = CopyBitmapPixels(bmp); allImagesPixels.Add(pixels); frmLoading.SetProgress(i + 1, allFiles.Count); } errorBitmap = null; errorPixels = null; bufferedImageWidth = width; bufferedImageHeight = height; DebugTrace.TraceInfo(string.Format("Loaded {0} camera images from '{1}'", allFiles.Count, bitmapFilesLocation)); } else { errorBitmap = new Bitmap(width, height); PrepareErrorMessage(string.Format("No bmp file found in '{0}'", bitmapFilesLocation)); errorPixels = CopyBitmapPixels(errorBitmap); DebugTrace.TraceError(string.Format("No camera images found in '{0}'", bitmapFilesLocation)); } } catch (Exception ex) { DebugTrace.TraceError(ex); errorBitmap = new Bitmap(width, height); PrepareErrorMessage(ex.Message); errorPixels = CopyBitmapPixels(errorBitmap); } finally { CloseProgressForm(frmLoading); } }