rtsp over udp
#include <stdio.h> #include <netinet/in.h> #include <sys/socket.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/select.h> #include <sys/time.h> #include <stdlib.h> #define SERVER_IP "192.168.1.163" #define SERVER_PORT 8557 #define CMD_LEN 1024*2 #define RTSP_URL "rtsp://192.168.1.163:8557/PSIA/Streaming/channels/2?videoCodecType=H.264" #define RECV_LEN 1460 #define BYTE_FILE "source.264" //生成rtsp发送命令 char * GetRTSPCmd(const char *); //发送命令数据 long SendRTSPCmd(int sock, const char *cmd, const char *szparam); //读取数据 long ReadSocket(int sock, char *buf, int len, int timeout ); int main(){ struct sockaddr_in addr_server; char buf[CMD_LEN]; char rbuf[RECV_LEN]; int sock_fd=socket(AF_INET,SOCK_STREAM,0); if(sock_fd==-1){ printf("create the socket failed.\n"); return 0; } memset(&addr_server,0,sizeof(struct sockaddr_in)); addr_server.sin_family=AF_INET; addr_server.sin_addr.s_addr=inet_addr(SERVER_IP); addr_server.sin_port=htons(SERVER_PORT); if(connect(sock_fd,(struct sockaddr*)&addr_server,sizeof(struct sockaddr))==-1){ printf("connect the server failed./n"); return 0; } memset(buf,0,CMD_LEN); sprintf(buf,GetRTSPCmd("OPTIONS"),RTSP_URL,1,"",""); SendRTSPCmd(sock_fd, "OPTIONS",buf); ReadSocket(sock_fd,buf,CMD_LEN,10); memset(buf,0,CMD_LEN); sprintf(buf,GetRTSPCmd("DESCRIBE"),RTSP_URL,1,"","",""); SendRTSPCmd(sock_fd, "DESCRIBE", buf); ReadSocket(sock_fd,buf,CMD_LEN,10); //connet to the video memset(buf,0,CMD_LEN); sprintf(buf, "SETUP rtsp://%s:%d/PSIA/Streaming/channels/2?videoCodecType=H.264/track1 RTSP/1.0\r\n" "CSeq: 1\r\n" "Transport: RTP/AVP;unicast;client_port=57102-57103\r\n\r\n",SERVER_IP,SERVER_PORT); SendRTSPCmd(sock_fd, "SETUP", buf); ReadSocket(sock_fd,buf,CMD_LEN,10); /* //connect to the audio memset(buf,0,CMD_LEN); sprintf(buf, "SETUP rtsp://%s:%d/PSIA/Streaming/channels/2?videoCodecType=H.264/track2 RTSP/1.0\r\n" "CSeq: 1\r\n" "Transport: RTP/AVP;unicast;client_port=57104-57105\r\n\r\n",SERVER_IP,SERVER_PORT); SendRTSPCmd(sock_fd, "SETUP", buf); ReadSocket(sock_fd,buf,CMD_LEN,10);*/ //get the session and server port char psession[50],pserverport[10]; char* ptr; memset(psession,0,50);memset(pserverport,0,10); ptr=strstr(buf,"Session:"); if(ptr==0){ printf("cannot find the session.\n"); return 0; } memcpy(psession,ptr+strlen("Session:"),strstr(ptr,"\r\n")-ptr-strlen("Session:")); printf("Session:%s\n",psession); ptr=strstr(buf,"server_port="); memcpy(pserverport,ptr+strlen("server_port="),strstr(ptr,"-")-ptr-strlen("server_port=")); printf("Server port: %s\n",pserverport); printf("\n"); memset(buf,0,CMD_LEN); sprintf(buf,GetRTSPCmd("PLAY"),RTSP_URL,1,psession,"Range: npt=0.000-\r\n","","",""); SendRTSPCmd(sock_fd, "PLAY", buf); ReadSocket(sock_fd,buf,CMD_LEN,10); int save_fd=open(BYTE_FILE,O_CREAT|O_WRONLY); struct sockaddr_in addr_stream; struct sockaddr_in addr_local; int recv_fd=socket(AF_INET,SOCK_DGRAM,0); if(recv_fd==-1){ printf("create the stream socket failed.\n"); return 0; } addr_local.sin_family=AF_INET; addr_local.sin_addr.s_addr=inet_addr("192.168.1.199"); addr_local.sin_port=htons(57102); if(bind(recv_fd,(struct sockaddr*)&addr_local,sizeof(struct sockaddr))==-1){ printf("bind the local stream socket failed.\n"); return 0; } addr_stream.sin_family=AF_INET; addr_stream.sin_addr.s_addr=inet_addr(SERVER_IP); addr_stream.sin_port=htons(atoi(pserverport)); int len_recv; int len_addr=sizeof(addr_stream); while(1){ memset(rbuf,0,RECV_LEN); len_recv=recvfrom(recv_fd,rbuf,RECV_LEN,0,(struct sockaddr*)&addr_stream,&len_addr); if(len_recv==-1){ printf("recvfrom the stream failed.\n"); return 0; } write(save_fd,rbuf,len_recv); printf("recvfrom:%d\n",len_recv); } close(recv_fd); close(save_fd); return 1; } char * GetRTSPCmd( const char * szName) { char *str = NULL; char const* cmdFmt = NULL; if(!strcmp(szName, "OPTIONS")) { cmdFmt = "OPTIONS %s RTSP/1.0\r\n" "CSeq: %d\r\n" "%s" "%s" "\r\n"; } else if(!strcmp(szName, "ANNOUNCE")) { cmdFmt = "ANNOUNCE %s RTSP/1.0\r\n" "CSeq: %d/r/n" "Content-Type: application/sdp\r\n" "%s" "Content-length: %d\r\n\r\n" "%s"; } else if(!strcmp(szName, "PLAY")) { cmdFmt = "PLAY %s RTSP/1.0\r\n" "CSeq: %d\r\n" "Session: %s\r\n" "%s" "%s" "%s" "%s" "\r\n"; } else if(!strcmp(szName, "PAUSE")) { cmdFmt = "PAUSE %s RTSP/1.0\r\n" "CSeq: %d\r\n" "Session: %s\r\n" "%s" "%s" "/r/n"; } else if(!strcmp(szName, "RECORD")) { cmdFmt = "RECORD %s%s%s RTSP/1.0\r\n" "CSeq: %d\r\n" "Session: %s\r\n" "Range: npt=0-\r\n" "%s" "%s" "\r\n"; } else if(!strcmp(szName, "SET_PARAMETER")) { cmdFmt = "SET_PARAMETER %s RTSP/1.0\r\n" "CSeq: %d\r\n" "Session: %s\r\n" "%s" "%s" "Content-length: %d\r\n\r\n" "%s: %s\r\n"; } else if(!strcmp(szName, "GET_PARAMETER")) { cmdFmt = "GET_PARAMETER %s RTSP/1.0\r\n" "CSeq: %d\r\n" "Session: %s\r\n" "%s" "%s" "Content-type: text/parameters\r\n" "Content-length: %d\r\n\r\n" "%s\r\n"; } else if(!strcmp(szName, "TEARDOWN")) { cmdFmt = "TEARDOWN %s RTSP/1.0\r\n" "CSeq: %d\r\n" "Session: %s\r\n" "%s" "%s" "\r\n"; } else if(!strcmp(szName, "DESCRIBE")) { cmdFmt = "DESCRIBE %s RTSP/1.0\r\n" "CSeq: %d\r\n" "%s" "%s" "%s" "\r\n"; } else if(!strcmp(szName, "ANNOUNCE")) { cmdFmt = "ANNOUNCE %s RTSP/1.0\r\n" "CSeq: %d\r\n" "Content-Type: application/sdp\r\n" "%s" "Content-length: %d\r\n\r\n" "%s"; } else if(!strcmp(szName, "SETUP")) { cmdFmt = "%s" "CSeq: %d\r\n" "%s" "%s" "%s" "%s" "\r\n"; } str = (char*)cmdFmt; return str; } long ReadSocket(int sock, char *buf, int len, int timeout ) { long lret ; int iret; fd_set fr; struct timeval tm; tm.tv_sec = timeout; tm.tv_usec = 0; FD_ZERO(&fr); FD_SET(sock,&fr); lret = select(sock+1, &fr, NULL, NULL, &tm); if(FD_ISSET(sock,&fr))//if(lret > 0) { lret = recv(sock, buf, len, 0); if(lret == -1) { printf("recv failed./n"); } else if(lret > 0) { printf("%s\n",buf); } } return lret; } long SendRTSPCmd( int sock, const char *cmd, const char *szparam ) { long lret; int ilen; ilen = strlen(szparam); lret = send(sock, szparam, ilen,0); if(lret == -1) { printf("send %s failed.\n",cmd); } return lret; }
posted on 2015-05-12 16:38 tangxiacun 阅读(560) 评论(1) 编辑 收藏 举报