修复一些mp4点播不支持的问题

This commit is contained in:
xiongziliang 2020-04-13 09:44:05 +08:00
parent 048b30c41c
commit d363871601
4 changed files with 19 additions and 12 deletions

@ -1 +1 @@
Subproject commit 97cf5e47a5af1ff3d4d187f3ebffd9254595df75 Subproject commit 24519a594c2c634b21fbe09fad28d54c4eba0885

View File

@ -142,7 +142,9 @@ struct Context{
BufferRaw::Ptr buffer; BufferRaw::Ptr buffer;
}; };
Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame) { Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
keyFrame = false;
eof = false;
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) { 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; Context *ctx = (Context *) param;
ctx->pts = pts; ctx->pts = pts;
@ -162,19 +164,23 @@ Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame) {
Context ctx = {this, 0}; Context ctx = {this, 0};
auto ret = mov_reader_read2(_mov_reader.get(), mov_onalloc, mov_reader_onread, &ctx); auto ret = mov_reader_read2(_mov_reader.get(), mov_onalloc, mov_reader_onread, &ctx);
switch (ret) { switch (ret) {
case 0 : case 0 : {
eof = true;
return nullptr; return nullptr;
}
case 1 : { case 1 : {
keyFrame = ctx.flags & MOV_AV_FLAG_KEYFREAME; keyFrame = ctx.flags & MOV_AV_FLAG_KEYFREAME;
return makeFrame(ctx.track_id, ctx.buffer, ctx.pts, ctx.dts); return makeFrame(ctx.track_id, ctx.buffer, ctx.pts, ctx.dts);
} }
default: default : {
eof = true;
WarnL << "读取mp4文件数据失败:" << ret; WarnL << "读取mp4文件数据失败:" << ret;
return nullptr; return nullptr;
} }
} }
}
template <typename Parent> template <typename Parent>
class FrameWrapper : public Parent{ class FrameWrapper : public Parent{

View File

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

View File

@ -48,11 +48,10 @@ MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &
bool MP4Reader::readSample() { bool MP4Reader::readSample() {
bool keyFrame = false; bool keyFrame = false;
bool eof = false; bool eof = false;
while (true) { while (!eof) {
auto frame = _demuxer->readFrame(keyFrame); auto frame = _demuxer->readFrame(keyFrame, eof);
if (!frame) { if (!frame) {
eof = true; continue;
break;
} }
_mediaMuxer->inputFrame(frame); _mediaMuxer->inputFrame(frame);
if (frame->dts() > getCurrentStamp()) { if (frame->dts() > getCurrentStamp()) {
@ -122,11 +121,12 @@ bool MP4Reader::seekTo(uint32_t ui32Stamp){
} }
//搜索到下一帧关键帧 //搜索到下一帧关键帧
bool keyFrame = false; bool keyFrame = false;
while (true) { bool eof = false;
auto frame = _demuxer->readFrame(keyFrame); while (!eof) {
auto frame = _demuxer->readFrame(keyFrame, eof);
if(!frame){ if(!frame){
//文件读完了都未找到下一帧关键帧 //文件读完了都未找到下一帧关键帧
return false; continue;
} }
if(keyFrame || frame->keyFrame() || frame->configFrame()){ if(keyFrame || frame->keyFrame() || frame->configFrame()){
//定位到key帧 //定位到key帧
@ -136,6 +136,7 @@ bool MP4Reader::seekTo(uint32_t ui32Stamp){
return true; return true;
} }
} }
return false;
} }
bool MP4Reader::close(MediaSource &sender,bool force){ bool MP4Reader::close(MediaSource &sender,bool force){