Pārlūkot izejas kodu

add mic python script

David 8 gadi atpakaļ
vecāks
revīzija
5068b8d061
1 mainītis faili ar 89 papildinājumiem un 0 dzēšanām
  1. 89 0
      mic1/mic.py

+ 89 - 0
mic1/mic.py

@@ -0,0 +1,89 @@
+#!/usr/bin/python3
+import time
+import threading
+import os
+import stat
+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/cam1/#")
+
+
+# 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)
+
+    SRC="alsasrc device=hw:CARD=CODEC,DEV=0 slave-method=resample do-timestamp=true"
+    p = """gst-launch-1.0 ${SRC} ! queue !\
+        audio/x-raw,format=S16LE,channels=2,rate=48000,layout=interleaved !\
+        audioconvert ! audioresample $DELAY !\
+        matroskamux streamable=true !\
+        tcpclientsink host=${HOST} port=10002"""
+    p = p.format(HOST="192.168.2.120", SRC=SRC)
+    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", "mic1")
+        time.sleep(10)
+
+    pipeline.set_state(Gst.State.NULL)