RtmpMediaSource.h 3.8 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 "RtmpParser.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"
xzl committed
47 48 49 50

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

namespace ZL {
namespace Rtmp {

56
class RtmpMediaSource: public MediaSource {
xzl committed
57 58
public:
	typedef std::shared_ptr<RtmpMediaSource> Ptr;
xiongziliang committed
59
	typedef RingBuffer<RtmpPacket::Ptr> RingType;
60 61 62

	RtmpMediaSource(const string &vhost,const string &strApp, const string &strId) :
			MediaSource(RTMP_SCHEMA,vhost,strApp,strId),
xiongziliang committed
63
			m_pRing(new RingBuffer<RtmpPacket::Ptr>()) {
xzl committed
64
	}
65 66
	virtual ~RtmpMediaSource() {}

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

xzl committed
72
	const AMFValue &getMetaData() const {
73
		lock_guard<recursive_mutex> lock(m_mtxMap);
xzl committed
74 75 76 77 78 79 80 81 82
		return m_metadata;
	}
	template<typename FUN>
	void getConfigFrame(const FUN &f) {
		lock_guard<recursive_mutex> lock(m_mtxMap);
		for (auto &pr : m_mapCfgFrame) {
			f(pr.second);
		}
	}
83

xzl committed
84
	virtual void onGetMetaData(const AMFValue &_metadata) {
85
		lock_guard<recursive_mutex> lock(m_mtxMap);
xzl committed
86
		m_metadata = _metadata;
87 88 89 90 91 92 93 94
		RtmpParser parser(_metadata);
		m_iCfgFrameSize = parser.containAudio() + parser.containVideo();
		if(ready()){
			MediaSource::regist();
			m_bRegisted = true;
		} else{
			m_bAsyncRegist = true;
		}
xzl committed
95
	}
xiongziliang committed
96
	virtual void onGetMedia(const RtmpPacket::Ptr &pkt) {
97
		if(!m_bRegisted){
xzl committed
98
			lock_guard<recursive_mutex> lock(m_mtxMap);
99 100 101 102 103 104 105 106 107
			if (m_mapCfgFrame.size() != m_iCfgFrameSize && pkt->isCfgFrame()) {
				m_mapCfgFrame.emplace(pkt->typeId, pkt);

				if( m_mapCfgFrame.size() == m_iCfgFrameSize && m_bAsyncRegist){
					m_bAsyncRegist = false;
					MediaSource::regist();
					m_bRegisted = true;
				}
			}
xzl committed
108
		}
109

xiongziliang committed
110
		m_pRing->write(pkt,pkt->isVideoKeyFrame());
xzl committed
111
	}
112 113 114 115 116
private:
	bool ready(){
		lock_guard<recursive_mutex> lock(m_mtxMap);
		return m_iCfgFrameSize != -1 && m_iCfgFrameSize == m_mapCfgFrame.size();
	}
xzl committed
117 118
protected:
	AMFValue m_metadata;
xiongziliang committed
119
	unordered_map<int, RtmpPacket::Ptr> m_mapCfgFrame;
xzl committed
120
	mutable recursive_mutex m_mtxMap;
xiongziliang committed
121
	RingBuffer<RtmpPacket::Ptr>::Ptr m_pRing; //rtp环形缓冲
122 123 124
	int m_iCfgFrameSize = -1;
	bool m_bAsyncRegist = false;
	bool m_bRegisted = false;
xzl committed
125 126 127 128 129 130
};

} /* namespace Rtmp */
} /* namespace ZL */

#endif /* SRC_RTMP_RTMPMEDIASOURCE_H_ */