Merge branch 'master' of github.com:ZLMediaKit/ZLMediaKit into feature/transcode2

This commit is contained in:
cqm
2025-01-01 22:31:00 +08:00
51 changed files with 3672 additions and 125 deletions

View File

@@ -26,19 +26,20 @@ void G711RtpEncoder::setOpt(int opt, const toolkit::Any &param) {
bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
auto ptr = frame->data() + frame->prefixSize();
auto size = frame->size() - frame->prefixSize();
_buffer.append(ptr, size);
_in_size += size;
_in_pts = frame->pts();
uint64_t in_pts;
if (!_pkt_bytes) {
in_pts = frame->pts();
// G711压缩率固定是2倍
_pkt_bytes = _pkt_dur_ms * _channels * (_sample_bit / 8) * _sample_rate / 1000 / 2;
} else {
in_pts = frame->pts() - _buffer.size() * _pkt_dur_ms / _pkt_bytes;
}
_buffer.append(ptr, size);
while (_buffer.size() >= _pkt_bytes) {
_out_size += _pkt_bytes;
auto pts = _in_pts - (_in_size - _out_size) * (_pkt_dur_ms / (float)_pkt_bytes);
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, _buffer.data(), _pkt_bytes, false, pts), false);
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, _buffer.data(), _pkt_bytes, false, in_pts), false);
in_pts += _pkt_dur_ms;
_buffer.erase(0, _pkt_bytes);
}
return true;

View File

@@ -59,9 +59,6 @@ private:
uint32_t _pkt_dur_ms = 20;
uint32_t _pkt_bytes = 0;
uint64_t _in_size = 0;
uint64_t _out_size = 0;
int64_t _in_pts = 0;
toolkit::BufferLikeString _buffer;
};

View File

@@ -267,31 +267,32 @@ bool H264Track::inputFrame_l(const Frame::Ptr &frame) {
switch (type) {
case H264Frame::NAL_SPS: {
_sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
_latest_is_config_frame = true;
_latest_is_sps = true;
ret = VideoTrack::inputFrame(frame);
break;
}
case H264Frame::NAL_PPS: {
_pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
_latest_is_config_frame = true;
_latest_is_pps = true;
ret = VideoTrack::inputFrame(frame);
break;
}
default:
// 避免识别不出关键帧 [AUTO-TRANSLATED:8eb84679]
// Avoid not being able to recognize keyframes
if (_latest_is_config_frame && !frame->dropAble()) {
if (latestIsConfigFrame() && !frame->dropAble()) {
if (!frame->keyFrame()) {
const_cast<Frame::Ptr &>(frame) = std::make_shared<FrameCacheAble>(frame, true);
}
}
// 判断是否是I帧, 并且如果是,那判断前面是否插入过config帧, 如果插入过就不插入了 [AUTO-TRANSLATED:40733cd8]
// Determine if it is an I frame, and if it is, determine if a config frame has been inserted before, and if it has been inserted, do not insert it
if (frame->keyFrame() && !_latest_is_config_frame) {
if (frame->keyFrame() && !latestIsConfigFrame()) {
insertConfigFrame(frame);
}
if(!frame->dropAble()){
_latest_is_config_frame = false;
_latest_is_pps = false;
_latest_is_sps = false;
}
ret = VideoTrack::inputFrame(frame);
break;
@@ -313,6 +314,10 @@ void H264Track::insertConfigFrame(const Frame::Ptr &frame) {
}
}
bool H264Track::latestIsConfigFrame(){
return _latest_is_sps && _latest_is_pps;
}
class H264Sdp : public Sdp {
public:
H264Sdp(const string &strSPS, const string &strPPS, int payload_type, int bitrate) : Sdp(90000, payload_type) {

View File

@@ -146,8 +146,11 @@ private:
bool inputFrame_l(const Frame::Ptr &frame);
void insertConfigFrame(const Frame::Ptr &frame);
bool latestIsConfigFrame();
private:
bool _latest_is_config_frame = false;
bool _latest_is_pps = false;
bool _latest_is_sps = false;
int _width = 0;
int _height = 0;
float _fps = 0;

77
ext-codec/MP3.cpp Normal file
View File

@@ -0,0 +1,77 @@
/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like 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.
*/
#include "MP3.h"
#include "MP3Rtp.h"
#include "Extension/Factory.h"
#include "Extension/CommonRtp.h"
#include "Extension/CommonRtmp.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
Sdp::Ptr MP3Track::getSdp(uint8_t payload_type) const {
return std::make_shared<DefaultSdp>(payload_type, *this);
}
Track::Ptr MP3Track::clone() const {
return std::make_shared<MP3Track>(*this);
}
namespace {
CodecId getCodec() {
return CodecMP3;
}
Track::Ptr getTrackByCodecId(int sample_rate, int channels, int sample_bit) {
return std::make_shared<MP3Track>(sample_rate, channels);
}
Track::Ptr getTrackBySdp(const SdpTrack::Ptr &track) {
return std::make_shared<MP3Track>(track->_samplerate, track->_channel);
}
RtpCodec::Ptr getRtpEncoderByCodecId(uint8_t pt) {
return std::make_shared<MP3RtpEncoder>();
}
RtpCodec::Ptr getRtpDecoderByCodecId() {
return std::make_shared<MP3RtpDecoder>();
}
RtmpCodec::Ptr getRtmpEncoderByTrack(const Track::Ptr &track) {
return std::make_shared<CommonRtmpEncoder>(track);
}
RtmpCodec::Ptr getRtmpDecoderByTrack(const Track::Ptr &track) {
return std::make_shared<CommonRtmpDecoder>(track);
}
Frame::Ptr getFrameFromPtr(const char *data, size_t bytes, uint64_t dts, uint64_t pts) {
return std::make_shared<FrameFromPtr>(CodecMP3, (char *)data, bytes, dts, pts);
}
} // namespace
CodecPlugin mp3_plugin = { getCodec,
getTrackByCodecId,
getTrackBySdp,
getRtpEncoderByCodecId,
getRtpDecoderByCodecId,
getRtmpEncoderByTrack,
getRtmpDecoderByTrack,
getFrameFromPtr };
}//namespace mediakit

33
ext-codec/MP3.h Normal file
View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like 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.
*/
#ifndef ZLMEDIAKIT_MP3_H
#define ZLMEDIAKIT_MP3_H
#include "Extension/Frame.h"
#include "Extension/Track.h"
namespace mediakit {
/**
* MP3音频通道
*/
class MP3Track : public AudioTrackImp{
public:
using Ptr = std::shared_ptr<MP3Track>;
MP3Track(int sample_rate, int channels) : AudioTrackImp(CodecMP3,sample_rate,channels,16){}
private:
Sdp::Ptr getSdp(uint8_t payload_type) const override;
Track::Ptr clone() const override;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_MP3_H

83
ext-codec/MP3Rtp.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include "MP3Rtp.h"
#define MPEG12_HEADER_LEN 4
namespace mediakit {
//MPEG Audio-specific header
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| MBZ | Frag_offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
MP3RtpEncoder::MP3RtpEncoder(int sample_rate, int channels, int sample_bit) {
_sample_rate = sample_rate;
_channels = channels;
_sample_bit = sample_bit;
}
void MP3RtpEncoder::outputRtp(const char *data, size_t len, size_t offset, bool mark, uint64_t stamp) {
auto rtp = getRtpInfo().makeRtp(TrackAudio, nullptr, len + 4, mark, stamp);
auto payload = rtp->data() + RtpPacket::kRtpTcpHeaderSize + RtpPacket::kRtpHeaderSize;
payload[0] = 0;
payload[1] = 0;
payload[2] = offset >> 8;
payload[3] = offset ;
memcpy(payload + 4, data, len);
RtpCodec::inputRtp(std::move(rtp), false);
}
bool MP3RtpEncoder::inputFrame(const Frame::Ptr &frame) {
auto ptr = frame->data() + frame->prefixSize();
auto size = frame->size() - frame->prefixSize();
auto remain_size = size;
auto max_size = getRtpInfo().getMaxSize() - MPEG12_HEADER_LEN;
int offset = 0;
while (remain_size > 0) {
if (remain_size <= max_size) {
outputRtp(ptr, remain_size, offset, true, frame->dts());
break;
}
outputRtp(ptr, max_size, offset, false, frame->dts());
ptr += max_size;
remain_size -= max_size;
offset += max_size;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////
MP3RtpDecoder::MP3RtpDecoder() {
obtainFrame();
}
void MP3RtpDecoder::obtainFrame() {
_frame = FrameImp::create();
_frame->_codec_id = CodecMP3;
}
bool MP3RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
auto payload_size = rtp->getPayloadSize();
if (payload_size <= MPEG12_HEADER_LEN) {
return false;
}
auto stamp = rtp->getStampMS();
auto ptr = rtp->getPayload();
_frame->_buffer.append((char *)ptr + MPEG12_HEADER_LEN, payload_size - MPEG12_HEADER_LEN);
flushData();
_last_dts = stamp;
return false;
}
void MP3RtpDecoder::flushData() {
RtpCodec::inputFrame(_frame);
obtainFrame();
}
} // namespace mediakit

57
ext-codec/MP3Rtp.h Normal file
View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like 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.
*/
#ifndef ZLMEDIAKIT_MP3RTP_H
#define ZLMEDIAKIT_MP3RTP_H
#include "Rtsp/RtpCodec.h"
#include "Extension/Frame.h"
#include "Extension/CommonRtp.h"
namespace mediakit {
class MP3RtpEncoder : public RtpCodec {
public:
using Ptr = std::shared_ptr<MP3RtpEncoder>;
MP3RtpEncoder(int sample_rate = 44100, int channels = 2, int sample_bit = 16);
void outputRtp(const char *data, size_t len, size_t offset, bool mark, uint64_t stamp);
bool inputFrame(const Frame::Ptr &frame) override;
private:
int _channels;
int _sample_rate;
int _sample_bit;
};
class MP3RtpDecoder : public RtpCodec {
public:
using Ptr = std::shared_ptr<MP3RtpDecoder>;
MP3RtpDecoder();
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = false) override;
private:
void obtainFrame();
void flushData();
private:
uint64_t _last_dts = 0;
FrameImp::Ptr _frame;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_MP3RTP_H