RtspMediaSourceMuxer.h 2.36 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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
        _listener = 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        _media_src->setSdp(getSdp());
    }

    void onReaderChanged(MediaSource &sender, int size) override {
        _enabled = size;
        if (!size) {
            _clear_cache = true;
        }
        MediaSourceEventInterceptor::onReaderChanged(sender, size);
    }

    void inputFrame(const Frame::Ptr &frame) override {
        if (_clear_cache) {
            _clear_cache = false;
            _media_src->clearCache();
        }
        if (_enabled) {
            RtspMuxer::inputFrame(frame);
        }
    }

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

74
private:
75 76 77
    bool _enabled = true;
    bool _clear_cache = false;
    RtspMediaSource::Ptr _media_src;
xiongziliang committed
78 79
};

80 81 82

}//namespace mediakit
#endif //ZLMEDIAKIT_RTSPMEDIASOURCEMUXER_H