兼容无metadata的rtmp流

This commit is contained in:
xiongziliang 2019-09-21 19:27:34 +08:00
parent 9d43c38e09
commit 8da9aee11a
3 changed files with 35 additions and 9 deletions

View File

@ -106,10 +106,15 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
//PreviousTagSize0 Always 0 //PreviousTagSize0 Always 0
onWrite(std::make_shared<BufferRaw>((char *)&size,4)); onWrite(std::make_shared<BufferRaw>((char *)&size,4));
//metadata
AMFEncoder invoke; auto &metadata = mediaSrc->getMetaData();
invoke << "onMetaData" << mediaSrc->getMetaData(); if(metadata){
onWriteFlvTag(MSG_DATA, std::make_shared<BufferString>(invoke.data()), 0); //在有metadata的情况下才发送metadata
//其实metadata没什么用有些推流器不产生metadata
AMFEncoder invoke;
invoke << "onMetaData" << metadata;
onWriteFlvTag(MSG_DATA, std::make_shared<BufferString>(invoke.data()), 0);
}
//config frame //config frame
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){ mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){

View File

@ -262,10 +262,16 @@ void RtmpSession::sendPlayResponse(const string &err,const RtmpMediaSource::Ptr
status.set("clientid", "0"); status.set("clientid", "0");
sendReply("onStatus", nullptr, status); sendReply("onStatus", nullptr, status);
// onMetaData auto &metadata = src->getMetaData();
invoke.clear(); if(metadata){
invoke << "onMetaData" << src->getMetaData(); //在有metadata的情况下才发送metadata
sendResponse(MSG_DATA, invoke.data()); //其实metadata没什么用有些推流器不产生metadata
// onMetaData
invoke.clear();
invoke << "onMetaData" << metadata;
sendResponse(MSG_DATA, invoke.data());
}
src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) { src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) {
//DebugP(this)<<"send initial frame"; //DebugP(this)<<"send initial frame";
@ -423,7 +429,9 @@ void RtmpSession::setMetaData(AMFDecoder &dec) {
if (type != "onMetaData") { if (type != "onMetaData") {
throw std::runtime_error("can only set metadata"); throw std::runtime_error("can only set metadata");
} }
_pPublisherSrc->onGetMetaData(dec.load<AMFValue>()); auto metadata = dec.load<AMFValue>();
//dumpMetadata(metadata);
_pPublisherSrc->onGetMetaData(metadata);
} }
void RtmpSession::onProcessCmd(AMFDecoder &dec) { void RtmpSession::onProcessCmd(AMFDecoder &dec) {
@ -542,4 +550,16 @@ void RtmpSession::setSocketFlags(){
(*this) << SocketFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE); (*this) << SocketFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE);
} }
} }
void RtmpSession::dumpMetadata(const AMFValue &metadata) {
if(metadata.type() != AMF_OBJECT && metadata.type() != AMF_ECMA_ARRAY){
WarnL << "invalid metedata type:" << metadata.type();
return ;
}
_StrPrinter printer;
metadata.object_for_each([&](const string &key, const AMFValue &val){
printer << "\r\n" << key << "\t:" << val.to_string() ;
});
InfoL << _mediaInfo._vhost << " " << _mediaInfo._app << " " << _mediaInfo._streamid << (string)printer;
}
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -87,6 +87,7 @@ private:
void onNoneReader(MediaSource &sender) override; void onNoneReader(MediaSource &sender) override;
void setSocketFlags(); void setSocketFlags();
string getStreamId(const string &str); string getStreamId(const string &str);
void dumpMetadata(const AMFValue &metedata);
private: private:
std::string _strTcUrl; std::string _strTcUrl;
MediaInfo _mediaInfo; MediaInfo _mediaInfo;