Qt截屏 X265lib编码 rtsp请求 rtp流发送 (rtsp)实现


rtsp跟http一样都是七层应用层协议.在rtsp协议组中的主要作用是:




1.客户端发送OPTIONS请求:

客户端(ffplay)请求报文:
OPTIONS rtsp://127.0.0.1:1133 RTSP/1.0\r\nCSeq: 1\r\nUser-Agent: Lavf57.83.100\r\n\r\n}$
服务端响应报文:
RTSP/1.0 200 OK\r\nCSeq:"+QString::number(cseq)+"\r\nPublic:DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\r\n\r\n
//服务器返回了服务器支持的方法;
//注意请求和返回都是\r\n\r\n结尾。

2.客户端发送DESCRIBE请求

客户端发送DESCRIBE请求
DESCRIBE rtsp://127.0.0.1:1133 RTSP/1.0\r\nAccept: application/sdp\r\nCSeq: 2\r\nUser-Agent: Lavf57.83.100\r\n\r\n"
服务端响应客户端请求.
"c=IN IP4 127.0.0.1\r\nm=video 0 RTP/AVP 96\r\na=rtpmap:96 H265/90000\r\na=fmtp:96 profile-space=0;profile-id=0;tier-flag=0;level-id=0\r\n\r\n"
//注释:a=rtpmap:96 H265/90000  96表示媒体类型是视频,H265/90000表示h265编码波特率是90000

3.客户端SETUP请求:

客户端setup请求报文
"SETUP rtsp://127.0.0.1:1133 RTSP/1.0\r\nTransport: RTP/AVP/UDP;unicast;client_port=\r\nCSeq: 3\r\nUser-Agent: Lavf57.83.100\r\n\r\n"
//红色为客户端监听的端口号.
服务端响应报文
"RTSP/1.0 200 OK\r\nCSeq:0\r\nTransport: RTP/AVP;unicast;client_port=18174-0;server_port=;ssrc=\r\nSession:\r\n\r\n"
//红色部分为server rtp-rtcp端口号,蓝色部分和绿色部分为会话唯一标识,类似于http的会话保持.

4.客户端发送PLAY请求

客户端PLAY请求报文
"PLAY rtsp://127.0.0.1:1133 RTSP/1.0\r\nRange: npt=0.000-\r\nCSeq: 4\r\nUser-Agent: Lavf57.83.100\r\nSession: Wang_qin_feng18174_0\r\n\r\n"
服务端返回报文
"RTSP/1.0 200 OK\r\nCSeq: 0\r\nSession:Wang_qin_feng10540_0\r\nRTP-Info: url=rtsp://127.0.0.1:1133;seq=9810092;rtptime=0\r\n\r\n"

5.此时ffplay等待server rtp端口号(1130)发送来的数据.
rtsp的实现:
cache.h全局缓存

#ifndef CACHE_H
#define CACHE_H
#include 
#include 
#include "rtp.h"
struct rtp_rtcp_struct{
  int rtp; //客户端端口
  int rtcp; //客户端端口
  QHostAddress ip; //客户端地址
};
extern rtp_rtcp_struct s_s_p;
extern QList client_rtp_rtcp_list;
extern rtp rtp_server;
class cache
{
public:
    cache();
};

#endif // CACHE_H

cache.cpp

#include "cache.h"
#include 

rtp_rtcp_struct s_s_p;
QList client_rtp_rtcp_list;
rtp rtp_server;
cache::cache()
{

}

rtsp.h

#define RTSP_H
#include 
#include 
#include 
class rtsp : public QTcpServer
{
    Q_OBJECT
public:
    explicit rtsp(int rtp,int rtcp,QTcpServer *parent = nullptr);
    void incomingConnection(qintptr socket_number);
signals:
public slots:
private:
    int server_rtp;
    int server_rtcp;
};
//客户端连接
class connect_socket :public QTcpSocket{
    Q_OBJECT
public:
    explicit connect_socket(int rtp,int rtcp,qintptr   socket_number,QTcpSocket *parent=nullptr);
    enum return_error{
        no_error=0,
        no_vision= -1,
        no_rtp_rtcp_port=-2,
    };
signals:
private:
    int router(QStringList head); //请求方法路由
    void send_methon();    //返回支持的方法
    void send_rtp_rtcp_session(); //返回会话端口号等信息
    void send_sdp();    //发送sdp文件
    int verison;
    int client_rtp_port; //client 监听rtp的端口号
    int client_rtcp_port; //client 监听的rtcp的端口号
    int server_rtp_port; 
    int server_rtcp_port;
    int cseq=0;
    QByteArray SSRC="Wang_qin_feng";
public slots:
    void read_data();
};
#endif // RTSP_H

rtsp.cpp

#include "rtsp.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include "cache.h"
rtsp::rtsp(int rtp,int rtcp,QTcpServer *parent) : QTcpServer(parent)
{
    server_rtp=rtp; 
    server_rtcp=rtcp;
}
//新的tcp socket连接时创建一个新的连接.
void rtsp::incomingConnection(qintptr socket_number){
 //new一个新的socket连接
    connect_socket *client_socket=new connect_socket(server_rtp,server_rtcp,socket_number);
 //创建一个线程.
    QThread *client_socket_thread=new QThread();
    client_socket->moveToThread(client_socket_thread);
    client_socket_thread->start();
}
connect_socket::connect_socket(int rtp,int rtcp,qintptr socket_number,QTcpSocket *parent):QTcpSocket (parent){
    this->setSocketDescriptor(socket_number);
    server_rtp_port=rtp; //server rtp_port
    server_rtcp_port=rtcp; //server rtcp_port
//有数据传入是调用槽
    connect(this,SIGNAL(readyRead()),this,SLOT(read_data()));
};
void connect_socket::read_data(){ 
    QString tmp_data;
    tmp_data=this->readAll();
    if(tmp_data.size()>50){ //因为vlc有传空请求来!所有判定下长度
        QStringList request_body=tmp_data.split(" ");
//        qDebug()<peerAddress();
            client_rtp_rtcp_list.append(s_s_p);
                        //返回会话数据
            send_rtp_rtcp_session();
            }else {
                        return error_return;
                    }
    }else if (methon=="PLAY") {
        QString url;
        if(head.size()>=1){
            url=head[1];
        }
        QByteArray data;
        data.append("RTSP/1.0 200 OK\r\nCSeq: "+QString::number(cseq)+"\r\nSession: "+SSRC+"\r\n"+"RTP-Info: url="+url
                    +";seq=9810092;rtptime=0\r\n\r\n");
        this->write(data);
//        qDebug()<waitForBytesWritten(3000);
    }else if (methon=="DESCRIBE") {
            send_sdp();
    }else{
    }
    return  error_return;
};
void connect_socket::send_methon(){ //发送支持的方法
    QByteArray data;
    data.append("RTSP/1.0 200 OK\r\n CSeq:"+QString::number(cseq)+"\r\nPublic:DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\r\n\r\n");
    this->write(data);
    this->waitForBytesWritten(3000);
}
void connect_socket::send_rtp_rtcp_session(){ //发送会话数据
    SSRC+=QString::number(client_rtp_port)+"_"+QString::number(client_rtcp_port);
    QByteArray data;
    data.append("RTSP/1.0 200 OK\r\nCSeq:"+QString::number(cseq)+"\r\nTransport: RTP/AVP;unicast;client_port="+
            QString::number(client_rtp_port)+"-"+QString::number(client_rtcp_port)+";"
            +"server_port="+QString::number(server_rtp_port)+"-"+QString::number(server_rtcp_port)+";"
            +"ssrc="+SSRC+"\r\nSession:"+SSRC+"\r\n\r\n");
//    qDebug()<write(data);
    this->waitForBytesWritten(3000);
}
void connect_socket::send_sdp(){
    QByteArray tmp_data;
    QByteArray data;
    tmp_data.append("c=IN IP4 "+this->peerAddress().toString().split(":")[3]+"\r\nm=video 0 RTP/AVP 96\r\na=rtpmap:96 H265/90000\r\na=fmtp:96 profile-space=0;profile-id=0;tier-flag=0;level-id=0\r\n\r\n");
    data.append( "RTSP/1.0 200 OK\r\nCSeq:"+QString::number(cseq)+"\r\nContent-Type: application/sdp\r\nContent-Length:"
                 +QString::number(tmp_data.size())+"\r\n\r\n");
    data.append(tmp_data);
//    qDebug()<write(data);
    this->waitForBytesWritten(3000);
}

你可能感兴趣的:(Qt截屏 X265lib编码 rtsp请求 rtp流发送 (rtsp)实现)