diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 349272a0..0eada8cc 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -232,15 +232,15 @@ static inline void addHttpListener(){ } //拉流代理器列表 -static unordered_map s_proxyMap; +static unordered_map s_proxyMap; static recursive_mutex s_proxyMapMtx; //推流代理器列表 -static unordered_map s_proxyPusherMap; +static unordered_map s_proxyPusherMap; static recursive_mutex s_proxyPusherMapMtx; //FFmpeg拉流代理器列表 -static unordered_map s_ffmpegMap; +static unordered_map s_ffmpegMap; static recursive_mutex s_ffmpegMapMtx; #if defined(ENABLE_RTPPROXY) @@ -607,12 +607,12 @@ void installWebApi() { const string &stream, const string &url, int retryCount, - const function &cb){ + const function &cb) { auto key = getProxyKey(vhost, app, stream); lock_guard lck(s_proxyPusherMapMtx); - if (s_proxyPusherMap.find(key) != s_proxyPusherMap.end()){ + if (s_proxyPusherMap.find(key) != s_proxyPusherMap.end()) { //已经在推流了 - cb(SockException(Err_success),key); + cb(SockException(Err_success), key); return; } @@ -621,21 +621,21 @@ void installWebApi() { if (retryCount != 0) retry_count = retryCount; //添加推流代理 - PusherProxy::Ptr pusher(new PusherProxy(schema,vhost, app, stream, retry_count, poller)); + PusherProxy::Ptr pusher(new PusherProxy(schema, vhost, app, stream, retry_count, poller)); s_proxyPusherMap[key] = pusher; //开始推流,如果推流失败或者推流中止,将会自动重试若干次,默认一直重试 - pusher->setPushCallbackOnce([cb, key, url](const SockException &ex){ - if (ex){ + pusher->setPushCallbackOnce([cb, key, url](const SockException &ex) { + if (ex) { InfoL << "key: " << key << ", " << "addStreamPusherProxy pusher callback error: " << ex.what(); lock_guard lck(s_proxyMapMtx); s_proxyMap.erase(key); } - cb(ex,key); + cb(ex, key); }); //被主动关闭推流 - pusher->setOnClose([key, url](const SockException &ex){ + pusher->setOnClose([key, url](const SockException &ex) { InfoL << "key: " << key << ", " << "addStreamPusherProxy close callback error: " << ex.what(); lock_guard lck(s_proxyMapMtx); s_proxyMap.erase(key); @@ -647,50 +647,46 @@ void installWebApi() { //测试url http://127.0.0.1/index/api/addStreamPusherProxy?schema=rtmp&vhost=__defaultVhost__&app=proxy&stream=0&dst_url=rtmp://127.0.0.1/live/obs api_regist("/index/api/addStreamPusherProxy", [](API_ARGS_MAP_ASYNC) { CHECK_SECRET(); - CHECK_ARGS("schema","vhost","app","stream"); + CHECK_ARGS("schema", "vhost", "app", "stream", "dst_url"); - InfoL << allArgs["schema"] << ", " << allArgs["vhost"] << ", " << allArgs["app"] << ", " << allArgs["stream"]; - - //查找源 + auto dst_url = allArgs["dst_url"]; + auto src_url = allArgs["schema"] + "/" + allArgs["vhost"] + "/" + allArgs["app"] + "/" + allArgs["stream"]; auto src = MediaSource::find(allArgs["schema"], allArgs["vhost"], allArgs["app"], allArgs["stream"]); - if (!src){ - InfoL << "addStreamPusherProxy, canont find source stream!"; - const_cast(val)["code"] = API::OtherFailed; - const_cast(val)["msg"] = "can not find the source stream"; + if (!src) { + WarnL << "addStreamPusherProxy, can not find source stream:" << src_url; + val["code"] = API::NotFound; + val["msg"] = "can not find the source stream"; invoker(200, headerOut, val.toStyledString()); return; } - std::string srcUrl = allArgs["schema"] + "://" + "127.0.0.1" + "/" + allArgs["app"] + "/" + allArgs["stream"]; - std::string pushUrl = decodeBase64(allArgs["dst_url"]); - InfoL << "addStreamPusherProxy, find stream: " << srcUrl << ", push dst url: " << pushUrl; + InfoL << "addStreamPusherProxy, find stream: " << src_url << ", push dst url: " << dst_url; addStreamPusherProxy(allArgs["schema"], allArgs["vhost"], allArgs["app"], allArgs["stream"], - pushUrl, + allArgs["dst_url"], allArgs["retry_count"], - [invoker,val,headerOut, pushUrl](const SockException &ex, const string &key){ - if(ex){ - const_cast(val)["code"] = API::OtherFailed; - const_cast(val)["msg"] = ex.what(); - InfoL << "Publish error url: " << pushUrl; - }else{ - const_cast(val)["data"]["key"] = key; - InfoL << "Publish success, Please play with player:" << pushUrl; + [invoker, val, headerOut, dst_url](const SockException &ex, const string &key) mutable { + if (ex) { + val["code"] = API::OtherFailed; + val["msg"] = ex.what(); + WarnL << "Publish stream failed, dst url is: " << dst_url; + } else { + val["data"]["key"] = key; + InfoL << "Publish success, please play with player:" << dst_url; } invoker(200, headerOut, val.toStyledString()); }); - }); //关闭推流代理 //测试url http://127.0.0.1/index/api/delStreamPusherProxy?key=__defaultVhost__/proxy/0 - api_regist("/index/api/delStreamPusherProxy",[](API_ARGS_MAP){ + api_regist("/index/api/delStreamPusherProxy", [](API_ARGS_MAP) { CHECK_SECRET(); CHECK_ARGS("key"); lock_guard lck(s_proxyPusherMapMtx);