mic.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. not_exited = True
  18. # def bus_call(bus, msg, *args):
  19. # if msg.type == Gst.MessageType.EOS:
  20. # print("End-of-stream")
  21. # loop.quit()
  22. # return
  23. # elif msg.type == Gst.MessageType.ERROR:
  24. # print("GST ERROR", msg.parse_error())
  25. # loop.quit()
  26. # return
  27. # return True
  28. # The callback for when the client receives a CONNACK response from the server.
  29. def on_connect(client, userdata, flags, rc):
  30. print("Connected with result code "+str(rc))
  31. # Subscribing in on_connect() means that if we lose the connection and
  32. # reconnect then subscriptions will be renewed.
  33. client.subscribe("video/mic1/#")
  34. # The callback for when a PUBLISH message is received from the server.
  35. def on_message(client, userdata, msg):
  36. print(msg.topic+" "+str(msg.payload))
  37. global not_exited
  38. if msg.topic == "video/mic1/restart":
  39. not_exited = False
  40. client = mqtt.Client()
  41. client.on_connect = on_connect
  42. client.on_message = on_message
  43. client.connect("192.168.2.123", 1883, 60)
  44. if __name__ == "__main__":
  45. # initialization
  46. Gst.init(None)
  47. SRC="alsasrc device=hw:CARD=CODEC,DEV=0 slave-method=resample do-timestamp=true"
  48. p = """{SRC} ! queue !\
  49. audio/x-raw,format=S16LE,channels=2,rate=48000,layout=interleaved !\
  50. audioconvert ! audioresample !\
  51. matroskamux streamable=true !\
  52. tcpclientsink host={HOST} port=10002"""
  53. p = p.format(HOST="192.168.2.120", SRC=SRC)
  54. print(p)
  55. pipeline = Gst.parse_launch(p)
  56. if pipeline is None:
  57. print("Failed to create pipeline")
  58. sys.exit(0)
  59. clock = GstNet.NetClientClock.new('CamVoctoClock', '192.168.2.123', 9998, 0)
  60. # FIXME: add clock support
  61. # print("Waiting for clock sync")
  62. # clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  63. # clock.do_wait(Gst.CLOCK_TIME_NONE)
  64. # print("Clock synced")
  65. pipeline.set_start_time(Gst.CLOCK_TIME_NONE)
  66. if clock is not None:
  67. pass
  68. # pipeline.use_clock(clock)
  69. pipeline.set_state(Gst.State.PLAYING)
  70. t = threading.Thread(target=client.loop_forever, daemon=True)
  71. t.start()
  72. counter = 0
  73. while not_exited:
  74. counter += 1
  75. time.sleep(1)
  76. if counter >= 10:
  77. client.publish("video/heartbeat", "mic1")
  78. counter = 0
  79. pipeline.set_state(Gst.State.NULL)