兼容无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
onWrite(std::make_shared<BufferRaw>((char *)&size,4));
//metadata
AMFEncoder invoke;
invoke << "onMetaData" << mediaSrc->getMetaData();
onWriteFlvTag(MSG_DATA, std::make_shared<BufferString>(invoke.data()), 0);
auto &metadata = mediaSrc->getMetaData();
if(metadata){
//在有metadata的情况下才发送metadata
//其实metadata没什么用有些推流器不产生metadata
AMFEncoder invoke;
invoke << "onMetaData" << metadata;
onWriteFlvTag(MSG_DATA, std::make_shared<BufferString>(invoke.data()), 0);
}
//config frame
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");
sendReply("onStatus", nullptr, status);
// onMetaData
invoke.clear();
invoke << "onMetaData" << src->getMetaData();
sendResponse(MSG_DATA, invoke.data());
auto &metadata = src->getMetaData();
if(metadata){
//在有metadata的情况下才发送metadata
//其实metadata没什么用有些推流器不产生metadata
// onMetaData
invoke.clear();
invoke << "onMetaData" << metadata;
sendResponse(MSG_DATA, invoke.data());
}
src->getConfigFrame([&](const RtmpPacket::Ptr &pkt) {
//DebugP(this)<<"send initial frame";
@ -423,7 +429,9 @@ void RtmpSession::setMetaData(AMFDecoder &dec) {
if (type != "onMetaData") {
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) {
@ -542,4 +550,16 @@ void RtmpSession::setSocketFlags(){
(*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 */

View File

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