2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_RTMP_RTMPMEDIASOURCE_H_
|
|
|
|
|
#define SRC_RTMP_RTMPMEDIASOURCE_H_
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <memory>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include "amf.h"
|
|
|
|
|
#include "Rtmp.h"
|
2018-08-31 14:13:00 +08:00
|
|
|
|
#include "RtmpParser.h"
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2018-02-02 18:06:08 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/RingBuffer.h"
|
|
|
|
|
#include "Util/TimeTicker.h"
|
|
|
|
|
#include "Util/ResourcePool.h"
|
|
|
|
|
#include "Util/NoticeCenter.h"
|
|
|
|
|
#include "Thread/ThreadPool.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-02-02 18:06:08 +08:00
|
|
|
|
class RtmpMediaSource: public MediaSource {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<RtmpMediaSource> Ptr;
|
2017-12-04 23:55:09 +08:00
|
|
|
|
typedef RingBuffer<RtmpPacket::Ptr> RingType;
|
2018-02-02 18:06:08 +08:00
|
|
|
|
|
|
|
|
|
RtmpMediaSource(const string &vhost,const string &strApp, const string &strId) :
|
|
|
|
|
MediaSource(RTMP_SCHEMA,vhost,strApp,strId),
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_pRing(new RingBuffer<RtmpPacket::Ptr>()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-02-02 18:06:08 +08:00
|
|
|
|
virtual ~RtmpMediaSource() {}
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
const RingType::Ptr &getRing() const {
|
|
|
|
|
//获取媒体源的rtp环形缓冲
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _pRing;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-02-02 18:06:08 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
const AMFValue &getMetaData() const {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lock(_mtxMap);
|
|
|
|
|
return _metadata;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
template<typename FUN>
|
|
|
|
|
void getConfigFrame(const FUN &f) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lock(_mtxMap);
|
|
|
|
|
for (auto &pr : _mapCfgFrame) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
f(pr.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-31 14:13:00 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
virtual void onGetMetaData(const AMFValue &metadata) {
|
|
|
|
|
lock_guard<recursive_mutex> lock(_mtxMap);
|
|
|
|
|
_metadata = metadata;
|
|
|
|
|
RtmpParser parser(metadata);
|
|
|
|
|
_iCfgFrameSize = parser.getTracks().size();
|
2018-08-31 14:13:00 +08:00
|
|
|
|
if(ready()){
|
|
|
|
|
MediaSource::regist();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_bRegisted = true;
|
2018-08-31 14:13:00 +08:00
|
|
|
|
} else{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_bAsyncRegist = true;
|
2018-08-31 14:13:00 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-12-04 23:55:09 +08:00
|
|
|
|
virtual void onGetMedia(const RtmpPacket::Ptr &pkt) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lock(_mtxMap);
|
2018-10-09 16:39:35 +08:00
|
|
|
|
if (pkt->isCfgFrame()) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_mapCfgFrame.emplace(pkt->typeId, pkt);
|
2018-08-31 14:13:00 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(_bAsyncRegist && !_bRegisted && _mapCfgFrame.size() == _iCfgFrameSize){
|
|
|
|
|
_bAsyncRegist = false;
|
2018-10-09 16:39:35 +08:00
|
|
|
|
MediaSource::regist();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_bRegisted = true;
|
2018-08-31 14:13:00 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-08-31 14:13:00 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_pRing->write(pkt,pkt->isVideoKeyFrame());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-08-31 14:13:00 +08:00
|
|
|
|
private:
|
|
|
|
|
bool ready(){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lock(_mtxMap);
|
|
|
|
|
return _iCfgFrameSize != -1 && _iCfgFrameSize == _mapCfgFrame.size();
|
2018-08-31 14:13:00 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
AMFValue _metadata;
|
|
|
|
|
unordered_map<int, RtmpPacket::Ptr> _mapCfgFrame;
|
|
|
|
|
mutable recursive_mutex _mtxMap;
|
|
|
|
|
RingBuffer<RtmpPacket::Ptr>::Ptr _pRing; //rtp环形缓冲
|
|
|
|
|
int _iCfgFrameSize = -1;
|
|
|
|
|
bool _bAsyncRegist = false;
|
|
|
|
|
bool _bRegisted = false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_RTMP_RTMPMEDIASOURCE_H_ */
|