修复flv PreviousTagSize相关问题

This commit is contained in:
xiongziliang 2019-04-08 15:06:40 +08:00
parent a638c029dd
commit c4d14ec415
4 changed files with 19 additions and 40 deletions

View File

@ -691,14 +691,6 @@ void HttpSession::onWrite(const Buffer::Ptr &buffer) {
send(buffer);
}
void HttpSession::onWrite(const char *data, int len) {
BufferRaw::Ptr buffer(new BufferRaw);
buffer->assign(data,len);
_ticker.resetTime();
_ui64TotalBytes += buffer->size();
send(buffer);
}
void HttpSession::onDetach() {
shutdown();
}

View File

@ -62,7 +62,6 @@ public:
protected:
//FlvMuxer override
void onWrite(const Buffer::Ptr &data) override ;
void onWrite(const char *data,int len) override;
void onDetach() override;
std::shared_ptr<FlvMuxer> getSharedPtr() override;
//HttpRequestSplitter override

View File

@ -74,7 +74,6 @@ void FlvMuxer::start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &
}
void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
_previousTagSize = 0;
CLEAR_ARR(_aui32FirstStamp);
//发送flv文件头
@ -101,11 +100,17 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
}
//flv header
onWrite(flv_file_header, sizeof(flv_file_header) - 1);
onWrite(std::make_shared<BufferRaw>(flv_file_header, sizeof(flv_file_header) - 1));
auto size = htonl(0);
//PreviousTagSize0 Always 0
onWrite(std::make_shared<BufferRaw>((char *)&size,4));
//metadata
AMFEncoder invoke;
invoke << "onMetaData" << mediaSrc->getMetaData();
onWriteFlvTag(MSG_DATA, invoke.data(), 0);
onWriteFlvTag(MSG_DATA, std::make_shared<BufferString>(invoke.data()), 0);
//config frame
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){
onWriteRtmp(pkt);
@ -132,29 +137,22 @@ public:
#endif // defined(_WIN32)
void FlvMuxer::onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp) {
auto size = htonl(_previousTagSize);
onWrite((char *)&size,4);//onWrite PreviousTagSize
RtmpTagHeader header;
header.type = pkt->typeId;
set_be24(header.data_size, pkt->strBuf.size());
header.timestamp_ex = (uint8_t) ((ui32TimeStamp >> 24) & 0xff);
set_be24(header.timestamp,ui32TimeStamp & 0xFFFFFF);
onWrite((char *)&header, sizeof(header));//onWrite tag header
onWrite(pkt);//onWrite tag data
_previousTagSize += (pkt->strBuf.size() + sizeof(header));
onWriteFlvTag(pkt->typeId,pkt,ui32TimeStamp);
}
void FlvMuxer::onWriteFlvTag(uint8_t ui8Type, const std::string &strBuf, uint32_t ui32TimeStamp) {
auto size = htonl(_previousTagSize);
onWrite((char *)&size,4);//onWrite PreviousTagSize
void FlvMuxer::onWriteFlvTag(uint8_t ui8Type, const Buffer::Ptr &buffer, uint32_t ui32TimeStamp) {
RtmpTagHeader header;
header.type = ui8Type;
set_be24(header.data_size, strBuf.size());
set_be24(header.data_size, buffer->size());
header.timestamp_ex = (uint8_t) ((ui32TimeStamp >> 24) & 0xff);
set_be24(header.timestamp,ui32TimeStamp & 0xFFFFFF);
onWrite((char *)&header, sizeof(header));//onWrite tag header
onWrite(std::make_shared<BufferString>(strBuf));//onWrite tag data
_previousTagSize += (strBuf.size() + sizeof(header));
//tag header
onWrite(std::make_shared<BufferRaw>((char *)&header, sizeof(header)));
//tag data
onWrite(buffer);
auto size = htonl((buffer->size() + sizeof(header)));
//PreviousTagSize
onWrite(std::make_shared<BufferRaw>((char *)&size,4));
}
void FlvMuxer::onWriteRtmp(const RtmpPacket::Ptr &pkt) {
@ -218,13 +216,6 @@ void FlvRecorder::onWrite(const Buffer::Ptr &data) {
}
}
void FlvRecorder::onWrite(const char *data, int len) {
lock_guard<recursive_mutex> lck(_file_mtx);
if(_file){
fwrite(data,len,1,_file.get());
}
}
void FlvRecorder::onDetach() {
lock_guard<recursive_mutex> lck(_file_mtx);
_file.reset();

View File

@ -43,18 +43,16 @@ public:
protected:
void start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media);
virtual void onWrite(const Buffer::Ptr &data) = 0;
virtual void onWrite(const char *data,int len) = 0;
virtual void onDetach() = 0;
virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
private:
void onWriteFlvHeader(const RtmpMediaSource::Ptr &media);
void onWriteRtmp(const RtmpPacket::Ptr &pkt);
void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp);
void onWriteFlvTag(uint8_t ui8Type, const std::string &strBuf, uint32_t ui32TimeStamp);
void onWriteFlvTag(uint8_t ui8Type, const Buffer::Ptr &buffer, uint32_t ui32TimeStamp);
private:
RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
uint32_t _aui32FirstStamp[2] = {0};
uint32_t _previousTagSize = 0;
};
@ -67,7 +65,6 @@ public:
void startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media,const string &file_path);
private:
virtual void onWrite(const Buffer::Ptr &data) override ;
virtual void onWrite(const char *data,int len) override;
virtual void onDetach() override;
virtual std::shared_ptr<FlvMuxer> getSharedPtr() override;
private: