private void SendLiveFeed(int CameraID, int Width, int Height, string sHttpVersion, ref Socket mySocket) { MemoryStream imageStream = null; try { CameraWindow _cw = this.Parent.GetCameraWindow(Convert.ToInt32(CameraID)); if (_cw != null && !_cw.Camera.LastFrameNull) { if (!String.IsNullOrEmpty(_cw.Camobject.encodekey)) { string msg = "Cannot view live feed when the stream is encrypted"; SendHeader(sHttpVersion, "text/html", msg.Length, " 200 OK", 0, ref mySocket); SendToBrowser(msg, ref mySocket); return; } imageStream = new MemoryStream(); Bitmap _b = _cw.Camera.LastFrame; if (Width != -1) { Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); Image myThumbnail = _b.GetThumbnailImage(Width, Height, myCallback, IntPtr.Zero); // put the image into the memory stream myThumbnail.Save(imageStream, ImageFormat.Jpeg); myThumbnail.Dispose(); } else { _b.Save(imageStream, ImageFormat.Jpeg); } // make byte array the same size as the image byte[] imageContent; imageContent = new Byte[imageStream.Length]; imageStream.Position = 0; // load the byte array with the image imageStream.Read(imageContent, 0, (int)imageStream.Length); // rewind the memory stream SendHeader(sHttpVersion, ".jpg", (int)imageStream.Length, " 200 OK", 0, ref mySocket); SendToBrowser(imageContent, ref mySocket); _b.Dispose(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Server Error (livefeed): " + ex.Message); MainForm.LogExceptionToFile(ex); } if (imageStream != null) { imageStream.Dispose(); } }
//This method Accepts new connection and //First it receives the welcome massage from the client, //Then it sends the Current date time to the Client. public void StartListen() { while (Running && numErr < 5) { //Accept a new connection try { Socket mySocket = myListener.AcceptSocket(); if (MySockets.Count() < _socketindex + 1) { MySockets.Add(mySocket); } else { MySockets[_socketindex] = mySocket; } if (mySocket.Connected) { mySocket.NoDelay = true; mySocket.ReceiveBufferSize = 8192; mySocket.ReceiveTimeout = iSpyServer.Default.ServerReceiveTimeout; try { //make a byte array and receive data from the client string sBuffer; string sHttpVersion; Byte[] bReceive = new Byte[1024]; mySocket.Receive(bReceive); sBuffer = Encoding.ASCII.GetString(bReceive); if (sBuffer.Substring(0, 4) == "TALK") { var socket = mySocket; var feed = new Thread(p => AudioIn(socket)); _socketindex++; feed.Start(); continue; } if (sBuffer.Substring(0, 3) != "GET") { continue; } int iStartPos = sBuffer.IndexOf("HTTP", 1); sHttpVersion = sBuffer.Substring(iStartPos, 8); int cid = -1, vid = -1, camid = -1; int w = -1, h = -1; string qs = sBuffer.Substring(4); qs = qs.Substring(0, qs.IndexOf(" ")).Trim('/').Trim('?'); string[] nvs = qs.Split('&'); foreach (string s in nvs) { string[] nv = s.Split('='); switch (nv[0].ToLower()) { case "c": cid = Convert.ToInt32(nv[1]); break; case "w": w = Convert.ToInt32(nv[1]); break; case "h": h = Convert.ToInt32(nv[1]); break; case "camid": camid = Convert.ToInt32(nv[1]); //mjpeg break; case "micid": vid = Convert.ToInt32(nv[1]); break; } } if (cid != -1) { SendLiveFeed(cid, w, h, sHttpVersion, ref mySocket); } else { if (camid != -1) { CameraWindow cw = Parent.GetCameraWindow(Convert.ToInt32(camid)); if (cw.Camobject.settings.active) { String sResponse = ""; sResponse += "HTTP/1.1 200 OK\r\n"; sResponse += "Server: iSpy\r\n"; sResponse += "Expires: 0\r\n"; sResponse += "Pragma: no-cache\r\n"; sResponse += "Content-Type: multipart/x-mixed-replace;boundary=--myboundary"; Byte[] bSendData = Encoding.ASCII.GetBytes(sResponse); SendToBrowser(bSendData, mySocket); cw.OutSockets.Add(mySocket); _socketindex++; continue; } } else { if (vid != -1) { VolumeLevel vl = Parent.GetMicrophone(Convert.ToInt32(vid)); if (vl != null) { String sResponse = ""; sResponse += "HTTP/1.1 200 OK\r\n"; sResponse += "Server: iSpy\r\n"; sResponse += "Expires: 0\r\n"; sResponse += "Pragma: no-cache\r\n"; sResponse += "Content-Type: multipart/x-mixed-replace;boundary=--myboundary"; sResponse += "\r\n\r\n"; Byte[] bSendData = Encoding.ASCII.GetBytes(sResponse); SendToBrowser(bSendData, mySocket); vl.OutSockets.Add(mySocket); _socketindex++; continue; } } else { string _resp = "iSpy server is running"; SendHeader(sHttpVersion, "", _resp.Length, " 200 OK", 0, ref mySocket); SendToBrowser(_resp, ref mySocket); } } } numErr = 0; } catch (SocketException ex) { System.Diagnostics.Debug.WriteLine("Server Error (socket): " + ex.Message); MainForm.LogExceptionToFile(ex); numErr++; } mySocket.Close(); mySocket = null; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Server Error (generic): " + ex.Message); MainForm.LogExceptionToFile(ex); numErr++; } } }
private void AddCamera(int videoSourceIndex) { var oc = new objectsCamera { alerts = new objectsCameraAlerts(), detector = new objectsCameraDetector { motionzones = new objectsCameraDetectorZone [0] } }; oc.notifications = new objectsCameraNotifications(); oc.recorder = new objectsCameraRecorder(); oc.schedule = new objectsCameraSchedule {entries = new objectsCameraScheduleEntry[0]}; oc.settings = new objectsCameraSettings(); oc.ftp = new objectsCameraFtp(); oc.id = -1; oc.directory = RandomString(5); oc.ptz = -1; oc.x = 0; oc.y = 0; oc.flipx = oc.flipy = false; oc.width = 320; oc.height = 240; oc.name = ""; oc.description = ""; oc.resolution = "320x240"; oc.newrecordingcount = 0; oc.alerts.active = false; oc.alerts.mode = "movement"; oc.alerts.alertoptions = "false,false"; oc.alerts.objectcountalert = 1; oc.alerts.minimuminterval = 180; oc.notifications.sendemail = false; oc.notifications.sendsms = false; oc.notifications.sendmms = false; oc.notifications.emailgrabinterval = 0; oc.ftp.enabled = false; oc.ftp.port = 21; oc.ftp.server = "ftp://"; oc.ftp.interval = 10; oc.ftp.filename = "mylivecamerafeed.jpg"; oc.ftp.ready = true; oc.ftp.text = "www.ispyconnect.com"; oc.schedule.active = false; oc.settings.active = false; oc.settings.deleteavi = true; oc.settings.ffmpeg = iSpyServer.Default.FFMPEG_Camera; oc.settings.emailaddress = EmailAddress; oc.settings.smsnumber = MobileNumber; oc.settings.suppressnoise = true; oc.settings.login = ""; oc.settings.password = ""; oc.settings.useragent = "Mozilla/5.0"; oc.settings.frameinterval = 10; oc.settings.sourceindex = videoSourceIndex; oc.settings.micpair = -1; oc.settings.frameinterval = 200; oc.settings.framerate = 10; oc.settings.timestamplocation = 0; oc.settings.timestampformatter = "FPS: {FPS} {0:G} "; oc.settings.notifyondisconnect = false; oc.settings.youtube = new objectsCameraSettingsYoutube(); oc.settings.youtube.autoupload = false; oc.settings.youtube.category = iSpyServer.Default.YouTubeDefaultCategory; oc.settings.youtube.tags = "iSpy, Motion Detection, Surveillance"; oc.settings.youtube.@public = true; oc.settings.desktopresizeheight = 480; oc.settings.desktopresizewidth = 640; oc.detector.recordondetect = true; oc.detector.keepobjectedges = false; oc.detector.processeveryframe = 1; oc.detector.nomovementinterval = 30; oc.detector.movementinterval = 0; oc.detector.calibrationdelay = 15; oc.detector.color = ColorTranslator.ToHtml(iSpyServer.Default.TrackingColor); oc.detector.type = "None"; oc.detector.postprocessor = "None"; oc.detector.sensitivity = 80; oc.recorder.bufferframes = 30; oc.recorder.inactiverecord = 5; oc.recorder.timelapse = 0; oc.recorder.timelapseframes = 0; oc.recorder.maxrecordtime = 900; var cameraControl = new CameraWindow(oc) {BackColor = iSpyServer.Default.BackColor}; _pnlCameras.Controls.Add(cameraControl); cameraControl.Location = new Point(oc.x, oc.y); cameraControl.Size = new Size(320, 240); cameraControl.AutoSize = true; cameraControl.BringToFront(); SetCameraEvents(cameraControl); var ac = new AddCamera {CameraControl = cameraControl}; ac.ShowDialog(this); if (ac.DialogResult == DialogResult.OK) { ac.CameraControl.Camobject.id = iSpyServer.Default.NextCameraID; Cameras.Add(oc); iSpyServer.Default.NextCameraID++; SetNewStartPosition(); NeedsSync = true; } else { cameraControl.Disable(); _pnlCameras.Controls.Remove(cameraControl); cameraControl.Dispose(); } ac.Dispose(); }
internal void DisplayCamera(objectsCamera cam) { var cameraControl = new CameraWindow(cam); SetCameraEvents(cameraControl); cameraControl.BackColor = iSpyServer.Default.BackColor; _pnlCameras.Controls.Add(cameraControl); cameraControl.Location = new Point(cam.x, cam.y); cameraControl.Size = new Size(cam.width, cam.height); cameraControl.BringToFront(); if (cam.settings.active) cameraControl.Enable(); }
private void SetCameraEvents(CameraWindow cameraControl) { cameraControl.MouseEnter += CameraControl_MouseEnter; cameraControl.MouseDown += CameraControlMouseDown; cameraControl.MouseUp += CameraControl_MouseUp; cameraControl.MouseLeave += CameraControl_MouseLeave; cameraControl.MouseMove += CameraControlMouseMove; cameraControl.DoubleClick += CameraControlDoubleClick; cameraControl.KeyDown += CameraControl_KeyDown; }
private void RemoveCamera(CameraWindow cameraControl) { cameraControl.ShuttingDown = true; cameraControl.Disable(); cameraControl.MouseEnter -= CameraControl_MouseEnter; cameraControl.MouseDown -= CameraControlMouseDown; cameraControl.MouseUp -= CameraControl_MouseUp; cameraControl.MouseLeave -= CameraControl_MouseLeave; cameraControl.MouseMove -= CameraControlMouseMove; cameraControl.DoubleClick -= CameraControlDoubleClick; cameraControl.KeyDown -= CameraControl_KeyDown; _pnlCameras.Controls.Remove(cameraControl); if (!_closing) { CameraWindow control = cameraControl; objectsCamera oc = Cameras.SingleOrDefault(p => p.id == control.Camobject.id); if (oc != null) Cameras.Remove(oc); NeedsSync = true; SetNewStartPosition(); } Application.DoEvents(); cameraControl.Dispose(); }