forked from ZLMediaKit/ZLMediaKit
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cqm
committed
Dec 29, 2023
1 parent
dde42bf
commit 8645c5e
Showing
4 changed files
with
137 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "Track.h" | ||
#include "Codec/Transcode.h" | ||
#include "Extension/Factory.h" | ||
#include "Poller/EventPoller.h" | ||
#include "Util/logger.h" | ||
|
||
namespace mediakit { | ||
|
||
#ifdef ENABLE_FFMPEG | ||
|
||
bool Track::inputFrame(const Frame::Ptr &frame) { | ||
bool ret = FrameDispatcher::inputFrame(frame); | ||
// 有人读取Raw数据,则解码 | ||
if (_trans_size) { | ||
if (!_decoder) { | ||
_decoder = std::make_shared<FFmpegDecoder>(shared_from_this()); | ||
InfoL << " open decoder " << getInfo(); | ||
_decoder->setOnDecode([this](const FFmpegFrame::Ptr &frame) { inputRawFrame(frame); }); | ||
} | ||
_decoder->inputFrame(frame, true, true); | ||
} else if (_decoder) { | ||
InfoL << " close decoder " << getInfo(); | ||
_decoder = nullptr; | ||
} | ||
return ret; | ||
} | ||
|
||
bool Track::inputRawFrame(const std::shared_ptr<FFmpegFrame> &frame) { | ||
bool ret = false; | ||
for (auto it : _trans_tracks) { | ||
auto track = it.second; | ||
if (track->_encoder && (track->size() || !track->ready())) { | ||
ret |= track->_encoder->inputFrame(frame, true); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
void Track::onSizeChange(int size) { | ||
if (_parent) { | ||
_parent->upateTransSize(); | ||
} | ||
return; | ||
if (size == 0) { | ||
_encoder = nullptr; | ||
} else if (_parent) { | ||
// transcode from | ||
_encoder = std::make_shared<FFmpegEncoder>(shared_from_this()); | ||
_encoder->setOnEncode([this](const Frame::Ptr &frame) { inputFrame(frame); }); | ||
} | ||
} | ||
|
||
void Track::upateTransSize() { | ||
int ret = 0; | ||
for (auto it : _trans_tracks) { | ||
ret += it.second->size(); | ||
} | ||
_trans_size = ret; | ||
} | ||
|
||
Track::Ptr Track::getTransodeTrack(CodecId id, int arg1, int arg2) { | ||
char key[256]; | ||
int arg3 = (mediakit::getTrackType(id) == TrackVideo) ? 30 : 16; | ||
snprintf(key, sizeof(key), "%s[%dx%dx%d]", mediakit::getCodecName(id), arg1, arg2, arg3); | ||
if (key == getInfo()) { | ||
return shared_from_this(); | ||
} | ||
Ptr ret = _trans_tracks[key]; | ||
if (!ret) { | ||
ret = Factory::getTrackByCodecId(id, arg1, arg2, arg3); | ||
if (ret) { | ||
ret->_parent = this; | ||
// Factory::getTrackByCodecId的宽高参数未必初始化完毕,因此这边立即创建 Encode | ||
Track::Ptr cfg; | ||
if (mediakit::getTrackType(id) == TrackVideo) | ||
cfg.reset(new VideoTrackImp(id, arg1, arg2, arg3)); | ||
else | ||
cfg.reset(new AudioTrackImp(id, arg1, arg2, arg3)); | ||
ret->_encoder = std::make_shared<FFmpegEncoder>(cfg); | ||
_encoder->setOnEncode([this](const Frame::Ptr &frame) { inputFrame(frame); }); | ||
_trans_tracks[key] = ret; | ||
} | ||
} | ||
return ret; | ||
} | ||
#endif | ||
|
||
} // namespace mediakit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters