Skip to content

Commit

Permalink
实现音频转码,有两种实现方式:
Browse files Browse the repository at this point in the history
- 基于MultiMediaSourceMuxer:基于多流实现,不能实现无人观看不转码
- 基于RtmpMuxer和RtcMuxer来实现:性能较好,能实现无人观看不转码
  • Loading branch information
cqm committed Jan 17, 2024
1 parent 0347342 commit 3d6cfdf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Common/MultiMediaSourceMuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,27 @@ bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {

bool ret = false;
if (_rtmp) {
ret = _rtmp->addTrack(track) ? true : ret;
/* 利用多音频流来实现转码,这样也行,但无法控制按需转码
if (track->getCodecId() == CodecOpus) {
GET_CONFIG(int, bitrate, General::kAacBitrate);
link(track->getTransodeTrack(CodecAAC, 44100, 1, bitrate));
}
else
*/
{
ret = _rtmp->addTrack(track) ? true : ret;
}
}
if (_rtc) {
ret = _rtc->addTrack(track) ? true : ret;
/* 利用多音频流来实现转码,这样也行,但无法控制按需转码
if (track->getCodecId() == CodecAAC) {
GET_CONFIG(int, bitrate, General::kOpusBitrate);
link(track->getTransodeTrack(CodecOpus, 48000, 1, bitrate));
}
else*/
{
ret = _rtc->addTrack(track) ? true : ret;
}
}
if (_rtsp) {
ret = _rtsp->addTrack(track) ? true : ret;
Expand All @@ -521,7 +538,7 @@ bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {
}

void MultiMediaSourceMuxer::onAllTrackReady() {
CHECK(!_create_in_poller || getOwnerPoller(MediaSource::NullMediaSource())->isCurrentThread());
//CHECK(!_create_in_poller || getOwnerPoller(MediaSource::NullMediaSource())->isCurrentThread());

if (_option.paced_sender_ms) {
std::weak_ptr<MultiMediaSourceMuxer> weak_self = shared_from_this();
Expand Down
17 changes: 17 additions & 0 deletions src/Rtmp/RtmpMediaSourceMuxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,27 @@ class RtmpMediaSourceMuxer final : public RtmpMuxer, public MediaSourceEventInte
_media_src->setMetaData(getMetadata());
}

bool addTrack(const Track::Ptr &track) override {
auto t = track;
if (_option.audio_transcode && track->getCodecId() == CodecOpus) {
GET_CONFIG(int, bitrate, General::kAacBitrate);
_trans = track->getTransodeTrack(CodecAAC, 44100, 1, bitrate);
if (_trans) t = _trans;
}
return RtmpMuxer::addTrack(t);
}

void onReaderChanged(MediaSource &sender, int size) override {
_enabled = _option.rtmp_demand ? size : true;
if (!size && _option.rtmp_demand) {
_clear_cache = true;
}
if (_trans) {
if (size)
_trans->addDelegate(shared_from_this());
else
_trans->delDelegate(this);
}
MediaSourceEventInterceptor::onReaderChanged(sender, size);
}

Expand All @@ -75,6 +91,7 @@ class RtmpMediaSourceMuxer final : public RtmpMuxer, public MediaSourceEventInte
}

private:
Track::Ptr _trans;
bool _enabled = true;
bool _clear_cache = false;
ProtocolOption _option;
Expand Down
22 changes: 22 additions & 0 deletions webrtc/RtcMediaSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ class RtcMediaSourceMuxer : public RtspMediaSourceMuxer {
RtcMediaSourceMuxer(const MediaTuple &tuple, const ProtocolOption &option, const TitleSdp::Ptr &title = nullptr)
: RtspMediaSourceMuxer(tuple, option, title, RTC_SCHEMA) {
}

bool addTrack(const Track::Ptr &track) override {
auto t = track;
if (_option.audio_transcode && track->getCodecId() == CodecAAC) {
GET_CONFIG(int, bitrate, General::kOpusBitrate);
_trans = track->getTransodeTrack(CodecOpus, 48000, 1, bitrate);
if (_trans) t = _trans;
}
return RtspMuxer::addTrack(t);
}

void onReaderChanged(MediaSource &sender, int size) override {
RtspMediaSourceMuxer::onReaderChanged(sender, size);
if (_trans) {
if (size)
_trans->addDelegate(shared_from_this());
else
_trans->delDelegate(this);
}
}
protected:
Track::Ptr _trans;
};

}
Expand Down

0 comments on commit 3d6cfdf

Please sign in to comment.