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
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* 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_RTSP_RTSPMEDIASOURCE_H_
|
|
|
|
|
#define SRC_RTSP_RTSPMEDIASOURCE_H_
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <mutex>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <memory>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <functional>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <unordered_map>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Rtsp.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"
|
2018-10-24 18:09:54 +08:00
|
|
|
|
#include "RtspMuxer/RtpCodec.h"
|
2018-02-02 18:06:08 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Util/logger.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/RingBuffer.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Util/TimeTicker.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/ResourcePool.h"
|
|
|
|
|
#include "Util/NoticeCenter.h"
|
|
|
|
|
#include "Thread/ThreadPool.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
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-10-25 16:46:00 +08:00
|
|
|
|
class RtspMediaSource: public MediaSource , public RingDelegate<RtpPacket::Ptr> {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2018-07-03 17:01:56 +08:00
|
|
|
|
typedef ResourcePool<RtpPacket> PoolType;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
typedef std::shared_ptr<RtspMediaSource> Ptr;
|
|
|
|
|
typedef RingBuffer<RtpPacket::Ptr> RingType;
|
2018-02-02 18:06:08 +08:00
|
|
|
|
|
2019-05-27 18:39:43 +08:00
|
|
|
|
RtspMediaSource(const string &strVhost,
|
|
|
|
|
const string &strApp,
|
|
|
|
|
const string &strId,
|
|
|
|
|
int ringSize = 0) :
|
2018-02-02 18:06:08 +08:00
|
|
|
|
MediaSource(RTSP_SCHEMA,strVhost,strApp,strId),
|
2019-05-27 18:39:43 +08:00
|
|
|
|
_ringSize(ringSize){}
|
|
|
|
|
|
2018-02-02 18:06:08 +08:00
|
|
|
|
virtual ~RtspMediaSource() {}
|
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
|
|
|
|
}
|
2019-05-27 16:09:29 +08:00
|
|
|
|
|
|
|
|
|
int readerCount() override {
|
2019-05-27 18:39:43 +08:00
|
|
|
|
return _pRing ? _pRing->readerCount() : 0;
|
2019-05-27 16:09:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string& getSdp() const {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//获取该源的媒体描述信息
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _strSdp;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 18:48:08 +08:00
|
|
|
|
virtual uint32_t getSsrc(TrackType trackType) {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
auto track = _sdpAttr.getTrack(trackType);
|
|
|
|
|
if(!track){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return track->_ssrc;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-07-05 18:48:08 +08:00
|
|
|
|
virtual uint16_t getSeqence(TrackType trackType) {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
auto track = _sdpAttr.getTrack(trackType);
|
|
|
|
|
if(!track){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return track->_seq;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-26 15:09:08 +08:00
|
|
|
|
|
|
|
|
|
uint32_t getTimeStamp(TrackType trackType) override {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
auto track = _sdpAttr.getTrack(trackType);
|
2018-10-26 15:09:08 +08:00
|
|
|
|
if(track) {
|
|
|
|
|
return track->_time_stamp;
|
|
|
|
|
}
|
|
|
|
|
auto tracks = _sdpAttr.getAvailableTrack();
|
|
|
|
|
switch (tracks.size()){
|
|
|
|
|
case 0: return 0;
|
|
|
|
|
case 1: return tracks[0]->_time_stamp;
|
|
|
|
|
default:return MAX(tracks[0]->_time_stamp,tracks[1]->_time_stamp);
|
2018-10-26 10:59:13 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 14:19:41 +08:00
|
|
|
|
virtual void setTimeStamp(uint32_t uiStamp) {
|
2018-10-26 14:12:16 +08:00
|
|
|
|
auto tracks = _sdpAttr.getAvailableTrack();
|
|
|
|
|
for (auto &track : tracks) {
|
|
|
|
|
track->_time_stamp = uiStamp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
virtual void onGetSDP(const string& sdp) {
|
|
|
|
|
//派生类设置该媒体源媒体描述信息
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strSdp = sdp;
|
2018-10-26 10:59:13 +08:00
|
|
|
|
_sdpAttr.load(sdp);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-25 16:55:48 +08:00
|
|
|
|
|
|
|
|
|
void onWrite(const RtpPacket::Ptr &rtppt, bool keyPos) override {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
auto track = _sdpAttr.getTrack(rtppt->type);
|
|
|
|
|
if(track){
|
|
|
|
|
track->_seq = rtppt->sequence;
|
|
|
|
|
track->_time_stamp = rtppt->timeStamp;
|
|
|
|
|
track->_ssrc = rtppt->ssrc;
|
|
|
|
|
}
|
2019-05-27 18:39:43 +08:00
|
|
|
|
if(!_pRing){
|
|
|
|
|
weak_ptr<RtspMediaSource> weakSelf = dynamic_pointer_cast<RtspMediaSource>(shared_from_this());
|
|
|
|
|
_pRing = std::make_shared<RingType>(_ringSize,[weakSelf](const EventPoller::Ptr &,int size,bool){
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if(!strongSelf){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strongSelf->onReaderChanged(size);
|
|
|
|
|
});
|
|
|
|
|
onReaderChanged(0);
|
|
|
|
|
regist();
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_pRing->write(rtppt,keyPos);
|
2019-05-27 18:39:43 +08:00
|
|
|
|
checkNoneReader();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
void onReaderChanged(int size){
|
2019-05-28 17:14:36 +08:00
|
|
|
|
//我们记录最后一次活动时间
|
2019-05-27 21:37:29 +08:00
|
|
|
|
_readerTicker.resetTime();
|
2019-05-27 18:39:43 +08:00
|
|
|
|
if(size != 0 || readerCount() != 0){
|
2019-05-28 17:14:36 +08:00
|
|
|
|
//还有消费者正在观看该流
|
2019-05-27 18:39:43 +08:00
|
|
|
|
_asyncEmitNoneReader = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_asyncEmitNoneReader = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void checkNoneReader(){
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(int,stream_none_reader_delay,General::kStreamNoneReaderDelayMS);
|
2019-05-27 18:39:43 +08:00
|
|
|
|
if(_asyncEmitNoneReader && _readerTicker.elapsedTime() > stream_none_reader_delay){
|
|
|
|
|
_asyncEmitNoneReader = false;
|
|
|
|
|
auto listener = _listener.lock();
|
|
|
|
|
if(!listener){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
listener->onNoneReader(*this);
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
protected:
|
2018-10-26 10:59:13 +08:00
|
|
|
|
SdpAttr _sdpAttr;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _strSdp; //媒体描述信息
|
|
|
|
|
RingType::Ptr _pRing; //rtp环形缓冲
|
2019-05-27 18:39:43 +08:00
|
|
|
|
int _ringSize;
|
|
|
|
|
Ticker _readerTicker;
|
|
|
|
|
bool _asyncEmitNoneReader = 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_RTSP_RTSPMEDIASOURCE_H_ */
|