RtspMediaSource.h 3.4 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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.
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 37
#include "Common/config.h"
#include "Common/MediaSender.h"
38 39
#include "Common/MediaSource.h"

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 48 49

using namespace std;
using namespace ZL::Util;
using namespace ZL::Thread;
50
using namespace ZL::Media;
xzl committed
51 52 53 54

namespace ZL {
namespace Rtsp {

55
class RtspMediaSource: public MediaSource {
xzl committed
56 57 58 59
public:
	typedef ResourcePool<RtpPacket, 64> PoolType;
	typedef std::shared_ptr<RtspMediaSource> Ptr;
	typedef RingBuffer<RtpPacket::Ptr> RingType;
60 61 62

	RtspMediaSource(const string &strVhost,const string &strApp, const string &strId) :
			MediaSource(RTSP_SCHEMA,strVhost,strApp,strId),
63
			m_pRing(new RingBuffer<RtpPacket::Ptr>()),
xzl committed
64 65
			m_thPool(MediaSender::sendThread()) {
	}
66
	virtual ~RtspMediaSource() {}
xzl committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

	const RingType::Ptr &getRing() const {
		//获取媒体源的rtp环形缓冲
		return m_pRing;
	}
	const string& getSdp() const {
		//获取该源的媒体描述信息
		return m_strSdp;
	}

	virtual uint32_t getSsrc(int trackId) {
		return m_mapTracks[trackId].ssrc;
	}
	virtual uint16_t getSeqence(int trackId) {
		return m_mapTracks[trackId].seq;
	}
	virtual uint32_t getTimestamp(int trackId) {
		return m_mapTracks[trackId].timeStamp;
	}

	virtual void onGetSDP(const string& sdp) {
		//派生类设置该媒体源媒体描述信息
89
		m_strSdp = sdp;
xzl committed
90
	}
91
	virtual void onGetRTP(const RtpPacket::Ptr &rtppt, bool keyPos) {
xzl committed
92 93 94 95 96 97 98
		auto &trackRef = m_mapTracks[rtppt->interleaved / 2];
		trackRef.seq = rtppt->sequence;
		trackRef.timeStamp = rtppt->timeStamp;
		trackRef.ssrc = rtppt->ssrc;
		trackRef.type = rtppt->type;
		auto _outRing = m_pRing;
		m_thPool.async([_outRing,rtppt,keyPos]() {
99
			_outRing->write(rtppt,keyPos);
xzl committed
100 101 102 103
		});
	}
protected:
	unordered_map<int, RtspTrack> m_mapTracks;
104 105
    string m_strSdp; //媒体描述信息
    RingType::Ptr m_pRing; //rtp环形缓冲
xzl committed
106 107 108 109 110 111 112
	ThreadPool &m_thPool;
};

} /* namespace Rtsp */
} /* namespace ZL */

#endif /* SRC_RTSP_RTSPMEDIASOURCE_H_ */