/**
 * \function 	SendLiveStreamDataPort
 * \brief    	使用TCP/IP协议实现H.264子码流的网络传输
 * \			默认作为服务器端,Port = 61001		
 * \note		除了TCP/IP的传输方式外,智能相机系统还支持H.264视频流的RTSP方式传输,其传输过程已被封装在系统内部,默认TCP/IP传输的优先级高于RTSP传输的优先级
**/	
void SendLiveStreamDataPort_Another()
{
	SOCKET	sockThis, sockAccept;
	struct	sockaddr_in addrThis, addrAccept;
	int		nAddrLen;
	int		nVideoWidth = 1600, nVideoHeight = 1216;
	int 	i;
	int		isValidLink = 1;

	fdOpenSession( TaskSelf() );

	do
	{
		g_pBuffsLiveStream_Another = MEM_alloc(extHeap, g_nMaxBuffsNumLiveStream_Another*sizeof(DataPoolItem), 256);
	}while(g_pBuffsLiveStream_Another == 0);
	
	memset(g_pBuffsLiveStream_Another, 0, g_nMaxBuffsNumLiveStream_Another*sizeof(DataPoolItem));
	for (i = 0; i < g_nMaxBuffsNumLiveStream_Another; i++)
	{
		do
		{
			g_pBuffsLiveStream_Another[i].pBuf = MEM_alloc(extHeap, g_nMaxFrameSizeLiveStream_Another, 256);
		}while(g_pBuffsLiveStream_Another[i].pBuf == 0);	
	}

	sockThis = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if( sockThis == INVALID_SOCKET )
	{
		Roseek_Reset();//如果创建侦听对象失败,重启相机
	}

	bzero( &addrAccept, sizeof(addrAccept) );
	nAddrLen = sizeof(addrAccept);
	bzero( &addrThis, sizeof(addrThis) );
	addrThis.sin_len	= sizeof( addrThis );
	addrThis.sin_family	= AF_INET;
	addrThis.sin_addr.s_addr = INADDR_ANY;
	addrThis.sin_port	= htons( 61001 );
	//sockFconListen绑定
	if ( bind( sockThis, (PSA) &addrThis, sizeof(addrThis) ) < 0 )
	{
		Roseek_Reset();//如果绑定失败,重启相机
	}
	//sockFconListen开始监听,同一时刻仅支持一个连接
	if ( listen( sockThis, 1) < 0 ){
		Roseek_Reset();//如果侦听失败,重启相机
	}
	//迭代循环
	while( 1 )
	{
		sockAccept = INVALID_SOCKET;
		do
		{
			sockAccept = accept( sockThis, (PSA)&addrAccept, &nAddrLen );
			if( sockAccept == INVALID_SOCKET)
			{
				break;
			}
			
			sock_setTimeout(sockAccept, SO_RCVTIMEO, 3000);
     	sock_setTimeout(sockAccept, SO_SNDTIMEO, 3000);

			g_bIsLiveStreamClientConnect_Another = TRUE;
			g_IsUsedByTCPSend_Another = TRUE;
			if (SafeSend(sockAccept, (Uint8*)&nVideoWidth, 4) != 4)
			{
				break;
			}
			if (SafeSend(sockAccept, (Uint8*)&nVideoHeight, 4) != 4)
			{
				break;
			}

			while( 1 )
			{
				if(isEncStop && g_nNumHasDataLSBuffs_Another <= 1)
				{
					Roseek_Start_Enc();
					isEncStop = 0;
					LOG_printf( &trace, "Roseek_Start_Enc!!!");
				}

				//等待信号灯同步
				isValidLink = 1;
				while (!SEM_pend( &sem_LiveStreamDataReady_Another, 5000))
				{
					int nRet;
					struct timeval tvTimeOut;
					fd_set fsVal;
					FD_ZERO(&fsVal);
					FD_SET(sockAccept, &fsVal);
					tvTimeOut.tv_sec = 0;
					tvTimeOut.tv_usec = 100;
					nRet = fdSelect(0, 0, &fsVal, 0, &tvTimeOut);
					if (0 == nRet || -1 == nRet)
					{
						isValidLink = 0;
						break;
					}
				}
				//SEM_pend( &sem_LiveStreamDataReady_Another, SYS_FOREVER );

				if (!isValidLink)
				{
					break;
				}
				
				//判断上传哪个缓冲区内容
				
				if (g_nNumHasDataLSBuffs_Another <= 0)
				{
					continue;
				}

				if (!isEncStop && (g_nNumHasDataLSBuffs_Another >= (g_nMaxBuffsNumLiveStream_Another-3)))
				{
					Roseek_Stop_Enc();
					isEncStop = 1;
					LOG_printf( &trace, "Roseek_Stop_Enc!!!");
				}

				if (g_nNumHasDataLSBuffs_Another >= (g_nMaxBuffsNumLiveStream_Another-1) && (g_pBuffsLiveStream_Another[g_nReadPosLSBuffs_Another].pBuf[8]&0x1F) != 7)
				{
					LOG_printf( &trace, "this frame not send!!!");
				}
				else
				{
					nDataLen = *(Uint32*)(g_pBuffsLiveStream_Another[g_nReadPosLSBuffs_Another].pBuf) + 4;
					if((nSendlen = SafeSend( sockAccept, g_pBuffsLiveStream_Another[g_nReadPosLSBuffs_Another].pBuf, nDataLen)) != nDataLen)
					{
							LOG_printf( &trace, "send error!!!");
							break;
					}
				}			
				
				g_nReadPosLSBuffs_Another = (++g_nReadPosLSBuffs_Another)%g_nMaxBuffsNumLiveStream_Another;
				g_nNumHasDataLSBuffs_Another--;
				
			}
		}while(0);
		
		if (sockAccept != INVALID_SOCKET)
		{
			fdClose(sockAccept);
			sockAccept = INVALID_SOCKET;
		}
		
		g_IsUsedByTCPSend_Another = FALSE;
		g_bIsLiveStreamClientConnect_Another = FALSE;
		SEM_reset( &sem_LiveStreamDataReady_Another, 0 );
		g_nNumHasDataLSBuffs_Another = 0;
		g_nReadPosLSBuffs_Another = 0;
		g_nWritePosLSBuffs_Another = 0;

		if(isEncStop)
		{
			Roseek_Start_Enc();
			isEncStop = 0;
			LOG_printf( &trace, "Roseek_Start_Enc!!!");
		}
	}//迭代循环
}
示例#2
0
int select(int maxfds, fd_set *readFds, fd_set *writeFds, fd_set *exceptFds, struct timeval *timeVal)
{
    return fdSelect(maxfds, readFds, writeFds, exceptFds, timeVal);
}
示例#3
0
void nullsrv()
{


    SOCKET   stcpactive = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    struct   timeval timeout;           // Timeout struct for select
    int      size;
    int      cnt;
    char     *pBuf;
    HANDLE   hBuffer;

    //gpioEnableGlobalInterrupt();
    // Allocate the file environment for this task
    fdOpenSession(TaskSelf());


    TaskSleep(15000);
    platform_write("Raw Eth Task Started ... \n");
    // Create the main TCP listen socket
    platform_write("creating main TCP listen socket\n");

    int k=0;
        int n=0;
       IMG_STORE_REQST_TYP a;


            int *data;
            UInt32 time1,time2;
           for(;;)
           {
        	   	   //time1=Clock_getTicks();
        	   	   a.fps=acquire_data();
        	   	   a.number=min((int)a.fps*sizeof(int),PULSE_SAMPLE*sizeof(int));
        	   	   //time2=Clock_getTicks();
        	   	   ///platform_write("time taken for acquire data is %lu \n",(unsigned long)(time2-time1));
        	   	   //time1=Clock_getTicks();
                   for(k=0;k<2;k++)
                   {
                    if(k==0)
                    data=&data_bufferA[0];
                    else
                    data=&data_bufferB[0];
                   //WWdis_data();
                       stcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                       if( stcp == INVALID_SOCKET )
                       {
                           int ret=fdError();
                           goto leave;

                       }


        timeout.tv_sec  = 30;
        timeout.tv_usec = 0;
        setsockopt( stcp, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) );
        setsockopt( stcp, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );
        //timeout.tv_sec  = 3;
        //int opt = 1;
        //setsockopt( stcp, SOL_SOCKET, SO_BLOCKING , &opt, sizeof( opt ) );

        platform_write("created main TCP client socket\n");
        // Set Port = 1001, leaving IP address = Any
        bzero( &sin1, sizeof(struct sockaddr_in) );
        sin1.sin_family = AF_INET;
        sin1.sin_addr.s_addr =inet_addr(PCStaticIP);
        sin1.sin_port   = htons(7000);
        sin1.sin_len    = sizeof( sin1 );




        platform_write("LLL %d \n",(sin1.sin_addr.s_addr));
        //fcntl(stcp, F_SETFL, O_NONBLOCK);





              //while(1)

              if( connect(stcp, (struct sockaddr *) &sin1, sizeof(sin1)) < 0)
              {
            	  if (fdError() == EINPROGRESS)
            	  {
            		  	  	  	 fd_set wrsd;
            		             struct timeval tv;

            		             FD_ZERO(&wrsd);
            		             FD_SET(stcp, &wrsd);
            		             tv.tv_sec = 2;
            		             tv.tv_usec = 0;
            		             int ret = fdSelect((int)stcp, 0, &wrsd, 0, &tv );
            		             if(ret>0)
            		             {
            		            	 int err, lenErr;
            		            	  lenErr = sizeof(err);
            		            	  ret = getsockopt(stcp, SOL_SOCKET, SO_ERROR, &err, &lenErr);
            		            	  if(ret==0 && err==0)
            		            	  {
            		            		  platform_write("connection completed");
            		            		  break;
            		            	  }
            		            	  else
            		            	  {
            		            		  platform_write("Attempting to connect again");
            		            		  continue;
            		            	  }
            		             }

            	  }

              }

          platform_write("completed connect \n");
         a.hydrophone=k;
         n=0;
            n = send(stcp,&a,sizeof(a),0);
              if (n < 0)
              {
                  perror("ERROR writing to socket");
                  break;
              }


              platform_write("writing hydrophone %d,bytes %d\n",k,a.number);

                n=0;
                int c=0;
                //platform_write("writing %s  \n",data);
                do
                {
                n = send(stcp,data,a.number-c,0);
                //platform_write("writing %d bytes \n",n);
                  if (n < 0)
                  {
                      perror("ERROR writing to socket");
                      break;
                  }
                  c=c+n;
                  data=data+n;
                }while(c<a.number);
                if (n < 0)
                	break;
                //free(orig);
                int status;
                HANDLE hbuffer;
                n = recv(stcp, &status, sizeof(status),0);

                if(status==0)
                {
                    platform_write("Client request success\n");
                }
                else
                {
                    platform_write("Client request error");
                    continue;
                }
                if( stcp != INVALID_SOCKET )
                fdClose(stcp);


             }
                   if( stcp != INVALID_SOCKET )
                   {

						      shutdown(stcp,0);
						      fdClose(stcp);
                   }
                   //time2=Clock_getTicks();
                   platform_write("time taken for  data transfer is  %lu \n",(unsigned long)(time2-time1));
               //TaskSleep(5);fe
    }

leave:

    TaskSleep(5000);
    // We only get here on an error - close the sockets
  //  if( stcp != INVALID_SOCKET )
    //    fdClose( stcp );

    platform_write("NullSrv Fatal Error\n");

//    NetworkClose((HANDLE)TaskSelf());
    // This task is killed by the system - here, we block
    //TaskBlock( TaskSelf() );
}