// RTSPClientTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock2.h> #include <conio.h> #pragma comment(lib, "wsock32.lib") #define Server_IP "192.168.1.31" #define RTSP_Port 554 #define RTSP_VERSION "RTSP/1.0" int socketInit(); int sendOPTIONS(SOCKET sockf, char *url, int CSeq); int sendDESCRIBE(SOCKET sockf, char *url, int CSeq); int sendSETUP(SOCKET sockf, char *url, int trackId, int CSeq); int sendPLAY(SOCKET sockf, char *url, int trackId, char *session, int CSeq); int CheckResultStr(char *resultStr, int len); char* getSession(char *setupResultStr); int _tmain(int argc, _TCHAR* argv[]) { struct sockaddr_in rtspaddr; struct sockaddr_in rtpaddr; struct sockaddr_in rtcpaddr; SOCKET rtspsock, rtpsock, rtcpsock; int rtpport, rtcpport; int err; char buff[1024*64]; fd_set read; SOCKET maxsock; struct timeval time_out; int readcount; int CSeq1 = 0; char url[100]; int commandFalg = 0; int commandEnd = 0; char Session[50]; rtspaddr.sin_family = AF_INET; rtspaddr.sin_addr.S_un.S_addr = inet_addr(Server_IP); rtspaddr.sin_port = htons(RTSP_Port); rtpaddr.sin_family = AF_INET; rtpaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.31"); rtpaddr.sin_port = htons(5000); if ((err = socketInit()) != 0) { printf("Can't find a usable winsock dll./nerrNumber: %d/n", err); } rtpsock = socket(AF_INET, SOCK_DGRAM, 0); if (rtpsock == INVALID_SOCKET) { err = WSAGetLastError(); printf("socket() fail./nerrNumber: %d/n", err); return -1; } err = bind(rtpsock, (struct sockaddr*)&rtpaddr, sizeof(rtpaddr)); rtspsock = socket(AF_INET, SOCK_STREAM, 0); if (rtspsock == INVALID_SOCKET) { err = WSAGetLastError(); printf("socket() fail./nerrNumber: %d/n", err); return -1; } err = connect(rtspsock, (struct sockaddr*)&rtspaddr, sizeof(rtspaddr)); if (err != 0) { err = WSAGetLastError(); printf("connect() fail./nerrNumber: %d/n", err); return -1; } sprintf_s(url, "rtsp://%s:%d/%s", Server_IP, RTSP_Port, "HD"); err = sendOPTIONS(rtspsock, url, CSeq1++); if (err != 0) { printf("send options fail./n"); return -1; } maxsock = rtspsock > rtpsock ? rtspsock : rtpsock; time_out.tv_sec = 3; time_out.tv_usec = 100; FILE *fp; fp = fopen("test", "wb"); if (fp == NULL) { printf("open file fail./n"); } int count = 0; while (1) { FD_ZERO(&read); FD_SET(rtspsock, &read); FD_SET(rtpsock, &read); err = select(maxsock + 1, &read, NULL, NULL, &time_out); if (err == 0) { printf("select timeout./n"); continue; } else if (err < 0) { printf("select fail./n"); break; } else { if (FD_ISSET(rtspsock, &read)) { printf("can read./n"); readcount = recv(rtspsock, buff, 1024*64-1, 0); buff[readcount] = '/0'; printf("read data: %s/n", buff); if (commandFalg == 3) { fwrite(buff, readcount, 1, fp); } if(commandEnd == 0) { if (CheckResultStr(buff, readcount) == 0) { if (getSession(buff) != NULL) { strcpy(Session, getSession(buff)); printf("session = %s", Session) ; } switch (commandFalg++) { case 0: sendDESCRIBE(rtspsock, url, CSeq1++); break; case 1: sendSETUP(rtspsock, url, 0, CSeq1++); break; case 2: sendPLAY(rtspsock, url, 0, Session, CSeq1++); commandEnd = 1; break; default: printf("no send data./n"); } } } } if (FD_ISSET(rtpsock, &read)) { printf("rtpsock can read./n"); } } if (kbhit()) { break; } } fclose(fp); closesocket(rtspsock); closesocket(rtpsock); getchar(); return 0; } int socketInit() { WORD wVersionRequest; WSADATA wsaData; int err; wVersionRequest = MAKEWORD(2, 2); err = WSAStartup(wVersionRequest, &wsaData); if (err != 0) {//tell the user that we could not find a usable winsock dll err = WSAGetLastError(); return err; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) !=2) {//tell the user that we could not find a usable winsock dll WSACleanup(); return -1; } return 0; } int sendOPTIONS(SOCKET sockf, char *url, int CSeq) { int err, len; char buff[1024]; char *Options = "OPTIONS %s %s/r/n/ CSeq: %d/r/n/ User-Agent: Robert TEST V1.0/r/n/r/n"; sprintf_s(buff, Options, url, RTSP_VERSION, CSeq); printf("play: %d/n", buff); len = strlen(buff); err = send(sockf, buff, len, 0); if (err != len) {//send data fail return -1; } return 0; } int sendDESCRIBE(SOCKET sockf, char *url, int CSeq) { int err, len; char buff[1024]; char *Describe = "DESCRIBE %s %s/r/n/ CSeq: %d/r/n/ Accept: application/sdp/r/n/ User-Agent: Robert TEST V1.0/r/n/r/n"; sprintf_s(buff, Describe, url, RTSP_VERSION, CSeq); printf("send describe data: /n%d", buff); len = strlen(buff); err = send(sockf, buff, len, 0); if (err != len) { return -1; } return 0; } int sendSETUP(SOCKET sockf, char *url, int trackId, int CSeq) { int err, len; char buff[1024]; char *Setup = "SETUP %s/trackID=%d %s/r/n/ CSeq: %d/r/n/ Transport: RTP/AVP/TCP;unicast;interleaved=0-1/r/n/ User-Agent: Robert TEST V1.0/r/n/r/n"; sprintf_s(buff, Setup, url, trackId, RTSP_VERSION, CSeq); printf("setup: %s/n", buff); len = strlen(buff); err = send(sockf, buff, len, 0); if (err != len) { return -1; } return 0; } int sendPLAY(SOCKET sockf, char *url, int trackId, char *session, int CSeq) { int err, len; char buff[1024]; char *Play = "PLAY %s/trackID=%d %s/r/n/ CSeq: %d/r/n/ Session: %s/r/n/ Range: npt=0.000-/r/n"; sprintf_s(buff, Play, url, trackId, RTSP_VERSION, CSeq, session); len = strlen(buff); err = send(sockf, buff, len, 0); if (err != len) { return -1; } return 0; } char* getSession(char *setupResultStr) { char temp[1024]; char temp1[50]; char *pos; char *sessionp = NULL; strcpy(temp, setupResultStr); pos = strstr(temp, "Session:"); if (pos != NULL) { strcpy(temp1, pos); //printf("pos = %s/n", pos); pos = strtok(temp1, ": "); pos = strtok(NULL, ": "); //printf("pos = %s/n", pos); sessionp = pos; } return sessionp; } int CheckResultStr(char *resultStr, int len) { char temp[1024]; strncpy(temp, resultStr, len); if (strncmp(temp, "RTSP/1.0 200 OK", strlen("RTSP/1.0 200 OK")) != 0) { return -1; } return 0; }