mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-11-24 00:47:21 +08:00
修复rtsp点播seek与直播时间戳相关的bug (#4802)
This commit is contained in:
Submodule 3rdpart/ZLToolKit updated: 00f56528d2...b311fdef4c
@@ -476,7 +476,6 @@ Value makeMediaSourceJson(MediaSource &media) {
|
||||
item["schema"] = media.getSchema();
|
||||
dumpMediaTuple(media.getMediaTuple(), item);
|
||||
item["createStamp"] = (Json::UInt64) media.getCreateStamp();
|
||||
item["currentStamp"] = (Json::UInt64) media.getTimeStamp(TrackInvalid);
|
||||
item["aliveSecond"] = (Json::UInt64) media.getAliveSecond();
|
||||
item["bytesSpeed"] = (Json::UInt64) media.getBytesSpeed();
|
||||
item["totalBytes"] = (Json::UInt64) media.getTotalBytes();
|
||||
|
||||
@@ -426,8 +426,6 @@ PYBIND11_EMBEDDED_MODULE(mk_loader, m) {
|
||||
.def("getSchema", &MediaSource::getSchema)
|
||||
.def("getUrl", &MediaSource::getUrl)
|
||||
.def("getMediaTuple", &MediaSource::getMediaTuple)
|
||||
.def("getTimeStamp", &MediaSource::getTimeStamp)
|
||||
.def("setTimeStamp", &MediaSource::setTimeStamp)
|
||||
.def("getBytesSpeed", &MediaSource::getBytesSpeed)
|
||||
.def("getTotalBytes", &MediaSource::getTotalBytes)
|
||||
.def("getCreateStamp", &MediaSource::getCreateStamp)
|
||||
|
||||
@@ -426,13 +426,6 @@ public:
|
||||
// Get all Tracks
|
||||
std::vector<Track::Ptr> getTracks(bool ready = true) const override;
|
||||
|
||||
// 获取流当前时间戳 [AUTO-TRANSLATED:f65f560a]
|
||||
// Get the current timestamp of the stream
|
||||
virtual uint32_t getTimeStamp(TrackType type) { return 0; };
|
||||
// 设置时间戳 [AUTO-TRANSLATED:2bfce32f]
|
||||
// Set timestamp
|
||||
virtual void setTimeStamp(uint32_t stamp) {};
|
||||
|
||||
// 获取数据速率,单位bytes/s [AUTO-TRANSLATED:c70465c1]
|
||||
// Get data rate, unit bytes/s
|
||||
size_t getBytesSpeed(TrackType type = TrackInvalid);
|
||||
|
||||
@@ -315,15 +315,6 @@ int MultiMediaSourceMuxer::totalReaderCount() const {
|
||||
(_ring ? _ring->readerCount() : 0);
|
||||
}
|
||||
|
||||
void MultiMediaSourceMuxer::setTimeStamp(uint32_t stamp) {
|
||||
if (_rtmp) {
|
||||
_rtmp->setTimeStamp(stamp);
|
||||
}
|
||||
if (_rtsp) {
|
||||
_rtsp->setTimeStamp(stamp);
|
||||
}
|
||||
}
|
||||
|
||||
int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) {
|
||||
auto listener = getDelegate();
|
||||
if (!listener) {
|
||||
|
||||
@@ -87,16 +87,6 @@ public:
|
||||
*/
|
||||
bool isEnabled();
|
||||
|
||||
/**
|
||||
* 设置MediaSource时间戳
|
||||
* @param stamp 时间戳
|
||||
* Set MediaSource timestamp
|
||||
* @param stamp Timestamp
|
||||
|
||||
* [AUTO-TRANSLATED:a75cc2fa]
|
||||
*/
|
||||
void setTimeStamp(uint32_t stamp);
|
||||
|
||||
/**
|
||||
* 重置track
|
||||
* Reset track
|
||||
|
||||
@@ -173,15 +173,9 @@ uint32_t MP4Reader::getCurrentStamp() {
|
||||
}
|
||||
|
||||
void MP4Reader::setCurrentStamp(uint32_t new_stamp) {
|
||||
auto old_stamp = getCurrentStamp();
|
||||
_seek_to = new_stamp;
|
||||
_last_dts = new_stamp;
|
||||
_seek_ticker.resetTime();
|
||||
if (old_stamp != new_stamp && _muxer) {
|
||||
// 时间轴未拖动时不操作 [AUTO-TRANSLATED:c5b53103]
|
||||
// Do not operate when the timeline is not dragged
|
||||
_muxer->setTimeStamp(new_stamp);
|
||||
}
|
||||
}
|
||||
|
||||
bool MP4Reader::seekTo(MediaSource &sender, uint32_t stamp) {
|
||||
|
||||
@@ -143,14 +143,6 @@ public:
|
||||
*/
|
||||
void onWrite(RtmpPacket::Ptr pkt, bool = true) override;
|
||||
|
||||
/**
|
||||
* 获取当前时间戳
|
||||
* Get the current timestamp
|
||||
|
||||
* [AUTO-TRANSLATED:42e38069]
|
||||
*/
|
||||
uint32_t getTimeStamp(TrackType trackType) override;
|
||||
|
||||
void clearCache() override{
|
||||
PacketCache<RtmpPacket>::clearCache();
|
||||
_ring->clearCache();
|
||||
|
||||
@@ -3,25 +3,6 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
uint32_t RtmpMediaSource::getTimeStamp(TrackType trackType) {
|
||||
assert(trackType >= TrackInvalid && trackType < TrackMax);
|
||||
if (trackType != TrackInvalid) {
|
||||
// 获取某track的时间戳 [AUTO-TRANSLATED:2735ed10]
|
||||
// Get the timestamp of a track
|
||||
return _track_stamps[trackType];
|
||||
}
|
||||
|
||||
// 获取所有track的最小时间戳 [AUTO-TRANSLATED:179a75cd]
|
||||
// Get the minimum timestamp of all tracks
|
||||
uint32_t ret = UINT32_MAX;
|
||||
for (auto &stamp : _track_stamps) {
|
||||
if (stamp > 0 && stamp < ret) {
|
||||
ret = stamp;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void RtmpMediaSource::setMetaData(const AMFValue &metadata) {
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
||||
|
||||
@@ -42,10 +42,6 @@ public:
|
||||
_media_src->setListener(shared_from_this());
|
||||
}
|
||||
|
||||
void setTimeStamp(uint32_t stamp){
|
||||
_media_src->setTimeStamp(stamp);
|
||||
}
|
||||
|
||||
int readerCount() const{
|
||||
return _media_src->readerCount();
|
||||
}
|
||||
|
||||
@@ -260,11 +260,6 @@ public:
|
||||
public:
|
||||
bool _inited = false;
|
||||
uint8_t _interleaved = 0;
|
||||
uint16_t _seq = 0;
|
||||
uint32_t _ssrc = 0;
|
||||
// 时间戳,单位毫秒 [AUTO-TRANSLATED:9513087f]
|
||||
// Timestamp, unit: milliseconds
|
||||
uint32_t _time_stamp = 0;
|
||||
};
|
||||
|
||||
class SdpParser {
|
||||
|
||||
@@ -111,52 +111,6 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取相应轨道的ssrc
|
||||
* Get the ssrc of the corresponding track
|
||||
|
||||
* [AUTO-TRANSLATED:d26d7f76]
|
||||
*/
|
||||
virtual uint32_t getSsrc(TrackType trackType) {
|
||||
assert(trackType >= 0 && trackType < TrackMax);
|
||||
auto &track = _tracks[trackType];
|
||||
if (!track) {
|
||||
return 0;
|
||||
}
|
||||
return track->_ssrc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取相应轨道的seqence
|
||||
* Get the sequence of the corresponding track
|
||||
|
||||
* [AUTO-TRANSLATED:24b0ee74]
|
||||
*/
|
||||
virtual uint16_t getSequence(TrackType trackType) {
|
||||
assert(trackType >= 0 && trackType < TrackMax);
|
||||
auto &track = _tracks[trackType];
|
||||
if (!track) {
|
||||
return 0;
|
||||
}
|
||||
return track->_seq;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取相应轨道的时间戳,单位毫秒
|
||||
* Get the timestamp of the corresponding track, in milliseconds
|
||||
|
||||
* [AUTO-TRANSLATED:564a0794]
|
||||
*/
|
||||
uint32_t getTimeStamp(TrackType trackType) override;
|
||||
|
||||
/**
|
||||
* 更新时间戳
|
||||
* Update timestamp
|
||||
|
||||
* [AUTO-TRANSLATED:8defe253]
|
||||
*/
|
||||
void setTimeStamp(uint32_t stamp) override;
|
||||
|
||||
/**
|
||||
* 设置sdp
|
||||
* Set sdp
|
||||
@@ -204,7 +158,6 @@ private:
|
||||
int _ring_size;
|
||||
std::string _sdp;
|
||||
RingType::Ptr _ring;
|
||||
SdpTrack::Ptr _tracks[TrackMax];
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
@@ -4,63 +4,17 @@
|
||||
namespace mediakit {
|
||||
void RtspMediaSource::setSdp(const std::string &sdp) {
|
||||
SdpParser sdp_parser(sdp);
|
||||
_tracks[TrackVideo] = sdp_parser.getTrack(TrackVideo);
|
||||
_tracks[TrackAudio] = sdp_parser.getTrack(TrackAudio);
|
||||
_have_video = (bool)_tracks[TrackVideo];
|
||||
_have_video = static_cast<bool>(sdp_parser.getTrack(TrackVideo));
|
||||
_sdp = sdp_parser.toString();
|
||||
if (_ring) {
|
||||
regist();
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t RtspMediaSource::getTimeStamp(TrackType trackType) {
|
||||
assert(trackType >= TrackInvalid && trackType < TrackMax);
|
||||
if (trackType != TrackInvalid) {
|
||||
// 获取某track的时间戳 [AUTO-TRANSLATED:e3fecb81]
|
||||
// Get the timestamp of a track
|
||||
auto &track = _tracks[trackType];
|
||||
if (track) {
|
||||
return track->_time_stamp;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有track的最小时间戳 [AUTO-TRANSLATED:8cf9218b]
|
||||
// Get the minimum timestamp of all tracks
|
||||
uint32_t ret = UINT32_MAX;
|
||||
for (auto &track : _tracks) {
|
||||
if (track && track->_time_stamp < ret) {
|
||||
ret = track->_time_stamp;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新时间戳
|
||||
* Update timestamp
|
||||
|
||||
* [AUTO-TRANSLATED:fa02a89e]
|
||||
*/
|
||||
void RtspMediaSource::setTimeStamp(uint32_t stamp) {
|
||||
for (auto &track : _tracks) {
|
||||
if (track) {
|
||||
track->_time_stamp = stamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RtspMediaSource::onWrite(RtpPacket::Ptr rtp, bool keyPos) {
|
||||
_speed[rtp->type] += rtp->size();
|
||||
assert(rtp->type >= 0 && rtp->type < TrackMax);
|
||||
auto &track = _tracks[rtp->type];
|
||||
auto stamp = rtp->getStampMS();
|
||||
bool is_video = rtp->type == TrackVideo;
|
||||
// 音频总是更新,视频在关键包时更新
|
||||
if (track && ((keyPos && _have_video && is_video) || (!is_video))) {
|
||||
track->_seq = rtp->getSeq();
|
||||
track->_time_stamp = rtp->getStamp() * uint64_t(1000) / rtp->sample_rate;
|
||||
track->_ssrc = rtp->getSSRC();
|
||||
}
|
||||
if (!_ring) {
|
||||
std::weak_ptr<RtspMediaSource> weakSelf = std::static_pointer_cast<RtspMediaSource>(shared_from_this());
|
||||
auto lam = [weakSelf](int size) {
|
||||
@@ -80,7 +34,7 @@ void RtspMediaSource::onWrite(RtpPacket::Ptr rtp, bool keyPos) {
|
||||
}
|
||||
}
|
||||
|
||||
PacketCache<RtpPacket>::inputPacket(stamp, is_video, std::move(rtp), keyPos);
|
||||
PacketCache<RtpPacket>::inputPacket(stamp, rtp->type == TrackVideo, std::move(rtp), keyPos);
|
||||
}
|
||||
|
||||
RtspMediaSourceImp::RtspMediaSourceImp(const MediaTuple& tuple, int ringSize): RtspMediaSource(tuple, ringSize)
|
||||
|
||||
@@ -46,10 +46,6 @@ public:
|
||||
return _media_src->readerCount();
|
||||
}
|
||||
|
||||
void setTimeStamp(uint32_t stamp){
|
||||
_media_src->setTimeStamp(stamp);
|
||||
}
|
||||
|
||||
void addTrackCompleted() override {
|
||||
RtspMuxer::addTrackCompleted();
|
||||
_media_src->setSdp(getSdp());
|
||||
|
||||
@@ -324,9 +324,9 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) {
|
||||
RtspSplitter::enableRecvRtp(_rtp_type == Rtsp::RTP_TCP);
|
||||
string ssrc = transport_map["ssrc"];
|
||||
if (!ssrc.empty()) {
|
||||
sscanf(ssrc.data(), "%x", &_sdp_track[track_idx]->_ssrc);
|
||||
sscanf(ssrc.data(), "%x", &_ssrc[track_idx]);
|
||||
} else {
|
||||
_sdp_track[track_idx]->_ssrc = 0;
|
||||
_ssrc[track_idx] = 0;
|
||||
}
|
||||
|
||||
if (_rtp_type == Rtsp::RTP_TCP) {
|
||||
@@ -627,6 +627,10 @@ void RtspPlayer::onRtpPacket(const char *data, size_t len) {
|
||||
if (trackIdx == -1) {
|
||||
return;
|
||||
}
|
||||
auto ssrc = _ssrc[trackIdx];
|
||||
if (ssrc && ssrc != ntohl(header->ssrc)) {
|
||||
WarnL << "ssrc not expected: " << ntohl(header->ssrc) << " != " << ssrc << ", track index: " << trackIdx;
|
||||
}
|
||||
handleOneRtp(
|
||||
trackIdx, _sdp_track[trackIdx]->_type, _sdp_track[trackIdx]->_samplerate, (uint8_t *)data + RtpPacket::kRtpTcpHeaderSize,
|
||||
len - RtpPacket::kRtpTcpHeaderSize);
|
||||
|
||||
@@ -215,6 +215,8 @@ private:
|
||||
std::vector<RtcpContext::Ptr> _rtcp_context;
|
||||
// 用户自定义rtsp头
|
||||
StrCaseMap _custom_header;
|
||||
// ssrc
|
||||
uint32_t _ssrc[TrackMax] { };
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
@@ -257,6 +257,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
|
||||
SdpParser sdpParser(parser.content());
|
||||
_sessionid = makeRandStr(12);
|
||||
_sdp_track = sdpParser.getAvailableTrack();
|
||||
_duration = sdpParser.getTrack(TrackTitle)->_duration;
|
||||
if (_sdp_track.empty()) {
|
||||
// sdp无效
|
||||
static constexpr auto err = "sdp中无有效track";
|
||||
@@ -405,6 +406,25 @@ void RtspSession::handleReq_Describe(const Parser &parser) {
|
||||
}
|
||||
}
|
||||
|
||||
void RtspSession::getRtpTrackInfo(bool use_gop) {
|
||||
auto rtsp_src = _play_src.lock();
|
||||
if (!rtsp_src) {
|
||||
return;
|
||||
}
|
||||
auto reader = rtsp_src->getRing()->attach(getPoller(), use_gop);
|
||||
reader->setReadCB([&](const RtspMediaSource::RingDataType &pkt) {
|
||||
pkt->for_each([&](const RtpPacket::Ptr &rtp) {
|
||||
auto &rtp_info = _rtp_info[getTrackIndexByTrackType(rtp->type)];
|
||||
if (!rtp_info) {
|
||||
rtp_info.ssrc = rtp->getSSRC();
|
||||
rtp_info.seq = rtp->getSeq();
|
||||
rtp_info.time_stamp = rtp->getStamp();
|
||||
rtp_info.time_stamp_ms = rtp->getStampMS(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void RtspSession::onAuthSuccess() {
|
||||
weak_ptr<RtspSession> weak_self = static_pointer_cast<RtspSession>(shared_from_this());
|
||||
MediaSource::findAsync(_media_info, weak_self.lock(), [weak_self](const MediaSource::Ptr &src){
|
||||
@@ -420,8 +440,11 @@ void RtspSession::onAuthSuccess() {
|
||||
strong_self->shutdown(SockException(Err_shutdown,err));
|
||||
return;
|
||||
}
|
||||
//找到了相应的rtsp流
|
||||
strong_self->_sdp_track = SdpParser(rtsp_src->getSdp()).getAvailableTrack();
|
||||
// 找到了相应的rtsp流
|
||||
SdpParser parser(rtsp_src->getSdp());
|
||||
strong_self->_sdp_track = parser.getAvailableTrack();
|
||||
strong_self->_duration = parser.getTrack(TrackTitle)->_duration;
|
||||
|
||||
if (strong_self->_sdp_track.empty()) {
|
||||
//该流无效
|
||||
WarnL << "sdp中无有效track,该流无效:" << rtsp_src->getSdp();
|
||||
@@ -435,12 +458,7 @@ void RtspSession::onAuthSuccess() {
|
||||
}
|
||||
strong_self->_sessionid = makeRandStr(12);
|
||||
strong_self->_play_src = rtsp_src;
|
||||
for(auto &track : strong_self->_sdp_track){
|
||||
track->_ssrc = rtsp_src->getSsrc(track->_type);
|
||||
track->_seq = rtsp_src->getSequence(track->_type);
|
||||
track->_time_stamp = rtsp_src->getTimeStamp(track->_type);
|
||||
}
|
||||
|
||||
strong_self->getRtpTrackInfo(true);
|
||||
strong_self->sendRtspResponse("200 OK",
|
||||
{"Content-Base", strong_self->_content_base + "/",
|
||||
"x-Accept-Retransmit","our-retransmit",
|
||||
@@ -698,7 +716,7 @@ void RtspSession::handleReq_Setup(const Parser &parser) {
|
||||
{"Transport", StrPrinter << "RTP/AVP/TCP;unicast;"
|
||||
<< "interleaved=" << (int) trackRef->_interleaved << "-"
|
||||
<< (int) trackRef->_interleaved + 1 << ";"
|
||||
<< "ssrc=" << printSSRC(trackRef->_ssrc),
|
||||
<< "ssrc=" << printSSRC(_rtp_info[trackIdx].ssrc),
|
||||
"x-Transport-Options", "late-tolerance=1.400000",
|
||||
"x-Dynamic-Rate", "1"
|
||||
});
|
||||
@@ -740,7 +758,7 @@ void RtspSession::handleReq_Setup(const Parser &parser) {
|
||||
<< "client_port=" << strClientPort << ";"
|
||||
<< "server_port=" << pr.first->get_local_port() << "-"
|
||||
<< pr.second->get_local_port() << ";"
|
||||
<< "ssrc=" << printSSRC(trackRef->_ssrc)
|
||||
<< "ssrc=" << printSSRC(_rtp_info[trackIdx].ssrc)
|
||||
});
|
||||
}
|
||||
break;
|
||||
@@ -778,7 +796,7 @@ void RtspSession::handleReq_Setup(const Parser &parser) {
|
||||
<< "source=" << get_local_ip() << ";"
|
||||
<< "port=" << iSrvPort << "-" << pSockRtcp->get_local_port() << ";"
|
||||
<< "ttl=" << udpTTL << ";"
|
||||
<< "ssrc=" << printSSRC(trackRef->_ssrc)
|
||||
<< "ssrc=" << printSSRC(_rtp_info[trackIdx].ssrc)
|
||||
});
|
||||
}
|
||||
break;
|
||||
@@ -800,60 +818,120 @@ void RtspSession::handleReq_Play(const Parser &parser) {
|
||||
}
|
||||
|
||||
bool use_gop = true;
|
||||
auto &strScale = parser["Scale"];
|
||||
auto &strRange = parser["Range"];
|
||||
StrCaseMap res_header;
|
||||
if (!strScale.empty()) {
|
||||
//这是设置播放速度
|
||||
res_header.emplace("Scale", strScale);
|
||||
auto speed = atof(strScale.data());
|
||||
play_src->speed(speed);
|
||||
InfoP(this) << "rtsp set play speed:" << speed;
|
||||
}
|
||||
|
||||
if (!strRange.empty()) {
|
||||
//这是seek操作
|
||||
res_header.emplace("Range", strRange);
|
||||
auto strStart = findSubString(strRange.data(), "npt=", "-");
|
||||
if (strStart == "now") {
|
||||
strStart = "0";
|
||||
if (_duration > 0.1) {
|
||||
// 点播才支持seek和speed
|
||||
auto &strScale = parser["Scale"];
|
||||
auto &strRange = parser["Range"];
|
||||
if (!strScale.empty()) {
|
||||
// 这是设置播放速度
|
||||
res_header.emplace("Scale", strScale);
|
||||
auto speed = atof(strScale.data());
|
||||
if (play_src->speed(speed)) {
|
||||
InfoP(this) << "rtsp set play speed:" << speed;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strRange.empty()) {
|
||||
// 这是seek操作
|
||||
auto strStart = findSubString(strRange.data(), "npt=", "-");
|
||||
if (strStart == "now") {
|
||||
strStart = "0";
|
||||
}
|
||||
auto iStartTime = 1000 * (float)atof(strStart.data());
|
||||
if (play_src->seekTo((uint32_t)iStartTime)) {
|
||||
InfoP(this) << "rtsp seekTo(ms):" << iStartTime;
|
||||
use_gop = false;
|
||||
}
|
||||
}
|
||||
auto iStartTime = 1000 * (float) atof(strStart.data());
|
||||
use_gop = !play_src->seekTo((uint32_t) iStartTime);
|
||||
InfoP(this) << "rtsp seekTo(ms):" << iStartTime;
|
||||
}
|
||||
|
||||
vector<TrackType> inited_tracks;
|
||||
_StrPrinter rtp_info;
|
||||
std::set<TrackType> inited_tracks;
|
||||
for (auto &track : _sdp_track) {
|
||||
if (track->_inited == false) {
|
||||
//为支持播放器播放单一track, 不校验没有发setup的track
|
||||
continue;
|
||||
if (track->_inited) {
|
||||
inited_tracks.emplace(track->_type);
|
||||
}
|
||||
inited_tracks.emplace_back(track->_type);
|
||||
track->_ssrc = play_src->getSsrc(track->_type);
|
||||
track->_seq = play_src->getSequence(track->_type);
|
||||
track->_time_stamp = play_src->getTimeStamp(track->_type);
|
||||
|
||||
rtp_info << "url=" << track->getControlUrl(_content_base) << ";"
|
||||
<< "seq=" << track->_seq << ";"
|
||||
<< "rtptime=" << (int64_t)(track->_time_stamp) * (int64_t)(track->_samplerate/ 1000) << ",";
|
||||
}
|
||||
|
||||
rtp_info.pop_back();
|
||||
|
||||
res_header.emplace("RTP-Info", rtp_info);
|
||||
//已存在Range时不覆盖
|
||||
res_header.emplace("Range", StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << play_src->getTimeStamp(TrackInvalid) / 1000.0);
|
||||
sendRtspResponse("200 OK", res_header);
|
||||
|
||||
//设置播放track
|
||||
if (inited_tracks.size() == 1) {
|
||||
_target_play_track = inited_tracks[0];
|
||||
InfoP(this) << "指定播放track:" << _target_play_track;
|
||||
if (inited_tracks.empty()) {
|
||||
// 所有track都没有setup
|
||||
shutdown(SockException(Err_shutdown, "none track is setuped"));
|
||||
return;
|
||||
}
|
||||
|
||||
//在回复rtsp信令后再恢复播放
|
||||
// 设置播放track
|
||||
if (_sdp_track.size() > inited_tracks.size()) {
|
||||
_target_play_track = *inited_tracks.begin();
|
||||
InfoP(this) << "only play selected track: " << getTrackString(_target_play_track);
|
||||
}
|
||||
size_t rtp_cout = 0;
|
||||
std::vector<RtspMediaSource::RingDataType> send_rtp_cache;
|
||||
_on_send_rtp = [=](const RtspMediaSource::RingDataType &pack, bool is_key) mutable {
|
||||
if (!use_gop) {
|
||||
// seek后关闭gop缓存,需要等待下一个关键帧才能开始发送seek后的数据
|
||||
if (!is_key) {
|
||||
return true;
|
||||
}
|
||||
use_gop = true;
|
||||
}
|
||||
|
||||
rtp_cout += pack->size();
|
||||
send_rtp_cache.emplace_back(pack);
|
||||
pack->for_each([&](const RtpPacket::Ptr &rtp) mutable {
|
||||
if (inited_tracks.count(rtp->type)) {
|
||||
auto &rtp_info = _rtp_info[getTrackIndexByTrackType(rtp->type)];
|
||||
rtp_info.ssrc = rtp->getSSRC();
|
||||
rtp_info.seq = rtp->getSeq();
|
||||
rtp_info.time_stamp = rtp->getStamp();
|
||||
rtp_info.time_stamp_ms = rtp->getStampMS(false);
|
||||
inited_tracks.erase(rtp->type);
|
||||
}
|
||||
});
|
||||
if (!inited_tracks.empty() && rtp_cout < 512) {
|
||||
// 限制rtp缓存个数,防止内存溢出
|
||||
return true;
|
||||
}
|
||||
if (!inited_tracks.empty()) {
|
||||
WarnL << "get rtp track info failed: " << getTrackString(*inited_tracks.begin());
|
||||
}
|
||||
|
||||
_StrPrinter rtp_info;
|
||||
int index = 0;
|
||||
for (auto &track : _sdp_track) {
|
||||
auto &rtp_track = _rtp_info[index++];
|
||||
if (track->_inited) {
|
||||
rtp_info << "url=" << track->getControlUrl(_content_base) << ";"
|
||||
<< "seq=" << rtp_track.seq << ";"
|
||||
<< "rtptime=" << rtp_track.time_stamp << ",";
|
||||
}
|
||||
}
|
||||
|
||||
rtp_info.pop_back();
|
||||
res_header.emplace("RTP-Info", rtp_info);
|
||||
|
||||
if (_duration > 0.1) {
|
||||
// vlc在播放rtsp点播时,range设置异常(还是直播样式),我们返回正确的时长
|
||||
auto time_stamp_ms = UINT64_MAX;
|
||||
for (auto &info : _rtp_info) {
|
||||
if (info && info.time_stamp_ms < time_stamp_ms) {
|
||||
time_stamp_ms = info.time_stamp_ms;
|
||||
}
|
||||
}
|
||||
// 点播
|
||||
res_header.emplace("Range", StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << time_stamp_ms / 1000.0f << '-' << _duration);
|
||||
} else {
|
||||
// 直播
|
||||
res_header.emplace("Range", "npt=0.00-");
|
||||
}
|
||||
sendRtspResponse("200 OK", res_header);
|
||||
for (auto &cache : send_rtp_cache) {
|
||||
sendRtpPacket(cache);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// 在回复rtsp信令后再恢复播放
|
||||
play_src->pause(false);
|
||||
|
||||
setSocketFlags();
|
||||
@@ -873,11 +951,18 @@ void RtspSession::handleReq_Play(const Parser &parser) {
|
||||
}
|
||||
strong_self->shutdown(SockException(Err_shutdown, "rtsp ring buffer detached"));
|
||||
});
|
||||
_play_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pack) {
|
||||
_play_reader->setReadCB2([weak_self](const RtspMediaSource::RingDataType &pack, bool is_key) {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (!strong_self) {
|
||||
return;
|
||||
}
|
||||
if (strong_self->_on_send_rtp) {
|
||||
auto again = strong_self->_on_send_rtp(pack, is_key);
|
||||
if (!again) {
|
||||
strong_self->_on_send_rtp = nullptr;
|
||||
}
|
||||
return;
|
||||
}
|
||||
strong_self->sendRtpPacket(pack);
|
||||
});
|
||||
}
|
||||
@@ -1100,7 +1185,6 @@ bool RtspSession::sendRtspResponse(const string &res_code, const StrCaseMap &hea
|
||||
if(!sdp.empty()){
|
||||
printer << sdp;
|
||||
}
|
||||
// DebugP(this) << printer;
|
||||
return send(std::make_shared<BufferString>(std::move(printer))) > 0 ;
|
||||
}
|
||||
|
||||
|
||||
@@ -196,6 +196,8 @@ private:
|
||||
// 设置socket标志 [AUTO-TRANSLATED:4086e686]
|
||||
// Set socket flag
|
||||
void setSocketFlags();
|
||||
// 获取rtp ssrc/seq/时间戳等信息
|
||||
void getRtpTrackInfo(bool use_gop);
|
||||
|
||||
private:
|
||||
// 是否已经触发on_play事件 [AUTO-TRANSLATED:49c937ce]
|
||||
@@ -211,6 +213,8 @@ private:
|
||||
// 收到的seq,回复时一致 [AUTO-TRANSLATED:64544fb4]
|
||||
// Received seq, consistent when replying
|
||||
int _cseq = 0;
|
||||
// rtsp src duration
|
||||
float _duration { 0 };
|
||||
// 消耗的总流量 [AUTO-TRANSLATED:45ad2785]
|
||||
// Total traffic consumed
|
||||
uint64_t _bytes_usage = 0;
|
||||
@@ -269,6 +273,18 @@ private:
|
||||
// 统计rtp并发送rtcp [AUTO-TRANSLATED:0ac2b665]
|
||||
// Count RTP and send RTCP
|
||||
std::vector<RtcpContext::Ptr> _rtcp_context;
|
||||
|
||||
struct RtpTrackInfo {
|
||||
uint32_t ssrc { 0 };
|
||||
uint16_t seq { 0 };
|
||||
// rtp原始时间戳
|
||||
uint32_t time_stamp { 0 };
|
||||
uint64_t time_stamp_ms { 0 };
|
||||
operator bool() const { return ssrc != 0 || seq != 0 || time_stamp != 0 || time_stamp_ms != 0; }
|
||||
};
|
||||
|
||||
RtpTrackInfo _rtp_info[2];
|
||||
std::function<bool(const RtspMediaSource::RingDataType &pack, bool is_key)> _on_send_rtp;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user