diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 0693ddae..87812741 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -21,12 +21,6 @@ static string printAddress(const struct sockaddr *addr){ } RtpProcess::RtpProcess(const string &stream_id) { - _track = std::make_shared(); - _track->_interleaved = 0; - _track->_samplerate = 90000; - _track->_type = TrackVideo; - _track->_ssrc = 0; - _media_info._schema = RTP_APP_NAME; _media_info._vhost = DEFAULT_VHOST; _media_info._app = RTP_APP_NAME; @@ -104,7 +98,7 @@ bool RtpProcess::inputRtp(const Socket::Ptr &sock, const char *data, int data_le } _total_bytes += data_len; - bool ret = handleOneRtp(0,_track,(unsigned char *)data,data_len); + bool ret = handleOneRtp(0, TrackVideo, 90000, (unsigned char *) data, data_len); if(dts_out){ *dts_out = _dts; } diff --git a/src/Rtp/RtpProcess.h b/src/Rtp/RtpProcess.h index 81dc7f2f..d2d9c5ee 100644 --- a/src/Rtp/RtpProcess.h +++ b/src/Rtp/RtpProcess.h @@ -78,7 +78,6 @@ private: std::shared_ptr _save_file_rtp; std::shared_ptr _save_file_ps; std::shared_ptr _save_file_video; - SdpTrack::Ptr _track; struct sockaddr *_addr = nullptr; uint16_t _sequence = 0; MultiMediaSourceMuxer::Ptr _muxer; diff --git a/src/Rtsp/RtpReceiver.cpp b/src/Rtsp/RtpReceiver.cpp index e7703de7..0b10063c 100644 --- a/src/Rtsp/RtpReceiver.cpp +++ b/src/Rtsp/RtpReceiver.cpp @@ -27,7 +27,7 @@ namespace mediakit { RtpReceiver::RtpReceiver() {} RtpReceiver::~RtpReceiver() {} -bool RtpReceiver::handleOneRtp(int track_index,SdpTrack::Ptr &track, unsigned char *rtp_raw_ptr, unsigned int rtp_raw_len) { +bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate, unsigned char *rtp_raw_ptr, unsigned int rtp_raw_len) { if(rtp_raw_len < 12){ WarnL << "rtp包太小:" << rtp_raw_len; return false; @@ -46,8 +46,8 @@ bool RtpReceiver::handleOneRtp(int track_index,SdpTrack::Ptr &track, unsigned ch auto rtp_ptr = _rtp_pool.obtain(); auto &rtp = *rtp_ptr; - rtp.type = track->_type; - rtp.interleaved = 2 * track->_type; + rtp.type = type; + rtp.interleaved = 2 * type; rtp.mark = rtp_raw_ptr[1] >> 7; rtp.PT = rtp_raw_ptr[1] & 0x7F; @@ -59,29 +59,29 @@ bool RtpReceiver::handleOneRtp(int track_index,SdpTrack::Ptr &track, unsigned ch memcpy(&rtp.timeStamp, rtp_raw_ptr + 4, 4); rtp.timeStamp = ntohl(rtp.timeStamp); - if(!track->_samplerate){ + if(!samplerate){ //无法把时间戳转换成毫秒 return false; } //时间戳转换成毫秒 - rtp.timeStamp = rtp.timeStamp * 1000LL / track->_samplerate; + rtp.timeStamp = rtp.timeStamp * 1000LL / samplerate; //ssrc,内存对齐 memcpy(&rtp.ssrc, rtp_raw_ptr + 8, 4); rtp.ssrc = ntohl(rtp.ssrc); - if (track->_ssrc != rtp.ssrc) { - if (track->_ssrc == 0) { + if (_ssrc[track_index] != rtp.ssrc) { + if (_ssrc[track_index] == 0) { //保存SSRC至track对象 - track->_ssrc = rtp.ssrc; + _ssrc[track_index] = rtp.ssrc; }else{ //ssrc错误 - WarnL << "ssrc错误:" << rtp.ssrc << " != " << track->_ssrc; + WarnL << "ssrc错误:" << rtp.ssrc << " != " << _ssrc[track_index]; if (_ssrc_err_count[track_index]++ > 10) { //ssrc切换后清除老数据 - WarnL << "ssrc更换:" << track->_ssrc << " -> " << rtp.ssrc; + WarnL << "ssrc更换:" << _ssrc[track_index] << " -> " << rtp.ssrc; _rtp_sort_cache_map[track_index].clear(); - track->_ssrc = rtp.ssrc; + _ssrc[track_index] = rtp.ssrc; } return false; } @@ -168,11 +168,12 @@ void RtpReceiver::sortRtp(const RtpPacket::Ptr &rtp,int track_index){ } void RtpReceiver::clear() { - CLEAR_ARR(_last_seq) - CLEAR_ARR(_ssrc_err_count) - CLEAR_ARR(_seq_ok_count) - CLEAR_ARR(_sort_started) - CLEAR_ARR(_seq_cycle_count) + CLEAR_ARR(_last_seq); + CLEAR_ARR(_ssrc); + CLEAR_ARR(_ssrc_err_count); + CLEAR_ARR(_seq_ok_count); + CLEAR_ARR(_sort_started); + CLEAR_ARR(_seq_cycle_count); _rtp_sort_cache_map[0].clear(); _rtp_sort_cache_map[1].clear(); diff --git a/src/Rtsp/RtpReceiver.h b/src/Rtsp/RtpReceiver.h index 1631184e..243fef20 100644 --- a/src/Rtsp/RtpReceiver.h +++ b/src/Rtsp/RtpReceiver.h @@ -32,12 +32,13 @@ protected: /** * 输入数据指针生成并排序rtp包 * @param track_index track下标索引 - * @param track sdp track相关信息 + * @param type track类型 + * @param samplerate rtp时间戳基准时钟,视频为90000,音频为采样率 * @param rtp_raw_ptr rtp数据指针 * @param rtp_raw_len rtp数据指针长度 * @return 解析成功返回true */ - bool handleOneRtp(int track_index,SdpTrack::Ptr &track, unsigned char *rtp_raw_ptr, unsigned int rtp_raw_len); + bool handleOneRtp(int track_index, TrackType type, int samplerate, unsigned char *rtp_raw_ptr, unsigned int rtp_raw_len); /** * rtp数据包排序后输出 @@ -49,9 +50,12 @@ protected: void setPoolSize(int size); int getJitterSize(int track_index); int getCycleCount(int track_index); + private: void sortRtp(const RtpPacket::Ptr &rtp , int track_index); + private: + uint32_t _ssrc[2] = { 0, 0 }; //ssrc不匹配计数 uint32_t _ssrc_err_count[2] = { 0, 0 }; //上次seq diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index 4f7c02bb..a2ce2b24 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -319,7 +319,8 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex) WarnL << "收到其他地址的rtp数据:" << SockUtil::inet_ntoa(((struct sockaddr_in *) addr)->sin_addr); return; } - strongSelf->handleOneRtp(uiTrackIndex, strongSelf->_sdp_track[uiTrackIndex], (unsigned char *) buf->data(), buf->size()); + strongSelf->handleOneRtp(uiTrackIndex, strongSelf->_sdp_track[uiTrackIndex]->_type, + strongSelf->_sdp_track[uiTrackIndex]->_samplerate, (unsigned char *) buf->data(), buf->size()); }); if(pRtcpSockRef) { @@ -464,7 +465,7 @@ void RtspPlayer::onRtpPacket(const char *data, uint64_t len) { uint8_t interleaved = data[1]; if(interleaved %2 == 0){ trackIdx = getTrackIndexByInterleaved(interleaved); - handleOneRtp(trackIdx, _sdp_track[trackIdx], (unsigned char *)data + 4, len - 4); + handleOneRtp(trackIdx, _sdp_track[trackIdx]->_type, _sdp_track[trackIdx]->_samplerate, (unsigned char *)data + 4, len - 4); }else{ trackIdx = getTrackIndexByInterleaved(interleaved - 1); onRtcpPacket(trackIdx, _sdp_track[trackIdx], (unsigned char *) data + 4, len - 4); diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index df43487e..8a160e75 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -189,7 +189,7 @@ void RtspSession::onRtpPacket(const char *data, uint64_t len) { uint8_t interleaved = data[1]; if(interleaved %2 == 0){ trackIdx = getTrackIndexByInterleaved(interleaved); - handleOneRtp(trackIdx,_aTrackInfo[trackIdx],(unsigned char *)data + 4, len - 4); + handleOneRtp(trackIdx, _aTrackInfo[trackIdx]->_type, _aTrackInfo[trackIdx]->_samplerate, (unsigned char *) data + 4, len - 4); }else{ trackIdx = getTrackIndexByInterleaved(interleaved - 1); onRtcpPacket(trackIdx, _aTrackInfo[trackIdx], (unsigned char *) data + 4, len - 4); @@ -922,7 +922,8 @@ inline void RtspSession::onRcvPeerUdpData(int intervaled, const Buffer::Ptr &pBu if(intervaled % 2 == 0){ if(_pushSrc){ //这是rtsp推流上来的rtp包 - handleOneRtp(intervaled / 2,_aTrackInfo[intervaled / 2],( unsigned char *)pBuf->data(),pBuf->size()); + auto &ref = _aTrackInfo[intervaled / 2]; + handleOneRtp(intervaled / 2, ref->_type, ref->_samplerate, (unsigned char *) pBuf->data(), pBuf->size()); }else if(!_udpSockConnected.count(intervaled)){ //这是rtsp播放器的rtp打洞包 _udpSockConnected.emplace(intervaled);