ZLMediaKit/src/Rtmp/FlvMuxer.cpp

192 lines
5.8 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2018-10-25 10:00:17 +08:00
*/
2018-08-30 19:29:54 +08:00
#include "FlvMuxer.h"
2018-10-24 18:09:54 +08:00
#include "Util/File.h"
#include "Rtmp/utils.h"
2018-08-30 19:29:54 +08:00
#define FILE_BUF_SIZE (64 * 1024)
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-08-30 19:29:54 +08:00
2021-07-15 11:16:11 +08:00
void FlvMuxer::start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media) {
if (!media) {
2018-08-30 19:29:54 +08:00
throw std::runtime_error("RtmpMediaSource 无效");
}
2021-07-15 11:16:11 +08:00
if (!poller->isCurrentThread()) {
2019-03-22 14:11:39 +08:00
weak_ptr<FlvMuxer> weakSelf = getSharedPtr();
//延时两秒启动录制目的是为了等待config帧收集完毕
2021-07-15 11:16:11 +08:00
poller->doDelayTask(2000, [weakSelf, poller, media]() {
2019-03-22 14:11:39 +08:00
auto strongSelf = weakSelf.lock();
2021-07-15 11:16:11 +08:00
if (strongSelf) {
strongSelf->start(poller, media);
2019-03-22 14:11:39 +08:00
}
return 0;
2019-03-22 14:11:39 +08:00
});
return;
}
2018-08-30 19:29:54 +08:00
onWriteFlvHeader(media);
std::weak_ptr<FlvMuxer> weakSelf = getSharedPtr();
_ring_reader = media->getRing()->attach(poller);
2021-07-15 11:16:11 +08:00
_ring_reader->setDetachCB([weakSelf]() {
2018-08-30 19:29:54 +08:00
auto strongSelf = weakSelf.lock();
2021-07-15 11:16:11 +08:00
if (!strongSelf) {
2018-08-30 19:29:54 +08:00
return;
}
strongSelf->onDetach();
});
//音频同步于视频
2020-05-15 18:08:54 +08:00
_stamp[0].syncTo(_stamp[1]);
2021-07-15 11:16:11 +08:00
_ring_reader->setReadCB([weakSelf](const RtmpMediaSource::RingDataType &pkt) {
2018-08-30 19:29:54 +08:00
auto strongSelf = weakSelf.lock();
2021-07-15 11:16:11 +08:00
if (!strongSelf) {
2018-08-30 19:29:54 +08:00
return;
}
2020-04-09 16:19:03 +08:00
size_t i = 0;
auto size = pkt->size();
2021-07-15 11:16:11 +08:00
pkt->for_each([&](const RtmpPacket::Ptr &rtmp) {
2020-04-09 16:19:03 +08:00
strongSelf->onWriteRtmp(rtmp, ++i == size);
});
2018-08-30 19:29:54 +08:00
});
}
2021-02-07 14:55:09 +08:00
BufferRaw::Ptr FlvMuxer::obtainBuffer(const void *data, size_t len) {
auto buffer = BufferRaw::create();
buffer->assign((const char *) data, len);
return buffer;
}
void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &src) {
2018-08-30 19:29:54 +08:00
//发送flv文件头
2021-07-15 11:16:11 +08:00
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);
header->have_video = src->haveVideo();
header->have_audio = src->haveAudio();
2018-08-30 19:29:54 +08:00
//flv header
2021-07-15 11:16:11 +08:00
onWrite(buffer, false);
2019-04-08 15:06:40 +08:00
//PreviousTagSize0 Always 0
2021-07-15 11:16:11 +08:00
auto size = htonl(0);
onWrite(obtainBuffer((char *) &size, 4), false);
2019-09-21 19:27:34 +08:00
auto &metadata = src->getMetaData();
2021-07-15 11:16:11 +08:00
if (metadata) {
2019-09-21 19:27:34 +08:00
//在有metadata的情况下才发送metadata
//其实metadata没什么用有些推流器不产生metadata
AMFEncoder invoke;
invoke << "onMetaData" << metadata;
2020-04-09 16:19:03 +08:00
onWriteFlvTag(MSG_DATA, std::make_shared<BufferString>(invoke.data()), 0, false);
2019-09-21 19:27:34 +08:00
}
2019-04-08 15:06:40 +08:00
2018-08-30 19:29:54 +08:00
//config frame
src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) {
2020-04-09 16:19:03 +08:00
onWriteRtmp(pkt, true);
2018-08-30 19:29:54 +08:00
});
}
2021-07-15 11:16:11 +08:00
void FlvMuxer::onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp, bool flush) {
2020-08-30 10:48:34 +08:00
onWriteFlvTag(pkt->type_id, pkt, time_stamp, flush);
2018-08-30 19:29:54 +08:00
}
2020-08-30 10:48:34 +08:00
void FlvMuxer::onWriteFlvTag(uint8_t type, const Buffer::Ptr &buffer, uint32_t time_stamp, bool flush) {
2018-08-30 19:29:54 +08:00
RtmpTagHeader header;
2020-08-30 10:48:34 +08:00
header.type = type;
2021-07-15 11:16:11 +08:00
set_be24(header.data_size, (uint32_t) buffer->size());
header.timestamp_ex = (time_stamp >> 24) & 0xff;
2020-08-30 10:48:34 +08:00
set_be24(header.timestamp, time_stamp & 0xFFFFFF);
2021-07-15 11:16:11 +08:00
2019-04-08 15:06:40 +08:00
//tag header
2021-07-15 11:16:11 +08:00
onWrite(obtainBuffer((char *) &header, sizeof(header)), false);
2019-04-08 15:06:40 +08:00
//tag data
2020-04-09 16:19:03 +08:00
onWrite(buffer, false);
2021-07-15 11:16:11 +08:00
2019-04-08 15:06:40 +08:00
//PreviousTagSize
2021-07-15 11:16:11 +08:00
uint32_t size = htonl((uint32_t) (buffer->size() + sizeof(header)));
onWrite(obtainBuffer((char *) &size, 4), flush);
2018-08-30 19:29:54 +08:00
}
2021-07-15 11:16:11 +08:00
void FlvMuxer::onWriteRtmp(const RtmpPacket::Ptr &pkt, bool flush) {
2019-08-27 11:34:50 +08:00
int64_t dts_out;
2020-08-30 10:48:34 +08:00
_stamp[pkt->type_id % 2].revise(pkt->time_stamp, 0, dts_out, dts_out);
2021-07-15 11:16:11 +08:00
onWriteFlvTag(pkt, (uint32_t) dts_out, flush);
2018-08-30 19:29:54 +08:00
}
void FlvMuxer::stop() {
2021-07-15 11:16:11 +08:00
if (_ring_reader) {
2018-08-30 19:29:54 +08:00
_ring_reader.reset();
onDetach();
}
}
///////////////////////////////////////////////////////FlvRecorder/////////////////////////////////////////////////////
2021-07-15 11:16:11 +08:00
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);
2018-08-30 19:29:54 +08:00
}
2021-07-15 11:16:11 +08:00
void FlvRecorder::startRecord(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media,
const string &file_path) {
2018-08-31 17:11:39 +08:00
stop();
lock_guard<recursive_mutex> lck(_file_mtx);
2018-08-30 19:29:54 +08:00
//开辟文件写缓存
2021-07-15 11:16:11 +08:00
std::shared_ptr<char> fileBuf(new char[FILE_BUF_SIZE], [](char *ptr) {
if (ptr) {
delete[] ptr;
2018-08-30 19:29:54 +08:00
}
});
//新建文件
2021-07-15 11:16:11 +08:00
_file.reset(File::create_file(file_path.data(), "wb"), [fileBuf](FILE *fp) {
if (fp) {
2018-08-30 19:29:54 +08:00
fflush(fp);
fclose(fp);
}
});
2021-07-15 11:16:11 +08:00
if (!_file) {
throw std::runtime_error(StrPrinter << "打开文件失败:" << file_path);
2018-08-30 19:29:54 +08:00
}
//设置文件写缓存
2021-07-15 11:16:11 +08:00
setvbuf(_file.get(), fileBuf.get(), _IOFBF, FILE_BUF_SIZE);
start(poller, media);
2018-08-30 19:29:54 +08:00
}
2020-04-09 16:19:03 +08:00
void FlvRecorder::onWrite(const Buffer::Ptr &data, bool flush) {
2018-08-30 19:29:54 +08:00
lock_guard<recursive_mutex> lck(_file_mtx);
2021-07-15 11:16:11 +08:00
if (_file) {
fwrite(data->data(), data->size(), 1, _file.get());
2018-08-30 19:29:54 +08:00
}
}
void FlvRecorder::onDetach() {
lock_guard<recursive_mutex> lck(_file_mtx);
_file.reset();
}
std::shared_ptr<FlvMuxer> FlvRecorder::getSharedPtr() {
2021-07-15 11:16:11 +08:00
return shared_from_this();
}
2018-10-24 17:17:55 +08:00
}//namespace mediakit