// Veo View 1.0 // // Copyright 2008 Dmitry Brant // http://dmitrybrant.com // // Based on Perl code from: // //http://www.kahunaburger.com/2004/08/12/veopm-a-perl-module-to-talk-to-the-veo-observer-network-cameras //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- unsigned char jpegHeader[] = { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x11, 0x00, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x02, 0x00, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x84, 0x00, 0x08, 0x05, 0x06, 0x07, 0x06, 0x05, 0x08, 0x07, 0x06, 0x07, 0x09, 0x08, 0x08, 0x09, 0x0D, 0x15, 0x0E, 0x0D, 0x0C, 0x0C, 0x0D, 0x1A, 0x11, 0x12, 0x0E, 0x15, 0x20, 0x1A, 0x20, 0x20, 0x1C, 0x1A, 0x1C, 0x1C, 0x20, 0x25, 0x2F, 0x27, 0x20, 0x22, 0x2F, 0x22, 0x1C, 0x1C, 0x2B, 0x39, 0x2B, 0x2F, 0x33, 0x33, 0x33, 0x39, 0x33, 0x20, 0x27, 0x39, 0x40, 0x39, 0x33, 0x40, 0x2F, 0x33, 0x33, 0x33, 0x01, 0x08, 0x09, 0x09, 0x0D, 0x0B, 0x0D, 0x17, 0x0E, 0x0E, 0x1C, 0x33, 0x22, 0x1C, 0x22, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01, 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF, 0xC4, 0x00, 0xD2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x3F, 0x00 }; unsigned long conv_long_endian(unsigned long big){ unsigned long temp1 = (big & 0x000000FFL) << 24; temp1 |= (big & 0x0000FF00L) << 8; temp1 |= (big & 0x00FF0000L) >> 8; temp1 |= (big & 0xFF000000L) >> 24; return(temp1); } //--------------------------------------------------------------------------- unsigned short conv_endian(unsigned short big){ return(((big & 0xFF00) >> 8) + ((big & 0xFF) << 8)); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner), stopping(false), jpegLength(0), jpegCurrentLength(0), gettingSnapshot(false) { WSADATA WSData; WSAStartup(MAKEWORD(1, 1), &WSData); SetWindowLong(sbGrippy->Handle, GWL_STYLE, GetWindowLong(sbGrippy->Handle, GWL_STYLE) | SBS_SIZEGRIP | SBS_SIZEBOXBOTTOMRIGHTALIGN); bmp = new Graphics::TBitmap(); bmp->PixelFormat = pf24bit; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action){ if(hSock){ shutdown(hSock, SD_BOTH); closesocket(hSock); hSock = NULL; } WSACleanup(); if(bmp){ delete bmp; bmp = NULL; } } //--------------------------------------------------------------------------- void TForm1::SetSendTimeout(){ FD_ZERO(&sockSet); FD_SET(hSock, &sockSet); blockTime.tv_sec = 1; blockTime.tv_usec = 0; //microseconds select(0, NULL, &sockSet, NULL, &blockTime); } //--------------------------------------------------------------------------- void TForm1::SetRecvTimeout(){ FD_ZERO(&sockSet); FD_SET(hSock, &sockSet); blockTime.tv_sec = 1; blockTime.tv_usec = 0; //microseconds select(0, &sockSet, NULL, NULL, &blockTime); } //--------------------------------------------------------------------------- void __fastcall TForm1::cmdConnectClick(TObject *Sender){ hSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(hSock == INVALID_SOCKET){ Application->MessageBox("Failed to create socket.", "Error", MB_ICONEXCLAMATION); hSock = NULL; return; } //set up socket address structure RemoteAddr.sin_family = AF_INET; RemoteAddr.sin_port = htons(atoi(txtPort->Text.c_str())); RemoteAddr.sin_addr.s_addr = inet_addr(txtAddress->Text.c_str()); if(RemoteAddr.sin_addr.s_addr == htonl(INADDR_NONE)){ Application->MessageBox("Invalid IP address.", "Error", MB_ICONEXCLAMATION); closesocket(hSock); hSock = NULL; return; } //make the socket nonblocking unsigned long nonblocking = 1; if(ioctlsocket(hSock, FIONBIO, &nonblocking) == SOCKET_ERROR){ Application->MessageBox("Failed to make socket nonblocking.", "Error", MB_ICONEXCLAMATION); closesocket(hSock); hSock = NULL; return; } //connect to the remote host if(connect(hSock, (PSOCKADDR)&RemoteAddr, sizeof(RemoteAddr)) == SOCKET_ERROR){ if(WSAGetLastError() != WSAEWOULDBLOCK){ Application->MessageBox("Failed to connect to IP address.", "Error", MB_ICONEXCLAMATION); closesocket(hSock); hSock = NULL; return; } } sequence = 0; //try to log on VeoLogonRequest rq; rq.hdr.length = conv_endian(sizeof(VeoLogonRequest)); rq.hdr.msgType = 0; rq.hdr.sequence = sequence++; strcpy(rq.user, txtUser->Text.c_str()); strcpy(rq.password, txtPass->Text.c_str()); SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoLogonRequest), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send logon request.", "Error", MB_ICONEXCLAMATION); closesocket(hSock); hSock = NULL; return; } VeoLogonResponse rs; SetRecvTimeout(); int bytesReceived = recv(hSock, (char*)&rs, sizeof(VeoLogonResponse), NULL); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ Application->MessageBox("Failed to receive logon response.", "Error", MB_ICONEXCLAMATION); closesocket(hSock); hSock = NULL; return; } if(rs.hdr.msgType != 4){ Application->MessageBox(("Logon failed: Error " + String(rs.hdr.msgType)).c_str(), "Error", MB_ICONEXCLAMATION); closesocket(hSock); hSock = NULL; return; } rs.streamID = conv_long_endian(rs.streamID); streamID = rs.streamID; rs.timePerFrame = conv_long_endian(rs.timePerFrame); rs.maxBrightness = conv_long_endian(rs.maxBrightness); String str; str += "Protocol version: " + String(rs.protoMajor) + "." + String(rs.protoMinor) + "\n"; str += "Access level: " + String(rs.accessLevel) + "\n"; str += "Stream ID: " + String(rs.streamID) + "\n"; str += "Format: " + String(rs.format) + "\n"; str += "Time per frame: " + String(rs.timePerFrame) + "\n"; str += "Max brightness: " + String(rs.maxBrightness) + "\n"; tbBrightness->Max = rs.maxBrightness; str += "Count: " + String(rs.streamCount) + "\n"; for(int i=0; iMessageBox(str.c_str(), "Info", MB_ICONINFORMATION); btnStreamStart->Enabled = true; btnStreamStop->Enabled = false; cmdConnect->Enabled = false; cmdDisconnect->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::cmdDisconnectClick(TObject *Sender){ shutdown(hSock, SD_BOTH); closesocket(hSock); hSock = NULL; cmdConnect->Enabled = true; cmdDisconnect->Enabled = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::btnMoveUpClick(TObject *Sender){ if(!hSock) return; VeoMoveRequest rq; rq.hdr.length = conv_endian(sizeof(VeoMoveRequest)); rq.hdr.msgType = 0x15; rq.hdr.sequence = sequence++; rq.moveDirection = ((TButton*)(Sender))->Tag; SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoMoveRequest), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } if(Timer1->Enabled) return; VeoMsgHeader rs; SetRecvTimeout(); int bytesReceived = recv(hSock, (char*)rsbuf, 255, NULL); memcpy(&rs, rsbuf, sizeof(VeoMsgHeader)); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ Application->MessageBox("Failed to receive response.", "Error", MB_ICONEXCLAMATION); return; } if(rs.msgType != 4){ Application->MessageBox(("Command failed: Error " + String(rs.msgType)).c_str(), "Error", MB_ICONEXCLAMATION); return; } } //--------------------------------------------------------------------------- void __fastcall TForm1::btnStatusOnClick(TObject *Sender){ if(!hSock) return; VeoStatusLightRequest rq; rq.hdr.length = conv_endian(sizeof(VeoStatusLightRequest)); rq.hdr.msgType = 0x18; rq.hdr.sequence = sequence++; rq.onOff = ((TButton*)(Sender))->Tag; SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoStatusLightRequest), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } // no response expected from this } //--------------------------------------------------------------------------- void __fastcall TForm1::tbBrightnessChange(TObject *Sender){ if(!hSock) return; VeoSetBrightnessRequest rq; rq.hdr.length = conv_endian(sizeof(VeoSetBrightnessRequest)); rq.hdr.msgType = 0x06; rq.hdr.sequence = sequence++; rq.brightness = conv_long_endian(tbBrightness->Position); SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoSetBrightnessRequest), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } if(Timer1->Enabled) return; VeoMsgHeader rs; SetRecvTimeout(); int bytesReceived = recv(hSock, (char*)rsbuf, 255, NULL); memcpy(&rs, rsbuf, sizeof(VeoMsgHeader)); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ Application->MessageBox("Failed to receive response.", "Error", MB_ICONEXCLAMATION); return; } if(rs.msgType != 4){ Application->MessageBox(("Command failed: Error " + String(rs.msgType)).c_str(), "Error", MB_ICONEXCLAMATION); return; } } //--------------------------------------------------------------------------- void __fastcall TForm1::btnStreamSelect1Click(TObject *Sender){ if(!hSock) return; VeoStreamSelectRequest rq; rq.hdr.length = conv_endian(sizeof(VeoStreamSelectRequest)); rq.hdr.msgType = 0x14; rq.hdr.sequence = sequence++; rq.streamType = ((TButton*)(Sender))->Tag; rq.delayTime = conv_long_endian(100000); SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoStreamSelectRequest), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } //not expecting a response } //--------------------------------------------------------------------------- void __fastcall TForm1::btnStreamStartClick(TObject *Sender){ if(!hSock) return; if(Timer1->Enabled) return; VeoStartStreamRequest rq; rq.hdr.length = conv_endian(sizeof(VeoStartStreamRequest)); rq.hdr.msgType = 0x1; rq.hdr.sequence = sequence++; rq.streamID = conv_long_endian(streamID); rq.reserved = conv_endian(0); rq.audio = 0; SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoStartStreamRequest), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } VeoStartStreamResponse rs; SetRecvTimeout(); int bytesReceived = recv(hSock, (char*)rsbuf, 255, NULL); memcpy(&rs, rsbuf, sizeof(VeoStartStreamResponse)); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ Application->MessageBox("Failed to receive response.", "Error", MB_ICONEXCLAMATION); return; } if(rs.hdr.msgType != 4){ Application->MessageBox(("Command failed: Error " + String(rs.hdr.msgType)).c_str(), "Error", MB_ICONEXCLAMATION); return; } Timer1->Enabled = true; btnStreamStart->Enabled = false; btnStreamStop->Enabled = true; jpegLength = 0; stopping = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::btnStreamStopClick(TObject *Sender){ if(!hSock) return; VeoMsgHeader rq; rq.length = conv_endian(sizeof(VeoMsgHeader)); rq.msgType = 0x2; rq.sequence = sequence++; SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoMsgHeader), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } stopping = true; if(Timer1->Enabled) return; //may or may not receive a response... don't care SetRecvTimeout(); recv(hSock, (char*)rsbuf, 255, NULL); } //--------------------------------------------------------------------------- void __fastcall TForm1::btnResetClick(TObject *Sender){ if(!hSock) return; VeoMsgHeader rq; rq.length = conv_endian(sizeof(VeoMsgHeader)); rq.msgType = 0x16; rq.sequence = sequence++; SetSendTimeout(); int bytesSent = send(hSock, (const char*)&rq, sizeof(VeoMsgHeader), NULL); if(bytesSent == SOCKET_ERROR){ Application->MessageBox("Failed to send request.", "Error", MB_ICONEXCLAMATION); return; } if(Timer1->Enabled) return; VeoMsgHeader rs; SetRecvTimeout(); int bytesReceived = recv(hSock, (char*)rsbuf, 255, NULL); memcpy(&rs, rsbuf, sizeof(VeoMsgHeader)); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ Application->MessageBox("Failed to receive response.", "Error", MB_ICONEXCLAMATION); return; } if(rs.msgType != 4){ Application->MessageBox(("Command failed: Error " + String(rs.msgType)).c_str(), "Error", MB_ICONEXCLAMATION); return; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender){ //continually read in packets... Timer1->Enabled = false; try{ if(stopping){ Screen->Cursor = crHourGlass; //drain packets while(true){ SetRecvTimeout(); int bytesReceived = recv(hSock, (char*)rsbuf, 4096, NULL); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ btnStreamStart->Enabled = true; btnStreamStop->Enabled = false; Screen->Cursor = crDefault; return; } } } SetRecvTimeout(); //receive just the size of data first int bytesReceived = recv(hSock, (char*)rsbuf, 2, NULL); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ return; } VeoReceivedPacket* rs = (VeoReceivedPacket*)rsbuf; rs->size = conv_endian(rs->size); if(rs->size < 3) rs->size = 3; SetRecvTimeout(); //receive the rest of the data bytesReceived = recv(hSock, (char*)&rsbuf[2], rs->size-2, NULL); if((bytesReceived == SOCKET_ERROR) || (!bytesReceived)){ return; } if(rs->type == 0){ VeoReceivedPacket0* p = (VeoReceivedPacket0*)rsbuf; p->frameNo = conv_endian(p->frameNo); p->seqNo = conv_endian(p->seqNo); p->height = conv_endian(p->height); p->width = conv_endian(p->width); //begin building the JPEG buffer by tacking on the header, //and patching the width and height memcpy(jpegBuf, jpegHeader, sizeof(jpegHeader)); jpegLength = sizeof(jpegHeader); *((unsigned short*)(&jpegBuf[0xA0])) = conv_endian(p->height); *((unsigned short*)(&jpegBuf[0xA2])) = conv_endian(p->width); //and copy the rest of the packet data memcpy(&jpegBuf[jpegLength], p->data, p->size-26); jpegLength += (p->size-26); }else if(rs->type == 1){ VeoReceivedPacket1* p = (VeoReceivedPacket1*)rsbuf; p->frameNo = conv_endian(p->frameNo); p->seqNo = conv_endian(p->seqNo); memcpy(&jpegBuf[jpegLength], p->data, p->size-8); jpegLength += (p->size-8); }else if(rs->type == 2){ VeoReceivedPacket1* p = (VeoReceivedPacket1*)rsbuf; p->frameNo = conv_endian(p->frameNo); p->seqNo = conv_endian(p->seqNo); memcpy(&jpegBuf[jpegLength], p->data, p->size-8); jpegLength += (p->size-8); //frame complete! if(gettingSnapshot) return; memcpy(jpegCurrent, jpegBuf, jpegLength); jpegCurrentLength = jpegLength; TMemoryStream* stream = new TMemoryStream; TJPEGImage* theImage = new TJPEGImage; try{ stream->Write(jpegCurrent, jpegCurrentLength); stream->Seek(0, soFromBeginning); try{ theImage->LoadFromStream(stream); bmp->Assign(theImage); BitBlt(PaintBox1->Canvas->Handle, 0, 0, bmp->Width, bmp->Height, bmp->Canvas->Handle, 0, 0, SRCCOPY); }catch(...){ } }__finally{ delete stream; delete theImage; } }else if(rs->type == 3){ //VeoReceivedPacket1* p = (VeoReceivedPacket1*)rsbuf; //audio? }else{ //it's a response to a pending request during streaming return; } }__finally{ if(!stopping) Timer1->Enabled = true; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender){ TSaveDialog* SaveDialog1 = new TSaveDialog(this); gettingSnapshot = true; try{ SaveDialog1->Title = "Save Image"; SaveDialog1->Filter = "JPEG Files (*.jpg)|*.jpg|All Files (*.*)|*.*"; SaveDialog1->FilterIndex = 1; SaveDialog1->DefaultExt = ".jpg"; SaveDialog1->FileName = "Snapshot.jpg"; SaveDialog1->Options << ofEnableSizing << ofOverwritePrompt; if(!SaveDialog1->Execute()) return; int f = FileCreate(SaveDialog1->FileName); if(f == -1){ Application->MessageBox("Failed to create the file for saving.", "Error", MB_ICONEXCLAMATION); return; } FileWrite(f, jpegCurrent, jpegCurrentLength); FileClose(f); }__finally{ gettingSnapshot = false; delete SaveDialog1; } } //---------------------------------------------------------------------------