Skip to content

Instantly share code, notes, and snippets.

@jeffskinnerbox
Created March 17, 2018 02:47
Show Gist options
  • Save jeffskinnerbox/df14b4438d4c0b8a99a2553aa4735bd9 to your computer and use it in GitHub Desktop.
Save jeffskinnerbox/df14b4438d4c0b8a99a2553aa4735bd9 to your computer and use it in GitHub Desktop.
Millisecond counter used to to help measure the latency in real-time video streaming
#!/usr/bin/python3
'''
Here is a simple Python script that will output the current time in milliseconds
to help measure the latency. Using this script, you encode and decode the
stream in two different terminal windows but on the same your desktop. Making a
screenshot of the desktop and it gives you the total system latency.
This may not be totally accurate because a snapshot of the desktop might occur
just before a new image is rendered by the X Window System, but should not be
too far off.
'''
import os
import sys
import time
import signal
# ANSI escape sequences for printing
BOLD = '\033[1m'
NORMAL = '\033[0m'
RED = '\033[91m'
def signal_handler(signal, frame):
print(NORMAL)
os.system('setterm -cursor on')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
stime = int(time.time() * 1000)
# clear terminal and hide the cursor so not to obscure millisecond counter
os.system('clear')
os.system('setterm -cursor off')
print('\n\n' + 'Millisecond Counter:' + BOLD + RED)
while True:
time.sleep(0.001)
print('%s\r' % (int(time.time() * 1000) - stime), end='')
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment