mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-30 06:42:22 +08:00
tab统一替换为4个空格键:#242
This commit is contained in:
@@ -43,29 +43,29 @@ using namespace mediakit::Client;
|
||||
namespace mediakit {
|
||||
|
||||
enum PlayType {
|
||||
type_play = 0,
|
||||
type_pause,
|
||||
type_seek
|
||||
type_play = 0,
|
||||
type_pause,
|
||||
type_seek
|
||||
};
|
||||
|
||||
RtspPlayer::RtspPlayer(const EventPoller::Ptr &poller) : TcpClient(poller){
|
||||
RtpReceiver::setPoolSize(64);
|
||||
RtpReceiver::setPoolSize(64);
|
||||
}
|
||||
RtspPlayer::~RtspPlayer(void) {
|
||||
DebugL << endl;
|
||||
}
|
||||
void RtspPlayer::teardown(){
|
||||
if (alive()) {
|
||||
sendRtspRequest("TEARDOWN" ,_strContentBase);
|
||||
shutdown(SockException(Err_shutdown,"teardown"));
|
||||
}
|
||||
if (alive()) {
|
||||
sendRtspRequest("TEARDOWN" ,_strContentBase);
|
||||
shutdown(SockException(Err_shutdown,"teardown"));
|
||||
}
|
||||
|
||||
_rtspMd5Nonce.clear();
|
||||
_rtspRealm.clear();
|
||||
_aTrackInfo.clear();
|
||||
_rtspMd5Nonce.clear();
|
||||
_rtspRealm.clear();
|
||||
_aTrackInfo.clear();
|
||||
_strSession.clear();
|
||||
_strContentBase.clear();
|
||||
RtpReceiver::clear();
|
||||
RtpReceiver::clear();
|
||||
|
||||
CLEAR_ARR(_apRtpSock);
|
||||
CLEAR_ARR(_apRtcpSock);
|
||||
@@ -74,65 +74,65 @@ void RtspPlayer::teardown(){
|
||||
CLEAR_ARR(_aui64RtpRecv)
|
||||
CLEAR_ARR(_aui16NowSeq)
|
||||
|
||||
_pPlayTimer.reset();
|
||||
_pPlayTimer.reset();
|
||||
_pRtpTimer.reset();
|
||||
_uiCseq = 1;
|
||||
_onHandshake = nullptr;
|
||||
_uiCseq = 1;
|
||||
_onHandshake = nullptr;
|
||||
}
|
||||
|
||||
void RtspPlayer::play(const string &strUrl){
|
||||
RtspUrl url;
|
||||
if(!url.parse(strUrl)){
|
||||
onPlayResult_l(SockException(Err_other,StrPrinter << "illegal rtsp url:" << strUrl),false);
|
||||
return;
|
||||
}
|
||||
RtspUrl url;
|
||||
if(!url.parse(strUrl)){
|
||||
onPlayResult_l(SockException(Err_other,StrPrinter << "illegal rtsp url:" << strUrl),false);
|
||||
return;
|
||||
}
|
||||
|
||||
teardown();
|
||||
teardown();
|
||||
|
||||
if (url._user.size()) {
|
||||
(*this)[kRtspUser] = url._user;
|
||||
}
|
||||
if (url._passwd.size()) {
|
||||
(*this)[kRtspPwd] = url._passwd;
|
||||
(*this)[kRtspPwdIsMD5] = false;
|
||||
}
|
||||
if (url._user.size()) {
|
||||
(*this)[kRtspUser] = url._user;
|
||||
}
|
||||
if (url._passwd.size()) {
|
||||
(*this)[kRtspPwd] = url._passwd;
|
||||
(*this)[kRtspPwdIsMD5] = false;
|
||||
}
|
||||
|
||||
_strUrl = url._url;
|
||||
_eType = (Rtsp::eRtpType)(int)(*this)[kRtpType];
|
||||
DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _eType;
|
||||
_strUrl = url._url;
|
||||
_eType = (Rtsp::eRtpType)(int)(*this)[kRtpType];
|
||||
DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _eType;
|
||||
|
||||
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
||||
float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
|
||||
_pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return false;
|
||||
}
|
||||
strongSelf->onPlayResult_l(SockException(Err_timeout,"play rtsp timeout"),false);
|
||||
return false;
|
||||
},getPoller()));
|
||||
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
||||
float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
|
||||
_pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return false;
|
||||
}
|
||||
strongSelf->onPlayResult_l(SockException(Err_timeout,"play rtsp timeout"),false);
|
||||
return false;
|
||||
},getPoller()));
|
||||
|
||||
if(!(*this)[kNetAdapter].empty()){
|
||||
setNetAdapter((*this)[kNetAdapter]);
|
||||
}
|
||||
startConnect(url._host, url._port, playTimeOutSec);
|
||||
if(!(*this)[kNetAdapter].empty()){
|
||||
setNetAdapter((*this)[kNetAdapter]);
|
||||
}
|
||||
startConnect(url._host, url._port, playTimeOutSec);
|
||||
}
|
||||
|
||||
void RtspPlayer::onConnect(const SockException &err){
|
||||
if(err.getErrCode() != Err_success) {
|
||||
onPlayResult_l(err,false);
|
||||
return;
|
||||
}
|
||||
if(err.getErrCode() != Err_success) {
|
||||
onPlayResult_l(err,false);
|
||||
return;
|
||||
}
|
||||
|
||||
sendDescribe();
|
||||
sendDescribe();
|
||||
}
|
||||
|
||||
void RtspPlayer::onRecv(const Buffer::Ptr& pBuf) {
|
||||
input(pBuf->data(),pBuf->size());
|
||||
}
|
||||
void RtspPlayer::onErr(const SockException &ex) {
|
||||
//定时器_pPlayTimer为空后表明握手结束了
|
||||
onPlayResult_l(ex,!_pPlayTimer);
|
||||
//定时器_pPlayTimer为空后表明握手结束了
|
||||
onPlayResult_l(ex,!_pPlayTimer);
|
||||
}
|
||||
// from live555
|
||||
bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) {
|
||||
@@ -167,25 +167,25 @@ bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) {
|
||||
return false;
|
||||
}
|
||||
void RtspPlayer::handleResDESCRIBE(const Parser& parser) {
|
||||
string authInfo = parser["WWW-Authenticate"];
|
||||
//发送DESCRIBE命令后的回复
|
||||
if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) {
|
||||
sendDescribe();
|
||||
return;
|
||||
}
|
||||
if(parser.Url() == "302" || parser.Url() == "301"){
|
||||
auto newUrl = parser["Location"];
|
||||
if(newUrl.empty()){
|
||||
throw std::runtime_error("未找到Location字段(跳转url)");
|
||||
}
|
||||
play(newUrl);
|
||||
return;
|
||||
}
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(
|
||||
StrPrinter << "DESCRIBE:" << parser.Url() << " " << parser.Tail() << endl);
|
||||
}
|
||||
_strContentBase = parser["Content-Base"];
|
||||
string authInfo = parser["WWW-Authenticate"];
|
||||
//发送DESCRIBE命令后的回复
|
||||
if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) {
|
||||
sendDescribe();
|
||||
return;
|
||||
}
|
||||
if(parser.Url() == "302" || parser.Url() == "301"){
|
||||
auto newUrl = parser["Location"];
|
||||
if(newUrl.empty()){
|
||||
throw std::runtime_error("未找到Location字段(跳转url)");
|
||||
}
|
||||
play(newUrl);
|
||||
return;
|
||||
}
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(
|
||||
StrPrinter << "DESCRIBE:" << parser.Url() << " " << parser.Tail() << endl);
|
||||
}
|
||||
_strContentBase = parser["Content-Base"];
|
||||
|
||||
if(_strContentBase.empty()){
|
||||
_strContentBase = _strUrl;
|
||||
@@ -194,58 +194,58 @@ void RtspPlayer::handleResDESCRIBE(const Parser& parser) {
|
||||
_strContentBase.pop_back();
|
||||
}
|
||||
|
||||
SdpParser sdpParser(parser.Content());
|
||||
//解析sdp
|
||||
_aTrackInfo = sdpParser.getAvailableTrack();
|
||||
auto title = sdpParser.getTrack(TrackTitle);
|
||||
bool isPlayback = false;
|
||||
if(title && title->_duration ){
|
||||
SdpParser sdpParser(parser.Content());
|
||||
//解析sdp
|
||||
_aTrackInfo = sdpParser.getAvailableTrack();
|
||||
auto title = sdpParser.getTrack(TrackTitle);
|
||||
bool isPlayback = false;
|
||||
if(title && title->_duration ){
|
||||
isPlayback = true;
|
||||
}
|
||||
}
|
||||
|
||||
for(auto &stamp : _stamp){
|
||||
stamp.setPlayBack(isPlayback);
|
||||
stamp.setRelativeStamp(0);
|
||||
}
|
||||
|
||||
if (_aTrackInfo.empty()) {
|
||||
throw std::runtime_error("无有效的Sdp Track");
|
||||
}
|
||||
if (!onCheckSDP(sdpParser.toString())) {
|
||||
throw std::runtime_error("onCheckSDP faied");
|
||||
}
|
||||
if (_aTrackInfo.empty()) {
|
||||
throw std::runtime_error("无有效的Sdp Track");
|
||||
}
|
||||
if (!onCheckSDP(sdpParser.toString())) {
|
||||
throw std::runtime_error("onCheckSDP faied");
|
||||
}
|
||||
|
||||
sendSetup(0);
|
||||
sendSetup(0);
|
||||
}
|
||||
|
||||
//有必要的情况下创建udp端口
|
||||
void RtspPlayer::createUdpSockIfNecessary(int track_idx){
|
||||
auto &rtpSockRef = _apRtpSock[track_idx];
|
||||
auto &rtcpSockRef = _apRtcpSock[track_idx];
|
||||
if(!rtpSockRef){
|
||||
rtpSockRef.reset(new Socket(getPoller()));
|
||||
//rtp随机端口
|
||||
if (!rtpSockRef->bindUdpSock(0, get_local_ip().data())) {
|
||||
rtpSockRef.reset();
|
||||
throw std::runtime_error("open rtp sock failed");
|
||||
}
|
||||
}
|
||||
auto &rtpSockRef = _apRtpSock[track_idx];
|
||||
auto &rtcpSockRef = _apRtcpSock[track_idx];
|
||||
if(!rtpSockRef){
|
||||
rtpSockRef.reset(new Socket(getPoller()));
|
||||
//rtp随机端口
|
||||
if (!rtpSockRef->bindUdpSock(0, get_local_ip().data())) {
|
||||
rtpSockRef.reset();
|
||||
throw std::runtime_error("open rtp sock failed");
|
||||
}
|
||||
}
|
||||
|
||||
if(!rtcpSockRef){
|
||||
rtcpSockRef.reset(new Socket(getPoller()));
|
||||
//rtcp端口为rtp端口+1,目的是为了兼容某些服务器,其实更推荐随机端口
|
||||
if (!rtcpSockRef->bindUdpSock(rtpSockRef->get_local_port() + 1, get_local_ip().data())) {
|
||||
rtcpSockRef.reset();
|
||||
throw std::runtime_error("open rtcp sock failed");
|
||||
}
|
||||
}
|
||||
if(!rtcpSockRef){
|
||||
rtcpSockRef.reset(new Socket(getPoller()));
|
||||
//rtcp端口为rtp端口+1,目的是为了兼容某些服务器,其实更推荐随机端口
|
||||
if (!rtcpSockRef->bindUdpSock(rtpSockRef->get_local_port() + 1, get_local_ip().data())) {
|
||||
rtcpSockRef.reset();
|
||||
throw std::runtime_error("open rtcp sock failed");
|
||||
}
|
||||
}
|
||||
|
||||
if(rtpSockRef->get_local_port() % 2 != 0){
|
||||
//如果rtp端口不是偶数,那么与rtcp端口互换,目的是兼容一些要求严格的服务器
|
||||
Socket::Ptr tmp = rtpSockRef;
|
||||
rtpSockRef = rtcpSockRef;
|
||||
rtcpSockRef = tmp;
|
||||
}
|
||||
if(rtpSockRef->get_local_port() % 2 != 0){
|
||||
//如果rtp端口不是偶数,那么与rtcp端口互换,目的是兼容一些要求严格的服务器
|
||||
Socket::Ptr tmp = rtpSockRef;
|
||||
rtpSockRef = rtcpSockRef;
|
||||
rtcpSockRef = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -253,91 +253,91 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){
|
||||
void RtspPlayer::sendSetup(unsigned int trackIndex) {
|
||||
_onHandshake = std::bind(&RtspPlayer::handleResSETUP,this, placeholders::_1,trackIndex);
|
||||
auto &track = _aTrackInfo[trackIndex];
|
||||
auto baseUrl = _strContentBase + "/" + track->_control_surffix;
|
||||
switch (_eType) {
|
||||
case Rtsp::RTP_TCP: {
|
||||
sendRtspRequest("SETUP",baseUrl,{"Transport",StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1});
|
||||
}
|
||||
break;
|
||||
case Rtsp::RTP_MULTICAST: {
|
||||
sendRtspRequest("SETUP",baseUrl,{"Transport","Transport: RTP/AVP;multicast"});
|
||||
}
|
||||
break;
|
||||
case Rtsp::RTP_UDP: {
|
||||
createUdpSockIfNecessary(trackIndex);
|
||||
sendRtspRequest("SETUP",baseUrl,{"Transport",
|
||||
auto baseUrl = _strContentBase + "/" + track->_control_surffix;
|
||||
switch (_eType) {
|
||||
case Rtsp::RTP_TCP: {
|
||||
sendRtspRequest("SETUP",baseUrl,{"Transport",StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1});
|
||||
}
|
||||
break;
|
||||
case Rtsp::RTP_MULTICAST: {
|
||||
sendRtspRequest("SETUP",baseUrl,{"Transport","Transport: RTP/AVP;multicast"});
|
||||
}
|
||||
break;
|
||||
case Rtsp::RTP_UDP: {
|
||||
createUdpSockIfNecessary(trackIndex);
|
||||
sendRtspRequest("SETUP",baseUrl,{"Transport",
|
||||
StrPrinter << "RTP/AVP;unicast;client_port="
|
||||
<< _apRtpSock[trackIndex]->get_local_port() << "-"
|
||||
<< _apRtcpSock[trackIndex]->get_local_port()});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex) {
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(
|
||||
StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
|
||||
}
|
||||
if (uiTrackIndex == 0) {
|
||||
_strSession = parser["Session"];
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(
|
||||
StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
|
||||
}
|
||||
if (uiTrackIndex == 0) {
|
||||
_strSession = parser["Session"];
|
||||
_strSession.append(";");
|
||||
_strSession = FindField(_strSession.data(), nullptr, ";");
|
||||
}
|
||||
}
|
||||
|
||||
auto strTransport = parser["Transport"];
|
||||
if(strTransport.find("TCP") != string::npos || strTransport.find("interleaved") != string::npos){
|
||||
_eType = Rtsp::RTP_TCP;
|
||||
}else if(strTransport.find("multicast") != string::npos){
|
||||
_eType = Rtsp::RTP_MULTICAST;
|
||||
}else{
|
||||
_eType = Rtsp::RTP_UDP;
|
||||
}
|
||||
auto strTransport = parser["Transport"];
|
||||
if(strTransport.find("TCP") != string::npos || strTransport.find("interleaved") != string::npos){
|
||||
_eType = Rtsp::RTP_TCP;
|
||||
}else if(strTransport.find("multicast") != string::npos){
|
||||
_eType = Rtsp::RTP_MULTICAST;
|
||||
}else{
|
||||
_eType = Rtsp::RTP_UDP;
|
||||
}
|
||||
|
||||
RtspSplitter::enableRecvRtp(_eType == Rtsp::RTP_TCP);
|
||||
RtspSplitter::enableRecvRtp(_eType == Rtsp::RTP_TCP);
|
||||
|
||||
if(_eType == Rtsp::RTP_TCP) {
|
||||
string interleaved = FindField( FindField((strTransport + ";").data(), "interleaved=", ";").data(), NULL, "-");
|
||||
_aTrackInfo[uiTrackIndex]->_interleaved = atoi(interleaved.data());
|
||||
}else{
|
||||
const char *strPos = (_eType == Rtsp::RTP_MULTICAST ? "port=" : "server_port=") ;
|
||||
auto port_str = FindField((strTransport + ";").data(), strPos, ";");
|
||||
uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data());
|
||||
if(_eType == Rtsp::RTP_TCP) {
|
||||
string interleaved = FindField( FindField((strTransport + ";").data(), "interleaved=", ";").data(), NULL, "-");
|
||||
_aTrackInfo[uiTrackIndex]->_interleaved = atoi(interleaved.data());
|
||||
}else{
|
||||
const char *strPos = (_eType == Rtsp::RTP_MULTICAST ? "port=" : "server_port=") ;
|
||||
auto port_str = FindField((strTransport + ";").data(), strPos, ";");
|
||||
uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data());
|
||||
uint16_t rtcp_port = atoi(FindField(port_str.data(), "-",NULL).data());
|
||||
auto &pRtpSockRef = _apRtpSock[uiTrackIndex];
|
||||
auto &pRtcpSockRef = _apRtcpSock[uiTrackIndex];
|
||||
|
||||
if (_eType == Rtsp::RTP_MULTICAST) {
|
||||
//udp组播
|
||||
auto multiAddr = FindField((strTransport + ";").data(), "destination=", ";");
|
||||
if (_eType == Rtsp::RTP_MULTICAST) {
|
||||
//udp组播
|
||||
auto multiAddr = FindField((strTransport + ";").data(), "destination=", ";");
|
||||
pRtpSockRef.reset(new Socket(getPoller()));
|
||||
if (!pRtpSockRef->bindUdpSock(rtp_port, multiAddr.data())) {
|
||||
pRtpSockRef.reset();
|
||||
throw std::runtime_error("open udp sock err");
|
||||
}
|
||||
auto fd = pRtpSockRef->rawFD();
|
||||
if (-1 == SockUtil::joinMultiAddrFilter(fd, multiAddr.data(), get_peer_ip().data(),get_local_ip().data())) {
|
||||
SockUtil::joinMultiAddr(fd, multiAddr.data(),get_local_ip().data());
|
||||
}
|
||||
} else {
|
||||
createUdpSockIfNecessary(uiTrackIndex);
|
||||
//udp单播
|
||||
struct sockaddr_in rtpto;
|
||||
rtpto.sin_port = ntohs(rtp_port);
|
||||
rtpto.sin_family = AF_INET;
|
||||
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
|
||||
pRtpSockRef->setSendPeerAddr((struct sockaddr *)&(rtpto));
|
||||
//发送rtp打洞包
|
||||
pRtpSockRef->send("\xce\xfa\xed\xfe", 4);
|
||||
if (!pRtpSockRef->bindUdpSock(rtp_port, multiAddr.data())) {
|
||||
pRtpSockRef.reset();
|
||||
throw std::runtime_error("open udp sock err");
|
||||
}
|
||||
auto fd = pRtpSockRef->rawFD();
|
||||
if (-1 == SockUtil::joinMultiAddrFilter(fd, multiAddr.data(), get_peer_ip().data(),get_local_ip().data())) {
|
||||
SockUtil::joinMultiAddr(fd, multiAddr.data(),get_local_ip().data());
|
||||
}
|
||||
} else {
|
||||
createUdpSockIfNecessary(uiTrackIndex);
|
||||
//udp单播
|
||||
struct sockaddr_in rtpto;
|
||||
rtpto.sin_port = ntohs(rtp_port);
|
||||
rtpto.sin_family = AF_INET;
|
||||
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
|
||||
pRtpSockRef->setSendPeerAddr((struct sockaddr *)&(rtpto));
|
||||
//发送rtp打洞包
|
||||
pRtpSockRef->send("\xce\xfa\xed\xfe", 4);
|
||||
|
||||
//设置rtcp发送目标,为后续发送rtcp做准备
|
||||
//设置rtcp发送目标,为后续发送rtcp做准备
|
||||
rtpto.sin_port = ntohs(rtcp_port);
|
||||
rtpto.sin_family = AF_INET;
|
||||
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
|
||||
pRtcpSockRef->setSendPeerAddr((struct sockaddr *)&(rtpto));
|
||||
}
|
||||
}
|
||||
|
||||
auto srcIP = inet_addr(get_peer_ip().data());
|
||||
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
||||
@@ -368,98 +368,98 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
|
||||
strongSelf->onRtcpPacket(uiTrackIndex, strongSelf->_aTrackInfo[uiTrackIndex], (unsigned char *) buf->data(), buf->size());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (uiTrackIndex < _aTrackInfo.size() - 1) {
|
||||
//需要继续发送SETUP命令
|
||||
sendSetup(uiTrackIndex + 1);
|
||||
return;
|
||||
}
|
||||
//所有setup命令发送完毕
|
||||
//发送play命令
|
||||
if (uiTrackIndex < _aTrackInfo.size() - 1) {
|
||||
//需要继续发送SETUP命令
|
||||
sendSetup(uiTrackIndex + 1);
|
||||
return;
|
||||
}
|
||||
//所有setup命令发送完毕
|
||||
//发送play命令
|
||||
sendPause(type_play, 0);
|
||||
}
|
||||
|
||||
void RtspPlayer::sendDescribe() {
|
||||
//发送DESCRIBE命令后处理函数:handleResDESCRIBE
|
||||
_onHandshake = std::bind(&RtspPlayer::handleResDESCRIBE,this, placeholders::_1);
|
||||
sendRtspRequest("DESCRIBE",_strUrl,{"Accept","application/sdp"});
|
||||
//发送DESCRIBE命令后处理函数:handleResDESCRIBE
|
||||
_onHandshake = std::bind(&RtspPlayer::handleResDESCRIBE,this, placeholders::_1);
|
||||
sendRtspRequest("DESCRIBE",_strUrl,{"Accept","application/sdp"});
|
||||
}
|
||||
|
||||
void RtspPlayer::sendPause(int type , uint32_t seekMS){
|
||||
_onHandshake = std::bind(&RtspPlayer::handleResPAUSE,this, placeholders::_1,type);
|
||||
//开启或暂停rtsp
|
||||
switch (type){
|
||||
case type_pause:
|
||||
sendRtspRequest("PAUSE", _strContentBase);
|
||||
break;
|
||||
case type_play:
|
||||
sendRtspRequest("PLAY", _strContentBase);
|
||||
break;
|
||||
case type_seek:
|
||||
sendRtspRequest("PLAY", _strContentBase, {"Range",StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << seekMS / 1000.0 << "-"});
|
||||
break;
|
||||
default:
|
||||
WarnL << "unknown type : " << type;
|
||||
_onHandshake = nullptr;
|
||||
break;
|
||||
}
|
||||
_onHandshake = std::bind(&RtspPlayer::handleResPAUSE,this, placeholders::_1,type);
|
||||
//开启或暂停rtsp
|
||||
switch (type){
|
||||
case type_pause:
|
||||
sendRtspRequest("PAUSE", _strContentBase);
|
||||
break;
|
||||
case type_play:
|
||||
sendRtspRequest("PLAY", _strContentBase);
|
||||
break;
|
||||
case type_seek:
|
||||
sendRtspRequest("PLAY", _strContentBase, {"Range",StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << seekMS / 1000.0 << "-"});
|
||||
break;
|
||||
default:
|
||||
WarnL << "unknown type : " << type;
|
||||
_onHandshake = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void RtspPlayer::pause(bool bPause) {
|
||||
sendPause(bPause ? type_pause : type_seek, getProgressMilliSecond());
|
||||
}
|
||||
|
||||
void RtspPlayer::handleResPAUSE(const Parser& parser,int type) {
|
||||
if (parser.Url() != "200") {
|
||||
switch (type) {
|
||||
case type_pause:
|
||||
WarnL << "Pause failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
break;
|
||||
case type_play:
|
||||
WarnL << "Play failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
break;
|
||||
case type_seek:
|
||||
WarnL << "Seek failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (parser.Url() != "200") {
|
||||
switch (type) {
|
||||
case type_pause:
|
||||
WarnL << "Pause failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
break;
|
||||
case type_play:
|
||||
WarnL << "Play failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
break;
|
||||
case type_seek:
|
||||
WarnL << "Seek failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == type_pause) {
|
||||
//暂停成功!
|
||||
_pRtpTimer.reset();
|
||||
return;
|
||||
}
|
||||
if (type == type_pause) {
|
||||
//暂停成功!
|
||||
_pRtpTimer.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
//play或seek成功
|
||||
uint32_t iSeekTo = 0;
|
||||
//修正时间轴
|
||||
auto strRange = parser["Range"];
|
||||
if (strRange.size()) {
|
||||
auto strStart = FindField(strRange.data(), "npt=", "-");
|
||||
if (strStart == "now") {
|
||||
strStart = "0";
|
||||
}
|
||||
iSeekTo = 1000 * atof(strStart.data());
|
||||
DebugL << "seekTo(ms):" << iSeekTo;
|
||||
}
|
||||
//设置相对时间戳
|
||||
_stamp[0].setRelativeStamp(iSeekTo);
|
||||
_stamp[1].setRelativeStamp(iSeekTo);
|
||||
onPlayResult_l(SockException(Err_success, type == type_seek ? "resum rtsp success" : "rtsp play success"), type == type_seek);
|
||||
//play或seek成功
|
||||
uint32_t iSeekTo = 0;
|
||||
//修正时间轴
|
||||
auto strRange = parser["Range"];
|
||||
if (strRange.size()) {
|
||||
auto strStart = FindField(strRange.data(), "npt=", "-");
|
||||
if (strStart == "now") {
|
||||
strStart = "0";
|
||||
}
|
||||
iSeekTo = 1000 * atof(strStart.data());
|
||||
DebugL << "seekTo(ms):" << iSeekTo;
|
||||
}
|
||||
//设置相对时间戳
|
||||
_stamp[0].setRelativeStamp(iSeekTo);
|
||||
_stamp[1].setRelativeStamp(iSeekTo);
|
||||
onPlayResult_l(SockException(Err_success, type == type_seek ? "resum rtsp success" : "rtsp play success"), type == type_seek);
|
||||
}
|
||||
|
||||
void RtspPlayer::onWholeRtspPacket(Parser &parser) {
|
||||
try {
|
||||
decltype(_onHandshake) fun;
|
||||
_onHandshake.swap(fun);
|
||||
decltype(_onHandshake) fun;
|
||||
_onHandshake.swap(fun);
|
||||
if(fun){
|
||||
fun(parser);
|
||||
}
|
||||
parser.Clear();
|
||||
} catch (std::exception &err) {
|
||||
//定时器_pPlayTimer为空后表明握手结束了
|
||||
onPlayResult_l(SockException(Err_other, err.what()),!_pPlayTimer);
|
||||
//定时器_pPlayTimer为空后表明握手结束了
|
||||
onPlayResult_l(SockException(Err_other, err.what()),!_pPlayTimer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -610,39 +610,39 @@ void RtspPlayer::sendReceiverReport(bool overTcp,int iTrackIndex){
|
||||
|
||||
|
||||
void RtspPlayer::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx){
|
||||
//统计丢包率
|
||||
if (_aui16FirstSeq[trackidx] == 0 || rtppt->sequence < _aui16FirstSeq[trackidx]) {
|
||||
_aui16FirstSeq[trackidx] = rtppt->sequence;
|
||||
_aui64RtpRecv[trackidx] = 0;
|
||||
}
|
||||
_aui64RtpRecv[trackidx] ++;
|
||||
_aui16NowSeq[trackidx] = rtppt->sequence;
|
||||
//统计丢包率
|
||||
if (_aui16FirstSeq[trackidx] == 0 || rtppt->sequence < _aui16FirstSeq[trackidx]) {
|
||||
_aui16FirstSeq[trackidx] = rtppt->sequence;
|
||||
_aui64RtpRecv[trackidx] = 0;
|
||||
}
|
||||
_aui64RtpRecv[trackidx] ++;
|
||||
_aui16NowSeq[trackidx] = rtppt->sequence;
|
||||
|
||||
//计算相对时间戳
|
||||
int64_t dts_out;
|
||||
_stamp[trackidx].revise(rtppt->timeStamp,rtppt->timeStamp,dts_out,dts_out);
|
||||
//计算相对时间戳
|
||||
int64_t dts_out;
|
||||
_stamp[trackidx].revise(rtppt->timeStamp,rtppt->timeStamp,dts_out,dts_out);
|
||||
rtppt->timeStamp = dts_out;
|
||||
onRecvRTP_l(rtppt,_aTrackInfo[trackidx]);
|
||||
onRecvRTP_l(rtppt,_aTrackInfo[trackidx]);
|
||||
}
|
||||
float RtspPlayer::getPacketLossRate(TrackType type) const{
|
||||
int iTrackIdx = getTrackIndexByTrackType(type);
|
||||
if(iTrackIdx == -1){
|
||||
uint64_t totalRecv = 0;
|
||||
uint64_t totalSend = 0;
|
||||
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
||||
totalRecv += _aui64RtpRecv[i];
|
||||
totalSend += (_aui16NowSeq[i] - _aui16FirstSeq[i] + 1);
|
||||
}
|
||||
if(totalSend == 0){
|
||||
return 0;
|
||||
}
|
||||
return 1.0 - (double)totalRecv / totalSend;
|
||||
}
|
||||
int iTrackIdx = getTrackIndexByTrackType(type);
|
||||
if(iTrackIdx == -1){
|
||||
uint64_t totalRecv = 0;
|
||||
uint64_t totalSend = 0;
|
||||
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
||||
totalRecv += _aui64RtpRecv[i];
|
||||
totalSend += (_aui16NowSeq[i] - _aui16FirstSeq[i] + 1);
|
||||
}
|
||||
if(totalSend == 0){
|
||||
return 0;
|
||||
}
|
||||
return 1.0 - (double)totalRecv / totalSend;
|
||||
}
|
||||
|
||||
if(_aui16NowSeq[iTrackIdx] - _aui16FirstSeq[iTrackIdx] + 1 == 0){
|
||||
return 0;
|
||||
}
|
||||
return 1.0 - (double)_aui64RtpRecv[iTrackIdx] / (_aui16NowSeq[iTrackIdx] - _aui16FirstSeq[iTrackIdx] + 1);
|
||||
if(_aui16NowSeq[iTrackIdx] - _aui16FirstSeq[iTrackIdx] + 1 == 0){
|
||||
return 0;
|
||||
}
|
||||
return 1.0 - (double)_aui64RtpRecv[iTrackIdx] / (_aui16NowSeq[iTrackIdx] - _aui16FirstSeq[iTrackIdx] + 1);
|
||||
}
|
||||
|
||||
uint32_t RtspPlayer::getProgressMilliSecond() const{
|
||||
@@ -653,70 +653,70 @@ void RtspPlayer::seekToMilliSecond(uint32_t ms) {
|
||||
}
|
||||
|
||||
void RtspPlayer::sendRtspRequest(const string &cmd, const string &url, const std::initializer_list<string> &header) {
|
||||
string key;
|
||||
StrCaseMap header_map;
|
||||
int i = 0;
|
||||
for(auto &val : header){
|
||||
if(++i % 2 == 0){
|
||||
header_map.emplace(key,val);
|
||||
}else{
|
||||
key = val;
|
||||
}
|
||||
}
|
||||
sendRtspRequest(cmd,url,header_map);
|
||||
string key;
|
||||
StrCaseMap header_map;
|
||||
int i = 0;
|
||||
for(auto &val : header){
|
||||
if(++i % 2 == 0){
|
||||
header_map.emplace(key,val);
|
||||
}else{
|
||||
key = val;
|
||||
}
|
||||
}
|
||||
sendRtspRequest(cmd,url,header_map);
|
||||
}
|
||||
void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrCaseMap &header_const) {
|
||||
auto header = header_const;
|
||||
header.emplace("CSeq",StrPrinter << _uiCseq++);
|
||||
header.emplace("User-Agent",SERVER_NAME "(build in " __DATE__ " " __TIME__ ")");
|
||||
auto header = header_const;
|
||||
header.emplace("CSeq",StrPrinter << _uiCseq++);
|
||||
header.emplace("User-Agent",SERVER_NAME "(build in " __DATE__ " " __TIME__ ")");
|
||||
|
||||
if(!_strSession.empty()){
|
||||
header.emplace("Session",_strSession);
|
||||
}
|
||||
if(!_strSession.empty()){
|
||||
header.emplace("Session",_strSession);
|
||||
}
|
||||
|
||||
if(!_rtspRealm.empty() && !(*this)[kRtspUser].empty()){
|
||||
if(!_rtspMd5Nonce.empty()){
|
||||
//MD5认证
|
||||
/*
|
||||
response计算方法如下:
|
||||
RTSP客户端应该使用username + password并计算response如下:
|
||||
(1)当password为MD5编码,则
|
||||
response = md5( password:nonce:md5(public_method:url) );
|
||||
(2)当password为ANSI字符串,则
|
||||
response= md5( md5(username:realm:password):nonce:md5(public_method:url) );
|
||||
*/
|
||||
string encrypted_pwd = (*this)[kRtspPwd];
|
||||
if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
||||
encrypted_pwd = MD5((*this)[kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest();
|
||||
}
|
||||
auto response = MD5( encrypted_pwd + ":" + _rtspMd5Nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
|
||||
_StrPrinter printer;
|
||||
printer << "Digest ";
|
||||
printer << "username=\"" << (*this)[kRtspUser] << "\", ";
|
||||
printer << "realm=\"" << _rtspRealm << "\", ";
|
||||
printer << "nonce=\"" << _rtspMd5Nonce << "\", ";
|
||||
printer << "uri=\"" << url << "\", ";
|
||||
printer << "response=\"" << response << "\"";
|
||||
header.emplace("Authorization",printer);
|
||||
}else if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
||||
//base64认证
|
||||
string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
|
||||
char authStrBase64[1024] = {0};
|
||||
av_base64_encode(authStrBase64,sizeof(authStrBase64),(uint8_t *)authStr.data(),authStr.size());
|
||||
header.emplace("Authorization",StrPrinter << "Basic " << authStrBase64 );
|
||||
}
|
||||
}
|
||||
if(!_rtspRealm.empty() && !(*this)[kRtspUser].empty()){
|
||||
if(!_rtspMd5Nonce.empty()){
|
||||
//MD5认证
|
||||
/*
|
||||
response计算方法如下:
|
||||
RTSP客户端应该使用username + password并计算response如下:
|
||||
(1)当password为MD5编码,则
|
||||
response = md5( password:nonce:md5(public_method:url) );
|
||||
(2)当password为ANSI字符串,则
|
||||
response= md5( md5(username:realm:password):nonce:md5(public_method:url) );
|
||||
*/
|
||||
string encrypted_pwd = (*this)[kRtspPwd];
|
||||
if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
||||
encrypted_pwd = MD5((*this)[kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest();
|
||||
}
|
||||
auto response = MD5( encrypted_pwd + ":" + _rtspMd5Nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
|
||||
_StrPrinter printer;
|
||||
printer << "Digest ";
|
||||
printer << "username=\"" << (*this)[kRtspUser] << "\", ";
|
||||
printer << "realm=\"" << _rtspRealm << "\", ";
|
||||
printer << "nonce=\"" << _rtspMd5Nonce << "\", ";
|
||||
printer << "uri=\"" << url << "\", ";
|
||||
printer << "response=\"" << response << "\"";
|
||||
header.emplace("Authorization",printer);
|
||||
}else if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
||||
//base64认证
|
||||
string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
|
||||
char authStrBase64[1024] = {0};
|
||||
av_base64_encode(authStrBase64,sizeof(authStrBase64),(uint8_t *)authStr.data(),authStr.size());
|
||||
header.emplace("Authorization",StrPrinter << "Basic " << authStrBase64 );
|
||||
}
|
||||
}
|
||||
|
||||
_StrPrinter printer;
|
||||
printer << cmd << " " << url << " RTSP/1.0\r\n";
|
||||
for (auto &pr : header){
|
||||
printer << pr.first << ": " << pr.second << "\r\n";
|
||||
}
|
||||
send(printer << "\r\n");
|
||||
_StrPrinter printer;
|
||||
printer << cmd << " " << url << " RTSP/1.0\r\n";
|
||||
for (auto &pr : header){
|
||||
printer << pr.first << ": " << pr.second << "\r\n";
|
||||
}
|
||||
send(printer << "\r\n");
|
||||
}
|
||||
|
||||
void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &pkt, const SdpTrack::Ptr &track) {
|
||||
_rtpTicker.resetTime();
|
||||
_rtpTicker.resetTime();
|
||||
onRecvRTP(pkt,track);
|
||||
|
||||
int iTrackIndex = getTrackIndexByInterleaved(pkt->interleaved);
|
||||
@@ -740,22 +740,22 @@ void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &pkt, const SdpTrack::Ptr &tra
|
||||
|
||||
}
|
||||
void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshakeCompleted) {
|
||||
WarnL << ex.getErrCode() << " " << ex.what();
|
||||
WarnL << ex.getErrCode() << " " << ex.what();
|
||||
|
||||
if(!ex){
|
||||
//播放成功,恢复rtp接收超时定时器
|
||||
_rtpTicker.resetTime();
|
||||
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
||||
int timeoutMS = (*this)[kMediaTimeoutMS].as<int>();
|
||||
//创建rtp数据接收超时检测定时器
|
||||
_pRtpTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() {
|
||||
//创建rtp数据接收超时检测定时器
|
||||
_pRtpTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() {
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return false;
|
||||
}
|
||||
if(strongSelf->_rtpTicker.elapsedTime()> timeoutMS) {
|
||||
//接收rtp媒体数据包超时
|
||||
strongSelf->onPlayResult_l(SockException(Err_timeout,"receive rtp timeout"), true);
|
||||
//接收rtp媒体数据包超时
|
||||
strongSelf->onPlayResult_l(SockException(Err_timeout,"receive rtp timeout"), true);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -780,27 +780,27 @@ void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshakeComplete
|
||||
}
|
||||
|
||||
int RtspPlayer::getTrackIndexByInterleaved(int interleaved) const{
|
||||
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
||||
if (_aTrackInfo[i]->_interleaved == interleaved) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
if(_aTrackInfo.size() == 1){
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
||||
if (_aTrackInfo[i]->_interleaved == interleaved) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
if(_aTrackInfo.size() == 1){
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int RtspPlayer::getTrackIndexByTrackType(TrackType trackType) const {
|
||||
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
||||
if (_aTrackInfo[i]->_type == trackType) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
if(_aTrackInfo.size() == 1){
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
||||
if (_aTrackInfo[i]->_type == trackType) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
if(_aTrackInfo.size() == 1){
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
Reference in New Issue
Block a user