2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2018-07-02 15:43:37 +08:00
|
|
|
|
* Copyright (c) 2018 huohuo <913481084@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <set>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <algorithm>
|
2018-12-17 13:48:19 +08:00
|
|
|
|
#include <iomanip>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "RtspPlayer.h"
|
2018-07-02 15:43:37 +08:00
|
|
|
|
#include "Util/MD5.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/mini.h"
|
2017-08-09 18:39:30 +08:00
|
|
|
|
#include "Util/util.h"
|
2018-09-20 15:43:49 +08:00
|
|
|
|
#include "Util/base64.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Network/sockutil.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2019-03-27 18:56:49 +08:00
|
|
|
|
using namespace mediakit::Client;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-04-01 10:16:15 +08:00
|
|
|
|
RtspPlayer::RtspPlayer(const EventPoller::Ptr &poller) : TcpClient(poller){
|
2018-12-14 17:46:12 +08:00
|
|
|
|
RtpReceiver::setPoolSize(64);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
RtspPlayer::~RtspPlayer(void) {
|
|
|
|
|
DebugL<<endl;
|
|
|
|
|
}
|
|
|
|
|
void RtspPlayer::teardown(){
|
|
|
|
|
if (alive()) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
sendRtspRequest("TEARDOWN" ,_strContentBase);
|
2019-05-29 18:08:50 +08:00
|
|
|
|
shutdown(SockException(Err_shutdown,"teardown"));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_rtspMd5Nonce.clear();
|
|
|
|
|
_rtspRealm.clear();
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2018-10-26 09:56:29 +08:00
|
|
|
|
_aTrackInfo.clear();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strSession.clear();
|
|
|
|
|
_strContentBase.clear();
|
2018-12-14 17:46:12 +08:00
|
|
|
|
RtpReceiver::clear();
|
|
|
|
|
|
2019-05-08 15:08:57 +08:00
|
|
|
|
CLEAR_ARR(_apRtpSock);
|
|
|
|
|
CLEAR_ARR(_apRtcpSock);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
CLEAR_ARR(_aui16FirstSeq)
|
|
|
|
|
CLEAR_ARR(_aui64RtpRecv)
|
|
|
|
|
CLEAR_ARR(_aui64RtpRecv)
|
|
|
|
|
CLEAR_ARR(_aui16NowSeq)
|
2018-12-14 17:46:12 +08:00
|
|
|
|
CLEAR_ARR(_aiFistStamp);
|
|
|
|
|
CLEAR_ARR(_aiNowStamp);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
|
|
|
|
|
_pPlayTimer.reset();
|
|
|
|
|
_pRtpTimer.reset();
|
2018-10-26 14:12:16 +08:00
|
|
|
|
_iSeekTo = 0;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
_uiCseq = 1;
|
|
|
|
|
_onHandshake = nullptr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-08-03 13:55:46 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
void RtspPlayer::play(const string &strUrl){
|
|
|
|
|
auto userAndPwd = FindField(strUrl.data(),"://","@");
|
|
|
|
|
Rtsp::eRtpType eType = (Rtsp::eRtpType)(int)(*this)[kRtpType];
|
2017-08-03 13:55:46 +08:00
|
|
|
|
if(userAndPwd.empty()){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
play(strUrl,"","",eType);
|
2017-08-03 13:55:46 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto suffix = FindField(strUrl.data(),"@",nullptr);
|
2017-08-03 13:55:46 +08:00
|
|
|
|
auto url = StrPrinter << "rtsp://" << suffix << endl;
|
|
|
|
|
if(userAndPwd.find(":") == string::npos){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
play(url,userAndPwd,"",eType);
|
2017-08-03 13:55:46 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto user = FindField(userAndPwd.data(),nullptr,":");
|
|
|
|
|
auto pwd = FindField(userAndPwd.data(),":",nullptr);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
play(url,user,pwd,eType);
|
2017-08-03 13:55:46 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//播放,指定是否走rtp over tcp
|
2019-03-27 18:41:52 +08:00
|
|
|
|
void RtspPlayer::play(const string &strUrl, const string &strUser, const string &strPwd, Rtsp::eRtpType eType ) {
|
2017-08-03 13:55:46 +08:00
|
|
|
|
DebugL << strUrl << " "
|
2019-03-27 18:41:52 +08:00
|
|
|
|
<< (strUser.size() ? strUser : "null") << " "
|
|
|
|
|
<< (strPwd.size() ? strPwd:"null") << " "
|
2017-08-03 13:55:46 +08:00
|
|
|
|
<< eType;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
teardown();
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(strUser.size()){
|
2018-07-02 15:43:37 +08:00
|
|
|
|
(*this)[kRtspUser] = strUser;
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(strPwd.size()){
|
2018-07-02 15:43:37 +08:00
|
|
|
|
(*this)[kRtspPwd] = strPwd;
|
|
|
|
|
(*this)[kRtspPwdIsMD5] = false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_eType = eType;
|
2018-10-30 10:31:27 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto ip = FindField(strUrl.data(), "://", "/");
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (!ip.size()) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
ip = FindField(strUrl.data(), "://", NULL);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto port = atoi(FindField(ip.data(), ":", NULL).data());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (port <= 0) {
|
|
|
|
|
//rtsp 默认端口554
|
|
|
|
|
port = 554;
|
|
|
|
|
} else {
|
|
|
|
|
//服务器域名
|
2019-03-27 18:41:52 +08:00
|
|
|
|
ip = FindField(ip.data(), NULL, ":");
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strUrl = strUrl;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
2019-03-27 18:56:49 +08:00
|
|
|
|
float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0;
|
2019-03-01 14:23:28 +08:00
|
|
|
|
_pPlayTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto strongSelf=weakSelf.lock();
|
|
|
|
|
if(!strongSelf) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
strongSelf->onPlayResult_l(SockException(Err_timeout,"play rtsp timeout"));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return false;
|
2019-01-30 17:00:28 +08:00
|
|
|
|
},getPoller()));
|
2019-03-01 14:23:28 +08:00
|
|
|
|
|
2019-03-27 18:56:49 +08:00
|
|
|
|
if(!(*this)[kNetAdapter].empty()){
|
|
|
|
|
setNetAdapter((*this)[kNetAdapter]);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
startConnect(ip, port , playTimeOutSec);
|
2019-03-01 14:23:28 +08:00
|
|
|
|
}
|
|
|
|
|
void RtspPlayer::onConnect(const SockException &err){
|
|
|
|
|
if(err.getErrCode()!=Err_success) {
|
|
|
|
|
onPlayResult_l(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendDescribe();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 15:36:51 +08:00
|
|
|
|
void RtspPlayer::onRecv(const Buffer::Ptr& pBuf) {
|
2018-10-30 10:31:27 +08:00
|
|
|
|
input(pBuf->data(),pBuf->size());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
void RtspPlayer::onErr(const SockException &ex) {
|
2019-03-28 11:52:07 +08:00
|
|
|
|
onPlayResult_l(ex);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
// from live555
|
|
|
|
|
bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(!_rtspRealm.empty()){
|
2018-07-02 15:43:37 +08:00
|
|
|
|
//已经认证过了
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 22:05:09 +08:00
|
|
|
|
char *realm = new char[paramsStr.size()];
|
|
|
|
|
char *nonce = new char[paramsStr.size()];
|
|
|
|
|
char *stale = new char[paramsStr.size()];
|
|
|
|
|
onceToken token(nullptr,[&](){
|
|
|
|
|
delete[] realm;
|
|
|
|
|
delete[] nonce;
|
|
|
|
|
delete[] stale;
|
|
|
|
|
});
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
|
|
|
|
if (sscanf(paramsStr.data(), "Digest realm=\"%[^\"]\", nonce=\"%[^\"]\", stale=%[a-zA-Z]", realm, nonce, stale) == 3) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_rtspRealm = (const char *)realm;
|
|
|
|
|
_rtspMd5Nonce = (const char *)nonce;
|
2018-07-02 15:43:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (sscanf(paramsStr.data(), "Digest realm=\"%[^\"]\", nonce=\"%[^\"]\"", realm, nonce) == 2) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_rtspRealm = (const char *)realm;
|
|
|
|
|
_rtspMd5Nonce = (const char *)nonce;
|
2018-07-02 15:43:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (sscanf(paramsStr.data(), "Basic realm=\"%[^\"]\"", realm) == 1) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_rtspRealm = (const char *)realm;
|
2018-07-02 15:43:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
void RtspPlayer::handleResDESCRIBE(const Parser& parser) {
|
|
|
|
|
string authInfo = parser["WWW-Authenticate"];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//发送DESCRIBE命令后的回复
|
2018-07-02 15:43:37 +08:00
|
|
|
|
if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) {
|
|
|
|
|
sendDescribe();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-01 09:53:58 +08:00
|
|
|
|
if(parser.Url() == "302" || parser.Url() == "301"){
|
2019-01-18 11:27:51 +08:00
|
|
|
|
auto newUrl = parser["Location"];
|
|
|
|
|
if(newUrl.empty()){
|
|
|
|
|
throw std::runtime_error("未找到Location字段(跳转url)");
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
play(newUrl);
|
2019-01-18 11:27:51 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (parser.Url() != "200") {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
StrPrinter << "DESCRIBE:" << parser.Url() << " " << parser.Tail() << endl);
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strContentBase = parser["Content-Base"];
|
2018-03-20 11:28:13 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(_strContentBase.empty()){
|
|
|
|
|
_strContentBase = _strUrl;
|
2018-03-20 11:28:13 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strContentBase.back() == '/') {
|
|
|
|
|
_strContentBase.pop_back();
|
2018-03-20 11:28:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:05:35 +08:00
|
|
|
|
SdpParser sdpParser(parser.Content());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//解析sdp
|
2019-08-22 16:05:35 +08:00
|
|
|
|
_aTrackInfo = sdpParser.getAvailableTrack();
|
2018-10-26 09:56:29 +08:00
|
|
|
|
|
|
|
|
|
if (_aTrackInfo.empty()) {
|
2018-10-26 10:12:37 +08:00
|
|
|
|
throw std::runtime_error("无有效的Sdp Track");
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-08-22 16:05:35 +08:00
|
|
|
|
if (!onCheckSDP(sdpParser.toString())) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
throw std::runtime_error("onCheckSDP faied");
|
|
|
|
|
}
|
2018-10-26 09:56:29 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
sendSetup(0);
|
|
|
|
|
}
|
2019-07-11 12:12:33 +08:00
|
|
|
|
|
2019-07-11 12:15:13 +08:00
|
|
|
|
//有必要的情况下创建udp端口
|
2019-07-11 12:12:33 +08:00
|
|
|
|
void RtspPlayer::createUdpSockIfNecessary(int track_idx){
|
|
|
|
|
auto &rtpSockRef = _apRtpSock[track_idx];
|
|
|
|
|
auto &rtcpSockRef = _apRtcpSock[track_idx];
|
|
|
|
|
if(!rtpSockRef){
|
|
|
|
|
rtpSockRef.reset(new Socket(getPoller()));
|
2019-07-11 12:15:13 +08:00
|
|
|
|
//rtp随机端口
|
2019-07-11 12:12:33 +08:00
|
|
|
|
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()));
|
2019-07-11 12:15:13 +08:00
|
|
|
|
//rtcp端口为rtp端口+1,目的是为了兼容某些服务器,其实更推荐随机端口
|
2019-07-11 12:12:33 +08:00
|
|
|
|
if (!rtcpSockRef->bindUdpSock(rtpSockRef->get_local_port() + 1, get_local_ip().data())) {
|
|
|
|
|
rtcpSockRef.reset();
|
|
|
|
|
throw std::runtime_error("open rtcp sock failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//发送SETUP命令
|
2019-03-28 11:52:07 +08:00
|
|
|
|
void RtspPlayer::sendSetup(unsigned int trackIndex) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_onHandshake = std::bind(&RtspPlayer::handleResSETUP,this, placeholders::_1,trackIndex);
|
|
|
|
|
auto &track = _aTrackInfo[trackIndex];
|
2018-10-26 09:56:29 +08:00
|
|
|
|
auto baseUrl = _strContentBase + "/" + track->_control_surffix;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
switch (_eType) {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_TCP: {
|
2019-03-28 11:52:07 +08:00
|
|
|
|
sendRtspRequest("SETUP",baseUrl,{"Transport",StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1});
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-03-28 11:52:07 +08:00
|
|
|
|
break;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_MULTICAST: {
|
2019-03-28 11:52:07 +08:00
|
|
|
|
sendRtspRequest("SETUP",baseUrl,{"Transport","Transport: RTP/AVP;multicast"});
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
2019-03-28 11:52:07 +08:00
|
|
|
|
break;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
case Rtsp::RTP_UDP: {
|
2019-07-11 12:12:33 +08:00
|
|
|
|
createUdpSockIfNecessary(trackIndex);
|
2019-05-08 15:08:57 +08:00
|
|
|
|
sendRtspRequest("SETUP",baseUrl,{"Transport",
|
|
|
|
|
StrPrinter << "RTP/AVP;unicast;client_port="
|
|
|
|
|
<< _apRtpSock[trackIndex]->get_local_port() << "-"
|
|
|
|
|
<< _apRtcpSock[trackIndex]->get_local_port()});
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
2019-03-28 11:52:07 +08:00
|
|
|
|
break;
|
2018-07-02 15:43:37 +08:00
|
|
|
|
default:
|
2019-03-28 11:52:07 +08:00
|
|
|
|
break;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 15:43:37 +08:00
|
|
|
|
void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (parser.Url() != "200") {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
|
|
|
|
|
}
|
|
|
|
|
if (uiTrackIndex == 0) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strSession = parser["Session"];
|
|
|
|
|
_strSession.append(";");
|
|
|
|
|
_strSession = FindField(_strSession.data(), nullptr, ";");
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto strTransport = parser["Transport"];
|
2019-07-11 11:53:11 +08:00
|
|
|
|
if(strTransport.find("TCP") != string::npos || strTransport.find("interleaved") != string::npos){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_eType = Rtsp::RTP_TCP;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else if(strTransport.find("multicast") != string::npos){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_eType = Rtsp::RTP_MULTICAST;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else{
|
2019-03-27 18:41:52 +08:00
|
|
|
|
_eType = Rtsp::RTP_UDP;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
RtspSplitter::enableRecvRtp(_eType == Rtsp::RTP_TCP);
|
2018-10-30 10:31:27 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(_eType == Rtsp::RTP_TCP) {
|
|
|
|
|
string interleaved = FindField( FindField((strTransport + ";").data(), "interleaved=", ";").data(), NULL, "-");
|
|
|
|
|
_aTrackInfo[uiTrackIndex]->_interleaved = atoi(interleaved.data());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else{
|
2019-03-27 18:41:52 +08:00
|
|
|
|
const char *strPos = (_eType == Rtsp::RTP_MULTICAST ? "port=" : "server_port=") ;
|
|
|
|
|
auto port_str = FindField((strTransport + ";").data(), strPos, ";");
|
2019-05-08 15:08:57 +08:00
|
|
|
|
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];
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if (_eType == Rtsp::RTP_MULTICAST) {
|
2019-05-08 15:08:57 +08:00
|
|
|
|
//udp组播
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto multiAddr = FindField((strTransport + ";").data(), "destination=", ";");
|
2019-05-31 18:01:38 +08:00
|
|
|
|
pRtpSockRef.reset(new Socket(getPoller()));
|
2019-05-20 09:43:13 +08:00
|
|
|
|
if (!pRtpSockRef->bindUdpSock(rtp_port, multiAddr.data())) {
|
2019-05-08 15:08:57 +08:00
|
|
|
|
pRtpSockRef.reset();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
throw std::runtime_error("open udp sock err");
|
|
|
|
|
}
|
2019-05-08 15:08:57 +08:00
|
|
|
|
auto fd = pRtpSockRef->rawFD();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
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 {
|
2019-07-11 12:12:33 +08:00
|
|
|
|
createUdpSockIfNecessary(uiTrackIndex);
|
|
|
|
|
//udp单播
|
2017-04-01 16:35:56 +08:00
|
|
|
|
struct sockaddr_in rtpto;
|
2019-05-08 15:08:57 +08:00
|
|
|
|
rtpto.sin_port = ntohs(rtp_port);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
rtpto.sin_family = AF_INET;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
|
2019-05-08 15:08:57 +08:00
|
|
|
|
pRtpSockRef->setSendPeerAddr((struct sockaddr *)&(rtpto));
|
|
|
|
|
//发送rtp打洞包
|
|
|
|
|
pRtpSockRef->send("\xce\xfa\xed\xfe", 4);
|
|
|
|
|
|
|
|
|
|
//设置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));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-05-08 15:08:57 +08:00
|
|
|
|
|
|
|
|
|
auto srcIP = inet_addr(get_peer_ip().data());
|
|
|
|
|
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
|
|
|
|
//设置rtp over udp接收回调处理函数
|
2019-06-11 12:28:48 +08:00
|
|
|
|
pRtpSockRef->setOnRead([srcIP, uiTrackIndex, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) {
|
2019-05-08 15:08:57 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (((struct sockaddr_in *) addr)->sin_addr.s_addr != srcIP) {
|
|
|
|
|
WarnL << "收到其他地址的rtp数据:" << inet_ntoa(((struct sockaddr_in *) addr)->sin_addr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strongSelf->handleOneRtp(uiTrackIndex, strongSelf->_aTrackInfo[uiTrackIndex], (unsigned char *) buf->data(), buf->size());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(pRtcpSockRef) {
|
|
|
|
|
//设置rtcp over udp接收回调处理函数
|
2019-06-11 12:28:48 +08:00
|
|
|
|
pRtcpSockRef->setOnRead([srcIP, uiTrackIndex, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) {
|
2019-05-08 15:08:57 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (((struct sockaddr_in *) addr)->sin_addr.s_addr != srcIP) {
|
|
|
|
|
WarnL << "收到其他地址的rtcp数据:" << inet_ntoa(((struct sockaddr_in *) addr)->sin_addr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-09 10:49:50 +08:00
|
|
|
|
strongSelf->onRtcpPacket(uiTrackIndex, strongSelf->_aTrackInfo[uiTrackIndex],
|
|
|
|
|
(unsigned char *) buf->data(), buf->size());
|
2019-05-08 15:08:57 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 09:56:29 +08:00
|
|
|
|
if (uiTrackIndex < _aTrackInfo.size() - 1) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//需要继续发送SETUP命令
|
|
|
|
|
sendSetup(uiTrackIndex + 1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-08 15:08:57 +08:00
|
|
|
|
//所有setup命令发送完毕
|
|
|
|
|
//发送play命令
|
2017-04-01 16:35:56 +08:00
|
|
|
|
pause(false);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 11:52:07 +08:00
|
|
|
|
void RtspPlayer::sendOptions() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_onHandshake = [](const Parser& parser){
|
2018-10-30 10:31:27 +08:00
|
|
|
|
// DebugL << "options response";
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
2019-03-28 11:52:07 +08:00
|
|
|
|
sendRtspRequest("OPTIONS",_strContentBase);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2019-03-28 11:52:07 +08:00
|
|
|
|
void RtspPlayer::sendDescribe() {
|
2018-07-02 15:43:37 +08:00
|
|
|
|
//发送DESCRIBE命令后处理函数:handleResDESCRIBE
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_onHandshake = std::bind(&RtspPlayer::handleResDESCRIBE,this, placeholders::_1);
|
2019-03-28 11:52:07 +08:00
|
|
|
|
sendRtspRequest("DESCRIBE",_strUrl,{"Accept","application/sdp"});
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-28 11:52:07 +08:00
|
|
|
|
void RtspPlayer::sendPause(bool bPause,uint32_t seekMS){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if(!bPause){
|
|
|
|
|
//修改时间轴
|
2018-10-26 14:48:09 +08:00
|
|
|
|
int iTimeInc = seekMS - getProgressMilliSecond();
|
2018-10-26 09:56:29 +08:00
|
|
|
|
for(unsigned int i = 0 ;i < _aTrackInfo.size() ;i++){
|
2018-10-26 14:12:16 +08:00
|
|
|
|
_aiFistStamp[i] = _aiNowStamp[i] + iTimeInc;
|
|
|
|
|
_aiNowStamp[i] = _aiFistStamp[i];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
_iSeekTo = seekMS;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
|
|
|
|
//开启或暂停rtsp
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_onHandshake = std::bind(&RtspPlayer::handleResPAUSE,this, placeholders::_1,bPause);
|
2019-03-28 11:52:07 +08:00
|
|
|
|
sendRtspRequest(bPause ? "PAUSE" : "PLAY",
|
|
|
|
|
_strContentBase,
|
|
|
|
|
{"Range",StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << seekMS / 1000.0 << "-"});
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
void RtspPlayer::pause(bool bPause) {
|
2018-10-26 14:12:16 +08:00
|
|
|
|
sendPause(bPause, getProgressMilliSecond());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 15:43:37 +08:00
|
|
|
|
void RtspPlayer::handleResPAUSE(const Parser& parser, bool bPause) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (parser.Url() != "200") {
|
|
|
|
|
WarnL <<(bPause ? "Pause" : "Play") << " failed:" << parser.Url() << " " << parser.Tail() << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!bPause) {
|
|
|
|
|
//修正时间轴
|
|
|
|
|
auto strRange = parser["Range"];
|
|
|
|
|
if (strRange.size()) {
|
|
|
|
|
auto strStart = FindField(strRange.data(), "npt=", "-");
|
|
|
|
|
if (strStart == "now") {
|
|
|
|
|
strStart = "0";
|
|
|
|
|
}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
_iSeekTo = 1000 * atof(strStart.data());
|
|
|
|
|
DebugL << "seekTo(ms):" << _iSeekTo ;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
auto strRtpInfo = parser["RTP-Info"];
|
|
|
|
|
if (strRtpInfo.size()) {
|
|
|
|
|
strRtpInfo.append(",");
|
2018-02-02 18:06:08 +08:00
|
|
|
|
vector<string> vec = split(strRtpInfo, ",");
|
2017-04-01 16:35:56 +08:00
|
|
|
|
for(auto &strTrack : vec){
|
2018-02-02 18:06:08 +08:00
|
|
|
|
strTrack.append(";");
|
2018-05-18 18:48:17 +08:00
|
|
|
|
auto strControlSuffix = strTrack.substr(1 + strTrack.rfind('/'),strTrack.find(';') - strTrack.rfind('/') - 1);
|
2018-02-02 18:06:08 +08:00
|
|
|
|
auto strRtpTime = FindField(strTrack.data(), "rtptime=", ";");
|
2018-10-26 14:12:16 +08:00
|
|
|
|
auto idx = getTrackIndexByControlSuffix(strControlSuffix);
|
2018-10-30 17:11:36 +08:00
|
|
|
|
if(idx != -1){
|
|
|
|
|
_aiFistStamp[idx] = atoll(strRtpTime.data()) * 1000 / _aTrackInfo[idx]->_samplerate;
|
|
|
|
|
_aiNowStamp[idx] = _aiFistStamp[idx];
|
|
|
|
|
DebugL << "rtptime(ms):" << strControlSuffix <<" " << strRtpTime;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
onPlayResult_l(SockException(Err_success, "rtsp play success"));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} else {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_pRtpTimer.reset();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:31:27 +08:00
|
|
|
|
void RtspPlayer::onWholeRtspPacket(Parser &parser) {
|
|
|
|
|
try {
|
|
|
|
|
decltype(_onHandshake) fun;
|
|
|
|
|
_onHandshake.swap(fun);
|
|
|
|
|
if(fun){
|
|
|
|
|
fun(parser);
|
|
|
|
|
}
|
|
|
|
|
parser.Clear();
|
|
|
|
|
} catch (std::exception &err) {
|
|
|
|
|
SockException ex(Err_other, err.what());
|
|
|
|
|
onPlayResult_l(ex);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 10:31:27 +08:00
|
|
|
|
void RtspPlayer::onRtpPacket(const char *data, uint64_t len) {
|
|
|
|
|
int trackIdx = -1;
|
|
|
|
|
uint8_t interleaved = data[1];
|
2019-05-08 15:08:57 +08:00
|
|
|
|
if(interleaved %2 == 0){
|
2018-10-30 10:31:27 +08:00
|
|
|
|
trackIdx = getTrackIndexByInterleaved(interleaved);
|
2019-05-08 15:08:57 +08:00
|
|
|
|
if (trackIdx != -1) {
|
|
|
|
|
handleOneRtp(trackIdx,_aTrackInfo[trackIdx],(unsigned char *)data + 4, len - 4);
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
trackIdx = getTrackIndexByInterleaved(interleaved - 1);
|
|
|
|
|
if (trackIdx != -1) {
|
2019-05-09 10:49:50 +08:00
|
|
|
|
onRtcpPacket(trackIdx, _aTrackInfo[trackIdx], (unsigned char *) data + 4, len - 4);
|
2019-05-08 15:08:57 +08:00
|
|
|
|
}
|
2018-10-30 10:31:27 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 10:49:50 +08:00
|
|
|
|
void RtspPlayer::onRtcpPacket(int iTrackidx, SdpTrack::Ptr &track, unsigned char *pucData, unsigned int uiLen){
|
2019-05-08 15:08:57 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 13:35:54 +08:00
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
//改代码提取自FFmpeg,参考之
|
|
|
|
|
// Receiver Report
|
|
|
|
|
avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
|
|
|
|
|
avio_w8(pb, RTCP_RR);
|
|
|
|
|
avio_wb16(pb, 7); /* length in words - 1 */
|
|
|
|
|
// our own SSRC: we use the server's SSRC + 1 to avoid conflicts
|
|
|
|
|
avio_wb32(pb, s->ssrc + 1);
|
|
|
|
|
avio_wb32(pb, s->ssrc); // server SSRC
|
|
|
|
|
// some placeholders we should really fill...
|
|
|
|
|
// RFC 1889/p64
|
|
|
|
|
extended_max = stats->cycles + stats->max_seq;
|
|
|
|
|
expected = extended_max - stats->base_seq;
|
|
|
|
|
lost = expected - stats->received;
|
|
|
|
|
lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
|
|
|
|
|
expected_interval = expected - stats->expected_prior;
|
|
|
|
|
stats->expected_prior = expected;
|
|
|
|
|
received_interval = stats->received - stats->received_prior;
|
|
|
|
|
stats->received_prior = stats->received;
|
|
|
|
|
lost_interval = expected_interval - received_interval;
|
|
|
|
|
if (expected_interval == 0 || lost_interval <= 0)
|
|
|
|
|
fraction = 0;
|
|
|
|
|
else
|
|
|
|
|
fraction = (lost_interval << 8) / expected_interval;
|
|
|
|
|
|
|
|
|
|
fraction = (fraction << 24) | lost;
|
|
|
|
|
|
|
|
|
|
avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
|
|
|
|
|
avio_wb32(pb, extended_max); /* max sequence received */
|
|
|
|
|
avio_wb32(pb, stats->jitter >> 4); /* jitter */
|
|
|
|
|
|
|
|
|
|
if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
|
|
|
|
|
avio_wb32(pb, 0); /* last SR timestamp */
|
|
|
|
|
avio_wb32(pb, 0); /* delay since last SR */
|
|
|
|
|
} else {
|
|
|
|
|
uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
|
|
|
|
|
uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
|
|
|
|
|
65536, AV_TIME_BASE);
|
|
|
|
|
|
|
|
|
|
avio_wb32(pb, middle_32_bits); /* last SR timestamp */
|
|
|
|
|
avio_wb32(pb, delay_since_last); /* delay since last SR */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CNAME
|
|
|
|
|
avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
|
|
|
|
|
avio_w8(pb, RTCP_SDES);
|
|
|
|
|
len = strlen(s->hostname);
|
|
|
|
|
avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
|
|
|
|
|
avio_wb32(pb, s->ssrc + 1);
|
|
|
|
|
avio_w8(pb, 0x01);
|
|
|
|
|
avio_w8(pb, len);
|
|
|
|
|
avio_write(pb, s->hostname, len);
|
|
|
|
|
avio_w8(pb, 0); /* END */
|
|
|
|
|
// padding
|
|
|
|
|
for (len = (7 + len) % 4; len % 4; len++)
|
|
|
|
|
avio_w8(pb, 0);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void RtspPlayer::sendReceiverReport(bool overTcp,int iTrackIndex){
|
|
|
|
|
static const char s_cname[] = "ZLMediaKitRtsp";
|
|
|
|
|
uint8_t aui8Rtcp[4 + 32 + 10 + sizeof(s_cname) + 1] = {0};
|
|
|
|
|
uint8_t *pui8Rtcp_RR = aui8Rtcp + 4, *pui8Rtcp_SDES = pui8Rtcp_RR + 32;
|
|
|
|
|
auto &track = _aTrackInfo[iTrackIndex];
|
|
|
|
|
auto &counter = _aRtcpCnt[iTrackIndex];
|
|
|
|
|
|
|
|
|
|
aui8Rtcp[0] = '$';
|
|
|
|
|
aui8Rtcp[1] = track->_interleaved + 1;
|
|
|
|
|
aui8Rtcp[2] = (sizeof(aui8Rtcp) - 4) >> 8;
|
|
|
|
|
aui8Rtcp[3] = (sizeof(aui8Rtcp) - 4) & 0xFF;
|
|
|
|
|
|
|
|
|
|
pui8Rtcp_RR[0] = 0x81;/* 1 report block */
|
|
|
|
|
pui8Rtcp_RR[1] = 0xC9;//RTCP_RR
|
|
|
|
|
pui8Rtcp_RR[2] = 0x00;
|
|
|
|
|
pui8Rtcp_RR[3] = 0x07;/* length in words - 1 */
|
|
|
|
|
|
|
|
|
|
uint32_t ssrc=htonl(track->_ssrc + 1);
|
|
|
|
|
// our own SSRC: we use the server's SSRC + 1 to avoid conflicts
|
|
|
|
|
memcpy(&pui8Rtcp_RR[4], &ssrc, 4);
|
|
|
|
|
ssrc=htonl(track->_ssrc);
|
|
|
|
|
// server SSRC
|
|
|
|
|
memcpy(&pui8Rtcp_RR[8], &ssrc, 4);
|
|
|
|
|
|
|
|
|
|
//FIXME: 8 bits of fraction, 24 bits of total packets lost
|
|
|
|
|
pui8Rtcp_RR[12] = 0x00;
|
|
|
|
|
pui8Rtcp_RR[13] = 0x00;
|
|
|
|
|
pui8Rtcp_RR[14] = 0x00;
|
|
|
|
|
pui8Rtcp_RR[15] = 0x00;
|
|
|
|
|
|
|
|
|
|
//FIXME: max sequence received
|
|
|
|
|
int cycleCount = getCycleCount(iTrackIndex);
|
|
|
|
|
pui8Rtcp_RR[16] = cycleCount >> 8;
|
|
|
|
|
pui8Rtcp_RR[17] = cycleCount & 0xFF;
|
|
|
|
|
pui8Rtcp_RR[18] = counter.pktCnt >> 8;
|
|
|
|
|
pui8Rtcp_RR[19] = counter.pktCnt & 0xFF;
|
|
|
|
|
|
|
|
|
|
uint32_t jitter = htonl(getJitterSize(iTrackIndex));
|
|
|
|
|
//FIXME: jitter
|
|
|
|
|
memcpy(pui8Rtcp_RR + 20, &jitter , 4);
|
|
|
|
|
/* last SR timestamp */
|
|
|
|
|
memcpy(pui8Rtcp_RR + 24, &counter.lastTimeStamp, 4);
|
|
|
|
|
uint32_t msInc = htonl(ntohl(counter.timeStamp) - ntohl(counter.lastTimeStamp));
|
|
|
|
|
/* delay since last SR */
|
|
|
|
|
memcpy(pui8Rtcp_RR + 28, &msInc, 4);
|
|
|
|
|
|
|
|
|
|
// CNAME
|
|
|
|
|
pui8Rtcp_SDES[0] = 0x81;
|
|
|
|
|
pui8Rtcp_SDES[1] = 0xCA;
|
|
|
|
|
pui8Rtcp_SDES[2] = 0x00;
|
|
|
|
|
pui8Rtcp_SDES[3] = 0x06;
|
|
|
|
|
|
|
|
|
|
memcpy(&pui8Rtcp_SDES[4], &ssrc, 4);
|
|
|
|
|
|
|
|
|
|
pui8Rtcp_SDES[8] = 0x01;
|
|
|
|
|
pui8Rtcp_SDES[9] = 0x0f;
|
|
|
|
|
memcpy(&pui8Rtcp_SDES[10], s_cname, sizeof(s_cname));
|
|
|
|
|
pui8Rtcp_SDES[10 + sizeof(s_cname)] = 0x00;
|
|
|
|
|
|
|
|
|
|
if(overTcp){
|
|
|
|
|
send(obtainBuffer((char *) aui8Rtcp, sizeof(aui8Rtcp)));
|
|
|
|
|
}else if(_apRtcpSock[iTrackIndex]) {
|
|
|
|
|
_apRtcpSock[iTrackIndex]->send((char *) aui8Rtcp + 4, sizeof(aui8Rtcp) - 4);
|
|
|
|
|
}
|
2019-05-08 15:08:57 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-05-09 13:35:54 +08:00
|
|
|
|
|
2018-12-14 17:46:12 +08:00
|
|
|
|
void RtspPlayer::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//统计丢包率
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_aui16FirstSeq[trackidx] == 0 || rtppt->sequence < _aui16FirstSeq[trackidx]) {
|
|
|
|
|
_aui16FirstSeq[trackidx] = rtppt->sequence;
|
|
|
|
|
_aui64RtpRecv[trackidx] = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_aui64RtpRecv[trackidx] ++;
|
|
|
|
|
_aui16NowSeq[trackidx] = rtppt->sequence;
|
2018-11-07 17:40:37 +08:00
|
|
|
|
_aiNowStamp[trackidx] = rtppt->timeStamp;
|
2018-11-15 15:37:13 +08:00
|
|
|
|
if( _aiFistStamp[trackidx] == 0){
|
|
|
|
|
_aiFistStamp[trackidx] = _aiNowStamp[trackidx];
|
|
|
|
|
}
|
2018-11-07 17:40:37 +08:00
|
|
|
|
|
|
|
|
|
rtppt->timeStamp -= _aiFistStamp[trackidx];
|
2018-10-24 15:43:52 +08:00
|
|
|
|
onRecvRTP_l(rtppt,_aTrackInfo[trackidx]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
float RtspPlayer::getPacketLossRate(TrackType type) const{
|
2018-10-26 11:03:53 +08:00
|
|
|
|
int iTrackIdx = getTrackIndexByTrackType(type);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if(iTrackIdx == -1){
|
|
|
|
|
uint64_t totalRecv = 0;
|
|
|
|
|
uint64_t totalSend = 0;
|
2018-10-26 09:56:29 +08:00
|
|
|
|
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
totalRecv += _aui64RtpRecv[i];
|
|
|
|
|
totalSend += (_aui16NowSeq[i] - _aui16FirstSeq[i] + 1);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
if(totalSend == 0){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1.0 - (double)totalRecv / totalSend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(_aui16NowSeq[iTrackIdx] - _aui16FirstSeq[iTrackIdx] + 1 == 0){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return 1.0 - (double)_aui64RtpRecv[iTrackIdx] / (_aui16NowSeq[iTrackIdx] - _aui16FirstSeq[iTrackIdx] + 1);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
uint32_t RtspPlayer::getProgressMilliSecond() const{
|
|
|
|
|
uint32_t iTime[2] = {0,0};
|
2018-10-26 09:56:29 +08:00
|
|
|
|
for(unsigned int i = 0 ;i < _aTrackInfo.size() ;i++){
|
2018-10-26 14:12:16 +08:00
|
|
|
|
iTime[i] = _aiNowStamp[i] - _aiFistStamp[i];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
return _iSeekTo + MAX(iTime[0],iTime[1]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
void RtspPlayer::seekToMilliSecond(uint32_t ms) {
|
|
|
|
|
sendPause(false,ms);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2019-03-28 11:52:07 +08:00
|
|
|
|
void RtspPlayer::sendRtspRequest(const string &cmd, const string &url, const std::initializer_list<string> &header) {
|
2018-12-17 13:48:19 +08:00
|
|
|
|
string key;
|
|
|
|
|
StrCaseMap header_map;
|
|
|
|
|
int i = 0;
|
|
|
|
|
for(auto &val : header){
|
|
|
|
|
if(++i % 2 == 0){
|
|
|
|
|
header_map.emplace(key,val);
|
|
|
|
|
}else{
|
|
|
|
|
key = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-28 11:52:07 +08:00
|
|
|
|
sendRtspRequest(cmd,url,header_map);
|
2018-12-17 13:48:19 +08:00
|
|
|
|
}
|
2019-03-28 11:52:07 +08:00
|
|
|
|
void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrCaseMap &header_const) {
|
2018-07-02 15:43:37 +08:00
|
|
|
|
auto header = header_const;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
header.emplace("CSeq",StrPrinter << _uiCseq++);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
header.emplace("User-Agent",SERVER_NAME "(build in " __DATE__ " " __TIME__ ")");
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(!_strSession.empty()){
|
|
|
|
|
header.emplace("Session",_strSession);
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:56:49 +08:00
|
|
|
|
if(!_rtspRealm.empty() && !(*this)[kRtspUser].empty()){
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(!_rtspMd5Nonce.empty()){
|
2018-07-02 15:43:37 +08:00
|
|
|
|
//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) );
|
|
|
|
|
*/
|
2019-03-27 18:56:49 +08:00
|
|
|
|
string encrypted_pwd = (*this)[kRtspPwd];
|
|
|
|
|
if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
|
|
|
|
encrypted_pwd = MD5((*this)[kRtspUser]+ ":" + _rtspRealm + ":" + encrypted_pwd).hexdigest();
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
auto response = MD5( encrypted_pwd + ":" + _rtspMd5Nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
|
2018-07-02 15:43:37 +08:00
|
|
|
|
_StrPrinter printer;
|
|
|
|
|
printer << "Digest ";
|
2019-03-27 18:56:49 +08:00
|
|
|
|
printer << "username=\"" << (*this)[kRtspUser] << "\", ";
|
2019-03-27 18:41:52 +08:00
|
|
|
|
printer << "realm=\"" << _rtspRealm << "\", ";
|
|
|
|
|
printer << "nonce=\"" << _rtspMd5Nonce << "\", ";
|
2018-07-02 15:43:37 +08:00
|
|
|
|
printer << "uri=\"" << url << "\", ";
|
|
|
|
|
printer << "response=\"" << response << "\"";
|
|
|
|
|
header.emplace("Authorization",printer);
|
2019-03-27 18:56:49 +08:00
|
|
|
|
}else if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
2018-07-02 15:43:37 +08:00
|
|
|
|
//base64认证
|
2019-03-27 18:56:49 +08:00
|
|
|
|
string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
|
2018-07-02 15:43:37 +08:00
|
|
|
|
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";
|
|
|
|
|
}
|
2019-03-28 11:52:07 +08:00
|
|
|
|
send(printer << "\r\n");
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 13:35:54 +08:00
|
|
|
|
void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &pkt, const SdpTrack::Ptr &track) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_rtpTicker.resetTime();
|
2019-05-09 13:35:54 +08:00
|
|
|
|
onRecvRTP(pkt,track);
|
|
|
|
|
|
|
|
|
|
int iTrackIndex = getTrackIndexByInterleaved(pkt->interleaved);
|
|
|
|
|
if(iTrackIndex == -1){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RtcpCounter &counter = _aRtcpCnt[iTrackIndex];
|
|
|
|
|
counter.pktCnt = pkt->sequence;
|
|
|
|
|
auto &ticker = _aRtcpTicker[iTrackIndex];
|
|
|
|
|
if (ticker.elapsedTime() > 5 * 1000) {
|
|
|
|
|
//send rtcp every 5 second
|
|
|
|
|
counter.lastTimeStamp = counter.timeStamp;
|
|
|
|
|
//直接保存网络字节序
|
2019-06-24 16:07:44 +08:00
|
|
|
|
memcpy(&counter.timeStamp, pkt->data() + 8 , 4);
|
2019-05-09 13:35:54 +08:00
|
|
|
|
if(counter.lastTimeStamp != 0){
|
|
|
|
|
sendReceiverReport(_eType == Rtsp::RTP_TCP,iTrackIndex);
|
|
|
|
|
ticker.resetTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
|
|
|
|
void RtspPlayer::onPlayResult_l(const SockException &ex) {
|
|
|
|
|
WarnL << ex.getErrCode() << " " << ex.what();
|
2019-03-28 11:52:07 +08:00
|
|
|
|
|
2019-05-08 15:27:37 +08:00
|
|
|
|
if(!ex){
|
|
|
|
|
//播放成功,恢复rtp接收超时定时器
|
|
|
|
|
_rtpTicker.resetTime();
|
|
|
|
|
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
|
|
|
|
int timeoutMS = (*this)[kMediaTimeoutMS].as<int>();
|
|
|
|
|
_pRtpTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() {
|
|
|
|
|
auto strongSelf=weakSelf.lock();
|
|
|
|
|
if(!strongSelf) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(strongSelf->_rtpTicker.elapsedTime()> timeoutMS) {
|
|
|
|
|
//recv rtp timeout!
|
|
|
|
|
strongSelf->onPlayResult_l(SockException(Err_timeout,"recv rtp timeout"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},getPoller()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_pPlayTimer) {
|
|
|
|
|
//开始播放阶段
|
|
|
|
|
_pPlayTimer.reset();
|
|
|
|
|
onPlayResult(ex);
|
|
|
|
|
}else {
|
|
|
|
|
//播放中途阶段
|
|
|
|
|
if (ex) {
|
|
|
|
|
//播放成功后异常断开回调
|
|
|
|
|
onShutdown(ex);
|
|
|
|
|
}else{
|
|
|
|
|
//恢复播放
|
|
|
|
|
onResume();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ex){
|
|
|
|
|
teardown();
|
|
|
|
|
}
|
2018-07-02 15:43:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 18:48:08 +08:00
|
|
|
|
int RtspPlayer::getTrackIndexByControlSuffix(const string &controlSuffix) const{
|
2018-10-26 09:56:29 +08:00
|
|
|
|
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
2018-11-07 17:40:37 +08:00
|
|
|
|
auto pos = _aTrackInfo[i]->_control_surffix.find(controlSuffix);
|
|
|
|
|
if (pos == 0) {
|
2018-07-02 15:43:37 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-05 18:48:08 +08:00
|
|
|
|
int RtspPlayer::getTrackIndexByInterleaved(int interleaved) const{
|
2018-10-26 09:56:29 +08:00
|
|
|
|
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
if (_aTrackInfo[i]->_interleaved == interleaved) {
|
2018-07-05 18:48:08 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RtspPlayer::getTrackIndexByTrackType(TrackType trackType) const {
|
2018-10-26 09:56:29 +08:00
|
|
|
|
for (unsigned int i = 0; i < _aTrackInfo.size(); i++) {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
if (_aTrackInfo[i]->_type == trackType) {
|
2018-07-02 15:43:37 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|