From 64553797bd3fe88c7fa998060874741e07590e2c Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Thu, 27 Apr 2023 19:54:47 +0800 Subject: [PATCH 01/66] h265 rtp pack avoid 1387 size nal fu-a only one end packet and first satrt end not exist try fix #2425 --- src/Extension/H265Rtp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extension/H265Rtp.cpp b/src/Extension/H265Rtp.cpp index 246b3ea3..23bbb109 100644 --- a/src/Extension/H265Rtp.cpp +++ b/src/Extension/H265Rtp.cpp @@ -302,7 +302,7 @@ void H265RtpEncoder::packRtpFu(const char *ptr, size_t len, uint64_t pts, bool i } void H265RtpEncoder::packRtp(const char *ptr, size_t len, uint64_t pts, bool is_mark, bool gop_pos){ - if (len + 3 <= getMaxSize()) { + if (len <= getMaxSize()) { //signal-nalu RtpCodec::inputRtp(makeRtp(getTrackType(), ptr, len, is_mark, pts), gop_pos); } else { From b3d4188fde6b4e678799df15404e1395f19977d9 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 28 Apr 2023 01:33:17 +0800 Subject: [PATCH 02/66] =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E7=AB=AF=E8=BF=94=E5=9B=9Econtent-length=3D0=E6=97=B6?= =?UTF-8?q?,TsPlayer=E4=B8=8D=E8=83=BD=E6=AD=A3=E7=A1=AE=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpClient.cpp | 6 +++--- src/Http/TsPlayer.cpp | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 0e50fe0c..32742200 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -226,7 +226,7 @@ ssize_t HttpClient::onRecvHeader(const char *data, size_t len) { if (_total_body_size == 0) { //后续没content,本次http请求结束 - onResponseCompleted_l(SockException(Err_success, "success")); + onResponseCompleted_l(SockException(Err_success, "The request is successful but has no body")); return 0; } @@ -260,7 +260,7 @@ void HttpClient::onRecvContent(const char *data, size_t len) { if (_recved_body_size == (size_t)_total_body_size) { //content接收完毕 onResponseBody(data, len); - onResponseCompleted_l(SockException(Err_success, "success")); + onResponseCompleted_l(SockException(Err_success, "completed")); return; } @@ -329,7 +329,7 @@ void HttpClient::onResponseCompleted_l(const SockException &ex) { if (_total_body_size > 0 && _recved_body_size >= (size_t)_total_body_size) { //回复header中有content-length信息,那么收到的body大于等于声明值则认为成功 - onResponseCompleted(SockException(Err_success, "success")); + onResponseCompleted(SockException(Err_success, "read body completed")); return; } diff --git a/src/Http/TsPlayer.cpp b/src/Http/TsPlayer.cpp index 70383899..20737bc5 100644 --- a/src/Http/TsPlayer.cpp +++ b/src/Http/TsPlayer.cpp @@ -34,7 +34,12 @@ void TsPlayer::teardown() { void TsPlayer::onResponseCompleted(const SockException &ex) { if (!_play_result) { _play_result = true; - onPlayResult(ex); + if (!ex && responseBodyTotalSize() == 0 && responseBodySize() == 0) { + //if the server does not return any data, it is considered a failure + onShutdown(ex); + } else { + onPlayResult(ex); + } } else { onShutdown(ex); } @@ -44,7 +49,7 @@ void TsPlayer::onResponseCompleted(const SockException &ex) { void TsPlayer::onResponseBody(const char *buf, size_t size) { if (!_play_result) { _play_result = true; - onPlayResult(SockException(Err_success, "play http-ts success")); + onPlayResult(SockException(Err_success, "read http-ts stream successfully")); } if (!_benchmark_mode) { HttpTSPlayer::onResponseBody(buf, size); From cdf97e7605d15e6ac218d786e9578241209823be Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 22:03:16 +0800 Subject: [PATCH 03/66] =?UTF-8?q?=E6=9B=B4=E6=96=B0zltoolkit,=E9=80=82?= =?UTF-8?q?=E9=85=8D=E6=9C=80=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- api/source/mk_tcp.cpp | 6 +++--- api/source/mk_tcp_private.h | 2 +- src/Http/HttpClient.cpp | 2 +- src/Http/HttpClient.h | 2 +- src/Http/HttpFileManager.cpp | 2 +- src/Http/HttpSession.cpp | 2 +- src/Http/WebSocketClient.h | 4 ++-- src/Rtmp/RtmpPlayer.cpp | 2 +- src/Rtmp/RtmpPlayer.h | 2 +- src/Rtmp/RtmpPusher.cpp | 2 +- src/Rtmp/RtmpPusher.h | 2 +- src/Rtsp/RtspPlayer.cpp | 2 +- src/Rtsp/RtspPlayer.h | 2 +- src/Rtsp/RtspPusher.cpp | 2 +- src/Rtsp/RtspPusher.h | 2 +- srt/SrtSession.cpp | 2 +- tests/test_wsClient.cpp | 2 +- webrtc/IceServer.cpp | 2 +- webrtc/WebRtcPlayer.cpp | 2 +- webrtc/WebRtcSession.cpp | 2 +- webrtc/WebRtcTransport.cpp | 4 ++-- 22 files changed, 26 insertions(+), 26 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index 7e40c751..d134fbb9 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit 7e40c751659d5c1ec623699732284c12e0a4feb8 +Subproject commit d134fbb9fe0b108dddb66a7bfa35b7b438426411 diff --git a/api/source/mk_tcp.cpp b/api/source/mk_tcp.cpp index 2b79d08a..e96ce4bf 100644 --- a/api/source/mk_tcp.cpp +++ b/api/source/mk_tcp.cpp @@ -139,7 +139,7 @@ API_EXPORT void API_CALL mk_tcp_session_send(const mk_tcp_session ctx, const cha API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ctx, mk_buffer buffer) { assert(ctx && buffer); try { - std::weak_ptr weak_session = ((SessionForC *) ctx)->shared_from_this(); + std::weak_ptr weak_session = ((SessionForC *) ctx)->shared_from_this(); auto ref = mk_buffer_ref(buffer); ((SessionForC *) ctx)->async([weak_session, ref]() { auto session_session = weak_session.lock(); @@ -149,7 +149,7 @@ API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ct mk_buffer_unref(ref); }); } catch (std::exception &ex) { - WarnL << "can not got the strong pionter of this mk_tcp_session:" << ex.what(); + WarnL << "can not got the strong pointer of this mk_tcp_session:" << ex.what(); } } @@ -271,7 +271,7 @@ void TcpClientForC::onRecv(const Buffer::Ptr &pBuf) { } } -void TcpClientForC::onErr(const SockException &ex) { +void TcpClientForC::onError(const SockException &ex) { if(_events.on_mk_tcp_client_disconnect){ _events.on_mk_tcp_client_disconnect(_client,ex.getErrCode(),ex.what()); } diff --git a/api/source/mk_tcp_private.h b/api/source/mk_tcp_private.h index 241e88b6..e569ca16 100644 --- a/api/source/mk_tcp_private.h +++ b/api/source/mk_tcp_private.h @@ -21,7 +21,7 @@ public: TcpClientForC(mk_tcp_client_events *events) ; ~TcpClientForC() override ; void onRecv(const toolkit::Buffer::Ptr &pBuf) override; - void onErr(const toolkit::SockException &ex) override; + void onError(const toolkit::SockException &ex) override; void onManager() override; void onConnect(const toolkit::SockException &ex) override; void setClient(mk_tcp_client client); diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 0e50fe0c..0ff9b789 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -176,7 +176,7 @@ void HttpClient::onRecv(const Buffer::Ptr &pBuf) { HttpRequestSplitter::input(pBuf->data(), pBuf->size()); } -void HttpClient::onErr(const SockException &ex) { +void HttpClient::onError(const SockException &ex) { onResponseCompleted_l(ex); } diff --git a/src/Http/HttpClient.h b/src/Http/HttpClient.h index 82fc2f9d..c82804d4 100644 --- a/src/Http/HttpClient.h +++ b/src/Http/HttpClient.h @@ -177,7 +177,7 @@ protected: //// TcpClient override //// void onConnect(const toolkit::SockException &ex) override; void onRecv(const toolkit::Buffer::Ptr &pBuf) override; - void onErr(const toolkit::SockException &ex) override; + void onError(const toolkit::SockException &ex) override; void onFlush() override; void onManager() override; diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index b690801a..fbd9c174 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -367,7 +367,7 @@ static void accessFile(Session &sender, const Parser &parser, const MediaInfo &m replace(const_cast(media_info._streamid), kHlsSuffix, ""); } - weak_ptr weakSession = sender.shared_from_this(); + weak_ptr weakSession = static_pointer_cast(sender.shared_from_this()); //判断是否有权限访问该文件 canAccessPath(sender, parser, media_info, false, [cb, file_path, parser, is_hls, media_info, weakSession](const string &err_msg, const HttpServerCookie::Ptr &cookie) { auto strongSession = weakSession.lock(); diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 79924933..7acd9fa7 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -612,7 +612,7 @@ void HttpSession::sendResponse(int code, } //发送http body - AsyncSenderData::Ptr data = std::make_shared(shared_from_this(), body, bClose); + AsyncSenderData::Ptr data = std::make_shared(static_pointer_cast(shared_from_this()), body, bClose); getSock()->setOnFlush([data]() { return AsyncSender::onSocketFlushed(data); }); AsyncSender::onSocketFlushed(data); } diff --git a/src/Http/WebSocketClient.h b/src/Http/WebSocketClient.h index 737ebcda..379ae66d 100644 --- a/src/Http/WebSocketClient.h +++ b/src/Http/WebSocketClient.h @@ -218,7 +218,7 @@ protected: /** * tcp连接断开 */ - void onErr(const toolkit::SockException &ex) override { + void onError(const toolkit::SockException &ex) override { // tcp断开或者shutdown导致的断开 onWebSocketException(ex); } @@ -350,7 +350,7 @@ private: // 握手成功之后的中途断开 _onRecv = nullptr; if (auto strong_ref = _weak_delegate.lock()) { - strong_ref->onErr(ex); + strong_ref->onError(ex); } return; } diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 780ce980..4e7d903d 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -90,7 +90,7 @@ void RtmpPlayer::play(const string &url) { startConnect(host_url, port, play_timeout_sec); } -void RtmpPlayer::onErr(const SockException &ex){ +void RtmpPlayer::onError(const SockException &ex){ //定时器_pPlayTimer为空后表明握手结束了 onPlayResult_l(ex, !_play_timer); } diff --git a/src/Rtmp/RtmpPlayer.h b/src/Rtmp/RtmpPlayer.h index 55e4c9c4..0a45f1b5 100644 --- a/src/Rtmp/RtmpPlayer.h +++ b/src/Rtmp/RtmpPlayer.h @@ -50,7 +50,7 @@ protected: //form Tcpclient void onRecv(const toolkit::Buffer::Ptr &buf) override; void onConnect(const toolkit::SockException &err) override; - void onErr(const toolkit::SockException &ex) override; + void onError(const toolkit::SockException &ex) override; //from RtmpProtocol void onRtmpChunk(RtmpPacket::Ptr chunk_data) override; void onStreamDry(uint32_t stream_index) override; diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index 109696c5..bf5ca332 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -97,7 +97,7 @@ void RtmpPusher::publish(const string &url) { startConnect(host_url, port); } -void RtmpPusher::onErr(const SockException &ex){ +void RtmpPusher::onError(const SockException &ex){ //定时器_pPublishTimer为空后表明握手结束了 onPublishResult_l(ex, !_publish_timer); } diff --git a/src/Rtmp/RtmpPusher.h b/src/Rtmp/RtmpPusher.h index bd41fd75..0b9cdb89 100644 --- a/src/Rtmp/RtmpPusher.h +++ b/src/Rtmp/RtmpPusher.h @@ -31,7 +31,7 @@ protected: //for Tcpclient override void onRecv(const toolkit::Buffer::Ptr &buf) override; void onConnect(const toolkit::SockException &err) override; - void onErr(const toolkit::SockException &ex) override; + void onError(const toolkit::SockException &ex) override; //for RtmpProtocol override void onRtmpChunk(RtmpPacket::Ptr chunk_data) override; diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index 2d7ab925..6fae5e07 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -131,7 +131,7 @@ void RtspPlayer::onRecv(const Buffer::Ptr& buf) { } } -void RtspPlayer::onErr(const SockException &ex) { +void RtspPlayer::onError(const SockException &ex) { //定时器_pPlayTimer为空后表明握手结束了 onPlayResult_l(ex,!_play_check_timer); } diff --git a/src/Rtsp/RtspPlayer.h b/src/Rtsp/RtspPlayer.h index 0bd101cd..5a9bd1ee 100644 --- a/src/Rtsp/RtspPlayer.h +++ b/src/Rtsp/RtspPlayer.h @@ -84,7 +84,7 @@ protected: /////////////TcpClient override///////////// void onConnect(const toolkit::SockException &err) override; void onRecv(const toolkit::Buffer::Ptr &buf) override; - void onErr(const toolkit::SockException &ex) override; + void onError(const toolkit::SockException &ex) override; private: void onPlayResult_l(const toolkit::SockException &ex , bool handshake_done); diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 04d0d1a1..575f551c 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -118,7 +118,7 @@ void RtspPusher::onPublishResult_l(const SockException &ex, bool handshake_done) } } -void RtspPusher::onErr(const SockException &ex) { +void RtspPusher::onError(const SockException &ex) { //定时器_pPublishTimer为空后表明握手结束了 onPublishResult_l(ex, !_publish_timer); } diff --git a/src/Rtsp/RtspPusher.h b/src/Rtsp/RtspPusher.h index 2cddd61f..63353368 100644 --- a/src/Rtsp/RtspPusher.h +++ b/src/Rtsp/RtspPusher.h @@ -35,7 +35,7 @@ protected: //for Tcpclient override void onRecv(const toolkit::Buffer::Ptr &buf) override; void onConnect(const toolkit::SockException &err) override; - void onErr(const toolkit::SockException &ex) override; + void onError(const toolkit::SockException &ex) override; //RtspSplitter override void onWholeRtspPacket(Parser &parser) override ; diff --git a/srt/SrtSession.cpp b/srt/SrtSession.cpp index 5f73d92f..9f83ddde 100644 --- a/srt/SrtSession.cpp +++ b/srt/SrtSession.cpp @@ -100,7 +100,7 @@ void SrtSession::onRecv(const Buffer::Ptr &buffer) { } if (_transport) { - _transport->setSession(shared_from_this()); + _transport->setSession(static_pointer_cast(shared_from_this())); } InfoP(this); } diff --git a/tests/test_wsClient.cpp b/tests/test_wsClient.cpp index bd3ba0b5..a3819799 100644 --- a/tests/test_wsClient.cpp +++ b/tests/test_wsClient.cpp @@ -32,7 +32,7 @@ protected: DebugL << pBuf->toString(); } //被动断开连接回调 - void onErr(const SockException &ex) override { + void onError(const SockException &ex) override { WarnL << ex; } //tcp连接成功后每2秒触发一次该事件 diff --git a/webrtc/IceServer.cpp b/webrtc/IceServer.cpp index f0f79358..40e53aa8 100644 --- a/webrtc/IceServer.cpp +++ b/webrtc/IceServer.cpp @@ -520,7 +520,7 @@ namespace RTC return; this->selectedTuple = storedTuple; - this->lastSelectedTuple = storedTuple->shared_from_this(); + this->lastSelectedTuple = static_pointer_cast(storedTuple->shared_from_this()); // Notify the listener. this->listener->OnIceServerSelectedTuple(this, this->selectedTuple); diff --git a/webrtc/WebRtcPlayer.cpp b/webrtc/WebRtcPlayer.cpp index bbdb6274..a766607f 100644 --- a/webrtc/WebRtcPlayer.cpp +++ b/webrtc/WebRtcPlayer.cpp @@ -47,7 +47,7 @@ void WebRtcPlayer::onStartWebRTC() { playSrc->pause(false); _reader = playSrc->getRing()->attach(getPoller(), true); weak_ptr weak_self = static_pointer_cast(shared_from_this()); - weak_ptr weak_session = getSession(); + weak_ptr weak_session = static_pointer_cast(getSession()); _reader->setGetInfoCB([weak_session]() { return weak_session.lock(); }); _reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt) { auto strong_self = weak_self.lock(); diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index 6216802c..28c60d58 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -108,7 +108,7 @@ void WebRtcSession::onError(const SockException &err) { if (!_transport) { return; } - auto self = shared_from_this(); + auto self = static_pointer_cast(shared_from_this()); auto transport = std::move(_transport); getPoller()->async([transport, self]() mutable { //延时减引用,防止使用transport对象时,销毁对象 diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 1d1431c9..05935076 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -230,7 +230,7 @@ void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTu Session::Ptr WebRtcTransport::getSession() const { auto tuple = _ice_server->GetSelectedTuple(true); - return tuple ? tuple->shared_from_this() : nullptr; + return tuple ? static_pointer_cast(tuple->shared_from_this()) : nullptr; } void WebRtcTransport::sendRtcpRemb(uint32_t ssrc, size_t bit_rate) { @@ -1209,7 +1209,7 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana MediaInfo info(args["url"]); bool preferred_tcp = args["preferred_tcp"]; - auto session_ptr = sender.shared_from_this(); + auto session_ptr = static_pointer_cast(sender.shared_from_this()); Broadcast::AuthInvoker invoker = [cb, info, session_ptr, preferred_tcp](const string &err) mutable { if (!err.empty()) { cb(WebRtcException(SockException(Err_other, err))); From d2349f01bd8039a6c6e940b53dc12a7f35befb8c Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 22:04:38 +0800 Subject: [PATCH 04/66] =?UTF-8?q?=E4=BD=BF=E7=94=A8static=5Fpointer=5Fcast?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/source/mk_tcp.cpp | 2 +- api/source/mk_thread.cpp | 2 +- src/FMP4/FMP4MediaSource.h | 2 +- src/Http/HlsPlayer.cpp | 6 +++--- src/Http/HttpRequester.cpp | 2 +- src/Http/HttpSession.cpp | 17 +++++++++-------- src/Http/HttpSession.h | 7 ++++--- src/Http/WebSocketClient.h | 2 +- src/Http/WebSocketSession.h | 2 +- src/Record/HlsMediaSource.cpp | 2 +- src/Rtmp/RtmpMediaSourceImp.cpp | 2 +- src/Rtmp/RtmpPlayer.cpp | 8 ++++---- src/Rtmp/RtmpPusher.cpp | 6 +++--- src/Rtmp/RtmpSession.cpp | 10 +++++----- src/Rtp/RtpSession.cpp | 2 +- src/Rtsp/RtspMediaSourceImp.cpp | 2 +- src/Rtsp/RtspPlayer.cpp | 6 +++--- src/Rtsp/RtspPusher.cpp | 6 +++--- src/Rtsp/RtspSession.cpp | 22 +++++++++++----------- src/Shell/ShellSession.cpp | 2 +- src/TS/TSMediaSource.h | 2 +- webrtc/WebRtcSession.cpp | 2 +- webrtc/WebRtcTransport.cpp | 2 +- 23 files changed, 59 insertions(+), 57 deletions(-) diff --git a/api/source/mk_tcp.cpp b/api/source/mk_tcp.cpp index e96ce4bf..d155cac8 100644 --- a/api/source/mk_tcp.cpp +++ b/api/source/mk_tcp.cpp @@ -155,7 +155,7 @@ API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ct API_EXPORT mk_tcp_session_ref API_CALL mk_tcp_session_ref_from(const mk_tcp_session ctx) { auto ref = ((SessionForC *) ctx)->shared_from_this(); - return (mk_tcp_session_ref)new std::shared_ptr(std::dynamic_pointer_cast(ref)); + return (mk_tcp_session_ref)new std::shared_ptr(std::static_pointer_cast(ref)); } API_EXPORT void mk_tcp_session_ref_release(const mk_tcp_session_ref ref) { diff --git a/api/source/mk_thread.cpp b/api/source/mk_thread.cpp index 56b5be2a..29775b43 100644 --- a/api/source/mk_thread.cpp +++ b/api/source/mk_thread.cpp @@ -143,7 +143,7 @@ public: } EventPoller::Ptr getPoller() { - return dynamic_pointer_cast(getExecutor()); + return static_pointer_cast(getExecutor()); } }; diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index e004dc5e..7986a08c 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -108,7 +108,7 @@ public: private: void createRing(){ - std::weak_ptr weak_self = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weak_self = std::static_pointer_cast(shared_from_this()); _ring = std::make_shared(_ring_size, [weak_self](int size) { auto strong_self = weak_self.lock(); if (!strong_self) { diff --git a/src/Http/HlsPlayer.cpp b/src/Http/HlsPlayer.cpp index e8ca1471..1de45cf8 100644 --- a/src/Http/HlsPlayer.cpp +++ b/src/Http/HlsPlayer.cpp @@ -80,7 +80,7 @@ void HlsPlayer::fetchSegment() { //播放器目前还存活,正在下载中 return; } - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); if (!_http_ts_player) { _http_ts_player = std::make_shared(getPoller()); _http_ts_player->setOnCreateSocket([weak_self](const EventPoller::Ptr &poller) { @@ -186,7 +186,7 @@ bool HlsPlayer::onParsed(bool is_m3u8_inner, int64_t sequence, const map weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); auto url = ts_map.rbegin()->second.url; getPoller()->async([weak_self, url]() { auto strong_self = weak_self.lock(); @@ -259,7 +259,7 @@ bool HlsPlayer::onRedirectUrl(const string &url, bool temporary) { } void HlsPlayer::playDelay() { - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _timer.reset(new Timer(delaySecond(), [weak_self]() { auto strong_self = weak_self.lock(); if (strong_self) { diff --git a/src/Http/HttpRequester.cpp b/src/Http/HttpRequester.cpp index ddc73631..3e403df2 100644 --- a/src/Http/HttpRequester.cpp +++ b/src/Http/HttpRequester.cpp @@ -27,7 +27,7 @@ void HttpRequester::onResponseBody(const char *buf, size_t size) { void HttpRequester::onResponseCompleted(const SockException &ex) { if (ex && _retry++ < _max_retry) { - std::weak_ptr weak_self = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weak_self = std::static_pointer_cast(shared_from_this()); getPoller()->doDelayTask(_retry_delay, [weak_self](){ if (auto self = weak_self.lock()) { InfoL << "resend request " << self->getUrl() << " with retry " << self->getRetry(); diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 7acd9fa7..8243cba0 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -202,7 +202,7 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffi } bool close_flag = !strcasecmp(_parser["Connection"].data(), "close"); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); //鉴权结果回调 auto onRes = [cb, weak_self, close_flag](const string &err) { @@ -266,7 +266,7 @@ bool HttpSession::checkLiveStreamFMP4(const function &cb){ //直播牺牲延时提升发送性能 setSocketFlags(); onWrite(std::make_shared(fmp4_src->getInitSegment()), true); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); fmp4_src->pause(false); _fmp4_reader = fmp4_src->getRing()->attach(getPoller()); _fmp4_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); }); @@ -308,7 +308,7 @@ bool HttpSession::checkLiveStreamTS(const function &cb){ //直播牺牲延时提升发送性能 setSocketFlags(); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); ts_src->pause(false); _ts_reader = ts_src->getRing()->attach(getPoller()); _ts_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); }); @@ -406,7 +406,7 @@ void HttpSession::Handle_Req_GET_l(ssize_t &content_len, bool sendBody) { } bool bClose = !strcasecmp(_parser["Connection"].data(),"close"); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); HttpFileManager::onAccessPath(*this, _parser, [weak_self, bClose](int code, const string &content_type, const StrCaseMap &responseHeader, const HttpBody::Ptr &body) { auto strong_self = weak_self.lock(); @@ -434,12 +434,13 @@ class AsyncSenderData { public: friend class AsyncSender; using Ptr = std::shared_ptr; - AsyncSenderData(const Session::Ptr &session, const HttpBody::Ptr &body, bool close_when_complete) { - _session = dynamic_pointer_cast(session); + AsyncSenderData(HttpSession::Ptr session, const HttpBody::Ptr &body, bool close_when_complete) { + _session = std::move(session); _body = body; _close_when_complete = close_when_complete; } ~AsyncSenderData() = default; + private: std::weak_ptr _session; HttpBody::Ptr _body; @@ -612,7 +613,7 @@ void HttpSession::sendResponse(int code, } //发送http body - AsyncSenderData::Ptr data = std::make_shared(static_pointer_cast(shared_from_this()), body, bClose); + AsyncSenderData::Ptr data = std::make_shared(static_pointer_cast(shared_from_this()), body, bClose); getSock()->setOnFlush([data]() { return AsyncSender::onSocketFlushed(data); }); AsyncSender::onSocketFlushed(data); } @@ -639,7 +640,7 @@ void HttpSession::urlDecode(Parser &parser){ bool HttpSession::emitHttpEvent(bool doInvoke){ bool bClose = !strcasecmp(_parser["Connection"].data(),"close"); /////////////////////异步回复Invoker/////////////////////////////// - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); HttpResponseInvoker invoker = [weak_self,bClose](int code, const KeyValue &headerOut, const HttpBody::Ptr &body){ auto strong_self = weak_self.lock(); if(!strong_self) { diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 42eaad44..b06a7f9e 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -28,15 +28,16 @@ class HttpSession: public toolkit::Session, public HttpRequestSplitter, public WebSocketSplitter { public: - typedef StrCaseMap KeyValue; - typedef HttpResponseInvokerImp HttpResponseInvoker; + using Ptr = std::shared_ptr; + using KeyValue = StrCaseMap; + using HttpResponseInvoker = HttpResponseInvokerImp ; friend class AsyncSender; /** * @param errMsg 如果为空,则代表鉴权通过,否则为错误提示 * @param accessPath 运行或禁止访问的根目录 * @param cookieLifeSecond 鉴权cookie有效期 **/ - typedef std::function HttpAccessPathInvoker; + using HttpAccessPathInvoker = std::function; HttpSession(const toolkit::Socket::Ptr &pSock); ~HttpSession() override; diff --git a/src/Http/WebSocketClient.h b/src/Http/WebSocketClient.h index 379ae66d..5850b027 100644 --- a/src/Http/WebSocketClient.h +++ b/src/Http/WebSocketClient.h @@ -316,7 +316,7 @@ private: if (!ex) { // websocket握手成功 // 此处截取TcpClient派生类发送的数据并进行websocket协议打包 - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weakSelf = std::static_pointer_cast(shared_from_this()); if (auto strong_ref = _weak_delegate.lock()) { strong_ref->setOnBeforeSendCB([weakSelf](const toolkit::Buffer::Ptr &buf) { auto strong_self = weakSelf.lock(); diff --git a/src/Http/WebSocketSession.h b/src/Http/WebSocketSession.h index c8d8b432..96021c4d 100644 --- a/src/Http/WebSocketSession.h +++ b/src/Http/WebSocketSession.h @@ -139,7 +139,7 @@ protected: } //此处截取数据并进行websocket协议打包 - std::weak_ptr weakSelf = std::dynamic_pointer_cast(HttpSessionType::shared_from_this()); + std::weak_ptr weakSelf = std::static_pointer_cast(HttpSessionType::shared_from_this()); std::dynamic_pointer_cast(_session)->setOnBeforeSendCB([weakSelf](const toolkit::Buffer::Ptr &buf) { auto strongSelf = weakSelf.lock(); if (strongSelf) { diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp index 406a6c6a..d6d4f4b4 100644 --- a/src/Record/HlsMediaSource.cpp +++ b/src/Record/HlsMediaSource.cpp @@ -68,7 +68,7 @@ HlsMediaSource::Ptr HlsCookieData::getMediaSource() const { void HlsMediaSource::setIndexFile(std::string index_file) { if (!_ring) { - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weakSelf = std::static_pointer_cast(shared_from_this()); auto lam = [weakSelf](int size) { auto strongSelf = weakSelf.lock(); if (!strongSelf) { diff --git a/src/Rtmp/RtmpMediaSourceImp.cpp b/src/Rtmp/RtmpMediaSourceImp.cpp index 8e49e534..7777a0fa 100644 --- a/src/Rtmp/RtmpMediaSourceImp.cpp +++ b/src/Rtmp/RtmpMediaSourceImp.cpp @@ -41,7 +41,7 @@ void RtmpMediaSource::onWrite(RtmpPacket::Ptr pkt, bool /*= true*/) } if (!_ring) { - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weakSelf = std::static_pointer_cast(shared_from_this()); auto lam = [weakSelf](int size) { auto strongSelf = weakSelf.lock(); if (!strongSelf) { diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 4e7d903d..ffaf0dfc 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -75,7 +75,7 @@ void RtmpPlayer::play(const string &url) { setNetAdapter((*this)[Client::kNetAdapter]); } - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); float play_timeout_sec = (*this)[Client::kTimeoutMS].as() / 1000.0f; _play_timer.reset(new Timer(play_timeout_sec, [weak_self]() { auto strong_self = weak_self.lock(); @@ -120,7 +120,7 @@ void RtmpPlayer::onPlayResult_l(const SockException &ex, bool handshake_done) { //播放成功,恢复rtmp接收超时定时器 _rtmp_recv_ticker.resetTime(); auto timeout_ms = (*this)[Client::kMediaTimeoutMS].as(); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); auto lam = [weak_self, timeout_ms]() { auto strong_self = weak_self.lock(); if (!strong_self) { @@ -146,7 +146,7 @@ void RtmpPlayer::onConnect(const SockException &err) { onPlayResult_l(err, false); return; } - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); startClientSession([weak_self]() { if (auto strong_self = weak_self.lock()) { strong_self->send_connect(); @@ -258,7 +258,7 @@ void RtmpPlayer::send_pause(bool pause) { _beat_timer.reset(); if (pause) { - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _beat_timer.reset(new Timer((*this)[Client::kBeatIntervalMS].as() / 1000.0f, [weak_self]() { auto strong_self = weak_self.lock(); if (!strong_self) { diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index bf5ca332..31143dcf 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -79,7 +79,7 @@ void RtmpPusher::publish(const string &url) { uint16_t port = 1935; splitUrl(host_url, host_url, port); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + weak_ptr weakSelf = static_pointer_cast(shared_from_this()); float publishTimeOutSec = (*this)[Client::kTimeoutMS].as() / 1000.0f; _publish_timer.reset(new Timer(publishTimeOutSec, [weakSelf]() { auto strongSelf = weakSelf.lock(); @@ -107,7 +107,7 @@ void RtmpPusher::onConnect(const SockException &err){ onPublishResult_l(err, false); return; } - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); startClientSession([weak_self]() { auto strong_self = weak_self.lock(); if (!strong_self) { @@ -193,7 +193,7 @@ void RtmpPusher::send_metaData(){ src->pause(false); _rtmp_reader = src->getRing()->attach(getPoller()); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _rtmp_reader->setReadCB([weak_self](const RtmpMediaSource::RingDataType &pkt) { auto strong_self = weak_self.lock(); if (!strong_self) { diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index ae827551..3c3f552e 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -127,7 +127,7 @@ void RtmpSession::onCmd_createStream(AMFDecoder &dec) { void RtmpSession::onCmd_publish(AMFDecoder &dec) { std::shared_ptr ticker(new Ticker); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); std::shared_ptr token(new onceToken(nullptr, [ticker, weak_self]() { auto strong_self = weak_self.lock(); if (strong_self) { @@ -188,7 +188,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { _push_src->setProtocolOption(option); } - _push_src->setListener(dynamic_pointer_cast(shared_from_this())); + _push_src->setListener(static_pointer_cast(shared_from_this())); _continue_push_ms = option.continue_push_ms; sendStatus({"level", "status", "code", "NetStream.Publish.Start", @@ -310,7 +310,7 @@ void RtmpSession::sendPlayResponse(const string &err, const RtmpMediaSource::Ptr src->pause(false); _ring_reader = src->getRing()->attach(getPoller()); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _ring_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); }); _ring_reader->setReadCB([weak_self](const RtmpMediaSource::RingDataType &pkt) { auto strong_self = weak_self.lock(); @@ -349,7 +349,7 @@ void RtmpSession::doPlayResponse(const string &err,const std::function weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); MediaSource::findAsync(_media_info, weak_self.lock(), [weak_self,cb](const MediaSource::Ptr &src){ auto rtmp_src = dynamic_pointer_cast(src); auto strong_self = weak_self.lock(); @@ -362,7 +362,7 @@ void RtmpSession::doPlayResponse(const string &err,const std::function ticker(new Ticker); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); std::shared_ptr token(new onceToken(nullptr, [ticker,weak_self](){ auto strong_self = weak_self.lock(); if (strong_self) { diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index 26faadfb..38b6d4cd 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -122,7 +122,7 @@ void RtpSession::onRtpPacket(const char *data, size_t len) { return; } _process->setOnlyAudio(_only_audio); - _process->setDelegate(dynamic_pointer_cast(shared_from_this())); + _process->setDelegate(static_pointer_cast(shared_from_this())); } try { uint32_t rtp_ssrc = 0; diff --git a/src/Rtsp/RtspMediaSourceImp.cpp b/src/Rtsp/RtspMediaSourceImp.cpp index a262fad5..7354306f 100644 --- a/src/Rtsp/RtspMediaSourceImp.cpp +++ b/src/Rtsp/RtspMediaSourceImp.cpp @@ -55,7 +55,7 @@ void RtspMediaSource::onWrite(RtpPacket::Ptr rtp, bool keyPos) { track->_ssrc = rtp->getSSRC(); } if (!_ring) { - std::weak_ptr weakSelf = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weakSelf = std::static_pointer_cast(shared_from_this()); auto lam = [weakSelf](int size) { auto strongSelf = weakSelf.lock(); if (!strongSelf) { diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index 6fae5e07..67d0b281 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -92,7 +92,7 @@ void RtspPlayer::play(const string &strUrl){ _rtp_type = (Rtsp::eRtpType)(int)(*this)[Client::kRtpType]; DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type; - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + weak_ptr weakSelf = static_pointer_cast(shared_from_this()); float playTimeOutSec = (*this)[Client::kTimeoutMS].as() / 1000.0f; _play_check_timer.reset(new Timer(playTimeOutSec, [weakSelf]() { auto strongSelf=weakSelf.lock(); @@ -348,7 +348,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { } auto peer_ip = get_peer_ip(); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + weak_ptr weakSelf = static_pointer_cast(shared_from_this()); //设置rtp over udp接收回调处理函数 pRtpSockRef->setOnRead([peer_ip, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) { auto strongSelf = weakSelf.lock(); @@ -705,7 +705,7 @@ void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshake_done) { //播放成功,恢复rtp接收超时定时器 _rtp_recv_ticker.resetTime(); auto timeoutMS = (*this)[Client::kMediaTimeoutMS].as(); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + weak_ptr weakSelf = static_pointer_cast(shared_from_this()); auto lam = [weakSelf, timeoutMS]() { auto strongSelf = weakSelf.lock(); if (!strongSelf) { diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 575f551c..01727adc 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -80,7 +80,7 @@ void RtspPusher::publish(const string &url_str) { DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type; - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); float publish_timeout_sec = (*this)[Client::kTimeoutMS].as() / 1000.0f; _publish_timer.reset(new Timer(publish_timeout_sec, [weak_self]() { auto strong_self = weak_self.lock(); @@ -320,7 +320,7 @@ void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) { rtcp_sock->bindPeerAddr((struct sockaddr *)&(rtcpto)); auto peer_ip = get_peer_ip(); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + weak_ptr weakSelf = static_pointer_cast(shared_from_this()); if(rtcp_sock) { //设置rtcp over udp接收回调处理函数 rtcp_sock->setOnRead([peer_ip, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) { @@ -451,7 +451,7 @@ void RtspPusher::sendRecord() { src->pause(false); _rtsp_reader = src->getRing()->attach(getPoller()); - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _rtsp_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt) { auto strong_self = weak_self.lock(); if (!strong_self) { diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index c02772ce..5388ad53 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -273,12 +273,12 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { _push_src->setSdp(parser.Content()); } - _push_src->setListener(dynamic_pointer_cast(shared_from_this())); + _push_src->setListener(static_pointer_cast(shared_from_this())); _continue_push_ms = option.continue_push_ms; sendRtspResponse("200 OK"); }; - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); Broadcast::PublishAuthInvoker invoker = [weak_self, onRes](const string &err, const ProtocolOption &option) { auto strong_self = weak_self.lock(); if (!strong_self) { @@ -325,7 +325,7 @@ void RtspSession::handleReq_RECORD(const Parser &parser){ } void RtspSession::emitOnPlay(){ - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); //url鉴权回调 auto onRes = [weak_self](const string &err) { auto strong_self = weak_self.lock(); @@ -364,7 +364,7 @@ void RtspSession::emitOnPlay(){ void RtspSession::handleReq_Describe(const Parser &parser) { //该请求中的认证信息 auto authorization = parser["Authorization"]; - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); //rtsp专属鉴权是否开启事件回调 onGetRealm invoker = [weak_self, authorization](const string &realm) { auto strong_self = weak_self.lock(); @@ -402,7 +402,7 @@ void RtspSession::handleReq_Describe(const Parser &parser) { } void RtspSession::onAuthSuccess() { - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); MediaSource::findAsync(_media_info, weak_self.lock(), [weak_self](const MediaSource::Ptr &src){ auto strong_self = weak_self.lock(); if(!strong_self){ @@ -471,7 +471,7 @@ void RtspSession::onAuthBasic(const string &realm, const string &auth_base64) { } auto user = user_pwd_vec[0]; auto pwd = user_pwd_vec[1]; - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); onAuth invoker = [pwd, realm, weak_self](bool encrypted, const string &good_pwd) { auto strong_self = weak_self.lock(); if (!strong_self) { @@ -564,7 +564,7 @@ void RtspSession::onAuthDigest(const string &realm,const string &auth_md5){ } }; - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); onAuth invoker = [realInvoker,weak_self](bool encrypted,const string &good_pwd){ auto strong_self = weak_self.lock(); if(!strong_self){ @@ -722,7 +722,7 @@ void RtspSession::handleReq_Setup(const Parser &parser) { send_NotAcceptable(); throw SockException(Err_shutdown, "can not get a available udp multicast socket"); } - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _multicaster->setDetachCB(this, [weak_self]() { auto strong_self = weak_self.lock(); if(!strong_self) { @@ -830,7 +830,7 @@ void RtspSession::handleReq_Play(const Parser &parser) { setSocketFlags(); if (!_play_reader && _rtp_type != Rtsp::RTP_MULTICAST) { - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); _play_reader = play_src->getRing()->attach(getPoller(), use_gop); _play_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); }); _play_reader->setDetachCB([weak_self]() { @@ -880,7 +880,7 @@ void RtspSession::handleReq_Get(const Parser &parser) { //注册http getter,以便http poster绑定 lock_guard lock(g_mtxGetter); - g_mapGetter[_http_x_sessioncookie] = dynamic_pointer_cast(shared_from_this()); + g_mapGetter[_http_x_sessioncookie] = static_pointer_cast(shared_from_this()); } void RtspSession::handleReq_Post(const Parser &parser) { @@ -973,7 +973,7 @@ void RtspSession::onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, cons } void RtspSession::startListenPeerUdpData(int track_idx) { - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); auto peer_ip = get_peer_ip(); auto onUdpData = [weak_self,peer_ip](const Buffer::Ptr &buf, struct sockaddr *peer_addr, int interleaved){ auto strong_self = weak_self.lock(); diff --git a/src/Shell/ShellSession.cpp b/src/Shell/ShellSession.cpp index 81e18d7b..fe0c23fd 100644 --- a/src/Shell/ShellSession.cpp +++ b/src/Shell/ShellSession.cpp @@ -120,7 +120,7 @@ inline void ShellSession::pleaseInputPasswd() { _loginInterceptor=nullptr; }; - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + weak_ptr weakSelf = static_pointer_cast(shared_from_this()); Broadcast::AuthInvoker invoker = [weakSelf,onAuth](const string &errMessage){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index 1f4503a4..8a22ad90 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -92,7 +92,7 @@ public: private: void createRing(){ - std::weak_ptr weak_self = std::dynamic_pointer_cast(shared_from_this()); + std::weak_ptr weak_self = std::static_pointer_cast(shared_from_this()); _ring = std::make_shared(_ring_size, [weak_self](int size) { auto strong_self = weak_self.lock(); if (!strong_self) { diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index 28c60d58..ee3051c0 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -54,7 +54,7 @@ WebRtcSession::WebRtcSession(const Socket::Ptr &sock) : Session(sock) { WebRtcSession::~WebRtcSession() = default; void WebRtcSession::attachServer(const Server &server) { - _server = std::dynamic_pointer_cast(const_cast(server).shared_from_this()); + _server = std::static_pointer_cast(const_cast(server).shared_from_this()); } void WebRtcSession::onRecv_l(const char *data, size_t len) { diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 05935076..470ef733 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -860,7 +860,7 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) { void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, MediaTrack &track) { // rid --> RtpReceiverImp auto &ref = track.rtp_channel[rid]; - weak_ptr weak_self = dynamic_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); ref = std::make_shared( getPoller(), [&track, this, rid](RtpPacket::Ptr rtp) mutable { onSortedRtp(track, rid, std::move(rtp)); }, [&track, weak_self, ssrc](const FCI_NACK &nack) mutable { From 8c049d4e285c1520c9cab56d7cf72005df8eac1d Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 22:06:46 +0800 Subject: [PATCH 05/66] =?UTF-8?q?=E5=AE=9E=E7=8E=B0whip/whep=20delete?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过whip/whep 回复http头中的Location url进行删除资源 新增delete token随机数实现删除鉴权 --- server/WebApi.cpp | 25 ++++++++++++++++++++++--- src/Http/HttpSession.cpp | 1 + webrtc/WebRtcTransport.cpp | 16 ++++++++++++++++ webrtc/WebRtcTransport.h | 6 +++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index aea85a6c..bc170cd0 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1628,18 +1628,21 @@ void installWebApi() { }); }); + static constexpr char delete_webrtc_url [] = "/index/api/delete_webrtc"; static auto whip_whep_func = [](const char *type, API_ARGS_STRING_ASYNC) { auto offer = allArgs.getArgs(); CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty"); - WebRtcPluginManager::Instance().getAnswerSdp(static_cast(sender), type, - WebRtcArgsImp(allArgs, sender.getIdentifier()), - [invoker, offer, headerOut](const WebRtcInterface &exchanger) mutable { + auto &session = static_cast(sender); + auto location = std::string("http") + (session.overSsl() ? "s" : "") + "://" + allArgs["host"] + delete_webrtc_url; + WebRtcPluginManager::Instance().getAnswerSdp(session, type, WebRtcArgsImp(allArgs, sender.getIdentifier()), + [invoker, offer, headerOut, location](const WebRtcInterface &exchanger) mutable { // 设置跨域 headerOut["Access-Control-Allow-Origin"] = "*"; try { // 设置返回类型 headerOut["Content-Type"] = "application/sdp"; + headerOut["Location"] = location + "?id=" + exchanger.getIdentifier() + "&token=" + exchanger.deleteRandStr(); invoker(201, headerOut, exchangeSdp(exchanger, offer)); } catch (std::exception &ex) { headerOut["Content-Type"] = "text/plain"; @@ -1650,6 +1653,22 @@ void installWebApi() { api_regist("/index/api/whip", [](API_ARGS_STRING_ASYNC) { whip_whep_func("push", API_ARGS_VALUE, invoker); }); api_regist("/index/api/whep", [](API_ARGS_STRING_ASYNC) { whip_whep_func("play", API_ARGS_VALUE, invoker); }); + + api_regist(delete_webrtc_url, [](API_ARGS_MAP_ASYNC) { + CHECK_ARGS("id", "token"); + CHECK(allArgs.getParser().Method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().Method()); + auto obj = WebRtcTransportManager::Instance().getItem(allArgs["id"]); + if (!obj) { + invoker(404, headerOut, "id not found"); + return; + } + if (obj->deleteRandStr() != allArgs["token"]) { + invoker(401, headerOut, "token incorrect"); + return; + } + obj->safeShutdown(SockException(Err_shutdown, "deleted by http api")); + invoker(200, headerOut, ""); + }); #endif #if defined(ENABLE_VERSION) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 8243cba0..a990f98b 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -52,6 +52,7 @@ ssize_t HttpSession::onRecvHeader(const char *header,size_t len) { static unordered_map s_func_map; static onceToken token([]() { s_func_map.emplace("GET",&HttpSession::Handle_Req_GET); + s_func_map.emplace("DELETE",&HttpSession::Handle_Req_GET); s_func_map.emplace("POST",&HttpSession::Handle_Req_POST); s_func_map.emplace("HEAD",&HttpSession::Handle_Req_HEAD); s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 470ef733..7dc9e816 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -113,6 +113,13 @@ const string &WebRtcTransport::getIdentifier() const { return _identifier; } +const std::string& WebRtcTransport::deleteRandStr() const { + if (_delete_rand_str.empty()) { + _delete_rand_str = makeRandStr(32); + } + return _delete_rand_str; +} + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void WebRtcTransport::OnIceServerSendStunPacket( @@ -1053,6 +1060,15 @@ void WebRtcTransportImp::onBeforeEncryptRtp(const char *buf, int &len, void *ctx } } +void WebRtcTransportImp::safeShutdown(const SockException &ex) { + std::weak_ptr weak_self = static_pointer_cast(shared_from_this()); + getPoller()->async([ex, weak_self]() { + if (auto strong_self = weak_self.lock()) { + strong_self->onShutdown(ex); + } + }); +} + void WebRtcTransportImp::onShutdown(const SockException &ex) { WarnL << ex; unrefSelf(); diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index eb33b090..2534ca3b 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -40,7 +40,8 @@ public: WebRtcInterface() = default; virtual ~WebRtcInterface() = default; virtual std::string getAnswerSdp(const std::string &offer) = 0; - virtual const std::string &getIdentifier() const = 0; + virtual const std::string& getIdentifier() const = 0; + virtual const std::string& deleteRandStr() const { static std::string s_null; return s_null; } }; std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer); @@ -92,6 +93,7 @@ public: * 获取对象唯一id */ const std::string& getIdentifier() const override; + const std::string& deleteRandStr() const override; /** * socket收到udp数据 @@ -174,6 +176,7 @@ protected: std::shared_ptr _ice_server; private: + mutable std::string _delete_rand_str; std::string _identifier; EventPoller::Ptr _poller; std::shared_ptr _dtls_transport; @@ -248,6 +251,7 @@ public: void createRtpChannel(const std::string &rid, uint32_t ssrc, MediaTrack &track); void removeTuple(RTC::TransportTuple* tuple); + void safeShutdown(const SockException &ex); protected: void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override; From ac46f5499e23e5a72fff46ac59f5fc4de0131068 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 22:44:57 +0800 Subject: [PATCH 06/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF:=20#2416?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MPEG.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Record/MPEG.h b/src/Record/MPEG.h index 4cb456db..1dd69d48 100644 --- a/src/Record/MPEG.h +++ b/src/Record/MPEG.h @@ -86,7 +86,7 @@ namespace mediakit { class MpegMuxer : public MediaSinkInterface { public: - MpegMuxer(bool is_ps) = default; + MpegMuxer(bool is_ps) {} ~MpegMuxer() override = default; bool addTrack(const Track::Ptr &track) override { return false; } void resetTracks() override {} From beb99ee5b5d43495eb80b8cc179c812cbdf8809b Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 22:45:17 +0800 Subject: [PATCH 07/66] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BD=9C=E8=80=85?= =?UTF-8?q?=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AUTHORS | 5 ++++- README.md | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 77f7ea9f..4b5e43fc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -70,4 +70,7 @@ WuPeng [ahaooahaz](https://github.com/AHAOAHA) [TempoTian](https://github.com/TempoTian) [Derek Liu](https://github.com/yjkhtddx) -[ljx0305](https://github.com/ljx0305) \ No newline at end of file +[ljx0305](https://github.com/ljx0305) +[朱如洪 ](https://github.com/zhu410289616) +[lijin](https://github.com/1461521844lijin) +[PioLing](https://github.com/PioLing) \ No newline at end of file diff --git a/README.md b/README.md index a7a13837..9843b217 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ - 支持es/ps/ts/ehome rtp推流 - 支持es/ps rtp转推 - 支持GB28181主动拉流模式 + - 支持双向语音对讲 - MP4点播与录制 - 支持录制为FLV/HLS/MP4 @@ -119,6 +120,7 @@ - 支持datachannel - 支持webrtc over tcp模式 - 优秀的nack、jitter buffer算法, 抗丢包能力卓越 + - 支持whip/whep协议 - [SRT支持](./srt/srt.md) - 其他 - 支持丰富的restful api以及web hook事件 @@ -302,6 +304,9 @@ bash build_docker_images.sh [TempoTian](https://github.com/TempoTian) [Derek Liu](https://github.com/yjkhtddx) [ljx0305](https://github.com/ljx0305) +[朱如洪 ](https://github.com/zhu410289616) +[lijin](https://github.com/1461521844lijin) +[PioLing](https://github.com/PioLing) ## 使用案例 From 19c53236331bdf0aab3a8fdfb12f457f571173ac Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 23:02:48 +0800 Subject: [PATCH 08/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8DstartSendRtp=E5=8F=91?= =?UTF-8?q?=E9=80=81mp4=E6=96=87=E4=BB=B6=E6=97=B6=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9A#2429?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MultiMediaSourceMuxer.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index 9c6f7a8a..daa68769 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -248,10 +248,11 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE auto ring = _ring; auto ssrc = args.ssrc; auto tracks = getTracks(false); - auto rtp_sender = std::make_shared(getOwnerPoller(sender)); + auto poller = getOwnerPoller(sender); + auto rtp_sender = std::make_shared(poller); weak_ptr weak_self = shared_from_this(); - rtp_sender->startSend(args, [ssrc, weak_self, rtp_sender, cb, tracks, ring](uint16_t local_port, const SockException &ex) mutable { + rtp_sender->startSend(args, [ssrc, weak_self, rtp_sender, cb, tracks, ring, poller](uint16_t local_port, const SockException &ex) mutable { cb(local_port, ex); auto strong_self = weak_self.lock(); if (!strong_self || ex) { @@ -273,7 +274,7 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE } }); - auto reader = ring->attach(EventPoller::getCurrentPoller()); + auto reader = ring->attach(poller); reader->setReadCB([rtp_sender](const Frame::Ptr &frame) { rtp_sender->inputFrame(frame); }); @@ -315,7 +316,7 @@ EventPoller::Ptr MultiMediaSourceMuxer::getOwnerPoller(MediaSource &sender) { try { auto ret = listener->getOwnerPoller(sender); if (ret != _poller) { - WarnL << "OwnerPoller changed:" << shortUrl(); + WarnL << "OwnerPoller changed " << _poller->getThreadName() << " -> " << ret->getThreadName() << " : " << shortUrl(); _poller = ret; } return ret; From 2708c85d577f348401f517e27b27fe43f64b3ea0 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 28 Apr 2023 23:08:59 +0800 Subject: [PATCH 09/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/IceServer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webrtc/IceServer.cpp b/webrtc/IceServer.cpp index 40e53aa8..d4530d10 100644 --- a/webrtc/IceServer.cpp +++ b/webrtc/IceServer.cpp @@ -520,7 +520,7 @@ namespace RTC return; this->selectedTuple = storedTuple; - this->lastSelectedTuple = static_pointer_cast(storedTuple->shared_from_this()); + this->lastSelectedTuple = std::static_pointer_cast(storedTuple->shared_from_this()); // Notify the listener. this->listener->OnIceServerSelectedTuple(this, this->selectedTuple); From 1dd0c69aa01e70984f962dbc82ec7de95b6e23f6 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Sat, 29 Apr 2023 09:02:19 +0800 Subject: [PATCH 10/66] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=8B=B1=E6=96=87?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3=20(#2431)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_en.md | 439 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 324 insertions(+), 115 deletions(-) diff --git a/README_en.md b/README_en.md index dd031c10..25980d01 100644 --- a/README_en.md +++ b/README_en.md @@ -1,138 +1,202 @@ ![logo](https://raw.githubusercontent.com/zlmediakit/ZLMediaKit/master/www/logo.png) -# A lightweight ,high performance and stable stream server and client framework based on C++11. +# An high-performance, enterprise-level streaming media service framework based on C++11. -[![license](http://img.shields.io/badge/license-MIT-green.svg)](https://github.com/xia-chu/ZLMediaKit/blob/master/LICENSE) -[![C++](https://img.shields.io/badge/language-c++-red.svg)](https://en.cppreference.com/) -[![platform](https://img.shields.io/badge/platform-linux%20|%20macos%20|%20windows-blue.svg)](https://github.com/xia-chu/ZLMediaKit) -[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-yellow.svg)](https://github.com/xia-chu/ZLMediaKit/pulls) -[![Build Status](https://travis-ci.org/xia-chu/ZLMediaKit.svg?branch=master)](https://travis-ci.org/xia-chu/ZLMediaKit) +[![](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/ZLMediaKit/ZLMediaKit/blob/master/LICENSE) +[![](https://img.shields.io/badge/language-c++-red.svg)](https://en.cppreference.com/) +[![](https://img.shields.io/badge/platform-linux%20|%20macos%20|%20windows-blue.svg)](https://github.com/ZLMediaKit/ZLMediaKit) +[![](https://img.shields.io/badge/PRs-welcome-yellow.svg)](https://github.com/ZLMediaKit/ZLMediaKit/pulls) -## Why ZLMediaKit? -- Developed based on C++ 11, the code is stable and reliable, avoiding the use of raw pointers, cross-platform porting is simple and convenient, and the code is clear and concise. -- Support rich streaming media protocols(`RTSP/RTMP/HLS/HTTP-FLV/WebSocket-flv/HTTP-TS/WebSocket-TS/HTTP-fMP4/Websocket-fMP4/MP4/WebRTC`),and support Inter-protocol conversion. -- Multiplexing asynchronous network IO based on epoll and multi thread,extreme performance. -- Well performance and stable test,can be used commercially. -- Support linux, macos, ios, android, Windows Platforms. -- Very low latency(lower then one second), video opened immediately. +[![](https://github.com/ZLMediaKit/ZLMediaKit/actions/workflows/android.yml/badge.svg)](https://github.com/ZLMediaKit/ZLMediaKit) +[![](https://github.com/ZLMediaKit/ZLMediaKit/actions/workflows/linux.yml/badge.svg)](https://github.com/ZLMediaKit/ZLMediaKit) +[![](https://github.com/ZLMediaKit/ZLMediaKit/actions/workflows/macos.yml/badge.svg)](https://github.com/ZLMediaKit/ZLMediaKit) +[![](https://github.com/ZLMediaKit/ZLMediaKit/actions/workflows/windows.yml/badge.svg)](https://github.com/ZLMediaKit/ZLMediaKit) -## Features +[![](https://github.com/ZLMediaKit/ZLMediaKit/actions/workflows/docker.yml/badge.svg)](https://hub.docker.com/r/zlmediakit/zlmediakit/tags) +[![](https://img.shields.io/docker/pulls/zlmediakit/zlmediakit)](https://hub.docker.com/r/zlmediakit/zlmediakit/tags) + +## Project Features +- Developed with C++11, avoiding the use of raw pointers, providing stable and reliable code with superior performance. +- Supports multiple protocols (RTSP/RTMP/HLS/HTTP-FLV/WebSocket-FLV/GB28181/HTTP-TS/WebSocket-TS/HTTP-fMP4/WebSocket-fMP4/MP4/WebRTC), and protocol conversion. +- Developed with multiplexing/multithreading/asynchronous network IO models, providing excellent concurrency performance and supporting massive client connections. +- The code has undergone extensive stability and performance testing, and has been extensively used in production environments. +- Supports all major platforms, including linux, macos, ios, android, and windows. +- Supports multiple instruction set platforms, such as x86, arm, risc-v, mips, Loongson, and Shenwei. +- Provides ultra-fast startup, extremely low latency (within 500 milliseconds, and can be as low as 100 milliseconds), and excellent user experience. +- Provides a comprehensive standard [C API](https://github.com/ZLMediaKit/ZLMediaKit/tree/master/api/include) that can be used as an SDK or called by other languages. +- Provides a complete [MediaServer](https://github.com/ZLMediaKit/ZLMediaKit/tree/master/server) server, which can be deployed directly as a commercial server without additional development. +- Provides a complete [restful api](https://github.com/ZLMediaKit/ZLMediaKit/wiki/MediaServer%E6%94%AF%E6%8C%81%E7%9A%84HTTP-API) and [web hook](https://github.com/ZLMediaKit/ZLMediaKit/wiki/MediaServer%E6%94%AF%E6%8C%81%E7%9A%84HTTP-HOOK-API), supporting rich business logic. +- Bridges the video surveillance protocol stack and the live streaming protocol stack, and provides comprehensive support for RTSP/RTMP. +- Fully supports H265/H264/AAC/G711/OPUS. +- Provides complete functions, including clustering, on-demand protocol conversion, on-demand push/pull streams, playback before publishing, and continuous publishing after disconnection. +- Provides ultimate performance, supporting 10W-level players on a single machine and 100Gb/s-level IO bandwidth capability. +- Provides ultimate user experience with [exclusive features](https://github.com/ZLMediaKit/ZLMediaKit/wiki/ZLMediakit%E7%8B%AC%E5%AE%B6%E7%89%B9%E6%80%A7%E4%BB%8B%E7%BB%8D). +- [Who is using zlmediakit?](https://github.com/ZLMediaKit/ZLMediaKit/issues/511) +- Fully supports IPv6 networks. + +## Project Positioning + +- Cross-platform streaming media solution for mobile and embedded systems. +- Commercial-grade streaming media server. +- Network programming secondary development SDK. + +## Feature List +### Overview of Features +Overview of Features - RTSP[S] - - RTSP[S] server,support rtsp push. - - RTSP[S] player and pusher. - - RTP Transport : `rtp over udp` `rtp over tcp` `rtp over http` `rtp udp multicast` . - - Basic/Digest/Url Authentication. - - H265/H264/AAC/G711/OPUS codec. - - Recorded as mp4. - - Vod of mp4. - + - RTSP[S] server, supports RTMP/MP4/HLS to RTSP[S] conversion, supports devices such as Amazon Echo Show + - RTSP[S] player, supports RTSP proxy, supports generating silent audio + - RTSP[S] push client and server + - Supports four RTP transmission modes: `rtp over udp` `rtp over tcp` `rtp over http` `rtp multicast` + - Server/client fully supports Basic/Digest authentication, asynchronous configurable authentication interface + - Supports H265 encoding + - The server supports RTSP pushing (including `rtp over udp` and `rtp over tcp`) + - Supports H264/H265/AAC/G711/OPUS/MJPEG encoding. Other encodings can be forwarded but cannot be converted to protocol + - RTMP[S] - - RTMP[S] server,support player and pusher. - - RTMP[S] player and pusher. - - Support HTTP-FLV/WebSocket-FLV sever. - - H265/H264/AAC/G711/OPUS codec. - - Recorded as flv or mp4. - - Vod of mp4. - - support [RTMP-H265](https://github.com/ksvc/FFmpeg/wiki) - + - RTMP[S] playback server, supports RTSP/MP4/HLS to RTMP conversion + - RTMP[S] publishing server, supports recording and publishing streams + - RTMP[S] player, supports RTMP proxy, supports generating silent audio + - RTMP[S] push client + - Supports http[s]-flv live streaming + - Supports websocket-flv live streaming + - Supports H264/H265/AAC/G711/OPUS encoding. Other encodings can be forwarded but cannot be converted to protocol + - Supports [RTMP-H265](https://github.com/ksvc/FFmpeg/wiki) + - Supports [RTMP-OPUS](https://github.com/ZLMediaKit/ZLMediaKit/wiki/RTMP%E5%AF%B9H265%E5%92%8COPUS%E7%9A%84%E6%94%AF%E6%8C%81) + - HLS - - RTSP RTMP can be converted into HLS,built-in HTTP server. - - Play authentication based on cookie. - - Support HLS player, support streaming HLS proxy to RTSP / RTMP / MP4. - + - Supports HLS file generation and comes with an HTTP file server + - Through cookie tracking technology, it can simulate HLS playback as a long connection, which can achieve HLS on-demand pulling, playback statistics, and other businesses + - Supports HLS player and can pull HLS to rtsp/rtmp/mp4 + - Supports H264/H265/AAC/G711/OPUS encoding + - TS - - Support HTTP-TS/WebSocket-TS sever. - + - Supports http[s]-ts live streaming + - Supports ws[s]-ts live streaming + - Supports H264/H265/AAC/G711/OPUS encoding + - fMP4 - - Support HTTP-fMP4/WebSocket-fMP4 sever. + - Supports http[s]-fmp4 live streaming + - Supports ws[s]-fmp4 live streaming + - Supports H264/H265/AAC/G711/OPUS/MJPEG encoding -- HTTP[S] - - HTTP server,suppor directory meun、RESTful http api. - - HTTP client,downloader,uploader,and http api requester. - - Cookie supported. - - WebSocket Server and Client. - - File access authentication. - -- WebRTC(experiential) - - Support webrtc push stream and transfer to other protocols - - Support webrtc play, support other protocol to webrtc - - Support simulcast - - Support rtx/nack - - Support transport-cc rtcp/rtp ext -- [SRT support](./srt/srt_en.md) +- HTTP[S] and WebSocket + - The server supports `directory index generation`, `file download`, `form submission requests` + - The client provides `file downloader (supports resume breakpoint)`, `interface requestor`, `file uploader` + - Complete HTTP API server, which can be used as a web backend development framework + - Supports cross-domain access + - Supports http client/server cookie + - Supports WebSocket server and client + - Supports http file access authentication + +- GB28181 and RTP Streaming + - Supports UDP/TCP RTP (PS/TS/ES) streaming server, which can be converted to RTSP/RTMP/HLS and other protocols + - Supports RTSP/RTMP/HLS and other protocol conversion to RTP streaming client, supports TCP/UDP mode, provides corresponding RESTful API, supports active and passive modes + - Supports H264/H265/AAC/G711/OPUS encoding + - Supports ES/PS/TS/EHOME RTP streaming + - Supports ES/PS RTP forwarding + - Supports GB28181 active pull mode + - Supports two-way voice intercom + +- MP4 VOD and Recording + - Supports recording as FLV/HLS/MP4 + - Supports MP4 file playback for RTSP/RTMP/HTTP-FLV/WS-FLV, supports seek + - Supports H264/H265/AAC/G711/OPUS encoding + +- WebRTC + - Supports WebRTC streaming and conversion to other protocols + - Supports WebRTC playback and conversion from other protocols to WebRTC + - Supports two-way echo testing + - Supports simulcast streaming + - Supports uplink and downlink RTX/NACK packet loss retransmission + - **Supports single-port, multi-threaded, and client network connection migration (unique in the open source community)**. + - Supports TWCC RTCP dynamic rate control + - Supports REMB/PLI/SR/RR RTCP + - Supports RTP extension parsing + - Supports GOP buffer and instant WebRTC playback + - Supports data channels + - Supports WebRTC over TCP mode + - Excellent NACK and jitter buffer algorithms with outstanding packet loss resistance + - Supports WHIP/WHEP protocols +- [SRT support](./srt/srt.md) - Others - - Support stream proxy by ffmpeg. - - RESTful http api and http hook event api. - - Config file hot loading. - - Vhost supported. - - Auto close stream when nobody played. - - Play and push authentication. - - Pull stream on Demand. - - Support TS / PS streaming push through RTP,and it can be converted to RTSP / RTMP / HLS / FLV. - - Support real-time online screenshot http api. - + - Supports rich RESTful APIs and webhook events + - Supports simple Telnet debugging + - Supports hot reloading of configuration files + - Supports traffic statistics, stream authentication, and other events + - Supports virtual hosts for isolating different domain names + - Supports on-demand streaming and automatic shutdown of streams with no viewers + - Supports pre-play before streaming to increase the rate of timely stream openings + - Provides a complete and powerful C API SDK + - Supports FFmpeg stream proxy for any format + - Supports HTTP API for real-time screenshot generation and return + - Supports on-demand demultiplexing and protocol conversion, reducing CPU usage by only enabling it when someone is watching + - Supports cluster deployment in traceable mode, with RTSP/RTMP/HLS/HTTP-TS support for traceable mode and HLS support for edge stations and multiple sources for source stations (using round-robin tracing) + - Can reconnect to streaming after abnormal disconnection in RTSP/RTMP/WebRTC pushing within a timeout period, with no impact on the player. + ## System Requirements -- Compiler support c++11,GCC4.8/Clang3.3/VC2015 or above. -- cmake3.1 or above. -- All Linux , both 32 and 64 bits -- Apple OSX(Darwin), both 32 and 64bits. -- All hardware with x86/x86_64/arm/mips cpu. +- Compiler with c++11 support, such as GCC 4.8+, Clang 3.3+, or VC2015+. +- CMake 3.1+. +- Linux (32-bit and 64-bit). +- Apple macOS (32-bit and 64-bit). +- Any hardware with x86, x86_64, ARM, or MIPS CPU. - Windows. ## How to build -It is recommended to compile on Ubuntu or MacOS,compiling on windows is cumbersome, and some features are not compiled by default. +It is recommended to compile on Ubuntu or macOS. Compiling on Windows is cumbersome, and some features are not compiled by default. -### Before build -- **You must use git to clone the complete code. Do not download the source code by downloading zip package. Otherwise, the sub-module code will not be downloaded by default.You can do it like this:** +### Before Building + +- **You must use Git to clone the complete code. Do not download the source code by downloading the ZIP package. Otherwise, the submodule code will not be downloaded by default. You can do it like this:** ``` git clone https://github.com/xia-chu/ZLMediaKit.git cd ZLMediaKit git submodule update --init ``` -### Build on linux +### Building on Linux -- My environment - - Ubuntu16.04 64 bit and gcc5.4 - - cmake 3.5.1 +- My Environment + - Ubuntu 16.04 (64-bit) with GCC 5.4. + - CMake 3.5.1. - Guidance ``` - # If it is on centos6.x, you need to install the newer version of GCC and cmake first, - # and then compile manually according to the script "build_for_linux.sh". - # If it is on a newer version of a system such as Ubuntu or Debain, + # If it is on CentOS 6.x, you need to install a newer version of GCC and CMake first, + # and then compile manually according to the "build_for_linux.sh" script. + # If it is on a newer version of a system such as Ubuntu or Debian, # step 4 can be manipulated directly. - # 1、Install GCC5.2 (this step can be skipped if the GCC version is higher than 4.7) + # 1. Install GCC 5.2 (this step can be skipped if the GCC version is higher than 4.7). sudo yum install centos-release-scl -y sudo yum install devtoolset-4-toolchain -y scl enable devtoolset-4 bash - # 2、Install cmake (this step can be skipped if the cmake version is higher than 3.1) - tar -xvf cmake-3.10.0-rc4.tar.gz #you need download cmake source file manually + # 2. Install CMake (this step can be skipped if the CMake version is higher than 3.1). + tar -xvf cmake-3.10.0-rc4.tar.gz #you need to download the CMake source file manually cd cmake-3.10.0-rc4 ./configure make -j4 sudo make install - # 3、Switch to high version GCC + # 3. Switch to a higher version of GCC. scl enable devtoolset-4 bash - # 4、build + # 4. Build. cd ZLMediaKit ./build_for_linux.sh ``` -### Build on macOS +### Building on macOS -- My environment - - macOS Sierra(10.12.1) + xcode8.3.1 - - Homebrew 1.1.3 - - cmake 3.8.0 +- My Environment + - macOS Sierra (10.12.1) with Xcode 8.3.1. + - Homebrew 1.1.3. + - CMake 3.8.0. - Guidance ``` @@ -140,7 +204,7 @@ git submodule update --init ./build_for_mac.sh ``` -### Build on iOS +### Building on iOS - You can generate Xcode projects and recompile them , [learn more](https://github.com/leetal/ios-cmake): ``` @@ -152,15 +216,16 @@ git submodule update --init ``` -### Build on Android +### Building on Android - Now you can open android sudio project in `Android` folder,this is a `aar library` and damo project. + Now you can open the Android Studio project in the `Android` folder. This is an `AAR` library and demo project. - My environment - - macOS Sierra(10.12.1) + xcode8.3.1 + - macOS Sierra (10.12.1) + Xcode 8.3.1 - Homebrew 1.1.3 - - cmake 3.8.0 - - [android-ndk-r14b](https://dl.google.com/android/repository/android-ndk-r14b-darwin-x86_64.zip) + - CMake 3.8.0 + - [Android NDK r14b](https://dl.google.com/android/repository/android-ndk-r14b-darwin-x86_64.zip) + - Guidance ``` @@ -168,23 +233,25 @@ git submodule update --init export ANDROID_NDK_ROOT=/path/to/ndk ./build_for_android.sh ``` -### Build on Windows + +### Building on Windows - My environment - - windows 10 - - visual studio 2017 - - [cmake-gui](https://cmake.org/files/v3.10/cmake-3.10.0-rc1-win32-x86.msi) + - Windows 10 + - Visual Studio 2017 + - [CMake GUI](https://cmake.org/files/v3.10/cmake-3.10.0-rc1-win32-x86.msi) - Guidance ``` -1 Enter the ZLMediaKit directory and execute git submodule update -- init downloads the code for ZLToolKit -2 Open the project with cmake-gui and generate the vs project file. -3 Find the project file (ZLMediaKit.sln), double-click to open it with vs2017. -4 Choose to compile Release version. Find the target file and run the test case. +1. Enter the ZLMediaKit directory and execute `git submodule update --init` to download the code for ZLToolKit. +2. Open the project with CMake GUI and generate the Visual Studio project file. +3. Find the project file (ZLMediaKit.sln), double-click to open it with VS2017. +4. Choose to compile the Release version. Find the target file and run the test cases. ``` + ## Usage -- As server: +- As a server: ```cpp TcpServer::Ptr rtspSrv(new TcpServer()); TcpServer::Ptr rtmpSrv(new TcpServer()); @@ -197,7 +264,7 @@ git submodule update --init httpsSrv->start(mINI::Instance()[Config::Http::kSSLPort]); ``` -- As player: +- As a player: ```cpp MediaPlayer::Ptr player(new MediaPlayer()); weak_ptr weakPlayer = player; @@ -210,7 +277,7 @@ git submodule update --init auto viedoTrack = strongPlayer->getTrack(TrackVideo); if (!viedoTrack) { - WarnL << "none video Track!"; + WarnL << "No video Track!"; return; } viedoTrack->addDelegate([](const Frame::Ptr &frame) { @@ -222,14 +289,13 @@ git submodule update --init ErrorL << "OnShutdown:" << ex.what(); }); - //rtp transport over tcp + //RTP transport over TCP (*player)[Client::kRtpType] = Rtsp::RTP_TCP; player->play("rtsp://admin:jzan123456@192.168.0.122/"); ``` -- As proxy server: +- As a proxy server: ```cpp - //support rtmp and rtsp url - //just support H264+AAC + //Support RTMP and RTSP URLs, but only H264 + AAC codec is supported auto urlList = {"rtmp://live.hkstv.hk.lxdns.com/live/hks", "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"}; map proxyMap; @@ -241,7 +307,7 @@ git submodule update --init } ``` -- As puser: +- As a pusher: ```cpp PlayerProxy::Ptr player(new PlayerProxy("app","stream")); player->play("rtmp://live.hkstv.hk.lxdns.com/live/hks"); @@ -255,19 +321,162 @@ git submodule update --init ``` ## Docker Image -You can pull a pre-built docker image from Docker Hub and run with + +You can download the pre-compiled image from Docker Hub and start it: + ```bash +#This image is pushed by the GitHub continuous integration automatic compilation to keep up with the latest code (master branch) docker run -id -p 1935:1935 -p 8080:80 -p 8443:443 -p 8554:554 -p 10000:10000 -p 10000:10000/udp -p 8000:8000/udp -p 9000:9000/udp zlmediakit/zlmediakit:master ``` -Dockerfile is also supplied to build images on Ubuntu 16.04 +You can also compile the image based on the Dockerfile: + ```bash -cd docker -docker build -t zlmediakit . +bash build_docker_images.sh ``` -## Contact - - Email:<1213642868@qq.com> - - QQ chat group:542509000 +## Collaborative Projects +- Visual management website + - [The latest web project with front-end and back-end separation, supporting webrtc playback](https://github.com/langmansh/AKStreamNVR) + - [Management web site based on ZLMediaKit master branch](https://gitee.com/kkkkk5G/MediaServerUI) + - [Management web site based on ZLMediaKit branch](https://github.com/chenxiaolei/ZLMediaKit_NVR_UI) + - [A very beautiful visual background management system](https://github.com/MingZhuLiu/ZLMediaServerManagent) + +- Media management platform + - [GB28181 complete solution with web management website, supporting webrtc and h265 playback](https://github.com/648540858/wvp-GB28181-pro) + - [Powerful media control and management interface platform, supporting GB28181](https://github.com/chatop2020/AKStream) + - [GB28181 server implemented in Go](https://github.com/panjjo/gosip) + - [Node-js version of GB28181 platform](https://gitee.com/hfwudao/GB28181_Node_Http) + - [Hikvision ehome server implemented in Go](https://github.com/tsingeye/FreeEhome) +- Client + - [Complete C# wrapper library for c sdk](https://github.com/malegend/ZLMediaKit.Autogen) + - [Push client implemented based on C SDK](https://github.com/hctym1995/ZLM_ApiDemo) + - [Http API and Hook in C#](https://github.com/chengxiaosheng/ZLMediaKit.HttpApi) + - [RESTful client in DotNetCore](https://github.com/MingZhuLiu/ZLMediaKit.DotNetCore.Sdk) + +- Player + - [Player supporting H265 based on wasm](https://github.com/numberwolf/h265web.js) + - [WebSocket-fmp4 player based on MSE](https://github.com/v354412101/wsPlayer) + - [Domestic webrtc sdk(metaRTC)](https://github.com/metartc/metaRTC) + +## License + +The self-owned code of this project is licensed under the permissive MIT License and can be freely applied to commercial and non-commercial projects while retaining copyright information. +However, this project also uses some scattered [open source code](https://github.com/ZLMediaKit/ZLMediaKit/wiki/%E4%BB%A3%E7%A0%81%E4%BE%9D%E8%B5%96%E4%B8%8E%E7%89%88%E6%9D%83%E5%A3%B0%E6%98%8E) , please replace or remove it for commercial use. +Any commercial disputes or infringement caused by using this project have nothing to do with the project and developers and shall be at your own legal risk. +When using the code of this project, the license agreement should also indicate the license of the third-party libraries that this project depends on. + +## Contact Information + +- Email: <1213642868@qq.com> (For project-related or streaming media-related questions, please follow the issue process. Otherwise, we will not reply to emails.) +- QQ groups: Both QQ groups with a total of 4000 members are full. We will not create new QQ groups in the future. Users can join the [Knowledge Planet](https://github.com/ZLMediaKit/ZLMediaKit/issues/2364) to ask questions and support this project. +- Follow WeChat Official Account: + + +## How to Ask Questions? + +If you have any questions about the project, we recommend that you: + +- 1. Carefully read the readme and wiki. If necessary, you can also check the issues. +- 2. If your question has not been resolved, you can raise an issue. +- 3. Some questions may not be suitable for issues, but can be raised in QQ groups. +- 4. We generally do not accept free technical consulting and support via QQ private chat. ([Why we don't encourage QQ private chat](https://github.com/ZLMediaKit/ZLMediaKit/wiki/%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E5%BB%BA%E8%AE%AEQQ%E7%A7%81%E8%81%8A%E5%92%A8%E8%AF%A2%E9%97%AE%E9%A2%98%EF%BC%9F)). +- 5. If you need more timely and thoughtful technical support, you can join the [Knowledge Planet](https://github.com/ZLMediaKit/ZLMediaKit/issues/2364) for a fee. + +## Special Thanks + +This project uses the [media-server](https://github.com/ireader/media-server) library developed by [Lao Chen](https://github.com/ireader). The reuse and de-multiplexing of ts/fmp4/mp4/ps container formats in this project depend on the media-server library. Lao Chen has provided invaluable help and support multiple times in implementing many functions of this project, and we would like to express our sincere gratitude to him! + +## Acknowledgments + +Thanks to all those who have supported this project in various ways, including but not limited to code contributions, problem feedback, and donations. The following list is not in any particular order: + +[老陈](https://github.com/ireader) +[Gemfield](https://github.com/gemfield) +[南冠彤](https://github.com/nanguantong2) +[凹凸慢](https://github.com/tsingeye) +[chenxiaolei](https://github.com/chenxiaolei) +[史前小虫](https://github.com/zqsong) +[清涩绿茶](https://github.com/baiyfcu) +[3503207480](https://github.com/3503207480) +[DroidChow](https://github.com/DroidChow) +[阿塞](https://github.com/HuoQiShuai) +[火宣](https://github.com/ChinaCCF) +[γ瑞γミ](https://github.com/JerryLinGd) +[linkingvision](https://www.linkingvision.com/) +[茄子](https://github.com/taotaobujue2008) +[好心情](mailto:409257224@qq.com) +[浮沉](https://github.com/MingZhuLiu) +[Xiaofeng Wang](https://github.com/wasphin) +[doodoocoder](https://github.com/doodoocoder) +[qingci](https://github.com/Colibrow) +[swwheihei](https://github.com/swwheihei) +[KKKKK5G](https://gitee.com/kkkkk5G) +[Zhou Weimin](mailto:zhouweimin@supremind.com) +[Jim Jin](https://github.com/jim-king-2000) +[西瓜丶](mailto:392293307@qq.com) +[MingZhuLiu](https://github.com/MingZhuLiu) +[chengxiaosheng](https://github.com/chengxiaosheng) +[big panda](mailto:2381267071@qq.com) +[tanningzhong](https://github.com/tanningzhong) +[hctym1995](https://github.com/hctym1995) +[hewenyuan](https://gitee.com/kingyuanyuan) +[sunhui](mailto:sunhui200475@163.com) +[mirs](mailto:fangpengcheng@bilibili.com) +[Kevin Cheng](mailto:kevin__cheng@outlook.com) +[Liu Jiang](mailto:root@oopy.org) +[along](https://github.com/alongl) +[qingci](mailto:xpy66swsry@gmail.com) +[lyg1949](mailto:zh.ghlong@qq.com) +[zhlong](mailto:zh.ghlong@qq.com) +[大裤衩](mailto:3503207480@qq.com) +[droid.chow](mailto:droid.chow@gmail.com) +[陈晓林](https://github.com/musicwood) +[CharleyWangHZ](https://github.com/CharleyWangHZ) +[Johnny](https://github.com/johzzy) +[DoubleX69](https://github.com/DoubleX69) +[lawrencehj](https://github.com/lawrencehj) +[yangkun](mailto:xyyangkun@163.com) +[Xinghua Zhao](mailto:holychaossword@hotmail.com) +[hejilin](https://github.com/brokensword2018) +[rqb500](https://github.com/rqb500) +[Alex](https://github.com/alexliyu7352) +[Dw9](https://github.com/Dw9) +[明月惊鹊](mailto:mingyuejingque@gmail.com) +[cgm](mailto:2958580318@qq.com) +[hejilin](mailto:1724010622@qq.com) +[alexliyu7352](mailto:liyu7352@gmail.com) +[cgm](mailto:2958580318@qq.com) +[haorui wang](https://github.com/HaoruiWang) +[joshuafc](mailto:joshuafc@foxmail.com) +[JayChen0519](https://github.com/JayChen0519) +[zx](mailto:zuoxue@qq.com) +[wangcker](mailto:wangcker@163.com) +[WuPeng](mailto:wp@zafu.edu.cn) +[starry](https://github.com/starry) +[mtdxc](https://github.com/mtdxc) +[胡刚风](https://github.com/hugangfeng333) +[zhao85](https://github.com/zhao85) +[dreamisdream](https://github.com/dreamisdream) +[dingcan](https://github.com/dcan123) +[Haibo Chen](https://github.com/duiniuluantanqin) +[Leon](https://gitee.com/leon14631) +[custompal](https://github.com/custompal) +[PioLing](https://github.com/PioLing) +[KevinZang](https://github.com/ZSC714725) +[gongluck](https://github.com/gongluck) +[a-ucontrol](https://github.com/a-ucontrol) +[TalusL](https://github.com/TalusL) +[ahaooahaz](https://github.com/AHAOAHA) +[TempoTian](https://github.com/TempoTian) +[Derek Liu](https://github.com/yjkhtddx) +[ljx0305](https://github.com/ljx0305) +[朱如洪 ](https://github.com/zhu410289616) +[lijin](https://github.com/1461521844lijin) +[PioLing](https://github.com/PioLing) + +## Use Cases + +This project has gained recognition from many companies and individual developers. According to the author's incomplete statistics, companies using this project include well-known Internet giants, leading cloud service companies in China, several well-known AI unicorn companies, as well as a series of small and medium-sized companies. Users can endorse this project by pasting their company name and relevant project information on the [issue page](https://github.com/ZLMediaKit/ZLMediaKit/issues/511). Thank you for your support! From bfec1b1e0e14dd7332aa5357caa39e9ba7241418 Mon Sep 17 00:00:00 2001 From: Johnny Date: Tue, 2 May 2023 20:40:40 +0800 Subject: [PATCH 11/66] fix typo --- src/Rtcp/RtcpContext.cpp | 6 +++--- src/Rtcp/RtcpContext.h | 4 ++-- src/Rtp/RtpProcess.cpp | 2 +- webrtc/WebRtcTransport.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Rtcp/RtcpContext.cpp b/src/Rtcp/RtcpContext.cpp index 796d3303..6d4469b4 100644 --- a/src/Rtcp/RtcpContext.cpp +++ b/src/Rtcp/RtcpContext.cpp @@ -34,7 +34,7 @@ size_t RtcpContext::getLost() { throw std::runtime_error("没有实现, rtp发送者无法统计丢包率"); } -size_t RtcpContext::geLostInterval() { +size_t RtcpContext::getLostInterval() { throw std::runtime_error("没有实现, rtp发送者无法统计丢包率"); } @@ -231,7 +231,7 @@ size_t RtcpContextForRecv::getLost() { return getExpectedPackets() - _packets; } -size_t RtcpContextForRecv::geLostInterval() { +size_t RtcpContextForRecv::getLostInterval() { auto lost = getLost(); auto ret = lost - _last_lost; _last_lost = lost; @@ -248,7 +248,7 @@ Buffer::Ptr RtcpContextForRecv::createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ss uint8_t fraction = 0; auto expected_interval = getExpectedPacketsInterval(); if (expected_interval) { - fraction = uint8_t(geLostInterval() << 8 / expected_interval); + fraction = uint8_t(getLostInterval() << 8 / expected_interval); } item->fraction = fraction; diff --git a/src/Rtcp/RtcpContext.h b/src/Rtcp/RtcpContext.h index 31b6167d..79920311 100644 --- a/src/Rtcp/RtcpContext.h +++ b/src/Rtcp/RtcpContext.h @@ -78,7 +78,7 @@ public: /** * 上次结果与本次结果间丢包个数 */ - virtual size_t geLostInterval(); + virtual size_t getLostInterval(); protected: // 收到或发送的rtp的字节数 @@ -120,7 +120,7 @@ public: size_t getExpectedPackets() const override; size_t getExpectedPacketsInterval() override; size_t getLost() override; - size_t geLostInterval() override; + size_t getLostInterval() override; void onRtcp(RtcpHeader *rtcp) override; private: diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index fac1bd2b..8b6bcb3d 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -299,7 +299,7 @@ float RtpProcess::getLossRate(MediaSource &sender, TrackType type) { if (!expected) { return -1; } - return geLostInterval() * 100 / expected; + return getLostInterval() * 100 / expected; } }//namespace mediakit diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 7dc9e816..fe8c6e5c 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -696,7 +696,7 @@ public: if (!expected) { return -1; } - return _rtcp_context.geLostInterval() * 100 / expected; + return _rtcp_context.getLostInterval() * 100 / expected; } private: From 7c89c1655f325250cd8aaf3df2a49122d4ae2c26 Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Wed, 3 May 2023 18:52:11 +0800 Subject: [PATCH 12/66] add getProxyPusherInfo and getProxyInfo restful api --- postman/ZLMediaKit.postman_collection.json | 72 +++++++++++++++++++--- server/WebApi.cpp | 42 +++++++++++++ src/Player/PlayerProxy.cpp | 34 +++++++++- src/Player/PlayerProxy.h | 12 ++++ src/Pusher/PusherProxy.cpp | 30 +++++++++ src/Pusher/PusherProxy.h | 9 +++ 6 files changed, 191 insertions(+), 8 deletions(-) diff --git a/postman/ZLMediaKit.postman_collection.json b/postman/ZLMediaKit.postman_collection.json index ff4305ce..044461e0 100644 --- a/postman/ZLMediaKit.postman_collection.json +++ b/postman/ZLMediaKit.postman_collection.json @@ -1,6 +1,6 @@ { "info": { - "_postman_id": "4626d766-16b5-4255-89ba-f7614de2398c", + "_postman_id": "39e8a1df-cc8e-4e3f-bf5e-197c86e7bf0f", "name": "ZLMediaKit", "description": "媒体服务器", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" @@ -627,7 +627,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{ZLMediaKit_URL}}/index/api/addStreamPusherProxy?secret={{ZLMediaKit_secret}}&schema=rtmp&vhost={{defaultVhost}}&app=live&stream=test&dst_url=rtmp://127.0.0.1/live/push", + "raw": "{{ZLMediaKit_URL}}/index/api/addStreamPusherProxy?secret={{ZLMediaKit_secret}}&schema=rtmp&vhost={{defaultVhost}}&app=live&stream=test&dst_url=rtmp://192.168.1.104/live/push", "host": [ "{{ZLMediaKit_URL}}" ], @@ -664,7 +664,7 @@ }, { "key": "dst_url", - "value": "rtmp://127.0.0.1/live/push", + "value": "rtmp://192.168.1.104/live/push", "description": "推流地址,需要与schema字段协议一致" }, { @@ -696,7 +696,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{ZLMediaKit_URL}}/index/api/delStreamPusherProxy?secret={{ZLMediaKit_secret}}&key=__defaultVhost__/live/test", + "raw": "{{ZLMediaKit_URL}}/index/api/delStreamPusherProxy?secret={{ZLMediaKit_secret}}&key=rtmp/__defaultVhost__/live/test/f40a8ab006cac16ecc0858409e890491", "host": [ "{{ZLMediaKit_URL}}" ], @@ -713,7 +713,7 @@ }, { "key": "key", - "value": "__defaultVhost__/live/test", + "value": "rtmp/__defaultVhost__/live/test/f40a8ab006cac16ecc0858409e890491", "description": "addStreamPusherProxy接口返回的key" } ] @@ -1363,7 +1363,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{ZLMediaKit_URL}}/index/api/openRtpServer?secret={{ZLMediaKit_secret}}&port=0&enable_tcp=1&stream_id=test", + "raw": "{{ZLMediaKit_URL}}/index/api/openRtpServer?secret={{ZLMediaKit_secret}}&port=0&tcp_mode=1&stream_id=test", "host": [ "{{ZLMediaKit_URL}}" ], @@ -1422,7 +1422,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{ZLMediaKit_URL}}/index/api/connectRtpServer?secret={{ZLMediaKit_secret}}&dst_url=127.0.0.1&dst_port=10000&stream_id=test", + "raw": "{{ZLMediaKit_URL}}/index/api/connectRtpServer?secret={{ZLMediaKit_secret}}&dst_url=0&dst_port=1&stream_id=test", "host": [ "{{ZLMediaKit_URL}}" ], @@ -1874,6 +1874,64 @@ } }, "response": [] + }, + { + "name": "获取拉流代理信息(getProxyInfo)", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{ZLMediaKit_URL}}/index/api/getProxyInfo?secret={{ZLMediaKit_secret}}&key=__defaultVhost__/live/test", + "host": [ + "{{ZLMediaKit_URL}}" + ], + "path": [ + "index", + "api", + "getProxyInfo" + ], + "query": [ + { + "key": "secret", + "value": "{{ZLMediaKit_secret}}" + }, + { + "key": "key", + "value": "__defaultVhost__/live/test" + } + ] + } + }, + "response": [] + }, + { + "name": "获取推流代理信息(getProxyPusherInfo)", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{ZLMediaKit_URL}}/index/api/getProxyPusherInfo?secret={{ZLMediaKit_secret}}&key=rtmp/__defaultVhost__/live/test/f40a8ab006cac16ecc0858409e890491", + "host": [ + "{{ZLMediaKit_URL}}" + ], + "path": [ + "index", + "api", + "getProxyPusherInfo" + ], + "query": [ + { + "key": "secret", + "value": "{{ZLMediaKit_secret}}" + }, + { + "key": "key", + "value": "rtmp/__defaultVhost__/live/test/f40a8ab006cac16ecc0858409e890491" + } + ] + } + }, + "response": [] } ], "event": [ diff --git a/server/WebApi.cpp b/server/WebApi.cpp index bc170cd0..6a1c47e5 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1420,6 +1420,48 @@ void installWebApi() { }); }); + api_regist("/index/api/getProxyPusherInfo", [](API_ARGS_MAP_ASYNC) { + CHECK_SECRET(); + CHECK_ARGS("key"); + decltype(s_proxyPusherMap.end()) it; + { + lock_guard lck(s_proxyPusherMapMtx); + it = s_proxyPusherMap.find(allArgs["key"]); + } + + if (it == s_proxyPusherMap.end()) { + throw ApiRetException("can not find pusher", API::NotFound); + } + + auto pusher = it->second; + + val["data"]["status"] = pusher->getStatus(); + val["data"]["liveSecs"] = pusher->getLiveSecs(); + val["data"]["rePublishCount"] = pusher->getRePublishCount(); + invoker(200, headerOut, val.toStyledString()); + }); + + api_regist("/index/api/getProxyInfo", [](API_ARGS_MAP_ASYNC) { + CHECK_SECRET(); + CHECK_ARGS("key"); + decltype(s_proxyMap.end()) it; + { + lock_guard lck(s_proxyMapMtx); + it = s_proxyMap.find(allArgs["key"]); + } + + if (it == s_proxyMap.end()) { + throw ApiRetException("can not find the proxy", API::NotFound); + } + + auto proxy = it->second; + + val["data"]["status"] = proxy->getStatus(); + val["data"]["liveSecs"] = proxy->getLiveSecs(); + val["data"]["rePullCount"] = proxy->getRePullCount(); + invoker(200, headerOut, val.toStyledString()); + }); + // 删除录像文件夹 // http://127.0.0.1/index/api/deleteRecordDirectroy?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01-01 api_regist("/index/api/deleteRecordDirectory", [](API_ARGS_MAP) { diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 521c38e8..ec865788 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -30,6 +30,10 @@ PlayerProxy::PlayerProxy(const string &vhost, const string &app, const string &s _app = app; _stream_id = stream_id; _retry_count = retry_count; + + _live_secs = 0; + _live_status = 1; + _repull_count = 0; _on_close = [](const SockException &) {}; (*this)[Client::kWaitTrackReady] = false; } @@ -58,10 +62,14 @@ void PlayerProxy::play(const string &strUrlTmp) { if (!err) { // 取消定时器,避免hls拉流索引文件因为网络波动失败重连成功后出现循环重试的情况 - strongSelf->_timer.reset(); + strongSelf->_timer.reset(); + strongSelf->_live_ticker.resetTime(); + strongSelf->_live_status = 0; // 播放成功 *piFailedCnt = 0;//连续播放失败次数清0 strongSelf->onPlaySuccess(); + + InfoL << "play " << strUrlTmp << " success"; } else if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) { // 播放失败,延时重试播放 strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++); @@ -92,8 +100,17 @@ void PlayerProxy::play(const string &strUrlTmp) { strongSelf->_muxer->resetTracks(); } } + + if(*piFailedCnt == 0){ + // 第一次重拉更新时长 + strongSelf->_live_secs += strongSelf->_live_ticker.elapsedTime()/1000; + strongSelf->_live_ticker.resetTime(); + TraceL<<" live secs "<_live_secs; + } + //播放异常中断,延时重试播放 if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) { + strongSelf->_repull_count++; strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++); } else { //达到了最大重试次数,回调关闭 @@ -241,4 +258,19 @@ void PlayerProxy::onPlaySuccess() { } } +int PlayerProxy::getStatus() { + return _live_status.load(); +} +uint64_t PlayerProxy::getLiveSecs() { + if(_live_status == 0){ + return _live_secs + _live_ticker.elapsedTime()/1000; + }else{ + return _live_secs; + } +} + +uint64_t PlayerProxy::getRePullCount(){ + return _repull_count; +} + } /* namespace mediakit */ diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 02cba77b..5ab23481 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -52,6 +52,11 @@ public: */ int totalReaderCount() ; + + int getStatus(); + uint64_t getLiveSecs(); + uint64_t getRePullCount(); + private: //MediaSourceEvent override bool close(MediaSource &sender) override; @@ -76,6 +81,13 @@ private: std::function _on_close; std::function _on_play; MultiMediaSourceMuxer::Ptr _muxer; + + toolkit::Ticker _live_ticker; + //0 表示正常 1 表示正在尝试拉流 + std::atomic _live_status; + std::atomic _live_secs; + + std::atomic _repull_count; }; } /* namespace mediakit */ diff --git a/src/Pusher/PusherProxy.cpp b/src/Pusher/PusherProxy.cpp index 22a658e7..b936842a 100644 --- a/src/Pusher/PusherProxy.cpp +++ b/src/Pusher/PusherProxy.cpp @@ -20,6 +20,9 @@ PusherProxy::PusherProxy(const MediaSource::Ptr &src, int retry_count, const Eve _retry_count = retry_count; _on_close = [](const SockException &) {}; _weak_src = src; + _live_secs = 0; + _live_status = 1; + _republish_count = 0; } PusherProxy::~PusherProxy() { @@ -52,10 +55,14 @@ void PusherProxy::publish(const string &dst_url) { auto src = strong_self->_weak_src.lock(); if (!err) { // 推流成功 + strong_self->_live_ticker.resetTime(); + strong_self->_live_status = 0; *failed_cnt = 0; InfoL << "Publish " << dst_url << " success"; } else if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) { // 推流失败,延时重试推送 + strong_self->_republish_count++; + strong_self->_live_status = 1; strong_self->rePublish(dst_url, (*failed_cnt)++); } else { //如果媒体源已经注销, 或达到了最大重试次数,回调关闭 @@ -69,9 +76,17 @@ void PusherProxy::publish(const string &dst_url) { return; } + if(*failed_cnt == 0){ + // 第一次重推更新时长 + strong_self->_live_secs += strong_self->_live_ticker.elapsedTime()/1000; + strong_self->_live_ticker.resetTime(); + TraceL<<" live secs "<_live_secs; + } + auto src = strong_self->_weak_src.lock(); //推流异常中断,延时重试播放 if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) { + strong_self->_republish_count++; strong_self->rePublish(dst_url, (*failed_cnt)++); } else { //如果媒体源已经注销, 或达到了最大重试次数,回调关闭 @@ -97,4 +112,19 @@ void PusherProxy::rePublish(const string &dst_url, int failed_cnt) { }, getPoller()); } +int PusherProxy::getStatus() { + return _live_status.load(); +} +uint64_t PusherProxy::getLiveSecs() { + if(_live_status == 0){ + return _live_secs + _live_ticker.elapsedTime()/1000; + }else{ + return _live_secs; + } +} + +uint64_t PusherProxy::getRePublishCount(){ + return _republish_count; +} + } /* namespace mediakit */ diff --git a/src/Pusher/PusherProxy.h b/src/Pusher/PusherProxy.h index bee32d63..baeef029 100644 --- a/src/Pusher/PusherProxy.h +++ b/src/Pusher/PusherProxy.h @@ -43,6 +43,10 @@ public: */ void publish(const std::string& dstUrl) override; + int getStatus(); + uint64_t getLiveSecs(); + uint64_t getRePublishCount(); + private: // 重推逻辑函数 void rePublish(const std::string &dstUrl, int iFailedCnt); @@ -50,6 +54,11 @@ private: private: int _retry_count; toolkit::Timer::Ptr _timer; + toolkit::Ticker _live_ticker; + //0 表示正常 1 表示正在尝试推流 + std::atomic _live_status; + std::atomic _live_secs; + std::atomic _republish_count; std::weak_ptr _weak_src; std::function _on_close; std::function _on_publish; From ffed4b3bb2b880eb5a7a70c65a9282e3cf059c4c Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Wed, 3 May 2023 21:46:25 +0800 Subject: [PATCH 13/66] format code style for pusher and player --- src/Player/PlayerProxy.cpp | 93 ++++++++++++++++++++------------------ src/Player/PlayerProxy.h | 27 ++++++----- src/Pusher/PusherProxy.cpp | 43 ++++++++++-------- src/Pusher/PusherProxy.h | 12 +++-- 4 files changed, 94 insertions(+), 81 deletions(-) diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index ec865788..b2b567ff 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -8,29 +8,31 @@ * may be found in the AUTHORS file in the root of the source tree. */ -#include "Common/config.h" #include "PlayerProxy.h" -#include "Util/mini.h" -#include "Util/MD5.h" -#include "Util/logger.h" +#include "Common/config.h" #include "Extension/AAC.h" #include "Rtmp/RtmpMediaSource.h" -#include "Rtsp/RtspMediaSource.h" #include "Rtmp/RtmpPlayer.h" +#include "Rtsp/RtspMediaSource.h" #include "Rtsp/RtspPlayer.h" +#include "Util/MD5.h" +#include "Util/logger.h" +#include "Util/mini.h" using namespace toolkit; using namespace std; namespace mediakit { -PlayerProxy::PlayerProxy(const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option, - int retry_count, const EventPoller::Ptr &poller) : MediaPlayer(poller) , _option(option) { +PlayerProxy::PlayerProxy( + const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option, int retry_count, const EventPoller::Ptr &poller) + : MediaPlayer(poller) + , _option(option) { _vhost = vhost; _app = app; _stream_id = stream_id; _retry_count = retry_count; - + _live_secs = 0; _live_status = 1; _repull_count = 0; @@ -48,7 +50,7 @@ void PlayerProxy::setOnClose(const function &cb) void PlayerProxy::play(const string &strUrlTmp) { weak_ptr weakSelf = shared_from_this(); - std::shared_ptr piFailedCnt(new int(0)); //连续播放失败次数 + std::shared_ptr piFailedCnt(new int(0)); // 连续播放失败次数 setOnPlayResult([weakSelf, strUrlTmp, piFailedCnt](const SockException &err) { auto strongSelf = weakSelf.lock(); if (!strongSelf) { @@ -66,7 +68,7 @@ void PlayerProxy::play(const string &strUrlTmp) { strongSelf->_live_ticker.resetTime(); strongSelf->_live_status = 0; // 播放成功 - *piFailedCnt = 0;//连续播放失败次数清0 + *piFailedCnt = 0; // 连续播放失败次数清0 strongSelf->onPlaySuccess(); InfoL << "play " << strUrlTmp << " success"; @@ -74,7 +76,7 @@ void PlayerProxy::play(const string &strUrlTmp) { // 播放失败,延时重试播放 strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++); } else { - //达到了最大重试次数,回调关闭 + // 达到了最大重试次数,回调关闭 strongSelf->_on_close(err); } }); @@ -84,7 +86,7 @@ void PlayerProxy::play(const string &strUrlTmp) { return; } - //注销直接拉流代理产生的流:#532 + // 注销直接拉流代理产生的流:#532 strongSelf->setMediaSource(nullptr); if (strongSelf->_muxer) { @@ -101,19 +103,19 @@ void PlayerProxy::play(const string &strUrlTmp) { } } - if(*piFailedCnt == 0){ + if (*piFailedCnt == 0) { // 第一次重拉更新时长 - strongSelf->_live_secs += strongSelf->_live_ticker.elapsedTime()/1000; + strongSelf->_live_secs += strongSelf->_live_ticker.elapsedTime() / 1000; strongSelf->_live_ticker.resetTime(); - TraceL<<" live secs "<_live_secs; + TraceL << " live secs " << strongSelf->_live_secs; } - //播放异常中断,延时重试播放 + // 播放异常中断,延时重试播放 if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) { strongSelf->_repull_count++; strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++); } else { - //达到了最大重试次数,回调关闭 + // 达到了最大重试次数,回调关闭 strongSelf->_on_close(err); } }); @@ -131,13 +133,13 @@ void PlayerProxy::play(const string &strUrlTmp) { void PlayerProxy::setDirectProxy() { MediaSource::Ptr mediaSource; if (dynamic_pointer_cast(_delegate)) { - //rtsp拉流 + // rtsp拉流 GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy); if (directProxy) { mediaSource = std::make_shared(_vhost, _app, _stream_id); } } else if (dynamic_pointer_cast(_delegate)) { - //rtmp拉流,rtmp强制直接代理 + // rtmp拉流,rtmp强制直接代理 mediaSource = std::make_shared(_vhost, _app, _stream_id); } if (mediaSource) { @@ -148,7 +150,7 @@ void PlayerProxy::setDirectProxy() { PlayerProxy::~PlayerProxy() { _timer.reset(); // 避免析构时, 忘记回调api请求 - if(_on_play) { + if (_on_play) { _on_play(SockException(Err_shutdown, "player proxy close")); _on_play = nullptr; } @@ -157,21 +159,24 @@ PlayerProxy::~PlayerProxy() { void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) { auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000, 60 * 1000)); weak_ptr weakSelf = shared_from_this(); - _timer = std::make_shared(iDelay / 1000.0f, [weakSelf, strUrl, iFailedCnt]() { - //播放失败次数越多,则延时越长 - auto strongPlayer = weakSelf.lock(); - if (!strongPlayer) { + _timer = std::make_shared( + iDelay / 1000.0f, + [weakSelf, strUrl, iFailedCnt]() { + // 播放失败次数越多,则延时越长 + auto strongPlayer = weakSelf.lock(); + if (!strongPlayer) { + return false; + } + WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl; + strongPlayer->MediaPlayer::play(strUrl); + strongPlayer->setDirectProxy(); return false; - } - WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl; - strongPlayer->MediaPlayer::play(strUrl); - strongPlayer->setDirectProxy(); - return false; - }, getPoller()); + }, + getPoller()); } bool PlayerProxy::close(MediaSource &sender) { - //通知其停止推流 + // 通知其停止推流 weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); getPoller()->async_first([weakSelf]() { auto strongSelf = weakSelf.lock(); @@ -214,19 +219,19 @@ float PlayerProxy::getLossRate(MediaSource &sender, TrackType type) { void PlayerProxy::onPlaySuccess() { GET_CONFIG(bool, reset_when_replay, General::kResetWhenRePlay); if (dynamic_pointer_cast(_media_src)) { - //rtsp拉流代理 + // rtsp拉流代理 if (reset_when_replay || !_muxer) { _option.enable_rtsp = false; _muxer = std::make_shared(_vhost, _app, _stream_id, getDuration(), _option); } } else if (dynamic_pointer_cast(_media_src)) { - //rtmp拉流代理 + // rtmp拉流代理 if (reset_when_replay || !_muxer) { _option.enable_rtmp = false; _muxer = std::make_shared(_vhost, _app, _stream_id, getDuration(), _option); } } else { - //其他拉流代理 + // 其他拉流代理 if (reset_when_replay || !_muxer) { _muxer = std::make_shared(_vhost, _app, _stream_id, getDuration(), _option); } @@ -235,25 +240,25 @@ void PlayerProxy::onPlaySuccess() { auto videoTrack = getTrack(TrackVideo, false); if (videoTrack) { - //添加视频 + // 添加视频 _muxer->addTrack(videoTrack); - //视频数据写入_mediaMuxer + // 视频数据写入_mediaMuxer videoTrack->addDelegate(_muxer); } auto audioTrack = getTrack(TrackAudio, false); if (audioTrack) { - //添加音频 + // 添加音频 _muxer->addTrack(audioTrack); - //音频数据写入_mediaMuxer + // 音频数据写入_mediaMuxer audioTrack->addDelegate(_muxer); } - //添加完毕所有track,防止单track情况下最大等待3秒 + // 添加完毕所有track,防止单track情况下最大等待3秒 _muxer->addTrackCompleted(); if (_media_src) { - //让_muxer对象拦截一部分事件(比如说录像相关事件) + // 让_muxer对象拦截一部分事件(比如说录像相关事件) _media_src->setListener(_muxer); } } @@ -262,14 +267,14 @@ int PlayerProxy::getStatus() { return _live_status.load(); } uint64_t PlayerProxy::getLiveSecs() { - if(_live_status == 0){ - return _live_secs + _live_ticker.elapsedTime()/1000; - }else{ + if (_live_status == 0) { + return _live_secs + _live_ticker.elapsedTime() / 1000; + } else { return _live_secs; } } -uint64_t PlayerProxy::getRePullCount(){ +uint64_t PlayerProxy::getRePullCount() { return _repull_count; } diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 5ab23481..d828f696 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -11,21 +11,25 @@ #ifndef SRC_DEVICE_PLAYERPROXY_H_ #define SRC_DEVICE_PLAYERPROXY_H_ -#include #include "Common/MultiMediaSourceMuxer.h" #include "Player/MediaPlayer.h" #include "Util/TimeTicker.h" +#include namespace mediakit { -class PlayerProxy : public MediaPlayer, public MediaSourceEvent, public std::enable_shared_from_this { +class PlayerProxy + : public MediaPlayer + , public MediaSourceEvent + , public std::enable_shared_from_this { public: using Ptr = std::shared_ptr; - //如果retry_count<0,则一直重试播放;否则重试retry_count次数 - //默认一直重试 - PlayerProxy(const std::string &vhost, const std::string &app, const std::string &stream_id, - const ProtocolOption &option, int retry_count = -1, const toolkit::EventPoller::Ptr &poller = nullptr); + // 如果retry_count<0,则一直重试播放;否则重试retry_count次数 + // 默认一直重试 + PlayerProxy( + const std::string &vhost, const std::string &app, const std::string &stream_id, const ProtocolOption &option, int retry_count = -1, + const toolkit::EventPoller::Ptr &poller = nullptr); ~PlayerProxy() override; @@ -50,15 +54,14 @@ public: /** * 获取观看总人数 */ - int totalReaderCount() ; - + int totalReaderCount(); int getStatus(); uint64_t getLiveSecs(); uint64_t getRePullCount(); private: - //MediaSourceEvent override + // MediaSourceEvent override bool close(MediaSource &sender) override; int totalReaderCount(MediaSource &sender) override; MediaOriginType getOriginType(MediaSource &sender) const override; @@ -66,7 +69,7 @@ private: std::shared_ptr getOriginSock(MediaSource &sender) const override; float getLossRate(MediaSource &sender, TrackType type) override; - void rePlay(const std::string &strUrl,int iFailedCnt); + void rePlay(const std::string &strUrl, int iFailedCnt); void onPlaySuccess(); void setDirectProxy(); @@ -83,8 +86,8 @@ private: MultiMediaSourceMuxer::Ptr _muxer; toolkit::Ticker _live_ticker; - //0 表示正常 1 表示正在尝试拉流 - std::atomic _live_status; + // 0 表示正常 1 表示正在尝试拉流 + std::atomic _live_status; std::atomic _live_secs; std::atomic _repull_count; diff --git a/src/Pusher/PusherProxy.cpp b/src/Pusher/PusherProxy.cpp index b936842a..ffe1a500 100644 --- a/src/Pusher/PusherProxy.cpp +++ b/src/Pusher/PusherProxy.cpp @@ -16,7 +16,7 @@ using namespace std; namespace mediakit { PusherProxy::PusherProxy(const MediaSource::Ptr &src, int retry_count, const EventPoller::Ptr &poller) - : MediaPusher(src, poller){ + : MediaPusher(src, poller) { _retry_count = retry_count; _on_close = [](const SockException &) {}; _weak_src = src; @@ -65,7 +65,7 @@ void PusherProxy::publish(const string &dst_url) { strong_self->_live_status = 1; strong_self->rePublish(dst_url, (*failed_cnt)++); } else { - //如果媒体源已经注销, 或达到了最大重试次数,回调关闭 + // 如果媒体源已经注销, 或达到了最大重试次数,回调关闭 strong_self->_on_close(err); } }); @@ -76,20 +76,20 @@ void PusherProxy::publish(const string &dst_url) { return; } - if(*failed_cnt == 0){ + if (*failed_cnt == 0) { // 第一次重推更新时长 - strong_self->_live_secs += strong_self->_live_ticker.elapsedTime()/1000; + strong_self->_live_secs += strong_self->_live_ticker.elapsedTime() / 1000; strong_self->_live_ticker.resetTime(); - TraceL<<" live secs "<_live_secs; + TraceL << " live secs " << strong_self->_live_secs; } auto src = strong_self->_weak_src.lock(); - //推流异常中断,延时重试播放 + // 推流异常中断,延时重试播放 if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) { strong_self->_republish_count++; strong_self->rePublish(dst_url, (*failed_cnt)++); } else { - //如果媒体源已经注销, 或达到了最大重试次数,回调关闭 + // 如果媒体源已经注销, 或达到了最大重试次数,回调关闭 strong_self->_on_close(err); } }); @@ -100,30 +100,33 @@ void PusherProxy::publish(const string &dst_url) { void PusherProxy::rePublish(const string &dst_url, int failed_cnt) { auto delay = MAX(2 * 1000, MIN(failed_cnt * 3000, 60 * 1000)); weak_ptr weak_self = shared_from_this(); - _timer = std::make_shared(delay / 1000.0f, [weak_self, dst_url, failed_cnt]() { - //推流失败次数越多,则延时越长 - auto strong_self = weak_self.lock(); - if (!strong_self) { + _timer = std::make_shared( + delay / 1000.0f, + [weak_self, dst_url, failed_cnt]() { + // 推流失败次数越多,则延时越长 + auto strong_self = weak_self.lock(); + if (!strong_self) { + return false; + } + WarnL << "推流重试[" << failed_cnt << "]:" << dst_url; + strong_self->MediaPusher::publish(dst_url); return false; - } - WarnL << "推流重试[" << failed_cnt << "]:" << dst_url; - strong_self->MediaPusher::publish(dst_url); - return false; - }, getPoller()); + }, + getPoller()); } int PusherProxy::getStatus() { return _live_status.load(); } uint64_t PusherProxy::getLiveSecs() { - if(_live_status == 0){ - return _live_secs + _live_ticker.elapsedTime()/1000; - }else{ + if (_live_status == 0) { + return _live_secs + _live_ticker.elapsedTime() / 1000; + } else { return _live_secs; } } -uint64_t PusherProxy::getRePublishCount(){ +uint64_t PusherProxy::getRePublishCount() { return _republish_count; } diff --git a/src/Pusher/PusherProxy.h b/src/Pusher/PusherProxy.h index baeef029..e0146514 100644 --- a/src/Pusher/PusherProxy.h +++ b/src/Pusher/PusherProxy.h @@ -16,7 +16,9 @@ namespace mediakit { -class PusherProxy : public MediaPusher, public std::enable_shared_from_this { +class PusherProxy + : public MediaPusher + , public std::enable_shared_from_this { public: using Ptr = std::shared_ptr; @@ -41,7 +43,7 @@ public: * 开始拉流播放 * @param dstUrl 目标推流地址 */ - void publish(const std::string& dstUrl) override; + void publish(const std::string &dstUrl) override; int getStatus(); uint64_t getLiveSecs(); @@ -55,8 +57,8 @@ private: int _retry_count; toolkit::Timer::Ptr _timer; toolkit::Ticker _live_ticker; - //0 表示正常 1 表示正在尝试推流 - std::atomic _live_status; + // 0 表示正常 1 表示正在尝试推流 + std::atomic _live_status; std::atomic _live_secs; std::atomic _republish_count; std::weak_ptr _weak_src; @@ -66,4 +68,4 @@ private: } /* namespace mediakit */ -#endif //SRC_DEVICE_PUSHERPROXY_H +#endif // SRC_DEVICE_PUSHERPROXY_H From e97e0d86bd4a0742261887ca20db9996492de837 Mon Sep 17 00:00:00 2001 From: BackT0TheFuture <10088733+BackT0TheFuture@users.noreply.github.com> Date: Fri, 5 May 2023 17:34:32 +0800 Subject: [PATCH 14/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drtmp=E5=A4=9A=E5=B1=82?= =?UTF-8?q?=E7=BA=A7url=E8=A7=A3=E6=9E=90=E5=BC=82=E5=B8=B8=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2435)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解析多层级rtmp url会丢失某些层级信息: #2433 --- src/Rtmp/RtmpSession.cpp | 44 ++++++++++++++++++++-------------------- src/Rtmp/RtmpSession.h | 1 - 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 3c3f552e..15b8a89f 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -81,27 +81,23 @@ void RtmpSession::onCmd_connect(AMFDecoder &dec) { ///////////set peerBandwidth//////////////// sendPeerBandwidth(5000000); - _media_info._app = params["app"].as_string(); - _tc_url = params["tcUrl"].as_string(); - if(_tc_url.empty()){ - //defaultVhost:默认vhost - _tc_url = string(RTMP_SCHEMA) + "://" + DEFAULT_VHOST + "/" + _media_info._app; + auto tc_url = params["tcUrl"].as_string(); + if (tc_url.empty()) { + // defaultVhost:默认vhost + tc_url = string(RTMP_SCHEMA) + "://" + DEFAULT_VHOST + "/" + _media_info._app; } else { - auto pos = _tc_url.rfind('?'); + auto pos = tc_url.rfind('?'); if (pos != string::npos) { - //tc_url 中可能包含?以及参数,参见issue: #692 - _tc_url = _tc_url.substr(0, pos); - } - auto stream_start = _tc_url.rfind('/'); - if (stream_start != string::npos && stream_start > 1) { - auto protocol_end = _tc_url.find("://") + 2; - auto app_start = _tc_url.rfind('/', stream_start - 1); - if (app_start != protocol_end) { - // contain stream name part - _tc_url = _tc_url.substr(0, stream_start); - } + // tc_url 中可能包含?以及参数,参见issue: #692 + tc_url = tc_url.substr(0, pos); } } + // 初步解析,只用于获取vhost信息 + _media_info.parse(tc_url); + _media_info._schema = RTMP_SCHEMA; + // 赋值rtmp app + _media_info._app = params["app"].as_string(); + bool ok = true; //(app == APP_NAME); AMFValue version(AMF_OBJECT); version.set("fmsVer", "FMS/3,0,1,123"); @@ -135,8 +131,10 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { } })); dec.load();/* NULL */ - _media_info.parse(_tc_url + "/" + getStreamId(dec.load())); - _media_info._schema = RTMP_SCHEMA; + // 赋值为rtmp stream id 信息 + _media_info._streamid = getStreamId(dec.load()); + // 再解析url,切割url为app/stream_id (不一定符合rtmp url切割规范) + _media_info.parse(_media_info._schema + "://" + _media_info._vhost + '/' + _media_info._app + '/' + _media_info._streamid); auto now_stream_index = _now_stream_index; auto on_res = [this, token, now_stream_index](const string &err, const ProtocolOption &option) { @@ -433,9 +431,11 @@ string RtmpSession::getStreamId(const string &str){ } void RtmpSession::onCmd_play(AMFDecoder &dec) { - dec.load();/* NULL */ - _media_info.parse(_tc_url + "/" + getStreamId(dec.load())); - _media_info._schema = RTMP_SCHEMA; + dec.load(); /* NULL */ + // 赋值为rtmp stream id 信息 + _media_info._streamid = getStreamId(dec.load()); + // 再解析url,切割url为app/stream_id (不一定符合rtmp url切割规范) + _media_info.parse(_media_info._schema + "://" + _media_info._vhost + '/' + _media_info._app + '/' + _media_info._streamid); doPlay(dec); } diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index 243ac749..f9c2bf2a 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -92,7 +92,6 @@ private: uint32_t _continue_push_ms = 0; //消耗的总流量 uint64_t _total_bytes = 0; - std::string _tc_url; //数据接收超时计时器 toolkit::Ticker _ticker; MediaInfo _media_info; From 7ef39d1b6f496479a3654a52e4cc9fb91779a6f5 Mon Sep 17 00:00:00 2001 From: ljx0305 Date: Mon, 8 May 2023 15:27:43 +0800 Subject: [PATCH 15/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8DopenRtpServer=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=A3=B0=E6=98=8E=E4=B8=8D=E4=B8=80=E8=87=B4=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2445)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/WebApi.h b/server/WebApi.h index aaf0b19e..15210d53 100755 --- a/server/WebApi.h +++ b/server/WebApi.h @@ -231,9 +231,12 @@ bool checkArgs(Args &args, const First &first, const KeyTypes &...keys) { void installWebApi(); void unInstallWebApi(); -uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc); +#if defined(ENABLE_RTPPROXY) +uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio); void connectRtpServer(const std::string &stream_id, const std::string &dst_url, uint16_t dst_port, const std::function &cb); bool closeRtpServer(const std::string &stream_id); +#endif + Json::Value makeMediaSourceJson(mediakit::MediaSource &media); void getStatisticJson(const std::function &cb); void addStreamProxy(const std::string &vhost, const std::string &app, const std::string &stream, const std::string &url, int retry_count, From 350e262433ca88f4c0873fcf0b2376444176b498 Mon Sep 17 00:00:00 2001 From: Per-Arne Andersen Date: Thu, 11 May 2023 03:58:48 +0200 Subject: [PATCH 16/66] Added support for GCC 13 (#2452) GCC Does not explicitly include stdint. This update includes them where needed. A very minor change that would allow GCC13 and above to compile correctly. In newer versions, cstdint is not implicitly included, hence it errors out during compilation for some files. --- src/Http/strCoding.h | 2 +- src/Record/HlsMaker.h | 1 + src/Rtmp/amf.h | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Http/strCoding.h b/src/Http/strCoding.h index 4be8328e..00177f74 100644 --- a/src/Http/strCoding.h +++ b/src/Http/strCoding.h @@ -13,7 +13,7 @@ #include #include - +#include namespace mediakit { class strCoding { diff --git a/src/Record/HlsMaker.h b/src/Record/HlsMaker.h index 57a5308a..be20ba1b 100644 --- a/src/Record/HlsMaker.h +++ b/src/Record/HlsMaker.h @@ -14,6 +14,7 @@ #include #include #include +#include namespace mediakit { diff --git a/src/Rtmp/amf.h b/src/Rtmp/amf.h index 283fbcc7..276315cf 100644 --- a/src/Rtmp/amf.h +++ b/src/Rtmp/amf.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace toolkit { class BufferLikeString; } From 1136b0a3c0ccc93689c02c95cef2592cd343bf53 Mon Sep 17 00:00:00 2001 From: codeRATny <60806889+codeRATny@users.noreply.github.com> Date: Fri, 12 May 2023 10:20:31 +0700 Subject: [PATCH 17/66] Adding new features for proxy player (#2448) Add some functions for the proxy player, such as: getting stream information (basic video/audio information) callbacks for any connection and disconnection from the server And now you can set delay between reconnections. See proxy player constructor --- src/Player/PlayerProxy.cpp | 59 +++++++++++++++++++++++++++++++++-- src/Player/PlayerProxy.h | 63 +++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index b2b567ff..289c7249 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -25,7 +25,8 @@ using namespace std; namespace mediakit { PlayerProxy::PlayerProxy( - const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option, int retry_count, const EventPoller::Ptr &poller) + const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option, int retry_count, + const EventPoller::Ptr &poller, int reconnect_delay_min, int reconnect_delay_max, int reconnect_delay_step) : MediaPlayer(poller) , _option(option) { _vhost = vhost; @@ -33,6 +34,12 @@ PlayerProxy::PlayerProxy( _stream_id = stream_id; _retry_count = retry_count; + setOnConnect(nullptr); + setOnDisconnect(nullptr); + + _reconnect_delay_min = reconnect_delay_min > 0 ? reconnect_delay_min : 2; + _reconnect_delay_max = reconnect_delay_max > 0 ? reconnect_delay_max : 60; + _reconnect_delay_step = reconnect_delay_step > 0 ? reconnect_delay_step : 3; _live_secs = 0; _live_status = 1; _repull_count = 0; @@ -48,6 +55,47 @@ void PlayerProxy::setOnClose(const function &cb) _on_close = cb ? cb : [](const SockException &) {}; } +void PlayerProxy::setOnDisconnect(std::function cb) { + _on_disconnect = cb ? std::move(cb) : [] () {}; +} + +void PlayerProxy::setOnConnect(std::function cb) { + _on_connect = cb ? std::move(cb) : [](const TranslationInfo&) {}; +} + +void PlayerProxy::setTranslationInfo() +{ + _transtalion_info.byte_speed = _media_src->getBytesSpeed(); + _transtalion_info.start_time_stamp = _media_src->getCreateStamp(); + _transtalion_info.stream_info.clear(); + auto tracks = _muxer->getTracks(); + for (auto &track : tracks) { + _transtalion_info.stream_info.emplace_back(); + auto &back = _transtalion_info.stream_info.back(); + back.bitrate = track->getBitRate(); + back.codec_type = track->getTrackType(); + back.codec_name = track->getCodecName(); + switch (back.codec_type) { + case TrackAudio : { + auto audio_track = dynamic_pointer_cast(track); + back.audio_sample_rate = audio_track->getAudioSampleRate(); + back.audio_channel = audio_track->getAudioChannel(); + back.audio_sample_bit = audio_track->getAudioSampleBit(); + break; + } + case TrackVideo : { + auto video_track = dynamic_pointer_cast(track); + back.video_width = video_track->getVideoWidth(); + back.video_height = video_track->getVideoHeight(); + back.video_fps = video_track->getVideoFps(); + break; + } + default: + break; + } + } +} + void PlayerProxy::play(const string &strUrlTmp) { weak_ptr weakSelf = shared_from_this(); std::shared_ptr piFailedCnt(new int(0)); // 连续播放失败次数 @@ -70,10 +118,13 @@ void PlayerProxy::play(const string &strUrlTmp) { // 播放成功 *piFailedCnt = 0; // 连续播放失败次数清0 strongSelf->onPlaySuccess(); + strongSelf->setTranslationInfo(); + strongSelf->_on_connect(strongSelf->_transtalion_info); InfoL << "play " << strUrlTmp << " success"; } else if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) { // 播放失败,延时重试播放 + strongSelf->_on_disconnect(); strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++); } else { // 达到了最大重试次数,回调关闭 @@ -157,7 +208,7 @@ PlayerProxy::~PlayerProxy() { } void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) { - auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000, 60 * 1000)); + auto iDelay = MAX(_reconnect_delay_min * 1000, MIN(iFailedCnt * _reconnect_delay_step * 1000, _reconnect_delay_max * 1000)); weak_ptr weakSelf = shared_from_this(); _timer = std::make_shared( iDelay / 1000.0f, @@ -216,6 +267,10 @@ float PlayerProxy::getLossRate(MediaSource &sender, TrackType type) { return getPacketLossRate(type); } +TranslationInfo PlayerProxy::getTranslationInfo() { + return _transtalion_info; +} + void PlayerProxy::onPlaySuccess() { GET_CONFIG(bool, reset_when_replay, General::kResetWhenRePlay); if (dynamic_pointer_cast(_media_src)) { diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index d828f696..1ed625c6 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -18,6 +18,45 @@ namespace mediakit { +struct StreamInfo +{ + TrackType codec_type; + std::string codec_name; + int bitrate; + int audio_sample_rate; + int audio_sample_bit; + int audio_channel; + int video_width; + int video_height; + float video_fps; + + StreamInfo() + { + codec_type = TrackInvalid; + codec_name = "none"; + bitrate = -1; + audio_sample_rate = -1; + audio_channel = -1; + audio_sample_bit = -1; + video_height = -1; + video_width = -1; + video_fps = -1.0; + } +}; + +struct TranslationInfo +{ + std::vector stream_info; + int byte_speed; + uint64_t start_time_stamp; + + TranslationInfo() + { + byte_speed = -1; + start_time_stamp = 0; + } +}; + class PlayerProxy : public MediaPlayer , public MediaSourceEvent @@ -29,7 +68,7 @@ public: // 默认一直重试 PlayerProxy( const std::string &vhost, const std::string &app, const std::string &stream_id, const ProtocolOption &option, int retry_count = -1, - const toolkit::EventPoller::Ptr &poller = nullptr); + const toolkit::EventPoller::Ptr &poller = nullptr, int reconnect_delay_min = 2, int reconnect_delay_max = 60, int reconnect_delay_step = 3); ~PlayerProxy() override; @@ -45,6 +84,18 @@ public: */ void setOnClose(const std::function &cb); + /** + * Set a callback for failed server connection + * @param cb 回调对象 + */ + void setOnDisconnect(std::function cb); + + /** + * Set a callback for a successful connection to the server + * @param cb 回调对象 + */ + void setOnConnect(std::function cb); + /** * 开始拉流播放 * @param strUrl @@ -60,6 +111,9 @@ public: uint64_t getLiveSecs(); uint64_t getRePullCount(); + // Using this only makes sense after a successful connection to the server + TranslationInfo getTranslationInfo(); + private: // MediaSourceEvent override bool close(MediaSource &sender) override; @@ -72,17 +126,24 @@ private: void rePlay(const std::string &strUrl, int iFailedCnt); void onPlaySuccess(); void setDirectProxy(); + void setTranslationInfo(); private: ProtocolOption _option; int _retry_count; + int _reconnect_delay_min; + int _reconnect_delay_max; + int _reconnect_delay_step; std::string _vhost; std::string _app; std::string _stream_id; std::string _pull_url; toolkit::Timer::Ptr _timer; + std::function _on_disconnect; + std::function _on_connect; std::function _on_close; std::function _on_play; + TranslationInfo _transtalion_info; MultiMediaSourceMuxer::Ptr _muxer; toolkit::Ticker _live_ticker; From f6ac39ade5c08e65c07d622bad6becac00eef834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E8=A1=B2=E4=B8=8D=E5=87=BA=E5=AE=B6?= Date: Fri, 12 May 2023 11:47:22 +0800 Subject: [PATCH 18/66] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E6=89=80=E6=9C=89=E8=B7=A8=E5=9F=9F=E8=AF=B7=E6=B1=82=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=20(#2449)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 配置项为http.allow_cross_domains --- conf/config.ini | 2 ++ src/Common/config.cpp | 2 ++ src/Common/config.h | 2 ++ src/Http/HttpSession.cpp | 9 +++++++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 8950b299..d2b2c0dd 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -230,6 +230,8 @@ forbidCacheSuffix= #可以把http代理前真实客户端ip放在http头中:https://github.com/ZLMediaKit/ZLMediaKit/issues/1388 #切勿暴露此key,否则可能导致伪造客户端ip forwarded_ip_header= +#默认允许所有跨域请求 +allow_cross_domains=1 [multicast] #rtp组播截止组播ip地址 diff --git a/src/Common/config.cpp b/src/Common/config.cpp index cae98d55..1654c61c 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -159,6 +159,7 @@ const string kNotFound = HTTP_FIELD "notFound"; const string kDirMenu = HTTP_FIELD "dirMenu"; const string kForbidCacheSuffix = HTTP_FIELD "forbidCacheSuffix"; const string kForwardedIpHeader = HTTP_FIELD "forwarded_ip_header"; +const string kAllowCrossDomains = HTTP_FIELD "allow_cross_domains"; static onceToken token([]() { mINI::Instance()[kSendBufSize] = 64 * 1024; @@ -186,6 +187,7 @@ static onceToken token([]() { << endl; mINI::Instance()[kForbidCacheSuffix] = ""; mINI::Instance()[kForwardedIpHeader] = ""; + mINI::Instance()[kAllowCrossDomains] = 1; }); } // namespace Http diff --git a/src/Common/config.h b/src/Common/config.h index 146c6067..ea874d64 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -246,6 +246,8 @@ extern const std::string kDirMenu; extern const std::string kForbidCacheSuffix; // 可以把http代理前真实客户端ip放在http头中:https://github.com/ZLMediaKit/ZLMediaKit/issues/1388 extern const std::string kForwardedIpHeader; +// 是否允许所有跨域请求 +extern const std::string kAllowCrossDomains; } // namespace Http ////////////SHELL配置/////////// diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index a990f98b..c1a045f6 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -39,8 +39,13 @@ void HttpSession::Handle_Req_HEAD(ssize_t &content_len){ void HttpSession::Handle_Req_OPTIONS(ssize_t &content_len) { KeyValue header; - header.emplace("Allow", "GET, POST, OPTIONS"); - header.emplace("Access-Control-Allow-Origin", "*"); + header.emplace("Allow", "GET, POST, HEAD, OPTIONS"); + GET_CONFIG(bool, allow_cross_domains, Http::kAllowCrossDomains); + if (allow_cross_domains) { + header.emplace("Access-Control-Allow-Origin", "*"); + header.emplace("Access-Control-Allow-Headers", "*"); + header.emplace("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS"); + } header.emplace("Access-Control-Allow-Credentials", "true"); header.emplace("Access-Control-Request-Methods", "GET, POST, OPTIONS"); header.emplace("Access-Control-Request-Headers", "Accept,Accept-Language,Content-Language,Content-Type"); From 0afe75229e30b5d4729a2f49c29aa8b75d78fe70 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 13 May 2023 00:02:06 +0800 Subject: [PATCH 19/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=89=E6=B5=81?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E8=8E=B7=E5=8F=96=E6=B5=81=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E8=AE=BF=E9=97=AE=E7=A9=BA=E6=8C=87=E9=92=88?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此bug由 #2448 引入,触发条件: - 1.配置文件rtsp.directProxy设置为0 - 2.调用addStreamProxy代理rtsp流 在非直接代理情况下,PlayerProxy::_media_src为空,导致访问空指针。 --- src/Player/PlayerProxy.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 289c7249..5bdb3083 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -65,8 +65,8 @@ void PlayerProxy::setOnConnect(std::function cb) { void PlayerProxy::setTranslationInfo() { - _transtalion_info.byte_speed = _media_src->getBytesSpeed(); - _transtalion_info.start_time_stamp = _media_src->getCreateStamp(); + _transtalion_info.byte_speed = _media_src ? _media_src->getBytesSpeed() : -1; + _transtalion_info.start_time_stamp = _media_src ? _media_src->getCreateStamp() : 0; _transtalion_info.stream_info.clear(); auto tracks = _muxer->getTracks(); for (auto &track : tracks) { From 4fa04d6a370eb4787de21ef55ad108fed63b34b5 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 13 May 2023 00:14:35 +0800 Subject: [PATCH 20/66] =?UTF-8?q?=E4=BC=98=E5=8C=96PlayerProxy=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Player/PlayerProxy.cpp | 15 +++++++-------- src/Player/PlayerProxy.h | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 5bdb3083..d1f414d3 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -34,6 +34,7 @@ PlayerProxy::PlayerProxy( _stream_id = stream_id; _retry_count = retry_count; + setOnClose(nullptr); setOnConnect(nullptr); setOnDisconnect(nullptr); @@ -43,16 +44,15 @@ PlayerProxy::PlayerProxy( _live_secs = 0; _live_status = 1; _repull_count = 0; - _on_close = [](const SockException &) {}; (*this)[Client::kWaitTrackReady] = false; } -void PlayerProxy::setPlayCallbackOnce(const function &cb) { - _on_play = cb; +void PlayerProxy::setPlayCallbackOnce(function cb) { + _on_play = std::move(cb); } -void PlayerProxy::setOnClose(const function &cb) { - _on_close = cb ? cb : [](const SockException &) {}; +void PlayerProxy::setOnClose(function cb) { + _on_close = cb ? std::move(cb) : [](const SockException &) {}; } void PlayerProxy::setOnDisconnect(std::function cb) { @@ -174,7 +174,7 @@ void PlayerProxy::play(const string &strUrlTmp) { MediaPlayer::play(strUrlTmp); } catch (std::exception &ex) { ErrorL << ex.what(); - _on_play_result(SockException(Err_other, ex.what())); + onPlayResult(SockException(Err_other, ex.what())); return; } _pull_url = strUrlTmp; @@ -324,9 +324,8 @@ int PlayerProxy::getStatus() { uint64_t PlayerProxy::getLiveSecs() { if (_live_status == 0) { return _live_secs + _live_ticker.elapsedTime() / 1000; - } else { - return _live_secs; } + return _live_secs; } uint64_t PlayerProxy::getRePullCount() { diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 1ed625c6..9cf621da 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -76,13 +76,13 @@ public: * 设置play结果回调,只触发一次;在play执行之前有效 * @param cb 回调对象 */ - void setPlayCallbackOnce(const std::function &cb); + void setPlayCallbackOnce(std::function cb); /** * 设置主动关闭回调 * @param cb 回调对象 */ - void setOnClose(const std::function &cb); + void setOnClose(std::function cb); /** * Set a callback for failed server connection From 4f6e32b51088c64cdffd4ac08efbd86844496c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sun, 14 May 2023 09:57:59 +0800 Subject: [PATCH 21/66] =?UTF-8?q?mk=5Fmedia=5Finput=5Faac=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=8F=AF=E4=BB=A5=E4=B8=8D=E6=8C=87=E5=AE=9Aadts?= =?UTF-8?q?=E5=A4=B4(#2463)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复issue: #2432 --- api/source/mk_media.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 070c12ed..9bd2afa4 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -263,7 +263,7 @@ API_EXPORT void API_CALL mk_media_input_yuv(mk_media ctx, const char *yuv[3], in } API_EXPORT int API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint64_t dts, void *adts) { - assert(ctx && data && len > 0 && adts); + assert(ctx && data && len > 0); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; return (*obj)->getChannel()->inputAAC((const char *) data, len, dts, (char *) adts); } From cb362371b917ff36ab0a8e20deb6525827e1f12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sun, 14 May 2023 10:10:08 +0800 Subject: [PATCH 22/66] =?UTF-8?q?=E6=9B=B4=E6=96=B0zltoolkit,=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DiOS=E4=B8=8B=E7=BC=96=E8=AF=91=E9=97=AE=E9=A2=98=20=20?= =?UTF-8?q?(#2464)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 参考:#2459 --- 3rdpart/ZLToolKit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index d134fbb9..b30eeca0 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit d134fbb9fe0b108dddb66a7bfa35b7b438426411 +Subproject commit b30eeca034456417dfca72aa0d2258031013a5e6 From 15e76293d0921c9b9a73bddac9516f9940423a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sun, 14 May 2023 10:19:07 +0800 Subject: [PATCH 23/66] =?UTF-8?q?readme=E6=96=B0=E5=A2=9E=E5=A4=9A?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E8=B7=B3=E8=BD=AC=E9=93=BE=E6=8E=A5=20(#2465?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ README_en.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 9843b217..fa9f50d5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ![logo](https://raw.githubusercontent.com/ZLMediaKit/ZLMediaKit/master/www/logo.png) +简体中文 | [English](./README_en.md) + # 一个基于C++11的高性能运营级流媒体服务框架 [![](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/ZLMediaKit/ZLMediaKit/blob/master/LICENSE) diff --git a/README_en.md b/README_en.md index 25980d01..b7410f8b 100644 --- a/README_en.md +++ b/README_en.md @@ -1,5 +1,7 @@ ![logo](https://raw.githubusercontent.com/zlmediakit/ZLMediaKit/master/www/logo.png) +[简体中文](./README.md) | English + # An high-performance, enterprise-level streaming media service framework based on C++11. From 7e4010df83ffd9b513daaea02f93b53f77a26716 Mon Sep 17 00:00:00 2001 From: Kiki Date: Thu, 18 May 2023 16:35:54 +0800 Subject: [PATCH 24/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8DstartSendRtp=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3gop=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98=20(#2455?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果帧序列为SPS PPS SEI IDR,那么GOP缓存会从IDR开始,导致解码器未获取到SPS PPS从而不能秒开 --- src/Common/MultiMediaSourceMuxer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index daa68769..440adcc1 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -327,7 +327,6 @@ EventPoller::Ptr MultiMediaSourceMuxer::getOwnerPoller(MediaSource &sender) { } bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) { - bool ret = false; if (_rtmp) { ret = _rtmp->addTrack(track) ? true : ret; @@ -471,7 +470,9 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) { // 视频时,遇到第一帧配置帧或关键帧则标记为gop开始处 auto video_key_pos = frame->keyFrame() || frame->configFrame(); _ring->write(frame, video_key_pos && !_video_key_pos); - _video_key_pos = video_key_pos; + if (!frame->dropAble()) { + _video_key_pos = video_key_pos; + } } else { // 没有视频时,设置is_key为true,目的是关闭gop缓存 _ring->write(frame, !haveVideo()); From d4f80171171cdfc00c6be27fab33124eae6a1bdd Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Wed, 24 May 2023 10:50:49 +0800 Subject: [PATCH 25/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmk=5Ftcp=5Fsession=5Fre?= =?UTF-8?q?f=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=E8=AF=AF=E5=AF=BC?= =?UTF-8?q?=E6=80=A7=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_tcp.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/include/mk_tcp.h b/api/include/mk_tcp.h index 84deed8e..4ab19da1 100644 --- a/api/include/mk_tcp.h +++ b/api/include/mk_tcp.h @@ -82,11 +82,11 @@ API_EXPORT void API_CALL mk_tcp_session_send_buffer(const mk_tcp_session ctx, mk API_EXPORT void API_CALL mk_tcp_session_send_safe(const mk_tcp_session ctx, const char *data, size_t len); API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ctx, mk_buffer buffer); -//创建mk_tcp_session的弱引用 +//创建mk_tcp_session的强引用 API_EXPORT mk_tcp_session_ref API_CALL mk_tcp_session_ref_from(const mk_tcp_session ctx); -//删除mk_tcp_session的弱引用 +//删除mk_tcp_session的强引用 API_EXPORT void mk_tcp_session_ref_release(const mk_tcp_session_ref ref); -//根据弱引用获取mk_tcp_session,如果mk_tcp_session已经销毁,那么返回NULL +//根据强引用获取mk_tcp_session API_EXPORT mk_tcp_session mk_tcp_session_from_ref(const mk_tcp_session_ref ref); ///////////////////////////////////////////自定义tcp服务///////////////////////////////////////////// From 936babf4599e619b4dc721a0dc75562ddd67481c Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Thu, 25 May 2023 16:57:03 +0800 Subject: [PATCH 26/66] =?UTF-8?q?=E6=96=B0=E5=A2=9Emk=5Fmedia=5Fsource=5Ff?= =?UTF-8?q?ind2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_events_objects.h | 6 ++++++ api/source/mk_events_objects.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index f8d3c571..8f362980 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -132,6 +132,12 @@ API_EXPORT void API_CALL mk_media_source_find(const char *schema, int from_mp4, void *user_data, on_mk_media_source_find_cb cb); + +API_EXPORT const mk_media_source API_CALL mk_media_source_find2(const char *schema, + const char *vhost, + const char *app, + const char *stream, + int from_mp4); //MediaSource::for_each_media() API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb, const char *schema, const char *vhost, const char *app, const char *stream); diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index 4b641a2b..fa820062 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -248,6 +248,16 @@ API_EXPORT void API_CALL mk_media_source_find(const char *schema, cb(user_data, (mk_media_source)src.get()); } +API_EXPORT const mk_media_source API_CALL mk_media_source_find2(const char *schema, + const char *vhost, + const char *app, + const char *stream, + int from_mp4) { + assert(schema && vhost && app && stream); + auto src = MediaSource::find(schema, vhost, app, stream, from_mp4); + return (mk_media_source)src.get(); +} + API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb, const char *schema, const char *vhost, const char *app, const char *stream) { assert(cb); From 3f447246c2a77bf01c189bafe34be127cc7eb1d3 Mon Sep 17 00:00:00 2001 From: PioLing <964472638@qq.com> Date: Fri, 26 May 2023 17:02:21 +0800 Subject: [PATCH 27/66] =?UTF-8?q?=E4=BD=BF=E7=94=A8SDL=5FOpenAudioDevice?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=89=93=E5=BC=80=E9=9F=B3=E9=A2=91=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=20(#2489)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复SDL_OpenAudio()接口可能打开失败问题 --- player/SDLAudioDevice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/player/SDLAudioDevice.cpp b/player/SDLAudioDevice.cpp index 41835fb7..76093c89 100644 --- a/player/SDLAudioDevice.cpp +++ b/player/SDLAudioDevice.cpp @@ -33,9 +33,9 @@ SDLAudioDevice::SDLAudioDevice() { SDLAudioDevice *_this = (SDLAudioDevice *) userdata; _this->onReqPCM((char *) stream, len); }; - if (SDL_OpenAudio(&wanted_spec, &_audio_config) < 0) { - throw std::runtime_error("SDL_OpenAudio failed"); - } + if (SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &_audio_config, SDL_AUDIO_ALLOW_ANY_CHANGE) < 0) { + throw std::runtime_error("SDL_OpenAudioDevice failed"); + } InfoL << "actual audioSpec, " << "freq:" << _audio_config.freq << ", format:" << hex << _audio_config.format << dec From 032a2ac91d1b04fdfa8c7033c320fedeb7c6a3f0 Mon Sep 17 00:00:00 2001 From: dengjfzh <76604422+dengjfzh@users.noreply.github.com> Date: Fri, 26 May 2023 17:34:47 +0800 Subject: [PATCH 28/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8DTCP=20RTP=202=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E5=A4=B4=E8=A2=AB=E8=AF=AF=E5=88=A4=E4=B8=BA4?= =?UTF-8?q?=E5=AD=97=E8=8A=82=E5=A4=B4=E7=9A=84=E9=97=AE=E9=A2=98=20(#2499?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当RFC4571封装的RTP包大小在0x2400到0x24ff之间时,会被误认为是RTSP Interleaved 封装的数据。因为0x24正好是'$'的ASCII码,而RtpSplitter::onSearchPacketTail通过'$'来识别RTSP Interleaved数据。暂时没有想到什么好的办法能够百分之百的正确区分RFC4571数据和RTSP Interleaved数据。增加一个状态变量记录之前的状态,一旦检测到数据不是RTSP Interleaved格式,则后续不再进行尝试,以减小误判的可能性。 --- src/Rtp/RtpSplitter.cpp | 12 ++++++++---- src/Rtp/RtpSplitter.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Rtp/RtpSplitter.cpp b/src/Rtp/RtpSplitter.cpp index f7175b30..78c5223c 100644 --- a/src/Rtp/RtpSplitter.cpp +++ b/src/Rtp/RtpSplitter.cpp @@ -59,11 +59,15 @@ const char *RtpSplitter::onSearchPacketTail(const char *data, size_t len) { return onSearchPacketTail_l(data + kEHOME_OFFSET + 2, len - kEHOME_OFFSET - 2); } - if (data[0] == '$') { - //可能是4个字节的rtp头 - _offset = 4; - return onSearchPacketTail_l(data + 2, len - 2); + if ( _is_rtsp_interleaved ) { + if (data[0] == '$') { + //可能是4个字节的rtp头 + _offset = 4; + return onSearchPacketTail_l(data + 2, len - 2); + } + _is_rtsp_interleaved = false; } + //两个字节的rtp头 _offset = 2; return onSearchPacketTail_l(data, len); diff --git a/src/Rtp/RtpSplitter.h b/src/Rtp/RtpSplitter.h index 9401691e..50e3af43 100644 --- a/src/Rtp/RtpSplitter.h +++ b/src/Rtp/RtpSplitter.h @@ -36,6 +36,7 @@ protected: private: bool _is_ehome = false; + bool _is_rtsp_interleaved = true; size_t _offset = 0; }; From 18dbbc8d19e325704d846598955d433f99c91550 Mon Sep 17 00:00:00 2001 From: dengjfzh <76604422+dengjfzh@users.noreply.github.com> Date: Sat, 27 May 2023 22:27:10 +0800 Subject: [PATCH 29/66] =?UTF-8?q?=E9=99=8D=E4=BD=8ETCP=20RTP=202=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E5=A4=B4=E8=A2=AB=E8=AF=AF=E5=88=A4=E4=B8=BAEHOME?= =?UTF-8?q?=E5=A4=B4=E7=9A=84=E9=A3=8E=E9=99=A9=20(#2502)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当TCP RTP包大小为256字节时,可能触发RtpSplitter::onSearchPacketTail误判为EHOME格式。 这个修改一旦检测到数据不是EHOME格式,则后续不再进行尝试,以减小误判的风险。 --- src/Rtp/RtpSplitter.cpp | 23 +++++++++++++---------- src/Rtp/RtpSplitter.h | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Rtp/RtpSplitter.cpp b/src/Rtp/RtpSplitter.cpp index 78c5223c..0f4b6dc4 100644 --- a/src/Rtp/RtpSplitter.cpp +++ b/src/Rtp/RtpSplitter.cpp @@ -46,17 +46,20 @@ const char *RtpSplitter::onSearchPacketTail(const char *data, size_t len) { return nullptr; } - if (isEhome(data, len)) { - //是ehome协议 - if (len < kEHOME_OFFSET + 4) { - //数据不够 - return nullptr; + if ( _is_ehome ) { + if (isEhome(data, len)) { + //是ehome协议 + if (len < kEHOME_OFFSET + 4) { + //数据不够 + return nullptr; + } + //忽略ehome私有头后是rtsp样式的rtp,多4个字节, + _offset = kEHOME_OFFSET + 4; + _is_ehome = true; + //忽略ehome私有头 + return onSearchPacketTail_l(data + kEHOME_OFFSET + 2, len - kEHOME_OFFSET - 2); } - //忽略ehome私有头后是rtsp样式的rtp,多4个字节, - _offset = kEHOME_OFFSET + 4; - _is_ehome = true; - //忽略ehome私有头 - return onSearchPacketTail_l(data + kEHOME_OFFSET + 2, len - kEHOME_OFFSET - 2); + _is_ehome = false; } if ( _is_rtsp_interleaved ) { diff --git a/src/Rtp/RtpSplitter.h b/src/Rtp/RtpSplitter.h index 50e3af43..a2990a7f 100644 --- a/src/Rtp/RtpSplitter.h +++ b/src/Rtp/RtpSplitter.h @@ -35,7 +35,7 @@ protected: const char *onSearchPacketTail_l(const char *data, size_t len); private: - bool _is_ehome = false; + bool _is_ehome = true; bool _is_rtsp_interleaved = true; size_t _offset = 0; }; From 51e93132751a09edb3e817e8c9f5471431a7beae Mon Sep 17 00:00:00 2001 From: a-ucontrol <55526028+a-ucontrol@users.noreply.github.com> Date: Mon, 29 May 2023 09:48:13 +0300 Subject: [PATCH 30/66] Make available the media_info variable for the class inherited from WebRtcSession and HttpSession. (#2505 #2461) --- src/Http/HttpSession.h | 4 +++- webrtc/WebRtcPlayer.h | 1 + webrtc/WebRtcSession.h | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index b06a7f9e..68b2030d 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -124,6 +124,9 @@ private: //设置socket标志 void setSocketFlags(); +protected: + MediaInfo _mediaInfo; + private: bool _is_live_stream = false; bool _live_over_websocket = false; @@ -132,7 +135,6 @@ private: std::string _origin; Parser _parser; toolkit::Ticker _ticker; - MediaInfo _mediaInfo; TSMediaSource::RingType::RingReader::Ptr _ts_reader; FMP4MediaSource::RingType::RingReader::Ptr _fmp4_reader; //处理content数据的callback diff --git a/webrtc/WebRtcPlayer.h b/webrtc/WebRtcPlayer.h index fbe168b2..02208f6d 100644 --- a/webrtc/WebRtcPlayer.h +++ b/webrtc/WebRtcPlayer.h @@ -21,6 +21,7 @@ public: using Ptr = std::shared_ptr; ~WebRtcPlayer() override = default; static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info, bool preferred_tcp = false); + MediaInfo getMediaInfo() { return _media_info; } protected: ///////WebRtcTransportImp override/////// diff --git a/webrtc/WebRtcSession.h b/webrtc/WebRtcSession.h index f70d5e74..9cf7343c 100644 --- a/webrtc/WebRtcSession.h +++ b/webrtc/WebRtcSession.h @@ -35,6 +35,9 @@ public: void onManager() override; static EventPoller::Ptr queryPoller(const Buffer::Ptr &buffer); +protected: + WebRtcTransportImp::Ptr _transport; + private: //// HttpRequestSplitter override //// ssize_t onRecvHeader(const char *data, size_t len) override; @@ -47,7 +50,6 @@ private: bool _find_transport = true; Ticker _ticker; std::weak_ptr _server; - WebRtcTransportImp::Ptr _transport; }; }// namespace mediakit From f4ee607febd032b3b5c1146b0a3bc799a99213d0 Mon Sep 17 00:00:00 2001 From: Johnny Date: Thu, 11 May 2023 20:47:40 +0800 Subject: [PATCH 31/66] add RtspMediaSource::Clone --- src/Rtsp/RtspMediaSource.h | 4 ++++ src/Rtsp/RtspMediaSourceImp.cpp | 6 ++++++ src/Rtsp/RtspMediaSourceImp.h | 1 + webrtc/WebRtcPusher.cpp | 9 ++++----- webrtc/WebRtcPusher.h | 8 ++++---- webrtc/WebRtcTransport.cpp | 2 ++ 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index 4296f131..b6dc3d46 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -76,6 +76,10 @@ public: return _sdp; } + virtual RtspMediaSource::Ptr Clone(const std::string& stream) { + return nullptr; + } + /** * 获取相应轨道的ssrc */ diff --git a/src/Rtsp/RtspMediaSourceImp.cpp b/src/Rtsp/RtspMediaSourceImp.cpp index 7354306f..b10b071c 100644 --- a/src/Rtsp/RtspMediaSourceImp.cpp +++ b/src/Rtsp/RtspMediaSourceImp.cpp @@ -126,6 +126,12 @@ void RtspMediaSourceImp::setProtocolOption(const ProtocolOption &option) } } +RtspMediaSource::Ptr RtspMediaSourceImp::Clone(const std::string &stream) { + auto src_imp = std::make_shared(getVhost(), getApp(), stream); + src_imp->setSdp(getSdp()); + src_imp->setProtocolOption(getProtocolOption()); + return src_imp; +} } diff --git a/src/Rtsp/RtspMediaSourceImp.h b/src/Rtsp/RtspMediaSourceImp.h index 385fc80a..ca341836 100644 --- a/src/Rtsp/RtspMediaSourceImp.h +++ b/src/Rtsp/RtspMediaSourceImp.h @@ -107,6 +107,7 @@ public: } } + RtspMediaSource::Ptr Clone(const std::string& stream) override; private: bool _all_track_ready = false; ProtocolOption _option; diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index 8fe6549c..03e25200 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -10,13 +10,14 @@ #include "WebRtcPusher.h" #include "Common/config.h" +#include "Rtsp/RtspMediaSourceImp.h" using namespace std; namespace mediakit { WebRtcPusher::Ptr WebRtcPusher::create(const EventPoller::Ptr &poller, - const RtspMediaSourceImp::Ptr &src, + const RtspMediaSource::Ptr &src, const std::shared_ptr &ownership, const MediaInfo &info, const ProtocolOption &option, @@ -30,7 +31,7 @@ WebRtcPusher::Ptr WebRtcPusher::create(const EventPoller::Ptr &poller, } WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller, - const RtspMediaSourceImp::Ptr &src, + const RtspMediaSource::Ptr &src, const std::shared_ptr &ownership, const MediaInfo &info, const ProtocolOption &option, @@ -98,10 +99,8 @@ void WebRtcPusher::onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Pt auto &src = _push_src_sim[rid]; if (!src) { auto stream_id = rid.empty() ? _push_src->getId() : _push_src->getId() + "_" + rid; - auto src_imp = std::make_shared(_push_src->getVhost(), _push_src->getApp(), stream_id); + auto src_imp = _push_src->Clone(stream_id); _push_src_sim_ownership[rid] = src_imp->getOwnership(); - src_imp->setSdp(_push_src->getSdp()); - src_imp->setProtocolOption(_push_src->getProtocolOption()); src_imp->setListener(static_pointer_cast(shared_from_this())); src = src_imp; } diff --git a/webrtc/WebRtcPusher.h b/webrtc/WebRtcPusher.h index f00e0483..32c47055 100644 --- a/webrtc/WebRtcPusher.h +++ b/webrtc/WebRtcPusher.h @@ -12,7 +12,7 @@ #define ZLMEDIAKIT_WEBRTCPUSHER_H #include "WebRtcTransport.h" -#include "Rtsp/RtspMediaSourceImp.h" +#include "Rtsp/RtspMediaSource.h" namespace mediakit { @@ -20,7 +20,7 @@ class WebRtcPusher : public WebRtcTransportImp, public MediaSourceEvent { public: using Ptr = std::shared_ptr; ~WebRtcPusher() override = default; - static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src, + static Ptr create(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const std::shared_ptr &ownership, const MediaInfo &info, const ProtocolOption &option, bool preferred_tcp = false); protected: @@ -51,7 +51,7 @@ protected: float getLossRate(MediaSource &sender,TrackType type) override; private: - WebRtcPusher(const EventPoller::Ptr &poller, const RtspMediaSourceImp::Ptr &src, + WebRtcPusher(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const std::shared_ptr &ownership, const MediaInfo &info, const ProtocolOption &option, bool preferred_tcp); private: @@ -61,7 +61,7 @@ private: //媒体相关元数据 MediaInfo _media_info; //推流的rtsp源 - RtspMediaSourceImp::Ptr _push_src; + RtspMediaSource::Ptr _push_src; //推流所有权 std::shared_ptr _push_src_ownership; //推流的rtsp源,支持simulcast diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index fe8c6e5c..1c9fb0fc 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -23,6 +23,8 @@ #include "WebRtcPlayer.h" #include "WebRtcPusher.h" +#include "Rtsp/RtspMediaSourceImp.h" + #define RTP_SSRC_OFFSET 1 #define RTX_SSRC_OFFSET 2 #define RTP_CNAME "zlmediakit-rtp" From 0232caf068d82e7175c52c2eeaf67dbcf0b7f68f Mon Sep 17 00:00:00 2001 From: Johnny Date: Thu, 25 May 2023 16:23:24 +0800 Subject: [PATCH 32/66] add MediaTuple --- api/include/mk_events_objects.h | 14 +++---- api/source/mk_events_objects.cpp | 14 +++---- api/source/mk_media.cpp | 3 +- server/FFmpegSource.cpp | 22 +++++------ server/WebApi.cpp | 6 ++- server/WebHook.cpp | 28 +++++++------- src/Common/Device.h | 6 +-- src/Common/MediaSource.cpp | 58 +++++++++++++--------------- src/Common/MediaSource.h | 32 +++++++-------- src/Common/MultiMediaSourceMuxer.cpp | 29 +++++++------- src/Common/MultiMediaSourceMuxer.h | 9 +++-- src/FMP4/FMP4MediaSource.h | 6 +-- src/FMP4/FMP4MediaSourceMuxer.h | 7 +--- src/Http/HttpFileManager.cpp | 12 +++--- src/Http/HttpSession.cpp | 2 +- src/Player/PlayerProxy.cpp | 16 ++++---- src/Player/PlayerProxy.h | 4 +- src/Record/HlsMakerImp.cpp | 2 +- src/Record/HlsMediaSource.h | 3 +- src/Record/HlsRecorder.h | 4 +- src/Record/MP4Reader.cpp | 11 +++--- src/Record/Recorder.cpp | 22 +++++------ src/Record/Recorder.h | 18 ++++++--- src/Rtmp/RtmpMediaSource.h | 7 +--- src/Rtmp/RtmpMediaSourceImp.cpp | 4 +- src/Rtmp/RtmpMediaSourceImp.h | 2 +- src/Rtmp/RtmpMediaSourceMuxer.h | 6 +-- src/Rtmp/RtmpSession.cpp | 30 +++++++------- src/Rtp/GB28181Process.cpp | 6 +-- src/Rtp/RtpProcess.cpp | 20 +++++----- src/Rtsp/RtspMediaSource.h | 8 +--- src/Rtsp/RtspMediaSourceImp.cpp | 11 +++--- src/Rtsp/RtspMediaSourceImp.h | 4 +- src/Rtsp/RtspMediaSourceMuxer.h | 6 +-- src/Rtsp/RtspSession.cpp | 12 +++--- src/TS/TSMediaSource.h | 5 +-- src/TS/TSMediaSourceMuxer.h | 7 +--- srt/SrtTransportImp.cpp | 32 +++++++-------- tests/test_server.cpp | 15 ++++--- webrtc/WebRtcPusher.cpp | 4 +- webrtc/WebRtcTransport.cpp | 8 ++-- 41 files changed, 242 insertions(+), 273 deletions(-) diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index 8f362980..c114c783 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -61,19 +61,19 @@ API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_ ///////////////////////////////////////////MediaInfo///////////////////////////////////////////// //MediaInfo对象的C映射 typedef struct mk_media_info_t *mk_media_info; -//MediaInfo::_param_strs +//MediaInfo::param_strs API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx); -//MediaInfo::_schema +//MediaInfo::schema API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx); -//MediaInfo::_vhost +//MediaInfo::vhost API_EXPORT const char* API_CALL mk_media_info_get_vhost(const mk_media_info ctx); -//MediaInfo::_app +//MediaInfo::app API_EXPORT const char* API_CALL mk_media_info_get_app(const mk_media_info ctx); -//MediaInfo::_streamid +//MediaInfo::stream API_EXPORT const char* API_CALL mk_media_info_get_stream(const mk_media_info ctx); -//MediaInfo::_host +//MediaInfo::host API_EXPORT const char* API_CALL mk_media_info_get_host(const mk_media_info ctx); -//MediaInfo::_port +//MediaInfo::port API_EXPORT uint16_t API_CALL mk_media_info_get_port(const mk_media_info ctx); diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index fa820062..1903fa05 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -126,43 +126,43 @@ API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_ API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_param_strs.c_str(); + return info->param_strs.c_str(); } API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_schema.c_str(); + return info->schema.c_str(); } API_EXPORT const char* API_CALL mk_media_info_get_vhost(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_vhost.c_str(); + return info->vhost.c_str(); } API_EXPORT const char* API_CALL mk_media_info_get_host(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_host.c_str(); + return info->host.c_str(); } API_EXPORT uint16_t API_CALL mk_media_info_get_port(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_port; + return info->port; } API_EXPORT const char* API_CALL mk_media_info_get_app(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_app.c_str(); + return info->app.c_str(); } API_EXPORT const char* API_CALL mk_media_info_get_stream(const mk_media_info ctx){ assert(ctx); MediaInfo *info = (MediaInfo *)ctx; - return info->_streamid.c_str(); + return info->stream.c_str(); } ///////////////////////////////////////////MediaSource///////////////////////////////////////////// diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 9bd2afa4..3e1d33e0 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -22,7 +22,8 @@ public: MediaHelper(const char *vhost, const char *app, const char *stream, float duration, const ProtocolOption &option) { _poller = EventPollerPool::Instance().getPoller(); // 在poller线程中创建DevChannel(MultiMediaSourceMuxer)对象,确保严格的线程安全限制 - _poller->sync([&]() { _channel = std::make_shared(vhost, app, stream, duration, option); }); + auto tuple = MediaTuple{vhost, app, stream}; + _poller->sync([&]() { _channel = std::make_shared(tuple, duration, option); }); } ~MediaHelper() = default; diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index 48fb2cd5..5e7fc246 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -102,9 +102,9 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url,cons _process.run(cmd, log_file); InfoL << cmd; - if (is_local_ip(_media_info._host)) { + if (is_local_ip(_media_info.host)) { //推流给自己的,通过判断流是否注册上来判断是否正常 - if(_media_info._schema != RTSP_SCHEMA && _media_info._schema != RTMP_SCHEMA){ + if (_media_info.schema != RTSP_SCHEMA && _media_info.schema != RTMP_SCHEMA) { cb(SockException(Err_other,"本服务只支持rtmp/rtsp推流")); return; } @@ -154,10 +154,10 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url,cons } void FFmpegSource::findAsync(int maxWaitMS, const function &cb) { - auto src = MediaSource::find(_media_info._schema, - _media_info._vhost, - _media_info._app, - _media_info._streamid); + auto src = MediaSource::find(_media_info.schema, + _media_info.vhost, + _media_info.app, + _media_info.stream); if(src || !maxWaitMS){ cb(src); return; @@ -183,10 +183,10 @@ void FFmpegSource::findAsync(int maxWaitMS, const function_media_info._schema || - sender.getVhost() != strongSelf->_media_info._vhost || - sender.getApp() != strongSelf->_media_info._app || - sender.getId() != strongSelf->_media_info._streamid) { + sender.getSchema() != strongSelf->_media_info.schema || + sender.getVhost() != strongSelf->_media_info.vhost || + sender.getApp() != strongSelf->_media_info.app || + sender.getId() != strongSelf->_media_info.stream) { //不是自己感兴趣的事件,忽略之 return; } @@ -223,7 +223,7 @@ void FFmpegSource::startTimer(int timeout_ms) { return false; } bool needRestart = ffmpeg_restart_sec > 0 && strongSelf->_replay_ticker.elapsedTime() > ffmpeg_restart_sec * 1000; - if (is_local_ip(strongSelf->_media_info._host)) { + if (is_local_ip(strongSelf->_media_info.host)) { //推流给自己的,我们通过检查是否已经注册来判断FFmpeg是否工作正常 strongSelf->findAsync(0, [&](const MediaSource::Ptr &src) { //同步查找流 diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 6a1c47e5..f1bc4f8f 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1467,7 +1467,8 @@ void installWebApi() { api_regist("/index/api/deleteRecordDirectory", [](API_ARGS_MAP) { CHECK_SECRET(); CHECK_ARGS("vhost", "app", "stream"); - auto record_path = Recorder::getRecordPath(Recorder::type_mp4, allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["customized_path"]); + auto tuple = MediaTuple{allArgs["vhost"], allArgs["app"], allArgs["stream"]}; + auto record_path = Recorder::getRecordPath(Recorder::type_mp4, tuple, allArgs["customized_path"]); auto period = allArgs["period"]; record_path = record_path + period + "/"; int result = File::delete_file(record_path.data()); @@ -1484,7 +1485,8 @@ void installWebApi() { api_regist("/index/api/getMp4RecordFile", [](API_ARGS_MAP){ CHECK_SECRET(); CHECK_ARGS("vhost", "app", "stream"); - auto record_path = Recorder::getRecordPath(Recorder::type_mp4, allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["customized_path"]); + auto tuple = MediaTuple{allArgs["vhost"], allArgs["app"], allArgs["stream"]}; + auto record_path = Recorder::getRecordPath(Recorder::type_mp4, tuple, allArgs["customized_path"]); auto period = allArgs["period"]; //判断是获取mp4文件列表还是获取文件夹列表 diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 35c78957..e4b381ac 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -215,11 +215,11 @@ void do_http_hook(const string &url, const ArgsType &body, const function snprintf(url, sizeof(url), origin_fmt.data(), info._app.data(), info._streamid.data())) { + if ((ssize_t)origin_fmt.size() > snprintf(url, sizeof(url), origin_fmt.data(), info.app.data(), info.stream.data())) { WarnL << "get origin url failed, origin_fmt:" << origin_fmt; return ""; } // 告知源站这是来自边沿站的拉流请求,如果未找到流请立即返回拉流失败 - return string(url) + '?' + kEdgeServerParam + '&' + VHOST_KEY + '=' + info._vhost + '&' + info._param_strs; + return string(url) + '?' + kEdgeServerParam + '&' + VHOST_KEY + '=' + info.vhost + '&' + info.param_strs; } static void pullStreamFromOrigin(const vector &urls, size_t index, size_t failed_cnt, const MediaInfo &args, const function &closePlayer) { @@ -280,10 +280,10 @@ static void pullStreamFromOrigin(const vector &urls, size_t index, size_ InfoL << "pull stream from origin, failed_cnt: " << failed_cnt << ", timeout_sec: " << timeout_sec << ", url: " << url; ProtocolOption option; - option.enable_hls = option.enable_hls || (args._schema == HLS_SCHEMA); + option.enable_hls = option.enable_hls || (args.schema == HLS_SCHEMA); option.enable_mp4 = false; - addStreamProxy(args._vhost, args._app, args._streamid, url, retry_count, option, Rtsp::RTP_TCP, timeout_sec, [=](const SockException &ex, const string &key) mutable { + addStreamProxy(args.vhost, args.app, args.stream, url, retry_count, option, Rtsp::RTP_TCP, timeout_sec, [=](const SockException &ex, const string &key) mutable { if (!ex) { return; } @@ -321,7 +321,7 @@ void installWebHook() { NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) { GET_CONFIG(string, hook_publish, Hook::kOnPublish); - if (!hook_enable || args._param_strs == hook_adminparams || hook_publish.empty() || sender.get_peer_ip() == "127.0.0.1") { + if (!hook_enable || args.param_strs == hook_adminparams || hook_publish.empty() || sender.get_peer_ip() == "127.0.0.1") { invoker("", ProtocolOption()); return; } @@ -346,7 +346,7 @@ void installWebHook() { NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastMediaPlayed, [](BroadcastMediaPlayedArgs) { GET_CONFIG(string, hook_play, Hook::kOnPlay); - if (!hook_enable || args._param_strs == hook_adminparams || hook_play.empty() || sender.get_peer_ip() == "127.0.0.1") { + if (!hook_enable || args.param_strs == hook_adminparams || hook_play.empty() || sender.get_peer_ip() == "127.0.0.1") { invoker(""); return; } @@ -360,7 +360,7 @@ void installWebHook() { NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastFlowReport, [](BroadcastFlowReportArgs) { GET_CONFIG(string, hook_flowreport, Hook::kOnFlowReport); - if (!hook_enable || args._param_strs == hook_adminparams || hook_flowreport.empty() || sender.get_peer_ip() == "127.0.0.1") { + if (!hook_enable || args.param_strs == hook_adminparams || hook_flowreport.empty() || sender.get_peer_ip() == "127.0.0.1") { return; } auto body = make_json(args); @@ -379,7 +379,7 @@ void installWebHook() { // 监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问 NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastOnGetRtspRealm, [](BroadcastOnGetRtspRealmArgs) { GET_CONFIG(string, hook_rtsp_realm, Hook::kOnRtspRealm); - if (!hook_enable || args._param_strs == hook_adminparams || hook_rtsp_realm.empty() || sender.get_peer_ip() == "127.0.0.1") { + if (!hook_enable || args.param_strs == hook_adminparams || hook_rtsp_realm.empty() || sender.get_peer_ip() == "127.0.0.1") { // 无需认证 invoker(""); return; @@ -467,7 +467,7 @@ void installWebHook() { return; } - if (start_with(args._param_strs, kEdgeServerParam)) { + if (start_with(args.param_strs, kEdgeServerParam)) { // 源站收到来自边沿站的溯源请求,流不存在时立即返回拉流失败 closePlayer(); return; diff --git a/src/Common/Device.h b/src/Common/Device.h index 963fb0dc..cb99657d 100644 --- a/src/Common/Device.h +++ b/src/Common/Device.h @@ -47,10 +47,8 @@ public: using Ptr = std::shared_ptr; //fDuration<=0为直播,否则为点播 - DevChannel( - const std::string &vhost, const std::string &app, const std::string &stream_id, float duration = 0, - const ProtocolOption &option = ProtocolOption()) - : MultiMediaSourceMuxer(vhost, app, stream_id, duration, option) {} + DevChannel(const MediaTuple& tuple, float duration = 0, const ProtocolOption &option = ProtocolOption()) + : MultiMediaSourceMuxer(tuple, duration, option) {} ~DevChannel() override = default; /** diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 5e378124..fb3a21ce 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -105,7 +105,7 @@ ProtocolOption::ProtocolOption() { ////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct MediaSourceNull : public MediaSource { - MediaSourceNull() : MediaSource("schema", "vhost", "app", "stream") {}; + MediaSourceNull() : MediaSource("schema", MediaTuple{"vhost", "app", "stream"}) {}; int readerCount() override { return 0; } }; @@ -114,16 +114,12 @@ MediaSource &MediaSource::NullMediaSource() { return *s_null; } -MediaSource::MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id){ +MediaSource::MediaSource(const string &schema, const MediaTuple& tuple): _tuple(tuple) { GET_CONFIG(bool, enableVhost, General::kEnableVhost); - if (!enableVhost) { - _vhost = DEFAULT_VHOST; - } else { - _vhost = vhost.empty() ? DEFAULT_VHOST : vhost; + if (!enableVhost || _tuple.vhost.empty()) { + _tuple.vhost = DEFAULT_VHOST; } _schema = schema; - _app = app; - _stream_id = stream_id; _create_stamp = time(NULL); } @@ -136,16 +132,16 @@ const string& MediaSource::getSchema() const { } const string& MediaSource::getVhost() const { - return _vhost; + return _tuple.vhost; } const string& MediaSource::getApp() const { //获取该源的id - return _app; + return _tuple.app; } const string& MediaSource::getId() const { - return _stream_id; + return _tuple.stream; } std::shared_ptr MediaSource::getOwnership() { @@ -424,7 +420,7 @@ static MediaSource::Ptr find_l(const string &schema, const string &vhost_in, con static void findAsync_l(const MediaInfo &info, const std::shared_ptr &session, bool retry, const function &cb){ - auto src = find_l(info._schema, info._vhost, info._app, info._streamid, true); + auto src = find_l(info.schema, info.vhost, info.app, info.stream, true); if (src || !retry) { cb(src); return; @@ -459,10 +455,10 @@ static void findAsync_l(const MediaInfo &info, const std::shared_ptr &s weak_ptr weak_session = session; auto on_register = [weak_session, info, cb_once, cancel_all, poller](BroadcastMediaChangedArgs) { if (!bRegist || - sender.getSchema() != info._schema || - sender.getVhost() != info._vhost || - sender.getApp() != info._app || - sender.getId() != info._streamid) { + sender.getSchema() != info.schema || + sender.getVhost() != info.vhost || + sender.getApp() != info.app || + sender.getId() != info.stream) { //不是自己感兴趣的事件,忽略之 return; } @@ -527,7 +523,7 @@ void MediaSource::regist() { { //减小互斥锁临界区 lock_guard lock(s_media_source_mtx); - auto &ref = s_media_source_map[_schema][_vhost][_app][_stream_id]; + auto &ref = s_media_source_map[_schema][_tuple.vhost][_tuple.app][_tuple.stream]; auto src = ref.lock(); if (src) { if (src.get() == this) { @@ -570,7 +566,7 @@ bool MediaSource::unregist() { { //减小互斥锁临界区 lock_guard lock(s_media_source_mtx); - erase_media_source(ret, this, s_media_source_map, _schema, _vhost, _app, _stream_id); + erase_media_source(ret, this, s_media_source_map, _schema, _tuple.vhost, _tuple.app, _tuple.stream); } if (ret) { @@ -582,31 +578,31 @@ bool MediaSource::unregist() { /////////////////////////////////////MediaInfo////////////////////////////////////// void MediaInfo::parse(const std::string &url_in){ - _full_url = url_in; + full_url = url_in; auto url = url_in; auto pos = url.find("?"); if (pos != string::npos) { - _param_strs = url.substr(pos + 1); + param_strs = url.substr(pos + 1); url.erase(pos); } auto schema_pos = url.find("://"); if (schema_pos != string::npos) { - _schema = url.substr(0, schema_pos); + schema = url.substr(0, schema_pos); } else { schema_pos = -3; } auto split_vec = split(url.substr(schema_pos + 3), "/"); if (split_vec.size() > 0) { - splitUrl(split_vec[0], _host, _port); - _vhost = _host; - if (_vhost == "localhost" || isIP(_vhost.data())) { + splitUrl(split_vec[0], host, port); + vhost = host; + if (vhost == "localhost" || isIP(vhost.data())) { //如果访问的是localhost或ip,那么则为默认虚拟主机 - _vhost = DEFAULT_VHOST; + vhost = DEFAULT_VHOST; } } if (split_vec.size() > 1) { - _app = split_vec[1]; + app = split_vec[1]; } if (split_vec.size() > 2) { string stream_id; @@ -616,18 +612,18 @@ void MediaInfo::parse(const std::string &url_in){ if (stream_id.back() == '/') { stream_id.pop_back(); } - _streamid = stream_id; + stream = stream_id; } - auto params = Parser::parseArgs(_param_strs); + auto params = Parser::parseArgs(param_strs); if (params.find(VHOST_KEY) != params.end()) { - _vhost = params[VHOST_KEY]; + vhost = params[VHOST_KEY]; } GET_CONFIG(bool, enableVhost, General::kEnableVhost); - if (!enableVhost || _vhost.empty()) { + if (!enableVhost || vhost.empty()) { //如果关闭虚拟主机或者虚拟主机为空,则设置虚拟主机为默认 - _vhost = DEFAULT_VHOST; + vhost = DEFAULT_VHOST; } } diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index c06a7c83..df855293 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -252,24 +252,20 @@ private: /** * 解析url获取媒体相关信息 */ -class MediaInfo { +class MediaInfo: public MediaTuple { public: ~MediaInfo() = default; MediaInfo() = default; MediaInfo(const std::string &url) { parse(url); } void parse(const std::string &url); - std::string shortUrl() const { return _vhost + "/" + _app + "/" + _streamid; } - std::string getUrl() const { return _schema + "://" + shortUrl(); } + std::string getUrl() const { return schema + "://" + shortUrl(); } public: - uint16_t _port = 0; - std::string _full_url; - std::string _schema; - std::string _host; - std::string _vhost; - std::string _app; - std::string _streamid; - std::string _param_strs; + uint16_t port = 0; + std::string full_url; + std::string schema; + std::string host; + std::string param_strs; }; /** @@ -280,7 +276,7 @@ public: static MediaSource& NullMediaSource(); using Ptr = std::shared_ptr; - MediaSource(const std::string &schema, const std::string &vhost, const std::string &app, const std::string &stream_id); + MediaSource(const std::string &schema, const MediaTuple& tuple); virtual ~MediaSource(); ////////////////获取MediaSource相关信息//////////////// @@ -294,7 +290,11 @@ public: // 流id const std::string& getId() const; - std::string shortUrl() const { return _vhost + "/" + _app + "/" + _stream_id; } + const MediaTuple& getMediaTuple() const { + return _tuple; + } + + std::string shortUrl() const { return _tuple.shortUrl(); } std::string getUrl() const { return _schema + "://" + shortUrl(); } @@ -369,7 +369,7 @@ public: // 同步查找流 static Ptr find(const std::string &schema, const std::string &vhost, const std::string &app, const std::string &id, bool from_mp4 = false); static Ptr find(const MediaInfo &info, bool from_mp4 = false) { - return find(info._schema, info._vhost, info._app, info._streamid, from_mp4); + return find(info.schema, info.vhost, info.app, info.stream, from_mp4); } // 忽略schema,同步查找流,可能返回rtmp/rtsp/hls类型 @@ -394,15 +394,13 @@ private: protected: toolkit::BytesSpeed _speed[TrackMax]; + MediaTuple _tuple; private: std::atomic_flag _owned { false }; time_t _create_stamp; toolkit::Ticker _ticker; std::string _schema; - std::string _vhost; - std::string _app; - std::string _stream_id; std::weak_ptr _listener; // 对象个数统计 toolkit::ObjectStatistic _statistic; diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index 440adcc1..75c79c27 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -25,7 +25,7 @@ namespace { class MediaSourceForMuxer : public MediaSource { public: MediaSourceForMuxer(const MultiMediaSourceMuxer::Ptr &muxer) - : MediaSource("muxer", muxer->getVhost(), muxer->getApp(), muxer->getStreamId()) { + : MediaSource("muxer", muxer->getMediaTuple()) { MediaSource::setListener(muxer); } int readerCount() override { return 0; } @@ -33,7 +33,7 @@ public: } // namespace static std::shared_ptr makeRecorder(MediaSource &sender, const vector &tracks, Recorder::type type, const ProtocolOption &option){ - auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId(), option); + auto recorder = Recorder::createRecorder(type, sender.getMediaTuple(), option); for (auto &track : tracks) { recorder->addTrack(track); } @@ -71,15 +71,15 @@ static string getTrackInfoStr(const TrackSource *track_src){ } const std::string &MultiMediaSourceMuxer::getVhost() const { - return _vhost; + return _tuple.vhost; } const std::string &MultiMediaSourceMuxer::getApp() const { - return _app; + return _tuple.app; } const std::string &MultiMediaSourceMuxer::getStreamId() const { - return _stream_id; + return _tuple.stream; } std::string MultiMediaSourceMuxer::shortUrl() const { @@ -87,35 +87,32 @@ std::string MultiMediaSourceMuxer::shortUrl() const { if (!ret.empty()) { return ret; } - return _vhost + "/" + _app + "/" + _stream_id; + return _tuple.shortUrl(); } -MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost, const string &app, const string &stream, float dur_sec, const ProtocolOption &option) { +MultiMediaSourceMuxer::MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_sec, const ProtocolOption &option): _tuple(tuple) { _poller = EventPollerPool::Instance().getPoller(); _create_in_poller = _poller->isCurrentThread(); - _vhost = vhost; - _app = app; - _stream_id = stream; _option = option; if (option.enable_rtmp) { - _rtmp = std::make_shared(vhost, app, stream, option, std::make_shared(dur_sec)); + _rtmp = std::make_shared(_tuple, option, std::make_shared(dur_sec)); } if (option.enable_rtsp) { - _rtsp = std::make_shared(vhost, app, stream, option, std::make_shared(dur_sec)); + _rtsp = std::make_shared(_tuple, option, std::make_shared(dur_sec)); } if (option.enable_hls) { - _hls = dynamic_pointer_cast(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream, option)); + _hls = dynamic_pointer_cast(Recorder::createRecorder(Recorder::type_hls, _tuple, option)); } if (option.enable_mp4) { - _mp4 = Recorder::createRecorder(Recorder::type_mp4, vhost, app, stream, option); + _mp4 = Recorder::createRecorder(Recorder::type_mp4, _tuple, option); } if (option.enable_ts) { - _ts = std::make_shared(vhost, app, stream, option); + _ts = std::make_shared(_tuple, option); } #if defined(ENABLE_MP4) if (option.enable_fmp4) { - _fmp4 = std::make_shared(vhost, app, stream, option); + _fmp4 = std::make_shared(_tuple, option); } #endif diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index 77585f84..0e4f6a5b 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -37,7 +37,7 @@ public: virtual void onAllTrackReady() = 0; }; - MultiMediaSourceMuxer(const std::string &vhost, const std::string &app, const std::string &stream, float dur_sec = 0.0,const ProtocolOption &option = ProtocolOption()); + MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_sec = 0.0,const ProtocolOption &option = ProtocolOption()); ~MultiMediaSourceMuxer() override = default; /** @@ -129,6 +129,9 @@ public: const std::string& getVhost() const; const std::string& getApp() const; const std::string& getStreamId() const; + const MediaTuple& getMediaTuple() const { + return _tuple; + } std::string shortUrl() const; protected: @@ -159,9 +162,7 @@ private: bool _is_enable = false; bool _create_in_poller = false; bool _video_key_pos = false; - std::string _vhost; - std::string _app; - std::string _stream_id; + MediaTuple _tuple; ProtocolOption _option; toolkit::Ticker _last_check; Stamp _stamp[2]; diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index 7986a08c..53359e30 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -39,10 +39,8 @@ public: using RingDataType = std::shared_ptr >; using RingType = toolkit::RingBuffer; - FMP4MediaSource(const std::string &vhost, - const std::string &app, - const std::string &stream_id, - int ring_size = FMP4_GOP_SIZE) : MediaSource(FMP4_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {} + FMP4MediaSource(const MediaTuple& tuple, + int ring_size = FMP4_GOP_SIZE) : MediaSource(FMP4_SCHEMA, tuple), _ring_size(ring_size) {} ~FMP4MediaSource() override { flush(); } diff --git a/src/FMP4/FMP4MediaSourceMuxer.h b/src/FMP4/FMP4MediaSourceMuxer.h index ad16f352..47ea5fd0 100644 --- a/src/FMP4/FMP4MediaSourceMuxer.h +++ b/src/FMP4/FMP4MediaSourceMuxer.h @@ -23,12 +23,9 @@ class FMP4MediaSourceMuxer final : public MP4MuxerMemory, public MediaSourceEven public: using Ptr = std::shared_ptr; - FMP4MediaSourceMuxer(const std::string &vhost, - const std::string &app, - const std::string &stream_id, - const ProtocolOption &option) { + FMP4MediaSourceMuxer(const MediaTuple& tuple, const ProtocolOption &option) { _option = option; - _media_src = std::make_shared(vhost, app, stream_id); + _media_src = std::make_shared(tuple); } ~FMP4MediaSourceMuxer() override { MP4MuxerMemory::flush(); }; diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index fbd9c174..92f349fc 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -278,7 +278,7 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo HttpCookieManager::Instance().delCookie(cookie); } - bool is_hls = media_info._schema == HLS_SCHEMA; + bool is_hls = media_info.schema == HLS_SCHEMA; SockInfoImp::Ptr info = std::make_shared(); info->_identifier = sender.getIdentifier(); @@ -363,8 +363,8 @@ static void accessFile(Session &sender, const Parser &parser, const MediaInfo &m } if (is_hls) { // hls,那么移除掉后缀获取真实的stream_id并且修改协议为HLS - const_cast(media_info._schema) = HLS_SCHEMA; - replace(const_cast(media_info._streamid), kHlsSuffix, ""); + const_cast(media_info.schema) = HLS_SCHEMA; + replace(const_cast(media_info.stream), kHlsSuffix, ""); } weak_ptr weakSession = static_pointer_cast(sender.shared_from_this()); @@ -465,11 +465,11 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, Sess }); string url, path; - auto it = virtualPathMap.find(media_info._app); + auto it = virtualPathMap.find(media_info.app); if (it != virtualPathMap.end()) { //访问的是virtualPath path = it->second; - url = parser.Url().substr(1 + media_info._app.size()); + url = parser.Url().substr(1 + media_info.app.size()); } else { //访问的是rootPath path = rootPath; @@ -481,7 +481,7 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, Sess ch = '/'; } } - auto ret = File::absolutePath(enableVhost ? media_info._vhost + url : url, path); + auto ret = File::absolutePath(enableVhost ? media_info.vhost + url : url, path); NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast(sender)); return ret; } diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index c1a045f6..9f2ce375 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -202,7 +202,7 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffi //解析带上协议+参数完整的url _mediaInfo.parse(schema + "://" + _parser["Host"] + url); - if (_mediaInfo._app.empty() || _mediaInfo._streamid.empty()) { + if (_mediaInfo.app.empty() || _mediaInfo.stream.empty()) { //url不合法 return false; } diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index d1f414d3..36b4d850 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -29,9 +29,9 @@ PlayerProxy::PlayerProxy( const EventPoller::Ptr &poller, int reconnect_delay_min, int reconnect_delay_max, int reconnect_delay_step) : MediaPlayer(poller) , _option(option) { - _vhost = vhost; - _app = app; - _stream_id = stream_id; + _tuple.vhost = vhost; + _tuple.app = app; + _tuple.stream = stream_id; _retry_count = retry_count; setOnClose(nullptr); @@ -187,11 +187,11 @@ void PlayerProxy::setDirectProxy() { // rtsp拉流 GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy); if (directProxy) { - mediaSource = std::make_shared(_vhost, _app, _stream_id); + mediaSource = std::make_shared(_tuple); } } else if (dynamic_pointer_cast(_delegate)) { // rtmp拉流,rtmp强制直接代理 - mediaSource = std::make_shared(_vhost, _app, _stream_id); + mediaSource = std::make_shared(_tuple); } if (mediaSource) { setMediaSource(mediaSource); @@ -277,18 +277,18 @@ void PlayerProxy::onPlaySuccess() { // rtsp拉流代理 if (reset_when_replay || !_muxer) { _option.enable_rtsp = false; - _muxer = std::make_shared(_vhost, _app, _stream_id, getDuration(), _option); + _muxer = std::make_shared(_tuple, getDuration(), _option); } } else if (dynamic_pointer_cast(_media_src)) { // rtmp拉流代理 if (reset_when_replay || !_muxer) { _option.enable_rtmp = false; - _muxer = std::make_shared(_vhost, _app, _stream_id, getDuration(), _option); + _muxer = std::make_shared(_tuple, getDuration(), _option); } } else { // 其他拉流代理 if (reset_when_replay || !_muxer) { - _muxer = std::make_shared(_vhost, _app, _stream_id, getDuration(), _option); + _muxer = std::make_shared(_tuple, getDuration(), _option); } } _muxer->setMediaListener(shared_from_this()); diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 9cf621da..a0d1cf32 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -134,9 +134,7 @@ private: int _reconnect_delay_min; int _reconnect_delay_max; int _reconnect_delay_step; - std::string _vhost; - std::string _app; - std::string _stream_id; + MediaTuple _tuple; std::string _pull_url; toolkit::Timer::Ptr _timer; std::function _on_disconnect; diff --git a/src/Record/HlsMakerImp.cpp b/src/Record/HlsMakerImp.cpp index 865561b7..4a140ac6 100644 --- a/src/Record/HlsMakerImp.cpp +++ b/src/Record/HlsMakerImp.cpp @@ -163,10 +163,10 @@ std::shared_ptr HlsMakerImp::makeFile(const string &file, bool setbuf) { } void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) { - _media_src = std::make_shared(vhost, app, stream_id); _info.app = app; _info.stream = stream_id; _info.vhost = vhost; + _media_src = std::make_shared(_info); } HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const { diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index 4211eae4..a1149a7f 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -25,8 +25,7 @@ public: using RingType = toolkit::RingBuffer; using Ptr = std::shared_ptr; - HlsMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id) - : MediaSource(HLS_SCHEMA, vhost, app, stream_id) {} + HlsMediaSource(const MediaTuple& tuple): MediaSource(HLS_SCHEMA, tuple) {} ~HlsMediaSource() override = default; /** diff --git a/src/Record/HlsRecorder.h b/src/Record/HlsRecorder.h index 12ff668e..1ec13f86 100644 --- a/src/Record/HlsRecorder.h +++ b/src/Record/HlsRecorder.h @@ -35,8 +35,8 @@ public: ~HlsRecorder() { MpegMuxer::flush(); }; - void setMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id) { - _hls->setMediaSource(vhost, app, stream_id); + void setMediaSource(const MediaTuple& tuple) { + _hls->setMediaSource(tuple.vhost, tuple.app, tuple.stream); } void setListener(const std::weak_ptr &listener) { diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index 35e28ac8..2d4105e0 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -20,17 +20,18 @@ using namespace toolkit; namespace mediakit { -MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) { +MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, const string &file_path) { //读写文件建议放在后台线程 + auto tuple = MediaTuple{vhost, app, stream_id}; _poller = WorkThreadPool::Instance().getPoller(); _file_path = file_path; if (_file_path.empty()) { GET_CONFIG(string, recordPath, Protocol::kMP4SavePath); GET_CONFIG(bool, enableVhost, General::kEnableVhost); if (enableVhost) { - _file_path = vhost + "/" + app + "/" + stream_id; + _file_path = tuple.shortUrl(); } else { - _file_path = app + "/" + stream_id; + _file_path = tuple.app + "/" + tuple.stream; } _file_path = File::absolutePath(_file_path, recordPath); } @@ -38,14 +39,14 @@ MP4Reader::MP4Reader(const string &vhost, const string &app, const string &strea _demuxer = std::make_shared(); _demuxer->openMP4(_file_path); - if (stream_id.empty()) { + if (tuple.stream.empty()) { return; } ProtocolOption option; //读取mp4文件并流化时,不重复生成mp4/hls文件 option.enable_mp4 = false; option.enable_hls = false; - _muxer = std::make_shared(vhost, app, stream_id, _demuxer->getDurationMS() / 1000.0f, option); + _muxer = std::make_shared(tuple, _demuxer->getDurationMS() / 1000.0f, option); auto tracks = _demuxer->getTracks(false); if (tracks.empty()) { throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path); diff --git a/src/Record/Recorder.cpp b/src/Record/Recorder.cpp index f3e91db5..d9cd75f7 100644 --- a/src/Record/Recorder.cpp +++ b/src/Record/Recorder.cpp @@ -21,16 +21,16 @@ using namespace toolkit; namespace mediakit { -string Recorder::getRecordPath(Recorder::type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path) { +string Recorder::getRecordPath(Recorder::type type, const MediaTuple& tuple, const string &customized_path) { GET_CONFIG(bool, enableVhost, General::kEnableVhost); switch (type) { case Recorder::type_hls: { GET_CONFIG(string, hlsPath, Protocol::kHlsSavePath); string m3u8FilePath; if (enableVhost) { - m3u8FilePath = vhost + "/" + app + "/" + stream_id + "/hls.m3u8"; + m3u8FilePath = tuple.shortUrl() + "/hls.m3u8"; } else { - m3u8FilePath = app + "/" + stream_id + "/hls.m3u8"; + m3u8FilePath = tuple.app + "/" + tuple.stream + "/hls.m3u8"; } //Here we use the customized file path. if (!customized_path.empty()) { @@ -43,9 +43,9 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s GET_CONFIG(string, recordAppName, Record::kAppName); string mp4FilePath; if (enableVhost) { - mp4FilePath = vhost + "/" + recordAppName + "/" + app + "/" + stream_id + "/"; + mp4FilePath = tuple.vhost + "/" + recordAppName + "/" + tuple.app + "/" + tuple.stream + "/"; } else { - mp4FilePath = recordAppName + "/" + app + "/" + stream_id + "/"; + mp4FilePath = recordAppName + "/" + tuple.app + "/" + tuple.stream + "/"; } //Here we use the customized file path. if (!customized_path.empty()) { @@ -58,14 +58,14 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s } } -std::shared_ptr Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option){ +std::shared_ptr Recorder::createRecorder(type type, const MediaTuple& tuple, const ProtocolOption &option){ switch (type) { case Recorder::type_hls: { #if defined(ENABLE_HLS) - auto path = Recorder::getRecordPath(type, vhost, app, stream_id, option.hls_save_path); + auto path = Recorder::getRecordPath(type, tuple, option.hls_save_path); GET_CONFIG(bool, enable_vhost, General::kEnableVhost); - auto ret = std::make_shared(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "", option); - ret->setMediaSource(vhost, app, stream_id); + auto ret = std::make_shared(path, enable_vhost ? string(VHOST_KEY) + "=" + tuple.vhost : "", option); + ret->setMediaSource(tuple); return ret; #else throw std::invalid_argument("hls相关功能未打开,请开启ENABLE_HLS宏后编译再测试"); @@ -75,8 +75,8 @@ std::shared_ptr Recorder::createRecorder(type type, const st case Recorder::type_mp4: { #if defined(ENABLE_MP4) - auto path = Recorder::getRecordPath(type, vhost, app, stream_id, option.mp4_save_path); - return std::make_shared(path, vhost, app, stream_id, option.mp4_max_second); + auto path = Recorder::getRecordPath(type, tuple, option.mp4_save_path); + return std::make_shared(path, tuple.vhost, tuple.app, tuple.stream, option.mp4_max_second); #else throw std::invalid_argument("mp4相关功能未打开,请开启ENABLE_MP4宏后编译再测试"); #endif diff --git a/src/Record/Recorder.h b/src/Record/Recorder.h index 491088ff..7ccbc04e 100644 --- a/src/Record/Recorder.h +++ b/src/Record/Recorder.h @@ -18,7 +18,16 @@ namespace mediakit { class MediaSinkInterface; class ProtocolOption; -class RecordInfo { +struct MediaTuple { + std::string vhost; + std::string app; + std::string stream; + std::string shortUrl() const { + return vhost + '/' + app + '/' + stream; + } +}; + +class RecordInfo: public MediaTuple { public: time_t start_time; // GMT 标准时间,单位秒 float time_len; // 录像长度,单位秒 @@ -27,9 +36,6 @@ public: std::string file_name; // 文件名称 std::string folder; // 文件夹路径 std::string url; // 播放路径 - std::string app; // 应用名称 - std::string stream; // 流 ID - std::string vhost; // 虚拟主机 }; class Recorder{ @@ -50,7 +56,7 @@ public: * @param customized_path 录像文件保存自定义根目录,为空则采用配置文件设置 * @return 录制文件绝对路径 */ - static std::string getRecordPath(type type, const std::string &vhost, const std::string &app, const std::string &stream_id,const std::string &customized_path = ""); + static std::string getRecordPath(type type, const MediaTuple& tuple, const std::string &customized_path = ""); /** * 创建录制器对象 @@ -62,7 +68,7 @@ public: * @param max_second mp4录制最大切片时间,单位秒,置0则采用配置文件配置 * @return 对象指针,可能为nullptr */ - static std::shared_ptr createRecorder(type type, const std::string &vhost, const std::string &app, const std::string &stream_id, const ProtocolOption &option); + static std::shared_ptr createRecorder(type type, const MediaTuple& tuple, const ProtocolOption &option); private: Recorder() = delete; diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index d91393b0..5c862a36 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -46,12 +46,7 @@ public: * @param stream_id 流id * @param ring_size 可以设置固定的环形缓冲大小,0则自适应 */ - RtmpMediaSource(const std::string &vhost, - const std::string &app, - const std::string &stream_id, - int ring_size = RTMP_GOP_SIZE) : - MediaSource(RTMP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) { - } + RtmpMediaSource(const MediaTuple& tuple, int ring_size = RTMP_GOP_SIZE): MediaSource(RTMP_SCHEMA, tuple), _ring_size(ring_size) {} ~RtmpMediaSource() override { flush(); } diff --git a/src/Rtmp/RtmpMediaSourceImp.cpp b/src/Rtmp/RtmpMediaSourceImp.cpp index 7777a0fa..ce64226a 100644 --- a/src/Rtmp/RtmpMediaSourceImp.cpp +++ b/src/Rtmp/RtmpMediaSourceImp.cpp @@ -63,7 +63,7 @@ void RtmpMediaSource::onWrite(RtmpPacket::Ptr pkt, bool /*= true*/) } -RtmpMediaSourceImp::RtmpMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize) : RtmpMediaSource(vhost, app, id, ringSize) +RtmpMediaSourceImp::RtmpMediaSourceImp(const MediaTuple& tuple, int ringSize) : RtmpMediaSource(tuple, ringSize) { _demuxer = std::make_shared(); _demuxer->setTrackListener(this); @@ -99,7 +99,7 @@ void RtmpMediaSourceImp::setProtocolOption(const ProtocolOption &option) _option = option; //不重复生成rtmp协议 _option.enable_rtmp = false; - _muxer = std::make_shared(getVhost(), getApp(), getId(), _demuxer->getDuration(), _option); + _muxer = std::make_shared(_tuple, _demuxer->getDuration(), _option); _muxer->setMediaListener(getListener()); _muxer->setTrackListener(std::static_pointer_cast(shared_from_this())); //让_muxer对象拦截一部分事件(比如说录像相关事件) diff --git a/src/Rtmp/RtmpMediaSourceImp.h b/src/Rtmp/RtmpMediaSourceImp.h index c6382c8d..31fa24fe 100644 --- a/src/Rtmp/RtmpMediaSourceImp.h +++ b/src/Rtmp/RtmpMediaSourceImp.h @@ -35,7 +35,7 @@ public: * @param id 流id * @param ringSize 环形缓存大小 */ - RtmpMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTMP_GOP_SIZE); + RtmpMediaSourceImp(const MediaTuple& tuple, int ringSize = RTMP_GOP_SIZE); ~RtmpMediaSourceImp() override = default; diff --git a/src/Rtmp/RtmpMediaSourceMuxer.h b/src/Rtmp/RtmpMediaSourceMuxer.h index 70f3fffe..51f8b46c 100644 --- a/src/Rtmp/RtmpMediaSourceMuxer.h +++ b/src/Rtmp/RtmpMediaSourceMuxer.h @@ -21,13 +21,11 @@ class RtmpMediaSourceMuxer final : public RtmpMuxer, public MediaSourceEventInte public: using Ptr = std::shared_ptr; - RtmpMediaSourceMuxer(const std::string &vhost, - const std::string &strApp, - const std::string &strId, + RtmpMediaSourceMuxer(const MediaTuple& tuple, const ProtocolOption &option, const TitleMeta::Ptr &title = nullptr) : RtmpMuxer(title) { _option = option; - _media_src = std::make_shared(vhost, strApp, strId); + _media_src = std::make_shared(tuple); getRtmpRing()->setDelegate(_media_src); } diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 15b8a89f..56a799cb 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -84,7 +84,7 @@ void RtmpSession::onCmd_connect(AMFDecoder &dec) { auto tc_url = params["tcUrl"].as_string(); if (tc_url.empty()) { // defaultVhost:默认vhost - tc_url = string(RTMP_SCHEMA) + "://" + DEFAULT_VHOST + "/" + _media_info._app; + tc_url = string(RTMP_SCHEMA) + "://" + DEFAULT_VHOST + "/" + _media_info.app; } else { auto pos = tc_url.rfind('?'); if (pos != string::npos) { @@ -94,9 +94,9 @@ void RtmpSession::onCmd_connect(AMFDecoder &dec) { } // 初步解析,只用于获取vhost信息 _media_info.parse(tc_url); - _media_info._schema = RTMP_SCHEMA; + _media_info.schema = RTMP_SCHEMA; // 赋值rtmp app - _media_info._app = params["app"].as_string(); + _media_info.app = params["app"].as_string(); bool ok = true; //(app == APP_NAME); AMFValue version(AMF_OBJECT); @@ -109,7 +109,7 @@ void RtmpSession::onCmd_connect(AMFDecoder &dec) { status.set("objectEncoding", params["objectEncoding"]); sendReply(ok ? "_result" : "_error", version, status); if (!ok) { - throw std::runtime_error("Unsupported application: " + _media_info._app); + throw std::runtime_error("Unsupported application: " + _media_info.app); } AMFEncoder invoke; @@ -132,9 +132,9 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { })); dec.load();/* NULL */ // 赋值为rtmp stream id 信息 - _media_info._streamid = getStreamId(dec.load()); + _media_info.stream = getStreamId(dec.load()); // 再解析url,切割url为app/stream_id (不一定符合rtmp url切割规范) - _media_info.parse(_media_info._schema + "://" + _media_info._vhost + '/' + _media_info._app + '/' + _media_info._streamid); + _media_info.parse(_media_info.getUrl()); auto now_stream_index = _now_stream_index; auto on_res = [this, token, now_stream_index](const string &err, const ProtocolOption &option) { @@ -149,7 +149,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { } assert(!_push_src); - auto src = MediaSource::find(RTMP_SCHEMA, _media_info._vhost, _media_info._app, _media_info._streamid); + auto src = MediaSource::find(RTMP_SCHEMA, _media_info.vhost, _media_info.app, _media_info.stream); auto push_failed = (bool)src; while (src) { @@ -180,7 +180,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { } if (!_push_src) { - _push_src = std::make_shared(_media_info._vhost, _media_info._app, _media_info._streamid); + _push_src = std::make_shared(_media_info); //获取所有权 _push_src_ownership = _push_src->getOwnership(); _push_src->setProtocolOption(option); @@ -196,7 +196,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { setSocketFlags(); }; - if(_media_info._app.empty() || _media_info._streamid.empty()){ + if(_media_info.app.empty() || _media_info.stream.empty()){ //不允许莫名其妙的推流url on_res("rtmp推流url非法", ProtocolOption()); return; @@ -256,7 +256,7 @@ void RtmpSession::sendPlayResponse(const string &err, const RtmpMediaSource::Ptr sendStatus({ "level", (ok ? "status" : "error"), "code", (ok ? "NetStream.Play.Reset" : (auth_success ? "NetStream.Play.StreamNotFound" : "NetStream.Play.BadAuth")), "description", (ok ? "Resetting and playing." : (auth_success ? "No such stream." : err.data())), - "details", _media_info._streamid, + "details", _media_info.stream, "clientid", "0" }); if (!ok) { @@ -270,7 +270,7 @@ void RtmpSession::sendPlayResponse(const string &err, const RtmpMediaSource::Ptr sendStatus({ "level", "status", "code", "NetStream.Play.Start", "description", "Started playing." , - "details", _media_info._streamid, + "details", _media_info.stream, "clientid", "0"}); // |RtmpSampleAccess(true, true) @@ -289,7 +289,7 @@ void RtmpSession::sendPlayResponse(const string &err, const RtmpMediaSource::Ptr sendStatus({ "level", "status", "code", "NetStream.Play.PublishNotify", "description", "Now published." , - "details", _media_info._streamid, + "details", _media_info.stream, "clientid", "0"}); auto &metadata = src->getMetaData(); @@ -433,9 +433,9 @@ string RtmpSession::getStreamId(const string &str){ void RtmpSession::onCmd_play(AMFDecoder &dec) { dec.load(); /* NULL */ // 赋值为rtmp stream id 信息 - _media_info._streamid = getStreamId(dec.load()); + _media_info.stream = getStreamId(dec.load()); // 再解析url,切割url为app/stream_id (不一定符合rtmp url切割规范) - _media_info.parse(_media_info._schema + "://" + _media_info._vhost + '/' + _media_info._app + '/' + _media_info._streamid); + _media_info.parse(_media_info.getUrl()); doPlay(dec); } @@ -589,7 +589,7 @@ MediaOriginType RtmpSession::getOriginType(MediaSource &sender) const{ } string RtmpSession::getOriginUrl(MediaSource &sender) const { - return _media_info._full_url; + return _media_info.full_url; } std::shared_ptr RtmpSession::getOriginSock(MediaSource &sender) const { diff --git a/src/Rtp/GB28181Process.cpp b/src/Rtp/GB28181Process.cpp index b4f93f75..204d98f8 100644 --- a/src/Rtp/GB28181Process.cpp +++ b/src/Rtp/GB28181Process.cpp @@ -134,7 +134,7 @@ bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) { // 设置dump目录 GET_CONFIG(string, dump_dir, RtpProxy::kDumpDir); if (!dump_dir.empty()) { - auto save_path = File::absolutePath(_media_info._streamid + ".mpeg", dump_dir); + auto save_path = File::absolutePath(_media_info.stream + ".mpeg", dump_dir); _save_file_ps.reset(File::create_file(save_path.data(), "wb"), [](FILE *fp) { if (fp) { fclose(fp); @@ -171,11 +171,11 @@ void GB28181Process::onRtpDecode(const Frame::Ptr &frame) { // 创建解码器 if (checkTS((uint8_t *)frame->data(), frame->size())) { // 猜测是ts负载 - InfoL << _media_info._streamid << " judged to be TS"; + InfoL << _media_info.stream << " judged to be TS"; _decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, _interface); } else { // 猜测是ps负载 - InfoL << _media_info._streamid << " judged to be PS"; + InfoL << _media_info.stream << " judged to be PS"; _decoder = DecoderImp::createDecoder(DecoderImp::decoder_ps, _interface); } } diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 8b6bcb3d..9700e09c 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -26,14 +26,14 @@ static constexpr size_t kMaxCachedFrame = 200; namespace mediakit { RtpProcess::RtpProcess(const string &stream_id) { - _media_info._schema = kRtpAppName; - _media_info._vhost = DEFAULT_VHOST; - _media_info._app = kRtpAppName; - _media_info._streamid = stream_id; + _media_info.schema = kRtpAppName; + _media_info.vhost = DEFAULT_VHOST; + _media_info.app = kRtpAppName; + _media_info.stream = stream_id; GET_CONFIG(string, dump_dir, RtpProxy::kDumpDir); { - FILE *fp = !dump_dir.empty() ? File::create_file(File::absolutePath(_media_info._streamid + ".rtp", dump_dir).data(), "wb") : nullptr; + FILE *fp = !dump_dir.empty() ? File::create_file(File::absolutePath(_media_info.stream + ".rtp", dump_dir).data(), "wb") : nullptr; if (fp) { _save_file_rtp.reset(fp, [](FILE *fp) { fclose(fp); @@ -42,7 +42,7 @@ RtpProcess::RtpProcess(const string &stream_id) { } { - FILE *fp = !dump_dir.empty() ? File::create_file(File::absolutePath(_media_info._streamid + ".video", dump_dir).data(), "wb") : nullptr; + FILE *fp = !dump_dir.empty() ? File::create_file(File::absolutePath(_media_info.stream + ".video", dump_dir).data(), "wb") : nullptr; if (fp) { _save_file_video.reset(fp, [](FILE *fp) { fclose(fp); @@ -234,7 +234,7 @@ uint16_t RtpProcess::get_local_port() { } string RtpProcess::getIdentifier() const { - return _media_info._streamid; + return _media_info.stream; } void RtpProcess::emitOnPublish() { @@ -251,9 +251,7 @@ void RtpProcess::emitOnPublish() { return; } if (err.empty()) { - strong_self->_muxer = std::make_shared(strong_self->_media_info._vhost, - strong_self->_media_info._app, - strong_self->_media_info._streamid,0.0f, + strong_self->_muxer = std::make_shared(strong_self->_media_info, 0.0f, option); if (strong_self->_only_audio) { strong_self->_muxer->setOnlyAudio(); @@ -291,7 +289,7 @@ toolkit::EventPoller::Ptr RtpProcess::getOwnerPoller(MediaSource &sender) { if (_sock) { return _sock->getPoller(); } - throw std::runtime_error("RtpProcess::getOwnerPoller failed:" + _media_info._streamid); + throw std::runtime_error("RtpProcess::getOwnerPoller failed:" + _media_info.stream); } float RtpProcess::getLossRate(MediaSource &sender, TrackType type) { diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index b6dc3d46..f70396d8 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -42,11 +42,7 @@ public: * @param stream_id 流id * @param ring_size 可以设置固定的环形缓冲大小,0则自适应 */ - RtspMediaSource(const std::string &vhost, - const std::string &app, - const std::string &stream_id, - int ring_size = RTP_GOP_SIZE) : - MediaSource(RTSP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {} + RtspMediaSource(const MediaTuple& tuple, int ring_size = RTP_GOP_SIZE): MediaSource(RTSP_SCHEMA, tuple), _ring_size(ring_size) {} ~RtspMediaSource() override { flush(); } @@ -76,7 +72,7 @@ public: return _sdp; } - virtual RtspMediaSource::Ptr Clone(const std::string& stream) { + virtual RtspMediaSource::Ptr clone(const std::string& stream) { return nullptr; } diff --git a/src/Rtsp/RtspMediaSourceImp.cpp b/src/Rtsp/RtspMediaSourceImp.cpp index b10b071c..ea630415 100644 --- a/src/Rtsp/RtspMediaSourceImp.cpp +++ b/src/Rtsp/RtspMediaSourceImp.cpp @@ -74,8 +74,7 @@ void RtspMediaSource::onWrite(RtpPacket::Ptr rtp, bool keyPos) { PacketCache::inputPacket(stamp, is_video, std::move(rtp), keyPos); } -RtspMediaSourceImp::RtspMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize) - : RtspMediaSource(vhost, app, id, ringSize) +RtspMediaSourceImp::RtspMediaSourceImp(const MediaTuple& tuple, int ringSize): RtspMediaSource(tuple, ringSize) { _demuxer = std::make_shared(); _demuxer->setTrackListener(this); @@ -114,7 +113,7 @@ void RtspMediaSourceImp::setProtocolOption(const ProtocolOption &option) //导致rtc无法播放,所以在rtsp推流rtc播放时,建议关闭直接代理模式 _option = option; _option.enable_rtsp = !direct_proxy; - _muxer = std::make_shared(getVhost(), getApp(), getId(), _demuxer->getDuration(), _option); + _muxer = std::make_shared(_tuple, _demuxer->getDuration(), _option); _muxer->setMediaListener(getListener()); _muxer->setTrackListener(std::static_pointer_cast(shared_from_this())); //让_muxer对象拦截一部分事件(比如说录像相关事件) @@ -126,8 +125,10 @@ void RtspMediaSourceImp::setProtocolOption(const ProtocolOption &option) } } -RtspMediaSource::Ptr RtspMediaSourceImp::Clone(const std::string &stream) { - auto src_imp = std::make_shared(getVhost(), getApp(), stream); +RtspMediaSource::Ptr RtspMediaSourceImp::clone(const std::string &stream) { + auto tuple = _tuple; + tuple.stream = stream; + auto src_imp = std::make_shared(tuple); src_imp->setSdp(getSdp()); src_imp->setProtocolOption(getProtocolOption()); return src_imp; diff --git a/src/Rtsp/RtspMediaSourceImp.h b/src/Rtsp/RtspMediaSourceImp.h index ca341836..483d7610 100644 --- a/src/Rtsp/RtspMediaSourceImp.h +++ b/src/Rtsp/RtspMediaSourceImp.h @@ -28,7 +28,7 @@ public: * @param id 流id * @param ringSize 环形缓存大小 */ - RtspMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTP_GOP_SIZE); + RtspMediaSourceImp(const MediaTuple& tuple, int ringSize = RTP_GOP_SIZE); ~RtspMediaSourceImp() override = default; @@ -107,7 +107,7 @@ public: } } - RtspMediaSource::Ptr Clone(const std::string& stream) override; + RtspMediaSource::Ptr clone(const std::string& stream) override; private: bool _all_track_ready = false; ProtocolOption _option; diff --git a/src/Rtsp/RtspMediaSourceMuxer.h b/src/Rtsp/RtspMediaSourceMuxer.h index d8f40101..c772149a 100644 --- a/src/Rtsp/RtspMediaSourceMuxer.h +++ b/src/Rtsp/RtspMediaSourceMuxer.h @@ -21,13 +21,11 @@ class RtspMediaSourceMuxer final : public RtspMuxer, public MediaSourceEventInte public: using Ptr = std::shared_ptr; - RtspMediaSourceMuxer(const std::string &vhost, - const std::string &strApp, - const std::string &strId, + RtspMediaSourceMuxer(const MediaTuple& tuple, const ProtocolOption &option, const TitleSdp::Ptr &title = nullptr) : RtspMuxer(title) { _option = option; - _media_src = std::make_shared(vhost,strApp,strId); + _media_src = std::make_shared(tuple); getRtpRing()->setDelegate(_media_src); } diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 5388ad53..13f57d54 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -133,7 +133,7 @@ void RtspSession::onWholeRtspPacket(Parser &parser) { if (_content_base.empty() && method != "GET") { _content_base = parser.Url(); _media_info.parse(parser.FullUrl()); - _media_info._schema = RTSP_SCHEMA; + _media_info.schema = RTSP_SCHEMA; } using rtsp_request_handler = void (RtspSession::*)(const Parser &parser); @@ -208,7 +208,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { _media_info.parse(full_url); } - if (_media_info._app.empty() || _media_info._streamid.empty()) { + if (_media_info.app.empty() || _media_info.stream.empty()) { //推流rtsp url必须最少两级(rtsp://host/app/stream_id),不允许莫名其妙的推流url static constexpr auto err = "rtsp推流url非法,最少确保两级rtsp url"; sendRtspResponse("403 Forbidden", {"Content-Type", "text/plain"}, err); @@ -223,7 +223,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { } assert(!_push_src); - auto src = MediaSource::find(RTSP_SCHEMA, _media_info._vhost, _media_info._app, _media_info._streamid); + auto src = MediaSource::find(RTSP_SCHEMA, _media_info.vhost, _media_info.app, _media_info.stream); auto push_failed = (bool)src; while (src) { @@ -266,7 +266,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { } if (!_push_src) { - _push_src = std::make_shared(_media_info._vhost, _media_info._app, _media_info._streamid); + _push_src = std::make_shared(_media_info); //获取所有权 _push_src_ownership = _push_src->getOwnership(); _push_src->setProtocolOption(option); @@ -717,7 +717,7 @@ void RtspSession::handleReq_Setup(const Parser &parser) { break; case Rtsp::RTP_MULTICAST: { if(!_multicaster){ - _multicaster = RtpMultiCaster::get(*this, get_local_ip(), _media_info._vhost, _media_info._app, _media_info._streamid, _multicast_ip, _multicast_video_port, _multicast_audio_port); + _multicaster = RtpMultiCaster::get(*this, get_local_ip(), _media_info.vhost, _media_info.app, _media_info.stream, _multicast_ip, _multicast_video_port, _multicast_audio_port); if (!_multicaster) { send_NotAcceptable(); throw SockException(Err_shutdown, "can not get a available udp multicast socket"); @@ -1145,7 +1145,7 @@ MediaOriginType RtspSession::getOriginType(MediaSource &sender) const{ } string RtspSession::getOriginUrl(MediaSource &sender) const { - return _media_info._full_url; + return _media_info.full_url; } std::shared_ptr RtspSession::getOriginSock(MediaSource &sender) const { diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index 8a22ad90..71797c1b 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -39,10 +39,7 @@ public: using RingDataType = std::shared_ptr >; using RingType = toolkit::RingBuffer; - TSMediaSource(const std::string &vhost, - const std::string &app, - const std::string &stream_id, - int ring_size = TS_GOP_SIZE) : MediaSource(TS_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {} + TSMediaSource(const MediaTuple& tuple, int ring_size = TS_GOP_SIZE): MediaSource(TS_SCHEMA, tuple), _ring_size(ring_size) {} ~TSMediaSource() override { flush(); } diff --git a/src/TS/TSMediaSourceMuxer.h b/src/TS/TSMediaSourceMuxer.h index 392b3236..389ea901 100644 --- a/src/TS/TSMediaSourceMuxer.h +++ b/src/TS/TSMediaSourceMuxer.h @@ -21,12 +21,9 @@ class TSMediaSourceMuxer final : public MpegMuxer, public MediaSourceEventInterc public: using Ptr = std::shared_ptr; - TSMediaSourceMuxer(const std::string &vhost, - const std::string &app, - const std::string &stream_id, - const ProtocolOption &option) : MpegMuxer(false) { + TSMediaSourceMuxer(const MediaTuple& tuple, const ProtocolOption &option) : MpegMuxer(false) { _option = option; - _media_src = std::make_shared(vhost, app, stream_id); + _media_src = std::make_shared(tuple); } ~TSMediaSourceMuxer() override { MpegMuxer::flush(); }; diff --git a/srt/SrtTransportImp.cpp b/srt/SrtTransportImp.cpp index b91505f1..91ae7488 100644 --- a/srt/SrtTransportImp.cpp +++ b/srt/SrtTransportImp.cpp @@ -35,7 +35,7 @@ void SrtTransportImp::onHandShakeFinished(std::string &streamid, struct sockaddr return; } - auto params = Parser::parseArgs(_media_info._param_strs); + auto params = Parser::parseArgs(_media_info.param_strs); if (params["m"] == "publish") { _is_pusher = true; _decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, this); @@ -52,7 +52,7 @@ bool SrtTransportImp::parseStreamid(std::string &streamid) { if (!toolkit::start_with(streamid, "#!::")) { return false; } - _media_info._schema = SRT_SCHEMA; + _media_info.schema = SRT_SCHEMA; std::string real_streamid = streamid.substr(4); std::string vhost, app, stream_name; @@ -70,10 +70,10 @@ bool SrtTransportImp::parseStreamid(std::string &streamid) { app = tmps[0]; stream_name = tmps[1]; } else { - if (_media_info._param_strs.empty()) { - _media_info._param_strs = it.first + "=" + it.second; + if (_media_info.param_strs.empty()) { + _media_info.param_strs = it.first + "=" + it.second; } else { - _media_info._param_strs += "&" + it.first + "=" + it.second; + _media_info.param_strs += "&" + it.first + "=" + it.second; } } } @@ -82,15 +82,15 @@ bool SrtTransportImp::parseStreamid(std::string &streamid) { } if (vhost != "") { - _media_info._vhost = vhost; + _media_info.vhost = vhost; } else { - _media_info._vhost = DEFAULT_VHOST; + _media_info.vhost = DEFAULT_VHOST; } - _media_info._app = app; - _media_info._streamid = stream_name; + _media_info.app = app; + _media_info.stream = stream_name; - TraceL << " mediainfo=" << _media_info.shortUrl() << " params=" << _media_info._param_strs; + TraceL << " mediainfo=" << _media_info.shortUrl() << " params=" << _media_info.param_strs; return true; } @@ -136,7 +136,7 @@ mediakit::MediaOriginType SrtTransportImp::getOriginType(mediakit::MediaSource & // 获取媒体源url或者文件路径 std::string SrtTransportImp::getOriginUrl(mediakit::MediaSource &sender) const { - return _media_info._full_url; + return _media_info.full_url; } // 获取媒体源客户端相关信息 @@ -157,9 +157,7 @@ void SrtTransportImp::emitOnPublish() { return; } if (err.empty()) { - strong_self->_muxer = std::make_shared(strong_self->_media_info._vhost, - strong_self->_media_info._app, - strong_self->_media_info._streamid,0.0f, + strong_self->_muxer = std::make_shared(strong_self->_media_info,0.0f, option); strong_self->_muxer->setMediaListener(strong_self); strong_self->doCachedFunc(); @@ -207,7 +205,7 @@ void SrtTransportImp::emitOnPlay() { void SrtTransportImp::doPlay() { // 异步查找直播流 MediaInfo info = _media_info; - info._schema = TS_SCHEMA; + info.schema = TS_SCHEMA; std::weak_ptr weak_self = static_pointer_cast(shared_from_this()); MediaSource::findAsync(info, getSession(), [weak_self](const MediaSource::Ptr &src) { auto strong_self = weak_self.lock(); @@ -281,7 +279,7 @@ uint16_t SrtTransportImp::get_local_port() { } std::string SrtTransportImp::getIdentifier() const { - return _media_info._streamid; + return _media_info.stream; } bool SrtTransportImp::inputFrame(const Frame::Ptr &frame) { @@ -293,7 +291,7 @@ bool SrtTransportImp::inputFrame(const Frame::Ptr &frame) { auto diff = _type_to_stamp[TrackType::TrackVideo].getRelativeStamp() - _type_to_stamp[TrackType::TrackAudio].getRelativeStamp(); if(std::abs(diff) > 5000){ // 超过5s,应该同步 TODO - WarnL << _media_info._full_url<<" video or audio not sync : "<getCodecName()<<" dts "<dts()<<" pts "<pts(); diff --git a/tests/test_server.cpp b/tests/test_server.cpp index c13b8595..6a15c532 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -90,8 +90,8 @@ void initEventListener() { static onceToken s_token([]() { //监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问 NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnGetRtspRealm, [](BroadcastOnGetRtspRealmArgs) { - DebugL << "RTSP是否需要鉴权事件:" << args.getUrl() << " " << args._param_strs; - if (string("1") == args._streamid) { + DebugL << "RTSP是否需要鉴权事件:" << args.getUrl() << " " << args.param_strs; + if (string("1") == args.stream) { // live/1需要认证 //该流需要认证,并且设置realm invoker(REALM); @@ -104,7 +104,7 @@ void initEventListener() { //监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码 NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnRtspAuth, [](BroadcastOnRtspAuthArgs) { - DebugL << "RTSP播放鉴权:" << args.getUrl() << " " << args._param_strs; + DebugL << "RTSP播放鉴权:" << args.getUrl() << " " << args.param_strs; DebugL << "RTSP用户:" << user_name << (must_no_encrypt ? " Base64" : " MD5") << " 方式登录"; string user = user_name; //假设我们异步读取数据库 @@ -134,14 +134,14 @@ void initEventListener() { //监听rtsp/rtmp推流事件,返回结果告知是否有推流权限 NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) { - DebugL << "推流鉴权:" << args.getUrl() << " " << args._param_strs; + DebugL << "推流鉴权:" << args.getUrl() << " " << args.param_strs; invoker("", ProtocolOption());//鉴权成功 //invoker("this is auth failed message");//鉴权失败 }); //监听rtsp/rtsps/rtmp/http-flv播放事件,返回结果告知是否有播放权限(rtsp通过kBroadcastOnRtspAuth或此事件都可以实现鉴权) NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPlayed, [](BroadcastMediaPlayedArgs) { - DebugL << "播放鉴权:" << args.getUrl() << " " << args._param_strs; + DebugL << "播放鉴权:" << args.getUrl() << " " << args.param_strs; invoker("");//鉴权成功 //invoker("this is auth failed message");//鉴权失败 }); @@ -182,14 +182,13 @@ void initEventListener() { * 你可以在这个事件触发时再去拉流,这样就可以实现按需拉流 * 拉流成功后,ZLMediaKit会把其立即转发给播放器(最大等待时间约为5秒,如果5秒都未拉流成功,播放器会播放失败) */ - DebugL << "未找到流事件:" << args.getUrl() << " " << args._param_strs; + DebugL << "未找到流事件:" << args.getUrl() << " " << args.param_strs; }); //监听播放或推流结束时消耗流量事件 NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastFlowReport, [](BroadcastFlowReportArgs) { - DebugL << "播放器(推流器)断开连接事件:" << args.getUrl() << " " << args._param_strs - << "\r\n使用流量:" << totalBytes << " bytes,连接时长:" << totalDuration << "秒"; + DebugL << "播放器(推流器)断开连接事件:" << args.getUrl() << " " << args.param_strs << "\r\n使用流量:" << totalBytes << " bytes,连接时长:" << totalDuration << "秒"; }); diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index 03e25200..334236bf 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -71,7 +71,7 @@ MediaOriginType WebRtcPusher::getOriginType(MediaSource &sender) const { } string WebRtcPusher::getOriginUrl(MediaSource &sender) const { - return _media_info._full_url; + return _media_info.full_url; } std::shared_ptr WebRtcPusher::getOriginSock(MediaSource &sender) const { @@ -99,7 +99,7 @@ void WebRtcPusher::onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Pt auto &src = _push_src_sim[rid]; if (!src) { auto stream_id = rid.empty() ? _push_src->getId() : _push_src->getId() + "_" + rid; - auto src_imp = _push_src->Clone(stream_id); + auto src_imp = _push_src->clone(stream_id); _push_src_sim_ownership[rid] = src_imp->getOwnership(); src_imp->setListener(static_pointer_cast(shared_from_this())); src = src_imp; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 1c9fb0fc..f7b60cf1 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -1179,7 +1179,7 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana RtspMediaSourceImp::Ptr push_src; std::shared_ptr push_src_ownership; - auto src = MediaSource::find(RTSP_SCHEMA, info._vhost, info._app, info._streamid); + auto src = MediaSource::find(RTSP_SCHEMA, info.vhost, info.app, info.stream); auto push_failed = (bool)src; while (src) { @@ -1206,7 +1206,7 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana } if (!push_src) { - push_src = std::make_shared(info._vhost, info._app, info._streamid); + push_src = std::make_shared(info); push_src_ownership = push_src->getOwnership(); push_src->setProtocolOption(option); } @@ -1235,7 +1235,7 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana } // webrtc播放的是rtsp的源 - info._schema = RTSP_SCHEMA; + info.schema = RTSP_SCHEMA; MediaSource::findAsync(info, session_ptr, [=](const MediaSource::Ptr &src_in) mutable { auto src = dynamic_pointer_cast(src_in); if (!src) { @@ -1243,7 +1243,7 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana return; } // 还原成rtc,目的是为了hook时识别哪种播放协议 - info._schema = RTC_SCHEMA; + info.schema = RTC_SCHEMA; auto rtc = WebRtcPlayer::create(EventPollerPool::Instance().getPoller(), src, info, preferred_tcp); cb(*rtc); }); From 11a39c68c46e3921247d1037662d2c0824c49a4e Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Wed, 31 May 2023 09:59:41 +0800 Subject: [PATCH 33/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=B7=E5=BA=B7rtsp?= =?UTF-8?q?=E7=82=B9=E6=92=AD=E6=8B=89=E6=B5=81seek=E7=9B=B8=E5=85=B3bug(#?= =?UTF-8?q?2501=20#2511)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtsp/RtspPlayer.cpp | 347 ++++++++++++++++++++-------------------- 1 file changed, 170 insertions(+), 177 deletions(-) diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index 67d0b281..e99c8a3e 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -8,40 +8,35 @@ * may be found in the AUTHORS file in the root of the source tree. */ -#include -#include -#include -#include -#include "Common/config.h" #include "RtspPlayer.h" -#include "Util/MD5.h" -#include "Util/base64.h" +#include "Common/config.h" #include "Rtcp/Rtcp.h" #include "Rtcp/RtcpContext.h" -#include "RtspMediaSource.h" #include "RtspDemuxer.h" +#include "RtspMediaSource.h" #include "RtspPlayerImp.h" +#include "Util/MD5.h" +#include "Util/base64.h" +#include +#include +#include +#include using namespace toolkit; using namespace std; namespace mediakit { -enum PlayType { - type_play = 0, - type_pause, - type_seek, - type_speed -}; +enum PlayType { type_play = 0, type_pause, type_seek, type_speed }; -RtspPlayer::RtspPlayer(const EventPoller::Ptr &poller) : TcpClient(poller){ -} +RtspPlayer::RtspPlayer(const EventPoller::Ptr &poller) + : TcpClient(poller) {} RtspPlayer::~RtspPlayer(void) { DebugL << endl; } -void RtspPlayer::sendTeardown(){ +void RtspPlayer::sendTeardown() { if (alive()) { if (!_content_base.empty()) { sendRtspRequest("TEARDOWN", _content_base); @@ -50,7 +45,7 @@ void RtspPlayer::sendTeardown(){ } } -void RtspPlayer::teardown(){ +void RtspPlayer::teardown() { sendTeardown(); _md5_nonce.clear(); _realm.clear(); @@ -69,7 +64,7 @@ void RtspPlayer::teardown(){ _on_response = nullptr; } -void RtspPlayer::play(const string &strUrl){ +void RtspPlayer::play(const string &strUrl) { RtspUrl url; try { url.parse(strUrl); @@ -94,32 +89,35 @@ void RtspPlayer::play(const string &strUrl){ weak_ptr weakSelf = static_pointer_cast(shared_from_this()); float playTimeOutSec = (*this)[Client::kTimeoutMS].as() / 1000.0f; - _play_check_timer.reset(new Timer(playTimeOutSec, [weakSelf]() { - auto strongSelf=weakSelf.lock(); - if(!strongSelf) { + _play_check_timer.reset(new Timer( + playTimeOutSec, + [weakSelf]() { + auto strongSelf = weakSelf.lock(); + if (!strongSelf) { + return false; + } + strongSelf->onPlayResult_l(SockException(Err_timeout, "play rtsp timeout"), false); return false; - } - strongSelf->onPlayResult_l(SockException(Err_timeout,"play rtsp timeout"),false); - return false; - }, getPoller())); + }, + getPoller())); - if(!(*this)[Client::kNetAdapter].empty()){ + if (!(*this)[Client::kNetAdapter].empty()) { setNetAdapter((*this)[Client::kNetAdapter]); } startConnect(url._host, url._port, playTimeOutSec); } -void RtspPlayer::onConnect(const SockException &err){ - if(err.getErrCode() != Err_success) { - onPlayResult_l(err,false); +void RtspPlayer::onConnect(const SockException &err) { + if (err.getErrCode() != Err_success) { + onPlayResult_l(err, false); return; } sendOptions(); } -void RtspPlayer::onRecv(const Buffer::Ptr& buf) { - if(_benchmark_mode && !_play_check_timer){ - //在性能测试模式下,如果rtsp握手完毕后,不再解析rtp包 +void RtspPlayer::onRecv(const Buffer::Ptr &buf) { + if (_benchmark_mode && !_play_check_timer) { + // 在性能测试模式下,如果rtsp握手完毕后,不再解析rtp包 _rtp_recv_ticker.resetTime(); return; } @@ -132,21 +130,21 @@ void RtspPlayer::onRecv(const Buffer::Ptr& buf) { } void RtspPlayer::onError(const SockException &ex) { - //定时器_pPlayTimer为空后表明握手结束了 - onPlayResult_l(ex,!_play_check_timer); + // 定时器_pPlayTimer为空后表明握手结束了 + onPlayResult_l(ex, !_play_check_timer); } // from live555 bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) { - if(!_realm.empty()){ - //已经认证过了 + if (!_realm.empty()) { + // 已经认证过了 return false; } char *realm = new char[paramsStr.size()]; char *nonce = new char[paramsStr.size()]; char *stale = new char[paramsStr.size()]; - onceToken token(nullptr,[&](){ + onceToken token(nullptr, [&]() { delete[] realm; delete[] nonce; delete[] stale; @@ -169,16 +167,16 @@ bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) { return false; } -bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser){ +bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser) { string authInfo = parser["WWW-Authenticate"]; - //发送DESCRIBE命令后的回复 + // 发送DESCRIBE命令后的回复 if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) { sendOptions(); return false; } - if(parser.Url() == "302" || parser.Url() == "301"){ + if (parser.Url() == "302" || parser.Url() == "301") { auto newUrl = parser["Location"]; - if(newUrl.empty()){ + if (newUrl.empty()) { throw std::runtime_error("未找到Location字段(跳转url)"); } play(newUrl); @@ -190,19 +188,19 @@ bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser){ return true; } -void RtspPlayer::handleResDESCRIBE(const Parser& parser) { +void RtspPlayer::handleResDESCRIBE(const Parser &parser) { if (!handleResponse("DESCRIBE", parser)) { return; } _content_base = parser["Content-Base"]; - if(_content_base.empty()){ + if (_content_base.empty()) { _content_base = _play_url; } if (_content_base.back() == '/') { _content_base.pop_back(); } - //解析sdp + // 解析sdp SdpParser sdpParser(parser.Content()); string sdp; @@ -224,16 +222,16 @@ void RtspPlayer::handleResDESCRIBE(const Parser& parser) { } _rtcp_context.clear(); for (auto &track : _sdp_track) { - if(track->_pt != 0xff){ - setPayloadType(_rtcp_context.size(),track->_pt); + if (track->_pt != 0xff) { + setPayloadType(_rtcp_context.size(), track->_pt); } _rtcp_context.emplace_back(std::make_shared()); } sendSetup(0); } -//有必要的情况下创建udp端口 -void RtspPlayer::createUdpSockIfNecessary(int track_idx){ +// 有必要的情况下创建udp端口 +void RtspPlayer::createUdpSockIfNecessary(int track_idx) { auto &rtpSockRef = _rtp_sock[track_idx]; auto &rtcpSockRef = _rtcp_sock[track_idx]; if (!rtpSockRef || !rtcpSockRef) { @@ -244,30 +242,27 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){ } } -//发送SETUP命令 +// 发送SETUP命令 void RtspPlayer::sendSetup(unsigned int track_idx) { _on_response = std::bind(&RtspPlayer::handleResSETUP, this, placeholders::_1, track_idx); auto &track = _sdp_track[track_idx]; auto control_url = track->getControlUrl(_content_base); switch (_rtp_type) { case Rtsp::RTP_TCP: { - sendRtspRequest("SETUP", control_url, {"Transport", StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1}); - } - break; + sendRtspRequest( + "SETUP", control_url, { "Transport", StrPrinter << "RTP/AVP/TCP;unicast;interleaved=" << track->_type * 2 << "-" << track->_type * 2 + 1 }); + } break; case Rtsp::RTP_MULTICAST: { - sendRtspRequest("SETUP", control_url, {"Transport", "RTP/AVP;multicast"}); - } - break; + sendRtspRequest("SETUP", control_url, { "Transport", "RTP/AVP;multicast" }); + } break; case Rtsp::RTP_UDP: { createUdpSockIfNecessary(track_idx); - sendRtspRequest("SETUP", control_url, {"Transport", - StrPrinter << "RTP/AVP;unicast;client_port=" - << _rtp_sock[track_idx]->get_local_port() << "-" - << _rtcp_sock[track_idx]->get_local_port()}); - } - break; - default: - break; + sendRtspRequest( + "SETUP", control_url, + { "Transport", + StrPrinter << "RTP/AVP;unicast;client_port=" << _rtp_sock[track_idx]->get_local_port() << "-" << _rtcp_sock[track_idx]->get_local_port() }); + } break; + default: break; } } @@ -292,9 +287,9 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { auto transport_map = Parser::parseArgs(strTransport, ";", "="); RtspSplitter::enableRecvRtp(_rtp_type == Rtsp::RTP_TCP); string ssrc = transport_map["ssrc"]; - if(!ssrc.empty()){ + if (!ssrc.empty()) { sscanf(ssrc.data(), "%x", &_sdp_track[track_idx]->_ssrc); - } else{ + } else { _sdp_track[track_idx]->_ssrc = 0; } @@ -310,47 +305,47 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { auto &pRtcpSockRef = _rtcp_sock[track_idx]; if (_rtp_type == Rtsp::RTP_MULTICAST) { - //udp组播 - auto multiAddr = transport_map["destination"]; + // udp组播 + auto multiAddr = transport_map["destination"]; pRtpSockRef = createSocket(); - //目前组播仅支持ipv4 + // 目前组播仅支持ipv4 if (!pRtpSockRef->bindUdpSock(rtp_port, "0.0.0.0")) { pRtpSockRef.reset(); throw std::runtime_error("open udp sock err"); } auto fd = pRtpSockRef->rawFD(); - if (-1 == SockUtil::joinMultiAddrFilter(fd, multiAddr.data(), get_peer_ip().data(),get_local_ip().data())) { - SockUtil::joinMultiAddr(fd, multiAddr.data(),get_local_ip().data()); + if (-1 == SockUtil::joinMultiAddrFilter(fd, multiAddr.data(), get_peer_ip().data(), get_local_ip().data())) { + SockUtil::joinMultiAddr(fd, multiAddr.data(), get_local_ip().data()); } - //设置rtcp发送端口 + // 设置rtcp发送端口 pRtcpSockRef = createSocket(); - //目前组播仅支持ipv4 + // 目前组播仅支持ipv4 if (!pRtcpSockRef->bindUdpSock(0, "0.0.0.0")) { - //分配端口失败 + // 分配端口失败 throw runtime_error("open udp socket failed"); } - //设置发送地址和发送端口 + // 设置发送地址和发送端口 auto dst = SockUtil::make_sockaddr(get_peer_ip().data(), rtcp_port); pRtcpSockRef->bindPeerAddr((struct sockaddr *)&(dst)); } else { createUdpSockIfNecessary(track_idx); - //udp单播 + // udp单播 auto dst = SockUtil::make_sockaddr(get_peer_ip().data(), rtp_port); pRtpSockRef->bindPeerAddr((struct sockaddr *)&(dst)); - //发送rtp打洞包 + // 发送rtp打洞包 pRtpSockRef->send("\xce\xfa\xed\xfe", 4); dst = SockUtil::make_sockaddr(get_peer_ip().data(), rtcp_port); - //设置rtcp发送目标,为后续发送rtcp做准备 + // 设置rtcp发送目标,为后续发送rtcp做准备 pRtcpSockRef->bindPeerAddr((struct sockaddr *)&(dst)); } auto peer_ip = get_peer_ip(); weak_ptr weakSelf = static_pointer_cast(shared_from_this()); - //设置rtp over udp接收回调处理函数 - pRtpSockRef->setOnRead([peer_ip, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) { + // 设置rtp over udp接收回调处理函数 + pRtpSockRef->setOnRead([peer_ip, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len) { auto strongSelf = weakSelf.lock(); if (!strongSelf) { return; @@ -359,13 +354,13 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { WarnL << "收到其他地址的rtp数据:" << SockUtil::inet_ntoa(addr); return; } - strongSelf->handleOneRtp(track_idx, strongSelf->_sdp_track[track_idx]->_type, - strongSelf->_sdp_track[track_idx]->_samplerate, (uint8_t *) buf->data(), buf->size()); + strongSelf->handleOneRtp( + track_idx, strongSelf->_sdp_track[track_idx]->_type, strongSelf->_sdp_track[track_idx]->_samplerate, (uint8_t *)buf->data(), buf->size()); }); - if(pRtcpSockRef) { - //设置rtcp over udp接收回调处理函数 - pRtcpSockRef->setOnRead([peer_ip, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr , int addr_len) { + if (pRtcpSockRef) { + // 设置rtcp over udp接收回调处理函数 + pRtcpSockRef->setOnRead([peer_ip, track_idx, weakSelf](const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len) { auto strongSelf = weakSelf.lock(); if (!strongSelf) { return; @@ -374,68 +369,66 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { WarnL << "收到其他地址的rtcp数据:" << SockUtil::inet_ntoa(addr); return; } - strongSelf->onRtcpPacket(track_idx, strongSelf->_sdp_track[track_idx], (uint8_t *) buf->data(), buf->size()); + strongSelf->onRtcpPacket(track_idx, strongSelf->_sdp_track[track_idx], (uint8_t *)buf->data(), buf->size()); }); } } if (track_idx < _sdp_track.size() - 1) { - //需要继续发送SETUP命令 + // 需要继续发送SETUP命令 sendSetup(track_idx + 1); return; } - //所有setup命令发送完毕 - //发送play命令 + // 所有setup命令发送完毕 + // 发送play命令 sendPause(type_play, 0); } void RtspPlayer::sendDescribe() { - //发送DESCRIBE命令后处理函数:handleResDESCRIBE + // 发送DESCRIBE命令后处理函数:handleResDESCRIBE _on_response = std::bind(&RtspPlayer::handleResDESCRIBE, this, placeholders::_1); - sendRtspRequest("DESCRIBE", _play_url, {"Accept", "application/sdp"}); + sendRtspRequest("DESCRIBE", _play_url, { "Accept", "application/sdp" }); } -void RtspPlayer::sendOptions(){ - _on_response = [this](const Parser& parser){ +void RtspPlayer::sendOptions() { + _on_response = [this](const Parser &parser) { if (!handleResponse("OPTIONS", parser)) { return; } - //获取服务器支持的命令 + // 获取服务器支持的命令 _supported_cmd.clear(); - auto public_val = split(parser["Public"],","); - for(auto &cmd : public_val){ + auto public_val = split(parser["Public"], ","); + for (auto &cmd : public_val) { trim(cmd); _supported_cmd.emplace(cmd); } - //发送Describe请求,获取sdp + // 发送Describe请求,获取sdp sendDescribe(); }; sendRtspRequest("OPTIONS", _play_url); } -void RtspPlayer::sendKeepAlive(){ +void RtspPlayer::sendKeepAlive() { _on_response = [](const Parser &parser) {}; - if(_supported_cmd.find("GET_PARAMETER") != _supported_cmd.end()){ - //支持GET_PARAMETER,用此命令保活 + if (_supported_cmd.find("GET_PARAMETER") != _supported_cmd.end()) { + // 支持GET_PARAMETER,用此命令保活 sendRtspRequest("GET_PARAMETER", _content_base); - }else{ - //不支持GET_PARAMETER,用OPTIONS命令保活 + } else { + // 不支持GET_PARAMETER,用OPTIONS命令保活 sendRtspRequest("OPTIONS", _play_url); } } -void RtspPlayer::sendPause(int type , uint32_t seekMS){ +void RtspPlayer::sendPause(int type, uint32_t seekMS) { _on_response = std::bind(&RtspPlayer::handleResPAUSE, this, placeholders::_1, type); - //开启或暂停rtsp - switch (type){ - case type_pause: - sendRtspRequest("PAUSE", _content_base); - break; + // 开启或暂停rtsp + switch (type) { + case type_pause: sendRtspRequest("PAUSE", _content_base); break; case type_play: - sendRtspRequest("PLAY", _content_base); - break; + // sendRtspRequest("PLAY", _content_base); + // break; case type_seek: - sendRtspRequest("PLAY", _content_base, {"Range",StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << seekMS / 1000.0 << "-"}); + sendRtspRequest("PLAY", _content_base, { "Range", StrPrinter << "npt=" << setiosflags(ios::fixed) << setprecision(2) << seekMS / 1000.0 << "-" }); break; default: WarnL << "unknown type : " << type; @@ -449,35 +442,31 @@ void RtspPlayer::pause(bool bPause) { } void RtspPlayer::speed(float speed) { - sendRtspRequest("PLAY", _content_base, {"Scale", StrPrinter << speed}); + sendRtspRequest("PLAY", _content_base, { "Scale", StrPrinter << speed }); } -void RtspPlayer::handleResPAUSE(const Parser& parser,int type) { +void RtspPlayer::handleResPAUSE(const Parser &parser, int type) { if (parser.Url() != "200") { switch (type) { - case type_pause: - WarnL << "Pause failed:" << parser.Url() << " " << parser.Tail() << endl; - break; + case type_pause: WarnL << "Pause failed:" << parser.Url() << " " << parser.Tail() << endl; break; case type_play: WarnL << "Play failed:" << parser.Url() << " " << parser.Tail() << endl; - onPlayResult_l(SockException(Err_shutdown, StrPrinter << "rtsp play failed:" << parser.Url() << " " << parser.Tail() ), !_play_check_timer); - break; - case type_seek: - WarnL << "Seek failed:" << parser.Url() << " " << parser.Tail() << endl; + onPlayResult_l(SockException(Err_shutdown, StrPrinter << "rtsp play failed:" << parser.Url() << " " << parser.Tail()), !_play_check_timer); break; + case type_seek: WarnL << "Seek failed:" << parser.Url() << " " << parser.Tail() << endl; break; } return; } if (type == type_pause) { - //暂停成功! + // 暂停成功! _rtp_check_timer.reset(); return; } - //play或seek成功 + // play或seek成功 uint32_t iSeekTo = 0; - //修正时间轴 + // 修正时间轴 auto strRange = parser["Range"]; if (strRange.size()) { auto strStart = FindField(strRange.data(), "npt=", "-"); @@ -495,54 +484,56 @@ void RtspPlayer::onWholeRtspPacket(Parser &parser) { try { decltype(_on_response) func; _on_response.swap(func); - if(func){ + if (func) { func(parser); } parser.Clear(); } catch (std::exception &err) { - //定时器_pPlayTimer为空后表明握手结束了 - onPlayResult_l(SockException(Err_other, err.what()),!_play_check_timer); + // 定时器_pPlayTimer为空后表明握手结束了 + onPlayResult_l(SockException(Err_other, err.what()), !_play_check_timer); } } void RtspPlayer::onRtpPacket(const char *data, size_t len) { int trackIdx = -1; uint8_t interleaved = data[1]; - if(interleaved %2 == 0){ + if (interleaved % 2 == 0) { trackIdx = getTrackIndexByInterleaved(interleaved); if (trackIdx == -1) { return; } - handleOneRtp(trackIdx, _sdp_track[trackIdx]->_type, _sdp_track[trackIdx]->_samplerate, (uint8_t *)data + RtpPacket::kRtpTcpHeaderSize, len - RtpPacket::kRtpTcpHeaderSize); - }else{ + handleOneRtp( + trackIdx, _sdp_track[trackIdx]->_type, _sdp_track[trackIdx]->_samplerate, (uint8_t *)data + RtpPacket::kRtpTcpHeaderSize, + len - RtpPacket::kRtpTcpHeaderSize); + } else { trackIdx = getTrackIndexByInterleaved(interleaved - 1); if (trackIdx == -1) { return; } - onRtcpPacket(trackIdx, _sdp_track[trackIdx], (uint8_t *) data + RtpPacket::kRtpTcpHeaderSize, len - RtpPacket::kRtpTcpHeaderSize); + onRtcpPacket(trackIdx, _sdp_track[trackIdx], (uint8_t *)data + RtpPacket::kRtpTcpHeaderSize, len - RtpPacket::kRtpTcpHeaderSize); } } -//此处预留rtcp处理函数 -void RtspPlayer::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len){ - auto rtcp_arr = RtcpHeader::loadFromBytes((char *) data, len); +// 此处预留rtcp处理函数 +void RtspPlayer::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len) { + auto rtcp_arr = RtcpHeader::loadFromBytes((char *)data, len); for (auto &rtcp : rtcp_arr) { _rtcp_context[track_idx]->onRtcp(rtcp); - if ((RtcpType) rtcp->pt == RtcpType::RTCP_SR) { - auto sr = (RtcpSR *) (rtcp); - //设置rtp时间戳与ntp时间戳的对应关系 + if ((RtcpType)rtcp->pt == RtcpType::RTCP_SR) { + auto sr = (RtcpSR *)(rtcp); + // 设置rtp时间戳与ntp时间戳的对应关系 setNtpStamp(track_idx, sr->rtpts, sr->getNtpUnixStampMS()); } } } -void RtspPlayer::onRtpSorted(RtpPacket::Ptr rtppt, int trackidx){ +void RtspPlayer::onRtpSorted(RtpPacket::Ptr rtppt, int trackidx) { _stamp[trackidx] = rtppt->getStampMS(); _rtp_recv_ticker.resetTime(); onRecvRTP(std::move(rtppt), _sdp_track[trackidx]); } -float RtspPlayer::getPacketLossRate(TrackType type) const{ +float RtspPlayer::getPacketLossRate(TrackType type) const { size_t lost = 0, expected = 0; try { auto track_idx = getTrackIndexByTrackType(type); @@ -558,32 +549,32 @@ float RtspPlayer::getPacketLossRate(TrackType type) const{ if (!expected) { return 0; } - return (float) (double(lost) / double(expected)); + return (float)(double(lost) / double(expected)); } -uint32_t RtspPlayer::getProgressMilliSecond() const{ - return MAX(_stamp[0],_stamp[1]); +uint32_t RtspPlayer::getProgressMilliSecond() const { + return MAX(_stamp[0], _stamp[1]); } void RtspPlayer::seekToMilliSecond(uint32_t ms) { - sendPause(type_seek,ms); + sendPause(type_seek, ms); } void RtspPlayer::sendRtspRequest(const string &cmd, const string &url, const std::initializer_list &header) { string key; StrCaseMap header_map; int i = 0; - for(auto &val : header){ - if(++i % 2 == 0){ - header_map.emplace(key,val); - }else{ + for (auto &val : header) { + if (++i % 2 == 0) { + header_map.emplace(key, val); + } else { key = val; } } - sendRtspRequest(cmd,url,header_map); + sendRtspRequest(cmd, url, header_map); } -void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrCaseMap &header_const) { +void RtspPlayer::sendRtspRequest(const string &cmd, const string &url, const StrCaseMap &header_const) { auto header = header_const; header.emplace("CSeq", StrPrinter << _cseq_send++); header.emplace("User-Agent", kServerName); @@ -624,44 +615,49 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC } _StrPrinter printer; - printer << cmd << " " << url << " RTSP/1.0\r\n"; - for (auto &pr : header){ + if (cmd == "PLAY") { + printer << cmd << " " << _play_url << " RTSP/1.0\r\n"; + } else { + printer << cmd << " " << url << " RTSP/1.0\r\n"; + } + + for (auto &pr : header) { printer << pr.first << ": " << pr.second << "\r\n"; } printer << "\r\n"; SockSender::send(std::move(printer)); } -void RtspPlayer::onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_idx){ +void RtspPlayer::onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_idx) { auto &rtcp_ctx = _rtcp_context[track_idx]; rtcp_ctx->onRtp(rtp->getSeq(), rtp->getStamp(), rtp->ntp_stamp, rtp->sample_rate, rtp->size() - RtpPacket::kRtpTcpHeaderSize); auto &ticker = _rtcp_send_ticker[track_idx]; if (ticker.elapsedTime() < 3 * 1000) { - //时间未到 + // 时间未到 return; } auto &rtcp_flag = _send_rtcp[track_idx]; - //每3秒发送一次心跳,rtcp与rtsp信令轮流心跳,该特性用于兼容issue:642 - //有些rtsp服务器需要rtcp保活,有些需要发送信令保活 + // 每3秒发送一次心跳,rtcp与rtsp信令轮流心跳,该特性用于兼容issue:642 + // 有些rtsp服务器需要rtcp保活,有些需要发送信令保活 - //发送信令保活 + // 发送信令保活 if (!rtcp_flag) { if (track_idx == 0) { sendKeepAlive(); } ticker.resetTime(); - //下次发送rtcp保活 + // 下次发送rtcp保活 rtcp_flag = true; return; } - //发送rtcp + // 发送rtcp static auto send_rtcp = [](RtspPlayer *thiz, int index, Buffer::Ptr ptr) { if (thiz->_rtp_type == Rtsp::RTP_TCP) { auto &track = thiz->_sdp_track[index]; - thiz->send(makeRtpOverTcpPrefix((uint16_t) (ptr->size()), track->_interleaved + 1)); + thiz->send(makeRtpOverTcpPrefix((uint16_t)(ptr->size()), track->_interleaved + 1)); thiz->send(std::move(ptr)); } else { thiz->_rtcp_sock[index]->send(std::move(ptr)); @@ -670,39 +666,39 @@ void RtspPlayer::onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_idx){ auto ssrc = rtp->getSSRC(); auto rtcp = rtcp_ctx->createRtcpRR(ssrc + 1, ssrc); - auto rtcp_sdes = RtcpSdes::create({kServerName}); - rtcp_sdes->chunks.type = (uint8_t) SdesType::RTCP_SDES_CNAME; + auto rtcp_sdes = RtcpSdes::create({ kServerName }); + rtcp_sdes->chunks.type = (uint8_t)SdesType::RTCP_SDES_CNAME; rtcp_sdes->chunks.ssrc = htonl(ssrc); send_rtcp(this, track_idx, std::move(rtcp)); send_rtcp(this, track_idx, RtcpHeader::toBuffer(rtcp_sdes)); ticker.resetTime(); - //下次发送信令保活 + // 下次发送信令保活 rtcp_flag = false; } -void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshake_done) { +void RtspPlayer::onPlayResult_l(const SockException &ex, bool handshake_done) { if (ex.getErrCode() == Err_shutdown) { - //主动shutdown的,不触发回调 + // 主动shutdown的,不触发回调 return; } WarnL << ex.getErrCode() << " " << ex.what(); if (!handshake_done) { - //开始播放阶段 + // 开始播放阶段 _play_check_timer.reset(); onPlayResult(ex); - //是否为性能测试模式 + // 是否为性能测试模式 _benchmark_mode = (*this)[Client::kBenchmarkMode].as(); } else if (ex) { - //播放成功后异常断开回调 + // 播放成功后异常断开回调 onShutdown(ex); } else { - //恢复播放 + // 恢复播放 onResume(); } if (!ex) { - //播放成功,恢复rtp接收超时定时器 + // 播放成功,恢复rtp接收超时定时器 _rtp_recv_ticker.resetTime(); auto timeoutMS = (*this)[Client::kMediaTimeoutMS].as(); weak_ptr weakSelf = static_pointer_cast(shared_from_this()); @@ -712,13 +708,13 @@ void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshake_done) { return false; } if (strongSelf->_rtp_recv_ticker.elapsedTime() > timeoutMS) { - //接收rtp媒体数据包超时 + // 接收rtp媒体数据包超时 strongSelf->onPlayResult_l(SockException(Err_timeout, "receive rtp timeout"), true); return false; } return true; }; - //创建rtp数据接收超时检测定时器 + // 创建rtp数据接收超时检测定时器 _rtp_check_timer = std::make_shared(timeoutMS / 2000.0f, lam, getPoller()); } else { sendTeardown(); @@ -752,8 +748,7 @@ int RtspPlayer::getTrackIndexByTrackType(TrackType track_type) const { /////////////////////////////////////////////////// // RtspPlayerImp -float RtspPlayerImp::getDuration() const -{ +float RtspPlayerImp::getDuration() const { return _demuxer ? _demuxer->getDuration() : 0; } @@ -770,13 +765,11 @@ void RtspPlayerImp::addTrackCompleted() { } } -std::vector RtspPlayerImp::getTracks(bool ready /*= true*/) const -{ +std::vector RtspPlayerImp::getTracks(bool ready /*= true*/) const { return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); } -bool RtspPlayerImp::onCheckSDP(const std::string &sdp) -{ +bool RtspPlayerImp::onCheckSDP(const std::string &sdp) { _rtsp_media_src = std::dynamic_pointer_cast(_media_src); if (_rtsp_media_src) { _rtsp_media_src->setSdp(sdp); @@ -788,7 +781,7 @@ bool RtspPlayerImp::onCheckSDP(const std::string &sdp) } void RtspPlayerImp::onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) { - //rtp解复用时可以判断是否为关键帧起始位置 + // rtp解复用时可以判断是否为关键帧起始位置 auto key_pos = _demuxer->inputRtp(rtp); if (_rtsp_media_src) { _rtsp_media_src->onWrite(std::move(rtp), key_pos); From 14537bfbc7ffa64c4fd9fa5df1b56924bf147135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E9=B8=A3?= <94030128+ixingqiao@users.noreply.github.com> Date: Wed, 31 May 2023 23:16:16 +0800 Subject: [PATCH 34/66] =?UTF-8?q?=E8=A7=A3=E5=86=B3ws-flv=E5=92=8Chttp-flv?= =?UTF-8?q?=E7=9A=84=E7=B4=AF=E7=A7=AF=E6=97=B6=E5=BB=B6=E9=97=AE=E9=A2=98?= =?UTF-8?q?(#2509)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 9f2ce375..42c8cb1d 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -147,9 +147,15 @@ bool HttpSession::checkWebSocket(){ _live_over_websocket = true; sendResponse(101, false, nullptr, headerOut, nullptr, true); }; + + auto res_cb_flv = [this, header = std::move(headerOut)]() mutable { + _live_over_websocket = true; + header.emplace("Cache-Control", "no-store"); + sendResponse(101, false, nullptr, header, nullptr, true); + }; //判断是否为websocket-flv - if (checkLiveStreamFlv(res_cb)) { + if (checkLiveStreamFlv(res_cb_flv)) { //这里是websocket-flv直播请求 return true; } @@ -349,7 +355,9 @@ bool HttpSession::checkLiveStreamFlv(const function &cb){ assert(rtmp_src); if (!cb) { //找到源,发送http头,负载后续发送 - sendResponse(200, false, HttpFileManager::getContentType(".flv").data(), KeyValue(), nullptr, true); + KeyValue headerOut; + headerOut["Cache-Control"] = "no-store"; + sendResponse(200, false, HttpFileManager::getContentType(".flv").data(), headerOut, nullptr, true); } else { //自定义发送http头 cb(); From 7970138966a9b032bed230411d8371e5ffa52f71 Mon Sep 17 00:00:00 2001 From: fruit Juice <2317232721@qq.com> Date: Thu, 1 Jun 2023 14:29:19 +0800 Subject: [PATCH 35/66] Fix MP4Reader available speed range bug (#2516) --- src/Record/MP4Reader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index 2d4105e0..5c144d2e 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -180,7 +180,7 @@ bool MP4Reader::pause(MediaSource &sender, bool pause) { } bool MP4Reader::speed(MediaSource &sender, float speed) { - if (speed < 0.1 && speed > 20) { + if (speed < 0.1 || speed > 20) { WarnL << "播放速度取值范围非法:" << speed; return false; } From af57691bc83e31d9f2679c3bf33a79d9248880cf Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 2 Jun 2023 21:43:24 +0800 Subject: [PATCH 36/66] =?UTF-8?q?c++14=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=BAc++11=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 42c8cb1d..b443dfec 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -148,10 +148,10 @@ bool HttpSession::checkWebSocket(){ sendResponse(101, false, nullptr, headerOut, nullptr, true); }; - auto res_cb_flv = [this, header = std::move(headerOut)]() mutable { + auto res_cb_flv = [this, headerOut]() mutable { _live_over_websocket = true; - header.emplace("Cache-Control", "no-store"); - sendResponse(101, false, nullptr, header, nullptr, true); + headerOut.emplace("Cache-Control", "no-store"); + sendResponse(101, false, nullptr, headerOut, nullptr, true); }; //判断是否为websocket-flv From e4acc59bec9f1faad96323a399c9ba462faf4cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sat, 3 Jun 2023 17:34:34 +0800 Subject: [PATCH 37/66] =?UTF-8?q?=E5=AA=92=E4=BD=93=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E9=BB=98=E8=AE=A4=E6=94=B9=E4=B8=BA=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=EF=BC=8C=E9=80=9A=E8=BF=87=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=E7=9F=AB=E6=AD=A3=E5=99=A8=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E8=B7=B3=E8=B7=83=E5=92=8C=E5=9B=9E=E9=80=80=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#2523)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过用户反馈发现,遇到较多直播源时间戳混乱问题,包括rtsp流之rtcp ntp时间戳混乱,国标流时间戳混乱问题。 之前默认采用源流绝对时间戳,但是由于时间戳混乱会导致hls切片异常或视频播放不了等问题。 故默认改成相对时间戳(modify_stamp=2),通过时间戳矫正器过滤了时间戳跳跃以及回退问题(强制加1ms), 同时保留了对之前时间戳覆盖(modify_stamp=1)的兼容。 该修改同时兼容点播流,在点播流开启时间戳覆盖时,不起实质作用(采用原始绝对时间戳)。 --- conf/config.ini | 5 +- postman/ZLMediaKit.postman_collection.json | 2 +- src/Common/MediaSource.cpp | 2 +- src/Common/MediaSource.h | 10 +- src/Common/MultiMediaSourceMuxer.cpp | 16 ++- src/Common/Stamp.cpp | 154 ++++++++++----------- src/Common/config.cpp | 3 +- src/Extension/Frame.cpp | 7 +- src/Extension/Frame.h | 2 +- 9 files changed, 111 insertions(+), 90 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index d2b2c0dd..4fb5cc1c 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -32,7 +32,10 @@ restart_sec=0 #转协议相关开关;如果addStreamProxy api和on_publish hook回复未指定转协议参数,则采用这些配置项 [protocol] #转协议时,是否开启帧级时间戳覆盖 -modify_stamp=0 +# 0:采用源视频流绝对时间戳,不做任何改变 +# 1:采用zlmediakit接收数据时的系统时间戳(有平滑处理) +# 2:采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正 +modify_stamp=2 #转协议是否开启音频 enable_audio=1 #添加acc静音音频,在关闭音频时,此开关无效 diff --git a/postman/ZLMediaKit.postman_collection.json b/postman/ZLMediaKit.postman_collection.json index 044461e0..ad1929bd 100644 --- a/postman/ZLMediaKit.postman_collection.json +++ b/postman/ZLMediaKit.postman_collection.json @@ -582,7 +582,7 @@ { "key": "modify_stamp", "value": null, - "description": "是否重新计算时间戳", + "description": "是否修改原始时间戳,默认值2;取值范围:0.采用源视频流绝对时间戳,不做任何改变;1.采用zlmediakit接收数据时的系统时间戳(有平滑处理);2.采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正", "disabled": true } ] diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index fb3a21ce..f2861153 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -53,7 +53,7 @@ string getOriginTypeString(MediaOriginType type){ ////////////////////////////////////////////////////////////////////////////////////////////////////////////// ProtocolOption::ProtocolOption() { - GET_CONFIG(bool, s_modify_stamp, Protocol::kModifyStamp); + GET_CONFIG(int, s_modify_stamp, Protocol::kModifyStamp); GET_CONFIG(bool, s_enabel_audio, Protocol::kEnableAudio); GET_CONFIG(bool, s_add_mute_audio, Protocol::kAddMuteAudio); GET_CONFIG(uint32_t, s_continue_push_ms, Protocol::kContinuePushMS); diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index df855293..bbe8035f 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -136,8 +136,14 @@ class ProtocolOption { public: ProtocolOption(); - //时间戳修复这一路流标志位 - bool modify_stamp; + enum { + kModifyStampOff = 0, // 采用源视频流绝对时间戳,不做任何改变 + kModifyStampSystem = 1, // 采用zlmediakit接收数据时的系统时间戳(有平滑处理) + kModifyStampRelative = 2 // 采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正 + }; + // 时间戳类型 + int modify_stamp; + //转协议是否开启音频 bool enable_audio; //添加静音音频,在关闭音频时,此开关无效 diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index 75c79c27..a9d904be 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -94,6 +94,11 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_ _poller = EventPollerPool::Instance().getPoller(); _create_in_poller = _poller->isCurrentThread(); _option = option; + if (dur_sec > 0.01) { + // 点播 + _stamp[TrackVideo].setPlayBack(); + _stamp[TrackAudio].setPlayBack(); + } if (option.enable_rtmp) { _rtmp = std::make_shared(_tuple, option, std::make_shared(dur_sec)); @@ -378,6 +383,11 @@ void MultiMediaSourceMuxer::onAllTrackReady() { createGopCacheIfNeed(); } #endif + auto tracks = getTracks(false); + if (tracks.size() >= 2) { + // 音频时间戳同步于视频,因为音频时间戳被修改后不影响播放 + _stamp[TrackAudio].syncTo(_stamp[TrackVideo]); + } InfoL << "stream: " << shortUrl() << " , codec info: " << getTrackInfoStr(this); } @@ -429,9 +439,9 @@ void MultiMediaSourceMuxer::resetTracks() { bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) { auto frame = frame_in; - if (_option.modify_stamp) { - //开启了时间戳覆盖 - frame = std::make_shared(frame, _stamp[frame->getTrackType()],true); + if (_option.modify_stamp != ProtocolOption::kModifyStampOff) { + // 时间戳不采用原始的绝对时间戳 + frame = std::make_shared(frame, _stamp[frame->getTrackType()], _option.modify_stamp); } bool ret = false; diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index df7f7368..c7b1ab3c 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -10,7 +10,7 @@ #include "Stamp.h" -//时间戳最大允许跳变3秒,主要是防止网络抖动导致的跳变 +// 时间戳最大允许跳变3秒,主要是防止网络抖动导致的跳变 #define MAX_DELTA_STAMP (3 * 1000) #define STAMP_LOOP_DELTA (60 * 1000) #define MAX_CTS 500 @@ -25,51 +25,51 @@ int64_t DeltaStamp::relativeStamp(int64_t stamp) { return _relative_stamp; } -int64_t DeltaStamp::relativeStamp(){ +int64_t DeltaStamp::relativeStamp() { return _relative_stamp; } int64_t DeltaStamp::deltaStamp(int64_t stamp) { - if(!_last_stamp){ - //第一次计算时间戳增量,时间戳增量为0 - if(stamp){ + if (!_last_stamp) { + // 第一次计算时间戳增量,时间戳增量为0 + if (stamp) { _last_stamp = stamp; } return 0; } int64_t ret = stamp - _last_stamp; - if(ret >= 0){ - //时间戳增量为正,返回之 + if (ret >= 0) { + // 时间戳增量为正,返回之 _last_stamp = stamp; - //在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP - return ret < MAX_DELTA_STAMP ? ret : 0; + // 在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP,否则强制相对时间戳加1 + return ret < MAX_DELTA_STAMP ? ret : 1; } - //时间戳增量为负,说明时间戳回环了或回退了 + // 时间戳增量为负,说明时间戳回环了或回退了 _last_stamp = stamp; - //如果时间戳回退不多,那么返回负值 - return -ret < MAX_CTS ? ret : 0; + // 时间戳回退了,强制相对时间戳加1 + return 1; } void Stamp::setPlayBack(bool playback) { _playback = playback; } -void Stamp::syncTo(Stamp &other){ +void Stamp::syncTo(Stamp &other) { _sync_master = &other; } -//限制dts回退 -void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) { +// 限制dts回退 +void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) { revise_l(dts, pts, dts_out, pts_out, modifyStamp); if (_playback) { - //回放允许时间戳回退 + // 回放允许时间戳回退 return; } if (dts_out < _last_dts_out) { -// WarnL << "dts回退:" << dts_out << " < " << _last_dts_out; + // WarnL << "dts回退:" << dts_out << " < " << _last_dts_out; dts_out = _last_dts_out; pts_out = _last_pts_out; return; @@ -78,35 +78,35 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, _last_pts_out = pts_out; } -//音视频时间戳同步 -void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) { +// 音视频时间戳同步 +void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) { revise_l2(dts, pts, dts_out, pts_out, modifyStamp); if (!_sync_master || modifyStamp || _playback) { - //自动生成时间戳或回放或同步完毕 + // 自动生成时间戳或回放或同步完毕 return; } if (_sync_master && _sync_master->_last_dts_in) { - //音视频dts当前时间差 + // 音视频dts当前时间差 int64_t dts_diff = _last_dts_in - _sync_master->_last_dts_in; if (ABS(dts_diff) < 5000) { - //如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步 + // 如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步 _relative_stamp = _sync_master->_relative_stamp + dts_diff; } - //下次不用再强制同步 + // 下次不用再强制同步 _sync_master = nullptr; } } -//求取相对时间戳 -void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) { +// 求取相对时间戳 +void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) { if (!pts) { - //没有播放时间戳,使其赋值为解码时间戳 + // 没有播放时间戳,使其赋值为解码时间戳 pts = dts; } if (_playback) { - //这是点播 + // 这是点播 dts_out = dts; pts_out = pts; _relative_stamp = dts_out; @@ -114,13 +114,13 @@ void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_o return; } - //pts和dts的差值 + // pts和dts的差值 int64_t pts_dts_diff = pts - dts; if (_last_dts_in != dts) { - //时间戳发生变更 + // 时间戳发生变更 if (modifyStamp) { - //内部自己生产时间戳 + // 内部自己生产时间戳 _relative_stamp = _ticker.elapsedTime(); } else { _relative_stamp += deltaStamp(dts); @@ -131,7 +131,7 @@ void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_o //////////////以下是播放时间戳的计算////////////////// if (ABS(pts_dts_diff) > MAX_CTS) { - //如果差值太大,则认为由于回环导致时间戳错乱了 + // 如果差值太大,则认为由于回环导致时间戳错乱了 pts_dts_diff = 0; } @@ -146,86 +146,86 @@ int64_t Stamp::getRelativeStamp() const { return _relative_stamp; } -bool DtsGenerator::getDts(uint64_t pts, uint64_t &dts){ +bool DtsGenerator::getDts(uint64_t pts, uint64_t &dts) { bool ret = false; if (pts == _last_pts) { - //pts未变,说明dts也不会变,返回上次dts + // pts未变,说明dts也不会变,返回上次dts if (_last_dts) { dts = _last_dts; ret = true; } } else { - //pts变了,尝试计算dts + // pts变了,尝试计算dts ret = getDts_l(pts, dts); if (ret) { - //获取到了dts,保存本次结果 + // 获取到了dts,保存本次结果 _last_dts = dts; } } if (!ret) { - //pts排序列队长度还不知道,也就是不知道有没有B帧, - //那么先强制dts == pts,这样可能导致有B帧的情况下,起始画面有几帧回退 + // pts排序列队长度还不知道,也就是不知道有没有B帧, + // 那么先强制dts == pts,这样可能导致有B帧的情况下,起始画面有几帧回退 dts = pts; } - //记录上次pts + // 记录上次pts _last_pts = pts; return ret; } -//该算法核心思想是对pts进行排序,排序好的pts就是dts。 -//排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量 -bool DtsGenerator::getDts_l(uint64_t pts, uint64_t &dts){ - if(_sorter_max_size == 1){ - //没有B帧,dts就等于pts +// 该算法核心思想是对pts进行排序,排序好的pts就是dts。 +// 排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量 +bool DtsGenerator::getDts_l(uint64_t pts, uint64_t &dts) { + if (_sorter_max_size == 1) { + // 没有B帧,dts就等于pts dts = pts; return true; } - if(!_sorter_max_size){ - //尚未计算出pts排序列队长度(也就是P帧间B帧个数) - if(pts > _last_max_pts){ - //pts时间戳增加了,那么说明这帧画面不是B帧(说明是P帧或关键帧) - if(_frames_since_last_max_pts && _count_sorter_max_size++ > 0){ - //已经出现多次非B帧的情况,那么我们就能知道P帧间B帧的个数 + if (!_sorter_max_size) { + // 尚未计算出pts排序列队长度(也就是P帧间B帧个数) + if (pts > _last_max_pts) { + // pts时间戳增加了,那么说明这帧画面不是B帧(说明是P帧或关键帧) + if (_frames_since_last_max_pts && _count_sorter_max_size++ > 0) { + // 已经出现多次非B帧的情况,那么我们就能知道P帧间B帧的个数 _sorter_max_size = _frames_since_last_max_pts; - //我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计) + // 我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计) _dts_pts_offset = (pts - _last_max_pts); - //除以2,防止dts大于pts + // 除以2,防止dts大于pts _dts_pts_offset /= 2; } - //遇到P帧或关键帧,连续B帧计数清零 + // 遇到P帧或关键帧,连续B帧计数清零 _frames_since_last_max_pts = 0; - //记录上次非B帧的pts时间戳(同时也是dts),用于统计连续B帧时间戳增量 + // 记录上次非B帧的pts时间戳(同时也是dts),用于统计连续B帧时间戳增量 _last_max_pts = pts; } - //如果pts时间戳小于上一个P帧,那么断定这个是B帧,我们记录B帧连续个数 + // 如果pts时间戳小于上一个P帧,那么断定这个是B帧,我们记录B帧连续个数 ++_frames_since_last_max_pts; } - //pts放入排序缓存列队,缓存列队最大等于连续B帧个数 + // pts放入排序缓存列队,缓存列队最大等于连续B帧个数 _pts_sorter.emplace(pts); - if(_sorter_max_size && _pts_sorter.size() > _sorter_max_size){ - //如果启用了pts排序(意味着存在B帧),并且pts排序缓存列队长度大于连续B帧个数, - //意味着后续的pts都会比最早的pts大,那么说明可以取出最早的pts了,这个pts将当做该帧的dts基准 + if (_sorter_max_size && _pts_sorter.size() > _sorter_max_size) { + // 如果启用了pts排序(意味着存在B帧),并且pts排序缓存列队长度大于连续B帧个数, + // 意味着后续的pts都会比最早的pts大,那么说明可以取出最早的pts了,这个pts将当做该帧的dts基准 auto it = _pts_sorter.begin(); - //由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts), - //那么我们加上时间戳偏移量,基本等于该帧的dts + // 由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts), + // 那么我们加上时间戳偏移量,基本等于该帧的dts dts = *it + _dts_pts_offset; - if(dts > pts){ - //dts不能大于pts(基本不可能到达这个逻辑) + if (dts > pts) { + // dts不能大于pts(基本不可能到达这个逻辑) dts = pts; } - //pts排序缓存出列 + // pts排序缓存出列 _pts_sorter.erase(it); return true; } - //排序缓存尚未满 + // 排序缓存尚未满 return false; } @@ -235,7 +235,7 @@ void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { if (ntp_stamp_ms == 0) { - //实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0 + // 实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0 return; } _last_rtp_stamp = rtp_stamp; @@ -251,51 +251,51 @@ uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) { uint64_t NtpStamp::getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate) { if (!_last_ntp_stamp_ms) { - //尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧 + // 尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧 update(rtp_stamp, getCurrentMillisecond(true)); } - //rtp时间戳正增长 + // rtp时间戳正增长 if (rtp_stamp >= _last_rtp_stamp) { auto diff = static_cast((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f)); if (diff < MAX_DELTA_STAMP) { - //时间戳正常增长 + // 时间戳正常增长 update(rtp_stamp, _last_ntp_stamp_ms + diff); return _last_ntp_stamp_ms; } - //时间戳大幅跳跃 + // 时间戳大幅跳跃 uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) { - //应该是rtp时间戳溢出+乱序 + // 应该是rtp时间戳溢出+乱序 uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; return _last_ntp_stamp_ms + diff - max_rtp_ms; } - //不明原因的时间戳大幅跳跃,直接返回上次值 + // 不明原因的时间戳大幅跳跃,直接返回上次值 WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp; update(rtp_stamp, _last_ntp_stamp_ms); return _last_ntp_stamp_ms; } - //rtp时间戳负增长 + // rtp时间戳负增长 auto diff = static_cast((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f)); if (diff < MAX_DELTA_STAMP) { - //正常范围的时间戳回退,说明收到rtp乱序了 + // 正常范围的时间戳回退,说明收到rtp乱序了 return _last_ntp_stamp_ms - diff; } - //时间戳大幅度回退 + // 时间戳大幅度回退 uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) { - //确定是时间戳溢出 + // 确定是时间戳溢出 uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; update(rtp_stamp, _last_ntp_stamp_ms + (max_rtp_ms - diff)); return _last_ntp_stamp_ms; } - //不明原因的时间戳回退,直接返回上次值 + // 不明原因的时间戳回退,直接返回上次值 WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp; update(rtp_stamp, _last_ntp_stamp_ms); return _last_ntp_stamp_ms; } -}//namespace mediakit +} // namespace mediakit diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 1654c61c..ce5d8935 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -9,6 +9,7 @@ */ #include "Common/config.h" +#include "MediaSource.h" #include "Util/NoticeCenter.h" #include "Util/logger.h" #include "Util/onceToken.h" @@ -120,7 +121,7 @@ const string kTSDemand = PROTOCOL_FIELD "ts_demand"; const string kFMP4Demand = PROTOCOL_FIELD "fmp4_demand"; static onceToken token([]() { - mINI::Instance()[kModifyStamp] = 0; + mINI::Instance()[kModifyStamp] = (int)ProtocolOption::kModifyStampRelative; mINI::Instance()[kEnableAudio] = 1; mINI::Instance()[kAddMuteAudio] = 1; mINI::Instance()[kContinuePushMS] = 15000; diff --git a/src/Extension/Frame.cpp b/src/Extension/Frame.cpp index 7cc3cb41..152eea1f 100644 --- a/src/Extension/Frame.cpp +++ b/src/Extension/Frame.cpp @@ -13,6 +13,7 @@ #include "H265.h" #include "Common/Parser.h" #include "Common/Stamp.h" +#include "Common/MediaSource.h" using namespace std; using namespace toolkit; @@ -31,11 +32,11 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){ return std::make_shared(frame); } -FrameStamp::FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp) +FrameStamp::FrameStamp(Frame::Ptr frame, Stamp &stamp, int modify_stamp) { _frame = std::move(frame); - //覆盖时间戳 - stamp.revise(_frame->dts(), _frame->pts(), _dts, _pts, modify_stamp); + // kModifyStampSystem时采用系统时间戳,kModifyStampRelative采用相对时间戳 + stamp.revise(_frame->dts(), _frame->pts(), _dts, _pts, modify_stamp == ProtocolOption::kModifyStampSystem); } TrackType getTrackType(CodecId codecId) { diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index 4328e544..63546596 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -492,7 +492,7 @@ private: class FrameStamp : public Frame { public: using Ptr = std::shared_ptr; - FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp); + FrameStamp(Frame::Ptr frame, Stamp &stamp, int modify_stamp); ~FrameStamp() override {} uint64_t dts() const override { return (uint64_t)_dts; } From 7e1078bbcc8e4c2c0c1deb1f66b10fce9a4a6ee6 Mon Sep 17 00:00:00 2001 From: a-ucontrol <55526028+a-ucontrol@users.noreply.github.com> Date: Tue, 6 Jun 2023 04:23:34 +0300 Subject: [PATCH 38/66] Update submodule ZLToolKit (#2529) https://github.com/ZLMediaKit/ZLMediaKit/issues/2526#issue-1740204646 --- 3rdpart/ZLToolKit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index b30eeca0..b6e5af3e 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit b30eeca034456417dfca72aa0d2258031013a5e6 +Subproject commit b6e5af3e7ad95d87f4e45a89e36d9a8ab45209a9 From fff53cf0e209bb93bf917ba2e8f0d5e692e08b1a Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 09:45:46 +0800 Subject: [PATCH 39/66] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96HttpSession.cp?= =?UTF-8?q?p=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 343 +++++++++++++++++++-------------------- 1 file changed, 166 insertions(+), 177 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index b443dfec..5a5aa113 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -24,16 +24,16 @@ using namespace toolkit; namespace mediakit { HttpSession::HttpSession(const Socket::Ptr &pSock) : Session(pSock) { - GET_CONFIG(uint32_t,keep_alive_sec,Http::kKeepAliveSecond); + GET_CONFIG(uint32_t, keep_alive_sec, Http::kKeepAliveSecond); pSock->setSendTimeOutSecond(keep_alive_sec); } HttpSession::~HttpSession() = default; -void HttpSession::Handle_Req_HEAD(ssize_t &content_len){ - //暂时全部返回200 OK,因为HTTP GET存在按需生成流的操作,所以不能按照HTTP GET的流程返回 - //如果直接返回404,那么又会导致按需生成流的逻辑失效,所以HTTP HEAD在静态文件或者已存在资源时才有效 - //对于按需生成流的直播场景并不适用 +void HttpSession::Handle_Req_HEAD(ssize_t &content_len) { + // 暂时全部返回200 OK,因为HTTP GET存在按需生成流的操作,所以不能按照HTTP GET的流程返回 + // 如果直接返回404,那么又会导致按需生成流的逻辑失效,所以HTTP HEAD在静态文件或者已存在资源时才有效 + // 对于按需生成流的直播场景并不适用 sendResponse(200, false); } @@ -52,16 +52,16 @@ void HttpSession::Handle_Req_OPTIONS(ssize_t &content_len) { sendResponse(200, true, nullptr, header); } -ssize_t HttpSession::onRecvHeader(const char *header,size_t len) { - typedef void (HttpSession::*HttpCMDHandle)(ssize_t &); - static unordered_map s_func_map; +ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { + using func_type = void (HttpSession::*)(ssize_t &); + static unordered_map s_func_map; static onceToken token([]() { - s_func_map.emplace("GET",&HttpSession::Handle_Req_GET); - s_func_map.emplace("DELETE",&HttpSession::Handle_Req_GET); - s_func_map.emplace("POST",&HttpSession::Handle_Req_POST); - s_func_map.emplace("HEAD",&HttpSession::Handle_Req_HEAD); - s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS); - }, nullptr); + s_func_map.emplace("GET", &HttpSession::Handle_Req_GET); + s_func_map.emplace("DELETE", &HttpSession::Handle_Req_GET); + s_func_map.emplace("POST", &HttpSession::Handle_Req_POST); + s_func_map.emplace("HEAD", &HttpSession::Handle_Req_HEAD); + s_func_map.emplace("OPTIONS", &HttpSession::Handle_Req_OPTIONS); + }); _parser.Parse(header); CHECK(_parser.Url()[0] == '/'); @@ -75,60 +75,54 @@ ssize_t HttpSession::onRecvHeader(const char *header,size_t len) { return 0; } - //跨域 + // 跨域 _origin = _parser["Origin"]; //默认后面数据不是content而是header ssize_t content_len = 0; (this->*(it->second))(content_len); - //清空解析器节省内存 + // 清空解析器节省内存 _parser.Clear(); - //返回content长度 + // 返回content长度 return content_len; } -void HttpSession::onRecvContent(const char *data,size_t len) { - if(_contentCallBack){ - if(!_contentCallBack(data,len)){ - _contentCallBack = nullptr; - } +void HttpSession::onRecvContent(const char *data, size_t len) { + if (_contentCallBack && !_contentCallBack(data, len)) { + _contentCallBack = nullptr; } } void HttpSession::onRecv(const Buffer::Ptr &pBuf) { _ticker.resetTime(); - input(pBuf->data(),pBuf->size()); + input(pBuf->data(), pBuf->size()); } -void HttpSession::onError(const SockException& err) { +void HttpSession::onError(const SockException &err) { if (_is_live_stream) { - //flv/ts播放器 + // flv/ts播放器 uint64_t duration = _ticker.createdTime() / 1000; - WarnP(this) << "FLV/TS/FMP4播放器(" - << _mediaInfo.shortUrl() - << ")断开:" << err - << ",耗时(s):" << duration; + WarnP(this) << "FLV/TS/FMP4播放器(" << _mediaInfo.shortUrl() << ")断开:" << err << ",耗时(s):" << duration; GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold); if (_total_bytes_usage >= iFlowThreshold * 1024) { - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _mediaInfo, _total_bytes_usage, - duration, true, static_cast(*this)); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _mediaInfo, _total_bytes_usage, duration, true, static_cast(*this)); } return; } } void HttpSession::onManager() { - GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond); + GET_CONFIG(uint32_t, keepAliveSec, Http::kKeepAliveSecond); - if(_ticker.elapsedTime() > keepAliveSec * 1000){ - //1分钟超时 - shutdown(SockException(Err_timeout,"session timeout")); + if (_ticker.elapsedTime() > keepAliveSec * 1000) { + // 1分钟超时 + shutdown(SockException(Err_timeout, "session timeout")); } } -bool HttpSession::checkWebSocket(){ +bool HttpSession::checkWebSocket() { auto Sec_WebSocket_Key = _parser["Sec-WebSocket-Key"]; if (Sec_WebSocket_Key.empty()) { return false; @@ -147,32 +141,32 @@ bool HttpSession::checkWebSocket(){ _live_over_websocket = true; sendResponse(101, false, nullptr, headerOut, nullptr, true); }; - + auto res_cb_flv = [this, headerOut]() mutable { _live_over_websocket = true; headerOut.emplace("Cache-Control", "no-store"); sendResponse(101, false, nullptr, headerOut, nullptr, true); }; - //判断是否为websocket-flv + // 判断是否为websocket-flv if (checkLiveStreamFlv(res_cb_flv)) { - //这里是websocket-flv直播请求 + // 这里是websocket-flv直播请求 return true; } - //判断是否为websocket-ts + // 判断是否为websocket-ts if (checkLiveStreamTS(res_cb)) { - //这里是websocket-ts直播请求 + // 这里是websocket-ts直播请求 return true; } - //判断是否为websocket-fmp4 + // 判断是否为websocket-fmp4 if (checkLiveStreamFMP4(res_cb)) { - //这里是websocket-fmp4直播请求 + // 这里是websocket-fmp4直播请求 return true; } - //这是普通的websocket连接 + // 这是普通的websocket连接 if (!onWebSocketConnect(_parser)) { sendResponse(501, true, nullptr, headerOut); return true; @@ -181,7 +175,7 @@ bool HttpSession::checkWebSocket(){ return true; } -bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix, const function &cb){ +bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix, const function &cb) { std::string url = _parser.Url(); auto it = _parser.getUrlArgs().find("schema"); if (it != _parser.getUrlArgs().end()) { @@ -192,57 +186,57 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffi } else { auto prefix_size = url_suffix.size(); if (url.size() < prefix_size || strcasecmp(url.data() + (url.size() - prefix_size), url_suffix.data())) { - //未找到后缀 + // 未找到后缀 return false; } // url去除特殊后缀 url.resize(url.size() - prefix_size); } - //带参数的url + // 带参数的url if (!_parser.Params().empty()) { url += "?"; url += _parser.Params(); } - //解析带上协议+参数完整的url + // 解析带上协议+参数完整的url _mediaInfo.parse(schema + "://" + _parser["Host"] + url); if (_mediaInfo.app.empty() || _mediaInfo.stream.empty()) { - //url不合法 + // url不合法 return false; } bool close_flag = !strcasecmp(_parser["Connection"].data(), "close"); weak_ptr weak_self = static_pointer_cast(shared_from_this()); - //鉴权结果回调 + // 鉴权结果回调 auto onRes = [cb, weak_self, close_flag](const string &err) { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } if (!err.empty()) { - //播放鉴权失败 + // 播放鉴权失败 strong_self->sendResponse(401, close_flag, nullptr, KeyValue(), std::make_shared(err)); return; } - //异步查找直播流 + // 异步查找直播流 MediaSource::findAsync(strong_self->_mediaInfo, strong_self, [weak_self, close_flag, cb](const MediaSource::Ptr &src) { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } if (!src) { - //未找到该流 + // 未找到该流 strong_self->sendNotFound(close_flag); } else { strong_self->_is_live_stream = true; - //触发回调 + // 触发回调 cb(src); } }); @@ -256,26 +250,26 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffi auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed, _mediaInfo, invoker, static_cast(*this)); if (!flag) { - //该事件无人监听,默认不鉴权 + // 该事件无人监听,默认不鉴权 onRes(""); } return true; } -//http-fmp4 链接格式:http://vhost-url:port/app/streamid.live.mp4?key1=value1&key2=value2 -bool HttpSession::checkLiveStreamFMP4(const function &cb){ +// http-fmp4 链接格式:http://vhost-url:port/app/streamid.live.mp4?key1=value1&key2=value2 +bool HttpSession::checkLiveStreamFMP4(const function &cb) { return checkLiveStream(FMP4_SCHEMA, ".live.mp4", [this, cb](const MediaSource::Ptr &src) { auto fmp4_src = dynamic_pointer_cast(src); assert(fmp4_src); if (!cb) { - //找到源,发送http头,负载后续发送 + // 找到源,发送http头,负载后续发送 sendResponse(200, false, HttpFileManager::getContentType(".mp4").data(), KeyValue(), nullptr, true); } else { - //自定义发送http头 + // 自定义发送http头 cb(); } - //直播牺牲延时提升发送性能 + // 直播牺牲延时提升发送性能 setSocketFlags(); onWrite(std::make_shared(fmp4_src->getInitSegment()), true); weak_ptr weak_self = static_pointer_cast(shared_from_this()); @@ -285,7 +279,7 @@ bool HttpSession::checkLiveStreamFMP4(const function &cb){ _fmp4_reader->setDetachCB([weak_self]() { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } strong_self->shutdown(SockException(Err_shutdown, "fmp4 ring buffer detached")); @@ -293,85 +287,80 @@ bool HttpSession::checkLiveStreamFMP4(const function &cb){ _fmp4_reader->setReadCB([weak_self](const FMP4MediaSource::RingDataType &fmp4_list) { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } size_t i = 0; auto size = fmp4_list->size(); - fmp4_list->for_each([&](const FMP4Packet::Ptr &ts) { - strong_self->onWrite(ts, ++i == size); - }); + fmp4_list->for_each([&](const FMP4Packet::Ptr &ts) { strong_self->onWrite(ts, ++i == size); }); }); }); } -//http-ts 链接格式:http://vhost-url:port/app/streamid.live.ts?key1=value1&key2=value2 -bool HttpSession::checkLiveStreamTS(const function &cb){ +// http-ts 链接格式:http://vhost-url:port/app/streamid.live.ts?key1=value1&key2=value2 +bool HttpSession::checkLiveStreamTS(const function &cb) { return checkLiveStream(TS_SCHEMA, ".live.ts", [this, cb](const MediaSource::Ptr &src) { auto ts_src = dynamic_pointer_cast(src); assert(ts_src); if (!cb) { - //找到源,发送http头,负载后续发送 + // 找到源,发送http头,负载后续发送 sendResponse(200, false, HttpFileManager::getContentType(".ts").data(), KeyValue(), nullptr, true); } else { - //自定义发送http头 + // 自定义发送http头 cb(); } - //直播牺牲延时提升发送性能 + // 直播牺牲延时提升发送性能 setSocketFlags(); weak_ptr weak_self = static_pointer_cast(shared_from_this()); ts_src->pause(false); _ts_reader = ts_src->getRing()->attach(getPoller()); _ts_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); }); - _ts_reader->setDetachCB([weak_self](){ + _ts_reader->setDetachCB([weak_self]() { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } - strong_self->shutdown(SockException(Err_shutdown,"ts ring buffer detached")); + strong_self->shutdown(SockException(Err_shutdown, "ts ring buffer detached")); }); _ts_reader->setReadCB([weak_self](const TSMediaSource::RingDataType &ts_list) { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } size_t i = 0; auto size = ts_list->size(); - ts_list->for_each([&](const TSPacket::Ptr &ts) { - strong_self->onWrite(ts, ++i == size); - }); + ts_list->for_each([&](const TSPacket::Ptr &ts) { strong_self->onWrite(ts, ++i == size); }); }); }); } -//http-flv 链接格式:http://vhost-url:port/app/streamid.live.flv?key1=value1&key2=value2 -bool HttpSession::checkLiveStreamFlv(const function &cb){ +// http-flv 链接格式:http://vhost-url:port/app/streamid.live.flv?key1=value1&key2=value2 +bool HttpSession::checkLiveStreamFlv(const function &cb) { auto start_pts = atoll(_parser.getUrlArgs()["starPts"].data()); return checkLiveStream(RTMP_SCHEMA, ".live.flv", [this, cb, start_pts](const MediaSource::Ptr &src) { auto rtmp_src = dynamic_pointer_cast(src); assert(rtmp_src); if (!cb) { - //找到源,发送http头,负载后续发送 + // 找到源,发送http头,负载后续发送 KeyValue headerOut; headerOut["Cache-Control"] = "no-store"; sendResponse(200, false, HttpFileManager::getContentType(".flv").data(), headerOut, nullptr, true); } else { - //自定义发送http头 + // 自定义发送http头 cb(); } - //直播牺牲延时提升发送性能 + // 直播牺牲延时提升发送性能 setSocketFlags(); - //非H264/AAC时打印警告日志,防止用户提无效问题 + // 非H264/AAC时打印警告日志,防止用户提无效问题 auto tracks = src->getTracks(false); for (auto &track : tracks) { switch (track->getCodecId()) { case CodecH264: - case CodecAAC: - break; + case CodecAAC: break; default: { WarnP(this) << "flv播放器一般只支持H264和AAC编码,该编码格式可能不被播放器支持:" << track->getCodecName(); break; @@ -388,11 +377,11 @@ void HttpSession::Handle_Req_GET(ssize_t &content_len) { } void HttpSession::Handle_Req_GET_l(ssize_t &content_len, bool sendBody) { - //先看看是否为WebSocket请求 + // 先看看是否为WebSocket请求 if (checkWebSocket()) { content_len = -1; _contentCallBack = [this](const char *data, size_t len) { - WebSocketSplitter::decode((uint8_t *) data, len); + WebSocketSplitter::decode((uint8_t *)data, len); //_contentCallBack是可持续的,后面还要处理后续数据 return true; }; @@ -400,29 +389,29 @@ void HttpSession::Handle_Req_GET_l(ssize_t &content_len, bool sendBody) { } if (emitHttpEvent(false)) { - //拦截http api事件 + // 拦截http api事件 return; } if (checkLiveStreamFlv()) { - //拦截http-flv播放器 + // 拦截http-flv播放器 return; } if (checkLiveStreamTS()) { - //拦截http-ts播放器 + // 拦截http-ts播放器 return; } if (checkLiveStreamFMP4()) { - //拦截http-fmp4播放器 + // 拦截http-fmp4播放器 return; } - bool bClose = !strcasecmp(_parser["Connection"].data(),"close"); + bool bClose = !strcasecmp(_parser["Connection"].data(), "close"); weak_ptr weak_self = static_pointer_cast(shared_from_this()); HttpFileManager::onAccessPath(*this, _parser, [weak_self, bClose](int code, const string &content_type, - const StrCaseMap &responseHeader, const HttpBody::Ptr &body) { + const StrCaseMap &responseHeader, const HttpBody::Ptr &body) { auto strong_self = weak_self.lock(); if (!strong_self) { return; @@ -468,7 +457,7 @@ public: static bool onSocketFlushed(const AsyncSenderData::Ptr &data) { if (data->_read_complete) { if (data->_close_when_complete) { - //发送完毕需要关闭socket + // 发送完毕需要关闭socket shutdown(data->_session.lock()); } return false; @@ -478,13 +467,13 @@ public: data->_body->readDataAsync(sendBufSize, [data](const Buffer::Ptr &sendBuf) { auto session = data->_session.lock(); if (!session) { - //本对象已经销毁 + // 本对象已经销毁 return; } session->async([data, sendBuf]() { auto session = data->_session.lock(); if (!session) { - //本对象已经销毁 + // 本对象已经销毁 return; } onRequestData(data, session, sendBuf); @@ -497,14 +486,14 @@ private: static void onRequestData(const AsyncSenderData::Ptr &data, const std::shared_ptr &session, const Buffer::Ptr &sendBuf) { session->_ticker.resetTime(); if (sendBuf && session->send(sendBuf) != -1) { - //文件还未读完,还需要继续发送 + // 文件还未读完,还需要继续发送 if (!session->isSocketBusy()) { - //socket还可写,继续请求数据 + // socket还可写,继续请求数据 onSocketFlushed(data); } return; } - //文件写完了 + // 文件写完了 data->_read_complete = true; if (!session->isSocketBusy() && data->_close_when_complete) { shutdown(session); @@ -512,7 +501,7 @@ private: } static void shutdown(const std::shared_ptr &session) { - if(session){ + if (session) { session->shutdown(SockException(Err_shutdown, StrPrinter << "close connection after send http body completed.")); } } @@ -532,14 +521,14 @@ void HttpSession::sendResponse(int code, const char *pcContentType, const HttpSession::KeyValue &header, const HttpBody::Ptr &body, - bool no_content_length ){ - GET_CONFIG(string,charSet,Http::kCharSet); - GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond); + bool no_content_length) { + GET_CONFIG(string, charSet, Http::kCharSet); + GET_CONFIG(uint32_t, keepAliveSec, Http::kKeepAliveSecond); - //body默认为空 + // body默认为空 int64_t size = 0; if (body && body->remainSize()) { - //有body,获取body大小 + // 有body,获取body大小 size = body->remainSize(); } @@ -547,7 +536,7 @@ void HttpSession::sendResponse(int code, // http-flv直播是Keep-Alive类型 bClose = false; } else if ((size_t)size >= SIZE_MAX || size < 0) { - //不固定长度的body,那么发送完body后应该关闭socket,以便浏览器做下载完毕的判断 + // 不固定长度的body,那么发送完body后应该关闭socket,以便浏览器做下载完毕的判断 bClose = true; } @@ -563,30 +552,30 @@ void HttpSession::sendResponse(int code, } if (!_origin.empty()) { - //设置跨域 + // 设置跨域 headerOut.emplace(kAccessControlAllowOrigin, _origin); headerOut.emplace(kAccessControlAllowCredentials, "true"); } if (!no_content_length && size >= 0 && (size_t)size < SIZE_MAX) { - //文件长度为固定值,且不是http-flv强制设置Content-Length + // 文件长度为固定值,且不是http-flv强制设置Content-Length headerOut[kContentLength] = to_string(size); } if (size && !pcContentType) { - //有body时,设置缺省类型 + // 有body时,设置缺省类型 pcContentType = "text/plain"; } if ((size || no_content_length) && pcContentType) { - //有body时,设置文件类型 + // 有body时,设置文件类型 string strContentType = pcContentType; strContentType += "; charset="; strContentType += charSet; headerOut.emplace(kContentType, std::move(strContentType)); } - //发送http头 + // 发送http头 string str; str.reserve(256); str += "HTTP/1.1 "; @@ -605,9 +594,9 @@ void HttpSession::sendResponse(int code, _ticker.resetTime(); if (!size) { - //没有body + // 没有body if (bClose) { - shutdown(SockException(Err_shutdown,StrPrinter << "close connection after send http header completed with status code:" << code)); + shutdown(SockException(Err_shutdown, StrPrinter << "close connection after send http header completed with status code:" << code)); } return; } @@ -622,20 +611,20 @@ void HttpSession::sendResponse(int code, GET_CONFIG(uint32_t, sendBufSize, Http::kSendBufSize); if (body->remainSize() > sendBufSize) { - //文件下载提升发送性能 + // 文件下载提升发送性能 setSocketFlags(); } - //发送http body + // 发送http body AsyncSenderData::Ptr data = std::make_shared(static_pointer_cast(shared_from_this()), body, bClose); getSock()->setOnFlush([data]() { return AsyncSender::onSocketFlushed(data); }); AsyncSender::onSocketFlushed(data); } -string HttpSession::urlDecode(const string &str){ +string HttpSession::urlDecode(const string &str) { auto ret = strCoding::UrlDecode(str); #ifdef _WIN32 - GET_CONFIG(string,charSet,Http::kCharSet); + GET_CONFIG(string, charSet, Http::kCharSet); bool isGb2312 = !strcasecmp(charSet.data(), "gb2312"); if (isGb2312) { ret = strCoding::UTF8ToGB2312(ret); @@ -644,117 +633,117 @@ string HttpSession::urlDecode(const string &str){ return ret; } -void HttpSession::urlDecode(Parser &parser){ +void HttpSession::urlDecode(Parser &parser) { parser.setUrl(urlDecode(parser.Url())); - for(auto &pr : _parser.getUrlArgs()){ + for (auto &pr : _parser.getUrlArgs()) { const_cast(pr.second) = urlDecode(pr.second); } } -bool HttpSession::emitHttpEvent(bool doInvoke){ - bool bClose = !strcasecmp(_parser["Connection"].data(),"close"); +bool HttpSession::emitHttpEvent(bool doInvoke) { + bool bClose = !strcasecmp(_parser["Connection"].data(), "close"); /////////////////////异步回复Invoker/////////////////////////////// weak_ptr weak_self = static_pointer_cast(shared_from_this()); - HttpResponseInvoker invoker = [weak_self,bClose](int code, const KeyValue &headerOut, const HttpBody::Ptr &body){ + HttpResponseInvoker invoker = [weak_self, bClose](int code, const KeyValue &headerOut, const HttpBody::Ptr &body) { auto strong_self = weak_self.lock(); - if(!strong_self) { + if (!strong_self) { return; } strong_self->async([weak_self, bClose, code, headerOut, body]() { auto strong_self = weak_self.lock(); if (!strong_self) { - //本对象已经销毁 + // 本对象已经销毁 return; } strong_self->sendResponse(code, bClose, nullptr, headerOut, body); }); }; ///////////////////广播HTTP事件/////////////////////////// - bool consumed = false;//该事件是否被消费 - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpRequest,_parser,invoker,consumed,static_cast(*this)); - if(!consumed && doInvoke){ - //该事件无人消费,所以返回404 - invoker(404,KeyValue(), HttpBody::Ptr()); + bool consumed = false; // 该事件是否被消费 + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpRequest, _parser, invoker, consumed, static_cast(*this)); + if (!consumed && doInvoke) { + // 该事件无人消费,所以返回404 + invoker(404, KeyValue(), HttpBody::Ptr()); } return consumed; } std::string HttpSession::get_peer_ip() { GET_CONFIG(string, forwarded_ip_header, Http::kForwardedIpHeader); - if(!forwarded_ip_header.empty() && !_parser.getHeader()[forwarded_ip_header].empty()){ + if (!forwarded_ip_header.empty() && !_parser.getHeader()[forwarded_ip_header].empty()) { return _parser.getHeader()[forwarded_ip_header]; } return Session::get_peer_ip(); } void HttpSession::Handle_Req_POST(ssize_t &content_len) { - GET_CONFIG(size_t,maxReqSize,Http::kMaxReqSize); + GET_CONFIG(size_t, maxReqSize, Http::kMaxReqSize); ssize_t totalContentLen = _parser["Content-Length"].empty() ? -1 : atoll(_parser["Content-Length"].data()); - if(totalContentLen == 0){ - //content为空 - //emitHttpEvent内部会选择是否关闭连接 + if (totalContentLen == 0) { + // content为空 + // emitHttpEvent内部会选择是否关闭连接 emitHttpEvent(true); return; } - if(totalContentLen > 0 && (size_t)totalContentLen < maxReqSize ){ - //返回固定长度的content + if (totalContentLen > 0 && (size_t)totalContentLen < maxReqSize) { + // 返回固定长度的content content_len = totalContentLen; auto parserCopy = _parser; - _contentCallBack = [this,parserCopy](const char *data,size_t len){ - //恢复http头 + _contentCallBack = [this, parserCopy](const char *data, size_t len) { + // 恢复http头 _parser = parserCopy; - //设置content - _parser.setContent(string(data,len)); - //触发http事件,emitHttpEvent内部会选择是否关闭连接 + // 设置content + _parser.setContent(string(data, len)); + // 触发http事件,emitHttpEvent内部会选择是否关闭连接 emitHttpEvent(true); - //清空数据,节省内存 + // 清空数据,节省内存 _parser.Clear(); - //content已经接收完毕 + // content已经接收完毕 return false; }; - }else{ - //返回不固定长度的content或者超过长度限制的content + } else { + // 返回不固定长度的content或者超过长度限制的content content_len = -1; auto parserCopy = _parser; std::shared_ptr recvedContentLen = std::make_shared(0); - bool bClose = !strcasecmp(_parser["Connection"].data(),"close"); + bool bClose = !strcasecmp(_parser["Connection"].data(), "close"); - _contentCallBack = [this,parserCopy,totalContentLen,recvedContentLen,bClose](const char *data,size_t len){ + _contentCallBack = [this, parserCopy, totalContentLen, recvedContentLen, bClose](const char *data, size_t len) { *(recvedContentLen) += len; if (totalContentLen < 0) { - //不固定长度的content,源源不断接收数据 + // 不固定长度的content,源源不断接收数据 onRecvUnlimitedContent(parserCopy, data, len, SIZE_MAX, *(recvedContentLen)); return true; } - //长度超过限制的content - onRecvUnlimitedContent(parserCopy,data,len,totalContentLen,*(recvedContentLen)); + // 长度超过限制的content + onRecvUnlimitedContent(parserCopy, data, len, totalContentLen, *(recvedContentLen)); - if(*(recvedContentLen) < (size_t)totalContentLen){ - //数据还没接收完毕 + if (*(recvedContentLen) < (size_t)totalContentLen) { + // 数据还没接收完毕 //_contentCallBack是可持续的,后面还要处理后续content数据 return true; } - //数据接收完毕 - if(!bClose){ - //keep-alive类型连接 - //content接收完毕,后续都是http header + // 数据接收完毕 + if (!bClose) { + // keep-alive类型连接 + // content接收完毕,后续都是http header setContentLen(0); - //content已经接收完毕 + // content已经接收完毕 return false; } - //连接类型是close类型,收完content就关闭连接 - shutdown(SockException(Err_shutdown,"recv http content completed")); - //content已经接收完毕 - return false ; + // 连接类型是close类型,收完content就关闭连接 + shutdown(SockException(Err_shutdown, "recv http content completed")); + // content已经接收完毕 + return false; }; } - //有后续content数据要处理,暂时不关闭连接 + // 有后续content数据要处理,暂时不关闭连接 } void HttpSession::sendNotFound(bool bClose) { @@ -762,19 +751,19 @@ void HttpSession::sendNotFound(bool bClose) { sendResponse(404, bClose, "text/html", KeyValue(), std::make_shared(notFound)); } -void HttpSession::setSocketFlags(){ +void HttpSession::setSocketFlags() { GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); - if(mergeWriteMS > 0) { - //推流模式下,关闭TCP_NODELAY会增加推流端的延时,但是服务器性能将提高 + if (mergeWriteMS > 0) { + // 推流模式下,关闭TCP_NODELAY会增加推流端的延时,但是服务器性能将提高 SockUtil::setNoDelay(getSock()->rawFD(), false); - //播放模式下,开启MSG_MORE会增加延时,但是能提高发送性能 + // 播放模式下,开启MSG_MORE会增加延时,但是能提高发送性能 setSendFlags(SOCKET_DEFAULE_FLAGS | FLAG_MORE); } } void HttpSession::onWrite(const Buffer::Ptr &buffer, bool flush) { - if(flush){ - //需要flush那么一次刷新缓存 + if (flush) { + // 需要flush那么一次刷新缓存 HttpSession::setSendFlushFlag(true); } @@ -792,18 +781,18 @@ void HttpSession::onWrite(const Buffer::Ptr &buffer, bool flush) { } if (flush) { - //本次刷新缓存后,下次不用刷新缓存 + // 本次刷新缓存后,下次不用刷新缓存 HttpSession::setSendFlushFlag(false); } } -void HttpSession::onWebSocketEncodeData(Buffer::Ptr buffer){ +void HttpSession::onWebSocketEncodeData(Buffer::Ptr buffer) { _total_bytes_usage += buffer->size(); send(std::move(buffer)); } -void HttpSession::onWebSocketDecodeComplete(const WebSocketHeader &header_in){ - WebSocketHeader& header = const_cast(header_in); +void HttpSession::onWebSocketDecodeComplete(const WebSocketHeader &header_in) { + WebSocketHeader &header = const_cast(header_in); header._mask_flag = false; switch (header._opcode) { @@ -813,15 +802,15 @@ void HttpSession::onWebSocketDecodeComplete(const WebSocketHeader &header_in){ break; } - default : break; + default: break; } } void HttpSession::onDetach() { - shutdown(SockException(Err_shutdown,"rtmp ring buffer detached")); + shutdown(SockException(Err_shutdown, "rtmp ring buffer detached")); } -std::shared_ptr HttpSession::getSharedPtr(){ +std::shared_ptr HttpSession::getSharedPtr() { return dynamic_pointer_cast(shared_from_this()); } From 52c7bc1d348e3c4b50e9a321675f55a4267e2ac1 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 10:16:45 +0800 Subject: [PATCH 40/66] =?UTF-8?q?=E7=B2=BE=E7=AE=80HttpServer=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpConst.cpp | 4 ++-- src/Http/HttpConst.h | 30 ++++++++++++++++++------------ src/Http/HttpFileManager.cpp | 2 +- src/Http/HttpSession.cpp | 32 +++++++------------------------- src/Http/HttpSession.h | 1 - 5 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/Http/HttpConst.cpp b/src/Http/HttpConst.cpp index 3e4116a7..0f7409ac 100644 --- a/src/Http/HttpConst.cpp +++ b/src/Http/HttpConst.cpp @@ -18,7 +18,7 @@ using namespace toolkit; namespace mediakit{ -const char *getHttpStatusMessage(int status) { +const char *HttpConst::getHttpStatusMessage(int status) { switch (status) { case 100: return "Continue"; case 101: return "Switching Protocol"; @@ -196,7 +196,7 @@ static const char *s_mime_src[][2] = { {"avi", "video/x-msvideo"}, }; -const string &getHttpContentType(const char *name) { +const string& HttpConst::getHttpContentType(const char *name) { const char *dot; dot = strrchr(name, '.'); static StrCaseMap mapType; diff --git a/src/Http/HttpConst.h b/src/Http/HttpConst.h index b68f8be5..bced1a7e 100644 --- a/src/Http/HttpConst.h +++ b/src/Http/HttpConst.h @@ -15,19 +15,25 @@ namespace mediakit{ -/** - * 根据http错误代码获取字符说明 - * @param status 譬如404 - * @return 错误代码字符说明,譬如Not Found - */ -const char *getHttpStatusMessage(int status); +class HttpConst { +public: + HttpConst() = delete; + ~HttpConst() = delete; -/** - * 根据文件后缀返回http mime - * @param name 文件后缀,譬如html - * @return mime值,譬如text/html - */ -const std::string &getHttpContentType(const char *name); + /** + * 根据http错误代码获取字符说明 + * @param status 譬如404 + * @return 错误代码字符说明,譬如Not Found + */ + static const char *getHttpStatusMessage(int status); + + /** + * 根据文件后缀返回http mime + * @param name 文件后缀,譬如html + * @return mime值,譬如text/html + */ + static const std::string &getHttpContentType(const char *name); +}; }//mediakit diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 92f349fc..468acdb7 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -46,7 +46,7 @@ struct HttpCookieAttachment { }; const string &HttpFileManager::getContentType(const char *name) { - return getHttpContentType(name); + return HttpConst::getHttpContentType(name); } static string searchIndexFile(const string &dir){ diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 5a5aa113..347359ed 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -75,9 +75,6 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { return 0; } - // 跨域 - _origin = _parser["Origin"]; - //默认后面数据不是content而是header ssize_t content_len = 0; (this->*(it->second))(content_len); @@ -507,15 +504,6 @@ private: } }; -static const string kDate = "Date"; -static const string kServer = "Server"; -static const string kConnection = "Connection"; -static const string kKeepAlive = "Keep-Alive"; -static const string kContentType = "Content-Type"; -static const string kContentLength = "Content-Length"; -static const string kAccessControlAllowOrigin = "Access-Control-Allow-Origin"; -static const string kAccessControlAllowCredentials = "Access-Control-Allow-Credentials"; - void HttpSession::sendResponse(int code, bool bClose, const char *pcContentType, @@ -541,25 +529,19 @@ void HttpSession::sendResponse(int code, } HttpSession::KeyValue &headerOut = const_cast(header); - headerOut.emplace(kDate, dateStr()); - headerOut.emplace(kServer, kServerName); - headerOut.emplace(kConnection, bClose ? "close" : "keep-alive"); + headerOut.emplace("Date", dateStr()); + headerOut.emplace("Server", kServerName); + headerOut.emplace("Connection", bClose ? "close" : "keep-alive"); if (!bClose) { string keepAliveString = "timeout="; keepAliveString += to_string(keepAliveSec); keepAliveString += ", max=100"; - headerOut.emplace(kKeepAlive, std::move(keepAliveString)); - } - - if (!_origin.empty()) { - // 设置跨域 - headerOut.emplace(kAccessControlAllowOrigin, _origin); - headerOut.emplace(kAccessControlAllowCredentials, "true"); + headerOut.emplace("Keep-Alive", std::move(keepAliveString)); } if (!no_content_length && size >= 0 && (size_t)size < SIZE_MAX) { // 文件长度为固定值,且不是http-flv强制设置Content-Length - headerOut[kContentLength] = to_string(size); + headerOut["Content-Length"] = to_string(size); } if (size && !pcContentType) { @@ -572,7 +554,7 @@ void HttpSession::sendResponse(int code, string strContentType = pcContentType; strContentType += "; charset="; strContentType += charSet; - headerOut.emplace(kContentType, std::move(strContentType)); + headerOut.emplace("Content-Type", std::move(strContentType)); } // 发送http头 @@ -581,7 +563,7 @@ void HttpSession::sendResponse(int code, str += "HTTP/1.1 "; str += to_string(code); str += ' '; - str += getHttpStatusMessage(code); + str += HttpConst::getHttpStatusMessage(code); str += "\r\n"; for (auto &pr : header) { str += pr.first; diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 68b2030d..59255fb8 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -132,7 +132,6 @@ private: bool _live_over_websocket = false; //消耗的总流量 uint64_t _total_bytes_usage = 0; - std::string _origin; Parser _parser; toolkit::Ticker _ticker; TSMediaSource::RingType::RingReader::Ptr _ts_reader; From af0ebf4633cd39585ebcec0a3c68c1896d3ac08f Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 11:04:52 +0800 Subject: [PATCH 41/66] =?UTF-8?q?=E4=BC=98=E5=8C=96Parser=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/source/mk_events_objects.cpp | 12 +++--- api/source/mk_httpclient.cpp | 6 +-- player/test_player.cpp | 3 +- server/WebApi.cpp | 18 ++++---- server/WebHook.cpp | 10 ++--- server/main.cpp | 4 +- src/Common/Parser.cpp | 26 +++++++---- src/Common/Parser.h | 74 ++++++++++++++++---------------- src/Http/HlsParser.cpp | 2 +- src/Http/HttpClient.cpp | 10 ++--- src/Http/HttpFileManager.cpp | 16 +++---- src/Http/HttpSession.cpp | 16 +++---- src/Rtmp/RtmpPlayer.cpp | 2 +- src/Rtmp/RtmpPlayerImp.h | 2 +- src/Rtmp/RtmpPusher.cpp | 2 +- src/Rtsp/RtspPlayer.cpp | 28 ++++++------ src/Rtsp/RtspPlayerImp.h | 2 +- src/Rtsp/RtspPusher.cpp | 16 +++---- src/Rtsp/RtspSession.cpp | 22 +++++----- src/Rtsp/RtspSplitter.cpp | 4 +- tests/test_httpApi.cpp | 12 +++--- tests/test_httpClient.cpp | 12 +++--- tests/test_wsServer.cpp | 2 +- webrtc/WebRtcTransport.cpp | 2 +- 24 files changed, 156 insertions(+), 147 deletions(-) diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index 1903fa05..617151e5 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -86,17 +86,17 @@ API_EXPORT const char* API_CALL mk_mp4_info_get_stream(const mk_mp4_info ctx){ API_EXPORT const char* API_CALL mk_parser_get_method(const mk_parser ctx){ assert(ctx); Parser *parser = (Parser *)ctx; - return parser->Method().c_str(); + return parser->method().c_str(); } API_EXPORT const char* API_CALL mk_parser_get_url(const mk_parser ctx){ assert(ctx); Parser *parser = (Parser *)ctx; - return parser->Url().c_str(); + return parser->url().c_str(); } API_EXPORT const char* API_CALL mk_parser_get_url_params(const mk_parser ctx){ assert(ctx); Parser *parser = (Parser *)ctx; - return parser->Params().c_str(); + return parser->params().c_str(); } API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,const char *key){ assert(ctx && key); @@ -106,7 +106,7 @@ API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,cons API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx){ assert(ctx); Parser *parser = (Parser *)ctx; - return parser->Tail().c_str(); + return parser->protocol().c_str(); } API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key){ assert(ctx && key); @@ -117,9 +117,9 @@ API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_ assert(ctx); Parser *parser = (Parser *)ctx; if(length){ - *length = parser->Content().size(); + *length = parser->content().size(); } - return parser->Content().c_str(); + return parser->content().c_str(); } ///////////////////////////////////////////MediaInfo///////////////////////////////////////////// diff --git a/api/source/mk_httpclient.cpp b/api/source/mk_httpclient.cpp index 9c52cd33..43de7a33 100755 --- a/api/source/mk_httpclient.cpp +++ b/api/source/mk_httpclient.cpp @@ -108,7 +108,7 @@ API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,cons API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx){ assert(ctx); HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx; - return (*obj)->response().Url().c_str(); + return (*obj)->response().status().c_str(); } API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key){ @@ -121,9 +121,9 @@ API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requ assert(ctx); HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx; if(length){ - *length = (*obj)->response().Content().size(); + *length = (*obj)->response().content().size(); } - return (*obj)->response().Content().c_str(); + return (*obj)->response().content().c_str(); } API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx){ diff --git a/player/test_player.cpp b/player/test_player.cpp index 126280a0..faf29af7 100644 --- a/player/test_player.cpp +++ b/player/test_player.cpp @@ -56,8 +56,7 @@ int main(int argc, char *argv[]) { if (argc < 3) { ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n" - << "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n" - << endl; + << "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"; return 0; } diff --git a/server/WebApi.cpp b/server/WebApi.cpp index f1bc4f8f..64eec188 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -130,7 +130,7 @@ static HttpApi toApi(const function &cb) { //参数解析成json对象然后处理 Json::Value args; Json::Reader reader; - reader.parse(parser.Content(), args); + reader.parse(parser.content(), args); cb(sender, headerOut, HttpAllArgs(parser, args), val, invoker); }; @@ -152,7 +152,7 @@ static HttpApi toApi(const function &cb) { Json::Value val; val["code"] = API::Success; - cb(sender, headerOut, HttpAllArgs(parser, (string &)parser.Content()), val, invoker); + cb(sender, headerOut, HttpAllArgs(parser, (string &)parser.content()), val, invoker); }; } @@ -191,13 +191,13 @@ void api_regist(const string &api_path, const function> jsonArgs; auto keys = jsonArgs.getMemberNames(); @@ -231,7 +231,7 @@ static inline void addHttpListener(){ GET_CONFIG(bool, api_debug, API::kApiDebug); //注册监听kBroadcastHttpRequest事件 NoticeCenter::Instance().addListener(&web_api_tag, Broadcast::kBroadcastHttpRequest, [](BroadcastHttpRequestArgs) { - auto it = s_map_api.find(parser.Url()); + auto it = s_map_api.find(parser.url()); if (it == s_map_api.end()) { return; } @@ -248,14 +248,14 @@ static inline void addHttpListener(){ } LogContextCapture log(getLogger(), toolkit::LTrace, __FILE__, "http api debug", __LINE__); - log << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"; + log << "\r\n# request:\r\n" << parser.method() << " " << parser.fullUrl() << "\r\n"; log << "# header:\r\n"; for (auto &pr : parser.getHeader()) { log << pr.first << " : " << pr.second << "\r\n"; } - auto &content = parser.Content(); + auto &content = parser.content(); log << "# content:\r\n" << (content.size() > 4 * 1024 ? content.substr(0, 4 * 1024) : content) << "\r\n"; if (size > 0 && size < 4 * 1024) { @@ -1637,7 +1637,7 @@ void installWebApi() { CHECK_ARGS("app", "stream"); return StrPrinter << RTC_SCHEMA << "://" << _args["Host"] << "/" << _args["app"] << "/" - << _args["stream"] << "?" << _args.getParser().Params() + "&session=" + _session_id; + << _args["stream"] << "?" << _args.getParser().params() + "&session=" + _session_id; } private: @@ -1700,7 +1700,7 @@ void installWebApi() { api_regist(delete_webrtc_url, [](API_ARGS_MAP_ASYNC) { CHECK_ARGS("id", "token"); - CHECK(allArgs.getParser().Method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().Method()); + CHECK(allArgs.getParser().method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().method()); auto obj = WebRtcTransportManager::Instance().getItem(allArgs["id"]); if (!obj) { invoker(404, headerOut, "id not found"); diff --git a/server/WebHook.cpp b/server/WebHook.cpp index e4b381ac..d50b1db9 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -100,14 +100,14 @@ static void parse_http_response(const SockException &ex, const Parser &res, cons fun(Json::nullValue, errStr, should_retry); return; } - if (res.Url() != "200") { - auto errStr = StrPrinter << "[bad http status code]:" << res.Url() << endl; + if (res.status() != "200") { + auto errStr = StrPrinter << "[bad http status code]:" << res.status() << endl; fun(Json::nullValue, errStr, should_retry); return; } Value result; try { - stringstream ss(res.Content()); + stringstream ss(res.content()); ss >> result; } catch (std::exception &ex) { auto errStr = StrPrinter << "[parse json failed]:" << ex.what() << endl; @@ -614,7 +614,7 @@ void installWebHook() { // 追踪用户的目的是为了缓存上次鉴权结果,减少鉴权次数,提高性能 NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastHttpAccess, [](BroadcastHttpAccessArgs) { GET_CONFIG(string, hook_http_access, Hook::kOnHttpAccess); - if (sender.get_peer_ip() == "127.0.0.1" || parser.Params() == hook_adminparams) { + if (sender.get_peer_ip() == "127.0.0.1" || parser.params() == hook_adminparams) { // 如果是本机或超级管理员访问,那么不做访问鉴权;权限有效期1个小时 invoker("", "", 60 * 60); return; @@ -632,7 +632,7 @@ void installWebHook() { body["id"] = sender.getIdentifier(); body["path"] = path; body["is_dir"] = is_dir; - body["params"] = parser.Params(); + body["params"] = parser.params(); for (auto &pr : parser.getHeader()) { body[string("header.") + pr.first] = pr.second; } diff --git a/server/main.cpp b/server/main.cpp index 9a190935..750560ba 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -363,8 +363,8 @@ int start_main(int argc,char *argv[]) { #endif//defined(ENABLE_SRT) } catch (std::exception &ex) { - WarnL << "端口占用或无权限:" << ex.what() << endl; - ErrorL << "程序启动失败,请修改配置文件中端口号后重试!" << endl; + WarnL << "端口占用或无权限:" << ex.what(); + ErrorL << "程序启动失败,请修改配置文件中端口号后重试!"; sleep(1); #if !defined(_WIN32) if (pid != getpid() && kill_parent_if_failed) { diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index 2620ba31..6aa75a48 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -44,7 +44,7 @@ string FindField(const char* buf, const char* start, const char *end ,size_t buf void Parser::Parse(const char *buf) { //解析 const char *start = buf; - Clear(); + clear(); while (true) { auto line = FindField(start, NULL, "\r\n"); if (line.size() == 0) { @@ -77,25 +77,33 @@ void Parser::Parse(const char *buf) { } } -const string &Parser::Method() const { +const string &Parser::method() const { return _strMethod; } -const string &Parser::Url() const { +const string &Parser::url() const { return _strUrl; } -string Parser::FullUrl() const { +const std::string &Parser::status() const { + return url(); +} + +string Parser::fullUrl() const { if (_params.empty()) { return _strUrl; } return _strUrl + "?" + _params; } -const string &Parser::Tail() const { +const string &Parser::protocol() const { return _strTail; } +const std::string &Parser::statusStr() const { + return protocol(); +} + const string &Parser::operator[](const char *name) const { auto it = _mapHeaders.find(name); if (it == _mapHeaders.end()) { @@ -104,11 +112,11 @@ const string &Parser::operator[](const char *name) const { return it->second; } -const string &Parser::Content() const { +const string &Parser::content() const { return _strContent; } -void Parser::Clear() { +void Parser::clear() { _strMethod.clear(); _strUrl.clear(); _params.clear(); @@ -118,7 +126,7 @@ void Parser::Clear() { _mapUrlArgs.clear(); } -const string &Parser::Params() const { +const string &Parser::params() const { return _params; } @@ -159,7 +167,7 @@ StrCaseMap Parser::parseArgs(const string &str, const char *pair_delim, const ch } return ret; } -std::string Parser::merge_url(const string &base_url, const string &path) { +std::string Parser::mergeUrl(const string &base_url, const string &path) { //以base_url为基础, 合并path路径生成新的url, path支持相对路径和绝对路径 if (base_url.empty()) { return path; diff --git a/src/Common/Parser.h b/src/Common/Parser.h index 6f312664..fa8298be 100644 --- a/src/Common/Parser.h +++ b/src/Common/Parser.h @@ -17,15 +17,13 @@ namespace mediakit { -//从字符串中提取子字符串 +// 从字符串中提取子字符串 std::string FindField(const char *buf, const char *start, const char *end, size_t bufSize = 0); -//把url解析为主机地址和端口号,兼容ipv4/ipv6/dns -void splitUrl(const std::string &url, std::string &host, uint16_t& port); +// 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns +void splitUrl(const std::string &url, std::string &host, uint16_t &port); struct StrCaseCompare { - bool operator()(const std::string &__x, const std::string &__y) const { - return strcasecmp(__x.data(), __y.data()) < 0; - } + bool operator()(const std::string &__x, const std::string &__y) const { return strcasecmp(__x.data(), __y.data()) < 0; } }; class StrCaseMap : public std::multimap { @@ -42,7 +40,7 @@ public: return it->second; } - template + template void emplace(const std::string &k, V &&v) { auto it = find(k); if (it != end()) { @@ -51,61 +49,65 @@ public: Super::emplace(k, std::forward(v)); } - template + template void emplace_force(const std::string k, V &&v) { Super::emplace(k, std::forward(v)); } }; -//rtsp/http/sip解析类 +// rtsp/http/sip解析类 class Parser { public: Parser() = default; ~Parser() = default; - //解析信令 + // 解析信令 void Parse(const char *buf); - //获取命令字 - const std::string &Method() const; + // 获取命令字 + const std::string &method() const; - //获取中间url,不包含?后面的参数 - const std::string &Url() const; + // 请求时,获取中间url,不包含?后面的参数 + const std::string &url() const; + // 回复时,获取状态码 + const std::string &status() const; - //获取中间url,包含?后面的参数 - std::string FullUrl() const; + // 获取中间url,包含?后面的参数 + std::string fullUrl() const; - //获取命令协议名 - const std::string &Tail() const; + // 请求时,获取协议名 + const std::string &protocol() const; + // 回复时,获取状态字符串 + const std::string &statusStr() const; - //根据header key名,获取请求header value值 + // 根据header key名,获取请求header value值 const std::string &operator[](const char *name) const; - //获取http body或sdp - const std::string &Content() const; + // 获取http body或sdp + const std::string &content() const; - //清空,为了重用 - void Clear(); + // 清空,为了重用 + void clear(); - //获取?后面的参数 - const std::string &Params() const; + // 获取?后面的参数 + const std::string ¶ms() const; - //重新设置url + // 重新设置url void setUrl(std::string url); - //重新设置content + // 重新设置content void setContent(std::string content); - //获取header列表 + // 获取header列表 StrCaseMap &getHeader() const; - //获取url参数列表 + // 获取url参数列表 StrCaseMap &getUrlArgs() const; - //解析?后面的参数 + // 解析?后面的参数 static StrCaseMap parseArgs(const std::string &str, const char *pair_delim = "&", const char *key_delim = "="); - static std::string merge_url(const std::string &base_url, const std::string &path); + static std::string mergeUrl(const std::string &base_url, const std::string &path); private: std::string _strMethod; @@ -118,8 +120,8 @@ private: mutable StrCaseMap _mapUrlArgs; }; -//解析rtsp url的工具类 -class RtspUrl{ +// 解析rtsp url的工具类 +class RtspUrl { public: bool _is_ssl; uint16_t _port; @@ -134,9 +136,9 @@ public: void parse(const std::string &url); private: - void setup(bool,const std::string &, const std::string &, const std::string &); + void setup(bool, const std::string &, const std::string &, const std::string &); }; -}//namespace mediakit +} // namespace mediakit -#endif //ZLMEDIAKIT_PARSER_H +#endif // ZLMEDIAKIT_PARSER_H diff --git a/src/Http/HlsParser.cpp b/src/Http/HlsParser.cpp index c7b45e3e..5cbd9195 100644 --- a/src/Http/HlsParser.cpp +++ b/src/Http/HlsParser.cpp @@ -37,7 +37,7 @@ bool HlsParser::parse(const string &http_url, const string &m3u8) { if ((_is_m3u8_inner || extinf_dur != 0) && line[0] != '#') { segment.duration = extinf_dur; - segment.url = Parser::merge_url(http_url, line); + segment.url = Parser::mergeUrl(http_url, line); if (!_is_m3u8_inner) { //ts按照先后顺序排序 ts_map.emplace(index++, segment); diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 143ae04d..daf757ad 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -100,7 +100,7 @@ void HttpClient::clearResponse() { _header_recved = false; _recved_body_size = 0; _total_body_size = 0; - _parser.Clear(); + _parser.clear(); _chunked_splitter = nullptr; _wait_header.resetTime(); _wait_body.resetTime(); @@ -182,19 +182,19 @@ void HttpClient::onError(const SockException &ex) { ssize_t HttpClient::onRecvHeader(const char *data, size_t len) { _parser.Parse(data); - if (_parser.Url() == "302" || _parser.Url() == "301" || _parser.Url() == "303") { - auto new_url = Parser::merge_url(_url, _parser["Location"]); + if (_parser.status() == "302" || _parser.status() == "301" || _parser.status() == "303") { + auto new_url = Parser::mergeUrl(_url, _parser["Location"]); if (new_url.empty()) { throw invalid_argument("未找到Location字段(跳转url)"); } - if (onRedirectUrl(new_url, _parser.Url() == "302")) { + if (onRedirectUrl(new_url, _parser.status() == "302")) { HttpClient::sendRequest(new_url); return 0; } } checkCookie(_parser.getHeader()); - onResponseHeader(_parser.Url(), _parser.getHeader()); + onResponseHeader(_parser.status(), _parser.getHeader()); _header_recved = true; if (_parser["Transfer-Encoding"] == "chunked") { diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 468acdb7..9a22e64f 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -240,8 +240,8 @@ public: static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo &media_info, bool is_dir, const function &callback) { //获取用户唯一id - auto uid = parser.Params(); - auto path = parser.Url(); + auto uid = parser.params(); + auto path = parser.url(); //先根据http头中的cookie字段获取cookie HttpServerCookie::Ptr cookie = HttpCookieManager::Instance().getCookie(kCookieName, parser.getHeader()); @@ -268,7 +268,7 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo return; } //上次鉴权失败,但是如果url参数发生变更,那么也重新鉴权下 - if (parser.Params().empty() || parser.Params() == cookie->getUid()) { + if (parser.params().empty() || parser.params() == cookie->getUid()) { //url参数未变,或者本来就没有url参数,那么判断本次请求为重复请求,无访问权限 callback(attach._err_msg, update_cookie ? cookie : nullptr); return; @@ -469,11 +469,11 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, Sess if (it != virtualPathMap.end()) { //访问的是virtualPath path = it->second; - url = parser.Url().substr(1 + media_info.app.size()); + url = parser.url().substr(1 + media_info.app.size()); } else { //访问的是rootPath path = rootPath; - url = parser.Url(); + url = parser.url(); } for (auto &ch : url) { if (ch == '\\') { @@ -493,7 +493,7 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, Sess * @param cb 回调对象 */ void HttpFileManager::onAccessPath(Session &sender, Parser &parser, const HttpFileManager::invoker &cb) { - auto fullUrl = string(HTTP_SCHEMA) + "://" + parser["Host"] + parser.FullUrl(); + auto fullUrl = string(HTTP_SCHEMA) + "://" + parser["Host"] + parser.fullUrl(); MediaInfo media_info(fullUrl); auto file_path = getFilePath(parser, media_info, sender); if (file_path.size() == 0) { @@ -506,13 +506,13 @@ void HttpFileManager::onAccessPath(Session &sender, Parser &parser, const HttpFi if (!indexFile.empty()) { //发现该文件夹下有index文件 file_path = pathCat(file_path, indexFile); - parser.setUrl(pathCat(parser.Url(), indexFile)); + parser.setUrl(pathCat(parser.url(), indexFile)); accessFile(sender, parser, media_info, file_path, cb); return; } string strMenu; //生成文件夹菜单索引 - if (!makeFolderMenu(parser.Url(), file_path, strMenu)) { + if (!makeFolderMenu(parser.url(), file_path, strMenu)) { //文件夹不存在 sendNotFound(cb); return; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 347359ed..7170fd63 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -64,10 +64,10 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { }); _parser.Parse(header); - CHECK(_parser.Url()[0] == '/'); + CHECK(_parser.url()[0] == '/'); urlDecode(_parser); - string cmd = _parser.Method(); + string cmd = _parser.method(); auto it = s_func_map.find(cmd); if (it == s_func_map.end()) { WarnP(this) << "不支持该命令:" << cmd; @@ -80,7 +80,7 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { (this->*(it->second))(content_len); // 清空解析器节省内存 - _parser.Clear(); + _parser.clear(); // 返回content长度 return content_len; } @@ -173,7 +173,7 @@ bool HttpSession::checkWebSocket() { } bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix, const function &cb) { - std::string url = _parser.Url(); + std::string url = _parser.url(); auto it = _parser.getUrlArgs().find("schema"); if (it != _parser.getUrlArgs().end()) { if (strcasecmp(it->second.c_str(), schema.c_str())) { @@ -191,9 +191,9 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix } // 带参数的url - if (!_parser.Params().empty()) { + if (!_parser.params().empty()) { url += "?"; - url += _parser.Params(); + url += _parser.params(); } // 解析带上协议+参数完整的url @@ -616,7 +616,7 @@ string HttpSession::urlDecode(const string &str) { } void HttpSession::urlDecode(Parser &parser) { - parser.setUrl(urlDecode(parser.Url())); + parser.setUrl(urlDecode(parser.url())); for (auto &pr : _parser.getUrlArgs()) { const_cast(pr.second) = urlDecode(pr.second); } @@ -682,7 +682,7 @@ void HttpSession::Handle_Req_POST(ssize_t &content_len) { // 触发http事件,emitHttpEvent内部会选择是否关闭连接 emitHttpEvent(true); // 清空数据,节省内存 - _parser.Clear(); + _parser.clear(); // content已经接收完毕 return false; }; diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index ffaf0dfc..71e3ecb9 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -27,7 +27,7 @@ namespace mediakit { RtmpPlayer::RtmpPlayer(const EventPoller::Ptr &poller) : TcpClient(poller) {} RtmpPlayer::~RtmpPlayer() { - DebugL << endl; + DebugL; } void RtmpPlayer::teardown() { diff --git a/src/Rtmp/RtmpPlayerImp.h b/src/Rtmp/RtmpPlayerImp.h index c09d460b..25b33741 100644 --- a/src/Rtmp/RtmpPlayerImp.h +++ b/src/Rtmp/RtmpPlayerImp.h @@ -27,7 +27,7 @@ public: RtmpPlayerImp(const toolkit::EventPoller::Ptr &poller) : Super(poller) {}; ~RtmpPlayerImp() override { - DebugL << std::endl; + DebugL; } float getProgress() const override { diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index 31143dcf..9379d015 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -27,7 +27,7 @@ RtmpPusher::RtmpPusher(const EventPoller::Ptr &poller, const RtmpMediaSource::Pt RtmpPusher::~RtmpPusher() { teardown(); - DebugL << endl; + DebugL; } void RtmpPusher::teardown() { diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index e99c8a3e..a58880d3 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -33,7 +33,7 @@ RtspPlayer::RtspPlayer(const EventPoller::Ptr &poller) : TcpClient(poller) {} RtspPlayer::~RtspPlayer(void) { - DebugL << endl; + DebugL; } void RtspPlayer::sendTeardown() { @@ -170,11 +170,11 @@ bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) { bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser) { string authInfo = parser["WWW-Authenticate"]; // 发送DESCRIBE命令后的回复 - if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) { + if ((parser.status() == "401") && handleAuthenticationFailure(authInfo)) { sendOptions(); return false; } - if (parser.Url() == "302" || parser.Url() == "301") { + if (parser.status() == "302" || parser.status() == "301") { auto newUrl = parser["Location"]; if (newUrl.empty()) { throw std::runtime_error("未找到Location字段(跳转url)"); @@ -182,8 +182,8 @@ bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser) { play(newUrl); return false; } - if (parser.Url() != "200") { - throw std::runtime_error(StrPrinter << cmd << ":" << parser.Url() << " " << parser.Tail() << endl); + if (parser.status() != "200") { + throw std::runtime_error(StrPrinter << cmd << ":" << parser.status() << " " << parser.statusStr() << endl); } return true; } @@ -201,7 +201,7 @@ void RtspPlayer::handleResDESCRIBE(const Parser &parser) { } // 解析sdp - SdpParser sdpParser(parser.Content()); + SdpParser sdpParser(parser.content()); string sdp; auto play_track = (TrackType)((int)(*this)[Client::kPlayTrack] - 1); @@ -267,8 +267,8 @@ void RtspPlayer::sendSetup(unsigned int track_idx) { } void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { - if (parser.Url() != "200") { - throw std::runtime_error(StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl); + if (parser.status() != "200") { + throw std::runtime_error(StrPrinter << "SETUP:" << parser.status() << " " << parser.statusStr() << endl); } if (track_idx == 0) { _session_id = parser["Session"]; @@ -446,14 +446,14 @@ void RtspPlayer::speed(float speed) { } void RtspPlayer::handleResPAUSE(const Parser &parser, int type) { - if (parser.Url() != "200") { + if (parser.status() != "200") { switch (type) { - case type_pause: WarnL << "Pause failed:" << parser.Url() << " " << parser.Tail() << endl; break; + case type_pause: WarnL << "Pause failed:" << parser.status() << " " << parser.statusStr(); break; case type_play: - WarnL << "Play failed:" << parser.Url() << " " << parser.Tail() << endl; - onPlayResult_l(SockException(Err_shutdown, StrPrinter << "rtsp play failed:" << parser.Url() << " " << parser.Tail()), !_play_check_timer); + WarnL << "Play failed:" << parser.status() << " " << parser.statusStr(); + onPlayResult_l(SockException(Err_shutdown, StrPrinter << "rtsp play failed:" << parser.status() << " " << parser.statusStr()), !_play_check_timer); break; - case type_seek: WarnL << "Seek failed:" << parser.Url() << " " << parser.Tail() << endl; break; + case type_seek: WarnL << "Seek failed:" << parser.status() << " " << parser.statusStr(); break; } return; } @@ -487,7 +487,7 @@ void RtspPlayer::onWholeRtspPacket(Parser &parser) { if (func) { func(parser); } - parser.Clear(); + parser.clear(); } catch (std::exception &err) { // 定时器_pPlayTimer为空后表明握手结束了 onPlayResult_l(SockException(Err_other, err.what()), !_play_check_timer); diff --git a/src/Rtsp/RtspPlayerImp.h b/src/Rtsp/RtspPlayerImp.h index 9243696a..ad0eaa12 100644 --- a/src/Rtsp/RtspPlayerImp.h +++ b/src/Rtsp/RtspPlayerImp.h @@ -28,7 +28,7 @@ public: RtspPlayerImp(const toolkit::EventPoller::Ptr &poller) : Super(poller) {} ~RtspPlayerImp() override { - DebugL << std::endl; + DebugL; } float getProgress() const override { diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 01727adc..e0855b19 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -26,7 +26,7 @@ RtspPusher::RtspPusher(const EventPoller::Ptr &poller, const RtspMediaSource::Pt RtspPusher::~RtspPusher() { teardown(); - DebugL << endl; + DebugL; } void RtspPusher::sendTeardown(){ @@ -147,7 +147,7 @@ void RtspPusher::onWholeRtspPacket(Parser &parser) { if (func) { func(parser); } - parser.Clear(); + parser.clear(); } void RtspPusher::onRtpPacket(const char *data, size_t len) { @@ -188,11 +188,11 @@ void RtspPusher::sendAnnounce() { void RtspPusher::handleResAnnounce(const Parser &parser) { string authInfo = parser["WWW-Authenticate"]; //发送DESCRIBE命令后的回复 - if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) { + if ((parser.status() == "401") && handleAuthenticationFailure(authInfo)) { sendAnnounce(); return; } - if (parser.Url() == "302") { + if (parser.status() == "302") { auto newUrl = parser["Location"]; if (newUrl.empty()) { throw std::runtime_error("未找到Location字段(跳转url)"); @@ -200,8 +200,8 @@ void RtspPusher::handleResAnnounce(const Parser &parser) { publish(newUrl); return; } - if (parser.Url() != "200") { - throw std::runtime_error(StrPrinter << "ANNOUNCE:" << parser.Url() << " " << parser.Tail()); + if (parser.status() != "200") { + throw std::runtime_error(StrPrinter << "ANNOUNCE:" << parser.status() << " " << parser.statusStr()); } _content_base = parser["Content-Base"]; @@ -285,8 +285,8 @@ void RtspPusher::sendSetup(unsigned int track_idx) { } void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) { - if (parser.Url() != "200") { - throw std::runtime_error(StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl); + if (parser.status() != "200") { + throw std::runtime_error(StrPrinter << "SETUP:" << parser.status() << " " << parser.statusStr() << endl); } if (track_idx == 0) { _session_id = parser["Session"]; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 13f57d54..33c59283 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -128,11 +128,11 @@ void RtspSession::onRecv(const Buffer::Ptr &buf) { } void RtspSession::onWholeRtspPacket(Parser &parser) { - string method = parser.Method(); //提取出请求命令字 + string method = parser.method(); //提取出请求命令字 _cseq = atoi(parser["CSeq"].data()); if (_content_base.empty() && method != "GET") { - _content_base = parser.Url(); - _media_info.parse(parser.FullUrl()); + _content_base = parser.url(); + _media_info.parse(parser.fullUrl()); _media_info.schema = RTSP_SCHEMA; } @@ -160,7 +160,7 @@ void RtspSession::onWholeRtspPacket(Parser &parser) { } (this->*(it->second))(parser); - parser.Clear(); + parser.clear(); } void RtspSession::onRtpPacket(const char *data, size_t len) { @@ -187,7 +187,7 @@ void RtspSession::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char * } ssize_t RtspSession::getContentLength(Parser &parser) { - if(parser.Method() == "POST"){ + if(parser.method() == "POST"){ //http post请求的content数据部分是base64编码后的rtsp请求信令包 return remainDataSize(); } @@ -200,7 +200,7 @@ void RtspSession::handleReq_Options(const Parser &parser) { } void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { - auto full_url = parser.FullUrl(); + auto full_url = parser.fullUrl(); _content_base = full_url; if (end_with(full_url, ".sdp")) { //去除.sdp后缀,防止EasyDarwin推流器强制添加.sdp后缀 @@ -250,7 +250,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { throw SockException(Err_shutdown, err); } - SdpParser sdpParser(parser.Content()); + SdpParser sdpParser(parser.content()); _sessionid = makeRandStr(12); _sdp_track = sdpParser.getAvailableTrack(); if (_sdp_track.empty()) { @@ -270,7 +270,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { //获取所有权 _push_src_ownership = _push_src->getOwnership(); _push_src->setProtocolOption(option); - _push_src->setSdp(parser.Content()); + _push_src->setSdp(parser.content()); } _push_src->setListener(static_pointer_cast(shared_from_this())); @@ -628,7 +628,7 @@ void RtspSession::send_SessionNotFound() { void RtspSession::handleReq_Setup(const Parser &parser) { //处理setup命令,该函数可能进入多次 - int trackIdx = getTrackIndexByControlUrl(parser.FullUrl()); + int trackIdx = getTrackIndexByControlUrl(parser.fullUrl()); SdpTrack::Ptr &trackRef = _sdp_track[trackIdx]; if (trackRef->_inited) { //已经初始化过该Track @@ -915,9 +915,9 @@ void RtspSession::handleReq_Post(const Parser &parser) { }); }; - if(!parser.Content().empty()){ + if(!parser.content().empty()){ //http poster后面的粘包 - _on_recv(std::make_shared(parser.Content())); + _on_recv(std::make_shared(parser.content())); } sendRtspResponse("200 OK", diff --git a/src/Rtsp/RtspSplitter.cpp b/src/Rtsp/RtspSplitter.cpp index a29e1249..e88af355 100644 --- a/src/Rtsp/RtspSplitter.cpp +++ b/src/Rtsp/RtspSplitter.cpp @@ -65,7 +65,7 @@ ssize_t RtspSplitter::onRecvHeader(const char *data, size_t len) { auto ret = getContentLength(_parser); if(ret == 0){ onWholeRtspPacket(_parser); - _parser.Clear(); + _parser.clear(); } return ret; } @@ -73,7 +73,7 @@ ssize_t RtspSplitter::onRecvHeader(const char *data, size_t len) { void RtspSplitter::onRecvContent(const char *data, size_t len) { _parser.setContent(string(data,len)); onWholeRtspPacket(_parser); - _parser.Clear(); + _parser.clear(); } void RtspSplitter::enableRecvRtp(bool enable) { diff --git a/tests/test_httpApi.cpp b/tests/test_httpApi.cpp index c03531c6..081311db 100644 --- a/tests/test_httpApi.cpp +++ b/tests/test_httpApi.cpp @@ -43,7 +43,7 @@ void initEventListener(){ static onceToken s_token([](){ NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastHttpRequest,[](BroadcastHttpRequestArgs){ //const Parser &parser,HttpSession::HttpResponseInvoker &invoker,bool &consumed - if(strstr(parser.Url().data(),"/api/") != parser.Url().data()){ + if(strstr(parser.url().data(),"/api/") != parser.url().data()){ return; } //url以"/api/起始,说明是http api" @@ -51,11 +51,11 @@ void initEventListener(){ _StrPrinter printer; ////////////////method//////////////////// - printer << "\r\nmethod:\r\n\t" << parser.Method(); + printer << "\r\nmethod:\r\n\t" << parser.method(); ////////////////url///////////////// - printer << "\r\nurl:\r\n\t" << parser.Url(); + printer << "\r\nurl:\r\n\t" << parser.url(); ////////////////protocol///////////////// - printer << "\r\nprotocol:\r\n\t" << parser.Tail(); + printer << "\r\nprotocol:\r\n\t" << parser.protocol(); ///////////////args////////////////// printer << "\r\nargs:\r\n"; for(auto &pr : parser.getUrlArgs()){ @@ -67,7 +67,7 @@ void initEventListener(){ printer << "\t" << pr.first << " : " << pr.second << "\r\n"; } ////////////////content///////////////// - printer << "\r\ncontent:\r\n" << parser.Content(); + printer << "\r\ncontent:\r\n" << parser.content(); auto contentOut = printer << endl; ////////////////我们测算异步回复,当然你也可以同步回复///////////////// @@ -113,7 +113,7 @@ int main(int argc,char *argv[]){ TcpServer::Ptr httpsSrv(new TcpServer()); httpsSrv->start(mINI::Instance()[Http::kSSLPort]);//默认443 - InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl; + InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1"; sem.wait(); return 0; diff --git a/tests/test_httpClient.cpp b/tests/test_httpClient.cpp index f218318b..9c581a1c 100644 --- a/tests/test_httpClient.cpp +++ b/tests/test_httpClient.cpp @@ -86,9 +86,9 @@ int main(int argc, char *argv[]) { for (auto &pr: parser.getHeader()) { printer << pr.first << ":" << pr.second << "\r\n"; } - InfoL << "status:" << parser.Url() << "\r\n" + InfoL << "status:" << parser.status() << "\r\n" << "header:\r\n" << (printer << endl) - << "\r\nbody:" << parser.Content(); + << "\r\nbody:" << parser.content(); } }); @@ -122,9 +122,9 @@ int main(int argc, char *argv[]) { for (auto &pr: parser.getHeader()) { printer << pr.first << ":" << pr.second << "\r\n"; } - InfoL << "status:" << parser.Url() << "\r\n" + InfoL << "status:" << parser.status() << "\r\n" << "header:\r\n" << (printer << endl) - << "\r\nbody:" << parser.Content(); + << "\r\nbody:" << parser.content(); } }); @@ -159,9 +159,9 @@ int main(int argc, char *argv[]) { for (auto &pr: parser.getHeader()) { printer << pr.first << ":" << pr.second << "\r\n"; } - InfoL << "status:" << parser.Url() << "\r\n" + InfoL << "status:" << parser.status() << "\r\n" << "header:\r\n" << (printer << endl) - << "\r\nbody:" << parser.Content(); + << "\r\nbody:" << parser.content(); } }); diff --git a/tests/test_wsServer.cpp b/tests/test_wsServer.cpp index 90dd645e..bd46787a 100644 --- a/tests/test_wsServer.cpp +++ b/tests/test_wsServer.cpp @@ -83,7 +83,7 @@ struct EchoSessionCreator { //返回的Session必须派生于SendInterceptor,可以返回null(拒绝连接) Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock) { // return nullptr; - if (header.Url() == "/") { + if (header.url() == "/") { return std::make_shared >(header, parent, pSock); } return std::make_shared >(header, parent, pSock); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index f7b60cf1..031710e8 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -323,7 +323,7 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup if (RTC::StunPacket::IsStun((const uint8_t *)buf, len)) { std::unique_ptr packet(RTC::StunPacket::Parse((const uint8_t *)buf, len)); if (!packet) { - WarnL << "parse stun error" << std::endl; + WarnL << "parse stun error"; return; } _ice_server->ProcessStunPacket(packet.get(), tuple); From 31944a92ad0fa870e133650470dccc3f3a6d374f Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 12:22:28 +0800 Subject: [PATCH 42/66] =?UTF-8?q?=E9=87=8D=E5=86=99Parser=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Parser.cpp | 134 ++++++++++++++++++++------------------ src/Common/Parser.h | 39 ++++++----- src/Http/HttpClient.cpp | 2 +- src/Http/HttpSession.cpp | 4 +- src/Rtsp/RtspSplitter.cpp | 2 +- 5 files changed, 95 insertions(+), 86 deletions(-) diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index 6aa75a48..b47a0013 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -12,17 +12,18 @@ #include "Parser.h" #include "macros.h" #include "Network/sockutil.h" +#include "Common/macros.h" using namespace std; using namespace toolkit; -namespace mediakit{ +namespace mediakit { -string FindField(const char* buf, const char* start, const char *end ,size_t bufSize) { - if(bufSize <=0 ){ - bufSize = strlen(buf); +string FindField(const char *buf, const char *start, const char *end, size_t buf_size) { + if (buf_size <= 0) { + buf_size = strlen(buf); } - const char *msg_start = buf, *msg_end = buf + bufSize; + const char *msg_start = buf, *msg_end = buf + buf_size; size_t len = 0; if (start != NULL) { len = strlen(start); @@ -41,48 +42,52 @@ string FindField(const char* buf, const char* start, const char *end ,size_t buf return string(msg_start, msg_end); } -void Parser::Parse(const char *buf) { - //解析 - const char *start = buf; +void Parser::parse(const char *buf, size_t size) { clear(); + auto ptr = buf; while (true) { - auto line = FindField(start, NULL, "\r\n"); - if (line.size() == 0) { - break; - } - if (start == buf) { - _strMethod = FindField(line.data(), NULL, " "); - auto strFullUrl = FindField(line.data(), " ", " "); - auto args_pos = strFullUrl.find('?'); - if (args_pos != string::npos) { - _strUrl = strFullUrl.substr(0, args_pos); - _params = strFullUrl.substr(args_pos + 1); - _mapUrlArgs = parseArgs(_params); - } else { - _strUrl = strFullUrl; + auto next_line = strstr(ptr, "\r\n"); + CHECK(next_line); + if (ptr == buf) { + auto blank = strchr(ptr, ' '); + CHECK(blank > ptr && blank < next_line); + _method = std::string(ptr, blank); + auto next_blank = strchr(blank + 1, ' '); + CHECK(next_blank && next_blank < next_line); + _url.assign(blank + 1, next_blank); + auto pos = _url.find('?'); + if (pos != string::npos) { + _params = _url.substr(pos + 1); + _url_args = parseArgs(_params); + _url = _url.substr(0, pos); } - _strTail = FindField(line.data(), (strFullUrl + " ").data(), NULL); + _protocol = std::string(next_blank + 1, next_line); } else { - auto field = FindField(line.data(), NULL, ": "); - auto value = FindField(line.data(), ": ", NULL); - if (field.size() != 0) { - _mapHeaders.emplace_force(field, value); + auto pos = strchr(ptr, ':'); + CHECK(pos > ptr && pos < next_line); + std::string key { ptr, pos }; + std::string value; + if (pos[1] == ' ') { + value.assign(pos + 2, next_line); + } else { + value.assign(pos + 1, next_line); } + _headers.emplace_force(trim(std::move(key)), trim(std::move(value))); } - start = start + line.size() + 2; - if (strncmp(start, "\r\n", 2) == 0) { //协议解析完毕 - _strContent = FindField(start, "\r\n", NULL); + ptr = next_line + 2; + if (strncmp(ptr, "\r\n", 2) == 0) { // 协议解析完毕 + _content.assign(ptr + 2, buf + size); break; } } } const string &Parser::method() const { - return _strMethod; + return _method; } const string &Parser::url() const { - return _strUrl; + return _url; } const std::string &Parser::status() const { @@ -91,39 +96,41 @@ const std::string &Parser::status() const { string Parser::fullUrl() const { if (_params.empty()) { - return _strUrl; + return _url; } - return _strUrl + "?" + _params; + return _url + "?" + _params; } const string &Parser::protocol() const { - return _strTail; + return _protocol; } const std::string &Parser::statusStr() const { return protocol(); } +static std::string kNull; + const string &Parser::operator[](const char *name) const { - auto it = _mapHeaders.find(name); - if (it == _mapHeaders.end()) { - return _strNull; + auto it = _headers.find(name); + if (it == _headers.end()) { + return kNull; } return it->second; } const string &Parser::content() const { - return _strContent; + return _content; } void Parser::clear() { - _strMethod.clear(); - _strUrl.clear(); + _method.clear(); + _url.clear(); _params.clear(); - _strTail.clear(); - _strContent.clear(); - _mapHeaders.clear(); - _mapUrlArgs.clear(); + _protocol.clear(); + _content.clear(); + _headers.clear(); + _url_args.clear(); } const string &Parser::params() const { @@ -131,44 +138,46 @@ const string &Parser::params() const { } void Parser::setUrl(string url) { - this->_strUrl = std::move(url); + _url = std::move(url); } void Parser::setContent(string content) { - this->_strContent = std::move(content); + _content = std::move(content); } StrCaseMap &Parser::getHeader() const { - return _mapHeaders; + return _headers; } StrCaseMap &Parser::getUrlArgs() const { - return _mapUrlArgs; + return _url_args; } StrCaseMap Parser::parseArgs(const string &str, const char *pair_delim, const char *key_delim) { StrCaseMap ret; auto arg_vec = split(str, pair_delim); - for (string &key_val : arg_vec) { + for (auto &key_val : arg_vec) { if (key_val.empty()) { - //忽略 + // 忽略 continue; } - auto key = trim(FindField(key_val.data(), NULL, key_delim)); - if (!key.empty()) { - auto val = trim(FindField(key_val.data(), key_delim, NULL)); - ret.emplace_force(key, val); + auto pos = key_val.find(key_delim); + if (pos != string::npos) { + auto key = trim(std::string(key_val, 0, pos)); + auto val = trim(key_val.substr(pos + strlen(key_delim))); + ret.emplace_force(std::move(key), std::move(val)); } else { trim(key_val); if (!key_val.empty()) { - ret.emplace_force(key_val, ""); + ret.emplace_force(std::move(key_val), ""); } } } return ret; } + std::string Parser::mergeUrl(const string &base_url, const string &path) { - //以base_url为基础, 合并path路径生成新的url, path支持相对路径和绝对路径 + // 以base_url为基础, 合并path路径生成新的url, path支持相对路径和绝对路径 if (base_url.empty()) { return path; } @@ -242,21 +251,22 @@ std::string Parser::mergeUrl(const string &base_url, const string &path) { } return final_url.str(); } + void RtspUrl::parse(const string &strUrl) { auto schema = FindField(strUrl.data(), nullptr, "://"); bool is_ssl = strcasecmp(schema.data(), "rtsps") == 0; - //查找"://"与"/"之间的字符串,用于提取用户名密码 + // 查找"://"与"/"之间的字符串,用于提取用户名密码 auto middle_url = FindField(strUrl.data(), "://", "/"); if (middle_url.empty()) { middle_url = FindField(strUrl.data(), "://", nullptr); } auto pos = middle_url.rfind('@'); if (pos == string::npos) { - //并没有用户名密码 + // 并没有用户名密码 return setup(is_ssl, strUrl, "", ""); } - //包含用户名密码 + // 包含用户名密码 auto user_pwd = middle_url.substr(0, pos); auto suffix = strUrl.substr(schema.size() + 3 + pos + 1); auto url = StrPrinter << "rtsp://" << suffix << endl; @@ -297,7 +307,7 @@ void splitUrl(const std::string &url, std::string &host, uint16_t &port) { CHECK(!url.empty(), "empty url"); auto pos = url.rfind(':'); if (pos == string::npos || url.back() == ']') { - //没有冒号,未指定端口;或者是纯粹的ipv6地址 + // 没有冒号,未指定端口;或者是纯粹的ipv6地址 host = url; checkHost(host); return; @@ -320,4 +330,4 @@ static onceToken token([](){ }); #endif -}//namespace mediakit +} // namespace mediakit diff --git a/src/Common/Parser.h b/src/Common/Parser.h index fa8298be..3d81cea3 100644 --- a/src/Common/Parser.h +++ b/src/Common/Parser.h @@ -18,7 +18,7 @@ namespace mediakit { // 从字符串中提取子字符串 -std::string FindField(const char *buf, const char *start, const char *end, size_t bufSize = 0); +std::string FindField(const char *buf, const char *start, const char *end, size_t buf_size = 0); // 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns void splitUrl(const std::string &url, std::string &host, uint16_t &port); @@ -40,18 +40,18 @@ public: return it->second; } - template - void emplace(const std::string &k, V &&v) { + template + void emplace(K &&k, V &&v) { auto it = find(k); if (it != end()) { return; } - Super::emplace(k, std::forward(v)); + Super::emplace(std::forward(k), std::forward(v)); } - template - void emplace_force(const std::string k, V &&v) { - Super::emplace(k, std::forward(v)); + template + void emplace_force(K &&k, V &&v) { + Super::emplace(std::forward(k), std::forward(v)); } }; @@ -61,23 +61,23 @@ public: Parser() = default; ~Parser() = default; - // 解析信令 - void Parse(const char *buf); + // 解析http/rtsp/sip请求,需要确保buf以\0结尾 + void parse(const char *buf, size_t size); - // 获取命令字 + // 获取命令字,如GET/POST const std::string &method() const; // 请求时,获取中间url,不包含?后面的参数 const std::string &url() const; - // 回复时,获取状态码 + // 回复时,获取状态码,如200/404 const std::string &status() const; // 获取中间url,包含?后面的参数 std::string fullUrl() const; - // 请求时,获取协议名 + // 请求时,获取协议名,如HTTP/1.1 const std::string &protocol() const; - // 回复时,获取状态字符串 + // 回复时,获取状态字符串,如 OK/Not Found const std::string &statusStr() const; // 根据header key名,获取请求header value值 @@ -110,14 +110,13 @@ public: static std::string mergeUrl(const std::string &base_url, const std::string &path); private: - std::string _strMethod; - std::string _strUrl; - std::string _strTail; - std::string _strContent; - std::string _strNull; + std::string _method; + std::string _url; + std::string _protocol; + std::string _content; std::string _params; - mutable StrCaseMap _mapHeaders; - mutable StrCaseMap _mapUrlArgs; + mutable StrCaseMap _headers; + mutable StrCaseMap _url_args; }; // 解析rtsp url的工具类 diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index daf757ad..99581ea9 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -181,7 +181,7 @@ void HttpClient::onError(const SockException &ex) { } ssize_t HttpClient::onRecvHeader(const char *data, size_t len) { - _parser.Parse(data); + _parser.parse(data, len); if (_parser.status() == "302" || _parser.status() == "301" || _parser.status() == "303") { auto new_url = Parser::mergeUrl(_url, _parser["Location"]); if (new_url.empty()) { diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 7170fd63..7d5689a2 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -63,14 +63,14 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { s_func_map.emplace("OPTIONS", &HttpSession::Handle_Req_OPTIONS); }); - _parser.Parse(header); + _parser.parse(header, len); CHECK(_parser.url()[0] == '/'); urlDecode(_parser); string cmd = _parser.method(); auto it = s_func_map.find(cmd); if (it == s_func_map.end()) { - WarnP(this) << "不支持该命令:" << cmd; + WarnP(this) << "Http method not supported: " << cmd; sendResponse(405, true); return 0; } diff --git a/src/Rtsp/RtspSplitter.cpp b/src/Rtsp/RtspSplitter.cpp index e88af355..f5d1e792 100644 --- a/src/Rtsp/RtspSplitter.cpp +++ b/src/Rtsp/RtspSplitter.cpp @@ -61,7 +61,7 @@ ssize_t RtspSplitter::onRecvHeader(const char *data, size_t len) { onRtpPacket(data,len); return 0; } - _parser.Parse(data); + _parser.parse(data, len); auto ret = getContentLength(_parser); if(ret == 0){ onWholeRtspPacket(_parser); From 64b8079ac10b17ca8012a2b3fecdfaba71d89269 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 12:28:49 +0800 Subject: [PATCH 43/66] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FindField改名为findSubString --- server/WebApi.cpp | 2 +- src/Common/Parser.cpp | 19 ++++++++++--------- src/Common/Parser.h | 2 +- src/Extension/Factory.cpp | 8 ++++---- src/Http/HttpClient.cpp | 12 ++++++------ src/Http/HttpCookieManager.cpp | 4 ++-- src/Http/HttpFileManager.cpp | 4 ++-- src/Player/PlayerBase.cpp | 2 +- src/Pusher/PusherBase.cpp | 2 +- src/Rtmp/RtmpPlayer.cpp | 4 ++-- src/Rtmp/RtmpPusher.cpp | 6 +++--- src/Rtsp/Rtsp.cpp | 6 +++--- src/Rtsp/RtspPlayer.cpp | 4 ++-- src/Rtsp/RtspPusher.cpp | 10 +++++----- src/Rtsp/RtspSession.cpp | 12 ++++++------ tests/test_bench_push.cpp | 2 +- tests/test_pusherMp4.cpp | 2 +- 17 files changed, 51 insertions(+), 50 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 64eec188..88e4d4ea 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1563,7 +1563,7 @@ void installWebApi() { } //找到截图 - auto tm = FindField(path.data() + scan_path.size(), nullptr, ".jpeg"); + auto tm = findSubString(path.data() + scan_path.size(), nullptr, ".jpeg"); if (atoll(tm.data()) + expire_sec < time(NULL)) { //截图已经过期,改名,以便再次请求时,可以返回老截图 rename(path.data(), new_snap.data()); diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index b47a0013..a7be567c 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -19,11 +19,12 @@ using namespace toolkit; namespace mediakit { -string FindField(const char *buf, const char *start, const char *end, size_t buf_size) { +string findSubString(const char *buf, const char *start, const char *end, size_t buf_size) { if (buf_size <= 0) { buf_size = strlen(buf); } - const char *msg_start = buf, *msg_end = buf + buf_size; + auto msg_start = buf; + auto msg_end = buf + buf_size; size_t len = 0; if (start != NULL) { len = strlen(start); @@ -253,12 +254,12 @@ std::string Parser::mergeUrl(const string &base_url, const string &path) { } void RtspUrl::parse(const string &strUrl) { - auto schema = FindField(strUrl.data(), nullptr, "://"); + auto schema = findSubString(strUrl.data(), nullptr, "://"); bool is_ssl = strcasecmp(schema.data(), "rtsps") == 0; // 查找"://"与"/"之间的字符串,用于提取用户名密码 - auto middle_url = FindField(strUrl.data(), "://", "/"); + auto middle_url = findSubString(strUrl.data(), "://", "/"); if (middle_url.empty()) { - middle_url = FindField(strUrl.data(), "://", nullptr); + middle_url = findSubString(strUrl.data(), "://", nullptr); } auto pos = middle_url.rfind('@'); if (pos == string::npos) { @@ -273,15 +274,15 @@ void RtspUrl::parse(const string &strUrl) { if (user_pwd.find(":") == string::npos) { return setup(is_ssl, url, user_pwd, ""); } - auto user = FindField(user_pwd.data(), nullptr, ":"); - auto pwd = FindField(user_pwd.data(), ":", nullptr); + auto user = findSubString(user_pwd.data(), nullptr, ":"); + auto pwd = findSubString(user_pwd.data(), ":", nullptr); return setup(is_ssl, url, user, pwd); } void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const string &passwd) { - auto ip = FindField(url.data(), "://", "/"); + auto ip = findSubString(url.data(), "://", "/"); if (ip.empty()) { - ip = split(FindField(url.data(), "://", NULL), "?")[0]; + ip = split(findSubString(url.data(), "://", NULL), "?")[0]; } uint16_t port = is_ssl ? 322 : 554; splitUrl(ip, ip, port); diff --git a/src/Common/Parser.h b/src/Common/Parser.h index 3d81cea3..3741f4bc 100644 --- a/src/Common/Parser.h +++ b/src/Common/Parser.h @@ -18,7 +18,7 @@ namespace mediakit { // 从字符串中提取子字符串 -std::string FindField(const char *buf, const char *start, const char *end, size_t buf_size = 0); +std::string findSubString(const char *buf, const char *start, const char *end, size_t buf_size = 0); // 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns void splitUrl(const std::string &url, std::string &host, uint16_t &port); diff --git a/src/Extension/Factory.cpp b/src/Extension/Factory.cpp index c85f920c..4e0b795c 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -45,9 +45,9 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { case CodecOpus : return std::make_shared(); case CodecAAC : { - string aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";"); + string aac_cfg_str = findSubString(track->_fmtp.data(), "config=", ";"); if (aac_cfg_str.empty()) { - aac_cfg_str = FindField(track->_fmtp.data(), "config=", nullptr); + aac_cfg_str = findSubString(track->_fmtp.data(), "config=", nullptr); } if (aac_cfg_str.empty()) { //如果sdp中获取不到aac config信息,那么在rtp也无法获取,那么忽略该Track @@ -67,8 +67,8 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { //a=fmtp:96 packetization-mode=1;profile-level-id=42C01F;sprop-parameter-sets=Z0LAH9oBQBboQAAAAwBAAAAPI8YMqA==,aM48gA== auto map = Parser::parseArgs(track->_fmtp, ";", "="); auto sps_pps = map["sprop-parameter-sets"]; - string base64_SPS = FindField(sps_pps.data(), NULL, ","); - string base64_PPS = FindField(sps_pps.data(), ",", NULL); + string base64_SPS = findSubString(sps_pps.data(), NULL, ","); + string base64_PPS = findSubString(sps_pps.data(), ",", NULL); auto sps = decodeBase64(base64_SPS); auto pps = decodeBase64(base64_PPS); if (sps.empty() || pps.empty()) { diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 99581ea9..328872e6 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -21,7 +21,7 @@ namespace mediakit { void HttpClient::sendRequest(const string &url) { clearResponse(); _url = url; - auto protocol = FindField(url.data(), NULL, "://"); + auto protocol = findSubString(url.data(), NULL, "://"); uint16_t port; bool is_https; if (strcasecmp(protocol.data(), "http") == 0) { @@ -35,11 +35,11 @@ void HttpClient::sendRequest(const string &url) { throw std::invalid_argument(strErr); } - auto host = FindField(url.data(), "://", "/"); + auto host = findSubString(url.data(), "://", "/"); if (host.empty()) { - host = FindField(url.data(), "://", NULL); + host = findSubString(url.data(), "://", NULL); } - _path = FindField(url.data(), host.data(), NULL); + _path = findSubString(url.data(), host.data(), NULL); if (_path.empty()) { _path = "/"; } @@ -361,8 +361,8 @@ void HttpClient::checkCookie(HttpClient::HttpHeader &headers) { int index = 0; auto arg_vec = split(it_set_cookie->second, ";"); for (string &key_val : arg_vec) { - auto key = FindField(key_val.data(), NULL, "="); - auto val = FindField(key_val.data(), "=", NULL); + auto key = findSubString(key_val.data(), NULL, "="); + auto val = findSubString(key_val.data(), "=", NULL); if (index++ == 0) { cookie->setKeyVal(key, val); diff --git a/src/Http/HttpCookieManager.cpp b/src/Http/HttpCookieManager.cpp index 2ce0f188..b846a3b9 100644 --- a/src/Http/HttpCookieManager.cpp +++ b/src/Http/HttpCookieManager.cpp @@ -158,9 +158,9 @@ HttpServerCookie::Ptr HttpCookieManager::getCookie(const string &cookie_name, co if (it == http_header.end()) { return nullptr; } - auto cookie = FindField(it->second.data(), (cookie_name + "=").data(), ";"); + auto cookie = findSubString(it->second.data(), (cookie_name + "=").data(), ";"); if (cookie.empty()) { - cookie = FindField(it->second.data(), (cookie_name + "=").data(), nullptr); + cookie = findSubString(it->second.data(), (cookie_name + "=").data(), nullptr); } if (cookie.empty()) { return nullptr; diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 9a22e64f..af032597 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -600,8 +600,8 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader, if (!strRange.empty()) { //分节下载 code = 206; - auto iRangeStart = atoll(FindField(strRange.data(), "bytes=", "-").data()); - auto iRangeEnd = atoll(FindField(strRange.data(), "-", nullptr).data()); + auto iRangeStart = atoll(findSubString(strRange.data(), "bytes=", "-").data()); + auto iRangeEnd = atoll(findSubString(strRange.data(), "-", nullptr).data()); auto fileSize = fileBody->remainSize(); if (iRangeEnd == 0) { iRangeEnd = fileSize - 1; diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index 31d8e051..cb00a0da 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -28,7 +28,7 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller, const s ptr->teardown(); }; string url = url_in; - string prefix = FindField(url.data(), NULL, "://"); + string prefix = findSubString(url.data(), NULL, "://"); auto pos = url.find('?'); if (pos != string::npos) { //去除?后面的字符串 diff --git a/src/Pusher/PusherBase.cpp b/src/Pusher/PusherBase.cpp index fa0d68e8..eddbf7b8 100644 --- a/src/Pusher/PusherBase.cpp +++ b/src/Pusher/PusherBase.cpp @@ -26,7 +26,7 @@ PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &poller, }); ptr->teardown(); }; - std::string prefix = FindField(url.data(), NULL, "://"); + std::string prefix = findSubString(url.data(), NULL, "://"); if (strcasecmp("rtsps",prefix.data()) == 0) { return PusherBase::Ptr(new TcpClientWithSSL(poller, std::dynamic_pointer_cast(src)), releasePusher); diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 71e3ecb9..1c2b335c 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -52,14 +52,14 @@ void RtmpPlayer::teardown() { void RtmpPlayer::play(const string &url) { teardown(); - string host_url = FindField(url.data(), "://", "/"); + string host_url = findSubString(url.data(), "://", "/"); { auto pos = url.find_last_of('/'); if (pos != string::npos) { _stream_id = url.substr(pos + 1); } } - _app = FindField(url.data(), (host_url + "/").data(), ("/" + _stream_id).data()); + _app = findSubString(url.data(), (host_url + "/").data(), ("/" + _stream_id).data()); _tc_url = string("rtmp://") + host_url + "/" + _app; if (!_app.size() || !_stream_id.size()) { diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index 9379d015..e8c55f70 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -65,9 +65,9 @@ void RtmpPusher::onPublishResult_l(const SockException &ex, bool handshake_done) void RtmpPusher::publish(const string &url) { teardown(); - string host_url = FindField(url.data(), "://", "/"); - _app = FindField(url.data(), (host_url + "/").data(), "/"); - _stream_id = FindField(url.data(), (host_url + "/" + _app + "/").data(), NULL); + string host_url = findSubString(url.data(), "://", "/"); + _app = findSubString(url.data(), (host_url + "/").data(), "/"); + _stream_id = findSubString(url.data(), (host_url + "/" + _app + "/").data(), NULL); _tc_url = string("rtmp://") + host_url + "/" + _app; if (!_app.size() || !_stream_id.size()) { diff --git a/src/Rtsp/Rtsp.cpp b/src/Rtsp/Rtsp.cpp index b80a88f7..2e3618da 100644 --- a/src/Rtsp/Rtsp.cpp +++ b/src/Rtsp/Rtsp.cpp @@ -180,11 +180,11 @@ void SdpParser::load(const string &sdp) { break; } case 'a': { - string attr = FindField(opt_val.data(), nullptr, ":"); + string attr = findSubString(opt_val.data(), nullptr, ":"); if (attr.empty()) { track->_attr.emplace(opt_val, ""); } else { - track->_attr.emplace(attr, FindField(opt_val.data(), ":", nullptr)); + track->_attr.emplace(attr, findSubString(opt_val.data(), ":", nullptr)); } break; } @@ -245,7 +245,7 @@ void SdpParser::load(const string &sdp) { it = track._attr.erase(it); continue; } - track._fmtp = FindField(fmtp.data(), " ", nullptr); + track._fmtp = findSubString(fmtp.data(), " ", nullptr); ++it; } diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index a58880d3..0dafa03a 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -273,7 +273,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { if (track_idx == 0) { _session_id = parser["Session"]; _session_id.append(";"); - _session_id = FindField(_session_id.data(), nullptr, ";"); + _session_id = findSubString(_session_id.data(), nullptr, ";"); } auto strTransport = parser["Transport"]; @@ -469,7 +469,7 @@ void RtspPlayer::handleResPAUSE(const Parser &parser, int type) { // 修正时间轴 auto strRange = parser["Range"]; if (strRange.size()) { - auto strStart = FindField(strRange.data(), "npt=", "-"); + auto strStart = findSubString(strRange.data(), "npt=", "-"); if (strStart == "now") { strStart = "0"; } diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index e0855b19..4748fb15 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -291,13 +291,13 @@ void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) { if (track_idx == 0) { _session_id = parser["Session"]; _session_id.append(";"); - _session_id = FindField(_session_id.data(), nullptr, ";"); + _session_id = findSubString(_session_id.data(), nullptr, ";"); } auto transport = parser["Transport"]; if (transport.find("TCP") != string::npos || transport.find("interleaved") != string::npos) { _rtp_type = Rtsp::RTP_TCP; - string interleaved = FindField(FindField((transport + ";").data(), "interleaved=", ";").data(), NULL, "-"); + string interleaved = findSubString(findSubString((transport + ";").data(), "interleaved=", ";").data(), NULL, "-"); _track_vec[track_idx]->_interleaved = atoi(interleaved.data()); } else if (transport.find("multicast") != string::npos) { throw std::runtime_error("SETUP rtsp pusher can not support multicast!"); @@ -305,9 +305,9 @@ void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) { _rtp_type = Rtsp::RTP_UDP; createUdpSockIfNecessary(track_idx); const char *strPos = "server_port="; - auto port_str = FindField((transport + ";").data(), strPos, ";"); - uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data()); - uint16_t rtcp_port = atoi(FindField(port_str.data(), "-", NULL).data()); + auto port_str = findSubString((transport + ";").data(), strPos, ";"); + uint16_t rtp_port = atoi(findSubString(port_str.data(), NULL, "-").data()); + uint16_t rtcp_port = atoi(findSubString(port_str.data(), "-", NULL).data()); auto &rtp_sock = _rtp_sock[track_idx]; auto &rtcp_sock = _rtcp_sock[track_idx]; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 33c59283..15db5aa8 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -595,8 +595,8 @@ void RtspSession::onAuthUser(const string &realm,const string &authorization){ return; } //请求中包含认证信息 - auto authType = FindField(authorization.data(),NULL," "); - auto authStr = FindField(authorization.data()," ",NULL); + auto authType = findSubString(authorization.data(), NULL, " "); + auto authStr = findSubString(authorization.data(), " ", NULL); if(authType.empty() || authStr.empty()){ //认证信息格式不合法,回复401 Unauthorized onAuthFailed(realm,"can not find auth type or auth string"); @@ -690,9 +690,9 @@ void RtspSession::handleReq_Setup(const Parser &parser) { _rtcp_socks[trackIdx] = pr.second; //设置客户端内网端口信息 - string strClientPort = FindField(parser["Transport"].data(), "client_port=", NULL); - uint16_t ui16RtpPort = atoi(FindField(strClientPort.data(), NULL, "-").data()); - uint16_t ui16RtcpPort = atoi(FindField(strClientPort.data(), "-", NULL).data()); + string strClientPort = findSubString(parser["Transport"].data(), "client_port=", NULL); + uint16_t ui16RtpPort = atoi(findSubString(strClientPort.data(), NULL, "-").data()); + uint16_t ui16RtcpPort = atoi(findSubString(strClientPort.data(), "-", NULL).data()); auto peerAddr = SockUtil::make_sockaddr(get_peer_ip().data(), ui16RtpPort); //设置rtp发送目标地址 @@ -785,7 +785,7 @@ void RtspSession::handleReq_Play(const Parser &parser) { if (!strRange.empty()) { //这是seek操作 res_header.emplace("Range", strRange); - auto strStart = FindField(strRange.data(), "npt=", "-"); + auto strStart = findSubString(strRange.data(), "npt=", "-"); if (strStart == "now") { strStart = "0"; } diff --git a/tests/test_bench_push.cpp b/tests/test_bench_push.cpp index d0929a8e..b34f8b99 100644 --- a/tests/test_bench_push.cpp +++ b/tests/test_bench_push.cpp @@ -124,7 +124,7 @@ int main(int argc, char *argv[]) { auto delay_ms = cmd_main["delay"].as(); auto pusher_count = cmd_main["count"].as(); auto merge_ms = cmd_main["merge"].as(); - auto schema = FindField(out_url.data(), nullptr, "://"); + auto schema = findSubString(out_url.data(), nullptr, "://"); if (schema != RTSP_SCHEMA && schema != RTMP_SCHEMA) { cout << "推流协议只支持rtsp或rtmp!" << endl; return -1; diff --git a/tests/test_pusherMp4.cpp b/tests/test_pusherMp4.cpp index ef338344..c28845d4 100644 --- a/tests/test_pusherMp4.cpp +++ b/tests/test_pusherMp4.cpp @@ -114,7 +114,7 @@ int domain(const string &filePath, const string &pushUrl) { auto poller = EventPollerPool::Instance().getPoller(); //vhost/app/stream可以随便自己填,现在不限制app应用名了 - createPusher(poller, FindField(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl); + createPusher(poller, findSubString(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl); //设置退出信号处理函数 static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 From d3f1c2fab1ba68d8a1a661058f2e8c18d9562e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sat, 10 Jun 2023 14:56:23 +0800 Subject: [PATCH 44/66] =?UTF-8?q?mp4=E5=BD=95=E5=88=B6=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E6=B7=BB=E5=8A=A0=E7=B4=A2=E5=BC=95=E5=8F=B7=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E4=B8=80=E7=A7=92=E5=86=85=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E5=90=8C=E5=90=8D=E6=96=87=E4=BB=B6=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E8=A6=86=E7=9B=96=20(#2542)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实测发现 如果在点播推流时 如果切片大小设置比较小 一秒钟可能产生多个mp4切片 以前切片名称粒度最小为一秒 所以会存在文件覆盖问题 --- src/Record/MP4Recorder.cpp | 10 +++++----- src/Record/MP4Recorder.h | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index fd7c8955..4784fcda 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -41,16 +41,16 @@ MP4Recorder::~MP4Recorder() { void MP4Recorder::createFile() { closeFile(); auto date = getTimeStr("%Y-%m-%d"); - auto time = getTimeStr("%H-%M-%S"); - auto full_path_tmp = _folder_path + date + "/." + time + ".mp4"; - auto full_path = _folder_path + date + "/" + time + ".mp4"; + auto file_name = getTimeStr("%H-%M-%S") + "-" + std::to_string(_file_index++) + ".mp4"; + auto full_path = _folder_path + date + "/" + file_name; + auto full_path_tmp = _folder_path + date + "/." + file_name; /////record 业务逻辑////// _info.start_time = ::time(NULL); - _info.file_name = time + ".mp4"; + _info.file_name = file_name; _info.file_path = full_path; GET_CONFIG(string, appName, Record::kAppName); - _info.url = appName + "/" + _info.app + "/" + _info.stream + "/" + date + "/" + time + ".mp4"; + _info.url = appName + "/" + _info.app + "/" + _info.stream + "/" + date + "/" + file_name; try { _muxer = std::make_shared(); diff --git a/src/Record/MP4Recorder.h b/src/Record/MP4Recorder.h index 69175238..3e0c8013 100644 --- a/src/Record/MP4Recorder.h +++ b/src/Record/MP4Recorder.h @@ -57,13 +57,14 @@ private: private: bool _have_video = false; size_t _max_second; + uint64_t _last_dts = 0; + uint64_t _file_index = 0; std::string _folder_path; std::string _full_path; std::string _full_path_tmp; RecordInfo _info; MP4Muxer::Ptr _muxer; std::list _tracks; - uint64_t _last_dts = 0; }; #endif ///ENABLE_MP4 From 24fb666fb39ff0a286d34829269fcd7b03f0f8a7 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 09:34:00 +0800 Subject: [PATCH 45/66] =?UTF-8?q?=E6=96=B0=E5=A2=9Emk=5Fmedia=5Fsource=5Fg?= =?UTF-8?q?et=5Ftrack=E6=96=B9=E6=B3=95:=20#2530?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_events_objects.h | 5 +++++ api/source/mk_events_objects.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index c114c783..425243a6 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -12,6 +12,7 @@ #define MK_EVENT_OBJECTS_H #include "mk_common.h" #include "mk_tcp.h" +#include "mk_track.h" #ifdef __cplusplus extern "C" { #endif @@ -95,6 +96,10 @@ API_EXPORT const char* API_CALL mk_media_source_get_stream(const mk_media_source API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source ctx); //MediaSource::totalReaderCount() API_EXPORT int API_CALL mk_media_source_get_total_reader_count(const mk_media_source ctx); +// get track count from MediaSource +API_EXPORT int API_CALL mk_media_source_get_track_count(const mk_media_source ctx); +// get track by index from MediaSource +API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx, int index); /** * 直播源在ZLMediaKit中被称作为MediaSource, * 目前支持3种,分别是RtmpMediaSource、RtspMediaSource、HlsMediaSource diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index 617151e5..b92aa7e8 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -198,6 +198,22 @@ API_EXPORT int API_CALL mk_media_source_get_total_reader_count(const mk_media_so return src->totalReaderCount(); } +API_EXPORT int API_CALL mk_media_source_get_track_count(const mk_media_source ctx) { + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + return src->getTracks(false).size(); +} + +API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx, int index) { + assert(ctx); + MediaSource *src = (MediaSource *)ctx; + auto tracks = src->getTracks(false); + if (index < 0 && index >= tracks.size()) { + return nullptr; + } + return reinterpret_cast(& (tracks[index])); +} + API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int force){ assert(ctx); MediaSource *src = (MediaSource *)ctx; From 67d5c42007febb69ecad66c65c6ab292108a9119 Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Sat, 10 Jun 2023 18:46:30 +0800 Subject: [PATCH 46/66] Fix addStreamProxy rtsp url user or pass contain special char result in auth failed (#2545) --- src/Common/Parser.cpp | 6 ++++-- src/{Http => Common}/strCoding.cpp | 0 src/{Http => Common}/strCoding.h | 0 src/Http/HttpClient.h | 2 +- src/Http/HttpFileManager.cpp | 2 +- src/Http/HttpSession.cpp | 2 +- 6 files changed, 7 insertions(+), 5 deletions(-) rename src/{Http => Common}/strCoding.cpp (100%) rename src/{Http => Common}/strCoding.h (100%) diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index a7be567c..3e793039 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -10,6 +10,7 @@ #include #include "Parser.h" +#include "strCoding.h" #include "macros.h" #include "Network/sockutil.h" #include "Common/macros.h" @@ -287,9 +288,10 @@ void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const st uint16_t port = is_ssl ? 322 : 554; splitUrl(ip, ip, port); + _url = std::move(url); - _user = std::move(user); - _passwd = std::move(passwd); + _user = strCoding::UrlDecode(std::move(user)); + _passwd = strCoding::UrlDecode(std::move(passwd)); _host = std::move(ip); _port = port; _is_ssl = is_ssl; diff --git a/src/Http/strCoding.cpp b/src/Common/strCoding.cpp similarity index 100% rename from src/Http/strCoding.cpp rename to src/Common/strCoding.cpp diff --git a/src/Http/strCoding.h b/src/Common/strCoding.h similarity index 100% rename from src/Http/strCoding.h rename to src/Common/strCoding.h diff --git a/src/Http/HttpClient.h b/src/Http/HttpClient.h index c82804d4..497f4a1d 100644 --- a/src/Http/HttpClient.h +++ b/src/Http/HttpClient.h @@ -22,7 +22,7 @@ #include "HttpRequestSplitter.h" #include "HttpCookie.h" #include "HttpChunkedSplitter.h" -#include "strCoding.h" +#include "Common/strCoding.h" #include "HttpBody.h" namespace mediakit { diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index af032597..7f69297b 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -20,7 +20,7 @@ #include "Record/HlsMediaSource.h" #include "Common/Parser.h" #include "Common/config.h" -#include "strCoding.h" +#include "Common/strCoding.h" using namespace std; using namespace toolkit; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 7d5689a2..fb7aed7f 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -12,7 +12,7 @@ #include #include #include "Common/config.h" -#include "strCoding.h" +#include "Common/strCoding.h" #include "HttpSession.h" #include "HttpConst.h" #include "Util/base64.h" From 5f936663b4f687597e63c7e4df9a89d9f180e1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sat, 10 Jun 2023 20:49:21 +0800 Subject: [PATCH 47/66] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86conte?= =?UTF-8?q?nt-length,=20=E4=BF=AE=E5=A4=8Dhttp=20get=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=B8=A6body=E4=B8=8D=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98(#252?= =?UTF-8?q?8=20#2544)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- src/Http/HttpSession.cpp | 184 +++++++++++++++++++-------------------- src/Http/HttpSession.h | 11 ++- 3 files changed, 96 insertions(+), 101 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index b6e5af3e..e4744a0a 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit b6e5af3e7ad95d87f4e45a89e36d9a8ab45209a9 +Subproject commit e4744a0a523817356f2ec995ee5a732264c31629 diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index fb7aed7f..f1cac788 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -30,14 +30,14 @@ HttpSession::HttpSession(const Socket::Ptr &pSock) : Session(pSock) { HttpSession::~HttpSession() = default; -void HttpSession::Handle_Req_HEAD(ssize_t &content_len) { +void HttpSession::onHttpRequest_HEAD() { // 暂时全部返回200 OK,因为HTTP GET存在按需生成流的操作,所以不能按照HTTP GET的流程返回 // 如果直接返回404,那么又会导致按需生成流的逻辑失效,所以HTTP HEAD在静态文件或者已存在资源时才有效 // 对于按需生成流的直播场景并不适用 sendResponse(200, false); } -void HttpSession::Handle_Req_OPTIONS(ssize_t &content_len) { +void HttpSession::onHttpRequest_OPTIONS() { KeyValue header; header.emplace("Allow", "GET, POST, HEAD, OPTIONS"); GET_CONFIG(bool, allow_cross_domains, Http::kAllowCrossDomains); @@ -53,21 +53,22 @@ void HttpSession::Handle_Req_OPTIONS(ssize_t &content_len) { } ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { - using func_type = void (HttpSession::*)(ssize_t &); + using func_type = void (HttpSession::*)(); static unordered_map s_func_map; static onceToken token([]() { - s_func_map.emplace("GET", &HttpSession::Handle_Req_GET); - s_func_map.emplace("DELETE", &HttpSession::Handle_Req_GET); - s_func_map.emplace("POST", &HttpSession::Handle_Req_POST); - s_func_map.emplace("HEAD", &HttpSession::Handle_Req_HEAD); - s_func_map.emplace("OPTIONS", &HttpSession::Handle_Req_OPTIONS); + s_func_map.emplace("GET", &HttpSession::onHttpRequest_GET); + s_func_map.emplace("POST", &HttpSession::onHttpRequest_POST); + // DELETE命令用于whip/whep用,只用于触发http api + s_func_map.emplace("DELETE", &HttpSession::onHttpRequest_POST); + s_func_map.emplace("HEAD", &HttpSession::onHttpRequest_HEAD); + s_func_map.emplace("OPTIONS", &HttpSession::onHttpRequest_OPTIONS); }); _parser.parse(header, len); CHECK(_parser.url()[0] == '/'); urlDecode(_parser); - string cmd = _parser.method(); + auto &cmd = _parser.method(); auto it = s_func_map.find(cmd); if (it == s_func_map.end()) { WarnP(this) << "Http method not supported: " << cmd; @@ -75,19 +76,84 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { return 0; } - //默认后面数据不是content而是header - ssize_t content_len = 0; - (this->*(it->second))(content_len); + size_t content_len; + auto &content_len_str = _parser["Content-Length"]; + if (content_len_str.empty()) { + if (it->first == "POST") { + // Http post未指定长度,我们认为是不定长的body + WarnL << "Received http post request without content-length, consider it to be unlimited length"; + content_len = SIZE_MAX; + } else { + content_len = 0; + } + } else { + // 已经指定长度 + content_len = atoll(content_len_str.data()); + } - // 清空解析器节省内存 - _parser.clear(); - // 返回content长度 - return content_len; + if (content_len == 0) { + //// 没有body的情况,直接触发回调 //// + (this->*(it->second))(); + _parser.clear(); + // 如果设置了_on_recv_body, 那么说明后续要处理body + return _on_recv_body ? -1 : 0; + } + + GET_CONFIG(size_t, maxReqSize, Http::kMaxReqSize); + if (content_len > maxReqSize) { + //// 不定长body或超大body //// + if (content_len != SIZE_MAX) { + WarnL << "Http body size is too huge: " << content_len << " > " << maxReqSize + << ", please set " << Http::kMaxReqSize << " in config.ini file."; + } + + size_t received = 0; + auto parser = std::move(_parser); + _on_recv_body = [this, parser, received, content_len](const char *data, size_t len) mutable { + received += len; + onRecvUnlimitedContent(parser, data, len, content_len, received); + if (received != content_len) { + // 还没收满 + return true; + } + + // 收满了 + setContentLen(0); + return false; + }; + // 声明后续都是body;Http body在本对象缓冲,不通过HttpRequestSplitter保存 + return -1; + } + + //// body size明确指定且小于最大值的情况 //// + auto body = std::make_shared(); + // 预留一定的内存buffer,防止频繁的内存拷贝 + body->reserve(content_len); + + _on_recv_body = [this, body, content_len, it](const char *data, size_t len) mutable { + body->append(data, len); + if (body->size() < content_len) { + // 未收满数据 + return true; + } + + // 收集body完毕 + _parser.setContent(std::move(*body)); + (this->*(it->second))(); + _parser.clear(); + + // 后续是header + setContentLen(0); + return false; + }; + + // 声明后续都是body;Http body在本对象缓冲,不通过HttpRequestSplitter保存 + return -1; } void HttpSession::onRecvContent(const char *data, size_t len) { - if (_contentCallBack && !_contentCallBack(data, len)) { - _contentCallBack = nullptr; + if (_on_recv_body && !_on_recv_body(data, len)) { + _on_recv_body = nullptr; } } @@ -369,17 +435,13 @@ bool HttpSession::checkLiveStreamFlv(const function &cb) { }); } -void HttpSession::Handle_Req_GET(ssize_t &content_len) { - Handle_Req_GET_l(content_len, true); -} - -void HttpSession::Handle_Req_GET_l(ssize_t &content_len, bool sendBody) { +void HttpSession::onHttpRequest_GET() { // 先看看是否为WebSocket请求 if (checkWebSocket()) { - content_len = -1; - _contentCallBack = [this](const char *data, size_t len) { + // 后续都是websocket body数据 + _on_recv_body = [this](const char *data, size_t len) { WebSocketSplitter::decode((uint8_t *)data, len); - //_contentCallBack是可持续的,后面还要处理后续数据 + // _contentCallBack是可持续的,后面还要处理后续数据 return true; }; return; @@ -658,74 +720,8 @@ std::string HttpSession::get_peer_ip() { return Session::get_peer_ip(); } -void HttpSession::Handle_Req_POST(ssize_t &content_len) { - GET_CONFIG(size_t, maxReqSize, Http::kMaxReqSize); - - ssize_t totalContentLen = _parser["Content-Length"].empty() ? -1 : atoll(_parser["Content-Length"].data()); - - if (totalContentLen == 0) { - // content为空 - // emitHttpEvent内部会选择是否关闭连接 - emitHttpEvent(true); - return; - } - - if (totalContentLen > 0 && (size_t)totalContentLen < maxReqSize) { - // 返回固定长度的content - content_len = totalContentLen; - auto parserCopy = _parser; - _contentCallBack = [this, parserCopy](const char *data, size_t len) { - // 恢复http头 - _parser = parserCopy; - // 设置content - _parser.setContent(string(data, len)); - // 触发http事件,emitHttpEvent内部会选择是否关闭连接 - emitHttpEvent(true); - // 清空数据,节省内存 - _parser.clear(); - // content已经接收完毕 - return false; - }; - } else { - // 返回不固定长度的content或者超过长度限制的content - content_len = -1; - auto parserCopy = _parser; - std::shared_ptr recvedContentLen = std::make_shared(0); - bool bClose = !strcasecmp(_parser["Connection"].data(), "close"); - - _contentCallBack = [this, parserCopy, totalContentLen, recvedContentLen, bClose](const char *data, size_t len) { - *(recvedContentLen) += len; - if (totalContentLen < 0) { - // 不固定长度的content,源源不断接收数据 - onRecvUnlimitedContent(parserCopy, data, len, SIZE_MAX, *(recvedContentLen)); - return true; - } - - // 长度超过限制的content - onRecvUnlimitedContent(parserCopy, data, len, totalContentLen, *(recvedContentLen)); - - if (*(recvedContentLen) < (size_t)totalContentLen) { - // 数据还没接收完毕 - //_contentCallBack是可持续的,后面还要处理后续content数据 - return true; - } - - // 数据接收完毕 - if (!bClose) { - // keep-alive类型连接 - // content接收完毕,后续都是http header - setContentLen(0); - // content已经接收完毕 - return false; - } - - // 连接类型是close类型,收完content就关闭连接 - shutdown(SockException(Err_shutdown, "recv http content completed")); - // content已经接收完毕 - return false; - }; - } - // 有后续content数据要处理,暂时不关闭连接 +void HttpSession::onHttpRequest_POST() { + emitHttpEvent(true); } void HttpSession::sendNotFound(bool bClose) { diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 59255fb8..513f8b63 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -101,11 +101,10 @@ protected: std::string get_peer_ip() override; private: - void Handle_Req_GET(ssize_t &content_len); - void Handle_Req_GET_l(ssize_t &content_len, bool sendBody); - void Handle_Req_POST(ssize_t &content_len); - void Handle_Req_HEAD(ssize_t &content_len); - void Handle_Req_OPTIONS(ssize_t &content_len); + void onHttpRequest_GET(); + void onHttpRequest_POST(); + void onHttpRequest_HEAD(); + void onHttpRequest_OPTIONS(); bool checkLiveStream(const std::string &schema, const std::string &url_suffix, const std::function &cb); @@ -137,7 +136,7 @@ private: TSMediaSource::RingType::RingReader::Ptr _ts_reader; FMP4MediaSource::RingType::RingReader::Ptr _fmp4_reader; //处理content数据的callback - std::function _contentCallBack; + std::function _on_recv_body; }; using HttpsSession = toolkit::SessionWithSSL; From fe37005526d451711d05dd0a09b6c38a4757e9ef Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 11 Jun 2023 09:21:15 +0800 Subject: [PATCH 48/66] =?UTF-8?q?=E5=85=BC=E5=AE=B9http=20body=E8=B6=85?= =?UTF-8?q?=E8=BF=87=E5=A3=B0=E6=98=8E=E5=A4=A7=E5=B0=8F=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index f1cac788..7896d281 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -112,7 +112,7 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { _on_recv_body = [this, parser, received, content_len](const char *data, size_t len) mutable { received += len; onRecvUnlimitedContent(parser, data, len, content_len, received); - if (received != content_len) { + if (received < content_len) { // 还没收满 return true; } From 5ca4ed53b2afc5902ca48377cf49a756181911c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sun, 11 Jun 2023 22:07:15 +0800 Subject: [PATCH 49/66] =?UTF-8?q?=E9=98=B2=E6=AD=A2=E6=9E=90=E6=9E=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=8A=9B=E5=BC=82=E5=B8=B8=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98=20(#2546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 6 +++++- src/Player/PlayerProxy.cpp | 6 +++++- src/Pusher/MediaPusher.cpp | 3 +-- src/Record/HlsMaker.cpp | 4 +--- src/Record/HlsMediaSource.cpp | 6 +++++- src/Record/MP4Demuxer.cpp | 2 +- src/Rtp/RtpProcess.cpp | 6 +++++- src/Rtp/RtpSender.cpp | 6 +++++- srt/SrtTransportImp.cpp | 8 +++++--- 9 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index f2861153..6f3a62e4 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -124,7 +124,11 @@ MediaSource::MediaSource(const string &schema, const MediaTuple& tuple): _tuple( } MediaSource::~MediaSource() { - unregist(); + try { + unregist(); + } catch (std::exception &ex) { + WarnL << "Exception occurred: " << ex.what(); + } } const string& MediaSource::getSchema() const { diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 36b4d850..2a319ff8 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -202,7 +202,11 @@ PlayerProxy::~PlayerProxy() { _timer.reset(); // 避免析构时, 忘记回调api请求 if (_on_play) { - _on_play(SockException(Err_shutdown, "player proxy close")); + try { + _on_play(SockException(Err_shutdown, "player proxy close")); + } catch (std::exception &ex) { + WarnL << "Exception occurred: " << ex.what(); + } _on_play = nullptr; } } diff --git a/src/Pusher/MediaPusher.cpp b/src/Pusher/MediaPusher.cpp index 865b4d2a..f0ea401b 100644 --- a/src/Pusher/MediaPusher.cpp +++ b/src/Pusher/MediaPusher.cpp @@ -31,8 +31,7 @@ MediaPusher::MediaPusher(const string &schema, MediaPusher(MediaSource::find(schema, vhost, app, stream), poller){ } -MediaPusher::~MediaPusher() { -} +MediaPusher::~MediaPusher() = default; static void setOnCreateSocket_l(const std::shared_ptr &delegate, const Socket::onCreateSocket &cb){ auto helper = dynamic_pointer_cast(delegate); diff --git a/src/Record/HlsMaker.cpp b/src/Record/HlsMaker.cpp index df4cb8de..bc6cf53d 100644 --- a/src/Record/HlsMaker.cpp +++ b/src/Record/HlsMaker.cpp @@ -22,9 +22,7 @@ HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number, bool seg_keep) { _seg_keep = seg_keep; } -HlsMaker::~HlsMaker() { -} - +HlsMaker::~HlsMaker() = default; void HlsMaker::makeIndexFile(bool eof) { char file_content[1024]; diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp index d6d4f4b4..f3fe516b 100644 --- a/src/Record/HlsMediaSource.cpp +++ b/src/Record/HlsMediaSource.cpp @@ -46,7 +46,11 @@ HlsCookieData::~HlsCookieData() { GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold); uint64_t bytes = _bytes.load(); if (bytes >= iFlowThreshold * 1024) { - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _info, bytes, duration, true, static_cast(*_sock_info)); + try { + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _info, bytes, duration, true, static_cast(*_sock_info)); + } catch (std::exception &ex) { + WarnL << "Exception occurred: " << ex.what(); + } } } } diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp index a1c657fd..a1bbd34c 100644 --- a/src/Record/MP4Demuxer.cpp +++ b/src/Record/MP4Demuxer.cpp @@ -21,7 +21,7 @@ using namespace std; namespace mediakit { -MP4Demuxer::MP4Demuxer() {} +MP4Demuxer::MP4Demuxer() = default; MP4Demuxer::~MP4Demuxer() { closeMP4(); diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 9700e09c..741cc4b1 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -66,7 +66,11 @@ RtpProcess::~RtpProcess() { //流量统计事件广播 GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold); if (_total_bytes >= iFlowThreshold * 1024) { - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _media_info, _total_bytes, duration, false, static_cast(*this)); + try { + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _media_info, _total_bytes, duration, false, static_cast(*this)); + } catch (std::exception &ex) { + WarnL << "Exception occurred: " << ex.what(); + } } } diff --git a/src/Rtp/RtpSender.cpp b/src/Rtp/RtpSender.cpp index a15884f0..a102d5e2 100644 --- a/src/Rtp/RtpSender.cpp +++ b/src/Rtp/RtpSender.cpp @@ -28,7 +28,11 @@ RtpSender::RtpSender(EventPoller::Ptr poller) { } RtpSender::~RtpSender() { - flush(); + try { + flush(); + } catch (std::exception &ex) { + WarnL << "Exception occurred: " << ex.what(); + } } void RtpSender::startSend(const MediaSourceEvent::SendRtpArgs &args, const function &cb){ diff --git a/srt/SrtTransportImp.cpp b/srt/SrtTransportImp.cpp index 91ae7488..c3c18c5d 100644 --- a/srt/SrtTransportImp.cpp +++ b/srt/SrtTransportImp.cpp @@ -16,9 +16,11 @@ SrtTransportImp::~SrtTransportImp() { // 流量统计事件广播 GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold); if (_total_bytes >= iFlowThreshold * 1024) { - NoticeCenter::Instance().emitEvent( - Broadcast::kBroadcastFlowReport, _media_info, _total_bytes, duration, !_is_pusher, - static_cast(*this)); + try { + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _media_info, _total_bytes, duration, !_is_pusher, static_cast(*this)); + } catch (std::exception &ex) { + WarnL << "Exception occurred: " << ex.what(); + } } } From 23e24d07e6462c54e50df9ba92a933ea83c70ba3 Mon Sep 17 00:00:00 2001 From: Luosh Date: Tue, 13 Jun 2023 17:39:43 +0800 Subject: [PATCH 50/66] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=AD=98=E5=9C=A8b?= =?UTF-8?q?=E5=B8=A7=E6=83=85=E5=86=B5=E4=B8=8B,rtsp=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E6=AD=A5=E9=97=AE=E9=A2=98=20(#2550=20#2549)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在根据rtsp pts生成ntp时,由于pts可能回退,旧有逻辑不允许回退将导致ntp异常 --- src/Common/Stamp.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index c7b1ab3c..3cb5ee32 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -48,8 +48,9 @@ int64_t DeltaStamp::deltaStamp(int64_t stamp) { // 时间戳增量为负,说明时间戳回环了或回退了 _last_stamp = stamp; - // 时间戳回退了,强制相对时间戳加1 - return 1; + + // 如果时间戳回退不多,那么返回负值,否则返回加1 + return -ret < MAX_CTS ? ret : 1; } void Stamp::setPlayBack(bool playback) { From 72ba87c80315eafea80386f43b0eaaeb82170bb4 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 17 Jun 2023 09:14:51 +0800 Subject: [PATCH 51/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmk=5Fmedia=5Fsource=5Fg?= =?UTF-8?q?et=5Ftrack=E6=8E=A5=E5=8F=A3=E5=B4=A9=E6=BA=83=E9=97=AE?= =?UTF-8?q?=E9=A2=98:#2552?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_events_objects.h | 2 +- api/source/mk_events_objects.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index 425243a6..018988f4 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -98,7 +98,7 @@ API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source c API_EXPORT int API_CALL mk_media_source_get_total_reader_count(const mk_media_source ctx); // get track count from MediaSource API_EXPORT int API_CALL mk_media_source_get_track_count(const mk_media_source ctx); -// get track by index from MediaSource +// copy track reference by index from MediaSource, please use mk_track_unref to release it API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx, int index); /** * 直播源在ZLMediaKit中被称作为MediaSource, diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index b92aa7e8..c8656cec 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -211,7 +211,7 @@ API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx if (index < 0 && index >= tracks.size()) { return nullptr; } - return reinterpret_cast(& (tracks[index])); + return (mk_track) new Track::Ptr(std::move(tracks[index])); } API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int force){ From ccddb84a1956188366dde5cec2855851930e336e Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 17 Jun 2023 09:15:50 +0800 Subject: [PATCH 52/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=A8=E5=9F=9F?= =?UTF-8?q?=E9=97=AE=E9=A2=98:=20#2548?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 7896d281..145a08a2 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -594,6 +594,13 @@ void HttpSession::sendResponse(int code, headerOut.emplace("Date", dateStr()); headerOut.emplace("Server", kServerName); headerOut.emplace("Connection", bClose ? "close" : "keep-alive"); + + GET_CONFIG(bool, allow_cross_domains, Http::kAllowCrossDomains); + if (allow_cross_domains) { + headerOut.emplace("Access-Control-Allow-Origin", "*"); + headerOut.emplace("Access-Control-Allow-Credentials", "true"); + } + if (!bClose) { string keepAliveString = "timeout="; keepAliveString += to_string(keepAliveSec); From 2410419911d0fcdaa406b5f1021c4a31b13409f3 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 17 Jun 2023 10:13:29 +0800 Subject: [PATCH 53/66] =?UTF-8?q?=E5=BF=BD=E7=95=A5=E9=9D=9E=E6=B3=95?= =?UTF-8?q?=E7=9A=84rtcp=20ntp=E6=97=B6=E9=97=B4=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 有些设备在发送sender report rtcp时,其rtp和ntp时间戳一直为0,将导致zlmediakit计算出的ntp时间戳异常 --- src/Common/Stamp.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index 3cb5ee32..a19b96d6 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -231,14 +231,15 @@ bool DtsGenerator::getDts_l(uint64_t pts, uint64_t &dts) { } void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { + if (!ntp_stamp_ms || !rtp_stamp) { + // 实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0 + WarnL << "Invalid sender report rtcp, ntp_stamp_ms = " << ntp_stamp_ms << ", rtp_stamp = " << rtp_stamp; + return; + } update(rtp_stamp, ntp_stamp_ms); } void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { - if (ntp_stamp_ms == 0) { - // 实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0 - return; - } _last_rtp_stamp = rtp_stamp; _last_ntp_stamp_ms = ntp_stamp_ms; } From c0a7a4ab7031902b6cf02be837141b589d198732 Mon Sep 17 00:00:00 2001 From: tbago Date: Sat, 17 Jun 2023 10:24:17 +0800 Subject: [PATCH 54/66] =?UTF-8?q?=E4=BC=98=E5=8C=96cmake=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6,=20=E6=B7=BB=E5=8A=A0=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E5=A4=B4=E6=96=87=E4=BB=B6=20(#2558)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - enable_server cmake选项生效; - enable_tests 选项在api目录下的test同样生效; - 增加stdio.h 防止编译失败; --- CMakeLists.txt | 4 +++- api/CMakeLists.txt | 6 ++++-- api/tests/CMakeLists.txt | 12 ------------ api/tests/pusher.c | 1 + api/tests/server.c | 1 + 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b110377..b1fb9bb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -461,7 +461,9 @@ if(ENABLE_PLAYER AND ENABLE_FFMPEG) endif() #MediaServer主程序 -add_subdirectory(server) +if(ENABLE_SERVER) + add_subdirectory(server) +endif() # Android 会 add_subdirectory 并依赖该变量 if(ENABLE_SERVER_LIB) diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index c7962a0f..851bb3c5 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -74,8 +74,6 @@ generate_export_header(mk_api STATIC_DEFINE MediaKitApi_STATIC EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/mk_export.h") -add_subdirectory(tests) - file(GLOB API_HEADER_LIST include/*.h ${CMAKE_CURRENT_BINARY_DIR}/*.h) install(FILES ${API_HEADER_LIST} DESTINATION ${INSTALL_PATH_INCLUDE}) @@ -83,3 +81,7 @@ install(TARGETS mk_api ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB} RUNTIME DESTINATION ${INSTALL_PATH_RUNTIME}) + +if (ENABLE_TESTS) + add_subdirectory(tests) +endif() diff --git a/api/tests/CMakeLists.txt b/api/tests/CMakeLists.txt index f2875ff6..8108b71b 100644 --- a/api/tests/CMakeLists.txt +++ b/api/tests/CMakeLists.txt @@ -43,15 +43,3 @@ foreach(TEST_SRC ${TEST_SRC_LIST}) target_link_libraries(${exe_name} mk_api) target_compile_options(${exe_name} PRIVATE ${COMPILE_OPTIONS_DEFAULT}) endforeach() - - - - - - - - - - - - diff --git a/api/tests/pusher.c b/api/tests/pusher.c index a6f3cd2d..2014fd09 100644 --- a/api/tests/pusher.c +++ b/api/tests/pusher.c @@ -9,6 +9,7 @@ */ #include +#include #include "mk_mediakit.h" typedef struct { diff --git a/api/tests/server.c b/api/tests/server.c index 8b283b6b..d331519c 100644 --- a/api/tests/server.c +++ b/api/tests/server.c @@ -9,6 +9,7 @@ */ #include +#include #include "mk_mediakit.h" #define LOG_LEV 4 From 03770ff4094005e601c83b59b8e5741423b002d6 Mon Sep 17 00:00:00 2001 From: johzzy Date: Sat, 17 Jun 2023 10:28:01 +0800 Subject: [PATCH 55/66] Replace the tuple (vhost/app/stream) with MediaTuple. (#2560) --- api/source/mk_events_objects.cpp | 6 +++--- server/FFmpegSource.cpp | 4 +--- server/WebApi.cpp | 10 +++++++--- server/WebHook.cpp | 22 +++++++--------------- src/Common/MediaSource.cpp | 26 +++++--------------------- src/Common/MediaSource.h | 16 ++++++---------- src/Common/MultiMediaSourceMuxer.cpp | 12 ------------ src/Common/MultiMediaSourceMuxer.h | 3 --- tests/test_pusher.cpp | 5 +++-- tests/test_server.cpp | 5 +++-- webrtc/WebRtcPusher.cpp | 4 ++-- 11 files changed, 37 insertions(+), 76 deletions(-) diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index c8656cec..fd7f696c 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -174,17 +174,17 @@ API_EXPORT const char* API_CALL mk_media_source_get_schema(const mk_media_source API_EXPORT const char* API_CALL mk_media_source_get_vhost(const mk_media_source ctx){ assert(ctx); MediaSource *src = (MediaSource *)ctx; - return src->getVhost().c_str(); + return src->getMediaTuple().vhost.c_str(); } API_EXPORT const char* API_CALL mk_media_source_get_app(const mk_media_source ctx){ assert(ctx); MediaSource *src = (MediaSource *)ctx; - return src->getApp().c_str(); + return src->getMediaTuple().app.c_str(); } API_EXPORT const char* API_CALL mk_media_source_get_stream(const mk_media_source ctx){ assert(ctx); MediaSource *src = (MediaSource *)ctx; - return src->getId().c_str(); + return src->getMediaTuple().stream.c_str(); } API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source ctx){ assert(ctx); diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index 5e7fc246..564dca46 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -184,9 +184,7 @@ void FFmpegSource::findAsync(int maxWaitMS, const function_media_info.schema || - sender.getVhost() != strongSelf->_media_info.vhost || - sender.getApp() != strongSelf->_media_info.app || - sender.getId() != strongSelf->_media_info.stream) { + !equalMediaTuple(sender.getMediaTuple(), strongSelf->_media_info)) { //不是自己感兴趣的事件,忽略之 return; } diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 88e4d4ea..2fe75f73 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -321,12 +321,16 @@ static void fillSockInfo(Value& val, SockInfo* info) { val["identifier"] = info->getIdentifier(); } +void dumpMediaTuple(const MediaTuple &tuple, Json::Value& item) { + item[VHOST_KEY] = tuple.vhost; + item["app"] = tuple.app; + item["stream"] = tuple.stream; +} + Value makeMediaSourceJson(MediaSource &media){ Value item; item["schema"] = media.getSchema(); - item[VHOST_KEY] = media.getVhost(); - item["app"] = media.getApp(); - item["stream"] = media.getId(); + dumpMediaTuple(media.getMediaTuple(), item); item["createStamp"] = (Json::UInt64) media.getCreateStamp(); item["aliveSecond"] = (Json::UInt64) media.getAliveSecond(); item["bytesSpeed"] = media.getBytesSpeed(); diff --git a/server/WebHook.cpp b/server/WebHook.cpp index d50b1db9..551d9c1d 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -213,12 +213,12 @@ void do_http_hook(const string &url, const ArgsType &body, const function weakSrc = sender.shared_from_this(); // 执行hook do_http_hook(hook_stream_none_reader, body, [weakSrc](const Value &obj, const string &err) { @@ -584,9 +578,7 @@ void installWebHook() { } ArgsType body; - body[VHOST_KEY] = sender.getVhost(); - body["app"] = sender.getApp(); - body["stream"] = sender.getStreamId(); + dumpMediaTuple(sender.getMediaTuple(), body); body["ssrc"] = ssrc; body["originType"] = (int)sender.getOriginType(MediaSource::NullMediaSource()); body["originTypeStr"] = getOriginTypeString(sender.getOriginType(MediaSource::NullMediaSource())); diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 6f3a62e4..7c48c3e1 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -131,23 +131,6 @@ MediaSource::~MediaSource() { } } -const string& MediaSource::getSchema() const { - return _schema; -} - -const string& MediaSource::getVhost() const { - return _tuple.vhost; -} - -const string& MediaSource::getApp() const { - //获取该源的id - return _tuple.app; -} - -const string& MediaSource::getId() const { - return _tuple.stream; -} - std::shared_ptr MediaSource::getOwnership() { if (_owned.test_and_set()) { //已经被所有 @@ -460,9 +443,7 @@ static void findAsync_l(const MediaInfo &info, const std::shared_ptr &s auto on_register = [weak_session, info, cb_once, cancel_all, poller](BroadcastMediaChangedArgs) { if (!bRegist || sender.getSchema() != info.schema || - sender.getVhost() != info.vhost || - sender.getApp() != info.app || - sender.getId() != info.stream) { + !equalMediaTuple(sender.getMediaTuple(), info)) { //不是自己感兴趣的事件,忽略之 return; } @@ -579,6 +560,9 @@ bool MediaSource::unregist() { return ret; } +bool equalMediaTuple(const MediaTuple& a, const MediaTuple& b) { + return a.vhost == b.vhost && a.app == b.app && a.stream == b.stream; +} /////////////////////////////////////MediaInfo////////////////////////////////////// void MediaInfo::parse(const std::string &url_in){ @@ -663,7 +647,7 @@ void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){ GET_CONFIG(string, record_app, Record::kAppName); GET_CONFIG(int, stream_none_reader_delay, General::kStreamNoneReaderDelayMS); //如果mp4点播, 无人观看时我们强制关闭点播 - bool is_mp4_vod = sender.getApp() == record_app; + bool is_mp4_vod = sender.getMediaTuple().app == record_app; weak_ptr weak_sender = sender.shared_from_this(); _async_close_timer = std::make_shared(stream_none_reader_delay / 1000.0f, [weak_sender, is_mp4_vod]() { diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index bbe8035f..9391e6fb 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -274,6 +274,8 @@ public: std::string param_strs; }; +bool equalMediaTuple(const MediaTuple& a, const MediaTuple& b); + /** * 媒体源,任何rtsp/rtmp的直播流都源自该对象 */ @@ -288,21 +290,15 @@ public: ////////////////获取MediaSource相关信息//////////////// // 获取协议类型 - const std::string& getSchema() const; - // 虚拟主机 - const std::string& getVhost() const; - // 应用名 - const std::string& getApp() const; - // 流id - const std::string& getId() const; + const std::string& getSchema() const { + return _schema; + } const MediaTuple& getMediaTuple() const { return _tuple; } - std::string shortUrl() const { return _tuple.shortUrl(); } - - std::string getUrl() const { return _schema + "://" + shortUrl(); } + std::string getUrl() const { return _schema + "://" + _tuple.shortUrl(); } //获取对象所有权 std::shared_ptr getOwnership(); diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index a9d904be..f1921a95 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -70,18 +70,6 @@ static string getTrackInfoStr(const TrackSource *track_src){ return std::move(codec_info); } -const std::string &MultiMediaSourceMuxer::getVhost() const { - return _tuple.vhost; -} - -const std::string &MultiMediaSourceMuxer::getApp() const { - return _tuple.app; -} - -const std::string &MultiMediaSourceMuxer::getStreamId() const { - return _tuple.stream; -} - std::string MultiMediaSourceMuxer::shortUrl() const { auto ret = getOriginUrl(MediaSource::NullMediaSource()); if (!ret.empty()) { diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index 0e4f6a5b..a9983550 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -126,9 +126,6 @@ public: */ toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override; - const std::string& getVhost() const; - const std::string& getApp() const; - const std::string& getStreamId() const; const MediaTuple& getMediaTuple() const { return _tuple; } diff --git a/tests/test_pusher.cpp b/tests/test_pusher.cpp index adf85199..8e5ca1f7 100644 --- a/tests/test_pusher.cpp +++ b/tests/test_pusher.cpp @@ -88,8 +88,9 @@ int domain(const string &playUrl, const string &pushUrl) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged, [pushUrl,poller](BroadcastMediaChangedArgs) { //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源 - if(bRegist && pushUrl.find(sender.getSchema()) == 0){ - createPusher(poller,sender.getSchema(),sender.getVhost(),sender.getApp(), sender.getId(), pushUrl); + if (bRegist && pushUrl.find(sender.getSchema()) == 0) { + auto tuple = sender.getMediaTuple(); + createPusher(poller, sender.getSchema(), tuple.vhost, tuple.app, tuple.stream, pushUrl); } }); diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 6a15c532..dc123ba2 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -155,9 +155,10 @@ void initEventListener() { //监听rtsp、rtmp源注册或注销事件;此处用于测试rtmp保存为flv录像,保存在http根目录下 NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged, [](BroadcastMediaChangedArgs) { - if (sender.getSchema() == RTMP_SCHEMA && sender.getApp() == "live") { + auto tuple = sender.getMediaTuple(); + if (sender.getSchema() == RTMP_SCHEMA && tuple.app == "live") { lock_guard lck(s_mtxFlvRecorder); - auto key = sender.shortUrl(); + auto key = tuple.shortUrl(); if (bRegist) { DebugL << "开始录制RTMP:" << sender.getUrl(); GET_CONFIG(string, http_root, Http::kRootPath); diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index 334236bf..e8e46a40 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -98,8 +98,8 @@ void WebRtcPusher::onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Pt //视频 auto &src = _push_src_sim[rid]; if (!src) { - auto stream_id = rid.empty() ? _push_src->getId() : _push_src->getId() + "_" + rid; - auto src_imp = _push_src->clone(stream_id); + const auto& stream = _push_src->getMediaTuple().stream; + auto src_imp = _push_src->clone(rid.empty() ? stream : stream + '_' + rid); _push_src_sim_ownership[rid] = src_imp->getOwnership(); src_imp->setListener(static_pointer_cast(shared_from_this())); src = src_imp; From 7e117b1c7f27cb96d11411ba639fdb49d772031a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sat, 17 Jun 2023 10:29:27 +0800 Subject: [PATCH 56/66] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81http-?= =?UTF-8?q?flv=E6=92=AD=E6=94=BE=E5=99=A8=20(#2562)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- README_en.md | 3 +- src/Player/PlayerBase.cpp | 18 ++++-- src/Rtmp/FlvMuxer.cpp | 10 ++- src/Rtmp/FlvPlayer.cpp | 72 ++++++++++++++++++++++ src/Rtmp/FlvPlayer.h | 48 +++++++++++++++ src/Rtmp/FlvSplitter.cpp | 125 ++++++++++++++++++++++++++++++++++++++ src/Rtmp/FlvSplitter.h | 42 +++++++++++++ src/Rtmp/Rtmp.h | 4 ++ src/Rtmp/RtmpPlayer.cpp | 59 +++--------------- src/Rtmp/RtmpPlayer.h | 4 +- src/Rtmp/RtmpPlayerImp.h | 118 ++++++++++++++++++++++++----------- tests/test_flv.cpp | 112 ++++++++++++++++++++++++++++++++++ 13 files changed, 514 insertions(+), 104 deletions(-) create mode 100644 src/Rtmp/FlvPlayer.cpp create mode 100644 src/Rtmp/FlvPlayer.h create mode 100644 src/Rtmp/FlvSplitter.cpp create mode 100644 src/Rtmp/FlvSplitter.h create mode 100644 tests/test_flv.cpp diff --git a/README.md b/README.md index fa9f50d5..ddee13cd 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,8 @@ - RTMP[S] 发布服务器,支持录制发布流 - RTMP[S] 播放器,支持RTMP代理,支持生成静音音频 - RTMP[S] 推流客户端 - - 支持http[s]-flv直播 + - 支持http[s]-flv直播服务器 + - 支持http[s]-flv直播播放器 - 支持websocket-flv直播 - 支持H264/H265/AAC/G711/OPUS编码,其他编码能转发但不能转协议 - 支持[RTMP-H265](https://github.com/ksvc/FFmpeg/wiki) diff --git a/README_en.md b/README_en.md index b7410f8b..154f51c7 100644 --- a/README_en.md +++ b/README_en.md @@ -62,7 +62,8 @@ - RTMP[S] publishing server, supports recording and publishing streams - RTMP[S] player, supports RTMP proxy, supports generating silent audio - RTMP[S] push client - - Supports http[s]-flv live streaming + - Supports http[s]-flv live streaming server + - Supports http[s]-flv live streaming player - Supports websocket-flv live streaming - Supports H264/H265/AAC/G711/OPUS encoding. Other encodings can be forwarded but cannot be converted to protocol - Supports [RTMP-H265](https://github.com/ksvc/FFmpeg/wiki) diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index cb00a0da..3f465bd7 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -12,6 +12,7 @@ #include "PlayerBase.h" #include "Rtsp/RtspPlayerImp.h" #include "Rtmp/RtmpPlayerImp.h" +#include "Rtmp/FlvPlayer.h" #include "Http/HlsPlayer.h" #include "Http/TsPlayerImp.h" @@ -20,12 +21,13 @@ using namespace toolkit; namespace mediakit { -PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller, const string &url_in) { - static auto releasePlayer = [](PlayerBase *ptr) { - onceToken token(nullptr, [&]() { - delete ptr; +PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, const string &url_in) { + auto poller = in_poller ? in_poller : EventPollerPool::Instance().getPoller(); + static auto releasePlayer = [poller](PlayerBase *ptr) { + poller->async([ptr]() { + onceToken token(nullptr, [&]() { delete ptr; }); + ptr->teardown(); }); - ptr->teardown(); }; string url = url_in; string prefix = findSubString(url.data(), NULL, "://"); @@ -53,9 +55,13 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller, const s if ((strcasecmp("http", prefix.data()) == 0 || strcasecmp("https", prefix.data()) == 0)) { if (end_with(url, ".m3u8") || end_with(url_in, ".m3u8")) { return PlayerBase::Ptr(new HlsPlayerImp(poller), releasePlayer); - } else if (end_with(url, ".ts") || end_with(url_in, ".ts")) { + } + if (end_with(url, ".ts") || end_with(url_in, ".ts")) { return PlayerBase::Ptr(new TsPlayerImp(poller), releasePlayer); } + if (end_with(url, ".flv") || end_with(url_in, ".flv")) { + return PlayerBase::Ptr(new FlvPlayerImp(poller), releasePlayer); + } } throw std::invalid_argument("not supported play schema:" + url_in); diff --git a/src/Rtmp/FlvMuxer.cpp b/src/Rtmp/FlvMuxer.cpp index bd64acf9..a68df5bc 100644 --- a/src/Rtmp/FlvMuxer.cpp +++ b/src/Rtmp/FlvMuxer.cpp @@ -97,18 +97,16 @@ void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &src) { header->flv[0] = 'F'; header->flv[1] = 'L'; header->flv[2] = 'V'; - header->version = 1; - header->length = htonl(9); + header->version = FLVHeader::kFlvVersion; + header->length = htonl(FLVHeader::kFlvHeaderLength); header->have_video = src->haveVideo(); header->have_audio = src->haveAudio(); + //memset时已经赋值为0 + //header->previous_tag_size0 = 0; //flv header onWrite(buffer, false); - //PreviousTagSize0 Always 0 - auto size = htonl(0); - onWrite(obtainBuffer((char *) &size, 4), false); - auto &metadata = src->getMetaData(); if (metadata) { //在有metadata的情况下才发送metadata diff --git a/src/Rtmp/FlvPlayer.cpp b/src/Rtmp/FlvPlayer.cpp new file mode 100644 index 00000000..c0dc1dfa --- /dev/null +++ b/src/Rtmp/FlvPlayer.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ + +#include "FlvPlayer.h" + +using namespace std; +using namespace toolkit; + +namespace mediakit { + +FlvPlayer::FlvPlayer(const EventPoller::Ptr &poller) { + setPoller(poller); +} + +void FlvPlayer::play(const string &url) { + TraceL << "play http-flv: " << url; + _play_result = false; + setHeaderTimeout((*this)[Client::kTimeoutMS].as()); + setBodyTimeout((*this)[Client::kMediaTimeoutMS].as()); + setMethod("GET"); + sendRequest(url); +} + +void FlvPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &header) { + if (status != "200" && status != "206") { + // http状态码不符合预期 + throw invalid_argument("bad http status code:" + status); + } + + auto content_type = const_cast(header)["Content-Type"]; + if (content_type.find("video/x-flv") != 0) { + throw invalid_argument("content type not http-flv: " + content_type); + } +} + +void FlvPlayer::teardown() { + HttpClientImp::shutdown(); +} + +void FlvPlayer::onResponseCompleted(const SockException &ex) { + if (!_play_result) { + _play_result = true; + onPlayResult(ex); + } else { + onShutdown(ex); + } +} + +void FlvPlayer::onResponseBody(const char *buf, size_t size) { + FlvSplitter::input(buf, size); +} + +bool FlvPlayer::onRecvMetadata(const AMFValue &metadata) { + return onMetadata(metadata); +} + +void FlvPlayer::onRecvRtmpPacket(RtmpPacket::Ptr packet) { + if (!_play_result && !packet->isCfgFrame()) { + _play_result = true; + onPlayResult(SockException(Err_success, "play http-flv success")); + } + onRtmpPacket(std::move(packet)); +} + +}//mediakit \ No newline at end of file diff --git a/src/Rtmp/FlvPlayer.h b/src/Rtmp/FlvPlayer.h new file mode 100644 index 00000000..c49b2b3f --- /dev/null +++ b/src/Rtmp/FlvPlayer.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef ZLMEDIAKIT_FLVPLAYER_H +#define ZLMEDIAKIT_FLVPLAYER_H + +#include "FlvSplitter.h" +#include "Http/HttpClientImp.h" +#include "Player/PlayerBase.h" + +namespace mediakit { + +class FlvPlayer : public PlayerBase, public HttpClientImp, private FlvSplitter { +public: + FlvPlayer(const toolkit::EventPoller::Ptr &poller); + ~FlvPlayer() override = default; + + void play(const std::string &url) override; + void teardown() override; + +protected: + void onResponseHeader(const std::string &status, const HttpHeader &header) override; + void onResponseCompleted(const toolkit::SockException &ex) override; + void onResponseBody(const char *buf, size_t size) override; + +protected: + virtual void onRtmpPacket(RtmpPacket::Ptr packet) = 0; + virtual bool onMetadata(const AMFValue &metadata) = 0; + +private: + bool onRecvMetadata(const AMFValue &metadata) override; + void onRecvRtmpPacket(RtmpPacket::Ptr packet) override; + +private: + bool _play_result = false; +}; + +using FlvPlayerImp = FlvPlayerBase; + +}//namespace mediakit +#endif //ZLMEDIAKIT_FLVPLAYER_H diff --git a/src/Rtmp/FlvSplitter.cpp b/src/Rtmp/FlvSplitter.cpp new file mode 100644 index 00000000..c2e21e6a --- /dev/null +++ b/src/Rtmp/FlvSplitter.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ +#include "FlvSplitter.h" +#include "utils.h" + +using namespace std; +using namespace toolkit; + +namespace mediakit { + +const char *FlvSplitter::onSearchPacketTail(const char *data, size_t len) { + if (!_flv_started) { + //还没获取到flv头 + if (len < sizeof(FLVHeader)) { + //数据不够 + return nullptr; + } + return data + sizeof(FLVHeader); + } + + //获取到flv头,处理tag数据 + if (len < sizeof(RtmpTagHeader)) { + //数据不够 + return nullptr; + } + return data + sizeof(RtmpTagHeader); +} + +ssize_t FlvSplitter::onRecvHeader(const char *data, size_t len) { + if (!_flv_started) { + //获取到flv头了 + auto header = reinterpret_cast(data); + if (memcmp(header->flv, "FLV", 3)) { + throw std::invalid_argument("不是flv容器格式!"); + } + if (header->version != FLVHeader::kFlvVersion) { + throw std::invalid_argument("flv头中version字段不正确"); + } + if (!header->have_video && !header->have_audio) { + throw std::invalid_argument("flv头中声明音频和视频都不存在"); + } + if (FLVHeader::kFlvHeaderLength != ntohl(header->length)) { + throw std::invalid_argument("flv头中length字段非法"); + } + if (0 != ntohl(header->previous_tag_size0)) { + throw std::invalid_argument("flv头中previous tag size字段非法"); + } + onRecvFlvHeader(*header); + _flv_started = true; + return 0; + } + + //获取到flv头,处理tag数据 + auto tag = reinterpret_cast(data); + auto data_size = load_be24(tag->data_size); + _type = tag->type; + _time_stamp = load_be24(tag->timestamp); + _time_stamp |= (tag->timestamp_ex << 24); + return data_size + 4/*PreviousTagSize*/; +} + +void FlvSplitter::onRecvContent(const char *data, size_t len) { + len -= 4; + auto previous_tag_size = load_be32(data + len); + if (len != previous_tag_size - sizeof(RtmpTagHeader)) { + WarnL << "flv previous tag size 字段非法:" << len << " != " << previous_tag_size - sizeof(RtmpTagHeader); + } + RtmpPacket::Ptr packet; + switch (_type) { + case MSG_AUDIO : { + packet = RtmpPacket::create(); + packet->chunk_id = CHUNK_AUDIO; + packet->stream_index = STREAM_MEDIA; + break; + } + case MSG_VIDEO: { + packet = RtmpPacket::create(); + packet->chunk_id = CHUNK_VIDEO; + packet->stream_index = STREAM_MEDIA; + break; + } + + case MSG_DATA: + case MSG_DATA3: { + BufferLikeString buffer(string(data, len)); + AMFDecoder dec(buffer, _type == MSG_DATA3 ? 3 : 0); + std::string type = dec.load(); + bool flag = true; + if (type == "@setDataFrame") { + std::string type = dec.load(); + if (type == "onMetaData") { + flag = onRecvMetadata(dec.load()); + } else { + WarnL << "unknown type:" << type; + } + } else if (type == "onMetaData") { + flag = onRecvMetadata(dec.load()); + } else { + WarnL << "unknown notify:" << type; + } + if(!flag){ + throw std::invalid_argument("check rtmp metadata failed"); + } + return; + } + + default: WarnL << "不识别的flv msg type:" << (int) _type; return; + } + + packet->time_stamp = _time_stamp; + packet->type_id = _type; + packet->body_size = len; + packet->buffer.assign(data, len); + onRecvRtmpPacket(std::move(packet)); +} + + +}//namespace mediakit \ No newline at end of file diff --git a/src/Rtmp/FlvSplitter.h b/src/Rtmp/FlvSplitter.h new file mode 100644 index 00000000..78f2d1d5 --- /dev/null +++ b/src/Rtmp/FlvSplitter.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef ZLMEDIAKIT_FLVSPLITTER_H +#define ZLMEDIAKIT_FLVSPLITTER_H + +#include "Rtmp.h" +#include "Http/HttpRequestSplitter.h" +#include "RtmpPlayerImp.h" + +namespace mediakit { + +class FlvSplitter : public HttpRequestSplitter { +public: + FlvSplitter() = default; + ~FlvSplitter() = default; + +protected: + void onRecvContent(const char *data,size_t len) override; + ssize_t onRecvHeader(const char *data,size_t len) override; + const char *onSearchPacketTail(const char *data, size_t len) override; + +protected: + virtual void onRecvFlvHeader(const FLVHeader &header) {}; + virtual bool onRecvMetadata(const AMFValue &metadata) = 0; + virtual void onRecvRtmpPacket(RtmpPacket::Ptr packet) = 0; + +private: + bool _flv_started = false; + uint8_t _type; + uint32_t _time_stamp; +}; + +}//namespace mediakit +#endif //ZLMEDIAKIT_FLVSPLITTER_H diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index 55802549..739d4cf5 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -113,6 +113,8 @@ public: class FLVHeader { public: + static constexpr uint8_t kFlvVersion = 1; + static constexpr uint8_t kFlvHeaderLength = 9; //FLV char flv[3]; //File version (for example, 0x01 for FLV version 1) @@ -138,6 +140,8 @@ public: #endif //The length of this header in bytes,固定为9 uint32_t length; + //固定为0 + uint32_t previous_tag_size0; } PACKED; class RtmpTagHeader { diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 1c2b335c..9546d889 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -313,8 +313,8 @@ void RtmpPlayer::onCmd_onStatus(AMFDecoder &dec) { void RtmpPlayer::onCmd_onMetaData(AMFDecoder &dec) { //TraceL; auto val = dec.load(); - if (!onCheckMeta(val)) { - throw std::runtime_error("onCheckMeta failed"); + if (!onMetadata(val)) { + throw std::runtime_error("onMetadata failed"); } _metadata_got = true; } @@ -328,18 +328,18 @@ void RtmpPlayer::onMediaData_l(RtmpPacket::Ptr chunk_data) { _rtmp_recv_ticker.resetTime(); if (!_play_timer) { //已经触发了onPlayResult事件,直接触发onMediaData事件 - onMediaData(chunk_data); + onRtmpPacket(chunk_data); return; } if (chunk_data->isCfgFrame()) { //输入配置帧以便初始化完成各个track - onMediaData(chunk_data); + onRtmpPacket(chunk_data); } else { //先触发onPlayResult事件,这个时候解码器才能初始化完毕 onPlayResult_l(SockException(Err_success, "play rtmp success"), false); //触发onPlayResult事件后,再把帧数据输入到解码器 - onMediaData(chunk_data); + onRtmpPacket(chunk_data); } } @@ -379,8 +379,8 @@ void RtmpPlayer::onRtmpChunk(RtmpPacket::Ptr packet) { _now_stamp[idx] = chunk_data.time_stamp; } if (!_metadata_got) { - if (!onCheckMeta(TitleMeta().getMetadata())) { - throw std::runtime_error("onCheckMeta failed"); + if (!onMetadata(TitleMeta().getMetadata())) { + throw std::runtime_error("onMetadata failed"); } _metadata_got = true; } @@ -420,49 +420,4 @@ void RtmpPlayer::seekToMilliSecond(uint32_t seekMS){ }); } -//////////////////////////////////////////// -float RtmpPlayerImp::getDuration() const -{ - return _demuxer ? _demuxer->getDuration() : 0; -} - -std::vector RtmpPlayerImp::getTracks(bool ready /*= true*/) const -{ - return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); -} - -bool RtmpPlayerImp::onCheckMeta(const AMFValue &val) -{ - //无metadata或metadata中无track信息时,需要从数据包中获取track - _wait_track_ready = (*this)[Client::kWaitTrackReady].as() || RtmpDemuxer::trackCount(val) == 0; - onCheckMeta_l(val); - return true; -} - -void RtmpPlayerImp::onMediaData(RtmpPacket::Ptr chunkData) -{ - if (!_demuxer) { - //有些rtmp流没metadata - onCheckMeta_l(TitleMeta().getMetadata()); - } - _demuxer->inputRtmp(chunkData); - if (_rtmp_src) { - _rtmp_src->onWrite(std::move(chunkData)); - } -} - -void RtmpPlayerImp::onCheckMeta_l(const AMFValue &val) -{ - _rtmp_src = std::dynamic_pointer_cast(_media_src); - if (_rtmp_src) { - _rtmp_src->setMetaData(val); - } - if (_demuxer) { - return; - } - _demuxer = std::make_shared(); - //TraceL<<" _wait_track_ready "<<_wait_track_ready; - _demuxer->setTrackListener(this, _wait_track_ready); - _demuxer->loadMetaData(val); -} } /* namespace mediakit */ diff --git a/src/Rtmp/RtmpPlayer.h b/src/Rtmp/RtmpPlayer.h index 0a45f1b5..75abf7f9 100644 --- a/src/Rtmp/RtmpPlayer.h +++ b/src/Rtmp/RtmpPlayer.h @@ -37,8 +37,8 @@ public: void teardown() override; protected: - virtual bool onCheckMeta(const AMFValue &val) = 0; - virtual void onMediaData(RtmpPacket::Ptr chunk_data) = 0; + virtual bool onMetadata(const AMFValue &val) = 0; + virtual void onRtmpPacket(RtmpPacket::Ptr chunk_data) = 0; uint32_t getProgressMilliSecond() const; void seekToMilliSecond(uint32_t ms); diff --git a/src/Rtmp/RtmpPlayerImp.h b/src/Rtmp/RtmpPlayerImp.h index 25b33741..ecf9a34f 100644 --- a/src/Rtmp/RtmpPlayerImp.h +++ b/src/Rtmp/RtmpPlayerImp.h @@ -13,16 +13,95 @@ #include #include +#include "Common/config.h" #include "RtmpPlayer.h" -#include "RtmpDemuxer.h" #include "RtmpMediaSource.h" +#include "RtmpDemuxer.h" +#include "Poller/Timer.h" +#include "Util/TimeTicker.h" namespace mediakit { -class RtmpPlayerImp: public PlayerImp, private TrackListener { +template +class FlvPlayerBase: public PlayerImp, private TrackListener { +public: + using Ptr = std::shared_ptr; + using Super = PlayerImp; + + FlvPlayerBase(const toolkit::EventPoller::Ptr &poller) : Super(poller) {}; + + ~FlvPlayerBase() override { + DebugL << std::endl; + } + + float getDuration() const override { + return _demuxer ? _demuxer->getDuration() : 0; + } + + std::vector getTracks(bool ready = true) const override { + return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready); + } + +private: + //派生类回调函数 + bool onMetadata(const AMFValue &val) override { + //无metadata或metadata中无track信息时,需要从数据包中获取track + _wait_track_ready = this->Super::operator[](Client::kWaitTrackReady).template as() || RtmpDemuxer::trackCount(val) == 0; + onCheckMeta_l(val); + return true; + } + + void onRtmpPacket(RtmpPacket::Ptr chunkData) override { + if (!_demuxer) { + //有些rtmp流没metadata + onCheckMeta_l(TitleMeta().getMetadata()); + } + _demuxer->inputRtmp(chunkData); + if (_rtmp_src) { + _rtmp_src->onWrite(std::move(chunkData)); + } + } + + void onPlayResult(const toolkit::SockException &ex) override { + if (!_wait_track_ready || ex) { + Super::onPlayResult(ex); + return; + } + } + + bool addTrack(const Track::Ptr &track) override { return true; } + + void addTrackCompleted() override { + if (_wait_track_ready) { + Super::onPlayResult(toolkit::SockException(toolkit::Err_success, "play success")); + } + } + +private: + void onCheckMeta_l(const AMFValue &val) { + _rtmp_src = std::dynamic_pointer_cast(this->Super::_media_src); + if (_rtmp_src) { + _rtmp_src->setMetaData(val); + } + if(_demuxer){ + return; + } + _demuxer = std::make_shared(); + //TraceL<<" _wait_track_ready "<<_wait_track_ready; + _demuxer->setTrackListener(this, _wait_track_ready); + _demuxer->loadMetaData(val); + } + +private: + bool _wait_track_ready = true; + RtmpDemuxer::Ptr _demuxer; + RtmpMediaSource::Ptr _rtmp_src; +}; + +class RtmpPlayerImp: public FlvPlayerBase { public: using Ptr = std::shared_ptr; - using Super = PlayerImp; + using Super = FlvPlayerBase; RtmpPlayerImp(const toolkit::EventPoller::Ptr &poller) : Super(poller) {}; @@ -46,39 +125,6 @@ public: uint32_t pos = MAX(float(0), MIN(seekPos, getDuration())) * 1000; seekToMilliSecond(pos); } - - float getDuration() const override; - - std::vector getTracks(bool ready = true) const override; - -private: - //派生类回调函数 - bool onCheckMeta(const AMFValue &val) override; - - void onMediaData(RtmpPacket::Ptr chunkData) override; - - void onPlayResult(const toolkit::SockException &ex) override { - if (!_wait_track_ready || ex) { - Super::onPlayResult(ex); - return; - } - } - - bool addTrack(const Track::Ptr &track) override { return true; } - - void addTrackCompleted() override { - if (_wait_track_ready) { - Super::onPlayResult(toolkit::SockException(toolkit::Err_success, "play success")); - } - } - -private: - void onCheckMeta_l(const AMFValue &val); - -private: - bool _wait_track_ready = true; - RtmpDemuxer::Ptr _demuxer; - RtmpMediaSource::Ptr _rtmp_src; }; diff --git a/tests/test_flv.cpp b/tests/test_flv.cpp new file mode 100644 index 00000000..ffed816f --- /dev/null +++ b/tests/test_flv.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ + +#include +#include +#include "Util/logger.h" +#include "Util/util.h" +#include "Network/TcpServer.h" +#include "Common/config.h" +#include "Rtsp/RtspSession.h" +#include "Rtmp/RtmpSession.h" +#include "Http/HttpSession.h" +#include "Rtmp/FlvSplitter.h" +#include "Rtmp/RtmpMediaSourceImp.h" + +using namespace std; +using namespace toolkit; +using namespace mediakit; + +class FlvSplitterImp : public FlvSplitter { +public: + FlvSplitterImp() { + _src = std::make_shared(MediaTuple{DEFAULT_VHOST, "live", "test"}); + } + ~FlvSplitterImp() override = default; + + void inputData(const char *data, size_t len, uint32_t &stamp) { + FlvSplitter::input(data, len); + stamp = _stamp; + } + +protected: + void onRecvFlvHeader(const FLVHeader &header) override { + } + + bool onRecvMetadata(const AMFValue &metadata) override { + _src->setMetaData(metadata); + _src->setProtocolOption(ProtocolOption()); + return true; + } + + void onRecvRtmpPacket(RtmpPacket::Ptr packet) override { + _stamp = packet->time_stamp; + _src->onWrite(std::move(packet)); + } + +private: + uint32_t _stamp; + RtmpMediaSourceImp::Ptr _src; +}; + +static bool loadFile(const char *path){ + FILE *fp = fopen(path, "rb"); + if (!fp) { + WarnL << "open file failed:" << path; + return false; + } + + char buffer[64 * 1024]; + uint32_t timeStamp_last = 0; + size_t len; + size_t total_size = 0; + FlvSplitterImp flv; + while (true) { + len = fread(buffer, 1, sizeof(buffer), fp); + if (len <= 0) { + break; + } + total_size += len; + uint32_t timeStamp; + flv.inputData(buffer, len, timeStamp); + auto diff = timeStamp - timeStamp_last; + if (diff > 0) { + usleep(diff * 1000); + } else { + usleep(1 * 1000); + } + timeStamp_last = timeStamp; + } + WarnL << total_size / 1024 << "KB"; + fclose(fp); + return true; +} + +int main(int argc,char *argv[]) { + //设置日志 + Logger::Instance().add(std::make_shared("ConsoleChannel")); + //启动异步日志线程 + Logger::Instance().setWriter(std::make_shared()); + loadIniConfig((exeDir() + "config.ini").data()); + TcpServer::Ptr rtspSrv(new TcpServer()); + TcpServer::Ptr rtmpSrv(new TcpServer()); + TcpServer::Ptr httpSrv(new TcpServer()); + rtspSrv->start(554);//默认554 + rtmpSrv->start(1935);//默认1935 + httpSrv->start(80);//默认80 + + if (argc == 2) + loadFile(argv[1]); + else + ErrorL << "parameter error."; + return 0; +} + + From 9f753b5e5f95a734117928729a49d85654e09d23 Mon Sep 17 00:00:00 2001 From: Talus Date: Wed, 21 Jun 2023 15:35:27 +0800 Subject: [PATCH 57/66] =?UTF-8?q?RTSP=E6=94=AF=E6=8C=81=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E5=8D=8F=E5=95=86RTP=E4=BC=A0=E8=BE=93=E6=96=B9=E5=BC=8F=20(#2?= =?UTF-8?q?572)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当客户端发起RTSP SETUP的时候,如果rtp传输类型和配置不一致则返回461 Unsupported transport。迫使客户端切换到对应rtp传输协议并重新SETUP;目前支持FFMPEG和VLC。 --- conf/config.ini | 4 ++++ src/Common/config.cpp | 2 ++ src/Common/config.h | 5 +++++ src/Rtsp/RtspSession.cpp | 43 +++++++++++++++++++++++++++++++--------- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 4fb5cc1c..348b134d 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -362,6 +362,10 @@ port=554 sslport=0 #rtsp 转发是否使用低延迟模式,当开启时,不会缓存rtp包,来提高并发,可以降低一帧的延迟 lowLatency=0 +#强制协商rtp传输方式 (0:TCP,1:UDP,2:MULTICAST,-1:不限制) +#当客户端发起RTSP SETUP的时候如果传输类型和此配置不一致则返回461 Unsupported transport +#迫使客户端重新SETUP并切换到对应协议。目前支持FFMPEG和VLC +rtpTransportType=-1 [shell] #调试telnet服务器接受最大bufffer大小 maxReqSize=1024 diff --git a/src/Common/config.cpp b/src/Common/config.cpp index ce5d8935..5783b54c 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -209,6 +209,7 @@ const string kHandshakeSecond = RTSP_FIELD "handshakeSecond"; const string kKeepAliveSecond = RTSP_FIELD "keepAliveSecond"; const string kDirectProxy = RTSP_FIELD "directProxy"; const string kLowLatency = RTSP_FIELD"lowLatency"; +const string kRtpTransportType = RTSP_FIELD"rtpTransportType"; static onceToken token([]() { // 默认Md5方式认证 @@ -217,6 +218,7 @@ static onceToken token([]() { mINI::Instance()[kKeepAliveSecond] = 15; mINI::Instance()[kDirectProxy] = 1; mINI::Instance()[kLowLatency] = 0; + mINI::Instance()[kRtpTransportType] = -1; }); } // namespace Rtsp diff --git a/src/Common/config.h b/src/Common/config.h index ea874d64..74ea5d71 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -273,6 +273,11 @@ extern const std::string kDirectProxy; // rtsp 转发是否使用低延迟模式,当开启时,不会缓存rtp包,来提高并发,可以降低一帧的延迟 extern const std::string kLowLatency; + +//强制协商rtp传输方式 (0:TCP,1:UDP,2:MULTICAST,-1:不限制) +//当客户端发起RTSP SETUP的时候如果传输类型和此配置不一致则返回461 Unsupport Transport +//迫使客户端重新SETUP并切换到对应协议。目前支持FFMPEG和VLC +extern const std::string kRtpTransportType; } // namespace Rtsp ////////////RTMP服务器配置/////////// diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 15db5aa8..e9c69ac2 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -634,19 +634,44 @@ void RtspSession::handleReq_Setup(const Parser &parser) { //已经初始化过该Track throw SockException(Err_shutdown, "can not setup one track twice"); } - trackRef->_inited = true; //现在初始化 - if(_rtp_type == Rtsp::RTP_Invalid){ - auto &strTransport = parser["Transport"]; - if(strTransport.find("TCP") != string::npos){ - _rtp_type = Rtsp::RTP_TCP; - }else if(strTransport.find("multicast") != string::npos){ - _rtp_type = Rtsp::RTP_MULTICAST; - }else{ - _rtp_type = Rtsp::RTP_UDP; + static auto getRtpTypeStr = [](const int type) { + switch (type) + { + case Rtsp::RTP_TCP: + return "TCP"; + case Rtsp::RTP_UDP: + return "UDP"; + case Rtsp::RTP_MULTICAST: + return "MULTICAST"; + default: + return "Invalid"; } + }; + + if (_rtp_type == Rtsp::RTP_Invalid) { + auto &strTransport = parser["Transport"]; + auto rtpType = Rtsp::RTP_Invalid; + if (strTransport.find("TCP") != string::npos) { + rtpType = Rtsp::RTP_TCP; + } else if (strTransport.find("multicast") != string::npos) { + rtpType = Rtsp::RTP_MULTICAST; + } else { + rtpType = Rtsp::RTP_UDP; + } + //检查RTP传输类型限制 + GET_CONFIG(int, transport, Rtsp::kRtpTransportType); + if (transport != Rtsp::RTP_Invalid && transport != rtpType) { + WarnL << "rtsp client setup transport " << getRtpTypeStr(rtpType) << " but config force transport " << getRtpTypeStr(transport); + //配置限定RTSP传输方式,但是客户端握手方式不一致,返回461 + sendRtspResponse("461 Unsupported transport"); + return; + } + _rtp_type = rtpType; } + trackRef->_inited = true; //现在初始化 + //允许接收rtp、rtcp包 RtspSplitter::enableRecvRtp(_rtp_type == Rtsp::RTP_TCP); From 24d210f61db3fc508cc6344a97ef84a38bbff3a2 Mon Sep 17 00:00:00 2001 From: Luosh Date: Wed, 21 Jun 2023 17:20:39 +0800 Subject: [PATCH 58/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNTP=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E8=AE=A1=E7=AE=97=E7=B2=BE=E5=BA=A6=E4=B8=8D=E8=B6=B3?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=AF=AF=E5=B7=AE=E7=B4=AF=E7=A7=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2576=20#2570=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ntp时间戳计算精度由毫秒调整为微秒,解决误差累积问题。 --- src/Common/Stamp.cpp | 56 ++++++++++++++++++++++---------------------- src/Common/Stamp.h | 6 ++--- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index a19b96d6..87282c39 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -236,68 +236,68 @@ void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { WarnL << "Invalid sender report rtcp, ntp_stamp_ms = " << ntp_stamp_ms << ", rtp_stamp = " << rtp_stamp; return; } - update(rtp_stamp, ntp_stamp_ms); + update(rtp_stamp, ntp_stamp_ms * 1000); } -void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { +void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_us) { _last_rtp_stamp = rtp_stamp; - _last_ntp_stamp_ms = ntp_stamp_ms; + _last_ntp_stamp_us = ntp_stamp_us; } uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) { if (rtp_stamp == _last_rtp_stamp) { - return _last_ntp_stamp_ms; + return _last_ntp_stamp_us / 1000; } - return getNtpStamp_l(rtp_stamp, sample_rate); + return getNtpStampUS(rtp_stamp, sample_rate) / 1000; } -uint64_t NtpStamp::getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate) { - if (!_last_ntp_stamp_ms) { +uint64_t NtpStamp::getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate) { + if (!_last_ntp_stamp_us) { // 尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧 - update(rtp_stamp, getCurrentMillisecond(true)); + update(rtp_stamp, getCurrentMicrosecond(true)); } // rtp时间戳正增长 if (rtp_stamp >= _last_rtp_stamp) { - auto diff = static_cast((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f)); - if (diff < MAX_DELTA_STAMP) { + auto diff_us = static_cast((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000000.0f)); + if (diff_us < MAX_DELTA_STAMP * 1000) { // 时间戳正常增长 - update(rtp_stamp, _last_ntp_stamp_ms + diff); - return _last_ntp_stamp_ms; + update(rtp_stamp, _last_ntp_stamp_us + diff_us); + return _last_ntp_stamp_us; } // 时间戳大幅跳跃 - uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; - if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) { + uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000; + if (_last_rtp_stamp < loop_delta_hz && rtp_stamp > UINT32_MAX - loop_delta_hz) { // 应该是rtp时间戳溢出+乱序 - uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; - return _last_ntp_stamp_ms + diff - max_rtp_ms; + uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate; + return _last_ntp_stamp_us + diff_us - max_rtp_us; } // 不明原因的时间戳大幅跳跃,直接返回上次值 WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp; - update(rtp_stamp, _last_ntp_stamp_ms); - return _last_ntp_stamp_ms; + update(rtp_stamp, _last_ntp_stamp_us); + return _last_ntp_stamp_us; } // rtp时间戳负增长 - auto diff = static_cast((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f)); - if (diff < MAX_DELTA_STAMP) { + auto diff_us = static_cast((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000000.0f)); + if (diff_us < MAX_DELTA_STAMP * 1000) { // 正常范围的时间戳回退,说明收到rtp乱序了 - return _last_ntp_stamp_ms - diff; + return _last_ntp_stamp_us - diff_us; } // 时间戳大幅度回退 - uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; - if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) { + uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000; + if (rtp_stamp < loop_delta_hz && _last_rtp_stamp > UINT32_MAX - loop_delta_hz) { // 确定是时间戳溢出 - uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; - update(rtp_stamp, _last_ntp_stamp_ms + (max_rtp_ms - diff)); - return _last_ntp_stamp_ms; + uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate; + update(rtp_stamp, _last_ntp_stamp_us + (max_rtp_us - diff_us)); + return _last_ntp_stamp_us; } // 不明原因的时间戳回退,直接返回上次值 WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp; - update(rtp_stamp, _last_ntp_stamp_ms); - return _last_ntp_stamp_ms; + update(rtp_stamp, _last_ntp_stamp_us); + return _last_ntp_stamp_us; } } // namespace mediakit diff --git a/src/Common/Stamp.h b/src/Common/Stamp.h index cc2dcae0..03cb7bb7 100644 --- a/src/Common/Stamp.h +++ b/src/Common/Stamp.h @@ -125,12 +125,12 @@ public: uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate); private: - void update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms); - uint64_t getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate); + void update(uint32_t rtp_stamp, uint64_t ntp_stamp_us); + uint64_t getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate); private: uint32_t _last_rtp_stamp = 0; - uint64_t _last_ntp_stamp_ms = 0; + uint64_t _last_ntp_stamp_us = 0; }; }//namespace mediakit From db8e9182ccc70dc5a906a0dc14ac04ecf3aad167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Mon, 26 Jun 2023 10:04:16 +0800 Subject: [PATCH 59/66] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=E6=8C=87=E5=AE=9AH264=20rtp=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E4=BC=98=E5=85=88=E9=87=87=E7=94=A8stap-a?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=20(#2582)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit H264 rtp打包模式是否采用stap-a模式(为了在老版本浏览器上兼容webrtc)还是采用Single NAL unit packet per H.264 模式 有些老的rtsp设备不支持stap-a rtp,设置此配置为0可提高兼容性 --- conf/config.ini | 3 +++ src/Common/config.cpp | 4 ++-- src/Common/config.h | 2 ++ src/Extension/H264Rtp.cpp | 24 +++++++++++++++++++----- src/Extension/H264Rtp.h | 9 ++++++--- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 348b134d..0abe303a 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -281,6 +281,9 @@ videoMtuSize=1400 rtpMaxSize=10 # rtp 打包时,低延迟开关,默认关闭(为0),h264存在一帧多个slice(NAL)的情况,在这种情况下,如果开启可能会导致画面花屏 lowLatency=0 +# H264 rtp打包模式是否采用stap-a模式(为了在老版本浏览器上兼容webrtc)还是采用Single NAL unit packet per H.264 模式 +# 有些老的rtsp设备不支持stap-a rtp,设置此配置为0可提高兼容性 +h264_stap_a=1 [rtp_proxy] #导出调试数据(包括rtp/ps/h264)至该目录,置空则关闭数据导出 diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 5783b54c..3d79d894 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -244,15 +244,15 @@ const string kVideoMtuSize = RTP_FIELD "videoMtuSize"; const string kAudioMtuSize = RTP_FIELD "audioMtuSize"; // rtp包最大长度限制,单位是KB const string kRtpMaxSize = RTP_FIELD "rtpMaxSize"; - const string kLowLatency = RTP_FIELD "lowLatency"; +const string kH264StapA = RTP_FIELD "h264_stap_a"; static onceToken token([]() { mINI::Instance()[kVideoMtuSize] = 1400; mINI::Instance()[kAudioMtuSize] = 600; mINI::Instance()[kRtpMaxSize] = 10; mINI::Instance()[kLowLatency] = 0; - + mINI::Instance()[kH264StapA] = 1; }); } // namespace Rtp diff --git a/src/Common/config.h b/src/Common/config.h index 74ea5d71..fb998cd5 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -298,6 +298,8 @@ extern const std::string kAudioMtuSize; extern const std::string kRtpMaxSize; // rtp 打包时,低延迟开关,默认关闭(为0),h264存在一帧多个slice(NAL)的情况,在这种情况下,如果开启可能会导致画面花屏 extern const std::string kLowLatency; +//H264 rtp打包模式是否采用stap-a模式(为了在老版本浏览器上兼容webrtc)还是采用Single NAL unit packet per H.264 模式 +extern const std::string kH264StapA; } // namespace Rtp ////////////组播配置/////////// diff --git a/src/Extension/H264Rtp.cpp b/src/Extension/H264Rtp.cpp index f12218a4..eae8edd1 100644 --- a/src/Extension/H264Rtp.cpp +++ b/src/Extension/H264Rtp.cpp @@ -209,8 +209,8 @@ void H264RtpEncoder::insertConfigFrame(uint64_t pts){ void H264RtpEncoder::packRtp(const char *ptr, size_t len, uint64_t pts, bool is_mark, bool gop_pos){ if (len + 3 <= getMaxSize()) { - //STAP-A模式打包小于MTU - packRtpStapA(ptr, len, pts, is_mark, gop_pos); + // 采用STAP-A/Single NAL unit packet per H.264 模式 + packRtpSmallFrame(ptr, len, pts, is_mark, gop_pos); } else { //STAP-A模式打包会大于MTU,所以采用FU-A模式 packRtpFu(ptr, len, pts, is_mark, gop_pos); @@ -220,8 +220,8 @@ void H264RtpEncoder::packRtp(const char *ptr, size_t len, uint64_t pts, bool is_ void H264RtpEncoder::packRtpFu(const char *ptr, size_t len, uint64_t pts, bool is_mark, bool gop_pos){ auto packet_size = getMaxSize() - 2; if (len <= packet_size + 1) { - //小于FU-A打包最小字节长度要求,采用STAP-A模式 - packRtpStapA(ptr, len, pts, is_mark, gop_pos); + // 小于FU-A打包最小字节长度要求,采用STAP-A/Single NAL unit packet per H.264 模式 + packRtpSmallFrame(ptr, len, pts, is_mark, gop_pos); return; } @@ -257,8 +257,17 @@ void H264RtpEncoder::packRtpFu(const char *ptr, size_t len, uint64_t pts, bool i } } +void H264RtpEncoder::packRtpSmallFrame(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos) { + GET_CONFIG(bool, h264_stap_a, Rtp::kH264StapA); + if (h264_stap_a) { + packRtpStapA(data, len, pts, is_mark, gop_pos); + } else { + packRtpSingleNalu(data, len, pts, is_mark, gop_pos); + } +} + void H264RtpEncoder::packRtpStapA(const char *ptr, size_t len, uint64_t pts, bool is_mark, bool gop_pos){ - //如果帧长度不超过mtu,为了兼容性 webrtc,采用STAP-A模式打包 + // 如果帧长度不超过mtu,为了兼容性 webrtc,采用STAP-A模式打包 auto rtp = makeRtp(getTrackType(), nullptr, len + 3, is_mark, pts); uint8_t *payload = rtp->getPayload(); //STAP-A @@ -270,6 +279,11 @@ void H264RtpEncoder::packRtpStapA(const char *ptr, size_t len, uint64_t pts, boo RtpCodec::inputRtp(rtp, gop_pos); } +void H264RtpEncoder::packRtpSingleNalu(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos) { + // Single NAL unit packet per H.264 模式 + RtpCodec::inputRtp(makeRtp(getTrackType(), data, len, is_mark, pts), gop_pos); +} + bool H264RtpEncoder::inputFrame(const Frame::Ptr &frame) { auto ptr = frame->data() + frame->prefixSize(); switch (H264_TYPE(ptr[0])) { diff --git a/src/Extension/H264Rtp.h b/src/Extension/H264Rtp.h index 77bca5d0..31200cb8 100644 --- a/src/Extension/H264Rtp.h +++ b/src/Extension/H264Rtp.h @@ -28,7 +28,7 @@ public: using Ptr = std::shared_ptr; H264RtpDecoder(); - ~H264RtpDecoder() {} + ~H264RtpDecoder() override = default; /** * 输入264 rtp包 @@ -77,9 +77,10 @@ public: uint32_t sample_rate = 90000, uint8_t pt = 96, uint8_t interleaved = TrackVideo * 2); - ~H264RtpEncoder() {} - /** + ~H264RtpEncoder() override = default; + + /** * 输入264帧 * @param frame 帧数据,必须 */ @@ -96,6 +97,8 @@ private: void packRtp(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos); void packRtpFu(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos); void packRtpStapA(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos); + void packRtpSingleNalu(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos); + void packRtpSmallFrame(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos); private: Frame::Ptr _sps; From 06742f9d255e2f5c27058281ac0cc7e318bcaf8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Mon, 26 Jun 2023 10:49:25 +0800 Subject: [PATCH 60/66] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=AE=BE=E7=BD=AE=E5=90=AF=E5=8A=A8=E5=8F=82?= =?UTF-8?q?=E6=95=B0=20(#2581)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.新增日志文件保存路径启动参数:log-dir 2.新增日志文件最大切片个数启动参数:log-slice 3.新增日志文件最大切片大小启动参数:log-size --- server/main.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/server/main.cpp b/server/main.cpp index 750560ba..33696405 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -179,6 +179,29 @@ public: throw ExitException(); }); #endif + (*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/ + "log-slice",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/ + Option::ArgRequired,/*该选项后面必须跟值*/ + "100",/*该选项默认值*/ + true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/ + "最大保存日志切片个数",/*该选项说明文字*/ + nullptr); + + (*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/ + "log-size",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/ + Option::ArgRequired,/*该选项后面必须跟值*/ + "256",/*该选项默认值*/ + true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/ + "单个日志切片最大容量,单位MB",/*该选项说明文字*/ + nullptr); + + (*_parser) << Option(0,/*该选项简称,如果是\x00则说明无简称*/ + "log-dir",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/ + Option::ArgRequired,/*该选项后面必须跟值*/ + (exeDir() + "log/").data(),/*该选项默认值*/ + true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/ + "日志保存文件夹路径",/*该选项说明文字*/ + nullptr); } ~CMD_main() override{} @@ -213,9 +236,11 @@ int start_main(int argc,char *argv[]) { //设置日志 Logger::Instance().add(std::make_shared("ConsoleChannel", logLevel)); #if !defined(ANDROID) - auto fileChannel = std::make_shared("FileChannel", exeDir() + "log/", logLevel); + auto fileChannel = std::make_shared("FileChannel", cmd_main["log-dir"], logLevel); // 日志最多保存天数 fileChannel->setMaxDay(cmd_main["max_day"]); + fileChannel->setFileMaxCount(cmd_main["log-slice"]); + fileChannel->setFileMaxSize(1024 * 1024 * cmd_main["log-size"].as()); Logger::Instance().add(fileChannel); #endif // !defined(ANDROID) From 6cd9e15cb8f90d24b42368a8fc521eca792d4589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Mon, 26 Jun 2023 11:04:52 +0800 Subject: [PATCH 61/66] =?UTF-8?q?=E4=BC=98=E5=8C=96FFmpeg=E6=88=AA?= =?UTF-8?q?=E5=9B=BE=E5=91=BD=E4=BB=A4=E6=A8=A1=E6=9D=BF=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E6=88=AA=E5=9B=BE=E6=88=90=E5=8A=9F=E7=8E=87=20(#2583?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -t 0.001参数替换为-frames:v 1,只截图一帧 --- conf/config.ini | 2 +- server/FFmpegSource.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 0abe303a..5d1651ed 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -22,7 +22,7 @@ bin=/usr/bin/ffmpeg #FFmpeg拉流再推流的命令模板,通过该模板可以设置再编码的一些参数 cmd=%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s #FFmpeg生成截图的命令,可以通过修改该配置改变截图分辨率或质量 -snap=%s -i %s -y -f mjpeg -t 0.001 %s +snap=%s -i %s -y -f mjpeg -frames:v 1 %s #FFmpeg日志的路径,如果置空则不生成FFmpeg日志 #可以为相对(相对于本可执行程序目录)或绝对路径 log=./ffmpeg/ffmpeg.log diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index 564dca46..fa9044ed 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -39,7 +39,7 @@ onceToken token([]() { //ffmpeg日志保存路径 mINI::Instance()[kLog] = "./ffmpeg/ffmpeg.log"; mINI::Instance()[kCmd] = "%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s"; - mINI::Instance()[kSnap] = "%s -i %s -y -f mjpeg -t 0.001 %s"; + mINI::Instance()[kSnap] = "%s -i %s -y -f mjpeg -frames:v 1 %s"; mINI::Instance()[kRestartSec] = 0; }); } From c530f281c3aceddae29bdda0ff78af74433dad51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=A6=82=E6=B4=AA?= Date: Tue, 27 Jun 2023 10:51:31 +0800 Subject: [PATCH 62/66] =?UTF-8?q?=E4=BC=98=E5=8C=96CMakeList,=20=E5=AE=8C?= =?UTF-8?q?=E5=96=84iOS=E4=B8=8B=E7=BC=96=E8=AF=91=E9=85=8D=E7=BD=AE=20(#2?= =?UTF-8?q?578)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、调整 json 的源码引入,外部可以引入 json/json.h 使用; 2、针对 iOS 工程调整 cmake 配置,解决 iOS 环境下mk_util.h 找不到问题;同时在 iOS 工程中引入 MediaServer 静态库; --- 3rdpart/CMakeLists.txt | 6 +++++- CMakeLists.txt | 10 +++++----- api/CMakeLists.txt | 14 +++++++------- server/CMakeLists.txt | 8 +++++++- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/3rdpart/CMakeLists.txt b/3rdpart/CMakeLists.txt index 44e3a551..07d5d388 100644 --- a/3rdpart/CMakeLists.txt +++ b/3rdpart/CMakeLists.txt @@ -24,7 +24,11 @@ ############################################################################## # jsoncpp -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/src/lib_json JSONCPP_SRC_LIST) +file(GLOB JSONCPP_SRC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/include/json/*.h + ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/src/lib_json/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/src/lib_json/*.h) + add_library(jsoncpp STATIC ${JSONCPP_SRC_LIST}) target_compile_options(jsoncpp PRIVATE ${COMPILE_OPTIONS_DEFAULT}) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1fb9bb8..66689d9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -449,11 +449,6 @@ if(ENABLE_API) add_subdirectory(api) endif() -# IOS 不编译可执行程序 -if(IOS) - return() -endif() - ############################################################################## if(ENABLE_PLAYER AND ENABLE_FFMPEG) @@ -470,6 +465,11 @@ if(ENABLE_SERVER_LIB) set(MK_LINK_LIBRARIES ${MK_LINK_LIBRARIES} PARENT_SCOPE) endif() +# IOS 不编译可执行程序 +if(IOS) + return() +endif() + #cpp测试demo程序 if (ENABLE_TESTS) add_subdirectory(tests) diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index 851bb3c5..102a03a6 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -30,13 +30,6 @@ file(GLOB API_SRC_LIST set(LINK_LIBRARIES ${MK_LINK_LIBRARIES}) -if(IOS) - add_library(mk_api STATIC ${API_SRC_LIST}) - target_link_libraries(mk_api - PRIVATE ${LINK_LIBRARIES}) - return() -endif () - set(COMPILE_DEFINITIONS ${MK_COMPILE_DEFINITIONS}) if (MSVC) @@ -46,6 +39,8 @@ endif () if(ENABLE_API_STATIC_LIB) add_library(mk_api STATIC ${API_SRC_LIST}) list(APPEND COMPILE_DEFINITIONS MediaKitApi_STATIC) +elseif(IOS) + add_library(mk_api STATIC ${API_SRC_LIST}) else() add_library(mk_api SHARED ${API_SRC_LIST}) endif() @@ -82,6 +77,11 @@ install(TARGETS mk_api LIBRARY DESTINATION ${INSTALL_PATH_LIB} RUNTIME DESTINATION ${INSTALL_PATH_RUNTIME}) +# IOS 跳过测试代码 +if(IOS) + return() +endif() + if (ENABLE_TESTS) add_subdirectory(tests) endif() diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index f0dea82b..748cb48a 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -38,7 +38,13 @@ if(ENABLE_SERVER_LIB) return() endif() -add_executable(MediaServer ${MediaServer_SRC_LIST}) +# IOS 不编译可执行程序,只做依赖库 +if(IOS) + add_library(MediaServer STATIC ${MediaServer_SRC_LIST}) +else() + add_executable(MediaServer ${MediaServer_SRC_LIST}) +endif() + target_compile_definitions(MediaServer PRIVATE ${COMPILE_DEFINITIONS}) target_compile_options(MediaServer From 2771bc15a5c6f5f2bec265b2d0d0957eab52c229 Mon Sep 17 00:00:00 2001 From: pedoc Date: Wed, 28 Jun 2023 11:36:41 +0800 Subject: [PATCH 63/66] =?UTF-8?q?=E6=B7=BB=E5=8A=A0on=5Fserver=5Fexited=20?= =?UTF-8?q?webhook=E4=BA=8B=E4=BB=B6,=E5=BD=93=E7=A8=8B=E5=BA=8F=E6=AD=A3?= =?UTF-8?q?=E5=B8=B8=E9=80=80=E5=87=BA=E6=97=B6=E8=A7=A6=E5=8F=91=20(#2591?= =?UTF-8?q?=20#2585)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.ini | 2 ++ server/WebHook.cpp | 18 ++++++++++++++++++ server/WebHook.h | 1 + server/main.cpp | 6 ++++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 5d1651ed..bd72cac6 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -168,6 +168,8 @@ on_stream_none_reader=https://127.0.0.1/index/hook/on_stream_none_reader on_stream_not_found=https://127.0.0.1/index/hook/on_stream_not_found #服务器启动报告,可以用于服务器的崩溃重启事件监听 on_server_started=https://127.0.0.1/index/hook/on_server_started +#服务器退出报告,当服务器正常退出时触发 +on_server_exited=https://127.0.0.1/index/hook/on_server_exited #server保活上报 on_server_keepalive=https://127.0.0.1/index/hook/on_server_keepalive #发送rtp(startSendRtp)被动关闭时回调 diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 551d9c1d..424b8d1a 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -44,6 +44,7 @@ const string kOnShellLogin = HOOK_FIELD "on_shell_login"; const string kOnStreamNoneReader = HOOK_FIELD "on_stream_none_reader"; const string kOnHttpAccess = HOOK_FIELD "on_http_access"; const string kOnServerStarted = HOOK_FIELD "on_server_started"; +const string kOnServerExited = HOOK_FIELD "on_server_exited"; const string kOnServerKeepalive = HOOK_FIELD "on_server_keepalive"; const string kOnSendRtpStopped = HOOK_FIELD "on_send_rtp_stopped"; const string kOnRtpServerTimeout = HOOK_FIELD "on_rtp_server_timeout"; @@ -69,6 +70,7 @@ static onceToken token([]() { mINI::Instance()[kOnStreamNoneReader] = ""; mINI::Instance()[kOnHttpAccess] = ""; mINI::Instance()[kOnServerStarted] = ""; + mINI::Instance()[kOnServerExited] = ""; mINI::Instance()[kOnServerKeepalive] = ""; mINI::Instance()[kOnSendRtpStopped] = ""; mINI::Instance()[kOnRtpServerTimeout] = ""; @@ -238,6 +240,18 @@ static void reportServerStarted() { do_http_hook(hook_server_started, body, nullptr); } +static void reportServerExited() { + GET_CONFIG(bool, hook_enable, Hook::kEnable); + GET_CONFIG(string, hook_server_exited, Hook::kOnServerExited); + if (!hook_enable || hook_server_exited.empty()) { + return; + } + + const ArgsType body; + // 执行hook + do_http_hook(hook_server_exited, body, nullptr); +} + // 服务器定时保活定时器 static Timer::Ptr g_keepalive_timer; static void reportServerKeepalive() { @@ -668,3 +682,7 @@ void unInstallWebHook() { g_keepalive_timer.reset(); NoticeCenter::Instance().delListener(&web_hook_tag); } + +void onProcessExited() { + reportServerExited(); +} \ No newline at end of file diff --git a/server/WebHook.h b/server/WebHook.h index ea99f736..c75d05c3 100755 --- a/server/WebHook.h +++ b/server/WebHook.h @@ -31,6 +31,7 @@ extern const std::string kTimeoutSec; void installWebHook(); void unInstallWebHook(); +void onProcessExited(); /** * 触发http hook请求 * @param url 请求地址 diff --git a/server/main.cpp b/server/main.cpp index 33696405..c43a9977 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -409,9 +409,9 @@ int start_main(int argc,char *argv[]) { static semaphore sem; signal(SIGINT, [](int) { InfoL << "SIGINT:exit"; - signal(SIGINT, SIG_IGN);// 设置退出信号 + signal(SIGINT, SIG_IGN); // 设置退出信号 sem.post(); - });// 设置退出信号 + }); // 设置退出信号 #if !defined(_WIN32) signal(SIGHUP, [](int) { mediakit::loadIniConfig(g_ini_file.data()); }); @@ -420,6 +420,8 @@ int start_main(int argc,char *argv[]) { } unInstallWebApi(); unInstallWebHook(); + onProcessExited(); + //休眠1秒再退出,防止资源释放顺序错误 InfoL << "程序退出中,请等待..."; sleep(1); From 2ad44bc77355ee753698f570dc9ddf0e35be665d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Wed, 28 Jun 2023 14:55:39 +0800 Subject: [PATCH 64/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=8F=82=E6=95=B0log-size=E5=8D=95=E4=BD=8D=E9=94=99=E8=AF=AFb?= =?UTF-8?q?ug=20(#2598)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FileChannel内部已经把做了转换为MB操作,外面无须再乘以1024x1024 --- server/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/main.cpp b/server/main.cpp index c43a9977..d09bf5d5 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -240,7 +240,7 @@ int start_main(int argc,char *argv[]) { // 日志最多保存天数 fileChannel->setMaxDay(cmd_main["max_day"]); fileChannel->setFileMaxCount(cmd_main["log-slice"]); - fileChannel->setFileMaxSize(1024 * 1024 * cmd_main["log-size"].as()); + fileChannel->setFileMaxSize(cmd_main["log-size"]); Logger::Instance().add(fileChannel); #endif // !defined(ANDROID) From d5bf99a27b030eef86c0d7a6e0a928443fb7f72c Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Wed, 28 Jun 2023 14:56:06 +0800 Subject: [PATCH 65/66] ts or ps muxer not support mjpeg video codec ignore it (#2593) --- src/Extension/Frame.h | 2 +- src/Record/MPEG.cpp | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index 63546596..3609ff41 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -40,7 +40,7 @@ typedef enum { XX(CodecVP8, TrackVideo, 7, "VP8", PSI_STREAM_VP8) \ XX(CodecVP9, TrackVideo, 8, "VP9", PSI_STREAM_VP9) \ XX(CodecAV1, TrackVideo, 9, "AV1", PSI_STREAM_AV1) \ - XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_JPEG_2000) + XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_RESERVED) typedef enum { CodecInvalid = -1, diff --git a/src/Record/MPEG.cpp b/src/Record/MPEG.cpp index 985cf98f..153fd796 100644 --- a/src/Record/MPEG.cpp +++ b/src/Record/MPEG.cpp @@ -30,19 +30,18 @@ MpegMuxer::~MpegMuxer() { releaseContext(); } -#define XX(name, type, value, str, mpeg_id) \ - case name : { \ - if (mpeg_id == PSI_STREAM_RESERVED) { \ - break; \ - } \ - _codec_to_trackid[track->getCodecId()] = mpeg_muxer_add_stream((::mpeg_muxer_t *)_context, mpeg_id, nullptr, 0); \ - return true; \ +#define XX(name, type, value, str, mpeg_id) \ + case name: { \ + if (mpeg_id == PSI_STREAM_RESERVED) { \ + break; \ + } \ + if (track->getTrackType() == TrackVideo) { \ + _have_video = true; \ + } \ + _codec_to_trackid[track->getCodecId()] = mpeg_muxer_add_stream((::mpeg_muxer_t *)_context, mpeg_id, nullptr, 0); \ + return true; \ } - bool MpegMuxer::addTrack(const Track::Ptr &track) { - if (track->getTrackType() == TrackVideo) { - _have_video = true; - } switch (track->getCodecId()) { CODEC_MAP(XX) default: break; @@ -85,6 +84,11 @@ bool MpegMuxer::inputFrame(const Frame::Ptr &frame) { //没有视频时,才以音频时间戳为TS的时间戳 _timestamp = frame->dts(); } + + if(frame->getTrackType() == TrackType::TrackVideo){ + _key_pos = frame->keyFrame(); + _timestamp = frame->dts(); + } _max_cache_size = 512 + 1.2 * frame->size(); mpeg_muxer_input((::mpeg_muxer_t *)_context, track_id, frame->keyFrame() ? 0x0001 : 0, frame->pts() * 90LL, frame->dts() * 90LL, frame->data(), frame->size()); flushCache(); From 6aa4b741a3595d61e3832979b00af26a6ba06b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Wed, 28 Jun 2023 14:56:24 +0800 Subject: [PATCH 66/66] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=9A=E7=94=A8rtp?= =?UTF-8?q?=E8=A7=A3=E7=A0=81=E5=99=A8=E8=BE=93=E5=85=A5=E5=B0=8F=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=E5=A2=9E=E9=87=8F=E6=B5=81=E5=9C=A8=E4=B8=A2?= =?UTF-8?q?=E5=8C=85=E6=97=B6=E4=B8=8D=E5=86=8D=E8=BE=93=E5=87=BA=E5=B8=A7?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84bug=20(#2589)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果rtp时间戳增量很小,在rtp时间戳在转换单位为毫秒时无变化, 从而被认为是上一帧数据,由于处于丢包状态,此rtp数据将被一直丢弃。 --- src/Extension/CommonRtp.cpp | 7 ++++--- src/Extension/CommonRtp.h | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Extension/CommonRtp.cpp b/src/Extension/CommonRtp.cpp index 9ba9b682..68767a6f 100644 --- a/src/Extension/CommonRtp.cpp +++ b/src/Extension/CommonRtp.cpp @@ -34,10 +34,10 @@ bool CommonRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool){ return false; } auto payload = rtp->getPayload(); - auto stamp = rtp->getStampMS(); + auto stamp = rtp->getStamp(); auto seq = rtp->getSeq(); - if (_frame->_dts != stamp || _frame->_buffer.size() > _max_frame_size) { + if (_last_stamp != stamp || _frame->_buffer.size() > _max_frame_size) { //时间戳发生变化或者缓存超过MAX_FRAME_SIZE,则清空上帧数据 if (!_frame->_buffer.empty()) { //有有效帧,则输出 @@ -46,7 +46,8 @@ bool CommonRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool){ //新的一帧数据 obtainFrame(); - _frame->_dts = stamp; + _frame->_dts = rtp->getStampMS(); + _last_stamp = stamp; _drop_flag = false; } else if (_last_seq != 0 && (uint16_t)(_last_seq + 1) != seq) { //时间戳未发生变化,但是seq却不连续,说明中间rtp丢包了,那么整帧应该废弃 diff --git a/src/Extension/CommonRtp.h b/src/Extension/CommonRtp.h index 7df29af8..6553207e 100644 --- a/src/Extension/CommonRtp.h +++ b/src/Extension/CommonRtp.h @@ -50,6 +50,7 @@ private: private: bool _drop_flag = false; uint16_t _last_seq = 0; + uint64_t _last_stamp = 0; size_t _max_frame_size; CodecId _codec; FrameImp::Ptr _frame;