Bladeren bron

initial cam2 script with mqtt supp

David 8 jaren geleden
bovenliggende
commit
308ee3f0dd
1 gewijzigde bestanden met toevoegingen van 86 en 0 verwijderingen
  1. 86 0
      camera2/cam2.py

+ 86 - 0
camera2/cam2.py

@@ -0,0 +1,86 @@
+#!/usr/bin/python3
+import time
+import threading
+import sys
+import gi
+import paho.mqtt.client as mqtt
+gi.require_version('Gst', '1.0')
+gi.require_version('GstNet', '1.0')
+from gi.repository import Gst, GstNet
+
+WIDTH = 1280
+HEIGHT = 720
+
+SCREEN_WIDTH = 800
+SCREEN_HEIGHT = 600
+zoom = 1
+
+
+# def bus_call(bus, msg, *args):
+#     if msg.type == Gst.MessageType.EOS:
+#         print("End-of-stream")
+#         loop.quit()
+#         return
+#     elif msg.type == Gst.MessageType.ERROR:
+#         print("GST ERROR", msg.parse_error())
+#         loop.quit()
+#         return
+#     return True
+
+
+# The callback for when the client receives a CONNACK response from the server.
+def on_connect(client, userdata, flags, rc):
+    print("Connected with result code "+str(rc))
+
+    # Subscribing in on_connect() means that if we lose the connection and
+    # reconnect then subscriptions will be renewed.
+    client.subscribe("video/cam2/#")
+
+
+# The callback for when a PUBLISH message is received from the server.
+def on_message(client, userdata, msg):
+    print(msg.topic+" "+str(msg.payload))
+
+
+client = mqtt.Client()
+client.on_connect = on_connect
+client.on_message = on_message
+
+client.connect("192.168.2.123", 1883, 60)
+
+
+if __name__ == "__main__":
+    # initialization
+    Gst.init(None)
+    WIDTH = 1280
+    HEIGHT = 720
+    p = """rpicamsrc rotation=0 preview=0 bitrate=0 quantisation-parameter=23 sensor-mode=4 keyframe-interval=5 do-timestamp=true !\
+        video/x-h264, width={WIDTH}, height={HEIGHT}, framerate=25/1, profile=high !\
+        h264parse ! matroskamux ! tcpserversink host=0.0.0.0 port=5000 sync-method=next-keyframe"""
+
+    p = p.format(HEIGHT=HEIGHT, WIDTH=WIDTH)
+    print(p)
+    pipeline = Gst.parse_launch(p)
+    if pipeline is None:
+        print("Failed to create pipeline")
+        sys.exit(0)
+
+    clock = GstNet.NetClientClock.new('CamVoctoClock', '192.168.2.123', 9998, 0)
+    # FIXME: add clock support
+    # print("Waiting for clock sync")
+    # clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
+    # clock.do_wait(Gst.CLOCK_TIME_NONE)
+    # print("Clock synced")
+    pipeline.set_start_time(Gst.CLOCK_TIME_NONE)
+    if clock is not None:
+        pass
+        # pipeline.use_clock(clock)
+
+    pipeline.set_state(Gst.State.PLAYING)
+    t = threading.Thread(target=client.loop_forever, daemon=True)
+    t.start()
+    while True:
+        client.publish("video/heartbeat", "cam2")
+        time.sleep(10)
+
+    pipeline.set_state(Gst.State.NULL)