From c8f00cefa0161dfecb92d67ecee859c2db24026c Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 15 Jul 2021 11:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=8E=B7=E5=8F=96flv?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=8C=85=E5=90=AB=E9=9F=B3=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtmp/FlvMuxer.cpp | 17 +++++------------ src/Rtmp/FlvMuxer.h | 2 +- src/Rtmp/RtmpMediaSource.h | 13 ++++++++++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Rtmp/FlvMuxer.cpp b/src/Rtmp/FlvMuxer.cpp index 49cc349b..ab105a28 100644 --- a/src/Rtmp/FlvMuxer.cpp +++ b/src/Rtmp/FlvMuxer.cpp @@ -67,7 +67,7 @@ BufferRaw::Ptr FlvMuxer::obtainBuffer(const void *data, size_t len) { return buffer; } -void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) { +void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &src) { //发送flv文件头 auto buffer = BufferRaw::create(); buffer->setCapacity(sizeof(FLVHeader)); @@ -80,15 +80,8 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) { header->flv[2] = 'V'; header->version = 1; header->length = htonl(9); - - mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt) { - if (pkt->type_id == MSG_VIDEO) { - header->have_video = 1; - } - if (pkt->type_id == MSG_AUDIO) { - header->have_audio = 1; - } - }); + header->have_video = src->haveVideo(); + header->have_audio = src->haveAudio(); //flv header onWrite(buffer, false); @@ -97,7 +90,7 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) { auto size = htonl(0); onWrite(obtainBuffer((char *) &size, 4), false); - auto &metadata = mediaSrc->getMetaData(); + auto &metadata = src->getMetaData(); if (metadata) { //在有metadata的情况下才发送metadata //其实metadata没什么用,有些推流器不产生metadata @@ -107,7 +100,7 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) { } //config frame - mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt) { + src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) { onWriteRtmp(pkt, true); }); } diff --git a/src/Rtmp/FlvMuxer.h b/src/Rtmp/FlvMuxer.h index d551e4da..2f4a389c 100644 --- a/src/Rtmp/FlvMuxer.h +++ b/src/Rtmp/FlvMuxer.h @@ -34,7 +34,7 @@ protected: virtual std::shared_ptr getSharedPtr() = 0; private: - void onWriteFlvHeader(const RtmpMediaSource::Ptr &media); + void onWriteFlvHeader(const RtmpMediaSource::Ptr &src); void onWriteRtmp(const RtmpPacket::Ptr &pkt, 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); diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index 045ee046..c8c19cbf 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -102,6 +102,8 @@ public: virtual void setMetaData(const AMFValue &metadata) { _metadata = metadata; _metadata.set("server", SERVER_NAME); + _have_video = _metadata["videocodecid"]; + _have_audio = _metadata["audiocodecid"]; if (_ring) { regist(); } @@ -125,7 +127,7 @@ public: //保存当前时间戳 switch (pkt->type_id) { 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; } @@ -187,6 +189,14 @@ public: _ring->clearCache(); } + bool haveVideo() const { + return _have_video; + } + + bool haveAudio() const { + return _have_audio; + } + private: /** * 批量flush rtmp包时触发该函数 @@ -200,6 +210,7 @@ private: private: bool _have_video = false; + bool _have_audio = false; int _ring_size; uint32_t _track_stamps[TrackMax] = {0}; AMFValue _metadata;