RtspToRtmpMediaSource.h 4.34 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3
* MIT License
*
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 25
*
* 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
26 27 28 29 30

#ifndef SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_
#define SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_

#include "Rtmp/amf.h"
xiongziliang committed
31
#include "RtspMediaSource.h"
xiongziliang committed
32
#include "RtspDemuxer.h"
33
#include "Common/MultiMediaSourceMuxer.h"
xiongziliang committed
34

xiongziliang committed
35
using namespace toolkit;
xzl committed
36

xiongziliang committed
37
namespace mediakit {
xzl committed
38

xiongziliang committed
39
class RtspToRtmpMediaSource : public RtspMediaSource {
xzl committed
40
public:
xiongziliang committed
41
    typedef std::shared_ptr<RtspToRtmpMediaSource> Ptr;
42

xiongziliang committed
43
    RtspToRtmpMediaSource(const string &vhost,
44 45
                          const string &app,
                          const string &id,
46
                          int ringSize = 0) : RtspMediaSource(vhost, app, id,ringSize) {
xiongziliang committed
47
    }
xzl committed
48

xiongziliang committed
49
    virtual ~RtspToRtmpMediaSource() {}
50

xiongziliang committed
51
    virtual void onGetSDP(const string &strSdp) override {
xiongziliang committed
52
        _demuxer = std::make_shared<RtspDemuxer>(strSdp);
xiongziliang committed
53 54
        RtspMediaSource::onGetSDP(strSdp);
    }
55

xiongziliang committed
56
    virtual void onWrite(const RtpPacket::Ptr &rtp, bool bKeyPos) override {
xiongziliang committed
57 58 59
        if (_demuxer) {
            bKeyPos = _demuxer->inputRtp(rtp);
            if (!_muxer && _demuxer->isInited(2000)) {
60 61 62
                _muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(),
                                                                 getApp(),
                                                                 getId(),
xiongziliang committed
63
                                                                 _demuxer->getDuration(),
64
                                                                 false,//不重复生成rtsp
65 66 67
                                                                 _enableRtmp,
                                                                 _enableHls,
                                                                 _enableMP4);
xiongziliang committed
68
                for (auto &track : _demuxer->getTracks(false)) {
69 70
                    _muxer->addTrack(track);
                    track->addDelegate(_muxer);
xiongziliang committed
71
                }
72
                _muxer->setListener(_listener);
xiongziliang committed
73 74 75 76
            }
        }
        RtspMediaSource::onWrite(rtp, bKeyPos);
    }
77 78 79

    void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
        RtspMediaSource::setListener(listener);
80 81
        if(_muxer){
            _muxer->setListener(listener);
82 83
        }
    }
84
    int readerCount() override {
85
        return RtspMediaSource::readerCount() + (_muxer ? _muxer->readerCount() : 0);
86
    }
87 88 89 90 91 92 93 94 95 96 97

    /**
     * 获取track
     * @return
     */
    vector<Track::Ptr> getTracks(bool trackReady) const override {
        if(!_demuxer){
            return this->RtspMediaSource::getTracks(trackReady);
        }
        return _demuxer->getTracks(trackReady);
    }
98 99 100 101 102 103 104 105 106 107 108 109 110

    /**
	 * 设置协议转换
	 * @param enableRtmp 是否转换成rtmp
	 * @param enableHls  是否转换成hls
	 * @param enableMP4  是否mp4录制
	 */
    void setProtocolTranslation(bool enableRtmp,bool enableHls,bool enableMP4){
//        DebugL << enableRtmp << " " << enableHls << " " << enableMP4;
        _enableRtmp = enableRtmp;
        _enableHls = enableHls;
        _enableMP4 = enableMP4;
    }
xiongziliang committed
111
private:
xiongziliang committed
112
    RtspDemuxer::Ptr _demuxer;
113
    MultiMediaSourceMuxer::Ptr _muxer;
114 115 116
    bool _enableHls = true;
    bool _enableMP4 = false;
    bool _enableRtmp = true;
xzl committed
117 118
};

xiongziliang committed
119
} /* namespace mediakit */
xzl committed
120 121

#endif /* SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_ */