1313#include < nabto/webrtc/util/token_generator.hpp>
1414#include < optional>
1515#include < webrtc_connection/webrtc_connection.hpp>
16+ #include < csignal>
17+ #include < atomic>
18+ #include < condition_variable>
19+ #include < mutex>
1620
1721#include " h264_opus_rtsp_handler.hpp"
1822
@@ -33,12 +37,28 @@ bool parse_options(int argc, char** argv, struct options& opts);
3337struct options defaultOptions ();
3438std::string readKeyFile (std::string path);
3539
40+ // Signal handling
41+ static std::atomic<bool > shutdownRequested (false );
42+ static std::mutex shutdownMutex;
43+ static std::condition_variable shutdownCv;
44+
45+ void signalHandler (int signal) {
46+ if (signal == SIGINT) {
47+ NPLOGI << " Received SIGINT, shutting down..." ;
48+ shutdownRequested = true ;
49+ shutdownCv.notify_all ();
50+ }
51+ }
52+
3653int main (int argc, char ** argv) {
3754 auto opts = defaultOptions ();
3855 if (!parse_options (argc, argv, opts)) {
3956 return 0 ;
4057 }
4158
59+ // Register signal handler for Ctrl-C
60+ std::signal (SIGINT, signalHandler);
61+
4262 static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
4363 nabto::webrtc::util::initLogger (opts.logLevel , &consoleAppender);
4464
@@ -47,6 +67,7 @@ int main(int argc, char** argv) {
4767 &consoleAppender);
4868
4969 NPLOGI << " Connecting to device: " << opts.deviceId ;
70+ NPLOGI << " Press Ctrl-C to shutdown gracefully" ;
5071
5172 nabto::webrtc::SignalingTokenGeneratorPtr jwtPtr =
5273 nabto::webrtc::util::NabtoTokenGenerator::create (
@@ -108,8 +129,17 @@ int main(int argc, char** argv) {
108129 });
109130 device->start ();
110131
111- int n;
112- std::cin >> n;
132+ // Wait for shutdown signal
133+ {
134+ std::unique_lock<std::mutex> lock (shutdownMutex);
135+ shutdownCv.wait (lock, []{ return shutdownRequested.load (); });
136+ }
137+
138+ NPLOGI << " Shutting down device..." ;
139+ device->close ();
140+ NPLOGI << " Device stopped, exiting" ;
141+
142+ return 0 ;
113143}
114144
115145bool parse_options (int argc, char ** argv, struct options & opts) {
0 commit comments