完善获取flv是否包含音视频相关逻辑

This commit is contained in:
ziyue 2021-07-15 11:29:21 +08:00
parent fe42ea30fc
commit c8f00cefa0
3 changed files with 18 additions and 14 deletions

View File

@ -67,7 +67,7 @@ BufferRaw::Ptr FlvMuxer::obtainBuffer(const void *data, size_t len) {
return buffer; return buffer;
} }
void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) { void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &src) {
//发送flv文件头 //发送flv文件头
auto buffer = BufferRaw::create(); auto buffer = BufferRaw::create();
buffer->setCapacity(sizeof(FLVHeader)); buffer->setCapacity(sizeof(FLVHeader));
@ -80,15 +80,8 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
header->flv[2] = 'V'; header->flv[2] = 'V';
header->version = 1; header->version = 1;
header->length = htonl(9); header->length = htonl(9);
header->have_video = src->haveVideo();
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt) { header->have_audio = src->haveAudio();
if (pkt->type_id == MSG_VIDEO) {
header->have_video = 1;
}
if (pkt->type_id == MSG_AUDIO) {
header->have_audio = 1;
}
});
//flv header //flv header
onWrite(buffer, false); onWrite(buffer, false);
@ -97,7 +90,7 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
auto size = htonl(0); auto size = htonl(0);
onWrite(obtainBuffer((char *) &size, 4), false); onWrite(obtainBuffer((char *) &size, 4), false);
auto &metadata = mediaSrc->getMetaData(); auto &metadata = src->getMetaData();
if (metadata) { if (metadata) {
//在有metadata的情况下才发送metadata //在有metadata的情况下才发送metadata
//其实metadata没什么用有些推流器不产生metadata //其实metadata没什么用有些推流器不产生metadata
@ -107,7 +100,7 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
} }
//config frame //config frame
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt) { src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) {
onWriteRtmp(pkt, true); onWriteRtmp(pkt, true);
}); });
} }

View File

@ -34,7 +34,7 @@ protected:
virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0; virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
private: private:
void onWriteFlvHeader(const RtmpMediaSource::Ptr &media); void onWriteFlvHeader(const RtmpMediaSource::Ptr &src);
void onWriteRtmp(const RtmpPacket::Ptr &pkt, bool flush); void onWriteRtmp(const RtmpPacket::Ptr &pkt, bool flush);
void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp, bool flush); void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp, bool flush);
void onWriteFlvTag(uint8_t type, const Buffer::Ptr &buffer, uint32_t time_stamp, bool flush); void onWriteFlvTag(uint8_t type, const Buffer::Ptr &buffer, uint32_t time_stamp, bool flush);

View File

@ -102,6 +102,8 @@ public:
virtual void setMetaData(const AMFValue &metadata) { virtual void setMetaData(const AMFValue &metadata) {
_metadata = metadata; _metadata = metadata;
_metadata.set("server", SERVER_NAME); _metadata.set("server", SERVER_NAME);
_have_video = _metadata["videocodecid"];
_have_audio = _metadata["audiocodecid"];
if (_ring) { if (_ring) {
regist(); regist();
} }
@ -125,7 +127,7 @@ public:
//保存当前时间戳 //保存当前时间戳
switch (pkt->type_id) { switch (pkt->type_id) {
case MSG_VIDEO : _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break; case MSG_VIDEO : _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break;
case MSG_AUDIO : _track_stamps[TrackAudio] = pkt->time_stamp; break; case MSG_AUDIO : _track_stamps[TrackAudio] = pkt->time_stamp, _have_audio = true; break;
default : break; default : break;
} }
@ -187,6 +189,14 @@ public:
_ring->clearCache(); _ring->clearCache();
} }
bool haveVideo() const {
return _have_video;
}
bool haveAudio() const {
return _have_audio;
}
private: private:
/** /**
* flush rtmp包时触发该函数 * flush rtmp包时触发该函数
@ -200,6 +210,7 @@ private:
private: private:
bool _have_video = false; bool _have_video = false;
bool _have_audio = false;
int _ring_size; int _ring_size;
uint32_t _track_stamps[TrackMax] = {0}; uint32_t _track_stamps[TrackMax] = {0};
AMFValue _metadata; AMFValue _metadata;