MediaSink.h 3.55 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 10 11 12 13 14 15
 */

#ifndef ZLMEDIAKIT_MEDIASINK_H
#define ZLMEDIAKIT_MEDIASINK_H

#include <mutex>
#include <memory>
16
#include "Util/TimeTicker.h"
17 18
#include "Extension/Frame.h"
#include "Extension/Track.h"
19 20 21 22 23 24

using namespace std;
using namespace toolkit;

namespace mediakit{

xiongziliang committed
25 26
class MediaSinkInterface : public FrameWriterInterface {
public:
xiongziliang committed
27 28
    typedef std::shared_ptr<MediaSinkInterface> Ptr;

xiongziliang committed
29 30 31 32 33 34 35 36 37 38 39
    MediaSinkInterface(){};
    virtual ~MediaSinkInterface(){};

    /**
     * 添加track,内部会调用Track的clone方法
     * 只会克隆sps pps这些信息 ,而不会克隆Delegate相关关系
     * @param track
     */
    virtual void addTrack(const Track::Ptr & track) = 0;

    /**
xiongziliang committed
40 41 42 43 44
     * 添加所有Track完毕
     */
    virtual void addTrackCompleted() {}

    /**
xiongziliang committed
45 46 47 48 49
     * 重置track
     */
    virtual void resetTracks() = 0;
};

xiongziliang committed
50 51 52 53
/**
 * 该类的作用是等待Track ready()返回true也就是就绪后再通知派生类进行下一步的操作
 * 目的是输入Frame前由Track截取处理下,以便获取有效的信息(譬如sps pps aa_cfg)
 */
xiongziliang committed
54
class MediaSink : public MediaSinkInterface , public TrackSource{
55 56 57 58 59 60 61 62 63
public:
    typedef std::shared_ptr<MediaSink> Ptr;
    MediaSink(){}
    virtual ~MediaSink(){}

    /**
     * 输入frame
     * @param frame
     */
64
    void inputFrame(const Frame::Ptr &frame) override;
65 66 67 68 69 70

    /**
     * 添加track,内部会调用Track的clone方法
     * 只会克隆sps pps这些信息 ,而不会克隆Delegate相关关系
     * @param track
     */
xiongziliang committed
71
    void addTrack(const Track::Ptr & track) override;
72

73
    /**
74 75 76 77
     * 添加Track完毕,如果是单Track,会最多等待3秒才会触发onAllTrackReady
     * 这样会增加生成流的延时,如果添加了音视频双Track,那么可以不调用此方法
     * 否则为了降低流注册延时,请手动调用此方法
     */
xiongziliang committed
78
    void addTrackCompleted() override;
79 80

    /**
81 82
     * 重置track
     */
xiongziliang committed
83
    void resetTracks() override;
84 85

    /**
xiongziliang committed
86
     * 获取所有Track
87
     * @param trackReady 是否获取已经准备好的Track
88
     */
xiongziliang committed
89
    vector<Track::Ptr> getTracks(bool trackReady = true) const override ;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
protected:
    /**
     * 某track已经准备好,其ready()状态返回true,
     * 此时代表可以获取其例如sps pps等相关信息了
     * @param track
     */
    virtual void onTrackReady(const Track::Ptr & track) {};

    /**
     * 所有Track已经准备好,
     */
    virtual void onAllTrackReady() {};

    /**
     * 某Track输出frame,在onAllTrackReady触发后才会调用此方法
     * @param frame
     */
    virtual void onTrackFrame(const Frame::Ptr &frame) {};
private:
109 110 111 112
    /**
     * 触发onAllTrackReady事件
     */
    void emitAllTrackReady();
113 114 115 116 117 118

    /**
     * 检查track是否准备完毕
     */
    void checkTrackIfReady(const Track::Ptr &track);
    void checkTrackIfReady_l(const Track::Ptr &track);
119
private:
120
    mutable recursive_mutex _mtx;
xiongziliang committed
121
    unordered_map<int,Track::Ptr> _track_map;
xiongziliang committed
122 123 124
    unordered_map<int,List<Frame::Ptr> > _frame_unread;
    unordered_map<int,function<void()> > _track_ready_callback;
    bool _all_track_ready = false;
125
    Ticker _ticker;
126
    int _max_track_size = 2;
127 128 129 130 131 132
};


}//namespace mediakit

#endif //ZLMEDIAKIT_MEDIASINK_H