Track.h 4.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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 */

#ifndef ZLMEDIAKIT_TRACK_H
#define ZLMEDIAKIT_TRACK_H

#include <memory>
#include <string>
#include "Frame.h"
#include "Util/RingBuffer.h"
#include "Rtsp/Rtsp.h"
using namespace toolkit;

namespace mediakit{

/**
 * 媒体通道描述类,也支持帧输入输出
 */
xiongziliang committed
26
class Track : public FrameDispatcher , public CodecInfo{
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
public:
    typedef std::shared_ptr<Track> Ptr;
    Track(){}

    virtual ~Track(){}

    /**
     * 是否准备好,准备好才能获取譬如sps pps等信息
     * @return
     */
    virtual bool ready() = 0;

    /**
     * 克隆接口,用于复制本对象用
     * 在调用该接口时只会复制派生类的信息
     * 环形缓存和代理关系不能拷贝,否则会关系紊乱
     * @return
     */
    virtual Track::Ptr clone() = 0;

    /**
xiongziliang committed
48 49 50 51 52 53
     * 生成sdp
     * @return  sdp对象
     */
    virtual Sdp::Ptr getSdp() = 0;

    /**
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
     * 复制拷贝,只能拷贝派生类的信息,
     * 环形缓存和代理关系不能拷贝,否则会关系紊乱
     * @param that
     */
    Track(const Track &that){}
};

/**
 * 视频通道描述Track类,支持获取宽高fps信息
 */
class VideoTrack : public Track {
public:
    typedef std::shared_ptr<VideoTrack> Ptr;

    /**
     * 返回视频高度
     * @return
     */
72
    virtual int getVideoHeight() const {return 0;};
73 74 75 76 77

    /**
     * 返回视频宽度
     * @return
     */
78
    virtual int getVideoWidth() const {return 0;};
79 80 81 82 83

    /**
     * 返回视频fps
     * @return
     */
84
    virtual float getVideoFps() const {return 0;};
85 86 87 88 89 90 91 92 93 94 95 96 97
};

/**
 * 音频Track派生类,支持采样率通道数,采用位数信息
 */
class AudioTrack : public Track {
public:
    typedef std::shared_ptr<AudioTrack> Ptr;

    /**
     * 返回音频采样率
     * @return
     */
98
    virtual int getAudioSampleRate() const  {return 0;};
99 100 101 102 103

    /**
     * 返回音频采样位数,一般为16或8
     * @return
     */
104
    virtual int getAudioSampleBit() const {return 0;};
105 106 107 108 109

    /**
     * 返回音频通道数
     * @return
     */
110
    virtual int getAudioChannel() const {return 0;};
111 112
};

xiongziliang committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
class AudioTrackImp : public AudioTrack{
public:
    typedef std::shared_ptr<AudioTrackImp> Ptr;

    /**
     * 构造函数
     * @param codecId 编码类型
     * @param sample_rate 采样率(HZ)
     * @param channels 通道数
     * @param sample_bit 采样位数,一般为16
     */
    AudioTrackImp(CodecId codecId,int sample_rate, int channels, int sample_bit){
        _codecid = codecId;
        _sample_rate = sample_rate;
        _channels = channels;
        _sample_bit = sample_bit;
    }

    /**
     * 返回编码类型
     */
    CodecId getCodecId() const override{
        return _codecid;
    }

    /**
     * 是否已经初始化
     */
    bool ready() override {
        return true;
    }

    /**
     * 返回音频采样率
     */
    int getAudioSampleRate() const override{
        return _sample_rate;
    }

    /**
     * 返回音频采样位数,一般为16或8
     */
    int getAudioSampleBit() const override{
        return _sample_bit;
    }

    /**
     * 返回音频通道数
     */
    int getAudioChannel() const override{
        return _channels;
    }
private:
    CodecId _codecid;
    int _sample_rate;
    int _channels;
    int _sample_bit;
};
171

xiongziliang committed
172 173 174 175 176 177
class TrackSource{
public:
    TrackSource(){}
    virtual ~TrackSource(){}

    /**
178 179 180
     * 获取全部的Track
     * @param trackReady 是否获取全部已经准备好的Track
     */
xiongziliang committed
181
    virtual vector<Track::Ptr> getTracks(bool trackReady = true) const = 0;
xiongziliang committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    /**
     * 获取特定Track
     * @param type track类型
     * @param trackReady 是否获取全部已经准备好的Track
     */
    Track::Ptr getTrack(TrackType type , bool trackReady = true) const {
        auto tracks = getTracks(trackReady);
        for(auto &track : tracks){
            if(track->getTrackType() == type){
                return track;
            }
        }
        return nullptr;
    }
};

199
}//namespace mediakit
xiongziliang committed
200
#endif //ZLMEDIAKIT_TRACK_H