mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-04 01:37:33 +08:00
支持bitrate
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
#include "Extension/Factory.h"
|
||||
namespace mediakit{
|
||||
|
||||
VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){
|
||||
VideoMeta::VideoMeta(const VideoTrack::Ptr &video){
|
||||
if(video->getVideoWidth() > 0 ){
|
||||
_metadata.set("width", video->getVideoWidth());
|
||||
}
|
||||
@@ -22,13 +22,17 @@ VideoMeta::VideoMeta(const VideoTrack::Ptr &video,int datarate ){
|
||||
if(video->getVideoFps() > 0 ){
|
||||
_metadata.set("framerate", video->getVideoFps());
|
||||
}
|
||||
_metadata.set("videodatarate", datarate);
|
||||
if (video->getBitRate()) {
|
||||
_metadata.set("videodatarate", video->getBitRate() / 1024);
|
||||
}
|
||||
_codecId = video->getCodecId();
|
||||
_metadata.set("videocodecid", Factory::getAmfByCodecId(_codecId));
|
||||
}
|
||||
|
||||
AudioMeta::AudioMeta(const AudioTrack::Ptr &audio,int datarate){
|
||||
_metadata.set("audiodatarate", datarate);
|
||||
AudioMeta::AudioMeta(const AudioTrack::Ptr &audio){
|
||||
if (audio->getBitRate()) {
|
||||
_metadata.set("audiodatarate", audio->getBitRate() / 1024);
|
||||
}
|
||||
if(audio->getAudioSampleRate() > 0){
|
||||
_metadata.set("audiosamplerate", audio->getAudioSampleRate());
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ class VideoMeta : public Metadata{
|
||||
public:
|
||||
typedef std::shared_ptr<VideoMeta> Ptr;
|
||||
|
||||
VideoMeta(const VideoTrack::Ptr &video,int datarate = 5000);
|
||||
VideoMeta(const VideoTrack::Ptr &video);
|
||||
virtual ~VideoMeta(){}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
@@ -275,7 +275,7 @@ class AudioMeta : public Metadata{
|
||||
public:
|
||||
typedef std::shared_ptr<AudioMeta> Ptr;
|
||||
|
||||
AudioMeta(const AudioTrack::Ptr &audio,int datarate = 160);
|
||||
AudioMeta(const AudioTrack::Ptr &audio);
|
||||
|
||||
virtual ~AudioMeta(){}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
|
||||
int audiosamplerate = 0;
|
||||
int audiochannels = 0;
|
||||
int audiosamplesize = 0;
|
||||
int videodatarate = 0;
|
||||
int audiodatarate = 0;
|
||||
const AMFValue *audiocodecid = nullptr;
|
||||
const AMFValue *videocodecid = nullptr;
|
||||
val.object_for_each([&](const string &key, const AMFValue &val) {
|
||||
@@ -48,16 +50,24 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
|
||||
audiocodecid = &val;
|
||||
return;
|
||||
}
|
||||
if (key == "audiodatarate") {
|
||||
audiodatarate = val.as_integer();
|
||||
return;
|
||||
}
|
||||
if (key == "videodatarate") {
|
||||
videodatarate = val.as_integer();
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (videocodecid) {
|
||||
//有视频
|
||||
ret = true;
|
||||
makeVideoTrack(*videocodecid);
|
||||
makeVideoTrack(*videocodecid, videodatarate * 1024);
|
||||
}
|
||||
if (audiocodecid) {
|
||||
//有音频
|
||||
ret = true;
|
||||
makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize);
|
||||
makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize, audiodatarate * 1024);
|
||||
}
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << ex.what();
|
||||
@@ -71,7 +81,7 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
if (!_try_get_video_track) {
|
||||
_try_get_video_track = true;
|
||||
auto codec = AMFValue(pkt->getMediaType());
|
||||
makeVideoTrack(codec);
|
||||
makeVideoTrack(codec, 0);
|
||||
}
|
||||
if (_video_rtmp_decoder) {
|
||||
_video_rtmp_decoder->inputRtmp(pkt);
|
||||
@@ -83,7 +93,7 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
if (!_try_get_audio_track) {
|
||||
_try_get_audio_track = true;
|
||||
auto codec = AMFValue(pkt->getMediaType());
|
||||
makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit());
|
||||
makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit(), 0);
|
||||
}
|
||||
if (_audio_rtmp_decoder) {
|
||||
_audio_rtmp_decoder->inputRtmp(pkt);
|
||||
@@ -94,10 +104,11 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
}
|
||||
}
|
||||
|
||||
void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec) {
|
||||
void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec, int bit_rate) {
|
||||
//生成Track对象
|
||||
_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getVideoTrackByAmf(videoCodec));
|
||||
if (_videoTrack) {
|
||||
_videoTrack->setBitRate(bit_rate);
|
||||
//生成rtmpCodec对象以便解码rtmp
|
||||
_video_rtmp_decoder = Factory::getRtmpCodecByTrack(_videoTrack, false);
|
||||
if (_video_rtmp_decoder) {
|
||||
@@ -112,10 +123,11 @@ void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec) {
|
||||
}
|
||||
}
|
||||
|
||||
void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit) {
|
||||
void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit, int bit_rate) {
|
||||
//生成Track对象
|
||||
_audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getAudioTrackByAmf(audioCodec, sample_rate, channels, sample_bit));
|
||||
if (_audioTrack) {
|
||||
_audioTrack->setBitRate(bit_rate);
|
||||
//生成rtmpCodec对象以便解码rtmp
|
||||
_audio_rtmp_decoder = Factory::getRtmpCodecByTrack(_audioTrack, false);
|
||||
if (_audio_rtmp_decoder) {
|
||||
|
||||
@@ -39,8 +39,8 @@ public:
|
||||
void inputRtmp(const RtmpPacket::Ptr &pkt);
|
||||
|
||||
private:
|
||||
void makeVideoTrack(const AMFValue &val);
|
||||
void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit);
|
||||
void makeVideoTrack(const AMFValue &val, int bit_rate);
|
||||
void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit, int bit_rate);
|
||||
|
||||
private:
|
||||
bool _try_get_video_track = false;
|
||||
|
||||
@@ -119,7 +119,8 @@ public:
|
||||
* @param pkt rtmp包
|
||||
*/
|
||||
void onWrite(RtmpPacket::Ptr pkt, bool = true) override {
|
||||
_speed += pkt->size();
|
||||
bool is_video = pkt->type_id == MSG_VIDEO;
|
||||
_speed[is_video ? TrackVideo : TrackAudio] += pkt->size();
|
||||
//保存当前时间戳
|
||||
switch (pkt->type_id) {
|
||||
case MSG_VIDEO : _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break;
|
||||
@@ -153,7 +154,6 @@ public:
|
||||
}
|
||||
}
|
||||
bool key = pkt->isVideoKeyFrame();
|
||||
bool is_video = pkt->type_id == MSG_VIDEO;
|
||||
auto stamp = pkt->time_stamp;
|
||||
PacketCache<RtmpPacket>::inputPacket(stamp, is_video, std::move(pkt), key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user