RtmpMediaSourceMuxer.h 2.69 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
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.
9
 */
xiongziliang committed
10

11 12
#ifndef ZLMEDIAKIT_RTMPMEDIASOURCEMUXER_H
#define ZLMEDIAKIT_RTMPMEDIASOURCEMUXER_H
xiongziliang committed
13

xiongziliang committed
14
#include "RtmpMuxer.h"
xiongziliang committed
15 16 17 18
#include "Rtmp/RtmpMediaSource.h"

namespace mediakit {

19 20
class RtmpMediaSourceMuxer : public RtmpMuxer, public MediaSourceEventInterceptor,
                             public std::enable_shared_from_this<RtmpMediaSourceMuxer> {
xiongziliang committed
21
public:
22 23 24 25 26
    typedef std::shared_ptr<RtmpMediaSourceMuxer> Ptr;

    RtmpMediaSourceMuxer(const string &vhost,
                         const string &strApp,
                         const string &strId,
27
                         const TitleMeta::Ptr &title = nullptr) : RtmpMuxer(title){
xiongziliang committed
28 29
        _media_src = std::make_shared<RtmpMediaSource>(vhost, strApp, strId);
        getRtmpRing()->setDelegate(_media_src);
30
    }
31 32

    ~RtmpMediaSourceMuxer() override{}
33 34

    void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
35
        setDelegate(listener);
36
        _media_src->setListener(shared_from_this());
37
    }
xiongziliang committed
38

39
    void setTimeStamp(uint32_t stamp){
xiongziliang committed
40
        _media_src->setTimeStamp(stamp);
41 42
    }

43
    int readerCount() const{
xiongziliang committed
44
        return _media_src->readerCount();
45
    }
xiongziliang committed
46 47

    void onAllTrackReady(){
48
        makeConfigPacket();
xiongziliang committed
49
        _media_src->setMetaData(getMetadata());
50
    }
xiongziliang committed
51

52
    void onReaderChanged(MediaSource &sender, int size) override {
53 54 55
        GET_CONFIG(bool, rtmp_demand, General::kRtmpDemand);
        _enabled = rtmp_demand ? size : true;
        if (!size && rtmp_demand) {
56 57 58 59 60
            _clear_cache = true;
        }
        MediaSourceEventInterceptor::onReaderChanged(sender, size);
    }

61
    bool inputFrame(const Frame::Ptr &frame) override {
62 63
        GET_CONFIG(bool, rtmp_demand, General::kRtmpDemand);
        if (_clear_cache && rtmp_demand) {
64 65 66
            _clear_cache = false;
            _media_src->clearCache();
        }
67
        if (_enabled || !rtmp_demand) {
68
            return RtmpMuxer::inputFrame(frame);
69
        }
70
        return false;
71 72 73
    }

    bool isEnabled() {
74
        GET_CONFIG(bool, rtmp_demand, General::kRtmpDemand);
75
        //缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
76
        return rtmp_demand ? (_clear_cache ? true : _enabled) : true;
77 78
    }

xiongziliang committed
79
private:
80 81
    bool _enabled = true;
    bool _clear_cache = false;
xiongziliang committed
82
    RtmpMediaSource::Ptr _media_src;
xiongziliang committed
83 84
};

85 86 87

}//namespace mediakit
#endif //ZLMEDIAKIT_RTMPMEDIASOURCEMUXER_H