mic.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/python3
  2. import time
  3. import threading
  4. import os
  5. import stat
  6. import sys
  7. import gi
  8. import paho.mqtt.client as mqtt
  9. gi.require_version('Gst', '1.0')
  10. gi.require_version('GstNet', '1.0')
  11. from gi.repository import Gst, GstNet
  12. WIDTH = 1280
  13. HEIGHT = 720
  14. SCREEN_WIDTH = 800
  15. SCREEN_HEIGHT = 600
  16. zoom = 1
  17. # def bus_call(bus, msg, *args):
  18. # if msg.type == Gst.MessageType.EOS:
  19. # print("End-of-stream")
  20. # loop.quit()
  21. # return
  22. # elif msg.type == Gst.MessageType.ERROR:
  23. # print("GST ERROR", msg.parse_error())
  24. # loop.quit()
  25. # return
  26. # return True
  27. # The callback for when the client receives a CONNACK response from the server.
  28. def on_connect(client, userdata, flags, rc):
  29. print("Connected with result code "+str(rc))
  30. # Subscribing in on_connect() means that if we lose the connection and
  31. # reconnect then subscriptions will be renewed.
  32. client.subscribe("video/cam1/#")
  33. # The callback for when a PUBLISH message is received from the server.
  34. def on_message(client, userdata, msg):
  35. print(msg.topic+" "+str(msg.payload))
  36. client = mqtt.Client()
  37. client.on_connect = on_connect
  38. client.on_message = on_message
  39. client.connect("192.168.2.123", 1883, 60)
  40. if __name__ == "__main__":
  41. # initialization
  42. Gst.init(None)
  43. SRC="alsasrc device=hw:CARD=CODEC,DEV=0 slave-method=resample do-timestamp=true"
  44. p = """gst-launch-1.0 ${SRC} ! queue !\
  45. audio/x-raw,format=S16LE,channels=2,rate=48000,layout=interleaved !\
  46. audioconvert ! audioresample $DELAY !\
  47. matroskamux streamable=true !\
  48. tcpclientsink host=${HOST} port=10002"""
  49. p = p.format(HOST="192.168.2.120", SRC=SRC)
  50. print(p)
  51. pipeline = Gst.parse_launch(p)
  52. if pipeline is None:
  53. print("Failed to create pipeline")
  54. sys.exit(0)
  55. clock = GstNet.NetClientClock.new('CamVoctoClock', '192.168.2.123', 9998, 0)
  56. # FIXME: add clock support
  57. # print("Waiting for clock sync")
  58. # clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  59. # clock.do_wait(Gst.CLOCK_TIME_NONE)
  60. # print("Clock synced")
  61. pipeline.set_start_time(Gst.CLOCK_TIME_NONE)
  62. if clock is not None:
  63. pass
  64. # pipeline.use_clock(clock)
  65. pipeline.set_state(Gst.State.PLAYING)
  66. t = threading.Thread(target=client.loop_forever, daemon=True)
  67. t.start()
  68. while True:
  69. client.publish("video/heartbeat", "mic1")
  70. time.sleep(10)
  71. pipeline.set_state(Gst.State.NULL)