From 9f2037ec5c2989c26c8a35683f309f3912396da4 Mon Sep 17 00:00:00 2001 From: sky <3178676884@qq.com> Date: Mon, 3 Nov 2025 04:57:10 +0000 Subject: [PATCH] =?UTF-8?q?=E7=9C=9F=E6=9C=BA=E5=BE=AE=E4=BF=A1=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=92=AD=E6=94=BEfmp4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sky <3178676884@qq.com> --- src/Http/HttpSession.cpp | 29 +++++++++++++++++++++++++---- src/Http/HttpSession.h | 3 ++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 05a56488..bb0a967a 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). @@ -378,7 +378,10 @@ bool HttpSession::checkLiveStreamFMP4(const function &cb) { if (!cb) { // 找到源,发送http头,负载后续发送 [AUTO-TRANSLATED:ac272410] // Found the source, send the http header, and send the load later - sendResponse(200, false, HttpFileManager::getContentType(".mp4").data(), KeyValue(), nullptr, true); + _is_chunked = true; + KeyValue headerOut; + headerOut["Transfer-Encoding"] = "chunked"; + sendResponse(200, false, HttpFileManager::getContentType(".mp4").data(), headerOut, nullptr, true); } else { // 自定义发送http头 [AUTO-TRANSLATED:b8a8f683] // Custom send http header @@ -849,8 +852,26 @@ void HttpSession::onWrite(const Buffer::Ptr &buffer, bool flush) { _ticker.resetTime(); if (!_live_over_websocket) { - _total_bytes_usage += buffer->size(); - send(buffer); + if (_is_chunked) { + std::stringstream ss; + ss << hex << buffer->size(); + string sizeStr = ss.str(); + + auto sizeBuffer = make_shared(); + sizeBuffer->assign(sizeStr.data(), sizeStr.size()); + sizeBuffer->append("\r\n"); + send(sizeBuffer); + send(buffer); + + auto lfcf = make_shared(); + lfcf->assign("\r\n"); + send(lfcf); + + _total_bytes_usage += buffer->size() + sizeStr.size() + 4; + } else { + _total_bytes_usage += buffer->size(); + send(buffer); + } } else { WebSocketHeader header; header._fin = true; diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index b714e4b6..0fba8794 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). @@ -159,6 +159,7 @@ private: bool _is_live_stream = false; bool _live_over_websocket = false; bool _is_websocket = false; + bool _is_chunked = false; // 超时时间 [AUTO-TRANSLATED:f15e2672] // Timeout size_t _keep_alive_sec = 0; -- Gitee