修复MP4点播相关bug

This commit is contained in:
xiongziliang 2020-04-03 21:39:44 +08:00
parent 095834fe57
commit 278591d5ee
3 changed files with 15 additions and 13 deletions

View File

@ -158,7 +158,7 @@ struct Context{
BufferRaw::Ptr buffer;
};
Frame::Ptr MP4Demuxer::readFrame(bool seekKeyFrame, bool *eof) {
Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
static mov_reader_onread mov_reader_onread = [](void *param, uint32_t track_id, const void *buffer, size_t bytes, int64_t pts, int64_t dts, int flags) {
Context *ctx = (Context *) param;
ctx->pts = pts;
@ -179,23 +179,19 @@ Frame::Ptr MP4Demuxer::readFrame(bool seekKeyFrame, bool *eof) {
auto ret = mov_reader_read2(_mov_reader.get(), mov_onalloc, mov_reader_onread, &ctx);
switch (ret) {
case 0 : {
if(eof){
*eof = true;
}
eof = true;
WarnL << "读取mp4文件完毕";
}
break;
case 1 : {
if (seekKeyFrame && !(ctx.flags & MOV_AV_FLAG_KEYFREAME)) {
//请求key帧但是这个帧不是
return nullptr;
}
keyFrame = ctx.flags & MOV_AV_FLAG_KEYFREAME;
return makeFrame(ctx.track_id,ctx.buffer, ctx.pts, ctx.dts);
}
break;
default:
eof = true;
WarnL << "读取mp4文件数据失败:" << ret;
break;
}

View File

@ -38,7 +38,7 @@ public:
MP4Demuxer(const char *file);
~MP4Demuxer() override;
int64_t seekTo(int64_t stamp_ms);
Frame::Ptr readFrame(bool seekKeyFrame = false, bool *eof = nullptr);
Frame::Ptr readFrame(bool &keyFrame, bool &eof);
vector<Track::Ptr> getTracks(bool trackReady) const override ;
uint64_t getDurationMS() const;
private:

View File

@ -47,7 +47,11 @@ MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &
_demuxer = std::make_shared<MP4Demuxer>(strFileName.data());
_mediaMuxer.reset(new MultiMediaSourceMuxer(strVhost, strApp, strId, _demuxer->getDurationMS() / 1000.0, true, true, false, false));
for(auto &track : _demuxer->getTracks(false)){
auto tracks = _demuxer->getTracks(false);
if(tracks.empty()){
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << strFileName);
}
for(auto &track : tracks){
_mediaMuxer->addTrack(track);
if(track->getTrackType() == TrackVideo){
_have_video = true;
@ -59,8 +63,9 @@ MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &
bool MP4Reader::readSample() {
bool eof = false;
bool keyFrame = false;
while (!eof) {
auto frame = _demuxer->readFrame(false, &eof);
auto frame = _demuxer->readFrame(keyFrame, eof);
if (!frame) {
break;
}
@ -139,12 +144,13 @@ bool MP4Reader::seekTo(uint32_t ui32Stamp){
}
//搜索到下一帧关键帧
bool eof = false;
bool keyFrame = false;
while (!eof) {
auto frame = _demuxer->readFrame(false, &eof);
auto frame = _demuxer->readFrame(keyFrame, eof);
if(!frame){
break;
}
if(frame->keyFrame() || frame->configFrame()){
if(keyFrame || frame->keyFrame() || frame->configFrame()){
//定位到key帧
_mediaMuxer->inputFrame(frame);
setNextStampForStop(frame->dts());