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-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2018-02-02 18:06:08 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
2019-06-28 17:30:13 +08:00
|
|
|
|
#include "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
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* rtsp媒体源的数据抽象
|
|
|
|
|
* rtsp有关键的两要素,分别是sdp、rtp包
|
|
|
|
|
* 只要生成了这两要素,那么要实现rtsp推流、rtsp服务器就很简单了
|
|
|
|
|
* rtsp推拉流协议中,先传递sdp,然后再协商传输方式(tcp/udp/组播),最后一直传递rtp
|
|
|
|
|
*/
|
|
|
|
|
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-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param vhost 虚拟主机名
|
|
|
|
|
* @param app 应用名
|
|
|
|
|
* @param stream_id 流id
|
|
|
|
|
* @param ring_size 可以设置固定的环形缓冲大小,0则自适应
|
|
|
|
|
*/
|
|
|
|
|
RtspMediaSource(const string &vhost,
|
|
|
|
|
const string &app,
|
|
|
|
|
const string &stream_id,
|
|
|
|
|
int ring_size = 0) :
|
|
|
|
|
MediaSource(RTSP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {}
|
2019-05-27 18:39:43 +08:00
|
|
|
|
|
2018-02-02 18:06:08 +08:00
|
|
|
|
virtual ~RtspMediaSource() {}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取媒体源的环形缓冲
|
|
|
|
|
*/
|
2017-04-01 16:35:56 +08:00
|
|
|
|
const RingType::Ptr &getRing() const {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
return _ring;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-05-27 16:09:29 +08:00
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取播放器个数
|
|
|
|
|
*/
|
|
|
|
|
int readerCount() override {
|
|
|
|
|
return _ring ? _ring->readerCount() : 0;
|
2019-05-27 16:09:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取该源的sdp
|
|
|
|
|
*/
|
|
|
|
|
const string &getSdp() const {
|
|
|
|
|
return _sdp;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取相应轨道的ssrc
|
|
|
|
|
*/
|
2018-07-05 18:48:08 +08:00
|
|
|
|
virtual uint32_t getSsrc(TrackType trackType) {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
auto track = _sdp_parser.getTrack(trackType);
|
|
|
|
|
if (!track) {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return track->_ssrc;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-12-25 11:04:12 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取相应轨道的seqence
|
|
|
|
|
*/
|
2018-07-05 18:48:08 +08:00
|
|
|
|
virtual uint16_t getSeqence(TrackType trackType) {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
auto track = _sdp_parser.getTrack(trackType);
|
|
|
|
|
if (!track) {
|
2018-10-26 10:59:13 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return track->_seq;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-26 15:09:08 +08:00
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取相应轨道的时间戳,单位毫秒
|
|
|
|
|
*/
|
2018-10-26 15:09:08 +08:00
|
|
|
|
uint32_t getTimeStamp(TrackType trackType) override {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
auto track = _sdp_parser.getTrack(trackType);
|
|
|
|
|
if (track) {
|
2018-10-26 15:09:08 +08:00
|
|
|
|
return track->_time_stamp;
|
|
|
|
|
}
|
2019-12-25 11:04:12 +08:00
|
|
|
|
auto tracks = _sdp_parser.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
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 更新时间戳
|
|
|
|
|
*/
|
2019-12-29 10:49:04 +08:00
|
|
|
|
void setTimeStamp(uint32_t uiStamp) override {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
auto tracks = _sdp_parser.getAvailableTrack();
|
2018-10-26 14:12:16 +08:00
|
|
|
|
for (auto &track : tracks) {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
track->_time_stamp = uiStamp;
|
2018-10-26 14:12:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置sdp
|
|
|
|
|
*/
|
|
|
|
|
virtual void setSdp(const string &sdp) {
|
|
|
|
|
_sdp = sdp;
|
|
|
|
|
_sdp_parser.load(sdp);
|
|
|
|
|
if (_ring) {
|
|
|
|
|
regist();
|
2019-05-30 16:01:56 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-25 16:55:48 +08:00
|
|
|
|
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 输入rtp
|
|
|
|
|
* @param rtp rtp包
|
|
|
|
|
* @param keyPos 该包是否为关键帧的第一个包
|
|
|
|
|
*/
|
|
|
|
|
void onWrite(const RtpPacket::Ptr &rtp, bool keyPos) override {
|
|
|
|
|
auto track = _sdp_parser.getTrack(rtp->type);
|
|
|
|
|
if (track) {
|
|
|
|
|
track->_seq = rtp->sequence;
|
|
|
|
|
track->_time_stamp = rtp->timeStamp;
|
|
|
|
|
track->_ssrc = rtp->ssrc;
|
2018-10-26 10:59:13 +08:00
|
|
|
|
}
|
2019-12-25 11:04:12 +08:00
|
|
|
|
if (!_ring) {
|
|
|
|
|
weak_ptr<RtspMediaSource> weakSelf = dynamic_pointer_cast<RtspMediaSource>(shared_from_this());
|
|
|
|
|
auto lam = [weakSelf](const EventPoller::Ptr &, int size, bool) {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strongSelf->onReaderChanged(size);
|
|
|
|
|
};
|
2020-01-15 11:07:55 +08:00
|
|
|
|
//rtp包缓存最大允许2048个,大概最多3MB数据
|
|
|
|
|
//但是这个是GOP缓存的上限值,真实的GOP缓存大小等于两个I帧之间的包数的两倍
|
|
|
|
|
//而且每次遇到I帧,则会清空GOP缓存,所以真实的GOP缓存远小于此值
|
|
|
|
|
_ring = std::make_shared<RingType>(_ring_size,2048, std::move(lam));
|
2019-12-25 11:04:12 +08:00
|
|
|
|
onReaderChanged(0);
|
|
|
|
|
if (!_sdp.empty()) {
|
|
|
|
|
regist();
|
|
|
|
|
}
|
2019-05-27 18:39:43 +08:00
|
|
|
|
}
|
2019-12-25 11:04:12 +08:00
|
|
|
|
_ring->write(rtp, keyPos);
|
|
|
|
|
checkNoneReader();
|
2019-05-27 18:39:43 +08:00
|
|
|
|
}
|
|
|
|
|
private:
|
2019-12-25 11:04:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 每次增减消费者都会触发该函数
|
|
|
|
|
*/
|
|
|
|
|
void onReaderChanged(int size) {
|
|
|
|
|
//我们记录最后一次活动时间
|
|
|
|
|
_reader_changed_ticker.resetTime();
|
2019-12-28 16:57:35 +08:00
|
|
|
|
if (size != 0 || totalReaderCount() != 0) {
|
2019-12-25 11:04:12 +08:00
|
|
|
|
//还有消费者正在观看该流
|
|
|
|
|
_async_emit_none_reader = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_async_emit_none_reader = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查是否无人消费该流,
|
|
|
|
|
* 如果无人消费且超过一定时间会触发onNoneReader事件
|
|
|
|
|
*/
|
|
|
|
|
void checkNoneReader() {
|
|
|
|
|
GET_CONFIG(int, stream_none_reader_delay, General::kStreamNoneReaderDelayMS);
|
|
|
|
|
if (_async_emit_none_reader && _reader_changed_ticker.elapsedTime() > stream_none_reader_delay) {
|
|
|
|
|
_async_emit_none_reader = false;
|
|
|
|
|
onNoneReader();
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
protected:
|
2019-12-25 11:04:12 +08:00
|
|
|
|
int _ring_size;
|
|
|
|
|
bool _async_emit_none_reader = false;
|
|
|
|
|
Ticker _reader_changed_ticker;
|
|
|
|
|
SdpParser _sdp_parser;
|
|
|
|
|
string _sdp;
|
|
|
|
|
RingType::Ptr _ring;
|
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_ */
|