private void process_NewFrame(object stateInfo) { Thread.CurrentThread.Priority = ThreadPriority.Highest; try { // get new one if (imgBuffer.Count > 0) { lastFrame = (VsImage)imgBuffer.Dequeue(); } if (lastFrame != null) { using (Graphics gb = Graphics.FromImage(lastFrame.Image)) { SolidBrush drawRec = new SolidBrush(Color.Black); gb.FillRectangle(drawRec, 0, 0, lastFrame.Image.Width, 16); //gb.DrawImage(isysLogo, 3, 3, 15, 15); DateTime date = DateTime.Now; // Create font and brush Font drawFont = new Font("Tahoma", 10, FontStyle.Bold); SolidBrush drawBrush = new SolidBrush(Color.White); gb.DrawString(date.ToString(), drawFont, drawBrush, new PointF(18, 0)); drawBrush.Dispose(); drawFont.Dispose(); drawRec.Dispose(); } // output if (FrameOut != null) { FrameOut(this, new VsImageEventArgs(lastFrame)); } } } catch (Exception err) { logger.Log(LogLevel.Error, err.Message + " " + err.Source + " " + err.StackTrace);; } finally { if (lastFrame != null) { lastFrame.Dispose(); } lastFrame = null; } }
// new frame protected void OnNewFrame(Bitmap image) { framesReceived++; if (FrameOut != null) { VsImage img = new VsImage(image); FrameOut(this, new VsImageEventArgs(img)); img.Dispose(); img = null; } }
// new frame protected void OnNewFrame(Bitmap image) { framesReceived++; if ((!stopEvent.WaitOne(0, true)) && (FrameOut != null)) { VsImage img = new VsImage(image); // notify client FrameOut(this, new VsImageEventArgs(img)); // release the image img.Dispose(); img = null; } }
public void FrameIn(object sender, VsImageEventArgs e) { try { if (imgBuffer.Count > 1000 / syncTimer) { VsImage rm = (VsImage)imgBuffer.Dequeue(); rm.Dispose(); rm = null; logger.Log(LogLevel.Warn, DateTime.Now.ToString() + "; frame removed from Streamer"); } VsImage img = (VsImage)e.Image.Clone(); imgBuffer.Enqueue(img); } catch (Exception err) { logger.Log(LogLevel.Error, err.Message + " " + err.Source + " " + err.StackTrace); } }
// On new frame ready //[MethodImpl(MethodImplOptions.Synchronized)] public void FrameIn(object sender, VsImageEventArgs e) { Thread.CurrentThread.Priority = ThreadPriority.Highest; try { if (imgBuffer.Count >= 1) { VsImage rm = (VsImage)imgBuffer.Dequeue(); rm.Dispose(); rm = null; } VsImage img = (VsImage)e.Image.Clone(); imgBuffer.Enqueue(img); } catch (Exception err) { logger.Log(LogLevel.Error, err.Message + " " + err.Source + " " + err.StackTrace);; } }
// Input 3 //[MethodImpl(MethodImplOptions.Synchronized)] public void FrameIn3(object sender, VsImageEventArgs e) { Thread.CurrentThread.Priority = ThreadPriority.Highest; try { if (imgBuffer3.Count >= bufCount) { VsImage rm = (VsImage)imgBuffer3.Dequeue(); rm.Dispose(); rm = null; logger.Log(LogLevel.Warn, DateTime.Now.ToString() + "; frame removed from Analyzer"); } VsImage img = (VsImage)e.Image.Clone(); img.IsDetected = false; img.IsAnalyzed = false; imgBuffer3.Enqueue(img); } catch (Exception err) { logger.Log(LogLevel.Error, err.Message + " " + err.Source + " " + err.StackTrace);; } }
private void FrameReceived(object sender, RtpStream.FrameReceivedEventArgs ea) { try { if (firstFrame) { Bitmap DrawImage = new Bitmap(video_width, video_height); EncoderParameter epQuality = new EncoderParameter(Encoder.Quality, video_quality); // Store the quality parameter in the list of encoder parameters EncoderParameters epParameters = new EncoderParameters(1); epParameters.Param[0] = epQuality; MemoryStream ms = new MemoryStream(); DrawImage.Save(ms, GetImageCodecInfo(ImageFormat.Jpeg), epParameters); Array.Copy(ms.GetBuffer(), 0, JpegHeader, 0, offset); firstFrame = false; } byte[] data = new byte[ea.Frame.Buffer.Length + offset]; Array.Copy(JpegHeader, data, JpegHeader.Length); Array.Copy(ea.Frame.Buffer, 0, data, offset, ea.Frame.Buffer.Length); System.IO.MemoryStream msImage = new MemoryStream(data); VsImage img = new VsImage((Bitmap)Image.FromStream(msImage)); int index = GetIndex(ea.RtpStream.IPAddress); if (index != -1 && vsRtpStream[index] != null && vsRtpStream[index].FrameOut != null) { vsRtpStream[index].FrameOut(this, new VsImageEventArgs(img)); } img.Dispose(); img = null; } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void process_NewFrame(object sender, System.Timers.ElapsedEventArgs e) { Thread.CurrentThread.Priority = ThreadPriority.Highest; // Stop analyzer StopAnalyzer(); try { // input frame if (numInput >= 5) { if (lastFrame5 == null && imgBuffer5.Count > 0) { lastFrame5 = (VsImage)imgBuffer5.Dequeue(); } if (lastFrame5 == null) { return; } } if (numInput >= 4) { if (lastFrame4 == null && imgBuffer4.Count > 0) { lastFrame4 = (VsImage)imgBuffer4.Dequeue(); } if (lastFrame4 == null) { return; } } if (numInput >= 3) { if (lastFrame3 == null && imgBuffer3.Count > 0) { lastFrame3 = (VsImage)imgBuffer3.Dequeue(); } if (lastFrame3 == null) { return; } } if (numInput >= 2) { if (lastFrame2 == null && imgBuffer2.Count > 0) { lastFrame2 = (VsImage)imgBuffer2.Dequeue(); } if (lastFrame2 == null) { return; } } if (numInput >= 1) { if (lastFrame1 == null && imgBuffer1.Count > 0) { lastFrame1 = (VsImage)imgBuffer1.Dequeue(); } if (lastFrame1 == null) { return; } } // process if (numInput == 5) { process_Frame5(lastFrame1, lastFrame2, lastFrame3, lastFrame4, lastFrame5); } if (numInput == 4) { process_Frame4(lastFrame1, lastFrame2, lastFrame3, lastFrame4); } if (numInput == 3) { process_Frame3(lastFrame1, lastFrame2, lastFrame3); } if (numInput == 2) { process_Frame2(lastFrame1, lastFrame2); } if (numInput == 1) { process_Frame1(lastFrame1); } if (lastFrame1 != null) { if (lastFrame1.IsDetected) { if (!lastMotion) { // new motion occurs // alert motion EventAlert(); } // still moving timeSave = interLength; lastMotion = true; lastResult = lastFrame1.Result; } else { if (timeSave > 0) { // motion smoothing lastFrame1.IsDetected = true; lastFrame1.Result = lastResult; lastMotion = true; } else { // motion disappear lastMotion = false; lastResult = ""; } } // draw image DrawMotion(lastFrame1.Image, lastFrame1.IsDetected); } // send out if (numOutput >= 5) { if (FrameOut5 != null) { FrameOut5(this, new VsImageEventArgs(lastFrame5)); } } if (numOutput >= 4) { if (FrameOut4 != null) { FrameOut4(this, new VsImageEventArgs(lastFrame4)); } } if (numOutput >= 3) { if (FrameOut3 != null) { FrameOut3(this, new VsImageEventArgs(lastFrame3)); } } if (numOutput >= 2) { if (FrameOut2 != null) { FrameOut2(this, new VsImageEventArgs(lastFrame2)); } } if (numOutput >= 1) { if (FrameOut1 != null) { FrameOut1(this, new VsImageEventArgs(lastFrame1)); } } } catch (Exception err) { logger.Log(LogLevel.Error, err.Message + " " + err.Source + " " + err.StackTrace);; } finally { // free buffer if (numInput >= 5 && lastFrame5 != null) { lastFrame5.Dispose(); lastFrame5 = null; } if (numInput >= 4 && lastFrame4 != null) { lastFrame4.Dispose(); lastFrame4 = null; } if (numInput >= 3 && lastFrame3 != null) { lastFrame3.Dispose(); lastFrame3 = null; } if (numInput >= 2 && lastFrame2 != null) { lastFrame2.Dispose(); lastFrame2 = null; } if (numInput >= 1 && lastFrame1 != null) { lastFrame1.Dispose(); lastFrame1 = null; } // Start analyzer StartAnalyzer(); } }
// Thread entry point public void WorkerThread() { byte[] buffer = new byte[bufSize]; // buffer to read stream while (true) { // reset reload event reloadEvent.Reset(); HttpWebRequest req = null; WebResponse resp = null; Stream stream = null; byte[] delimiter = null; byte[] delimiter2 = null; byte[] boundary = null; int boundaryLen, delimiterLen = 0, delimiter2Len = 0; int read, todo = 0, total = 0, pos = 0, align = 1; int start = 0, stop = 0; // align // 1 = searching for image start // 2 = searching for image end try { // create request req = (HttpWebRequest)WebRequest.Create(source); // set login and password if ((login != null) && (password != null) && (login != "")) { req.Credentials = new NetworkCredential(login, password); } // set connection group name if (useSeparateConnectionGroup) { req.ConnectionGroupName = GetHashCode().ToString(); } // get response resp = req.GetResponse(); // check content type string ct = resp.ContentType; if (ct.IndexOf("multipart/x-mixed-replace") == -1) { throw new ApplicationException("Invalid URL"); } // get boundary ASCIIEncoding encoding = new ASCIIEncoding(); boundary = encoding.GetBytes(ct.Substring(ct.IndexOf("boundary=", 0) + 9)); boundaryLen = boundary.Length; // get response stream stream = resp.GetResponseStream(); // loop while ((!stopEvent.WaitOne(0, true)) && (!reloadEvent.WaitOne(0, true))) { // check total read if (total > bufSize - readSize) { total = pos = todo = 0; } // read next portion from stream if ((read = stream.Read(buffer, total, readSize)) == 0) { throw new ApplicationException(); } total += read; todo += read; // increment received bytes counter bytesReceived += read; // does we know the delimiter ? if (delimiter == null) { // find boundary pos = ByteArrayUtils.Find(buffer, boundary, pos, todo); if (pos == -1) { // was not found todo = boundaryLen - 1; pos = total - todo; continue; } todo = total - pos; if (todo < 2) { continue; } // check new line delimiter type if (buffer[pos + boundaryLen] == 10) { delimiterLen = 2; delimiter = new byte[2] { 10, 10 }; delimiter2Len = 1; delimiter2 = new byte[1] { 10 }; } else { delimiterLen = 4; delimiter = new byte[4] { 13, 10, 13, 10 }; delimiter2Len = 2; delimiter2 = new byte[2] { 13, 10 }; } pos += boundaryLen + delimiter2Len; todo = total - pos; } // search for image if (align == 1) { start = ByteArrayUtils.Find(buffer, delimiter, pos, todo); if (start != -1) { // found delimiter start += delimiterLen; pos = start; todo = total - pos; align = 2; } else { // delimiter not found todo = delimiterLen - 1; pos = total - todo; } } // search for image end while ((align == 2) && (todo >= boundaryLen)) { stop = ByteArrayUtils.Find(buffer, boundary, pos, todo); if (stop != -1) { pos = stop; todo = total - pos; // increment frames counter framesReceived++; // image at stop if (FrameOut != null) { Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, start, stop - start)); VsImage img = new VsImage(bmp); // notify client FrameOut(this, new VsImageEventArgs(img)); // release the image img.Dispose(); img = null; } // shift array pos = stop + boundaryLen; todo = total - pos; Array.Copy(buffer, pos, buffer, 0, todo); total = todo; pos = 0; align = 1; } else { // delimiter not found todo = boundaryLen - 1; pos = total - todo; } } } } catch (WebException ex) { System.Diagnostics.Debug.WriteLine("=============: " + ex.Message); // wait for a while before the next try Thread.Sleep(250); } catch (ApplicationException ex) { System.Diagnostics.Debug.WriteLine("=============: " + ex.Message); // wait for a while before the next try Thread.Sleep(250); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("=============: " + ex.Message); } finally { // abort request if (req != null) { req.Abort(); req = null; } // close response stream if (stream != null) { stream.Close(); stream = null; } // close response if (resp != null) { resp.Close(); resp = null; } } // need to stop ? if (stopEvent.WaitOne(0, true)) { break; } } }
// Thread entry point public void WorkerThread() { byte[] buffer = new byte[bufSize]; // buffer to read stream HttpWebRequest req = null; WebResponse resp = null; Stream stream = null; Random rnd = new Random((int)DateTime.Now.Ticks); DateTime start; TimeSpan span; while (true) { int read, total = 0; try { start = DateTime.Now; // create request if (!preventCaching) { req = (HttpWebRequest)WebRequest.Create(source); } else { req = (HttpWebRequest)WebRequest.Create(source + ((source.IndexOf('?') == -1) ? '?' : '&') + "fake=" + rnd.Next().ToString()); } // set login and password if ((login != null) && (password != null) && (login != "")) { req.Credentials = new NetworkCredential(login, password); } // set connection group name if (useSeparateConnectionGroup) { req.ConnectionGroupName = GetHashCode().ToString(); } // get response resp = req.GetResponse(); // get response stream stream = resp.GetResponseStream(); // loop while (!stopEvent.WaitOne(0, true)) { // check total read if (total > bufSize - readSize) { total = 0; } // read next portion from stream if ((read = stream.Read(buffer, total, readSize)) == 0) { break; } total += read; // increment received bytes counter bytesReceived += read; } if (!stopEvent.WaitOne(0, true)) { // increment frames counter framesReceived++; // image at stop if (FrameOut != null) { Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total)); // notify client VsImage img = new VsImage(bmp); FrameOut(this, new VsImageEventArgs(img)); // release the image img.Dispose(); img = null; } } // wait for a while ? if (frameInterval > 0) { // times span span = DateTime.Now.Subtract(start); // miliseconds to sleep int msec = frameInterval - (int)span.TotalMilliseconds; while ((msec > 0) && (stopEvent.WaitOne(0, true) == false)) { // sleeping ... Thread.Sleep((msec < 100) ? msec : 100); msec -= 100; } } } catch (WebException ex) { System.Diagnostics.Debug.WriteLine("=============: " + ex.Message); // wait for a while before the next try Thread.Sleep(250); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("=============: " + ex.Message); } finally { // abort request if (req != null) { req.Abort(); req = null; } // close response stream if (stream != null) { stream.Close(); stream = null; } // close response if (resp != null) { resp.Close(); resp = null; } } // need to stop ? if (stopEvent.WaitOne(0, true)) { break; } } }
private void process_NewFrame(object sender, System.Timers.ElapsedEventArgs e) { Thread.CurrentThread.Priority = ThreadPriority.Highest; // stop encoder StopEncoder(); VsImage lastFrame = null; try { // get new one if (imgBuffer.Count > 0) { lastFrame = (VsImage)imgBuffer.Dequeue(); } if (lastFrame != null) { // process if (lastFrame.IsDetected) { if (++thumbCount > thumbMax && !thumbSave) { using (Graphics gp = Graphics.FromImage(thumbImage)) { gp.DrawImage(lastFrame.Image, 0, 0, 160, 120); } thumbSave = true; EmailAlert(lastFrame.Image); } } // process process_Frame(lastFrame); // send output if (FrameOut != null) { FrameOut(this, new VsImageEventArgs(lastFrame)); } } } catch (Exception err) { logger.Log(LogLevel.Error, err.Message + " " + err.Source + " " + err.StackTrace); } finally { // free release if (lastFrame != null) { lastFrame.Dispose(); } lastFrame = null; // restart encoder StartEncoder(); } }