mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-03 00:47:34 +08:00
实现音频转码功能:
- 启用rtc:// schecma - 增加音频转码码率配置 - aac转码使用原通道数
This commit is contained in:
@@ -42,7 +42,7 @@ public:
|
||||
* @param stream_id 流id
|
||||
* @param ring_size 可以设置固定的环形缓冲大小,0则自适应
|
||||
*/
|
||||
RtspMediaSource(const MediaTuple& tuple, int ring_size = RTP_GOP_SIZE): MediaSource(RTSP_SCHEMA, tuple), _ring_size(ring_size) {}
|
||||
RtspMediaSource(const MediaTuple& tuple, const std::string &schema = RTSP_SCHEMA, int ring_size = RTP_GOP_SIZE): MediaSource(schema, tuple), _ring_size(ring_size) {}
|
||||
|
||||
~RtspMediaSource() override { flush(); }
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ void RtspMediaSource::onWrite(RtpPacket::Ptr rtp, bool keyPos) {
|
||||
PacketCache<RtpPacket>::inputPacket(stamp, is_video, std::move(rtp), keyPos);
|
||||
}
|
||||
|
||||
RtspMediaSourceImp::RtspMediaSourceImp(const MediaTuple& tuple, int ringSize): RtspMediaSource(tuple, ringSize)
|
||||
RtspMediaSourceImp::RtspMediaSourceImp(const MediaTuple& tuple, const std::string& schema, int ringSize): RtspMediaSource(tuple, schema, ringSize)
|
||||
{
|
||||
_demuxer = std::make_shared<RtspDemuxer>();
|
||||
_demuxer->setTrackListener(this);
|
||||
@@ -112,7 +112,12 @@ void RtspMediaSourceImp::setProtocolOption(const ProtocolOption &option)
|
||||
//开启直接代理模式时,rtsp直接代理,不重复产生;但是有些rtsp推流端,由于sdp中已有sps pps,rtp中就不再包括sps pps,
|
||||
//导致rtc无法播放,所以在rtsp推流rtc播放时,建议关闭直接代理模式
|
||||
_option = option;
|
||||
_option.enable_rtsp = !direct_proxy;
|
||||
if (direct_proxy) {
|
||||
if (getSchema() == RTC_SCHEMA)
|
||||
_option.enable_rtc = false;
|
||||
if (getSchema() == RTSP_SCHEMA)
|
||||
_option.enable_rtsp = false;
|
||||
}
|
||||
_muxer = std::make_shared<MultiMediaSourceMuxer>(_tuple, _demuxer->getDuration(), _option);
|
||||
_muxer->setMediaListener(getListener());
|
||||
_muxer->setTrackListener(std::static_pointer_cast<RtspMediaSourceImp>(shared_from_this()));
|
||||
@@ -128,11 +133,48 @@ void RtspMediaSourceImp::setProtocolOption(const ProtocolOption &option)
|
||||
RtspMediaSource::Ptr RtspMediaSourceImp::clone(const std::string &stream) {
|
||||
auto tuple = _tuple;
|
||||
tuple.stream = stream;
|
||||
auto src_imp = std::make_shared<RtspMediaSourceImp>(tuple);
|
||||
auto src_imp = std::make_shared<RtspMediaSourceImp>(tuple, getSchema());
|
||||
src_imp->setSdp(getSdp());
|
||||
src_imp->setProtocolOption(getProtocolOption());
|
||||
return src_imp;
|
||||
}
|
||||
|
||||
bool RtspMediaSourceImp::addTrack(const Track::Ptr &track)
|
||||
{
|
||||
if (_muxer) {
|
||||
if (_muxer->addTrack(track)) {
|
||||
track->addDelegate(_muxer);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void RtspMediaSourceImp::addTrackCompleted()
|
||||
{
|
||||
if (_muxer) {
|
||||
_muxer->addTrackCompleted();
|
||||
}
|
||||
}
|
||||
|
||||
void RtspMediaSourceImp::resetTracks()
|
||||
{
|
||||
if (_muxer) {
|
||||
_muxer->resetTracks();
|
||||
}
|
||||
}
|
||||
|
||||
void RtspMediaSourceImp::setListener(const std::weak_ptr<MediaSourceEvent> &listener)
|
||||
{
|
||||
if (_muxer) {
|
||||
//_muxer对象不能处理的事件再给listener处理
|
||||
_muxer->setMediaListener(listener);
|
||||
}
|
||||
else {
|
||||
//未创建_muxer对象,事件全部给listener处理
|
||||
MediaSource::setListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,11 @@
|
||||
#define SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_
|
||||
|
||||
#include "RtspMediaSource.h"
|
||||
#include "RtspDemuxer.h"
|
||||
#include "Common/MultiMediaSourceMuxer.h"
|
||||
|
||||
namespace mediakit {
|
||||
class RtspDemuxer;
|
||||
class RtspMediaSourceImp final : public RtspMediaSource, private TrackListener, public MultiMediaSourceMuxer::Listener {
|
||||
class RtspMediaSourceImp : public RtspMediaSource, private TrackListener, public MultiMediaSourceMuxer::Listener {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<RtspMediaSourceImp>;
|
||||
|
||||
@@ -28,7 +27,7 @@ public:
|
||||
* @param id 流id
|
||||
* @param ringSize 环形缓存大小
|
||||
*/
|
||||
RtspMediaSourceImp(const MediaTuple& tuple, int ringSize = RTP_GOP_SIZE);
|
||||
RtspMediaSourceImp(const MediaTuple& tuple, const std::string &schema = RTSP_SCHEMA, int ringSize = RTP_GOP_SIZE);
|
||||
|
||||
~RtspMediaSourceImp() override = default;
|
||||
|
||||
@@ -61,30 +60,14 @@ public:
|
||||
/**
|
||||
* _demuxer触发的添加Track事件
|
||||
*/
|
||||
bool addTrack(const Track::Ptr &track) override {
|
||||
if (_muxer) {
|
||||
if (_muxer->addTrack(track)) {
|
||||
track->addDelegate(_muxer);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool addTrack(const Track::Ptr &track) override;
|
||||
|
||||
/**
|
||||
* _demuxer触发的Track添加完毕事件
|
||||
*/
|
||||
void addTrackCompleted() override {
|
||||
if (_muxer) {
|
||||
_muxer->addTrackCompleted();
|
||||
}
|
||||
}
|
||||
void addTrackCompleted() override;
|
||||
|
||||
void resetTracks() override {
|
||||
if (_muxer) {
|
||||
_muxer->resetTracks();
|
||||
}
|
||||
}
|
||||
void resetTracks() override;
|
||||
|
||||
/**
|
||||
* _muxer触发的所有Track就绪的事件
|
||||
@@ -97,21 +80,13 @@ public:
|
||||
* 设置事件监听器
|
||||
* @param listener 监听器
|
||||
*/
|
||||
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override{
|
||||
if (_muxer) {
|
||||
//_muxer对象不能处理的事件再给listener处理
|
||||
_muxer->setMediaListener(listener);
|
||||
} else {
|
||||
//未创建_muxer对象,事件全部给listener处理
|
||||
MediaSource::setListener(listener);
|
||||
}
|
||||
}
|
||||
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override;
|
||||
|
||||
RtspMediaSource::Ptr clone(const std::string& stream) override;
|
||||
private:
|
||||
protected:
|
||||
bool _all_track_ready = false;
|
||||
ProtocolOption _option;
|
||||
RtspDemuxer::Ptr _demuxer;
|
||||
std::shared_ptr<RtspDemuxer> _demuxer;
|
||||
MultiMediaSourceMuxer::Ptr _muxer;
|
||||
};
|
||||
} /* namespace mediakit */
|
||||
|
||||
@@ -16,16 +16,19 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtspMediaSourceMuxer final : public RtspMuxer, public MediaSourceEventInterceptor,
|
||||
class RtspMediaSourceMuxer : public RtspMuxer, public MediaSourceEventInterceptor,
|
||||
public std::enable_shared_from_this<RtspMediaSourceMuxer> {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<RtspMediaSourceMuxer>;
|
||||
|
||||
RtspMediaSourceMuxer(const MediaTuple& tuple,
|
||||
const ProtocolOption &option,
|
||||
const TitleSdp::Ptr &title = nullptr) : RtspMuxer(title) {
|
||||
const TitleSdp::Ptr &title = nullptr,
|
||||
const std::string &schema = RTSP_SCHEMA
|
||||
): RtspMuxer(title) {
|
||||
_option = option;
|
||||
_media_src = std::make_shared<RtspMediaSource>(tuple);
|
||||
_on_demand = schema == RTSP_SCHEMA ? option.rtsp_demand : option.rtc_demand;
|
||||
_media_src = std::make_shared<RtspMediaSource>(tuple, schema);
|
||||
getRtpRing()->setDelegate(_media_src);
|
||||
}
|
||||
|
||||
@@ -50,19 +53,19 @@ public:
|
||||
}
|
||||
|
||||
void onReaderChanged(MediaSource &sender, int size) override {
|
||||
_enabled = _option.rtsp_demand ? size : true;
|
||||
if (!size && _option.rtsp_demand) {
|
||||
_enabled = _on_demand ? size : true;
|
||||
if (!size && _on_demand) {
|
||||
_clear_cache = true;
|
||||
}
|
||||
MediaSourceEventInterceptor::onReaderChanged(sender, size);
|
||||
}
|
||||
|
||||
bool inputFrame(const Frame::Ptr &frame) override {
|
||||
if (_clear_cache && _option.rtsp_demand) {
|
||||
if (_clear_cache && _on_demand) {
|
||||
_clear_cache = false;
|
||||
_media_src->clearCache();
|
||||
}
|
||||
if (_enabled || !_option.rtsp_demand) {
|
||||
if (_enabled || !_on_demand) {
|
||||
return RtspMuxer::inputFrame(frame);
|
||||
}
|
||||
return false;
|
||||
@@ -70,12 +73,13 @@ public:
|
||||
|
||||
bool isEnabled() {
|
||||
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
|
||||
return _option.rtsp_demand ? (_clear_cache ? true : _enabled) : true;
|
||||
return _on_demand ? (_clear_cache ? true : _enabled) : true;
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
bool _enabled = true;
|
||||
bool _clear_cache = false;
|
||||
bool _on_demand = false;
|
||||
ProtocolOption _option;
|
||||
RtspMediaSource::Ptr _media_src;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user