RtmpToRtspMediaSource.h 4.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
 */

#ifndef SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_
#define SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_
xiongzilaing committed
29 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 36
#include "Util/util.h"
#include "Util/logger.h"
xzl committed
37 38 39
#include "amf.h"
#include "Rtmp.h"
#include "RtmpMediaSource.h"
xiongziliang committed
40
#include "RtmpDemuxer.h"
41 42
#include "Common/MultiMediaSourceMuxer.h"

xzl committed
43
using namespace std;
xiongziliang committed
44
using namespace toolkit;
xzl committed
45

xiongziliang committed
46
namespace mediakit {
xzl committed
47 48 49 50

class RtmpToRtspMediaSource: public RtmpMediaSource {
public:
	typedef std::shared_ptr<RtmpToRtspMediaSource> Ptr;
51

52 53 54
	RtmpToRtspMediaSource(const string &vhost,
                          const string &app,
                          const string &id,
xiongziliang committed
55 56
						  int ringSize = 0) :
            RtmpMediaSource(vhost, app, id,ringSize){
xiongziliang committed
57 58
	}
	virtual ~RtmpToRtspMediaSource(){}
xzl committed
59

60
	void onGetMetaData(const AMFValue &metadata) override {
61 62 63 64
		if(!_demuxer){
			//在未调用onWrite前,设置Metadata能触发生成RtmpDemuxer
			_demuxer = std::make_shared<RtmpDemuxer>(metadata);
		}
65
		RtmpMediaSource::onGetMetaData(metadata);
xzl committed
66 67
	}

68
	void onWrite(const RtmpPacket::Ptr &pkt,bool key_pos = true) override {
69 70 71 72
		if(!_demuxer){
			//尚未获取Metadata,那么不管有没有Metadata,都生成RtmpDemuxer
			_demuxer = std::make_shared<RtmpDemuxer>();
		}
xiongziliang committed
73 74
		_demuxer->inputRtmp(pkt);
		if(!_muxer && _demuxer->isInited(2000)){
75 76 77
			_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(),
															 getApp(),
															 getId(),
xiongziliang committed
78
															 _demuxer->getDuration(),
79
															 _enableRtsp,
80
															 false,//不重复生成rtmp
81 82
															 _enableHls,
															 _enableMP4);
xiongziliang committed
83
			for (auto &track : _demuxer->getTracks(false)){
84 85
				_muxer->addTrack(track);
				track->addDelegate(_muxer);
xzl committed
86
			}
xiongziliang committed
87
			_muxer->setListener(getListener());
xzl committed
88
		}
xiongziliang committed
89
		RtmpMediaSource::onWrite(pkt,key_pos);
xzl committed
90
	}
91 92 93

	void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
        RtmpMediaSource::setListener(listener);
94 95
        if(_muxer){
			_muxer->setListener(listener);
96 97
        }
    }
98 99

    int readerCount() override {
100
        return RtmpMediaSource::readerCount() + (_muxer ? _muxer->readerCount() : 0);
101
    }
102 103 104 105 106 107 108 109 110 111 112

	/**
     * 获取track
     * @return
     */
	vector<Track::Ptr> getTracks(bool trackReady) const override {
		if(!_demuxer){
			return this->RtmpMediaSource::getTracks(trackReady);
		}
		return _demuxer->getTracks(trackReady);
	}
113 114 115 116 117 118 119 120 121 122 123 124 125

	/**
	 * 设置协议转换
	 * @param enableRtsp 是否转换成rtsp
	 * @param enableHls  是否转换成hls
	 * @param enableMP4  是否mp4录制
	 */
	void setProtocolTranslation(bool enableRtsp,bool enableHls,bool enableMP4){
//		DebugL << enableRtsp << " " << enableHls << " " << enableMP4;
		_enableRtsp = enableRtsp;
		_enableHls = enableHls;
		_enableMP4 = enableMP4;
	}
xzl committed
126
private:
xiongziliang committed
127
	RtmpDemuxer::Ptr _demuxer;
128
	MultiMediaSourceMuxer::Ptr _muxer;
129 130 131
	bool _enableHls = true;
	bool _enableMP4 = false;
	bool _enableRtsp = true;
xzl committed
132 133
};

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

#endif /* SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_ */