Skip to content

Commit

Permalink
refactor: replace std::function<void(const SockException &ex)> to too…
Browse files Browse the repository at this point in the history
…lkit::Socket::onErrCB
  • Loading branch information
johzzy committed Mar 3, 2024
1 parent f49aed7 commit 7ad44fb
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion server/FFmpegSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void FFmpegSource::setupRecordFlag(bool enable_hls, bool enable_mp4){
_enable_mp4 = enable_mp4;
}

void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, const string &dst_url, int timeout_ms, const onPlay &cb) {
void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, const string &dst_url, int timeout_ms, const toolkit::Socket::onErrCB &cb) {
GET_CONFIG(string, ffmpeg_bin, FFmpeg::kBin);
GET_CONFIG(string, ffmpeg_cmd_default, FFmpeg::kCmd);
GET_CONFIG(string, ffmpeg_log, FFmpeg::kLog);
Expand Down
3 changes: 1 addition & 2 deletions server/FFmpegSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class FFmpegSnap {
class FFmpegSource : public std::enable_shared_from_this<FFmpegSource> , public mediakit::MediaSourceEventInterceptor{
public:
using Ptr = std::shared_ptr<FFmpegSource>;
using onPlay = std::function<void(const toolkit::SockException &ex)>;

FFmpegSource();
~FFmpegSource();
Expand All @@ -59,7 +58,7 @@ class FFmpegSource : public std::enable_shared_from_this<FFmpegSource> , public
* @param timeout_ms 等待结果超时时间,单位毫秒
* @param cb 成功与否回调
*/
void play(const std::string &ffmpeg_cmd_key, const std::string &src_url, const std::string &dst_url, int timeout_ms, const onPlay &cb);
void play(const std::string &ffmpeg_cmd_key, const std::string &src_url, const std::string &dst_url, int timeout_ms, const toolkit::Socket::onErrCB &cb);

/**
* 设置录制
Expand Down
2 changes: 1 addition & 1 deletion server/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void unInstallWebApi();

#if defined(ENABLE_RTPPROXY)
uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio, bool multiplex=false);
void connectRtpServer(const std::string &stream_id, const std::string &dst_url, uint16_t dst_port, const std::function<void(const toolkit::SockException &ex)> &cb);
void connectRtpServer(const std::string &stream_id, const std::string &dst_url, uint16_t dst_port, const toolkit::Socket::onErrCB &cb);
bool closeRtpServer(const std::string &stream_id);
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/Http/HttpTSPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void HttpTSPlayer::emitOnComplete(const SockException &ex) {
}
}

void HttpTSPlayer::setOnComplete(onComplete cb) {
void HttpTSPlayer::setOnComplete(toolkit::Socket::onErrCB cb) {
_on_complete = std::move(cb);
}

Expand Down
5 changes: 2 additions & 3 deletions src/Http/HttpTSPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ namespace mediakit {
class HttpTSPlayer : public HttpClientImp {
public:
using Ptr = std::shared_ptr<HttpTSPlayer>;
using onComplete = std::function<void(const toolkit::SockException &)>;

HttpTSPlayer(const toolkit::EventPoller::Ptr &poller = nullptr);

/**
* 设置下载完毕或异常断开回调
*/
void setOnComplete(onComplete cb);
void setOnComplete(toolkit::Socket::onErrCB cb);

/**
* 设置接收ts包回调
Expand All @@ -45,7 +44,7 @@ class HttpTSPlayer : public HttpClientImp {
void emitOnComplete(const toolkit::SockException &ex);

private:
onComplete _on_complete;
toolkit::Socket::onErrCB _on_complete;
TSSegment::onSegment _on_segment;
};

Expand Down
13 changes: 6 additions & 7 deletions src/Player/PlayerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace mediakit {
class PlayerBase : public TrackSource, public toolkit::mINI {
public:
using Ptr = std::shared_ptr<PlayerBase>;
using Event = std::function<void(const toolkit::SockException &ex)>;

static Ptr createPlayer(const toolkit::EventPoller::Ptr &poller, const std::string &strUrl);

Expand Down Expand Up @@ -102,12 +101,12 @@ class PlayerBase : public TrackSource, public toolkit::mINI {
/**
* 设置异常中断回调
*/
virtual void setOnShutdown(const Event &cb) = 0;
virtual void setOnShutdown(const toolkit::Socket::onErrCB &cb) = 0;

/**
* 设置播放结果回调
*/
virtual void setOnPlayResult(const Event &cb) = 0;
virtual void setOnPlayResult(const toolkit::Socket::onErrCB &cb) = 0;

/**
* 设置播放恢复回调
Expand Down Expand Up @@ -183,14 +182,14 @@ class PlayerImp : public Parent {
_media_src = src;
}

void setOnShutdown(const std::function<void(const toolkit::SockException &)> &cb) override {
void setOnShutdown(const toolkit::Socket::onErrCB &cb) override {
if (_delegate) {
_delegate->setOnShutdown(cb);
}
_on_shutdown = cb;
}

void setOnPlayResult(const std::function<void(const toolkit::SockException &ex)> &cb) override {
void setOnPlayResult(const toolkit::Socket::onErrCB &cb) override {
if (_delegate) {
_delegate->setOnPlayResult(cb);
}
Expand Down Expand Up @@ -227,8 +226,8 @@ class PlayerImp : public Parent {

protected:
std::function<void()> _on_resume;
PlayerBase::Event _on_shutdown;
PlayerBase::Event _on_play_result;
toolkit::Socket::onErrCB _on_shutdown;
toolkit::Socket::onErrCB _on_play_result;
MediaSource::Ptr _media_src;
std::shared_ptr<Delegate> _delegate;
};
Expand Down
4 changes: 2 additions & 2 deletions src/Player/PlayerProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ PlayerProxy::PlayerProxy(
(*this)[Client::kWaitTrackReady] = false;
}

void PlayerProxy::setPlayCallbackOnce(function<void(const SockException &ex)> cb) {
void PlayerProxy::setPlayCallbackOnce(toolkit::Socket::onErrCB cb) {
_on_play = std::move(cb);
}

void PlayerProxy::setOnClose(function<void(const SockException &ex)> cb) {
void PlayerProxy::setOnClose(toolkit::Socket::onErrCB cb) {
_on_close = cb ? std::move(cb) : [](const SockException &) {};
}

Expand Down
8 changes: 4 additions & 4 deletions src/Player/PlayerProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ class PlayerProxy
* 设置play结果回调,只触发一次;在play执行之前有效
* @param cb 回调对象
*/
void setPlayCallbackOnce(std::function<void(const toolkit::SockException &ex)> cb);
void setPlayCallbackOnce(toolkit::Socket::onErrCB cb);

/**
* 设置主动关闭回调
* @param cb 回调对象
*/
void setOnClose(std::function<void(const toolkit::SockException &ex)> cb);
void setOnClose(toolkit::Socket::onErrCB cb);

/**
* Set a callback for failed server connection
Expand Down Expand Up @@ -139,8 +139,8 @@ class PlayerProxy
toolkit::Timer::Ptr _timer;
std::function<void()> _on_disconnect;
std::function<void(const TranslationInfo &info)> _on_connect;
std::function<void(const toolkit::SockException &ex)> _on_close;
std::function<void(const toolkit::SockException &ex)> _on_play;
toolkit::Socket::onErrCB _on_close;
toolkit::Socket::onErrCB _on_play;
TranslationInfo _transtalion_info;
MultiMediaSourceMuxer::Ptr _muxer;

Expand Down
13 changes: 6 additions & 7 deletions src/Pusher/PusherBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace mediakit {
class PusherBase : public toolkit::mINI {
public:
using Ptr = std::shared_ptr<PusherBase>;
using Event = std::function<void(const toolkit::SockException &ex)>;

static Ptr createPusher(const toolkit::EventPoller::Ptr &poller,
const MediaSource::Ptr &src,
Expand All @@ -47,12 +46,12 @@ class PusherBase : public toolkit::mINI {
/**
* 摄像推流结果回调
*/
virtual void setOnPublished(const Event &cb) = 0;
virtual void setOnPublished(const toolkit::Socket::onErrCB &cb) = 0;

/**
* 设置断开回调
*/
virtual void setOnShutdown(const Event &cb) = 0;
virtual void setOnShutdown(const toolkit::Socket::onErrCB &cb) = 0;

protected:
virtual void onShutdown(const toolkit::SockException &ex) = 0;
Expand Down Expand Up @@ -89,7 +88,7 @@ class PusherImp : public Parent {
/**
* 摄像推流结果回调
*/
void setOnPublished(const PusherBase::Event &cb) override {
void setOnPublished(const toolkit::Socket::onErrCB &cb) override {
if (_delegate) {
_delegate->setOnPublished(cb);
}
Expand All @@ -99,7 +98,7 @@ class PusherImp : public Parent {
/**
* 设置断开回调
*/
void setOnShutdown(const PusherBase::Event &cb) override {
void setOnShutdown(const toolkit::Socket::onErrCB &cb) override {
if (_delegate) {
_delegate->setOnShutdown(cb);
}
Expand All @@ -122,8 +121,8 @@ class PusherImp : public Parent {
}

protected:
PusherBase::Event _on_shutdown;
PusherBase::Event _on_publish;
toolkit::Socket::onErrCB _on_shutdown;
toolkit::Socket::onErrCB _on_publish;
std::shared_ptr<Delegate> _delegate;
};

Expand Down
4 changes: 2 additions & 2 deletions src/Pusher/PusherProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ PusherProxy::~PusherProxy() {
_timer.reset();
}

void PusherProxy::setPushCallbackOnce(const function<void(const SockException &ex)> &cb) {
void PusherProxy::setPushCallbackOnce(const toolkit::Socket::onErrCB &cb) {
_on_publish = cb;
}

void PusherProxy::setOnClose(const function<void(const SockException &ex)> &cb) {
void PusherProxy::setOnClose(const toolkit::Socket::onErrCB &cb) {
_on_close = cb;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Pusher/PusherProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class PusherProxy
* 设置push结果回调,只触发一次;在publish执行之前有效
* @param cb 回调对象
*/
void setPushCallbackOnce(const std::function<void(const toolkit::SockException &ex)> &cb);
void setPushCallbackOnce(const toolkit::Socket::onErrCB &cb);

/**
* 设置主动关闭回调
* @param cb 回调对象
*/
void setOnClose(const std::function<void(const toolkit::SockException &ex)> &cb);
void setOnClose(const toolkit::Socket::onErrCB &cb);

/**
* 开始拉流播放
Expand All @@ -62,8 +62,8 @@ class PusherProxy
std::atomic<uint64_t> _live_secs;
std::atomic<uint64_t> _republish_count;
std::weak_ptr<MediaSource> _weak_src;
std::function<void(const toolkit::SockException &ex)> _on_close;
std::function<void(const toolkit::SockException &ex)> _on_publish;
toolkit::Socket::onErrCB _on_close;
toolkit::Socket::onErrCB _on_publish;
};

} /* namespace mediakit */
Expand Down
2 changes: 1 addition & 1 deletion src/Rtp/RtpSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void RtpSender::onErr(const SockException &ex) {
onClose(ex);
}

void RtpSender::setOnClose(std::function<void(const toolkit::SockException &ex)> on_close){
void RtpSender::setOnClose(toolkit::Socket::onErrCB on_close){
_on_close = std::move(on_close);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rtp/RtpSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class RtpSender final : public MediaSinkInterface, public std::enable_shared_fro
*/
virtual void resetTracks() override;

void setOnClose(std::function<void(const toolkit::SockException &ex)> on_close);
void setOnClose(toolkit::Socket::onErrCB on_close);

private:
//合并写输出
Expand All @@ -88,7 +88,7 @@ class RtpSender final : public MediaSinkInterface, public std::enable_shared_fro
toolkit::Ticker _rtcp_send_ticker;
toolkit::Ticker _rtcp_recv_ticker;
std::shared_ptr<RtpSession> _rtp_session;
std::function<void(const toolkit::SockException &ex)> _on_close;
toolkit::Socket::onErrCB _on_close;
};

}//namespace mediakit
Expand Down
2 changes: 1 addition & 1 deletion src/Rtp/RtpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ uint16_t RtpServer::getPort() {
return _udp_server ? _udp_server->getPort() : _rtp_socket->get_local_port();
}

void RtpServer::connectToServer(const std::string &url, uint16_t port, const function<void(const SockException &ex)> &cb) {
void RtpServer::connectToServer(const std::string &url, uint16_t port, const toolkit::Socket::onErrCB &cb) {
if (_tcp_mode != ACTIVE || !_rtp_socket) {
cb(SockException(Err_other, "仅支持tcp主动模式"));
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Rtp/RtpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class RtpServer : public std::enable_shared_from_this<RtpServer> {
* @param port 服务器端口
* @param cb 连接服务器是否成功的回调
*/
void connectToServer(const std::string &url, uint16_t port, const std::function<void(const toolkit::SockException &ex)> &cb);
void connectToServer(const std::string &url, uint16_t port, const toolkit::Socket::onErrCB &cb);

/**
* 获取绑定的本地端口
Expand Down

0 comments on commit 7ad44fb

Please sign in to comment.