Remove gstreamer_main_loop().
[toast/stream2beamer.git] / rtmp_test.py
1 # Imports
2 import gi
3
4 gi.require_version('Gst', '1.0')
5 from gi.repository import Gst
6
7 gi.require_version('GstWebRTC', '1.0')
8 from gi.repository import GstWebRTC, GLib
9
10 gi.require_version('GstSdp', '1.0')
11 from gi.repository import GstSdp
12
13 # libgstrtspserver-1.0-dev
14 gi.require_version('GstRtspServer', '1.0')
15 from gi.repository import GstRtspServer
16
17
18 def main():
19     # OS Variables and Requirements
20     gi.require_version('Gst', '1.0')
21     # os.environ["GST_DEBUG"] = "4" # Enable Debug
22
23     # Initialize GStreamer
24     Gst.init(None) # gst-launch-1.0 !
25     pipeline = Gst.Pipeline()
26
27     # Create Video Source (Video Test Source)
28     videosrc = Gst.ElementFactory.make("videotestsrc") # videotestsrc is-live=true !
29     videosrc.set_property('is-live', True)
30     pipeline.add(videosrc)
31
32     # Convert Video (to x264enc?)
33     # videoconvert = Gst.ElementFactory.make('autovideoconvert') # videoconvert
34     videoconvert = Gst.ElementFactory.make('videoconvert') # videoconvert
35     pipeline.add(videoconvert)
36
37     # IDK
38     idk = Gst.ElementFactory.make("x264enc") # x264enc bitrate=1000 tune=zerolatency
39     idk.set_property('bitrate', 1000)
40     idk.set_property('tune', 'zerolatency')
41     pipeline.add(idk)
42
43     # Queue Data
44     #queueRTMP = Gst.ElementFactory.make("queue") # queue
45     #pipeline.add(queueRTMP)
46
47     capsfilter = Gst.ElementFactory.make('capsfilter')
48     capsfilter.set_properties(Gst.Caps.from_string('video/x-h264,stream-format=(string)avc'))
49     pipeline.add(capsfilter)
50
51     # Convert to Mux
52     flvmux = Gst.ElementFactory.make("flvmux", "mux") # flvmux name=mux
53     pipeline.add(flvmux)
54
55     # Stream to RTMP Server
56     rtmpsink = Gst.ElementFactory.make("rtmpsink") # rtmpsink location='rtmp://live.twitch.tv/app/STREAM_KEY_HERE'
57     rtmpsink.set_property("location", 'rtmp://sirius/gregoa')
58     pipeline.add(rtmpsink)
59
60     ok = videosrc.link(videoconvert)
61     assert ok
62     ok = videoconvert.link(idk)
63     assert ok
64     ok = idk.link(capsfilter)
65     assert ok
66     ok = capsfilter.link(flvmux)
67     assert ok
68     #ok = queueRTMP.link(flvmux)
69     assert ok
70     ok = flvmux.link(rtmpsink)
71     assert ok
72
73     pipeline.set_state(Gst.State.PLAYING)
74     loop = GLib.MainLoop()
75     loop.run()
76     # time.sleep(20)
77
78
79 main()