RtspToRtmpMediaSource.h 3.8 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 46
                          const string &app,
                          const string &id,
                          bool bEnableHls = true,
47
                          bool bEnableMp4 = false,
48
                          int ringSize = 0) : RtspMediaSource(vhost, app, id,ringSize) {
49 50
        _bEnableHls = bEnableHls;
        _bEnableMp4 = bEnableMp4;
xiongziliang committed
51
    }
xzl committed
52

xiongziliang committed
53
    virtual ~RtspToRtmpMediaSource() {}
54

xiongziliang committed
55
    virtual void onGetSDP(const string &strSdp) override {
xiongziliang committed
56
        _demuxer = std::make_shared<RtspDemuxer>(strSdp);
xiongziliang committed
57 58
        RtspMediaSource::onGetSDP(strSdp);
    }
59

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

    void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
        RtspMediaSource::setListener(listener);
84 85
        if(_muxer){
            _muxer->setListener(listener);
86 87
        }
    }
88
    int readerCount() override {
89
        return RtspMediaSource::readerCount() + (_muxer ? _muxer->readerCount() : 0);
90
    }
xiongziliang committed
91
private:
xiongziliang committed
92
    RtspDemuxer::Ptr _demuxer;
93 94 95
    MultiMediaSourceMuxer::Ptr _muxer;
    bool _bEnableHls;
    bool _bEnableMp4;
xzl committed
96 97
};

xiongziliang committed
98
} /* namespace mediakit */
xzl committed
99 100

#endif /* SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_ */