RtspMediaSourceMuxer.h 2.62 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_RTSPMEDIASOURCEMUXER_H
#define ZLMEDIAKIT_RTSPMEDIASOURCEMUXER_H
xiongziliang committed
13

xiongziliang committed
14
#include "RtspMuxer.h"
xiongziliang committed
15 16 17 18
#include "Rtsp/RtspMediaSource.h"

namespace mediakit {

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

    RtspMediaSourceMuxer(const string &vhost,
                         const string &strApp,
                         const string &strId,
27
                         const TitleSdp::Ptr &title = nullptr) : RtspMuxer(title){
28 29
        _media_src = std::make_shared<RtspMediaSource>(vhost,strApp,strId);
        getRtpRing()->setDelegate(_media_src);
30
    }
31 32

    ~RtspMediaSourceMuxer() 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
    int readerCount() const{
40
        return _media_src->readerCount();
41
    }
xiongziliang committed
42

xiongziliang committed
43
    void setTimeStamp(uint32_t stamp){
44
        _media_src->setTimeStamp(stamp);
45
    }
xiongziliang committed
46 47

    void onAllTrackReady(){
48 49 50 51
        _media_src->setSdp(getSdp());
    }

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

    void inputFrame(const Frame::Ptr &frame) override {
61 62
        GET_CONFIG(bool, rtsp_demand, General::kRtspDemand);
        if (_clear_cache && rtsp_demand) {
63 64 65
            _clear_cache = false;
            _media_src->clearCache();
        }
66
        if (_enabled || !rtsp_demand) {
67 68 69 70 71
            RtspMuxer::inputFrame(frame);
        }
    }

    bool isEnabled() {
72
        GET_CONFIG(bool, rtsp_demand, General::kRtspDemand);
73
        //缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
74
        return rtsp_demand ? (_clear_cache ? true : _enabled) : true;
75
    }
xiongziliang committed
76

77
private:
78 79 80
    bool _enabled = true;
    bool _clear_cache = false;
    RtspMediaSource::Ptr _media_src;
xiongziliang committed
81 82
};

83 84 85

}//namespace mediakit
#endif //ZLMEDIAKIT_RTSPMEDIASOURCEMUXER_H