MultiMediaSourceMuxer.h 3.73 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
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.
 */
xiongziliang committed
26

27 28
#ifndef ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
#define ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
xiongziliang committed
29

xiongziliang committed
30 31
#include "Rtsp/RtspMediaSourceMuxer.h"
#include "Rtmp/RtmpMediaSourceMuxer.h"
32
#include "MediaFile/MediaRecorder.h"
xiongziliang committed
33

34
class MultiMediaSourceMuxer : public FrameWriterInterface{
xiongziliang committed
35
public:
36 37
    typedef std::shared_ptr<MultiMediaSourceMuxer> Ptr;

38 39 40
    MultiMediaSourceMuxer(const string &vhost,
                          const string &strApp,
                          const string &strId,
41
                          float dur_sec = 0.0,
42
                          bool bEanbleRtsp = true,
43
                          bool bEanbleRtmp = true,
44 45 46
                          bool bEanbleHls = true,
                          bool bEnableMp4 = false
                          ){
47 48 49 50 51 52
        if (bEanbleRtmp) {
            _rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, strApp, strId, std::make_shared<TitleMete>(dur_sec));
        }
        if (bEanbleRtsp) {
            _rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, strApp, strId, std::make_shared<TitleSdp>(dur_sec));
        }
53
        _record = std::make_shared<MediaRecorder>(vhost,strApp,strId,bEanbleHls,bEnableMp4);
54 55 56 57 58 59 60 61
    }
    virtual ~MultiMediaSourceMuxer(){}


    /**
     * 添加音视频媒体
     * @param track 媒体描述
     */
xiongziliang committed
62
    void addTrack(const Track::Ptr & track) {
63 64 65 66 67 68
        if(_rtmp){
            _rtmp->addTrack(track);
        }
        if(_rtsp){
            _rtsp->addTrack(track);
        }
69
        _record->addTrack(track);
70 71 72 73 74 75
    }

    /**
     * 写入帧数据然后打包rtmp
     * @param frame 帧数据
     */
xiongziliang committed
76
    void inputFrame(const Frame::Ptr &frame) override {
77 78 79 80 81 82
        if(_rtmp) {
            _rtmp->inputFrame(frame);
        }
        if(_rtsp) {
            _rtsp->inputFrame(frame);
        }
83
        _record->inputFrame(frame);
84
    }
xiongziliang committed
85 86 87 88 89 90

    /**
     * 设置事件监听器
     * @param listener
     */
    void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
91 92 93 94 95 96
        if(_rtmp) {
            _rtmp->setListener(listener);
        }
        if(_rtsp) {
            _rtsp->setListener(listener);
        }
xiongziliang committed
97
    }
98 99 100 101 102 103

    /**
     * 返回总的消费者个数
     * @return
     */
    int readerCount() const{
104
        return (_rtsp ? _rtsp->readerCount() : 0) + (_rtmp ? _rtmp->readerCount() : 0);
105 106
    }

xiongziliang committed
107
    void setTimeStamp(uint32_t stamp){
108 109 110
        if(_rtsp){
            _rtsp->setTimeStamp(stamp);
        }
111
    }
xiongziliang committed
112
private:
113 114
    RtmpMediaSourceMuxer::Ptr _rtmp;
    RtspMediaSourceMuxer::Ptr _rtsp;
115
    MediaRecorder::Ptr _record;
xiongziliang committed
116 117
};

118 119

#endif //ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H