mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 04:31:37 +08:00
整理flv复用器
This commit is contained in:
parent
0d48275115
commit
fe42ea30fc
@ -16,12 +16,6 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
|
||||
FlvMuxer::FlvMuxer() {
|
||||
}
|
||||
FlvMuxer::~FlvMuxer() {
|
||||
}
|
||||
|
||||
void FlvMuxer::start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media) {
|
||||
if (!media) {
|
||||
throw std::runtime_error("RtmpMediaSource 无效");
|
||||
@ -75,36 +69,34 @@ BufferRaw::Ptr FlvMuxer::obtainBuffer(const void *data, size_t len) {
|
||||
|
||||
void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
|
||||
//发送flv文件头
|
||||
char flv_file_header[] = "FLV\x1\x5\x0\x0\x0\x9"; // have audio and have video
|
||||
bool is_have_audio = false,is_have_video = false;
|
||||
auto buffer = BufferRaw::create();
|
||||
buffer->setCapacity(sizeof(FLVHeader));
|
||||
buffer->setSize(sizeof(FLVHeader));
|
||||
|
||||
FLVHeader *header = (FLVHeader *) buffer->data();
|
||||
memset(header, 0, sizeof(FLVHeader));
|
||||
header->flv[0] = 'F';
|
||||
header->flv[1] = 'L';
|
||||
header->flv[2] = 'V';
|
||||
header->version = 1;
|
||||
header->length = htonl(9);
|
||||
|
||||
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->type_id == MSG_VIDEO) {
|
||||
is_have_video = true;
|
||||
header->have_video = 1;
|
||||
}
|
||||
if (pkt->type_id == MSG_AUDIO) {
|
||||
is_have_audio = true;
|
||||
header->have_audio = 1;
|
||||
}
|
||||
});
|
||||
|
||||
if (is_have_audio && is_have_video) {
|
||||
flv_file_header[4] = 0x05;
|
||||
} else if (is_have_audio && !is_have_video) {
|
||||
flv_file_header[4] = 0x04;
|
||||
} else if (!is_have_audio && is_have_video) {
|
||||
flv_file_header[4] = 0x01;
|
||||
} else {
|
||||
flv_file_header[4] = 0x00;
|
||||
}
|
||||
|
||||
//flv header
|
||||
onWrite(obtainBuffer(flv_file_header, sizeof(flv_file_header) - 1), false);
|
||||
onWrite(buffer, false);
|
||||
|
||||
auto size = htonl(0);
|
||||
//PreviousTagSize0 Always 0
|
||||
auto size = htonl(0);
|
||||
onWrite(obtainBuffer((char *) &size, 4), false);
|
||||
|
||||
|
||||
auto &metadata = mediaSrc->getMetaData();
|
||||
if (metadata) {
|
||||
//在有metadata的情况下才发送metadata
|
||||
@ -120,25 +112,6 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(push, 1)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
class RtmpTagHeader {
|
||||
public:
|
||||
uint8_t type = 0;
|
||||
uint8_t data_size[3] = {0};
|
||||
uint8_t timestamp[3] = {0};
|
||||
uint8_t timestamp_ex = 0;
|
||||
uint8_t streamid[3] = {0}; /* Always 0. */
|
||||
}PACKED;
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(pop)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
void FlvMuxer::onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp, bool flush) {
|
||||
onWriteFlvTag(pkt->type_id, pkt, time_stamp, flush);
|
||||
}
|
||||
@ -147,14 +120,17 @@ void FlvMuxer::onWriteFlvTag(uint8_t type, const Buffer::Ptr &buffer, uint32_t t
|
||||
RtmpTagHeader header;
|
||||
header.type = type;
|
||||
set_be24(header.data_size, (uint32_t) buffer->size());
|
||||
header.timestamp_ex = (uint8_t) ((time_stamp >> 24) & 0xff);
|
||||
header.timestamp_ex = (time_stamp >> 24) & 0xff;
|
||||
set_be24(header.timestamp, time_stamp & 0xFFFFFF);
|
||||
|
||||
//tag header
|
||||
onWrite(obtainBuffer((char *) &header, sizeof(header)), false);
|
||||
|
||||
//tag data
|
||||
onWrite(buffer, false);
|
||||
uint32_t size = htonl((uint32_t)(buffer->size() + sizeof(header)));
|
||||
|
||||
//PreviousTagSize
|
||||
uint32_t size = htonl((uint32_t) (buffer->size() + sizeof(header)));
|
||||
onWrite(obtainBuffer((char *) &size, 4), flush);
|
||||
}
|
||||
|
||||
@ -172,11 +148,13 @@ void FlvMuxer::stop() {
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////FlvRecorder/////////////////////////////////////////////////////
|
||||
|
||||
void FlvRecorder::startRecord(const EventPoller::Ptr &poller, const string &vhost, const string &app, const string &stream, const string &file_path) {
|
||||
startRecord(poller, dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA, vhost, app, stream)), file_path);
|
||||
}
|
||||
|
||||
void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media, const string &file_path) {
|
||||
void FlvRecorder::startRecord(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media,
|
||||
const string &file_path) {
|
||||
stop();
|
||||
lock_guard<recursive_mutex> lck(_file_mtx);
|
||||
//开辟文件写缓存
|
||||
@ -217,11 +195,4 @@ std::shared_ptr<FlvMuxer> FlvRecorder::getSharedPtr() {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
FlvRecorder::FlvRecorder() {
|
||||
}
|
||||
|
||||
FlvRecorder::~FlvRecorder() {
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
@ -21,9 +21,10 @@ namespace mediakit {
|
||||
|
||||
class FlvMuxer {
|
||||
public:
|
||||
typedef std::shared_ptr<FlvMuxer> Ptr;
|
||||
FlvMuxer();
|
||||
virtual ~FlvMuxer();
|
||||
using Ptr = std::shared_ptr<FlvMuxer>;
|
||||
FlvMuxer() = default;
|
||||
virtual ~FlvMuxer() = default;
|
||||
|
||||
void stop();
|
||||
|
||||
protected:
|
||||
@ -47,9 +48,10 @@ private:
|
||||
|
||||
class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
|
||||
public:
|
||||
typedef std::shared_ptr<FlvRecorder> Ptr;
|
||||
FlvRecorder();
|
||||
virtual ~FlvRecorder();
|
||||
using Ptr = std::shared_ptr<FlvRecorder>;
|
||||
FlvRecorder() = default;
|
||||
~FlvRecorder() override = default;
|
||||
|
||||
void startRecord(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media, const string &file_path);
|
||||
void startRecord(const EventPoller::Ptr &poller, const string &vhost, const string &app, const string &stream, const string &file_path);
|
||||
|
||||
@ -63,6 +65,5 @@ private:
|
||||
recursive_mutex _file_mtx;
|
||||
};
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
#endif //ZLMEDIAKIT_FLVMUXER_H
|
||||
|
@ -129,6 +129,44 @@ public:
|
||||
uint8_t stream_index[4]; /* Note, this is little-endian while others are BE */
|
||||
}PACKED;
|
||||
|
||||
class FLVHeader {
|
||||
public:
|
||||
//FLV
|
||||
char flv[3];
|
||||
//File version (for example, 0x01 for FLV version 1)
|
||||
uint8_t version;
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
//保留,置0
|
||||
uint8_t : 5;
|
||||
//是否有音频
|
||||
uint8_t have_audio: 1;
|
||||
//保留,置0
|
||||
uint8_t : 1;
|
||||
//是否有视频
|
||||
uint8_t have_video: 1;
|
||||
#else
|
||||
//是否有视频
|
||||
uint8_t have_video: 1;
|
||||
//保留,置0
|
||||
uint8_t : 1;
|
||||
//是否有音频
|
||||
uint8_t have_audio: 1;
|
||||
//保留,置0
|
||||
uint8_t : 5;
|
||||
#endif
|
||||
//The length of this header in bytes,固定为9
|
||||
uint32_t length;
|
||||
} PACKED;
|
||||
|
||||
class RtmpTagHeader {
|
||||
public:
|
||||
uint8_t type = 0;
|
||||
uint8_t data_size[3] = {0};
|
||||
uint8_t timestamp[3] = {0};
|
||||
uint8_t timestamp_ex = 0;
|
||||
uint8_t streamid[3] = {0}; /* Always 0. */
|
||||
} PACKED;
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(pop)
|
||||
#endif // defined(_WIN32)
|
||||
|
Loading…
Reference in New Issue
Block a user