ZLMediaKit/src/Extension/Track.h

369 lines
9.3 KiB
C++
Raw Normal View History

2018-10-30 14:59:42 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2018-10-30 14:59:42 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2018-10-30 14:59:42 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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.
2018-10-30 14:59:42 +08:00
*/
#ifndef ZLMEDIAKIT_TRACK_H
#define ZLMEDIAKIT_TRACK_H
#include <memory>
#include <string>
#include "Frame.h"
#include "Rtsp/Rtsp.h"
namespace mediakit{
/**
*
* Media channel description class, also supports frame input and output
* [AUTO-TRANSLATED:a3acd089]
2018-10-30 14:59:42 +08:00
*/
2023-12-09 16:23:51 +08:00
class Track : public FrameDispatcher, public CodecInfo {
2018-10-30 14:59:42 +08:00
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<Track>;
2023-12-09 16:23:51 +08:00
/**
*
* Default constructor
* [AUTO-TRANSLATED:acda54ab]
2023-12-09 16:23:51 +08:00
*/
Track() = default;
2023-12-09 16:23:51 +08:00
/**
*
*
* Copy, only copy information of derived classes,
* Circular buffer and proxy relationships cannot be copied, otherwise the relationship will be disordered
* [AUTO-TRANSLATED:308e6502]
2023-12-09 16:23:51 +08:00
*/
2023-12-09 22:34:22 +08:00
Track(const Track &that) {
_bit_rate = that._bit_rate;
setIndex(that.getIndex());
}
2018-10-30 14:59:42 +08:00
/**
* sps pps等信息
* Whether it is ready, it can be used to get information such as sps pps
* [AUTO-TRANSLATED:6d819ef7]
2018-10-30 14:59:42 +08:00
*/
2023-12-09 16:23:51 +08:00
virtual bool ready() const = 0;
2018-10-30 14:59:42 +08:00
/**
*
*
*
* Clone interface, used to copy this object
* When calling this interface, only the information of the derived class will be copied
* Circular buffer and proxy relationships cannot be copied, otherwise the relationship will be disordered
* [AUTO-TRANSLATED:270874c6]
2018-10-30 14:59:42 +08:00
*/
2023-12-09 16:23:51 +08:00
virtual Track::Ptr clone() const = 0;
2018-10-30 14:59:42 +08:00
/**
* track信息sps/pps解析
* Update track information, such as triggering sps/pps parsing
* [AUTO-TRANSLATED:324879ef]
*/
virtual bool update() { return false; }
2019-06-28 16:12:39 +08:00
/**
* sdp
2023-12-09 16:23:51 +08:00
* @return sdp对象
* Generate sdp
* @return sdp object
* [AUTO-TRANSLATED:3ab2fd30]
2019-06-28 16:12:39 +08:00
*/
2023-12-09 16:23:51 +08:00
virtual Sdp::Ptr getSdp(uint8_t payload_type) const = 0;
/**
* extra data, rtmp/mp4生成
* Get extra data, generally used for rtmp/mp4 generation
* [AUTO-TRANSLATED:d8ff2cd5]
2023-12-09 16:23:51 +08:00
*/
virtual toolkit::Buffer::Ptr getExtraData() const { return nullptr; }
/**
* extra data
* Set extra data,
* [AUTO-TRANSLATED:9e551857]
2023-12-09 16:23:51 +08:00
*/
virtual void setExtraData(const uint8_t *data, size_t size) {}
2019-06-28 16:12:39 +08:00
2020-12-05 12:22:17 +08:00
/**
*
* @return
* Return bitrate
* @return Bitrate
* [AUTO-TRANSLATED:265dda35]
2020-12-05 12:22:17 +08:00
*/
virtual int getBitRate() const { return _bit_rate; }
/**
*
* @param bit_rate
* Set bitrate
* @param bit_rate Bitrate
* [AUTO-TRANSLATED:77a43064]
2020-12-05 12:22:17 +08:00
*/
virtual void setBitRate(int bit_rate) { _bit_rate = bit_rate; }
private:
int _bit_rate = 0;
2018-10-30 14:59:42 +08:00
};
/**
* Track类fps信息
* Video channel description Track class, supports getting width, height and fps information
* [AUTO-TRANSLATED:8d1893c5]
2018-10-30 14:59:42 +08:00
*/
class VideoTrack : public Track {
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<VideoTrack>;
2018-10-30 14:59:42 +08:00
/**
*
* Return video height
* [AUTO-TRANSLATED:b24aabc0]
2018-10-30 14:59:42 +08:00
*/
2023-02-05 22:00:36 +08:00
virtual int getVideoHeight() const { return 0; }
2018-10-30 14:59:42 +08:00
/**
*
* Return video width
* [AUTO-TRANSLATED:2f3bb6e3]
2018-10-30 14:59:42 +08:00
*/
2023-02-05 22:00:36 +08:00
virtual int getVideoWidth() const { return 0; }
2018-10-30 14:59:42 +08:00
/**
* fps
* Return video fps
* [AUTO-TRANSLATED:ced99aef]
2018-10-30 14:59:42 +08:00
*/
2023-02-05 22:00:36 +08:00
virtual float getVideoFps() const { return 0; }
/**
* sps/pps
* Return related sps/pps, etc.
* [AUTO-TRANSLATED:30fc4f63]
*/
virtual std::vector<Frame::Ptr> getConfigFrames() const { return std::vector<Frame::Ptr>{}; }
2018-10-30 14:59:42 +08:00
};
2023-12-09 16:23:51 +08:00
class VideoTrackImp : public VideoTrack {
public:
using Ptr = std::shared_ptr<VideoTrackImp>;
/**
*
* @param codec_id
* @param width
* @param height
* @param fps
* Constructor
* @param codec_id Encoding type
* @param width Width
* @param height Height
* @param fps Frame rate
* [AUTO-TRANSLATED:b3d1ef4d]
2023-12-09 16:23:51 +08:00
*/
VideoTrackImp(CodecId codec_id, int width, int height, int fps) {
_codec_id = codec_id;
_width = width;
_height = height;
_fps = fps;
}
2024-01-06 18:17:51 +08:00
int getVideoWidth() const override { return _width; }
int getVideoHeight() const override { return _height; }
2023-12-09 16:23:51 +08:00
float getVideoFps() const override { return _fps; }
bool ready() const override { return true; }
Track::Ptr clone() const override { return std::make_shared<VideoTrackImp>(*this); }
Sdp::Ptr getSdp(uint8_t payload_type) const override { return nullptr; }
CodecId getCodecId() const override { return _codec_id; }
private:
CodecId _codec_id;
int _width = 0;
int _height = 0;
float _fps = 0;
};
2018-10-30 14:59:42 +08:00
/**
* Track派生类
* Audio Track derived class, supports sampling rate, number of channels, and sampling bit information
* [AUTO-TRANSLATED:5f57819d]
2018-10-30 14:59:42 +08:00
*/
class AudioTrack : public Track {
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<AudioTrack>;
2018-10-30 14:59:42 +08:00
/**
*
* Return audio sampling rate
* [AUTO-TRANSLATED:9af5a0a4]
2018-10-30 14:59:42 +08:00
*/
virtual int getAudioSampleRate() const {return 0;};
2018-10-30 14:59:42 +08:00
/**
* 168
* Return audio sampling bit depth, generally 16 or 8
* [AUTO-TRANSLATED:5fedc65d]
2018-10-30 14:59:42 +08:00
*/
virtual int getAudioSampleBit() const {return 0;};
2018-10-30 14:59:42 +08:00
/**
*
* Return audio number of channels
* [AUTO-TRANSLATED:2613b317]
2018-10-30 14:59:42 +08:00
*/
virtual int getAudioChannel() const {return 0;};
2018-10-30 14:59:42 +08:00
};
2020-05-11 22:33:10 +08:00
class AudioTrackImp : public AudioTrack{
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<AudioTrackImp>;
2020-05-11 22:33:10 +08:00
/**
*
* @param codecId
* @param sample_rate (HZ)
* @param channels
* @param sample_bit 16
* Constructor
* @param codecId Encoding type
* @param sample_rate Sampling rate (HZ)
* @param channels Number of channels
* @param sample_bit Sampling bit depth, generally 16
* [AUTO-TRANSLATED:0ad0211f]
2020-05-11 22:33:10 +08:00
*/
2023-12-09 16:23:51 +08:00
AudioTrackImp(CodecId codecId, int sample_rate, int channels, int sample_bit){
2020-05-11 22:33:10 +08:00
_codecid = codecId;
_sample_rate = sample_rate;
_channels = channels;
_sample_bit = sample_bit;
}
/**
*
* Return encoding type
* [AUTO-TRANSLATED:c8731864]
2020-05-11 22:33:10 +08:00
*/
CodecId getCodecId() const override{
return _codecid;
}
/**
*
* Whether it has been initialized
* [AUTO-TRANSLATED:5dc6693e]
2020-05-11 22:33:10 +08:00
*/
2023-12-09 16:23:51 +08:00
bool ready() const override {
return true;
2020-05-11 22:33:10 +08:00
}
/**
*
* Return audio sampling rate
* [AUTO-TRANSLATED:9af5a0a4]
2020-05-11 22:33:10 +08:00
*/
int getAudioSampleRate() const override{
return _sample_rate;
}
/**
* 168
* Return audio sampling bit depth, generally 16 or 8
* [AUTO-TRANSLATED:5fedc65d]
2020-05-11 22:33:10 +08:00
*/
int getAudioSampleBit() const override{
return _sample_bit;
}
/**
*
* Return audio number of channels
* [AUTO-TRANSLATED:2613b317]
2020-05-11 22:33:10 +08:00
*/
int getAudioChannel() const override{
return _channels;
}
2023-12-09 16:23:51 +08:00
Track::Ptr clone() const override { return std::make_shared<AudioTrackImp>(*this); }
Sdp::Ptr getSdp(uint8_t payload_type) const override { return nullptr; }
2020-05-11 22:33:10 +08:00
private:
CodecId _codecid;
int _sample_rate;
int _channels;
int _sample_bit;
};
2018-10-30 14:59:42 +08:00
2023-12-09 16:23:51 +08:00
class TrackSource {
2019-12-03 12:32:57 +08:00
public:
virtual ~TrackSource() = default;
2019-12-03 12:32:57 +08:00
/**
2020-03-20 11:51:24 +08:00
* Track
* @param trackReady Track
* Get all Tracks
* @param trackReady Whether to get all ready Tracks
* [AUTO-TRANSLATED:f0779985]
2020-03-20 11:51:24 +08:00
*/
virtual std::vector<Track::Ptr> getTracks(bool trackReady = true) const = 0;
2019-12-03 12:32:57 +08:00
/**
* Track
* @param type track类型
* @param trackReady Track
* Get specific Track
* @param type Track type
* @param trackReady Whether to get all ready Tracks
* [AUTO-TRANSLATED:c50781b9]
2019-12-03 12:32:57 +08:00
*/
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;
}
};
2018-10-30 14:59:42 +08:00
}//namespace mediakit
2020-05-11 22:33:10 +08:00
#endif //ZLMEDIAKIT_TRACK_H