ZLMediaKit/src/Rtmp/FlvMuxer.cpp

222 lines
6.3 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
FlvMuxer::FlvMuxer() {
}
FlvMuxer::~FlvMuxer() {
}
void FlvMuxer::start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media) {
2018-08-30 19:29:54 +08:00
if(!media){
throw std::runtime_error("RtmpMediaSource 无效");
}
2019-03-22 14:11:39 +08:00
if(!poller->isCurrentThread()){
weak_ptr<FlvMuxer> weakSelf = getSharedPtr();
//延时两秒启动录制目的是为了等待config帧收集完毕
poller->doDelayTask(2000,[weakSelf,poller,media](){
2019-03-22 14:11:39 +08:00
auto strongSelf = weakSelf.lock();
if(strongSelf){
strongSelf->start(poller,media);
}
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);
2018-08-30 19:29:54 +08:00
_ring_reader->setDetachCB([weakSelf](){
auto strongSelf = weakSelf.lock();
if(!strongSelf){
return;
}
strongSelf->onDetach();
});
//音频同步于视频
2020-05-15 18:08:54 +08:00
_stamp[0].syncTo(_stamp[1]);
2020-04-09 16:19:03 +08:00
_ring_reader->setReadCB([weakSelf](const RtmpMediaSource::RingDataType &pkt){
2018-08-30 19:29:54 +08:00
auto strongSelf = weakSelf.lock();
if(!strongSelf){
return;
}
2020-04-09 16:19:03 +08:00
size_t i = 0;
auto size = pkt->size();
2020-04-09 16:19:03 +08:00
pkt->for_each([&](const RtmpPacket::Ptr &rtmp){
strongSelf->onWriteRtmp(rtmp, ++i == size);
});
2018-08-30 19:29:54 +08:00
});
}
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;
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){
2020-08-30 10:48:34 +08:00
if(pkt->type_id == MSG_VIDEO){
2018-08-30 19:29:54 +08:00
is_have_video = true;
}
2020-08-30 10:48:34 +08:00
if(pkt->type_id == MSG_AUDIO){
2018-08-30 19:29:54 +08:00
is_have_audio = true;
}
});
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
2020-04-09 16:19:03 +08:00
onWrite(std::make_shared<BufferRaw>(flv_file_header, sizeof(flv_file_header) - 1), false);
2019-04-08 15:06:40 +08:00
auto size = htonl(0);
//PreviousTagSize0 Always 0
2020-04-09 16:19:03 +08:00
onWrite(std::make_shared<BufferRaw>((char *)&size,4), false);
2019-04-08 15:06:40 +08:00
2019-09-21 19:27:34 +08:00
auto &metadata = mediaSrc->getMetaData();
if(metadata){
//在有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
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){
2020-04-09 16:19:03 +08:00
onWriteRtmp(pkt, true);
2018-08-30 19:29:54 +08:00
});
}
#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)
2020-08-30 10:48:34 +08:00
void FlvMuxer::onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp , bool flush) {
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;
set_be24(header.data_size, (uint32_t)buffer->size());
2020-08-30 10:48:34 +08:00
header.timestamp_ex = (uint8_t) ((time_stamp >> 24) & 0xff);
set_be24(header.timestamp, time_stamp & 0xFFFFFF);
2019-04-08 15:06:40 +08:00
//tag header
2020-04-09 16:19:03 +08:00
onWrite(std::make_shared<BufferRaw>((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);
uint32_t size = htonl((uint32_t)(buffer->size() + sizeof(header)));
2019-04-08 15:06:40 +08:00
//PreviousTagSize
2020-04-09 16:19:03 +08:00
onWrite(std::make_shared<BufferRaw>((char *)&size,4), flush);
2018-08-30 19:29:54 +08:00
}
2020-04-09 16:19:03 +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);
onWriteFlvTag(pkt, (uint32_t)dts_out,flush);
2018-08-30 19:29:54 +08:00
}
void FlvMuxer::stop() {
if(_ring_reader){
_ring_reader.reset();
onDetach();
}
}
///////////////////////////////////////////////////////FlvRecorder/////////////////////////////////////////////////////
void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const string &vhost, const string &app, const string &stream,const string &file_path) {
2020-05-26 10:11:58 +08:00
startRecord(poller,dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,vhost,app,stream)),file_path);
2018-08-30 19:29:54 +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
//开辟文件写缓存
std::shared_ptr<char> fileBuf(new char[FILE_BUF_SIZE],[](char *ptr){
if(ptr){
delete [] ptr;
}
});
//新建文件
2020-04-24 12:39:22 +08:00
_file.reset(File::create_file(file_path.data(), "wb"), [fileBuf](FILE *fp){
2018-08-30 19:29:54 +08:00
if(fp){
fflush(fp);
fclose(fp);
}
});
if (!_file){
throw std::runtime_error( StrPrinter << "打开文件失败:" << file_path);
}
//设置文件写缓存
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);
if(_file){
fwrite(data->data(),data->size(),1,_file.get());
}
}
void FlvRecorder::onDetach() {
lock_guard<recursive_mutex> lck(_file_mtx);
_file.reset();
}
std::shared_ptr<FlvMuxer> FlvRecorder::getSharedPtr() {
return shared_from_this();
}
FlvRecorder::FlvRecorder() {
}
FlvRecorder::~FlvRecorder() {
}
2018-08-30 19:29:54 +08:00
2018-10-24 17:17:55 +08:00
}//namespace mediakit