HLS遇到I帧才切片:#226

This commit is contained in:
xiongziliang 2020-01-24 22:00:55 +08:00
parent 224a35dbd5
commit a9539eb626
5 changed files with 28 additions and 13 deletions

View File

@ -75,14 +75,16 @@ void HlsMaker::makeIndexFile(bool eof) {
}
void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp) {
//分片数据中断结束
void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp, bool is_idr_fast_packet) {
if (data && len) {
addNewSegment(timestamp);
if(is_idr_fast_packet){
addNewSegment(timestamp);
}
onWriteSegment((char *) data, len);
//记录上次写入数据时间
_ticker_last_data.resetTime();
} else {
//resetTracks时触发此逻辑
flushLastSegment(true);
}
}

View File

@ -52,8 +52,9 @@ public:
* @param data
* @param len
* @param timestamp
* @param is_idr_fast_packet
*/
void inputData(void *data, uint32_t len, uint32_t timestamp);
void inputData(void *data, uint32_t len, uint32_t timestamp, bool is_idr_fast_packet);
protected:
/**
* ts切片文件回调

View File

@ -56,8 +56,8 @@ public:
}
#if defined(ENABLE_HLS)
protected:
void onTs(const void *packet, int bytes,uint32_t timestamp,int flags) override {
_hls->inputData((char *)packet,bytes,timestamp);
void onTs(const void *packet, int bytes,uint32_t timestamp,bool is_idr_fast_packet) override {
_hls->inputData((char *)packet,bytes,timestamp, is_idr_fast_packet);
};
#endif
private:

View File

@ -40,16 +40,21 @@ TsMuxer::~TsMuxer() {
}
void TsMuxer::addTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
switch (track->getCodecId()) {
case CodecH264: {
_have_video = true;
_codec_to_trackid[track->getCodecId()].track_id = mpeg_ts_add_stream(_context, PSI_STREAM_H264, nullptr, 0);
} break;
}
break;
case CodecH265: {
_have_video = true;
_codec_to_trackid[track->getCodecId()].track_id = mpeg_ts_add_stream(_context, PSI_STREAM_H265, nullptr, 0);
}break;
}
break;
case CodecAAC: {
_codec_to_trackid[track->getCodecId()].track_id = mpeg_ts_add_stream(_context, PSI_STREAM_AAC, nullptr, 0);
}break;
}
break;
default:
break;
}
@ -63,7 +68,7 @@ void TsMuxer::inputFrame(const Frame::Ptr &frame) {
//mp4文件时间戳需要从0开始
auto &track_info = it->second;
int64_t dts_out, pts_out;
_is_idr_fast_packet = !_have_video;
switch (frame->getCodecId()){
case CodecH265:
case CodecH264: {
@ -80,6 +85,9 @@ void TsMuxer::inputFrame(const Frame::Ptr &frame) {
merged.append("\x00\x00\x00\x01",4);
merged.append(frame->data(),frame->size());
}
if(frame->keyFrame()){
_is_idr_fast_packet = true;
}
});
merged_frame = std::make_shared<BufferString>(std::move(merged));
}
@ -101,6 +109,7 @@ void TsMuxer::inputFrame(const Frame::Ptr &frame) {
}
void TsMuxer::resetTracks() {
_have_video = false;
//通知片段中断
onTs(nullptr, 0, 0, 0);
uninit();
@ -119,7 +128,8 @@ void TsMuxer::init() {
},
[](void* param, const void* packet, size_t bytes){
TsMuxer *muxer = (TsMuxer *)param;
muxer->onTs(packet, bytes,muxer->_timestamp,0);
muxer->onTs(packet, bytes,muxer->_timestamp,muxer->_is_idr_fast_packet);
muxer->_is_idr_fast_packet = false;
}
};
if(_context == nullptr){

View File

@ -46,7 +46,7 @@ public:
void resetTracks() override;
void inputFrame(const Frame::Ptr &frame) override;
protected:
virtual void onTs(const void *packet, int bytes,uint32_t timestamp,int flags) = 0;
virtual void onTs(const void *packet, int bytes,uint32_t timestamp,bool is_idr_fast_packet) = 0;
private:
void init();
void uninit();
@ -61,6 +61,8 @@ private:
};
unordered_map<int,track_info> _codec_to_trackid;
List<Frame::Ptr> _frameCached;
bool _is_idr_fast_packet = false;
bool _have_video = false;
};
}//namespace mediakit