#!/usr/bin/env python3
import sys
from threading import Thread
import time

import gi

gi.require_version("Gst", "1.0")

from gi.repository import Gst, GLib


x=0
Gst.init(sys.argv[1:])
#Gst.debug_set_active(True)
#Gst.debug_set_default_threshold(3)

#main_loop = GLib.MainLoop()
main_loop = GLib.MainLoop.new(None, False)
thread = Thread(target=main_loop.run)
thread.daemon = True
thread.start()
print("thread.start")


# Create the empty pipeline
pipeline = Gst.Pipeline.new("test-pipeline")


# Create the Source
source = Gst.ElementFactory.make("v4l2src", "source")
source.set_property("do-timestamp", 1)
pipeline.add(source)

# Create the Caps1
#caps1 = Gst.Caps.from_string("image/jpeg,width=320,height=240")
#caps1 = Gst.Caps.from_string("image/jpeg,width=640,height=480")
caps1 = Gst.Caps.from_string("image/jpeg,width=1280,height=720")
capsfilter1 = Gst.ElementFactory.make("capsfilter", "capsfilter1")
capsfilter1.set_property("caps", caps1)
pipeline.add(capsfilter1)
source.link(capsfilter1)

# Create the MJPEG decoder
decoder = Gst.ElementFactory.make ("nvv4l2decoder", "decoder")
decoder.set_property("mjpeg", 1)
pipeline.add(decoder)
capsfilter1.link(decoder)

#Create nvvidconv1
nvvidconv1 = Gst.ElementFactory.make ("nvvidconv", "nvvidconv1")
pipeline.add(nvvidconv1)
decoder.link(nvvidconv1)

# Create the Caps3
caps3 = Gst.Caps.from_string("video/x-raw,format=I420")
capsfilter3 = Gst.ElementFactory.make("capsfilter", "capsfilter3")
capsfilter3.set_property("caps", caps3)
pipeline.add(capsfilter3)
nvvidconv1.link(capsfilter3)

# Create the textoverlay
filtery = Gst.ElementFactory.make ("textoverlay", "filtery")
pipeline.add(filtery)
capsfilter3.link(filtery)
filtery.props.font_desc="Sans, 32"
filtery.props.valignment="top"

# #Create nvvidconv2
nvvidconv2 = Gst.ElementFactory.make ("nvvidconv", "nvvidconv2")
pipeline.add(nvvidconv2)
filtery.link(nvvidconv2)

# # Create the Caps4
caps4 = Gst.Caps.from_string("video/x-raw(memory:NVMM), format=(string)I420")
capsfilter4 = Gst.ElementFactory.make("capsfilter", "capsfilter4")
capsfilter4.set_property("caps", caps4)
pipeline.add(capsfilter4)
nvvidconv2.link(capsfilter4)

# Create the encoder
encoder = Gst.ElementFactory.make ("nvv4l2h264enc", "encoder")
encoder.set_property("control-rate", 1)
encoder.set_property("bitrate", 1000000)
encoder.set_property("iframeinterval", 20)
encoder.set_property("insert-sps-pps", 1)
pipeline.add(encoder)
capsfilter4.link(encoder)

# Create the Caps2
caps2 = Gst.Caps.from_string("video/x-h264,stream-format=byte-stream ")
capsfilter2 = Gst.ElementFactory.make("capsfilter", "capsfilter2")
capsfilter2.set_property("caps", caps2)
pipeline.add(capsfilter2)
encoder.link(capsfilter2)

# Create the rtppay
rtppay = Gst.ElementFactory.make ("rtph264pay", "rtppay")
rtppay.set_property("config-interval", 1)
pipeline.add(rtppay)
capsfilter2.link(rtppay)

# Create the sink
sink = Gst.ElementFactory.make("udpsink", "sink")
pipeline.add(sink)
rtppay.link(sink)
sink.props.host="192.168.1.1"
sink.props.port=5000


#Pipeline START
pipeline.set_state(Gst.State.PLAYING)

try:
    while True:
        
        time.sleep(0.001)
        
        filtery.props.text=str(round(time.time() * 1000))
except KeyboardInterrupt:
    pass

pipeline.set_state(Gst.State.NULL)
main_loop.quit()
#thread.exit()
thread.join()
