cam.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. def set_preview(val):
  28. if val == 0:
  29. videosrc.set_property("preview-x", SCREEN_WIDTH)
  30. videosrc.set_property("preview-y", SCREEN_HEIGHT)
  31. else:
  32. videosrc.set_property("preview-x", 0)
  33. videosrc.set_property("preview-y", 0)
  34. def set_zoom(target):
  35. global zoom
  36. if target < zoom:
  37. direction = -1
  38. else:
  39. direction = 1
  40. target = min(target, 1)
  41. target = max(target, 0.40)
  42. while abs(target-zoom) > 0.01: # fuck IEEE floats
  43. zoom = zoom + direction*0.015
  44. zoom = min(zoom, 1)
  45. zoom = max(zoom, 0.4)
  46. videosrc.set_property("roi-x", (1-zoom)/2)
  47. videosrc.set_property("roi-y", (1-zoom)/2)
  48. videosrc.set_property("roi-w", zoom)
  49. videosrc.set_property("roi-h", zoom)
  50. time.sleep(0.03)
  51. # The callback for when the client receives a CONNACK response from the server.
  52. def on_connect(client, userdata, flags, rc):
  53. print("Connected with result code "+str(rc))
  54. # Subscribing in on_connect() means that if we lose the connection and
  55. # reconnect then subscriptions will be renewed.
  56. client.subscribe("video/cam1/#")
  57. # The callback for when a PUBLISH message is received from the server.
  58. def on_message(client, userdata, msg):
  59. print(msg.topic+" "+str(msg.payload))
  60. if msg.topic == "video/cam1/preview":
  61. set_preview(int(msg.payload))
  62. elif msg.topic == "video/cam1/zoom":
  63. set_zoom(float(msg.payload))
  64. client = mqtt.Client()
  65. client.on_connect = on_connect
  66. client.on_message = on_message
  67. client.connect("192.168.2.123", 1883, 60)
  68. if __name__ == "__main__":
  69. if 'LD_LIBRARY_PATH' not in os.environ:
  70. os.environ['LD_LIBRARY_PATH'] = '/usr/local/lib'
  71. os.execv(sys.argv[0], sys.argv)
  72. # initialization
  73. Gst.init(None)
  74. videocodec = "video/x-h264, width=%d, height=%d, framerate=25/1, profile=high ! h264parse" % (WIDTH, HEIGHT)
  75. p = "rpicamsrc sensor-mode=4 bitrate=0 quantisation-parameter=12 do-timestamp=true keyframe-interval=5 name=src ! "+videocodec+" ! matroskamux streamable=true ! tcpserversink host=0.0.0.0 port=5000 sync-method=next-keyframe blocksize=16384"
  76. print(p)
  77. pipeline = Gst.parse_launch(p)
  78. if pipeline is None:
  79. print("Failed to create pipeline")
  80. sys.exit(0)
  81. clock = GstNet.NetClientClock.new('CamVoctoClock', '192.168.2.123', 9998, 0)
  82. # FIXME: add clock support
  83. # print("Waiting for clock sync")
  84. # clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  85. # clock.do_wait(Gst.CLOCK_TIME_NONE)
  86. # print("Clock synced")
  87. pipeline.set_start_time(Gst.CLOCK_TIME_NONE)
  88. if clock is not None:
  89. pass
  90. # pipeline.use_clock(clock)
  91. videosrc = pipeline.get_by_name("src")
  92. videosrc.set_property("preview", 1)
  93. videosrc.set_property("preview-w", SCREEN_WIDTH)
  94. videosrc.set_property("preview-h", SCREEN_HEIGHT)
  95. videosrc.set_property("fullscreen", False)
  96. # videosrc.set_property("bitrate", 25000000)
  97. videosrc.set_property("rotation", 180)
  98. set_preview(0)
  99. pipeline.set_state(Gst.State.PLAYING)
  100. t = threading.Thread(target=client.loop_forever, daemon=True)
  101. t.start()
  102. while True:
  103. client.publish("video/heartbeat", "cam1")
  104. time.sleep(10)
  105. pipeline.set_state(Gst.State.NULL)