RtmpToRtspMediaSource.h 3.95 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,
55
						  int ringSize = 0) : RtmpMediaSource(vhost, app, id,ringSize){
xiongziliang committed
56
		_demuxer = std::make_shared<RtmpDemuxer>();
xiongziliang committed
57 58
	}
	virtual ~RtmpToRtspMediaSource(){}
xzl committed
59

60
	void onGetMetaData(const AMFValue &metadata) override {
xiongziliang committed
61
		_demuxer = std::make_shared<RtmpDemuxer>(metadata);
62
		RtmpMediaSource::onGetMetaData(metadata);
xzl committed
63 64
	}

65
	void onWrite(const RtmpPacket::Ptr &pkt,bool key_pos = true) override {
xiongziliang committed
66 67
		_demuxer->inputRtmp(pkt);
		if(!_muxer && _demuxer->isInited(2000)){
68 69 70
			_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(),
															 getApp(),
															 getId(),
xiongziliang committed
71
															 _demuxer->getDuration(),
72
															 _enableRtsp,
73
															 false,//不重复生成rtmp
74 75
															 _enableHls,
															 _enableMP4);
xiongziliang committed
76
			for (auto &track : _demuxer->getTracks(false)){
77 78
				_muxer->addTrack(track);
				track->addDelegate(_muxer);
xzl committed
79
			}
80
			_muxer->setListener(_listener);
xzl committed
81
		}
xiongziliang committed
82
		RtmpMediaSource::onWrite(pkt,key_pos);
xzl committed
83
	}
84 85 86

	void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
        RtmpMediaSource::setListener(listener);
87 88
        if(_muxer){
			_muxer->setListener(listener);
89 90
        }
    }
91 92

    int readerCount() override {
93
        return RtmpMediaSource::readerCount() + (_muxer ? _muxer->readerCount() : 0);
94
    }
95 96 97 98 99 100 101 102 103 104 105

	/**
     * 获取track
     * @return
     */
	vector<Track::Ptr> getTracks(bool trackReady) const override {
		if(!_demuxer){
			return this->RtmpMediaSource::getTracks(trackReady);
		}
		return _demuxer->getTracks(trackReady);
	}
106 107 108 109 110 111 112 113 114 115 116 117 118

	/**
	 * 设置协议转换
	 * @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
119
private:
xiongziliang committed
120
	RtmpDemuxer::Ptr _demuxer;
121
	MultiMediaSourceMuxer::Ptr _muxer;
122 123 124
	bool _enableHls = true;
	bool _enableMP4 = false;
	bool _enableRtsp = true;
xzl committed
125 126
};

xiongziliang committed
127
} /* namespace mediakit */
xzl committed
128 129

#endif /* SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_ */