RtspMediaSource.h 3.78 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>
xiongzilaing committed
35
#include "Rtsp.h"
xiongziliang committed
36
#include "Common/config.h"
37
#include "Common/MediaSource.h"
38
#include "RtspMuxer/RtpCodec.h"
39

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

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

xiongziliang committed
50
namespace mediakit {
xzl committed
51

52
class RtspMediaSource: public MediaSource , public RingDelegate<RtpPacket::Ptr> {
xzl committed
53
public:
xiongziliang committed
54
	typedef ResourcePool<RtpPacket> PoolType;
xzl committed
55 56
	typedef std::shared_ptr<RtspMediaSource> Ptr;
	typedef RingBuffer<RtpPacket::Ptr> RingType;
57

58
	RtspMediaSource(const string &strVhost,const string &strApp, const string &strId,int ringSize = 0) :
59
			MediaSource(RTSP_SCHEMA,strVhost,strApp,strId),
60
			_pRing(new RingBuffer<RtpPacket::Ptr>(ringSize)) {
xzl committed
61
	}
62
	virtual ~RtspMediaSource() {}
xzl committed
63 64 65

	const RingType::Ptr &getRing() const {
		//获取媒体源的rtp环形缓冲
66
		return _pRing;
xzl committed
67 68 69
	}
	const string& getSdp() const {
		//获取该源的媒体描述信息
70
		return _strSdp;
xzl committed
71 72
	}

xiongziliang committed
73
	virtual uint32_t getSsrc(TrackType trackType) {
74 75 76 77 78
		auto track = _sdpAttr.getTrack(trackType);
		if(!track){
			return 0;
		}
		return track->_ssrc;
xzl committed
79
	}
xiongziliang committed
80
	virtual uint16_t getSeqence(TrackType trackType) {
81 82 83 84 85
		auto track = _sdpAttr.getTrack(trackType);
		if(!track){
			return 0;
		}
		return track->_seq;
xzl committed
86
	}
87 88

	uint32_t getTimeStamp(TrackType trackType) override {
89
		auto track = _sdpAttr.getTrack(trackType);
90 91 92 93 94 95 96 97
		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);
98
		}
xzl committed
99 100
	}

xiongziliang committed
101
	virtual void setTimeStamp(uint32_t uiStamp) {
102 103 104 105 106 107
		auto tracks = _sdpAttr.getAvailableTrack();
		for (auto &track : tracks) {
			track->_time_stamp  = uiStamp;
		}
	}

xzl committed
108 109
	virtual void onGetSDP(const string& sdp) {
		//派生类设置该媒体源媒体描述信息
110
		_strSdp = sdp;
111
		_sdpAttr.load(sdp);
112
		regist();
xzl committed
113
	}
xiongziliang committed
114 115

	void onWrite(const RtpPacket::Ptr &rtppt, bool keyPos) override {
116 117 118 119 120 121
		auto track = _sdpAttr.getTrack(rtppt->type);
		if(track){
			track->_seq = rtppt->sequence;
			track->_time_stamp = rtppt->timeStamp;
			track->_ssrc = rtppt->ssrc;
		}
122
		_pRing->write(rtppt,keyPos);
xzl committed
123 124
	}
protected:
125
	SdpAttr _sdpAttr;
126 127
    string _strSdp; //媒体描述信息
    RingType::Ptr _pRing; //rtp环形缓冲
xzl committed
128 129
};

xiongziliang committed
130
} /* namespace mediakit */
xzl committed
131 132

#endif /* SRC_RTSP_RTSPMEDIASOURCE_H_ */