diff --git a/server/WebApi.cpp b/server/WebApi.cpp index cce6c974..4b2fb819 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -363,7 +363,7 @@ void installWebApi() { }); - API_REGIST(api,addStreamProxy,{ + API_REGIST_INVOKER(api,addStreamProxy,{ CHECK_SECRET(); CHECK_ARGS("vhost","app","stream","url","secret"); //添加拉流代理 @@ -376,12 +376,20 @@ void installWebApi() { )); //指定RTP over TCP(播放rtsp时有效) (*player)[kRtpType] = allArgs["rtp_type"].as(); - //开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试 + //开始播放,如果播放失败或者播放中止,将会自动重试若干次,默认一直重试 + player->setPlayCallbackOnce([invoker,val,headerOut,player](const SockException &ex){ + if(ex){ + const_cast(val)["code"] = API::OtherFailed; + const_cast(val)["msg"] = ex.what(); + }else{ + const_cast(val)["data"]["id"] = (uint64_t)player.get(); + lock_guard lck(s_proxyMapMtx); + s_proxyMap[(uint64_t)player.get()] = player; + } + const_cast(player).reset(); + invoker("200 OK", headerOut, val.toStyledString()); + }); player->play(allArgs["url"]); - - val["data"]["id"] = player.get(); - lock_guard lck(s_proxyMapMtx); - s_proxyMap[(uint64_t)player.get()] = player; }); API_REGIST(api,delStreamProxy,{ diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 595cf557..ae0d64be 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -75,6 +75,10 @@ PlayerProxy::PlayerProxy(const string &strVhost, _bEnableMp4 = bEnableMp4; _iRetryCount = iRetryCount; } + +void PlayerProxy::setPlayCallbackOnce(const function &cb){ + _playCB = cb; +} void PlayerProxy::play(const string &strUrlTmp) { weak_ptr weakSelf = shared_from_this(); std::shared_ptr piFailedCnt(new int(0)); //连续播放失败次数 @@ -83,6 +87,12 @@ void PlayerProxy::play(const string &strUrlTmp) { if(!strongSelf) { return; } + + if(strongSelf->_playCB) { + strongSelf->_playCB(err); + strongSelf->_playCB = nullptr; + } + if(!err) { // 播放成功 *piFailedCnt = 0;//连续播放失败次数清0 diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index c8e39a98..c6fa0e19 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -56,7 +56,14 @@ public: virtual ~PlayerProxy(); + /** + * 设置play结果回调,只触发一次;在play执行之前有效 + * @param cb + */ + void setPlayCallbackOnce(const function &cb); + void play(const string &strUrl) override; + bool close() override; private: void rePlay(const string &strUrl,int iFailedCnt); @@ -70,6 +77,7 @@ private: string _strApp; string _strSrc; Timer::Ptr _timer; + function _playCB; }; } /* namespace mediakit */