#include "rtsp.h"
#include <WinSock2.h>
#pragma comment(lib,"Ws2_32.lib")
Rtsp::Rtsp(char * FileName){
memset(mH264FileName, 0, sizeof(mH264FileName));
memcpy(mH264FileName,FileName,strlen(FileName));
mRtp = new Rtp();
}
Rtsp::~Rtsp(){
if(mRtp){
delete mRtp;
}
}
int Rtsp::CreateSocket(int isUDP)
{
int sockfd;
int optval = 1;
WSADATA wsaData = { 0 };
WSAStartup(MAKEWORD(2, 2), &wsaData);
if(isUDP){
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){
printf("%s socket isUDP error return -1 \n",__func__);
return -1;
}
}else{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
printf("%s socket error return -1 \n",__func__);
return -1;
}
}
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval));
return sockfd;
}
int Rtsp::BindSocketAddr(int sockfd, const char* ip, int port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
if(bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0){
printf("%s bind return -1 \n",__func__);
return -1;
}
return 0;
}
int Rtsp::AcceptClient(int sockfd, char* ip, int* port)
{
int clientfd;
int len = 0;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
len = sizeof(addr);
clientfd = accept(sockfd, (struct sockaddr *)&addr, &len);
if(clientfd < 0){
printf("%s accept return -1 \n",__func__);
return -1;
}
strcpy(ip, inet_ntoa(addr.sin_addr));
*port = ntohs(addr.sin_port);
return clientfd;
}
char* Rtsp:: GetLineFromBuf(char* buf, char* line)
{
while(*buf != '\n')
{
*line = *buf;
line++;
buf++;
}
*line = '\n';
line++;
*line = '\0';
buf++;
return buf;
}
int Rtsp::ProcessCmdOptions(char* result, int cseq)
{
sprintf(result, "RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n"
"Public: OPTIONS, DESCRIBE, SETUP, PLAY\r\n"
"\r\n",
cseq);
return 0;
}
int Rtsp::ProcessCmdDescribe(char* result, int cseq, char* url)
{
char sdp[500];
char ip[100];
sscanf(url, "rtsp://%[^:]:", ip);
sprintf(sdp, "v=0\r\n"
"o=- 9%ld 1 IN IP4 %s\r\n"
"t=0 0\r\n"
"a=control:*\r\n"
"m=video 0 RTP/AVP 96\r\n"
"a=rtpmap:96 H264/90000\r\n"
"a=control:track0\r\n",
time(NULL), ip);
sprintf(result, "RTSP/1.0 200 OK\r\nCSeq: %d\r\n"
"Content-Base: %s\r\n"
"Content-type: application/sdp\r\n"
"Content-length: %d\r\n\r\n"
"%s",
cseq,
url,
strlen(sdp),
sdp);
return 0;
}
int Rtsp::ProcessCmdSetup(char* result, int cseq, int clientRtpPort)
{
sprintf(result, "RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n"
"Transport: RTP/AVP;unicast;client_port=%d-%d;server_port=%d-%d;ssrc=66668899;mode=play\r\n"
"Session: 4bbbd85c8e; timeout=600\r\n"
"\r\n",
cseq,
clientRtpPort,
clientRtpPort+1,
SERVER_RTP_PORT,
SERVER_RTCP_PORT);
return 0;
}
int Rtsp::ProcessCmdPlay(char* result, int cseq)
{
sprintf(result, "RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n"
"Range: npt=0.000-\r\n"
"Session: 4bbbd85c8e; timeout=600\r\n\r\n",
cseq);
return 0;
}
int Rtsp::ProcessCmdHeartbeat(char* result, int cseq)
{
sprintf(result, "RTSP/1.0 200 OK\r\n"
"CSeq: %d\r\n"
"Range: npt=0.000-\r\n"
"Heartbeat: 4bbbd85c8e; \r\n\r\n",
cseq);
return 0;
}
void Rtsp::ProcessClient(int clientSockfd, const char* clientIP, int clientPort,
int serverRtpSockfd, int serverRtcpSockfd)
{
char method[40];
char url[100];
char version[40];
int cseq;
char *bufPtr;
char* rBuf = (char *)malloc(BUF_MAX_SIZE);
char* sBuf = (char *)malloc(BUF_MAX_SIZE);
char line[400] = {0};
int heartbeat = 0;
int heartbeatcount = 0;
while(1)
{
int recvLen;
recvLen = recv(clientSockfd, rBuf, BUF_MAX_SIZE, 0);
if(recvLen <= 0){
printf("%s recv goto out; \n",__func__);
goto out;
}
rBuf[recvLen] = '\0';
printf("---------------C->S--------------\n");
printf("%s", rBuf);
/* 解析方法 */
bufPtr = GetLineFromBuf(rBuf, line);
if(sscanf(line, "%s %s %s\r\n", method, url, version) != 3)
{
printf("%s parse err 1\ goto out; \n",__func__);
goto out;
}
/* 解析序列号 */
bufPtr = GetLineFromBuf(bufPtr, line);
if(sscanf(line, "CSeq: %d\r\n", &cseq) != 1)
{
printf("%s parse err 2\ goto out; \n",__func__);
goto out;
}
/* 如果是SETUP,那么就再解析client_port */
if(!strcmp(method, "SETUP"))
{
while(1)
{
bufPtr = GetLineFromBuf(bufPtr, line);
if(!strncmp(line, "Transport:", strlen("Transport:")))
{
//解析客户端使用的rtp和rtcp端口号,后面服务端就在在两个端口发送rtp和rtcp包数据。
sscanf(line, "Transport: RTP/AVP;unicast;client_port=%d-%d\r\n", &clientRtpPort, &clientRtcpPort);
break;
}
}
}
if(!strcmp(method, "OPTIONS"))
{
if(ProcessCmdOptions(sBuf, cseq))
{
printf("%s failed to Process options \n",__func__);
goto out;
}
}
else if(!strcmp(method, "DESCRIBE"))
{
if(ProcessCmdDescribe(sBuf, cseq, url))
{
printf("%s failed to Process describe \n",__func__);
goto out;
}
}
else if(!strcmp(method, "SETUP"))
{
if(ProcessCmdSetup(sBuf, cseq, clientRtpPort))
{
printf("%s failed to Process setup \n",__func__);
goto out;
}
}
else if(!strcmp(method, "PLAY"))
{
if(ProcessCmdPlay(sBuf, cseq))
{
printf("%s failed to Process play \n",__func__);
goto out;
}
}
else
{
goto out;
printf("%s else goto out; \n",__func__);
}
printf("---------------S->C--------------\n");
printf("%s", sBuf);
send(clientSockfd, sBuf, strlen(sBuf), 0);
/* 开始播放,发送RTP包 */
if(!strcmp(method, "PLAY"))
{
int frameSize, startCode;
char* frame = (char *)malloc(RTP_PACKET_SIZE);
struct RtpPacket* rtpPacket = (struct RtpPacket*)malloc(RTP_PACKET_SIZE);
FILE *fd = fopen(mH264FileName, "r");
assert(fd > 0);
mRtp->RtpHeaderInit(rtpPacket, 0, 0, 0, RTP_VESION, RTP_PAYLOAD_TYPE_H264, 0,
0, 0, 0x66668899);
printf("start play6666.....................................\n");
printf("client ip:%s\n", clientIP);
printf("client port:%d\n", clientRtpPort);
while (1)
{
frameSize = mRtp->GetOneFrameFromH264File(fd, frame, RTP_PACKET_SIZE);
if(frameSize < 0)
{
printf("%s if(frameSize < 0) break \n",__func__);
break;
}
if(mRtp->IsStartCode3(frame))
startCode = 3;
else
没有合适的资源?快使用搜索试试~ 我知道了~
RTSP支持UDP发送数据
共34个文件
tlog:6个
cpp:4个
ipch:4个
需积分: 5 1 下载量 157 浏览量
2023-05-23
18:03:03
上传
评论
收藏 38.87MB RAR 举报
温馨提示
支持RTSP协议,使用UDP发送数据,减少研究时间,直接使用。
资源推荐
资源详情
资源评论
收起资源包目录
RTSP-UDP.rar (34个子文件)
ConsoleApplication1
.vs
ConsoleApplication1
v15
Browse.VC.db 36.11MB
.suo 53KB
ipch
AutoPCH
2e3c37b5ee56b150
CONSOLEAPPLICATION1.ipch 29.19MB
49cb75e751c13f90
CORECRT_IO.ipch 1.31MB
20eb9c537b8dba3a
RTP.ipch 41.69MB
c30626fc94093bb1
RTSP.ipch 41.94MB
ConsoleApplication1
rtsp.h 1KB
ConsoleApplication1.vcxproj 8KB
rtsp.cpp 10KB
main.cpp 193B
ConsoleApplication1.vcxproj.user 165B
test.h264 10.11MB
ConsoleApplication1.vcxproj.filters 1KB
Debug
vc141.pdb 452KB
rtsp.obj 66KB
ConsoleApplication1.log 3KB
vc141.idb 507KB
ConsoleA.52C946DC.tlog
CL.write.1.tlog 3KB
ConsoleApplication1.lastbuildstate 203B
CL.command.1.tlog 2KB
link.command.1.tlog 2KB
link.read.1.tlog 4KB
link.write.1.tlog 766B
CL.read.1.tlog 72KB
rtp.obj 38KB
ConsoleApplication1.Build.CppClean.log 1KB
ConsoleApplication1.obj 56KB
rtp.cpp 7KB
rtp.h 3KB
ConsoleApplication1.cpp 1KB
ConsoleApplication1.sln 1KB
Debug
ConsoleApplication1.ilk 639KB
ConsoleApplication1.exe 66KB
ConsoleApplication1.pdb 812KB
共 34 条
- 1
资源评论
mede123
- 粉丝: 4
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功