Initial
This commit is contained in:
25
CapServer/hold/Main.cpp
Normal file
25
CapServer/hold/Main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <common/windows.hpp>
|
||||
#include <common/string.hpp>
|
||||
#include <CapServer/MainApplication.hpp>
|
||||
|
||||
int handlerRoutine(DWORD ctrlType);
|
||||
|
||||
bool running=true;
|
||||
|
||||
MainApplication mainApplication;
|
||||
|
||||
void main(int argc,char **argv)
|
||||
{
|
||||
mainApplication.run(argc,argv);
|
||||
}
|
||||
|
||||
int handlerRoutine(DWORD eventType) // this is connected ad-hoc to generic server, could be cleaner
|
||||
{
|
||||
if(CTRL_C_EVENT==eventType)
|
||||
{
|
||||
::printf("Caught Ctrl-C...\n");
|
||||
running=false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
124
CapServer/hold/MainApplication.cpp
Normal file
124
CapServer/hold/MainApplication.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#include <CapServer/MainApplication.hpp>
|
||||
|
||||
MainApplication::MainApplication()
|
||||
{
|
||||
}
|
||||
|
||||
MainApplication::~MainApplication()
|
||||
{
|
||||
}
|
||||
|
||||
int MainApplication::run(int argc,char **argv)
|
||||
{
|
||||
String command;
|
||||
|
||||
mParams.setArgs(argc,argv);
|
||||
switch(mParams.getRequest())
|
||||
{
|
||||
case Params::Client :
|
||||
mParams.showParams();
|
||||
handleClient(mParams.getServer(),mParams.getPort().toInt());
|
||||
break;
|
||||
case Params::Server :
|
||||
mParams.showParams();
|
||||
handleServer(mParams.getHost(),mParams.getPort().toInt(),mParams.getDevice().toInt(),mParams.getRetain(),mParams.getDebug(),mParams.getShowSrcDlg());
|
||||
break;
|
||||
case Params::Settings :
|
||||
mParams.showParams();
|
||||
handleSettings(mParams.getShowSrcDlg());
|
||||
break;
|
||||
case Params::None :
|
||||
displayUsage();
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MainApplication::handleServer(const String &host,int port,int deviceIndex,bool retain,bool debug,bool showSrcDlg)
|
||||
{
|
||||
message("[MainApplication::handleServer]starting server");
|
||||
SocketServer socketServer(deviceIndex,retain,debug,showSrcDlg);
|
||||
socketServer.listen(host,port);
|
||||
}
|
||||
|
||||
void MainApplication::handleClient(const String &host,int port)
|
||||
{
|
||||
INETSocketAddress internetSocketAddress;
|
||||
InternetAddress internetAddress(127,0,0,1);
|
||||
Socket socket;
|
||||
SocketHeader socketHeader;
|
||||
Array<BYTE> content;
|
||||
|
||||
message("[MainApplication::handleClient]starting client");
|
||||
internetSocketAddress.family(PF_INET);
|
||||
internetSocketAddress.port(1024);
|
||||
internetSocketAddress.internetAddress(internetAddress);
|
||||
if(!socket.create())
|
||||
{
|
||||
message("[MainApplication::handleClient]create failed...");
|
||||
return;
|
||||
}
|
||||
if(!socket.connect(internetSocketAddress))
|
||||
{
|
||||
message("[MainApplication::handleClient]connect failed...");
|
||||
return;
|
||||
}
|
||||
message("[MainApplication::handleClient]reading socket header...");
|
||||
socketHeader.unmarshall(socket);
|
||||
message("[MainApplication::handleClient]header length",socketHeader.getSizeHeader());
|
||||
message("[MainApplication::handleClient]version",socketHeader.getVersion());
|
||||
message("[MainApplication::handleClient]content length",socketHeader.getContentLength());
|
||||
message("[MainApplication::handleClient]content type",(int)socketHeader.getContentType());
|
||||
content.size(socketHeader.getContentLength());
|
||||
message("[MainApplication::handleClient]reading content...");
|
||||
socket.receive((char*)&content[0],content.size());
|
||||
message("[MainApplication::handleClient]done...");
|
||||
socket.destroy();
|
||||
}
|
||||
|
||||
void MainApplication::handleSettings(bool showSrcDlg)
|
||||
{
|
||||
VidReg vidReg;
|
||||
Block<DriverInfo> driverInfoBlock;
|
||||
|
||||
VidCap::getDrivers(driverInfoBlock);
|
||||
message("[MainApplication::handleSettings]Capture File",vidReg.getCaptureFile());
|
||||
message("[MainApplication::handleSettings]Sequencing",vidReg.getSequencing()?"true":"false");
|
||||
message("[MainApplication::handleSettings]Preview Rate",vidReg.getPreviewRate());
|
||||
message("[MainApplication::handleSettings]Preview Width",vidReg.getPreviewWidth());
|
||||
message("[MainApplication::handleSettings]Preview Height",vidReg.getPreviewHeight());
|
||||
message("[MainApplication::handleSettings]Capture Width",vidReg.getCaptureWidth());
|
||||
message("[MainApplication::handleSettings]Capture Height",vidReg.getCaptureHeight());
|
||||
message("[MainApplication::handleSettings]Capture Quality",vidReg.getQuality());
|
||||
message("[MainApplication::handleSettings]***********CAPTURE DEVICE LIST*********");
|
||||
if(!driverInfoBlock.size())message("[MainApplication::handleSettings]No capture devices on system.");
|
||||
for(int index=0;index<driverInfoBlock.size();index++)
|
||||
{
|
||||
WDMCap wdmCap(index);
|
||||
if(wdmCap.isConnected())
|
||||
{
|
||||
message(String("[MainApplication::handleSettings]")+driverInfoBlock[index].driverName()+String(" ***Available"));
|
||||
if(showSrcDlg)wdmCap.dialogVideoSource();
|
||||
}
|
||||
else message(String("[MainApplication::handleSettings]")+driverInfoBlock[index].driverName()+String(" ***Unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
void MainApplication::displayUsage(void)
|
||||
{
|
||||
message("CapServer [run host port device retain debug showsrcdlg]");
|
||||
message(" run={server} | {client} | {settings}");
|
||||
message(" server=<servername> | <serveraddress>");
|
||||
message(" host=<hostname> | <hostaddress>");
|
||||
message(" port=<port>");
|
||||
message(" device=<index>");
|
||||
message(" retain={true} | {false}");
|
||||
message(" debug={true} | {false}");
|
||||
message(" showsrcdlg={true} | {false}");
|
||||
message(" ");
|
||||
message(" CapServer run=server host=127.0.0.1 port=1024");
|
||||
message(" CapServer run=server host=127.0.0.1 port=1024 device=0");
|
||||
message(" CapServer run=client server=127.0.0.1 port=1024");
|
||||
message(" CapServer run=settings");
|
||||
}
|
||||
|
||||
55
CapServer/hold/MainApplication.hpp
Normal file
55
CapServer/hold/MainApplication.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef _CAPSERVER_MAINAPPLICATION_HPP_
|
||||
#define _CAPSERVER_MAINAPPLICATION_HPP_
|
||||
#ifndef _CAPSERVER_SOCKET_HEADER_HPP_
|
||||
#include <capserver/socketheader.hpp>
|
||||
#endif
|
||||
#ifndef _SOCKET_WSADATA_HPP_
|
||||
#include <socket/wsadata.hpp>
|
||||
#endif
|
||||
#ifndef _CAPSERVER_SOCKETSERVER_HPP_
|
||||
#include <CapServer/SocketServer.hpp>
|
||||
#endif
|
||||
#ifndef _CAPSERVER_PARAMS_HPP_
|
||||
#include <CapServer/Params.hpp>
|
||||
#endif
|
||||
#ifndef _VIDCAP_VIDREG_HPP_
|
||||
#include <vidcap/vidreg.hpp>
|
||||
#endif
|
||||
|
||||
class MainApplication
|
||||
{
|
||||
public:
|
||||
MainApplication();
|
||||
virtual ~MainApplication();
|
||||
int run(int argc,char **argv);
|
||||
private:
|
||||
void message(const String &message);
|
||||
void message(const String &message,int arg);
|
||||
void message(const String &message,const String &arg);
|
||||
void handleServer(const String &host,int port,int deviceIndex,bool retain,bool debug,bool showSrcDlg);
|
||||
void handleClient(const String &server,int port);
|
||||
void handleSettings(bool showSrcDlg);
|
||||
void displayUsage(void);
|
||||
|
||||
WSASystem mWSASystem;
|
||||
Params mParams;
|
||||
};
|
||||
|
||||
inline
|
||||
void MainApplication::message(const String &message)
|
||||
{
|
||||
printf("%s\n",message.str());
|
||||
}
|
||||
|
||||
inline
|
||||
void MainApplication::message(const String &message,int arg)
|
||||
{
|
||||
printf("%s=%d\n",message.str(),arg);
|
||||
}
|
||||
|
||||
inline
|
||||
void MainApplication::message(const String &message,const String &arg)
|
||||
{
|
||||
printf("%s=%s\n",message.str(),arg.str());
|
||||
}
|
||||
#endif
|
||||
65
CapServer/hold/NameValuePair.hpp
Normal file
65
CapServer/hold/NameValuePair.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef _CAPSERVER_NAMEVALUE_HPP_
|
||||
#define _CAPSERVER_NAMEVALUE_HPP_
|
||||
#ifndef _COMMON_STRING_HPP_
|
||||
#include <common/string.hpp>
|
||||
#endif
|
||||
|
||||
class NameValuePair
|
||||
{
|
||||
public:
|
||||
NameValuePair();
|
||||
NameValuePair(const String &name,const String &value);
|
||||
const String &getName(void)const;
|
||||
void setName(const String &name);
|
||||
const String &getValue(void)const;
|
||||
void setValue(const String &value);
|
||||
bool fromString(const String &string);
|
||||
private:
|
||||
String mName;
|
||||
String mValue;
|
||||
};
|
||||
|
||||
inline
|
||||
NameValuePair::NameValuePair()
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
NameValuePair::NameValuePair(const String &name,const String &value)
|
||||
: mName(name), mValue(value)
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
const String &NameValuePair::getName(void)const
|
||||
{
|
||||
return mName;
|
||||
}
|
||||
|
||||
inline
|
||||
void NameValuePair::setName(const String &name)
|
||||
{
|
||||
mName=name;
|
||||
}
|
||||
|
||||
inline
|
||||
const String &NameValuePair::getValue(void)const
|
||||
{
|
||||
return mValue;
|
||||
}
|
||||
|
||||
inline
|
||||
void NameValuePair::setValue(const String &value)
|
||||
{
|
||||
mValue=value;
|
||||
}
|
||||
|
||||
inline
|
||||
bool NameValuePair::fromString(const String &string)
|
||||
{
|
||||
if(string.isNull())return false;
|
||||
mName=string.betweenString(0,'=');
|
||||
mValue=string.betweenString('=',0);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
84
CapServer/hold/Params.cpp
Normal file
84
CapServer/hold/Params.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <common/stdio.hpp>
|
||||
#include <CapServer/Params.hpp>
|
||||
|
||||
void Params::setArgs(int argc,char **argv)
|
||||
{
|
||||
String strArg;
|
||||
getParams(argc,argv);
|
||||
if(getServer().isNull())setServer("127.0.0.1");
|
||||
if(getHost().isNull())setHost("127.0.0.1");
|
||||
if(getPort().isNull())setPort("1024");
|
||||
if(getDevice().isNull())setDevice("0");
|
||||
strArg=getValue(mRetainKey);
|
||||
strArg.lower();
|
||||
if(!(strArg==String("true")) && !(strArg==String("false")))setRetain(false);
|
||||
else if(strArg==String("true"))setRetain(true);
|
||||
else setRetain(false);
|
||||
strArg=getValue(mDebugKey);
|
||||
strArg.lower();
|
||||
if(!(strArg==String("true")) && !(strArg==String("false")))setDebug(false);
|
||||
else if(strArg==String("true"))setDebug(true);
|
||||
else setDebug(false);
|
||||
strArg=getValue(mShowSrcDlgKey);
|
||||
strArg.lower();
|
||||
if(!(strArg==String("true")) && !(strArg==String("false")))setShowSrcDlg(false);
|
||||
else if(strArg==String("true"))setShowSrcDlg(true);
|
||||
else setShowSrcDlg(false);
|
||||
}
|
||||
|
||||
int Params::getParams(int argc,char**argv)
|
||||
{
|
||||
mNameValuePairs.remove();
|
||||
for(int index=0;index<argc;index++)
|
||||
{
|
||||
NameValuePair nameValuePair;
|
||||
nameValuePair.fromString(argv[index]);
|
||||
mNameValuePairs.insert(&nameValuePair);
|
||||
}
|
||||
return mNameValuePairs.size();
|
||||
}
|
||||
|
||||
String Params::getValue(const String &key)const
|
||||
{
|
||||
for(int index=0;index<mNameValuePairs.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=((Params&)*this).mNameValuePairs[index];
|
||||
if(nameValuePair.getName()==key)return nameValuePair.getValue();
|
||||
}
|
||||
return String();
|
||||
}
|
||||
|
||||
void Params::setValue(const String &key,const String &value)
|
||||
{
|
||||
for(int index=0;index<mNameValuePairs.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=mNameValuePairs[index];
|
||||
if(nameValuePair.getName()==key)
|
||||
{
|
||||
nameValuePair.setValue(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
mNameValuePairs.insert(&NameValuePair(key,value));
|
||||
}
|
||||
|
||||
Params::Request Params::getRequest(void)const
|
||||
{
|
||||
String strRequest(getRun());
|
||||
if(strRequest=="client")return Client;
|
||||
else if(strRequest=="server")return Server;
|
||||
else if(strRequest=="settings")return Settings;
|
||||
return None;
|
||||
}
|
||||
|
||||
void Params::showParams(void)
|
||||
{
|
||||
for(int index=0;index<mNameValuePairs.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=mNameValuePairs[index];
|
||||
::printf("[Params::showParams]arg[%d]%s=%s\n",
|
||||
index,
|
||||
nameValuePair.getName().str(),
|
||||
nameValuePair.getValue().str());
|
||||
}
|
||||
}
|
||||
169
CapServer/hold/Params.hpp
Normal file
169
CapServer/hold/Params.hpp
Normal file
@@ -0,0 +1,169 @@
|
||||
#ifndef _CAPSERVER_PARAMS_HPP_
|
||||
#define _CAPSERVER_PARAMS_HPP_
|
||||
#ifndef _COMMON_BLOCK_HPP_
|
||||
#include <common/block.hpp>
|
||||
#endif
|
||||
#ifndef _CAPSERVER_NAMEVALUE_HPP_
|
||||
#include <CapServer/NameValuePair.hpp>
|
||||
#endif
|
||||
|
||||
class Params
|
||||
{
|
||||
public:
|
||||
typedef enum Request{Server,Client,Settings,None};
|
||||
Params(int argc,char **argv);
|
||||
Params();
|
||||
virtual ~Params();
|
||||
Request getRequest(void)const;
|
||||
String getServer(void)const;
|
||||
String getHost(void)const;
|
||||
String getPort(void)const;
|
||||
String getDevice(void)const;
|
||||
bool getRetain(void)const;
|
||||
bool getDebug(void)const;
|
||||
bool getShowSrcDlg(void)const;
|
||||
void setArgs(int argc,char **argv);
|
||||
void showParams(void);
|
||||
private:
|
||||
int getParams(int argc,char **argv);
|
||||
String getValue(const String &key)const;
|
||||
void setValue(const String &key,const String &value);
|
||||
String getRun(void)const;
|
||||
void setServer(const String &server);
|
||||
void setHost(const String &host);
|
||||
void setPort(const String &port);
|
||||
void setDevice(const String &device);
|
||||
void setRetain(bool retain);
|
||||
void setDebug(bool debug);
|
||||
void setShowSrcDlg(bool showSrcDlg);
|
||||
|
||||
Block<NameValuePair> mNameValuePairs;
|
||||
String mRunKey;
|
||||
String mServerKey;
|
||||
String mHostKey;
|
||||
String mPortKey;
|
||||
String mDeviceKey;
|
||||
String mRetainKey;
|
||||
String mDebugKey;
|
||||
String mShowSrcDlgKey;
|
||||
};
|
||||
|
||||
inline
|
||||
Params::Params(int argc,char **argv)
|
||||
: mRunKey("run"), mServerKey("server"), mHostKey("host"), mPortKey("port"), mDeviceKey("device"),
|
||||
mRetainKey("retain"), mDebugKey("debug"), mShowSrcDlgKey("showsrcdlg")
|
||||
{
|
||||
getParams(argc,argv);
|
||||
}
|
||||
|
||||
inline
|
||||
Params::Params()
|
||||
: mRunKey("run"), mServerKey("server"), mHostKey("host"), mPortKey("port"), mDeviceKey("device"),
|
||||
mRetainKey("retain"), mDebugKey("debug"), mShowSrcDlgKey("showsrcdlg")
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
Params::~Params()
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
String Params::getRun(void)const
|
||||
{
|
||||
return getValue(mRunKey);
|
||||
}
|
||||
|
||||
inline
|
||||
String Params::getServer(void)const
|
||||
{
|
||||
return getValue(mServerKey);
|
||||
}
|
||||
|
||||
inline
|
||||
String Params::getHost(void)const
|
||||
{
|
||||
return getValue(mHostKey);
|
||||
}
|
||||
|
||||
inline
|
||||
String Params::getPort(void)const
|
||||
{
|
||||
return getValue(mPortKey);
|
||||
}
|
||||
|
||||
inline
|
||||
String Params::getDevice(void)const
|
||||
{
|
||||
return getValue(mDeviceKey);
|
||||
}
|
||||
|
||||
inline
|
||||
bool Params::getRetain(void)const
|
||||
{
|
||||
String strRetain(getValue(mRetainKey));
|
||||
if(strRetain=="true")return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline
|
||||
bool Params::getDebug(void)const
|
||||
{
|
||||
String strDebug(getValue(mDebugKey));
|
||||
if(strDebug=="true")return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline
|
||||
bool Params::getShowSrcDlg(void)const
|
||||
{
|
||||
String strValue(getValue(mShowSrcDlgKey));
|
||||
if(strValue=="true")return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setServer(const String &server)
|
||||
{
|
||||
setValue(mServerKey,server);
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setHost(const String &host)
|
||||
{
|
||||
setValue(mHostKey,host);
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setPort(const String &port)
|
||||
{
|
||||
setValue(mPortKey,port);
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setDevice(const String &device)
|
||||
{
|
||||
setValue(mDeviceKey,device);
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setRetain(bool retain)
|
||||
{
|
||||
if(retain)setValue(mRetainKey,"true");
|
||||
else setValue(mRetainKey,"false");
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setDebug(bool debug)
|
||||
{
|
||||
if(debug)setValue(mDebugKey,"true");
|
||||
else setValue(mDebugKey,"false");
|
||||
}
|
||||
|
||||
inline
|
||||
void Params::setShowSrcDlg(bool showSrcDlg)
|
||||
{
|
||||
if(showSrcDlg)setValue(mShowSrcDlgKey,"true");
|
||||
else setValue(mShowSrcDlgKey,"false");
|
||||
}
|
||||
#endif
|
||||
105
CapServer/hold/SocketHeader.hpp
Normal file
105
CapServer/hold/SocketHeader.hpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#ifndef _CAPSERVER_SOCKET_HEADER_HPP_
|
||||
#define _CAPSERVER_SOCKET_HEADER_HPP_
|
||||
#ifndef _COMMON_WINDOWS_HPP_
|
||||
#include <common/windows.hpp>
|
||||
#endif
|
||||
#ifndef _SOCKET_SOCKET_HPP_
|
||||
#include <socket/socket.hpp>
|
||||
#endif
|
||||
|
||||
class SocketHeader
|
||||
{
|
||||
public:
|
||||
typedef enum ContentType{Unknown,ImageJPG};
|
||||
SocketHeader();
|
||||
virtual ~SocketHeader();
|
||||
int getSizeHeader(void)const;
|
||||
int getVersion(void)const;
|
||||
void setVersion(int version);
|
||||
int getContentLength(void)const;
|
||||
void setContentLength(int contentLength);
|
||||
ContentType getContentType(void)const;
|
||||
void setContentType(int contentType);
|
||||
bool marshall(Socket &socket);
|
||||
bool unmarshall(Socket &socket);
|
||||
private:
|
||||
int mSizeHeader;
|
||||
int mVersion;
|
||||
int mContentLength;
|
||||
int mContentType;
|
||||
};
|
||||
|
||||
inline
|
||||
SocketHeader::SocketHeader()
|
||||
: mSizeHeader(sizeof(int)+sizeof(int)+sizeof(int)+sizeof(int)),
|
||||
mVersion(0), mContentLength(0), mContentType(Unknown)
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
SocketHeader::~SocketHeader()
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
int SocketHeader::getSizeHeader(void)const
|
||||
{
|
||||
return mSizeHeader;
|
||||
}
|
||||
|
||||
inline
|
||||
int SocketHeader::getVersion(void)const
|
||||
{
|
||||
return mVersion;
|
||||
}
|
||||
|
||||
inline
|
||||
void SocketHeader::setVersion(int version)
|
||||
{
|
||||
mVersion=version;
|
||||
}
|
||||
|
||||
inline
|
||||
int SocketHeader::getContentLength(void)const
|
||||
{
|
||||
return mContentLength;
|
||||
}
|
||||
|
||||
inline
|
||||
void SocketHeader::setContentLength(int contentLength)
|
||||
{
|
||||
mContentLength=contentLength;
|
||||
}
|
||||
|
||||
inline
|
||||
SocketHeader::ContentType SocketHeader::getContentType(void)const
|
||||
{
|
||||
return (ContentType)mContentType;
|
||||
}
|
||||
|
||||
inline
|
||||
void SocketHeader::setContentType(int contentType)
|
||||
{
|
||||
mContentType=(ContentType)contentType;
|
||||
}
|
||||
|
||||
inline
|
||||
bool SocketHeader::marshall(Socket &socket)
|
||||
{
|
||||
if(!socket.send((char*)&mSizeHeader,sizeof(int)))return false;
|
||||
if(!socket.send((char*)&mVersion,sizeof(int)))return false;
|
||||
if(!socket.send((char*)&mContentLength,sizeof(int)))return false;
|
||||
if(!socket.send((char*)&mContentType,sizeof(int)))return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline
|
||||
bool SocketHeader::unmarshall(Socket &socket)
|
||||
{
|
||||
if(!socket.receive((char*)&mSizeHeader,sizeof(int)))return false;
|
||||
if(!socket.receive((char*)&mVersion,sizeof(int)))return false;
|
||||
if(!socket.receive((char*)&mContentLength,sizeof(int)))return false;
|
||||
if(!socket.receive((char*)&mContentType,sizeof(int)))return false;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
103
CapServer/hold/SocketServer.cpp
Normal file
103
CapServer/hold/SocketServer.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <common/OpenFile.hpp>
|
||||
#include <common/systime.hpp>
|
||||
#include <common/diskinfo.hpp>
|
||||
#include <CapServer/SocketServer.hpp>
|
||||
#include <CapServer/SocketHeader.hpp>
|
||||
|
||||
SocketServer::SocketServer(int deviceIndex,bool retain,bool debug,bool showSrcDlg)
|
||||
: mWDMCap(deviceIndex), mRetainImage(retain), mDebug(debug)
|
||||
{
|
||||
if(showSrcDlg)mWDMCap.dialogVideoSource();
|
||||
}
|
||||
|
||||
SocketServer::~SocketServer()
|
||||
{
|
||||
}
|
||||
|
||||
void SocketServer::acceptHandler(SmartPointer<Socket> &socketControl)
|
||||
{
|
||||
if(mDebug)sendDebugFrame(*socketControl);
|
||||
else sendFrame(*socketControl);
|
||||
|
||||
}
|
||||
|
||||
void SocketServer::message(const String &string)
|
||||
{
|
||||
::printf("%s\n",string.str());
|
||||
}
|
||||
|
||||
void SocketServer::sendFrame(Socket &socketControl)
|
||||
{
|
||||
DiskInfo diskInfo;
|
||||
SystemTime systemTime;
|
||||
String strPathCaptureFile;
|
||||
SocketHeader socketHeader;
|
||||
Array<BYTE> charBytes;
|
||||
FileHandle openFile;
|
||||
DWORD elapsedTime;
|
||||
|
||||
socketHeader.setVersion(1);
|
||||
socketHeader.setContentType(SocketHeader::ImageJPG);
|
||||
socketHeader.setContentLength(0);
|
||||
strPathCaptureFile.reserve(1024);
|
||||
::sprintf(strPathCaptureFile,"%04d%02d%02d%02d%02d%02d%02d.jpg",
|
||||
systemTime.year(),systemTime.month(),systemTime.day(),
|
||||
systemTime.hour(),systemTime.minute(),systemTime.second(),
|
||||
systemTime.milliseconds());
|
||||
elapsedTime=::GetTickCount();
|
||||
mWDMCap.setCaptureFileName(strPathCaptureFile);
|
||||
mWDMCap.grabFrameNoStop();
|
||||
elapsedTime=::GetTickCount()-elapsedTime;
|
||||
message(String("[SocketServer::sendFrame] Grab frame took ")+String().fromInt(elapsedTime)+String(" (ms)"));
|
||||
if(!openFile.open(strPathCaptureFile,FileHandle::Read,FileHandle::ShareRead))
|
||||
{
|
||||
socketHeader.marshall(socketControl);
|
||||
}
|
||||
else
|
||||
{
|
||||
charBytes.size(openFile.size());
|
||||
socketHeader.setContentLength(charBytes.size());
|
||||
elapsedTime=::GetTickCount();
|
||||
openFile.read((BYTE*)&charBytes[0],charBytes.size());
|
||||
elapsedTime=::GetTickCount()-elapsedTime;
|
||||
message(String("[SocketServer::sendFrame] Read ")+String().fromInt(charBytes.size())+String(" bytes took ")+String().fromInt(elapsedTime)+String(" (ms)"));
|
||||
elapsedTime=::GetTickCount();
|
||||
socketHeader.marshall(socketControl);
|
||||
socketControl.send((char*)&charBytes[0],charBytes.size());
|
||||
elapsedTime=::GetTickCount()-elapsedTime;
|
||||
message(String("[SocketServer::sendFrame] Send ")+String().fromInt(charBytes.size())+String(" bytes took ")+String().fromInt(elapsedTime)+String(" (ms)"));
|
||||
openFile.close();
|
||||
if(!mRetainImage)diskInfo.unlink(strPathCaptureFile);
|
||||
}
|
||||
message(String("[SocketServer::sendFrame]Sent ")+String().fromInt(charBytes.size())+String(" bytes"));
|
||||
return;
|
||||
}
|
||||
|
||||
void SocketServer::sendDebugFrame(Socket &socketControl)
|
||||
{
|
||||
DiskInfo diskInfo;
|
||||
Array<BYTE> charBytes;
|
||||
FileHandle openFile;
|
||||
SocketHeader socketHeader;
|
||||
String strPathFileName;
|
||||
|
||||
strPathFileName="./image.jpg";
|
||||
socketHeader.setVersion(1);
|
||||
socketHeader.setContentType(SocketHeader::ImageJPG);
|
||||
socketHeader.setContentLength(0);
|
||||
if(!openFile.open(strPathFileName,FileHandle::Read,FileHandle::ShareRead))
|
||||
{
|
||||
message("[SocketServer::sendDebugFrame] 'image.jpg' was not found, sending empty content");
|
||||
socketHeader.marshall(socketControl);
|
||||
}
|
||||
else
|
||||
{
|
||||
charBytes.size(openFile.size());
|
||||
socketHeader.setContentLength(charBytes.size());
|
||||
openFile.read((BYTE*)&charBytes[0],charBytes.size());
|
||||
socketHeader.marshall(socketControl);
|
||||
socketControl.send((char*)&charBytes[0],charBytes.size());
|
||||
message(String("[SocketServer::sendDebugFrame] Send ")+String().fromInt(charBytes.size()));
|
||||
openFile.close();
|
||||
}
|
||||
}
|
||||
32
CapServer/hold/SocketServer.hpp
Normal file
32
CapServer/hold/SocketServer.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef _CAPSERVER_SOCKETSERVER_HPP_
|
||||
#define _CAPSERVER_SOCKETSERVER_HPP_
|
||||
#ifndef _COMMON_BLOCK_HPP_
|
||||
#include <common/block.hpp>
|
||||
#endif
|
||||
#ifndef _THREAD_MONITOR_HPP_
|
||||
#include <thread/monitor.hpp>
|
||||
#endif
|
||||
#ifndef _CAPSERVER_WDMCAP_HPP_
|
||||
#include <CapServer/WDMCap.hpp>
|
||||
#endif
|
||||
#ifndef _CAPSERVER_GENERICSERVER_HPP_
|
||||
#include <CapServer/GenSrv.hpp>
|
||||
#endif
|
||||
|
||||
class SocketServer : public GenericServer
|
||||
{
|
||||
public:
|
||||
SocketServer(int deviceIndex=0,bool retain=false,bool debug=false,bool showSrcDlg=false);
|
||||
virtual ~SocketServer();
|
||||
protected:
|
||||
virtual void acceptHandler(SmartPointer<Socket> &socketControl);
|
||||
virtual void message(const String &message);
|
||||
private:
|
||||
void sendFrame(Socket &socketControl);
|
||||
void sendDebugFrame(Socket &socketControl);
|
||||
|
||||
WDMCap mWDMCap;
|
||||
bool mRetainImage;
|
||||
bool mDebug;
|
||||
};
|
||||
#endif
|
||||
101
CapServer/hold/WDMCap.cpp
Normal file
101
CapServer/hold/WDMCap.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <CapServer/WDMCap.hpp>
|
||||
#include <VidCap/VidReg.hpp>
|
||||
|
||||
WDMCap::WDMCap(int deviceIndex)
|
||||
: mDriverIndex(deviceIndex)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
WDMCap::~WDMCap()
|
||||
{
|
||||
}
|
||||
|
||||
void WDMCap::setCaptureFileName(const String &strPathCaptureFile)
|
||||
{
|
||||
BmpCap::setCaptureFileName(strPathCaptureFile);
|
||||
}
|
||||
|
||||
const String &WDMCap::getCaptureFileName(void)const
|
||||
{
|
||||
return BmpCap::getCaptureFileName();
|
||||
}
|
||||
|
||||
bool WDMCap::initialize(void)
|
||||
{
|
||||
String pathOutputFileName="capture.jpg";
|
||||
VidReg vidReg;
|
||||
|
||||
vidReg.setCaptureFile(pathOutputFileName);
|
||||
vidReg.setSequencing(false);
|
||||
VidCap::getDrivers(mDriverInfoBlock);
|
||||
if(!mDriverInfoBlock.size())
|
||||
{
|
||||
message("No drivers found! drivercount",mDriverInfoBlock.size());
|
||||
return false;
|
||||
}
|
||||
if(mDriverIndex>=mDriverInfoBlock.size())
|
||||
{
|
||||
message("[WDMCap::initilialize]Driver not found! device",mDriverIndex);
|
||||
return false;
|
||||
}
|
||||
showSettings();
|
||||
mControl=::new Control();
|
||||
mControl.disposition(PointerDisposition::Delete);
|
||||
mControl->createControl("BUTTON","",0,Rect(0,0,vidReg.getCaptureWidth(),vidReg.getCaptureHeight()),::GetDesktopWindow(),ControlID);
|
||||
BmpCap::initialize(mControl->getHandle(),mDriverIndex);
|
||||
if(!connect(mDriverIndex))
|
||||
{
|
||||
::printf("\007");
|
||||
message("[WDMCap::initilialize]Failed to connect to device",mDriverInfoBlock[mDriverIndex].driverName());
|
||||
return false;
|
||||
}
|
||||
saveFrames(true);
|
||||
return isConnected();
|
||||
}
|
||||
|
||||
void WDMCap::showSettings(void)
|
||||
{
|
||||
VidReg vidReg;
|
||||
|
||||
message("[WDMCap::initilialize]Capture File",vidReg.getCaptureFile());
|
||||
message("[WDMCap::initilialize]Sequencing",vidReg.getSequencing()?"true":"false");
|
||||
message("[WDMCap::initilialize]Preview Rate",vidReg.getPreviewRate());
|
||||
message("[WDMCap::initilialize]Preview Width",vidReg.getPreviewWidth());
|
||||
message("[WDMCap::initilialize]Preview Height",vidReg.getPreviewHeight());
|
||||
message("[WDMCap::initilialize]Capture Width",vidReg.getCaptureWidth());
|
||||
message("[WDMCap::initilialize]Capture Height",vidReg.getCaptureHeight());
|
||||
message("[WDMCap::initilialize]Capture Quality",vidReg.getQuality());
|
||||
for(int index=0;index<mDriverInfoBlock.size();index++)
|
||||
{
|
||||
if(index==mDriverIndex)message(String("[WDMCap::initilialize](*)")+mDriverInfoBlock[index].driverName());
|
||||
else message(String("[WDMCap::initilialize]")+mDriverInfoBlock[index].driverName());
|
||||
}
|
||||
}
|
||||
|
||||
bool WDMCap::grabFrame(void)
|
||||
{
|
||||
if(!isConnected())return false;
|
||||
mGrabTime=::GetTickCount();
|
||||
if(!BmpCap::grabFrame())return false;
|
||||
mFrameEvent.waitEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WDMCap::grabFrameNoStop(void)
|
||||
{
|
||||
if(!isConnected())return false;
|
||||
mGrabTime=::GetTickCount();
|
||||
if(!BmpCap::grabFrameNoStop())return false;
|
||||
mFrameEvent.waitEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
void WDMCap::frameHandler(VIDEOHDR &videoHeader)
|
||||
{
|
||||
mGrabTime=::GetTickCount()-mGrabTime;
|
||||
message("[WDMCap::frameHandler] Device grabbed frame (ms)",mGrabTime);
|
||||
BmpCap::frameHandler(videoHeader);
|
||||
mFrameEvent.setEvent();
|
||||
}
|
||||
|
||||
90
CapServer/hold/WDMCap.hpp
Normal file
90
CapServer/hold/WDMCap.hpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#ifndef _CAPSERVER_WDMCAP_HPP_
|
||||
#define _CAPSERVER_WDMCAP_HPP_
|
||||
#ifndef _COMMON_SMARTPOINTER_HPP_
|
||||
#include <common/pointer.hpp>
|
||||
#endif
|
||||
#ifndef _COMMON_BLOCK_HPP_
|
||||
#include <common/block.hpp>
|
||||
#endif
|
||||
#ifndef _COMMON_CONTROL_HPP_
|
||||
#include <common/control.hpp>
|
||||
#endif
|
||||
#ifndef _THREAD_EVENT_HPP_
|
||||
#include <thread/event.hpp>
|
||||
#endif
|
||||
#ifndef _VIDCAP_BMPCAP_HPP_
|
||||
#include <VidCap/BmpCap.hpp>
|
||||
#endif
|
||||
|
||||
class WDMCap : private BmpCap
|
||||
{
|
||||
public:
|
||||
WDMCap(int deviceIndex=0);
|
||||
virtual ~WDMCap();
|
||||
bool grabFrame(void);
|
||||
bool grabFrameNoStop(void);
|
||||
bool dialogVideoSource(void);
|
||||
bool isConnected(void)const;
|
||||
bool getDriverCaps(DriverCaps &someDriverCaps);
|
||||
bool getCaptureParams(CaptureParams &someCaptureParams);
|
||||
void setCaptureFileName(const String &strPathCaptureFile);
|
||||
const String &getCaptureFileName(void)const;
|
||||
protected:
|
||||
virtual void frameHandler(VIDEOHDR &videoHeader);
|
||||
private:
|
||||
enum{ControlID=100};
|
||||
bool initialize(void);
|
||||
void showSettings(void);
|
||||
void message(const String &message);
|
||||
void message(const String &message,const String &arg);
|
||||
void message(const String &message,int arg);
|
||||
|
||||
SmartPointer<Control> mControl;
|
||||
Block<DriverInfo> mDriverInfoBlock;
|
||||
Event mFrameEvent;
|
||||
DWORD mDriverIndex;
|
||||
DWORD mGrabTime;
|
||||
};
|
||||
|
||||
inline
|
||||
bool WDMCap::dialogVideoSource(void)
|
||||
{
|
||||
return BmpCap::dialogVideoSource();
|
||||
}
|
||||
|
||||
inline
|
||||
bool WDMCap::isConnected(void)const
|
||||
{
|
||||
return BmpCap::isConnected();
|
||||
}
|
||||
|
||||
inline
|
||||
bool WDMCap::getDriverCaps(DriverCaps &someDriverCaps)
|
||||
{
|
||||
return BmpCap::getDriverCaps(someDriverCaps);
|
||||
}
|
||||
|
||||
inline
|
||||
bool WDMCap::getCaptureParams(CaptureParams &someCaptureParams)
|
||||
{
|
||||
return BmpCap::getCaptureParams(someCaptureParams);
|
||||
}
|
||||
|
||||
inline
|
||||
void WDMCap::message(const String &message,int arg)
|
||||
{
|
||||
::printf("%s=%d\n",message.str(),arg);
|
||||
}
|
||||
|
||||
inline
|
||||
void WDMCap::message(const String &message,const String &arg)
|
||||
{
|
||||
::printf("%s=%s\n",message.str(),arg.str());
|
||||
}
|
||||
|
||||
inline
|
||||
void WDMCap::message(const String &message)
|
||||
{
|
||||
::printf("%s\n",message.str());
|
||||
}
|
||||
#endif
|
||||
96
CapServer/hold/gensrv.cpp
Normal file
96
CapServer/hold/gensrv.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <capserver/gensrv.hpp>
|
||||
#include <common/string.hpp>
|
||||
#include <common/systime.hpp>
|
||||
|
||||
extern bool running;
|
||||
|
||||
GenericServer::GenericServer(void)
|
||||
: mIsRunning(TRUE)
|
||||
{
|
||||
}
|
||||
|
||||
GenericServer::~GenericServer()
|
||||
{
|
||||
message("[GenericServer::~GenericServer]Server is destructing...");
|
||||
}
|
||||
|
||||
void GenericServer::close(void)
|
||||
{
|
||||
mIsRunning=FALSE;
|
||||
}
|
||||
|
||||
WORD GenericServer::listen(const String &hostName,short portNum)
|
||||
{
|
||||
HostEnt hostEntry;
|
||||
ServEnt serverEntry;
|
||||
String stringData;
|
||||
SystemTime systemTime;
|
||||
DWORD elapsedTime;
|
||||
|
||||
message(String("[GenericServer::listen]trying host'")+hostName+String("'..."));
|
||||
if(!mWSASystem.isInitialized()){message("[GenericServer::listen]WINSOCK initialization failure.");return FALSE;}
|
||||
InternetAddress internetAddress(hostName);
|
||||
if(!internetAddress.isZero()){if(!hostEntry.hostByAddress(internetAddress)){message(String("[GenericServer::listen]no DNS entry for ")+hostName);return FALSE;}}
|
||||
else if(!hostEntry.hostByName(hostName)){message(String("[GenericServer::listen]no DNS entry for ")+hostName);return FALSE;}
|
||||
message(String("[GenericServer::listen]connect...")+String("'")+hostEntry.hostName()+String("' (")+(String)(hostEntry.addresses())[0]+String(")"));
|
||||
mInternetSocketAddress.internetAddress((hostEntry.addresses())[0]);
|
||||
mInternetSocketAddress.family(PF_INET);
|
||||
mInternetSocketAddress.port(portNum);
|
||||
if(!mGenericControl.create()){message("[GenericServer::listen]socket initialization failure.");return FALSE;}
|
||||
mGenericControl.reuseAddress();
|
||||
if(!mGenericControl.bind(mInternetSocketAddress)){message("[GenericServer::listen]bind failed");return FALSE;}
|
||||
while(mIsRunning&&running)
|
||||
{
|
||||
SmartPointer<Socket> socketControl;
|
||||
socketControl=::new Socket();
|
||||
// socketControl.disposition(PointerDisposition::Assume);
|
||||
socketControl.disposition(PointerDisposition::Delete);
|
||||
INETSocketAddress internetSocketAddress;
|
||||
message(String("[GenericServer::listen]host='")+(hostEntry.addresses())[0]+String("' port=")+String().fromInt(portNum));
|
||||
String strLocalHost((String)mInternetSocketAddress.internetAddress());
|
||||
message(String("[GenericServer::listen]local address is ")+strLocalHost);
|
||||
if(!mGenericControl.listen()){message("[GenericServer::listen]listen failed");return FALSE;}
|
||||
message("[GenericServer::listen]waiting for connection...");
|
||||
if(!mGenericControl.accept(*socketControl,internetSocketAddress)){message("[GenericServer::listen]accept returned FALSE;");return FALSE;}
|
||||
|
||||
// socketControl->setLinger(0);
|
||||
// socketControl->bind(internetSocketAddress);
|
||||
|
||||
systemTime.refresh();
|
||||
message(String("[GenericServer::listen]service started from ")+internetSocketAddress.internetAddress().toString()+String(" on ")+systemTime.toString());
|
||||
elapsedTime=::GetTickCount();
|
||||
acceptHandler(socketControl);
|
||||
socketControl->destroy();
|
||||
socketControl.destroy();
|
||||
elapsedTime=::GetTickCount()-elapsedTime;
|
||||
message(String("[GenericServer::listen]service took ")+String().fromInt(elapsedTime)+String("(ms)"));
|
||||
}
|
||||
mGenericControl.destroy();
|
||||
message("[GenericServer::listen]terminating connection");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// virtuals
|
||||
|
||||
void GenericServer::message(const String &message)
|
||||
{
|
||||
::OutputDebugString(message+String("\n"));
|
||||
}
|
||||
|
||||
void GenericServer::message(Block<String> &msgData)
|
||||
{
|
||||
for(int msgIndex=0;msgIndex<msgData.size();msgIndex++)message(msgData[msgIndex]);
|
||||
}
|
||||
|
||||
void GenericServer::acceptHandler(SmartPointer<Socket> &socket)
|
||||
{
|
||||
SmartPointer<Socket> socketControl;
|
||||
|
||||
socketControl=socket;
|
||||
socketControl.disposition(PointerDisposition::Delete);
|
||||
String acceptString("[GenericServer::acceptHandler]200 GenericServer::acceptHandler accepting connection.");
|
||||
socketControl->send(acceptString);
|
||||
message(acceptString);
|
||||
return;
|
||||
}
|
||||
|
||||
42
CapServer/hold/gensrv.hpp
Normal file
42
CapServer/hold/gensrv.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef _CAPSERVER_GENERICSERVER_HPP_
|
||||
#define _CAPSERVER_GENERICSERVER_HPP_
|
||||
#ifndef _COMMON_STRING_HPP_
|
||||
#include <common/string.hpp>
|
||||
#endif
|
||||
#ifndef _COMMON_SMARTPOINTER_HPP_
|
||||
#include <common/pointer.hpp>
|
||||
#endif
|
||||
#ifndef _SOCKET_HOSTENT_HPP_
|
||||
#include <socket/hostent.hpp>
|
||||
#endif
|
||||
#ifndef _SOCKET_SERVENT_HPP_
|
||||
#include <socket/servent.hpp>
|
||||
#endif
|
||||
#ifndef _SOCKET_INETSOCKETADDRESS_HPP_
|
||||
#include <socket/intsaddr.hpp>
|
||||
#endif
|
||||
#ifndef _SOCKET_SOCKET_HPP_
|
||||
#include <socket/socket.hpp>
|
||||
#endif
|
||||
|
||||
class GenericServer
|
||||
{
|
||||
public:
|
||||
GenericServer(void);
|
||||
virtual ~GenericServer();
|
||||
WORD listen(const String &hostName,short portNum);
|
||||
WORD receive(char &charData,BOOL waitForData=TRUE);
|
||||
void close(void);
|
||||
protected:
|
||||
virtual void acceptHandler(SmartPointer<Socket> &socketControl);
|
||||
virtual void message(const String &message);
|
||||
virtual void message(Block<String> &msgData);
|
||||
private:
|
||||
enum{TimeOut=1000};
|
||||
INETSocketAddress mInternetSocketAddress;
|
||||
Socket mGenericControl;
|
||||
WSASystem mWSASystem;
|
||||
BOOL mIsRunning;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user