RtspMediaSource.h 5.83 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
xzl committed
25 26 27 28 29
 */

#ifndef SRC_RTSP_RTSPMEDIASOURCE_H_
#define SRC_RTSP_RTSPMEDIASOURCE_H_

xiongzilaing committed
30
#include <mutex>
xzl committed
31 32
#include <string>
#include <memory>
xiongzilaing committed
33
#include <functional>
xzl committed
34
#include <unordered_map>
xiongziliang committed
35
#include "Common/config.h"
36
#include "Common/MediaSource.h"
xiongziliang committed
37
#include "RtpCodec.h"
38

xzl committed
39
#include "Util/logger.h"
xiongzilaing committed
40
#include "Util/RingBuffer.h"
xzl committed
41
#include "Util/TimeTicker.h"
xiongzilaing committed
42 43 44
#include "Util/ResourcePool.h"
#include "Util/NoticeCenter.h"
#include "Thread/ThreadPool.h"
xzl committed
45 46

using namespace std;
xiongziliang committed
47
using namespace toolkit;
xzl committed
48

xiongziliang committed
49
namespace mediakit {
xzl committed
50

51 52 53 54 55 56 57
/**
 * rtsp媒体源的数据抽象
 * rtsp有关键的两要素,分别是sdp、rtp包
 * 只要生成了这两要素,那么要实现rtsp推流、rtsp服务器就很简单了
 * rtsp推拉流协议中,先传递sdp,然后再协商传输方式(tcp/udp/组播),最后一直传递rtp
 */
class RtspMediaSource : public MediaSource, public RingDelegate<RtpPacket::Ptr> {
xzl committed
58
public:
xiongziliang committed
59
	typedef ResourcePool<RtpPacket> PoolType;
xzl committed
60 61
	typedef std::shared_ptr<RtspMediaSource> Ptr;
	typedef RingBuffer<RtpPacket::Ptr> RingType;
62

63 64 65 66 67 68 69 70 71 72 73 74
	/**
	 * 构造函数
	 * @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) {}
75

76
	virtual ~RtspMediaSource() {}
xzl committed
77

78 79 80
	/**
	 * 获取媒体源的环形缓冲
	 */
xzl committed
81
	const RingType::Ptr &getRing() const {
82
		return _ring;
xzl committed
83
	}
84

85 86 87 88 89
	/**
	 * 获取播放器个数
	 */
	int readerCount() override {
		return _ring ? _ring->readerCount() : 0;
90 91
	}

92 93 94 95 96
	/**
	 * 获取该源的sdp
	 */
	const string &getSdp() const {
		return _sdp;
xzl committed
97 98
	}

99 100 101
	/**
	 * 获取相应轨道的ssrc
	 */
xiongziliang committed
102
	virtual uint32_t getSsrc(TrackType trackType) {
103 104
		auto track = _sdp_parser.getTrack(trackType);
		if (!track) {
105 106 107
			return 0;
		}
		return track->_ssrc;
xzl committed
108
	}
109 110 111 112

	/**
	 * 获取相应轨道的seqence
	 */
xiongziliang committed
113
	virtual uint16_t getSeqence(TrackType trackType) {
114 115
		auto track = _sdp_parser.getTrack(trackType);
		if (!track) {
116 117 118
			return 0;
		}
		return track->_seq;
xzl committed
119
	}
120

121 122 123
	/**
	 * 获取相应轨道的时间戳,单位毫秒
	 */
124
	uint32_t getTimeStamp(TrackType trackType) override {
125 126
		auto track = _sdp_parser.getTrack(trackType);
		if (track) {
127 128
			return track->_time_stamp;
		}
129 130 131 132 133 134 135 136
		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);
137
		}
xzl committed
138 139
	}

140 141 142
	/**
	 * 更新时间戳
	 */
xiongziliang committed
143
	virtual void setTimeStamp(uint32_t uiStamp) {
144
		auto tracks = _sdp_parser.getAvailableTrack();
145
		for (auto &track : tracks) {
146
			track->_time_stamp = uiStamp;
147 148 149
		}
	}

150 151 152 153 154 155 156 157
	/**
	 * 设置sdp
	 */
	virtual void setSdp(const string &sdp) {
		_sdp = sdp;
		_sdp_parser.load(sdp);
		if (_ring) {
			regist();
158
		}
xzl committed
159
	}
xiongziliang committed
160

161 162 163 164 165 166 167 168 169 170 171
	/**
	 * 输入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;
172
		}
173 174 175 176 177 178 179 180 181 182 183 184 185 186
		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);
			};
			_ring = std::make_shared<RingType>(_ring_size, std::move(lam));
			onReaderChanged(0);
			if (!_sdp.empty()) {
				regist();
			}
187
		}
188 189
		_ring->write(rtp, keyPos);
		checkNoneReader();
190 191
	}
private:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	/**
	 * 每次增减消费者都会触发该函数
	 */
	void onReaderChanged(int size) {
		//我们记录最后一次活动时间
		_reader_changed_ticker.resetTime();
		if (size != 0 || readerCount() != 0) {
			//还有消费者正在观看该流
			_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();
		}
xzl committed
216 217
	}
protected:
218 219 220 221 222 223
	int _ring_size;
	bool _async_emit_none_reader = false;
	Ticker _reader_changed_ticker;
	SdpParser _sdp_parser;
	string _sdp;
	RingType::Ptr _ring;
xzl committed
224 225
};

xiongziliang committed
226
} /* namespace mediakit */
xzl committed
227 228

#endif /* SRC_RTSP_RTSPMEDIASOURCE_H_ */