Skip to content

Instantly share code, notes, and snippets.

@jadonk jadonk/stream_client.py
Last active Apr 11, 2018

Embed
What would you like to do?
#!/usr/bin/env python
#
# sudo apt-get update
# sudo apt-get install -y python-pygame python-pil
# sudo ./stream_client.py
#
import urllib2
import pygame
from PIL import Image
from cStringIO import StringIO
import os
import signal
from functools import partial
import time
#os.environ['SDL_VIDEODRIVER']="fbcon"
#os.environ['SDL_NOMOUSE']="1"
#os.environ['SDL_FBDEV']="/dev/fb0"
black = (0, 0, 0)
red = (255, 0, 0)
#disp_size = (1920, 1080)
im_size = (640, 480)
phase = 0
def term_handler(signal, frame):
global phase
print 'Signal: {}'.format(signal)
phase = 2
raise Exception("Terminate")
def alarm_handler(signal, frame):
global phase
print 'Signal: {}'.format(signal)
if (phase == 0):
phase = 1
raise Exception("Timeout")
def main():
global phase
server = "http://bbbwa4-1.local:8090/?action=stream"
signal.signal(signal.SIGALRM, alarm_handler)
signal.signal(signal.SIGTERM, term_handler)
signal.signal(signal.SIGINT, term_handler)
pygame.init()
signal.alarm(1)
screen = pygame.display.set_mode()
signal.alarm(0)
screen.fill(red)
pygame.display.flip()
phase = 0
setAlarm = True
while (phase < 2):
try:
phase = 0
print "Connecting to ", server
signal.alarm(3)
stream = urllib2.urlopen(server)
signal.alarm(0)
bytes = ''
while (phase < 1):
try:
if setAlarm:
signal.alarm(1)
setAlarm = False
bytes += stream.read(1024)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a!=-1 and b!=-1:
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
file_jpgdata = StringIO(jpg)
im = Image.open(file_jpgdata)
surface = pygame.image.fromstring(im.tostring(), im.size, im.mode)
surface2x = pygame.transform.scale2x(surface)
screen.blit(surface2x, (320,60))
pygame.display.flip()
signal.alarm(0)
setAlarm = True
except Exception, exc:
print exc
signal.alarm(0)
setAlarm = True
time.sleep(1)
except Exception, exc:
print exc
signal.alarm(0)
setAlarm = True
time.sleep(3)
if __name__ == '__main__':
main()
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.