From 0738c25fb4cdc8bb17be4d913bef6c464ad3c0b1 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Sun, 1 Nov 2020 23:32:40 +0100 Subject: [PATCH] Fix memory leak stream UDP --- Connection.cpp | 8 +++++++- Connection.h | 1 + NetworkSource.cpp | 11 +++++------ NetworkSource.h | 1 + NetworkToolkit.cpp | 16 ++++++++-------- Streamer.cpp | 5 +++++ Streamer.h | 1 + UserInterfaceManager.cpp | 11 +++++------ 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Connection.cpp b/Connection.cpp index 03658c0..798c8e7 100644 --- a/Connection.cpp +++ b/Connection.cpp @@ -22,6 +22,13 @@ Connection::Connection() receiver_ = nullptr; } + +Connection::~Connection() +{ + if (receiver_!=nullptr) + delete receiver_; +} + bool Connection::init() { // add default info for myself @@ -161,7 +168,6 @@ void Connection::ask() p << Connection::manager().connections_[0].port_handshake; p << osc::EndMessage; - // loop infinitely while(true) { diff --git a/Connection.h b/Connection.h index 3ad5296..4a5b654 100644 --- a/Connection.h +++ b/Connection.h @@ -72,6 +72,7 @@ public: static Connection _instance; return _instance; } + ~Connection(); bool init(); void terminate(); diff --git a/NetworkSource.cpp b/NetworkSource.cpp index 4856600..306af63 100644 --- a/NetworkSource.cpp +++ b/NetworkSource.cpp @@ -24,7 +24,6 @@ #endif - // this is called when receiving an answer for streaming request void StreamerResponseListener::ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint ) @@ -34,7 +33,6 @@ void StreamerResponseListener::ProcessMessage( const osc::ReceivedMessage& m, try{ if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_STREAM_OFFER ) == 0 ){ - #ifdef NETWORK_DEBUG Log::Info("Received stream info from %s", sender); #endif @@ -54,7 +52,6 @@ void StreamerResponseListener::ProcessMessage( const osc::ReceivedMessage& m, } else if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_STREAM_REJECT ) == 0 ){ - #ifdef NETWORK_DEBUG Log::Info("Received rejection from %s", sender); #endif @@ -108,7 +105,7 @@ void NetworkStream::connect(const std::string &nameconnection) // Nope, cannot connect to unknown connection if (streamer_index < 0) { - Log::Warning("Cannot connect to %s: please make sure the program is active.", nameconnection.c_str()); + Log::Warning("Cannot connect to %s: please make sure %s is active on this machine.", nameconnection.c_str(), APP_NAME); failed_ = true; return; } @@ -134,7 +131,7 @@ void NetworkStream::connect(const std::string &nameconnection) } } if (receiver_ == nullptr) { - Log::Notify("Cannot establish connection with %s. Please check your network.", nameconnection.c_str()); + Log::Notify("Cannot establish connection with %s. Please check your network.", streamer_.name.c_str()); failed_ = true; return; } @@ -164,6 +161,7 @@ void NetworkStream::connect(const std::string &nameconnection) void NetworkStream::disconnect() { + // receiver should not be active anyway, make sure it is deleted if (receiver_) { delete receiver_; receiver_ = nullptr; @@ -251,7 +249,8 @@ void NetworkStream::update() } } else { - Log::Info("Connection rejected."); + Log::Warning("Connection was rejected by %s.\nMake sure it accepts connection and try again.", streamer_.name.c_str()); + failed_=true; } } } diff --git a/NetworkSource.h b/NetworkSource.h index 57b6721..7022c74 100644 --- a/NetworkSource.h +++ b/NetworkSource.h @@ -30,6 +30,7 @@ class NetworkStream : public Stream public: NetworkStream(); + void connect(const std::string &nameconnection); bool connected() const; void disconnect(); diff --git a/NetworkToolkit.cpp b/NetworkToolkit.cpp index f7881e3..f51f1da 100644 --- a/NetworkToolkit.cpp +++ b/NetworkToolkit.cpp @@ -72,20 +72,20 @@ const char* NetworkToolkit::protocol_name[NetworkToolkit::DEFAULT] = { const std::vector NetworkToolkit::protocol_send_pipeline { - "video/x-raw, format=RGB, framerate=30/1 ! queue max-size-buffers=3 ! shmsink buffer-time=100000 wait-for-connection=true name=sink", - "video/x-raw, format=I420, framerate=30/1 ! queue max-size-buffers=3 ! jpegenc ! rtpjpegpay ! udpsink name=sink", - "video/x-raw, format=I420, framerate=30/1 ! queue max-size-buffers=3 ! x264enc tune=\"zerolatency\" threads=2 ! rtph264pay ! udpsink name=sink", + "video/x-raw, format=RGB, framerate=30/1 ! queue max-size-buffers=10 ! shmsink buffer-time=100000 wait-for-connection=true name=sink", + "video/x-raw, format=I420, framerate=30/1 ! queue max-size-buffers=10 ! jpegenc ! rtpjpegpay ! udpsink name=sink", + "video/x-raw, format=I420, framerate=30/1 ! queue max-size-buffers=10 ! x264enc tune=\"zerolatency\" threads=2 ! rtph264pay ! udpsink name=sink", "video/x-raw, format=I420 ! queue max-size-buffers=3 ! jpegenc ! rtpjpegpay ! rtpstreampay ! tcpserversink name=sink", "video/x-raw, format=I420 ! queue max-size-buffers=3 ! x264enc tune=\"zerolatency\" threads=2 ! rtph264pay ! rtpstreampay ! tcpserversink name=sink" }; const std::vector NetworkToolkit::protocol_receive_pipeline { - "shmsrc socket-path=XXXX ! video/x-raw, format=RGB, framerate=30/1 ! queue max-size-buffers=3", - "udpsrc buffer-size=200000 timeout=200000 port=XXXX ! application/x-rtp,encoding-name=JPEG,payload=26 ! queue max-size-buffers=3 ! rtpjitterbuffer ! rtpjpegdepay ! jpegdec", - "udpsrc buffer-size=200000 timeout=200000 port=XXXX ! application/x-rtp,encoding-name=H264,payload=96,clock-rate=90000 ! queue max-size-buffers=3 ! rtph264depay ! avdec_h264", - "tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=3 ! application/x-rtp-stream,media=video,encoding-name=JPEG,payload=26 ! rtpstreamdepay ! rtpjpegdepay ! jpegdec", - "tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=3 ! application/x-rtp-stream,media=video,encoding-name=H264,payload=96,clock-rate=90000 ! rtpstreamdepay ! rtph264depay ! avdec_h264" + "shmsrc socket-path=XXXX ! video/x-raw, format=RGB, framerate=30/1 ! queue max-size-buffers=10", + "udpsrc buffer-size=200000 port=XXXX ! application/x-rtp,encoding-name=JPEG,payload=26 ! queue max-size-buffers=10 ! rtpjitterbuffer ! rtpjpegdepay ! jpegdec", + "udpsrc buffer-size=200000 port=XXXX ! application/x-rtp,encoding-name=H264,payload=96,clock-rate=90000 ! queue ! rtph264depay ! avdec_h264", + "tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=30 ! application/x-rtp-stream,media=video,encoding-name=JPEG,payload=26 ! rtpstreamdepay ! rtpjpegdepay ! jpegdec", + "tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=30 ! application/x-rtp-stream,media=video,encoding-name=H264,payload=96,clock-rate=90000 ! rtpstreamdepay ! rtph264depay ! avdec_h264" }; std::vector ipstrings; diff --git a/Streamer.cpp b/Streamer.cpp index c735c93..ca69b57 100644 --- a/Streamer.cpp +++ b/Streamer.cpp @@ -86,6 +86,11 @@ Streaming::Streaming() : enabled_(false), session_(nullptr), width_(0), height_( std::thread(wait_for_request_, receiver_).detach(); } +Streaming::~Streaming() +{ + if (receiver_!=nullptr) + delete receiver_; +} bool Streaming::busy() const { diff --git a/Streamer.h b/Streamer.h index e00ba00..62870f5 100644 --- a/Streamer.h +++ b/Streamer.h @@ -38,6 +38,7 @@ public: static Streaming _instance; return _instance; } + ~Streaming(); void enable(bool on); inline bool enabled() const { return enabled_; } diff --git a/UserInterfaceManager.cpp b/UserInterfaceManager.cpp index 7bd9ab5..7b850b2 100644 --- a/UserInterfaceManager.cpp +++ b/UserInterfaceManager.cpp @@ -1151,7 +1151,7 @@ void UserInterface::RenderPreview() // detecting the absence of video recorder but the variable is still not 0: fix this! if (video_recorder_ > 0) video_recorder_ = 0; - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0, 0.05, 0.05, 0.8f)); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0, 0.05, 0.05, 0.9f)); if ( ImGui::MenuItem( ICON_FA_CIRCLE " Record", CTRL_MOD "R") ) { FrameGrabber *fg = new VideoRecorder; video_recorder_ = fg->id(); @@ -1205,7 +1205,7 @@ void UserInterface::RenderPreview() } if (ImGui::BeginMenu("Stream")) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.05, 1.0, 0.05, 0.8f)); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.05, 0.8, 1.0, 0.9f)); if ( ImGui::MenuItem( ICON_FA_SHARE_ALT " Accept connections", NULL, &Settings::application.accept_connections) ) { Streaming::manager().enable(Settings::application.accept_connections); } @@ -1214,8 +1214,7 @@ void UserInterface::RenderPreview() { static char dummy_str[512]; sprintf(dummy_str, "%s", Connection::manager().info().name.c_str()); -// ImGui::SetNextItemWidth(IMGUI_RIGHT_ALIGN); - ImGui::InputText("Name", dummy_str, IM_ARRAYSIZE(dummy_str), ImGuiInputTextFlags_ReadOnly); + ImGui::InputText("My network name", dummy_str, IM_ARRAYSIZE(dummy_str), ImGuiInputTextFlags_ReadOnly); std::vector ls = Streaming::manager().listStreams(); if (ls.size()>0) { @@ -1264,9 +1263,9 @@ void UserInterface::RenderPreview() ImGui::SetCursorScreenPos(ImVec2(draw_pos.x + width - 2.f * r, draw_pos.y + r)); ImGuiToolkit::PushFont(ImGuiToolkit::FONT_LARGE); if ( Streaming::manager().busy()) - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.05, 1.0, 0.05, 0.8f)); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.05, 0.8, 1.0, 0.8f)); else - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.05, 1.0, 0.05, 0.2f)); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.05, 0.8, 1.0, 0.2f)); ImGui::Text(ICON_FA_SHARE_ALT_SQUARE); ImGui::PopStyleColor(1); ImGui::PopFont();