优化rtmp点播

This commit is contained in:
xiongziliang 2020-04-04 15:37:37 +08:00
parent cefb2a7cc4
commit 708300951b
3 changed files with 31 additions and 18 deletions

View File

@ -46,15 +46,15 @@ int64_t DeltaStamp::deltaStamp(int64_t stamp) {
//时间戳增量为正,返回之
_last_stamp = stamp;
//在直播情况下时间戳增量不得大于MAX_DELTA_STAMP
return ret < MAX_DELTA_STAMP ? ret : (_playback ? ret : 0);
return ret < MAX_DELTA_STAMP ? ret : 0;
}
//时间戳增量为负,说明时间戳回环了或回退了
_last_stamp = stamp;
return _playback ? ret : 0;
return 0;
}
void DeltaStamp::setPlayBack(bool playback) {
void Stamp::setPlayBack(bool playback) {
_playback = playback;
}
@ -64,6 +64,14 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,
pts = dts;
}
if(_playback){
//这是点播
dts_out = dts;
pts_out = pts;
_relativeStamp = dts_out;
return;
}
//pts和dts的差值
int pts_dts_diff = pts - dts;

View File

@ -45,15 +45,8 @@ public:
* @return
*/
int64_t deltaStamp(int64_t stamp);
/**
* 退
* @param playback
*/
void setPlayBack(bool playback = true);
private:
int64_t _last_stamp = 0;
bool _playback = false;
};
//该类解决时间戳回环、回退问题
//计算相对时间戳或者产生平滑时间戳
@ -83,10 +76,17 @@ public:
* @return
*/
int64_t getRelativeStamp() const ;
/**
* 退
* @param playback
*/
void setPlayBack(bool playback = true);
private:
int64_t _relativeStamp = 0;
int64_t _last_dts = -1;
SmoothTicker _ticker;
bool _playback = false;
};

View File

@ -274,6 +274,12 @@ void RtmpSession::sendPlayResponse(const string &err,const RtmpMediaSource::Ptr
invoke.clear();
invoke << "onMetaData" << metadata;
sendResponse(MSG_DATA, invoke.data());
auto duration = metadata["duration"].as_number();
if(duration > 0){
//这是点播,使用绝对时间戳
_stamp[0].setPlayBack();
_stamp[1].setPlayBack();
}
}
@ -510,20 +516,19 @@ void RtmpSession::onRtmpChunk(RtmpPacket &chunkData) {
void RtmpSession::onCmd_seek(AMFDecoder &dec) {
dec.load<AMFValue>();/* NULL */
auto milliSeconds = dec.load<AMFValue>().as_number();
InfoP(this) << "rtmp seekTo(ms):" << milliSeconds;
auto stongSrc = _pPlayerSrc.lock();
if (stongSrc) {
_stamp[0].setPlayBack();
_stamp[1].setPlayBack();
stongSrc->seekTo(milliSeconds);
}
AMFValue status(AMF_OBJECT);
AMFEncoder invoke;
status.set("level", "status");
status.set("code", "NetStream.Seek.Notify");
status.set("description", "Seeking.");
sendReply("onStatus", nullptr, status);
auto milliSeconds = dec.load<AMFValue>().as_number();
InfoP(this) << "rtmp seekTo(ms):" << milliSeconds;
auto stongSrc = _pPlayerSrc.lock();
if (stongSrc) {
stongSrc->seekTo(milliSeconds);
}
}
void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {