cam2.py 2.6 KB

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