RtmpMediaSource.h 4.03 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_RTMP_RTMPMEDIASOURCE_H_
#define SRC_RTMP_RTMPMEDIASOURCE_H_

xiongzilaing committed
30 31
#include <mutex>
#include <memory>
xzl committed
32 33 34 35 36
#include <string>
#include <functional>
#include <unordered_map>
#include "amf.h"
#include "Rtmp.h"
37
#include "RtmpMuxer/RtmpDemuxer.h"
xiongziliang committed
38
#include "Common/config.h"
39
#include "Common/MediaSource.h"
xiongzilaing committed
40 41 42 43 44 45 46
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/RingBuffer.h"
#include "Util/TimeTicker.h"
#include "Util/ResourcePool.h"
#include "Util/NoticeCenter.h"
#include "Thread/ThreadPool.h"
xiongziliang committed
47
using namespace toolkit;
xzl committed
48

xiongziliang committed
49
namespace mediakit {
xzl committed
50

51
class RtmpMediaSource: public MediaSource ,public RingDelegate<RtmpPacket::Ptr> {
xzl committed
52 53
public:
	typedef std::shared_ptr<RtmpMediaSource> Ptr;
xiongziliang committed
54
	typedef RingBuffer<RtmpPacket::Ptr> RingType;
55

56
	RtmpMediaSource(const string &vhost,const string &strApp, const string &strId,int ringSize = 0) :
57
			MediaSource(RTMP_SCHEMA,vhost,strApp,strId),
58
			_pRing(new RingBuffer<RtmpPacket::Ptr>(ringSize)) {
xzl committed
59
	}
60 61
	virtual ~RtmpMediaSource() {}

xzl committed
62 63
	const RingType::Ptr &getRing() const {
		//获取媒体源的rtp环形缓冲
64
		return _pRing;
xzl committed
65
	}
66

xzl committed
67
	const AMFValue &getMetaData() const {
68 69
		lock_guard<recursive_mutex> lock(_mtxMap);
		return _metadata;
xzl committed
70 71 72
	}
	template<typename FUN>
	void getConfigFrame(const FUN &f) {
73 74
		lock_guard<recursive_mutex> lock(_mtxMap);
		for (auto &pr : _mapCfgFrame) {
xzl committed
75 76 77
			f(pr.second);
		}
	}
78

xiongziliang committed
79

80 81 82
	virtual void onGetMetaData(const AMFValue &metadata) {
		lock_guard<recursive_mutex> lock(_mtxMap);
		_metadata = metadata;
83
		_iCfgFrameSize = RtmpDemuxer::getTrackCount(metadata);
84 85
		if(ready()){
			MediaSource::regist();
86
			_bRegisted = true;
87
		} else{
88
			_bAsyncRegist = true;
89
		}
xzl committed
90
	}
xiongziliang committed
91 92

    void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override {
93
		lock_guard<recursive_mutex> lock(_mtxMap);
94
		if (pkt->isCfgFrame()) {
95
			_mapCfgFrame[pkt->typeId] = pkt;
96

97 98
			if(_bAsyncRegist && !_bRegisted &&  _mapCfgFrame.size() == _iCfgFrameSize){
				_bAsyncRegist = false;
99
				MediaSource::regist();
100
				_bRegisted = true;
101
			}
102 103 104
		} else{
			_mapStamp[pkt->typeId] = pkt->timeStamp;
			_pRing->write(pkt,pkt->isVideoKeyFrame());
xzl committed
105 106
		}
	}
107 108 109 110 111 112 113 114 115 116 117 118

	uint32_t getTimeStamp(TrackType trackType) override {
		lock_guard<recursive_mutex> lock(_mtxMap);
		switch (trackType){
			case TrackVideo:
				return _mapStamp[MSG_VIDEO];
			case TrackAudio:
				return _mapStamp[MSG_AUDIO];
			default:
				return MAX(_mapStamp[MSG_VIDEO],_mapStamp[MSG_AUDIO]);
		}
	}
119 120
private:
	bool ready(){
121 122
		lock_guard<recursive_mutex> lock(_mtxMap);
		return _iCfgFrameSize != -1 && _iCfgFrameSize == _mapCfgFrame.size();
123
	}
xzl committed
124
protected:
125
	AMFValue _metadata;
126 127
	map<int, RtmpPacket::Ptr> _mapCfgFrame;
	map<int,uint32_t> _mapStamp;
128 129 130 131 132
	mutable recursive_mutex _mtxMap;
	RingBuffer<RtmpPacket::Ptr>::Ptr _pRing; //rtp环形缓冲
	int _iCfgFrameSize = -1;
	bool _bAsyncRegist = false;
	bool _bRegisted = false;
xzl committed
133 134
};

xiongziliang committed
135
} /* namespace mediakit */
xzl committed
136 137

#endif /* SRC_RTMP_RTMPMEDIASOURCE_H_ */