mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-16 13:05:59 +08:00
适配ZLToolKit代码,支持自定义创建Socket:#468
This commit is contained in:
@@ -13,7 +13,7 @@ namespace mediakit {
|
||||
|
||||
HlsPlayer::HlsPlayer(const EventPoller::Ptr &poller){
|
||||
_segment.setOnSegment([this](const char *data, uint64_t len) { onPacket(data, len); });
|
||||
_poller = poller ? poller : EventPollerPool::Instance().getPoller();
|
||||
setPoller(poller ? poller : EventPollerPool::Instance().getPoller());
|
||||
}
|
||||
|
||||
HlsPlayer::~HlsPlayer() {}
|
||||
@@ -63,6 +63,15 @@ void HlsPlayer::playNextTs(bool force){
|
||||
std::shared_ptr<Ticker> ticker(new Ticker);
|
||||
|
||||
_http_ts_player = std::make_shared<HttpTSPlayer>(getPoller(), false);
|
||||
|
||||
_http_ts_player->setOnCreateSocket([weakSelf](const EventPoller::Ptr &poller) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (strongSelf) {
|
||||
return strongSelf->createSocket();
|
||||
}
|
||||
return Socket::createSocket(poller, true);
|
||||
});
|
||||
|
||||
_http_ts_player->setOnDisconnect([weakSelf, ticker, ts_duration](const SockException &err) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (!strongSelf) {
|
||||
@@ -84,6 +93,7 @@ void HlsPlayer::playNextTs(bool force){
|
||||
}, strongSelf->getPoller()));
|
||||
}
|
||||
});
|
||||
|
||||
_http_ts_player->setOnPacket([weakSelf](const char *data, uint64_t len) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (!strongSelf) {
|
||||
@@ -94,9 +104,10 @@ void HlsPlayer::playNextTs(bool force){
|
||||
});
|
||||
|
||||
_http_ts_player->setMethod("GET");
|
||||
if(!(*this)[kNetAdapter].empty()) {
|
||||
if (!(*this)[kNetAdapter].empty()) {
|
||||
_http_ts_player->setNetAdapter((*this)[Client::kNetAdapter]);
|
||||
}
|
||||
|
||||
_http_ts_player->sendRequest(_ts_list.front().url, 2 * _ts_list.front().duration);
|
||||
_ts_list.pop_front();
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ void HttpClient::onConnect(const SockException &ex) {
|
||||
}
|
||||
|
||||
//先假设http客户端只会接收一点点数据(只接受http头,节省内存)
|
||||
_sock->setReadBuffer(std::make_shared<BufferRaw>(1 * 1024));
|
||||
getSock()->setReadBuffer(std::make_shared<BufferRaw>(1 * 1024));
|
||||
|
||||
_totalBodySize = 0;
|
||||
_recvedBodySize = 0;
|
||||
@@ -157,7 +157,7 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
|
||||
|
||||
if(_parser["Transfer-Encoding"] == "chunked"){
|
||||
//我们认为这种情况下后面应该有大量的数据过来,加大接收缓存提高性能
|
||||
_sock->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
|
||||
getSock()->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
|
||||
|
||||
//如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的
|
||||
_totalBodySize = -1;
|
||||
@@ -185,9 +185,9 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
|
||||
_recvedBodySize = 0;
|
||||
if(_totalBodySize > 0){
|
||||
//根据_totalBodySize设置接收缓存大小
|
||||
_sock->setReadBuffer(std::make_shared<BufferRaw>(MIN(_totalBodySize + 1,256 * 1024)));
|
||||
getSock()->setReadBuffer(std::make_shared<BufferRaw>(MIN(_totalBodySize + 1,256 * 1024)));
|
||||
}else{
|
||||
_sock->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
|
||||
getSock()->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
@@ -476,7 +476,7 @@ void HttpSession::sendResponse(const char *pcStatus,
|
||||
|
||||
//发送http body
|
||||
AsyncSenderData::Ptr data = std::make_shared<AsyncSenderData>(shared_from_this(),body,bClose);
|
||||
_sock->setOnFlush([data](){
|
||||
getSock()->setOnFlush([data](){
|
||||
return AsyncSender::onSocketFlushed(data);
|
||||
});
|
||||
AsyncSender::onSocketFlushed(data);
|
||||
@@ -543,10 +543,10 @@ void HttpSession::Handle_Req_POST(int64_t &content_len) {
|
||||
|
||||
//根据Content-Length设置接收缓存大小
|
||||
if(totalContentLen > 0){
|
||||
_sock->setReadBuffer(std::make_shared<BufferRaw>(MIN(totalContentLen + 1,256 * 1024)));
|
||||
getSock()->setReadBuffer(std::make_shared<BufferRaw>(MIN(totalContentLen + 1,256 * 1024)));
|
||||
}else{
|
||||
//不定长度的Content-Length
|
||||
_sock->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
|
||||
getSock()->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
|
||||
}
|
||||
|
||||
if(totalContentLen > 0 && totalContentLen < maxReqSize ){
|
||||
@@ -610,7 +610,7 @@ void HttpSession::setSocketFlags(){
|
||||
GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS);
|
||||
if(mergeWriteMS > 0) {
|
||||
//推流模式下,关闭TCP_NODELAY会增加推流端的延时,但是服务器性能将提高
|
||||
SockUtil::setNoDelay(_sock->rawFD(), false);
|
||||
SockUtil::setNoDelay(getSock()->rawFD(), false);
|
||||
//播放模式下,开启MSG_MORE会增加延时,但是能提高发送性能
|
||||
setSendFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE);
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
namespace mediakit {
|
||||
|
||||
HttpTSPlayer::HttpTSPlayer(const EventPoller::Ptr &poller, bool split_ts){
|
||||
_segment.setOnSegment([this](const char *data, uint64_t len) { onPacket(data, len); });
|
||||
_poller = poller ? poller : EventPollerPool::Instance().getPoller();
|
||||
_split_ts = split_ts;
|
||||
_segment.setOnSegment([this](const char *data, uint64_t len) { onPacket(data, len); });
|
||||
setPoller(poller ? poller : EventPollerPool::Instance().getPoller());
|
||||
}
|
||||
|
||||
HttpTSPlayer::~HttpTSPlayer() {}
|
||||
@@ -25,8 +25,8 @@ int64_t HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::H
|
||||
shutdown(SockException(Err_other, StrPrinter << "bad http status code:" + status));
|
||||
return 0;
|
||||
}
|
||||
auto contet_type = const_cast< HttpClient::HttpHeader &>(headers)["Content-Type"];
|
||||
if (contet_type.find("video/mp2t") == 0 || contet_type.find("video/mpeg") == 0) {
|
||||
auto content_type = const_cast< HttpClient::HttpHeader &>(headers)["Content-Type"];
|
||||
if (content_type.find("video/mp2t") == 0 || content_type.find("video/mpeg") == 0) {
|
||||
_is_ts_content = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ public:
|
||||
|
||||
HttpWsClient(ClientTypeImp<ClientType,DataType> &delegate) : _delegate(delegate){
|
||||
_Sec_WebSocket_Key = encodeBase64(SHA1::encode_bin(makeRandStr(16, false)));
|
||||
_poller = delegate.getPoller();
|
||||
setPoller(delegate.getPoller());
|
||||
}
|
||||
~HttpWsClient(){}
|
||||
|
||||
@@ -312,7 +312,7 @@ private:
|
||||
});
|
||||
|
||||
//设置sock,否则shutdown等接口都无效
|
||||
_delegate.setSock(HttpClientImp::_sock);
|
||||
_delegate.setSock(HttpClientImp::getSock());
|
||||
//触发连接成功事件
|
||||
_delegate.onConnect(ex);
|
||||
//拦截websocket数据接收
|
||||
|
||||
@@ -117,7 +117,7 @@ protected:
|
||||
*/
|
||||
bool onWebSocketConnect(const Parser &header) override{
|
||||
//创建websocket session类
|
||||
_session = _creator(header, *this,HttpSessionType::_sock);
|
||||
_session = _creator(header, *this,HttpSessionType::getSock());
|
||||
if(!_session){
|
||||
//此url不允许创建websocket连接
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user