mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-30 06:42:22 +08:00
MediaPusher: 抽象精简代码
This commit is contained in:
@@ -46,7 +46,7 @@ void RtmpPusher::teardown() {
|
||||
}
|
||||
}
|
||||
|
||||
void RtmpPusher::onPublishResult(const SockException &ex, bool handshake_done) {
|
||||
void RtmpPusher::onPublishResult_l(const SockException &ex, bool handshake_done) {
|
||||
DebugL << ex.what();
|
||||
if (ex.getErrCode() == Err_shutdown) {
|
||||
//主动shutdown的,不触发回调
|
||||
@@ -55,14 +55,10 @@ void RtmpPusher::onPublishResult(const SockException &ex, bool handshake_done) {
|
||||
if (!handshake_done) {
|
||||
//播放结果回调
|
||||
_publish_timer.reset();
|
||||
if (_on_published) {
|
||||
_on_published(ex);
|
||||
}
|
||||
onPublishResult(ex);
|
||||
} else {
|
||||
//播放成功后异常断开回调
|
||||
if (_on_shutdown) {
|
||||
_on_shutdown(ex);
|
||||
}
|
||||
onShutdown(ex);
|
||||
}
|
||||
|
||||
if (ex) {
|
||||
@@ -78,7 +74,7 @@ void RtmpPusher::publish(const string &url) {
|
||||
_tc_url = string("rtmp://") + host_url + "/" + _app;
|
||||
|
||||
if (!_app.size() || !_stream_id.size()) {
|
||||
onPublishResult(SockException(Err_other, "rtmp url非法"), false);
|
||||
onPublishResult_l(SockException(Err_other, "rtmp url非法"), false);
|
||||
return;
|
||||
}
|
||||
DebugL << host_url << " " << _app << " " << _stream_id;
|
||||
@@ -99,7 +95,7 @@ void RtmpPusher::publish(const string &url) {
|
||||
if (!strongSelf) {
|
||||
return false;
|
||||
}
|
||||
strongSelf->onPublishResult(SockException(Err_timeout, "publish rtmp timeout"), false);
|
||||
strongSelf->onPublishResult_l(SockException(Err_timeout, "publish rtmp timeout"), false);
|
||||
return false;
|
||||
}, getPoller()));
|
||||
|
||||
@@ -112,12 +108,12 @@ void RtmpPusher::publish(const string &url) {
|
||||
|
||||
void RtmpPusher::onErr(const SockException &ex){
|
||||
//定时器_pPublishTimer为空后表明握手结束了
|
||||
onPublishResult(ex, !_publish_timer);
|
||||
onPublishResult_l(ex, !_publish_timer);
|
||||
}
|
||||
|
||||
void RtmpPusher::onConnect(const SockException &err){
|
||||
if (err) {
|
||||
onPublishResult(err, false);
|
||||
onPublishResult_l(err, false);
|
||||
return;
|
||||
}
|
||||
weak_ptr<RtmpPusher> weak_self = dynamic_pointer_cast<RtmpPusher>(shared_from_this());
|
||||
@@ -138,7 +134,7 @@ void RtmpPusher::onRecv(const Buffer::Ptr &buf){
|
||||
} catch (exception &e) {
|
||||
SockException ex(Err_other, e.what());
|
||||
//定时器_pPublishTimer为空后表明握手结束了
|
||||
onPublishResult(ex, !_publish_timer);
|
||||
onPublishResult_l(ex, !_publish_timer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,10 +222,10 @@ inline void RtmpPusher::send_metaData(){
|
||||
_rtmp_reader->setDetachCB([weak_self]() {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (strong_self) {
|
||||
strong_self->onPublishResult(SockException(Err_other, "媒体源被释放"), !strong_self->_publish_timer);
|
||||
strong_self->onPublishResult_l(SockException(Err_other, "媒体源被释放"), !strong_self->_publish_timer);
|
||||
}
|
||||
});
|
||||
onPublishResult(SockException(Err_success, "success"), false);
|
||||
onPublishResult_l(SockException(Err_success, "success"), false);
|
||||
//提升发送性能
|
||||
setSocketFlags();
|
||||
}
|
||||
|
||||
@@ -27,14 +27,6 @@ public:
|
||||
void publish(const string &url) override ;
|
||||
void teardown() override;
|
||||
|
||||
void setOnPublished(const Event &cb) override {
|
||||
_on_published = cb;
|
||||
}
|
||||
|
||||
void setOnShutdown(const Event &cb) override{
|
||||
_on_shutdown = cb;
|
||||
}
|
||||
|
||||
protected:
|
||||
//for Tcpclient override
|
||||
void onRecv(const Buffer::Ptr &buf) override;
|
||||
@@ -48,7 +40,7 @@ protected:
|
||||
}
|
||||
|
||||
private:
|
||||
void onPublishResult(const SockException &ex, bool handshake_done);
|
||||
void onPublishResult_l(const SockException &ex, bool handshake_done);
|
||||
|
||||
template<typename FUN>
|
||||
inline void addOnResultCB(const FUN &fun) {
|
||||
@@ -81,16 +73,14 @@ private:
|
||||
deque<function<void(AMFValue &dec)> > _deque_on_status;
|
||||
unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result;
|
||||
|
||||
//事件监听
|
||||
Event _on_shutdown;
|
||||
Event _on_published;
|
||||
|
||||
//推流超时定时器
|
||||
std::shared_ptr<Timer> _publish_timer;
|
||||
std::weak_ptr<RtmpMediaSource> _publish_src;
|
||||
RtmpMediaSource::RingType::RingReader::Ptr _rtmp_reader;
|
||||
};
|
||||
|
||||
using RtmpPusherImp = PusherImp<RtmpPusher, PusherBase>;
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
#endif /* SRC_RTMP_RTMPPUSHER_H_ */
|
||||
|
||||
Reference in New Issue
Block a user