From 41e467ddfe1744e58b4b87a148c097d69aef4d31 Mon Sep 17 00:00:00 2001 From: Exil Productions Date: Sat, 27 Dec 2025 02:37:09 +0100 Subject: [PATCH] Add Cpp Impl --- CMakeLists.txt | 9 ++-- server/{ => c}/main.c | 0 server/cpp/main.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 3 deletions(-) rename server/{ => c}/main.c (100%) create mode 100644 server/cpp/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 965cbb0..40f2c5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,11 @@ set_target_properties(rtmp PROPERTIES PUBLIC_HEADER include/rtmp_capi.h ) -add_executable(rtmp_server server/main.c) -target_link_libraries(rtmp_server rtmp) +add_executable(rtmp_server_c server/c/main.c) +target_link_libraries(rtmp_server_c rtmp) + +add_executable(rtmp_server_cpp server/cpp/main.cpp) +target_link_libraries(rtmp_server_cpp rtmp) install(TARGETS rtmp LIBRARY DESTINATION lib @@ -29,4 +32,4 @@ install(TARGETS rtmp PUBLIC_HEADER DESTINATION include ) -install(TARGETS rtmp_server DESTINATION bin) +install(TARGETS rtmp_server_c rtmp_server_cpp DESTINATION bin) diff --git a/server/main.c b/server/c/main.c similarity index 100% rename from server/main.c rename to server/c/main.c diff --git a/server/cpp/main.cpp b/server/cpp/main.cpp new file mode 100644 index 0000000..dbda0ca --- /dev/null +++ b/server/cpp/main.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../include/rtmp_server.hpp" + +using namespace rtmp; + +// Callbacks +void onConnect(std::shared_ptr session) { + std::cout << "Client connected: " << session->getStreamInfo().client_ip << std::endl; +} + +void onPublish(std::shared_ptr session, const std::string& app, const std::string& key) { + std::cout << "Publish from " << session->getStreamInfo().client_ip << ": " << app << "/" << key << std::endl; +} + +static struct termios g_orig_termios; + +static void restore_terminal() { + tcsetattr(STDIN_FILENO, TCSANOW, &g_orig_termios); +} + +static bool setup_nonblocking_stdin() { + struct termios raw; + int flags; + + if (tcgetattr(STDIN_FILENO, &g_orig_termios) != 0) + return false; + + raw = g_orig_termios; + raw.c_lflag &= ~(ICANON | ECHO); + if (tcsetattr(STDIN_FILENO, TCSANOW, &raw) != 0) + return false; + + flags = fcntl(STDIN_FILENO, F_GETFL, 0); + if (flags < 0) + return false; + + if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0) + return false; + + std::atexit(restore_terminal); + return true; +} + +int main() { + // Set log level + Logger::getInstance().setLevel(LogLevel::INFO); + + // Setup non-blocking stdin + if (!setup_nonblocking_stdin()) { + std::cerr << "Failed to configure terminal input" << std::endl; + return 1; + } + + // Create RTMP server on port 1935 + RTMPServer server(1935); + + // Set callbacks + server.setOnConnect(onConnect); + server.setOnPublish(onPublish); + + // Enable GOP cache + server.enableGOPCache(true); + + bool isRunning = false; + if (!server.start(isRunning)) { + std::cerr << "Failed to start server" << std::endl; + return 1; + } + + std::cout << "RTMP server running. Press 'q' to stop." << std::endl; + + while (isRunning) { + fd_set readfds; + struct timeval tv; + + FD_ZERO(&readfds); + FD_SET(STDIN_FILENO, &readfds); + + tv.tv_sec = 1; + tv.tv_usec = 0; + + int ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv); + if (ret > 0 && FD_ISSET(STDIN_FILENO, &readfds)) { + char ch; + ssize_t n = read(STDIN_FILENO, &ch, 1); + if (n == 1 && (ch == 'q' || ch == 'Q')) { + std::cout << "Shutting down..." << std::endl; + server.stop(); + break; + } + } + } + + return 0; +}