Skip to content

Commit bbe0b60

Browse files
committed
Fix sigint termination
1 parent 04ed4d8 commit bbe0b60

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

samples/kvssink_intermittent_sample.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LOGGER_TAG("com.amazonaws.kinesis.video.gstreamer");
3030
GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
3131
std::atomic<bool> terminated(FALSE);
3232
std::condition_variable cv;
33+
std::mutex cv_mutex;
3334

3435
typedef enum _StreamSource {
3536
TEST_SOURCE,
@@ -46,6 +47,7 @@ typedef struct _CustomData {
4647
} CustomData;
4748

4849
void shutdown_sample () {
50+
std::lock_guard<std::mutex> lock(cv_mutex);
4951
terminated = TRUE;
5052
cv.notify_all();
5153
if (main_loop != NULL) {
@@ -136,8 +138,7 @@ void determine_aws_credentials(GstElement *kvssink, char* streamName) {
136138

137139
// This function handles the intermittent starting and stopping of the stream in a loop.
138140
void stopStartLoop(GstElement *pipeline, GstElement *source) {
139-
std::mutex cv_m;
140-
std::unique_lock<std::mutex> lck(cv_m);
141+
std::unique_lock<std::mutex> lck(cv_mutex);
141142
GstStateChangeReturn gst_state_change_ret;
142143

143144
while (!terminated) {
@@ -304,7 +305,7 @@ int main(int argc, char *argv[]) {
304305
/* kvssink */
305306
kvssink = gst_element_factory_make("kvssink", "kvssink");
306307
if (IS_EMPTY_STRING(stream_name)) {
307-
LOG_INFO("No stream name specified, using default kvssink stream name.")
308+
LOG_INFO("No stream name specified, using default kvssink stream name.");
308309
} else {
309310
g_object_set(G_OBJECT(kvssink), "stream-name", stream_name, NULL);
310311
}
@@ -363,11 +364,15 @@ int main(int argc, char *argv[]) {
363364
// Start the stop/start thread for intermittent streaming.
364365
std::thread stopStartThread(stopStartLoop, pipeline, source);
365366

367+
// Start the timer thread to terminate the sample after a specified duration.
366368
std::thread timerThread([runtime_duration_seconds]() {
367-
std::this_thread::sleep_for(std::chrono::seconds(runtime_duration_seconds));
368-
if (!terminated) {
369-
LOG_INFO("[KVS sample] Reached maximum runtime of " << runtime_duration_seconds << " seconds. Terminating.");
370-
shutdown_sample();
369+
LOG_INFO("[KVS sample] Timer thread started. Will wait for " << runtime_duration_seconds << " seconds or until terminated.");
370+
std::unique_lock<std::mutex> lck(cv_mutex);
371+
if (cv.wait_for(lck, std::chrono::seconds(runtime_duration_seconds)) == std::cv_status::timeout) {
372+
if (!terminated) {
373+
LOG_INFO("[KVS sample] Reached maximum runtime of " << runtime_duration_seconds << " seconds. Terminating.");
374+
shutdown_sample();
375+
}
371376
}
372377
});
373378

0 commit comments

Comments
 (0)