RtspMediaSource.h 5.15 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
class RtspMediaSource: public MediaSource , public RingDelegate<RtpPacket::Ptr> {
xzl committed
52
public:
xiongziliang committed
53
	typedef ResourcePool<RtpPacket> PoolType;
xzl committed
54 55
	typedef std::shared_ptr<RtspMediaSource> Ptr;
	typedef RingBuffer<RtpPacket::Ptr> RingType;
56

57 58 59 60
	RtspMediaSource(const string &strVhost,
	                const string &strApp,
	                const string &strId,
	                int ringSize = 0) :
61
			MediaSource(RTSP_SCHEMA,strVhost,strApp,strId),
62 63
			_ringSize(ringSize){}

64
	virtual ~RtspMediaSource() {}
xzl committed
65 66 67

	const RingType::Ptr &getRing() const {
		//获取媒体源的rtp环形缓冲
68
		return _pRing;
xzl committed
69
	}
70 71

    int readerCount() override {
72
        return _pRing ? _pRing->readerCount() : 0;
73 74 75
	}

    const string& getSdp() const {
xzl committed
76
		//获取该源的媒体描述信息
77
		return _strSdp;
xzl committed
78 79
	}

xiongziliang committed
80
	virtual uint32_t getSsrc(TrackType trackType) {
xiongziliang committed
81
		auto track = _sdpParser.getTrack(trackType);
82 83 84 85
		if(!track){
			return 0;
		}
		return track->_ssrc;
xzl committed
86
	}
xiongziliang committed
87
	virtual uint16_t getSeqence(TrackType trackType) {
xiongziliang committed
88
		auto track = _sdpParser.getTrack(trackType);
89 90 91 92
		if(!track){
			return 0;
		}
		return track->_seq;
xzl committed
93
	}
94 95

	uint32_t getTimeStamp(TrackType trackType) override {
xiongziliang committed
96
		auto track = _sdpParser.getTrack(trackType);
97 98 99
		if(track) {
			return track->_time_stamp;
		}
xiongziliang committed
100
		auto tracks = _sdpParser.getAvailableTrack();
101 102 103 104
		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);
105
		}
xzl committed
106 107
	}

xiongziliang committed
108
	virtual void setTimeStamp(uint32_t uiStamp) {
xiongziliang committed
109
		auto tracks = _sdpParser.getAvailableTrack();
110 111 112 113 114
		for (auto &track : tracks) {
			track->_time_stamp  = uiStamp;
		}
	}

xzl committed
115 116
	virtual void onGetSDP(const string& sdp) {
		//派生类设置该媒体源媒体描述信息
117
		_strSdp = sdp;
xiongziliang committed
118
		_sdpParser.load(sdp);
119 120 121
		if(_pRing){
            regist();
		}
xzl committed
122
	}
xiongziliang committed
123 124

	void onWrite(const RtpPacket::Ptr &rtppt, bool keyPos) override {
xiongziliang committed
125
		auto track = _sdpParser.getTrack(rtppt->type);
126 127 128 129 130
		if(track){
			track->_seq = rtppt->sequence;
			track->_time_stamp = rtppt->timeStamp;
			track->_ssrc = rtppt->ssrc;
		}
131 132 133 134 135 136 137 138 139 140
		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);
141 142 143
            if(!_strSdp.empty()){
                regist();
            }
144
		}
145
		_pRing->write(rtppt,keyPos);
146 147 148 149
        checkNoneReader();
	}
private:
    void onReaderChanged(int size){
150
	    //我们记录最后一次活动时间
151
        _readerTicker.resetTime();
152
        if(size != 0 || readerCount() != 0){
153
            //还有消费者正在观看该流
154 155 156 157 158 159 160
            _asyncEmitNoneReader = false;
            return;
        }
        _asyncEmitNoneReader  = true;
    }

    void checkNoneReader(){
161
        GET_CONFIG(int,stream_none_reader_delay,General::kStreamNoneReaderDelayMS);
162 163
        if(_asyncEmitNoneReader && _readerTicker.elapsedTime() > stream_none_reader_delay){
            _asyncEmitNoneReader = false;
xiongziliang committed
164
            onNoneReader();
165
        }
xzl committed
166 167
	}
protected:
xiongziliang committed
168
	SdpParser _sdpParser;
169 170
    string _strSdp; //媒体描述信息
    RingType::Ptr _pRing; //rtp环形缓冲
171 172 173
    int _ringSize;
    Ticker _readerTicker;
    bool _asyncEmitNoneReader = false;
xzl committed
174 175
};

xiongziliang committed
176
} /* namespace mediakit */
xzl committed
177 178

#endif /* SRC_RTSP_RTSPMEDIASOURCE_H_ */