Sorry i didn't have enough scope in the diff view, and seeing a comment "sleeping 2 seconds" with the next line usleep(1000) is odd.
Perhaps the comment could be moved above before the loop on keep_waiting.

Sleeping 1ms 2000 times look very much like an active waiter, but i guess gstreamer doe not have any other method for doing it.