00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <mxcpcCameraLiveMxPEGFastStreamPOSIX.h>
00015 #include <mxcpc_exceptions.h>
00016
00017 #include <cstdio>
00018
00019 #include <sys/types.h>
00020
00021 #ifdef WIN32
00022 #define in_addr_t unsigned long
00023 #define socklen_t int
00024 #include <windows.h>
00025 #include <io.h>
00026 #else
00027 #include <sys/socket.h>
00028 #include <netinet/in.h>
00029 #include <unistd.h>
00030 #endif
00031 #include <fcntl.h>
00032
00033
00034
00035 mxcpcCameraLiveMxPEGFastStreamPOSIX::mxcpcCameraLiveMxPEGFastStreamPOSIX(int ip1,
00036 int ip2,
00037 int ip3,
00038 int ip4,
00039 int port,
00040 int fps)
00041 : mxcpcCameraLiveMxPEGFastStream(ip1,
00042 ip2,
00043 ip3,
00044 ip4,
00045 port,
00046 fps) {
00047
00048 in_addr_t server_ip;
00049 struct sockaddr_in server_socket_info;
00050 #ifndef WIN32
00051 int flags;
00052 #endif
00053 char txt[100], *txt_ptr;
00054 int len, written;
00055
00056
00057 Socket = -1;
00058 try {
00059
00060
00061 Socket = socket(PF_INET, SOCK_STREAM, 0);
00062 if(Socket < 0)
00063 throw mxcpcIOFailure();
00064
00065
00066 server_socket_info.sin_family = AF_INET;
00067 server_socket_info.sin_port = htons(port);
00068 server_ip = 0;
00069 server_ip += ip4; server_ip *= 256;
00070 server_ip += ip3; server_ip *= 256;
00071 server_ip += ip2; server_ip *= 256;
00072 server_ip += ip1;
00073 server_socket_info.sin_addr.s_addr = server_ip;
00074 if(::connect(Socket, (const struct sockaddr *)&server_socket_info,
00075 (socklen_t)sizeof(server_socket_info)))
00076 throw mxcpcIOFailure();
00077
00078
00079 #ifdef WIN32 // WIN32 oughta be POSIX, eh?
00080 unsigned long i_dummy = 1;
00081 ioctlsocket(Socket, FIONBIO, &i_dummy);
00082 #else
00083 flags = fcntl(Socket, F_GETFL);
00084 flags |= O_NONBLOCK;
00085 fcntl(Socket, F_SETFL, flags);
00086 #endif
00087
00088 if(fps < 1) fps = 4;
00089 std::sprintf(txt, "GET /control/faststream.jpg?noaudio&fps=%d HTTP/1.0\n\n",
00090 fps);
00091 len = std::strlen(txt);
00092 txt_ptr = txt;
00093 do {
00094 written = write(Socket, txt_ptr, len);
00095 if(written < 0) throw mxcpcIOFailure();
00096 len -= written;
00097 txt_ptr += written;
00098 } while(len);
00099
00100 } catch(std::exception &e) {
00101
00102 MXCPC_RETHROW_UNHANDLED_EXCEPTION(e);
00103
00104 if(Socket >= 0) {
00105 #ifdef WIN32
00106 closesocket(Socket);
00107 #else
00108 shutdown(Socket, SHUT_RDWR);
00109 #endif
00110 }
00111
00112 throw;
00113 }
00114 }
00115
00116
00117 mxcpcCameraLiveMxPEGFastStreamPOSIX::~mxcpcCameraLiveMxPEGFastStreamPOSIX() {
00118
00119 if(Socket >= 0) {
00120 #ifdef WIN32
00121 closesocket(Socket);
00122 #else
00123 shutdown(Socket, SHUT_RDWR);
00124 #endif
00125 }
00126 }
00127
00128
00129
00130 int mxcpcCameraLiveMxPEGFastStreamPOSIX::fetchBytes(unsigned char *buffer,
00131 int num) {
00132
00133 int result;
00134
00135 result = read(Socket, buffer, num);
00136 if(result < 0) result = 0;
00137
00138 return(result);
00139 }