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) { if (data && len) {
if(is_idr_fast_packet){
addNewSegment(timestamp); addNewSegment(timestamp);
}
onWriteSegment((char *) data, len); onWriteSegment((char *) data, len);
//记录上次写入数据时间 //记录上次写入数据时间
_ticker_last_data.resetTime(); _ticker_last_data.resetTime();
} else { } else {
//resetTracks时触发此逻辑
flushLastSegment(true); flushLastSegment(true);
} }
} }

View File

@ -52,8 +52,9 @@ public:
* @param data * @param data
* @param len * @param len
* @param timestamp * @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: protected:
/** /**
* ts切片文件回调 * ts切片文件回调

View File

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

View File

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

View File

@ -46,7 +46,7 @@ public:
void resetTracks() override; void resetTracks() override;
void inputFrame(const Frame::Ptr &frame) override; void inputFrame(const Frame::Ptr &frame) override;
protected: 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: private:
void init(); void init();
void uninit(); void uninit();
@ -61,6 +61,8 @@ private:
}; };
unordered_map<int,track_info> _codec_to_trackid; unordered_map<int,track_info> _codec_to_trackid;
List<Frame::Ptr> _frameCached; List<Frame::Ptr> _frameCached;
bool _is_idr_fast_packet = false;
bool _have_video = false;
}; };
}//namespace mediakit }//namespace mediakit