diff --git a/example/main.c b/example/main.c index d0610ff..36590ba 100644 --- a/example/main.c +++ b/example/main.c @@ -1,4 +1,4 @@ -#include "../include/rtmp_capi.h" +#include "../include/rtmp_capi.hpp" #include #include @@ -27,10 +27,12 @@ int main() rtmp_server_set_on_publish(server, on_publish_cb, NULL); rtmp_server_set_on_audio_data(server, on_audio_cb, NULL); rtmp_server_enable_gop_cache(server, true); - if (rtmp_server_start(server)) + bool isRunning = false; + if (rtmp_server_start(server, &isRunning)) { - printf("RTMP Server started on port 1935. Press Ctrl+C to stop.\n"); - sleep(300); // 5 min + while (isRunning) { + sleep(1); // Block main Thread + } } else { diff --git a/include/rtmp_capi.h b/include/rtmp_capi.hpp similarity index 98% rename from include/rtmp_capi.h rename to include/rtmp_capi.hpp index ce7635a..f797529 100644 --- a/include/rtmp_capi.h +++ b/include/rtmp_capi.hpp @@ -48,7 +48,7 @@ typedef bool (*RtmpAuthCallback)(const char *app, const char *stream_key, // Create and destroy RtmpServerHandle rtmp_server_create(int port); void rtmp_server_destroy(RtmpServerHandle handle); -bool rtmp_server_start(RtmpServerHandle handle); +bool rtmp_server_start(RtmpServerHandle handle, bool* isRunning); void rtmp_server_stop(RtmpServerHandle handle); bool rtmp_server_is_running(RtmpServerHandle handle); diff --git a/include/rtmp_server.h b/include/rtmp_server.hpp similarity index 99% rename from include/rtmp_server.h rename to include/rtmp_server.hpp index 21809ba..6b8db95 100644 --- a/include/rtmp_server.h +++ b/include/rtmp_server.hpp @@ -340,7 +340,7 @@ public: RTMPServer(int port = 1935); ~RTMPServer(); - bool start(); + bool start(bool& isRunning); void stop(); bool isRunning() const { return running; } diff --git a/src/rtmp_capi.cpp b/src/rtmp_capi.cpp index a749bd9..164f678 100644 --- a/src/rtmp_capi.cpp +++ b/src/rtmp_capi.cpp @@ -1,5 +1,5 @@ -#include "../include/rtmp_capi.h" -#include "../include/rtmp_server.h" +#include "../include/rtmp_capi.hpp" +#include "../include/rtmp_server.hpp" #include #include @@ -109,12 +109,15 @@ void rtmp_server_destroy(RtmpServerHandle handle) { delete impl->server; delete impl; } -bool rtmp_server_start(RtmpServerHandle handle) { - if (!handle) +bool rtmp_server_start(RtmpServerHandle handle, bool *isRunning) { + if (!handle || !isRunning) return false; + Impl *impl = static_cast(handle); - return impl->server->start(); + bool result = impl->server->start(*isRunning); + return result; } + void rtmp_server_stop(RtmpServerHandle handle) { if (!handle) return; diff --git a/src/rtmp_server.cpp b/src/rtmp_server.cpp index 759fa8c..9a95e50 100644 --- a/src/rtmp_server.cpp +++ b/src/rtmp_server.cpp @@ -1,4 +1,4 @@ -#include "../include/rtmp_server.h" +#include "../include/rtmp_server.hpp" #include #include #include @@ -1007,7 +1007,7 @@ RTMPServer::RTMPServer(int port) : port(port), server_fd(-1), running(false) {} RTMPServer::~RTMPServer() { stop(); } -bool RTMPServer::start() { +bool RTMPServer::start(bool& isRunning) { server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd < 0) { LOG_ERROR("Failed to create socket"); @@ -1040,6 +1040,7 @@ bool RTMPServer::start() { } timeout_thread = std::thread(&RTMPServer::timeoutCheckRoutine, this); LOG_INFO("RTMP Server started on port " + std::to_string(port)); + isRunning = true; return true; } @@ -1436,10 +1437,10 @@ bool RTMPServer::checkConnectionLimits(const std::string &app, bool is_publisher) const { if (is_publisher) { int current = countPublishers(app, stream_key); - return current < max_publishers_per_stream; + return current >= max_publishers_per_stream; } else { int current = countPlayers(app, stream_key); - return current < max_players_per_stream; + return current >= max_players_per_stream; } }