cam2.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/python3
  2. import time
  3. import threading
  4. import sys
  5. import gi
  6. import paho.mqtt.client as mqtt
  7. gi.require_version('Gst', '1.0')
  8. gi.require_version('GstNet', '1.0')
  9. from gi.repository import Gst, GstNet
  10. WIDTH = 1280
  11. HEIGHT = 720
  12. SCREEN_WIDTH = 800
  13. SCREEN_HEIGHT = 600
  14. zoom = 1
  15. # def bus_call(bus, msg, *args):
  16. # if msg.type == Gst.MessageType.EOS:
  17. # print("End-of-stream")
  18. # loop.quit()
  19. # return
  20. # elif msg.type == Gst.MessageType.ERROR:
  21. # print("GST ERROR", msg.parse_error())
  22. # loop.quit()
  23. # return
  24. # return True
  25. # The callback for when the client receives a CONNACK response from the server.
  26. def on_connect(client, userdata, flags, rc):
  27. print("Connected with result code "+str(rc))
  28. # Subscribing in on_connect() means that if we lose the connection and
  29. # reconnect then subscriptions will be renewed.
  30. client.subscribe("video/cam2/#")
  31. # The callback for when a PUBLISH message is received from the server.
  32. def on_message(client, userdata, msg):
  33. print(msg.topic+" "+str(msg.payload))
  34. client = mqtt.Client()
  35. client.on_connect = on_connect
  36. client.on_message = on_message
  37. client.connect("192.168.2.123", 1883, 60)
  38. if __name__ == "__main__":
  39. # initialization
  40. Gst.init(None)
  41. WIDTH = 1280
  42. HEIGHT = 720
  43. p = """rpicamsrc rotation=0 preview=0 bitrate=0 quantisation-parameter=23 sensor-mode=4 keyframe-interval=5 do-timestamp=true !\
  44. video/x-h264, width={WIDTH}, height={HEIGHT}, framerate=25/1, profile=high !\
  45. h264parse ! matroskamux ! tcpserversink host=0.0.0.0 port=5000 sync-method=next-keyframe"""
  46. p = p.format(HEIGHT=HEIGHT, WIDTH=WIDTH)
  47. print(p)
  48. pipeline = Gst.parse_launch(p)
  49. if pipeline is None:
  50. print("Failed to create pipeline")
  51. sys.exit(0)
  52. clock = GstNet.NetClientClock.new('CamVoctoClock', '192.168.2.123', 9998, 0)
  53. # FIXME: add clock support
  54. # print("Waiting for clock sync")
  55. # clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  56. # clock.do_wait(Gst.CLOCK_TIME_NONE)
  57. # print("Clock synced")
  58. pipeline.set_start_time(Gst.CLOCK_TIME_NONE)
  59. if clock is not None:
  60. pass
  61. # pipeline.use_clock(clock)
  62. pipeline.set_state(Gst.State.PLAYING)
  63. t = threading.Thread(target=client.loop_forever, daemon=True)
  64. t.start()
  65. while True:
  66. client.publish("video/heartbeat", "cam2")
  67. time.sleep(10)
  68. pipeline.set_state(Gst.State.NULL)