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
Jan 4, 2024
1 parent
51efa13
commit 19ee45f
Showing
3 changed files
with
136 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "Track.h" | ||
#include "Codec/Transcode.h" | ||
#include "Extension/Factory.h" | ||
#include "Poller/EventPoller.h" | ||
#include "Util/logger.h" | ||
|
||
namespace mediakit { | ||
|
||
#ifdef ENABLE_FFMPEG | ||
|
||
void Track::setRawFrameCB(RawFrameCB cb) { | ||
std::unique_lock<decltype(_trans_mutex)> l(_trans_mutex); | ||
_raw_cb = cb; | ||
} | ||
|
||
int Track::trans_size() { | ||
std::unique_lock<decltype(_trans_mutex)> l(_trans_mutex); | ||
return _trans_tracks.size(); | ||
} | ||
|
||
bool Track::needDecode() { | ||
std::unique_lock<decltype(_trans_mutex)> l(_trans_mutex); | ||
return _raw_cb != nullptr || _trans_tracks.size() > 0; | ||
} | ||
|
||
bool Track::inputFrame(const Frame::Ptr &frame) { | ||
bool ret = FrameDispatcher::inputFrame(frame); | ||
// 有人读取Raw数据,则解码 | ||
if (needDecode()) { | ||
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; | ||
} | ||
|
||
void Track::inputRawFrame(const std::shared_ptr<FFmpegFrame> &frame) { | ||
std::list<Ptr> tracks; | ||
{ | ||
std::unique_lock<decltype(_trans_mutex)> l(_trans_mutex); | ||
for (auto it : _trans_tracks) { | ||
tracks.push_back(it.second); | ||
} | ||
if (_raw_cb) | ||
_raw_cb(frame); | ||
} | ||
for (auto it : tracks) { | ||
auto track = it; | ||
if (track->_encoder && (track->size() || !track->ready())) { | ||
track->_encoder->inputFrame(frame, true); | ||
} | ||
} | ||
} | ||
|
||
Track::Ptr Track::getTransodeTrack(CodecId id, int arg1, int arg2) { | ||
if (_parent) | ||
return _parent->getTransodeTrack(id, arg1, 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(); | ||
} | ||
std::unique_lock<decltype(_trans_mutex)> l(_trans_mutex); | ||
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