#pragma comment(lib, "Ws2_32.lib")
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#include "H264Decoder.h"
#define BASEPORT 0
int main(int argc,char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
int portIndex =0;
if (argc >1)
{
portIndex = atoi(argv[1]); // argv like 8001,is port 8001 recv rtp.
}
H264Decoder h264Decoder(BASEPORT+portIndex);
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
// SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM, 0);
int i;
int af, type, proto;
af = 2;
type = 2;
proto =0;
SOCKET sockSrv[socketNum];
//Try to find a QOS-enabled protocol
DWORD bufferSize = 0;
DWORD numProtocols = WSAEnumProtocols(proto != 0 ? &proto : NULL, NULL, &bufferSize);
LPWSAPROTOCOL_INFO installedProtocols = (LPWSAPROTOCOL_INFO)(new BYTE[bufferSize]);
numProtocols = WSAEnumProtocols(proto != 0 ? &proto : NULL, installedProtocols, &bufferSize);
LPWSAPROTOCOL_INFO qosProtocol = installedProtocols;
for (DWORD j = 0; j < numProtocols; qosProtocol++, j++) {
if ((qosProtocol->dwServiceFlags1 & XP1_QOS_SUPPORTED) &&
(qosProtocol->iSocketType == type) &&
(qosProtocol->iAddressFamily == af)) {
for (i =0;i <socketNum;i++)
{
sockSrv[i] = WSASocket(af, type, proto, qosProtocol, 0, WSA_FLAG_OVERLAPPED);
}
break;
}
}
/*FD_SET readSet;
FD_ZERO(&readSet);
FD_SET(sockSrv,&readSet);*/
SOCKADDR_IN addrServ[socketNum];
int value =0;
socklen_t valSize = sizeof(value);
for (i =0;i <socketNum;i++)
{
addrServ[i].sin_addr.S_un.S_addr = htonl(INADDR_ANY);//addr4->sin_addr = ip;
addrServ[i].sin_family = AF_INET;
addrServ[i].sin_port = htons(BASEPORT+portIndex);
bind(sockSrv[i],(SOCKADDR*)&addrServ[i],sizeof(SOCKADDR));
getsockopt(sockSrv[i], SOL_SOCKET, SO_RCVBUF,(char *)&value, &valSize);
printf("socket size: %d\n",value);
/*value = 16384*4;
setsockopt(sockSrv[i], SOL_SOCKET, SO_RCVBUF,(char *)&value, sizeof(value));
value =0;
getsockopt(sockSrv[i], SOL_SOCKET, SO_RCVBUF,(char *)&value, &valSize);
printf("socket size: %d\n",value);*/
}
/* int value =0;
bind(sockSrv,(SOCKADDR*)&addrServ,sizeof(SOCKADDR));
setsockopt(sockSrv, 65535, SO_REUSEADDR,(char *)&value, sizeof(value));
value =184;
setsockopt(sockSrv, IPPROTO_IP, IP_TOS,(char *)&value, sizeof(value));*/
SOCKADDR_IN addrClient[socketNum];
int length = sizeof(SOCKADDR);//sockaddr_storage
//sockaddr_storage addrClient;
// int length = sizeof(sockaddr_storage);//sockaddr_storage
unsigned char* recvBuf;//=(unsigned char*)malloc(1500);
unsigned flags = 0;
int recvBufLength = 0;
int lastSeqNum =0;
int len =10000;
i =0;
int readyRead = 0;
recvBuf=(unsigned char*)malloc(len);
//DWORD fionbio =1;
//ioctlsocket(sockSrv, FIONBIO, &fionbio);
while (1)
{
flags = 0;
len =10000;
//memset(recvBuf,0,len);
DWORD available;
for (i =0;i <socketNum;i++)
{
ioctlsocket(sockSrv[i], FIONREAD, &available);
if (available > 0 && len >available)
len = available;
memset(recvBuf,0,len);
recvBufLength = recvfrom(sockSrv[i],(char*)recvBuf,len,0,(SOCKADDR*)&addrServ[i],&length);
if (recvBufLength < 0)
{
printf("*****recvBufLength < 0");
continue;
}
/*RTPFrame recvRTP(recvBuf, recvBufLength);
if (( recvRTP.GetSequenceNumber() - lastSeqNum) > 1)
{
printf("*****lost packet %d. recvBufLength :%d available:%d\n",( recvRTP.GetSequenceNumber() - lastSeqNum),recvBufLength,available);
}
lastSeqNum = recvRTP.GetSequenceNumber();*/
//printf("available:%d\n",available);
h264Decoder.Transcode(recvBuf,recvBufLength,flags);
}
/*while (available == 0) {
ioctlsocket(sockSrv, FIONREAD, &available);
int selval = ::select(1,&readSet, NULL, NULL, NULL);
if (selval == SOCKET_ERROR)
{
continue;
}
for (i=0;i<readSet.fd_array[i];i++)
{
if (FD_ISSET(readSet.fd_array[i],&readSet))
{
readyRead =1;
}
}
if (readyRead ==0)
{
continue;
}else
{
readyRead =0;
}
}*/
//Sleep(1);
}
free(recvBuf);
for (i =0;i <socketNum;i++)
{
closesocket(sockSrv[i]);
}
WSACleanup();
system("PAUSE");
return 0;
}
评论6
最新资源