From 06784d86be77e97d18de1c53f4731b60220e1c21 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 23 Aug 2019 09:45:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84websocket-flv=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E9=94=99=E8=AF=AF=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 37 +++++++++++++++++++++++-------------- src/Http/HttpSession.h | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 980e85ef..55a27ba7 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -211,13 +211,25 @@ inline bool HttpSession::checkWebSocket(){ if(!_parser["Sec-WebSocket-Protocol"].empty()){ headerOut["Sec-WebSocket-Protocol"] = _parser["Sec-WebSocket-Protocol"]; } - sendResponse("101 Switching Protocols",headerOut,""); - checkLiveFlvStream(true); + + auto res_cb = [this,headerOut](){ + _flv_over_websocket = true; + sendResponse("101 Switching Protocols",headerOut,""); + }; + + //判断是否为websocket-flv + if(checkLiveFlvStream(res_cb)){ + //这里是websocket-flv直播请求 + return true; + } + + //如果checkLiveFlvStream返回false,则代表不是websocket-flv,而是普通的websocket连接 + sendResponse("101 Switching Protocols",headerOut,""); return true; } //http-flv 链接格式:http://vhost-url:port/app/streamid.flv?key1=value1&key2=value2 //如果url(除去?以及后面的参数)后缀是.flv,那么表明该url是一个http-flv直播。 -inline bool HttpSession::checkLiveFlvStream(bool over_websocket){ +inline bool HttpSession::checkLiveFlvStream(const function &cb){ auto pos = strrchr(_parser.Url().data(),'.'); if(!pos){ //未找到".flv"后缀 @@ -240,7 +252,7 @@ inline bool HttpSession::checkLiveFlvStream(bool over_websocket){ bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt); weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,bClose,this,over_websocket](const MediaSource::Ptr &src){ + MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,bClose,this,cb](const MediaSource::Ptr &src){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ //本对象已经销毁 @@ -249,35 +261,32 @@ inline bool HttpSession::checkLiveFlvStream(bool over_websocket){ auto rtmp_src = dynamic_pointer_cast(src); if(!rtmp_src){ //未找到该流 - if(!over_websocket){ - sendNotFound(bClose); - } + sendNotFound(bClose); if(bClose){ shutdown(SockException(Err_shutdown,"flv stream not found")); } return; } //找到流了 - auto onRes = [this,rtmp_src,over_websocket](const string &err){ + auto onRes = [this,rtmp_src,cb](const string &err){ bool authSuccess = err.empty(); if(!authSuccess){ - if(!over_websocket){ - sendResponse("401 Unauthorized", makeHttpHeader(true,err.size()),err); - } + sendResponse("401 Unauthorized", makeHttpHeader(true,err.size()),err); shutdown(SockException(Err_shutdown,StrPrinter << "401 Unauthorized:" << err)); return ; } - if(!over_websocket) { + if(!cb) { //找到rtmp源,发送http头,负载后续发送 sendResponse("200 OK", makeHttpHeader(false, 0, get_mime_type(".flv")), ""); + }else{ + cb(); } //开始发送rtmp负载 //关闭tcp_nodelay ,优化性能 SockUtil::setNoDelay(_sock->rawFD(),false); (*this) << SocketFlags(kSockFlags); - _flv_over_websocket = over_websocket; try{ start(getPoller(),rtmp_src); }catch (std::exception &ex){ @@ -480,7 +489,7 @@ inline void HttpSession::Handle_Req_GET(int64_t &content_len) { } //再看看是否为http-flv直播请求 - if(checkLiveFlvStream(false)){ + if(checkLiveFlvStream()){ return; } diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 48545e8e..d99bff95 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -110,7 +110,7 @@ protected: private: inline void Handle_Req_GET(int64_t &content_len); inline void Handle_Req_POST(int64_t &content_len); - inline bool checkLiveFlvStream(bool over_websocket = false); + inline bool checkLiveFlvStream(const function &cb = nullptr); inline bool checkWebSocket(); inline bool emitHttpEvent(bool doInvoke); inline void urlDecode(Parser &parser);