Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

mxcpcCameraLiveMxPEGFastStreamPOSIX.cpp

00001 //           ///          //                                        Mx clientSDK
00002 //          /////        ////                    Mx Crossplatform Client Library
00003 //         /// XXX     XXX ///
00004 //        ///    XXX XXX    ///     $RCSfile: mxcpcCameraLiveMxPEGFastStreamPOSIX.cpp,v $
00005 //       ///       XXX       ///     $Revision: 1.3 $
00006 //      ///      XXX XXX      ///     $Date: 2005/12/19 17:38:39 $
00007 //     ////    XXX     XXX    ////     $Author: cvs-kai $
00008 //    ////                     ////
00009 //   ////  M  O  B  O  T  I  X  ////////////////////////////////////////////////
00010 //  //// Security Vision Systems //////////////////////////////////////////////
00011 //                                                                          //
00012 //  Copyright (C) 2005 - 2006, MOBOTIX AG, Germany                         //
00013 //  This software is made available under the BSD licence. Please refer   //
00014 //  to the file LICENCE.TXT contained in this distribution for details.  //
00015 //                                                                      //
00016 //  /////////////////////////////////////////////////////////////////////
00017 
00018 
00019 
00020 #include <mxcpcCameraLiveMxPEGFastStreamPOSIX.h>
00021 #include <mxcpc_exceptions.h>
00022 
00023 #include <cstdio>
00024 
00025 #include <sys/types.h>
00026 #include <sys/socket.h>
00027 #include <netinet/in.h>
00028 #include <unistd.h>
00029 #include <fcntl.h>
00030 
00031 
00032 
00033 mxcpcCameraLiveMxPEGFastStreamPOSIX
00034  ::mxcpcCameraLiveMxPEGFastStreamPOSIX(int ip1,
00035                                        int ip2, 
00036                                        int ip3, 
00037                                        int ip4, 
00038                                        int port,
00039                                        int fps,
00040                                        bool asynchronous)
00041     : mxcpcCameraLiveMxPEGFastStream(ip1,
00042                                      ip2,
00043                                      ip3,
00044                                      ip4,
00045                                      port,
00046                                      fps) {
00047                                                                          
00048   constructCommonStuff(mxcpcIPAddress(ip1, ip2, ip3, ip4), port,
00049                        (float)fps,
00050                        asynchronous);
00051 }
00052    
00053 
00054 mxcpcCameraLiveMxPEGFastStreamPOSIX
00055  ::mxcpcCameraLiveMxPEGFastStreamPOSIX(const mxcpcIPAddress& camera_ip, 
00056                                        int port,
00057                                        float fps,
00058                                        bool asynchronous)
00059     : mxcpcCameraLiveMxPEGFastStream(camera_ip.getIP1(),
00060                                      camera_ip.getIP2(),
00061                                      camera_ip.getIP3(),
00062                                      camera_ip.getIP4(),
00063                                      port,
00064                                      (int)fps)                         {
00065          
00066   constructCommonStuff(camera_ip, port,
00067                        fps,
00068                        asynchronous);
00069 }
00070 
00071 
00072 void mxcpcCameraLiveMxPEGFastStreamPOSIX
00073       ::constructCommonStuff(const mxcpcIPAddress& camera_ip, 
00074                              int port,
00075                              float fps,
00076                              bool asynchronous)               {         
00077                                                                                     
00078   in_addr_t server_ip;
00079   struct sockaddr_in server_socket_info;
00080   int flags;
00081   char txt[160], *txt_ptr;
00082   int len, written;
00083   
00084   
00085   Socket = -1;
00086   try {
00087   
00088     // get a socket
00089     Socket = socket(PF_INET, SOCK_STREAM, 0);
00090     if(Socket < 0) 
00091       throw mxcpcIOFailure();
00092     
00093     // connect to server...
00094     server_socket_info.sin_family = AF_INET;
00095     server_socket_info.sin_port   = htons(port);
00096     server_ip = 0;
00097     server_ip += camera_ip.getIP1();   server_ip *= 256;
00098     server_ip += camera_ip.getIP2();   server_ip *= 256;
00099     server_ip += camera_ip.getIP3();   server_ip *= 256;
00100     server_ip += camera_ip.getIP4();
00101     server_socket_info.sin_addr.s_addr = htonl(server_ip);
00102     if(::connect(Socket, (const struct sockaddr *)&server_socket_info,
00103                          (socklen_t)sizeof(server_socket_info))) 
00104       throw mxcpcIOFailure();
00105       
00106     // if requested, make socket nonblocking...
00107     if(asynchronous) {
00108       flags = fcntl(Socket, F_GETFL);
00109       flags |= O_NONBLOCK;
00110       fcntl(Socket, F_SETFL, flags);  
00111     }
00112       
00113     // do HTTP GET request 
00114     if(fps < 0.0f) fps = 4.0f;
00115     std::sprintf(txt, "GET /control/faststream.jpg?noaudio&fps=%f HTTP/1.0\n\n",
00116                  fps);
00117     len = std::strlen(txt);
00118     txt_ptr = txt;
00119     do {
00120       written = write(Socket, txt_ptr, len);
00121       if(written < 0) throw mxcpcIOFailure();
00122       len -= written;
00123       txt_ptr += written;
00124     } while(len);
00125       
00126   } catch(std::exception &e) {
00127     
00128       MXCPC_RETHROW_UNHANDLED_EXCEPTION(e);
00129       
00130       if(Socket >= 0) shutdown(Socket, SHUT_RDWR);
00131     
00132       throw;
00133     }
00134 }
00135 
00136 
00137 mxcpcCameraLiveMxPEGFastStreamPOSIX::~mxcpcCameraLiveMxPEGFastStreamPOSIX() {
00138 
00139   shutdown(Socket, SHUT_RDWR);
00140 }
00141 
00142 
00143 
00144 int mxcpcCameraLiveMxPEGFastStreamPOSIX::fetchBytes(unsigned char *buffer, 
00145                                                     int num)               {
00146                                                     
00147    int result;
00148    
00149    result = read(Socket, buffer, num);
00150    if(result < 0) result = 0;
00151    
00152    return(result);
00153 }

Generated on Fri Jan 20 13:33:31 2006 for mxcpc by  doxygen 1.4.4