RtmpToRtspMediaSource.h 3.56 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
#include "MediaFile/MediaRecorder.h"
xiongziliang committed
42
#include "Rtsp/RtspMediaSourceMuxer.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
						  int ringSize = 0):RtmpMediaSource(vhost, app, id,ringSize){
xiongziliang committed
58
		_recorder = std::make_shared<MediaRecorder>(vhost, app, id, bEnableHls, bEnableMp4);
59
		_rtmpDemuxer = std::make_shared<RtmpDemuxer>();
xiongziliang committed
60 61
	}
	virtual ~RtmpToRtspMediaSource(){}
xzl committed
62

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

xiongziliang committed
68
	void onWrite(const RtmpPacket::Ptr &pkt,bool key_pos) override {
69 70 71 72 73 74 75 76 77 78 79
		_rtmpDemuxer->inputRtmp(pkt);
		if(!_rtspMuxer && _rtmpDemuxer->isInited(2000)){
			_rtspMuxer = std::make_shared<RtspMediaSourceMuxer>(getVhost(),
																getApp(),
																getId(),
																std::make_shared<TitleSdp>(_rtmpDemuxer->getDuration()));
			for (auto &track : _rtmpDemuxer->getTracks(false)){
				_rtspMuxer->addTrack(track);
				_recorder->addTrack(track);
				track->addDelegate(_rtspMuxer);
				track->addDelegate(_recorder);
xzl committed
80
			}
81
            _rtspMuxer->setListener(_listener);
xzl committed
82
		}
xiongziliang committed
83
		RtmpMediaSource::onWrite(pkt,key_pos);
xzl committed
84
	}
85 86 87 88 89 90 91

	void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
        RtmpMediaSource::setListener(listener);
        if(_rtspMuxer){
            _rtspMuxer->setListener(listener);
        }
    }
92 93

    int readerCount() override {
94
        return RtmpMediaSource::readerCount() + (_rtspMuxer ? _rtspMuxer->readerCount() : 0);
95
    }
xzl committed
96
private:
xiongziliang committed
97 98 99
	RtmpDemuxer::Ptr _rtmpDemuxer;
	RtspMediaSourceMuxer::Ptr _rtspMuxer;
	MediaRecorder::Ptr _recorder;
xzl committed
100 101
};

xiongziliang committed
102
} /* namespace mediakit */
xzl committed
103 104

#endif /* SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_ */