RtmpMediaSourceImp.h 4.63 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xzl committed
9 10 11 12
 */

#ifndef SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_
#define SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_
xiongzilaing committed
13 14

#include <mutex>
xzl committed
15 16
#include <string>
#include <memory>
xiongzilaing committed
17
#include <functional>
xzl committed
18
#include <unordered_map>
xiongziliang committed
19 20
#include "Util/util.h"
#include "Util/logger.h"
xzl committed
21 22 23
#include "amf.h"
#include "Rtmp.h"
#include "RtmpMediaSource.h"
xiongziliang committed
24
#include "RtmpDemuxer.h"
25
#include "Common/MultiMediaSourceMuxer.h"
xzl committed
26
using namespace std;
xiongziliang committed
27
using namespace toolkit;
xzl committed
28

xiongziliang committed
29
namespace mediakit {
30
class RtmpMediaSourceImp: public RtmpMediaSource, public TrackListener , public MultiMediaSourceMuxer::Listener {
xzl committed
31
public:
32
    typedef std::shared_ptr<RtmpMediaSourceImp> Ptr;
33

34 35 36 37 38 39 40 41 42 43 44
    /**
     * 构造函数
     * @param vhost 虚拟主机
     * @param app 应用名
     * @param id 流id
     * @param ringSize 环形缓存大小
     */
    RtmpMediaSourceImp(const string &vhost, const string &app, const string &id, int ringSize = RTMP_GOP_SIZE) : RtmpMediaSource(vhost, app, id, ringSize) {
        _demuxer = std::make_shared<RtmpDemuxer>();
        _demuxer->setTrackListener(this);
    }
xzl committed
45

46
    ~RtmpMediaSourceImp() = default;
xzl committed
47

48 49 50 51
    /**
     * 设置metadata
     */
    void setMetaData(const AMFValue &metadata) override{
52 53 54 55 56
        if(!_demuxer->loadMetaData(metadata)){
            //该metadata无效,需要重新生成
            _metadata = metadata;
            _recreate_metadata = true;
        }
57
        RtmpMediaSource::setMetaData(metadata);
58
    }
59

60 61 62
    /**
     * 输入rtmp并解析
     */
63
    void onWrite(RtmpPacket::Ptr pkt, bool = true) override {
64 65 66
        if (!_all_track_ready || _muxer->isEnabled()) {
            //未获取到所有Track后,或者开启转协议,那么需要解复用rtmp
            _demuxer->inputRtmp(pkt);
67
        }
68
        RtmpMediaSource::onWrite(std::move(pkt));
69
    }
70

71 72 73 74 75
    /**
     * 获取观看总人数,包括(hls/rtsp/rtmp)
     */
    int totalReaderCount() override{
        return readerCount() + (_muxer ? _muxer->totalReaderCount() : 0);
76
    }
77

78 79 80 81 82
    /**
     * 设置协议转换
     * @param enableHls  是否转换成hls
     * @param enableMP4  是否mp4录制
     */
83
    void setProtocolTranslation(bool enableHls, bool enableMP4) {
84
        //不重复生成rtmp
85
        _muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), true, false, enableHls, enableMP4);
86
        _muxer->setMediaListener(getListener());
87
        _muxer->setTrackListener(static_pointer_cast<RtmpMediaSourceImp>(shared_from_this()));
88
        //让_muxer对象拦截一部分事件(比如说录像相关事件)
89
        MediaSource::setListener(_muxer);
90

91 92 93 94
        for(auto &track : _demuxer->getTracks(false)){
            _muxer->addTrack(track);
            track->addDelegate(_muxer);
        }
95
    }
96

97 98 99
    /**
     * _demuxer触发的添加Track事件
     */
100
    void addTrack(const Track::Ptr &track) override {
101 102 103 104 105
        if(_muxer){
            _muxer->addTrack(track);
            track->addDelegate(_muxer);
        }
    }
106

107
    /**
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
     * _demuxer触发的Track添加完毕事件
     */
    void addTrackCompleted() override {
        if (_muxer) {
            _muxer->addTrackCompleted();
        }
    }

    void resetTracks() override {
        if (_muxer) {
            _muxer->resetTracks();
        }
    }

    /**
123 124 125
     * _muxer触发的所有Track就绪的事件
     */
    void onAllTrackReady() override{
126
        _all_track_ready = true;
127

xiongziliang committed
128
        if (_recreate_metadata) {
129
            //更新metadata
130
            for (auto &track : _muxer->getTracks(*this)) {
xiongziliang committed
131 132
                Metadata::addTrack(_metadata, track);
            }
133
            RtmpMediaSource::updateMetaData(_metadata);
134
        }
135
    }
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150
    /**
     * 设置事件监听器
     * @param listener 监听器
     */
    void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override{
        if (_muxer) {
            //_muxer对象不能处理的事件再给listener处理
            _muxer->setMediaListener(listener);
        } else {
            //未创建_muxer对象,事件全部给listener处理
            MediaSource::setListener(listener);
        }
    }

xzl committed
151
private:
152
    bool _all_track_ready = false;
153
    bool _recreate_metadata = false;
xiongziliang committed
154 155 156 157
    AMFValue _metadata;
    RtmpDemuxer::Ptr _demuxer;
    MultiMediaSourceMuxer::Ptr _muxer;

xzl committed
158
};
xiongziliang committed
159
} /* namespace mediakit */
xzl committed
160 161

#endif /* SRC_RTMP_RTMPTORTSPMEDIASOURCE_H_ */