RtmpToRtspMediaSource.h 3.46 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 55
	RtmpToRtspMediaSource(const string &vhost,
                          const string &app,
                          const string &id,
                          bool bEnableHls = true,
56
                          bool bEnableMp4 = false,
57 58 59
						  int ringSize = 0) : RtmpMediaSource(vhost, app, id,ringSize){
		_bEnableHls = bEnableHls;
		_bEnableMp4 = bEnableMp4;
xiongziliang committed
60
		_demuxer = std::make_shared<RtmpDemuxer>();
xiongziliang committed
61 62
	}
	virtual ~RtmpToRtspMediaSource(){}
xzl committed
63

64
	void onGetMetaData(const AMFValue &metadata) override {
xiongziliang committed
65
		_demuxer = std::make_shared<RtmpDemuxer>(metadata);
66
		RtmpMediaSource::onGetMetaData(metadata);
xzl committed
67 68
	}

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

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

    int readerCount() override {
97
        return RtmpMediaSource::readerCount() + (_muxer ? _muxer->readerCount() : 0);
98
    }
xzl committed
99
private:
xiongziliang committed
100
	RtmpDemuxer::Ptr _demuxer;
101 102 103
	MultiMediaSourceMuxer::Ptr _muxer;
	bool _bEnableHls;
	bool _bEnableMp4;
xzl committed
104 105
};

xiongziliang committed
106
} /* namespace mediakit */
xzl committed
107 108

#endif /* SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_ */