-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.py
executable file
·77 lines (54 loc) · 1.77 KB
/
sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/env python3
from threading import Thread
import gi
import sys
import os
from time import sleep
gi.require_version("Gst", "1.0")
from gi.repository import Gst, GLib
Gst.init(sys.argv[1:])
def isVideo(element):
return True if element.startswith("video") else False
def getCamsAndPipes():
cams = filter(isVideo, os.listdir("/dev/")) # gets list of video devices if isVideo finds them in os list
pipelines = []
for i, cam in enumerate(cams):
pipStr = 'v4l2src device="/dev/{}" ! image/jpeg,width=640,framerate=15/1,rate=15 ! rtpjpegpay ! udpsink host=192.168.2.255 port=5{}00'.format(cam, i)
pipeline = Gst.parse_launch(pipStr)
pipelines.append(pipeline)
print("PIPELINE : " + pipStr)
for pipeline in pipelines:
if not pipeline:
print("pipeline error")
sys.exit(1)
return pipelines
def cleanPipelins(pipelines):
for i, pipe in enumerate(pipelines):
pipe.set_state(Gst.State.NULL)
print("- pipeline " + str(i) + " resetting to null")
def startPipes(pipelines = getCamsAndPipes()):
for i, pipeline in enumerate(pipelines):
ret = pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
print("pipeline {} couldn't started.".format(i))
else:
print("pipeline {} started.".format(i))
return pipelines
def mainloop():
while True:
sleep(0.01)
main_loop = GLib.MainLoop()
thread = Thread(target=main_loop.run, daemon=True)
thread.start()
pipes = startPipes()
if len(pipes) < 2:
print("Not enough cams present, exiting")
exit(1)
try:
mainloop()
except KeyboardInterrupt:
print("\nexiting...")
finally:
cleanPipelins(pipes)
main_loop.quit()
exit(0)