Advertisement
Guest User

dtlssrt example

a guest
Feb 9th, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # create two endpoints, each with tx and rx pipelines using the DTLS
  4. # elements and let audio flowing for a while to give time for a packet capture
  5.  
  6. import time
  7. #gi.require_version('Gst','1.0')
  8. from gi.repository import Gst, GObject, GLib
  9. GObject.threads_init()
  10. Gst.init(None)
  11.  
  12.  
  13. def _start_pipeline(pipeline):
  14. pipeline.set_state(Gst.State.PLAYING)
  15. pipeline.get_state(Gst.CLOCK_TIME_NONE)
  16.  
  17.  
  18. def _sleep_while_iterating_gloop(secs):
  19. """ block for secs seconds but iterate the gloop while you do """
  20. for _ in range(10 * secs):
  21. gloop = GLib.MainLoop()
  22. gloop_context = gloop.get_context()
  23. gloop_context.iteration(may_block=False)
  24. time.sleep(0.1)
  25.  
  26. def dtls_tx_pipeline_description(name, is_client, port):
  27. return ' ! '.join([
  28. 'audiotestsrc is-live=true',
  29. 'audio/x-raw, rate=8000, format=S16LE, channels=1',
  30. 'opusenc frame-size=10',
  31. 'rtpopuspay pt=103',
  32. '.rtp_sink_0 dtlssrtpenc connection-id={name} is-client={client} .src',
  33. 'udpsink port={port}'
  34. ]).format(name=name, client=is_client, port=port)
  35.  
  36.  
  37. def dtls_rx_pipeline_description(name, port):
  38. return ' ! '.join([
  39. 'udpsrc port={port}',
  40. '.sink dtlssrtpdec connection-id={name} .rtp_src',
  41. 'queue',
  42. 'fakesink async=false'
  43. ]).format(name=name, port=port)
  44.  
  45.  
  46. class Endpoint:
  47. def __init__(self, name, is_client, tx_port, rx_port):
  48. self.name = name
  49. tx_pipeline_description = dtls_tx_pipeline_description(
  50. name, is_client, tx_port
  51. )
  52. rx_pipeline_description = dtls_rx_pipeline_description(name, rx_port)
  53. print(rx_pipeline_description)
  54. print(tx_pipeline_description)
  55.  
  56. self.rx_pipeline = Gst.parse_launch(rx_pipeline_description)
  57. self.tx_pipeline = Gst.parse_launch(tx_pipeline_description)
  58.  
  59. def start(self):
  60. # Start RX first, otherwise it fails due to the current implementation
  61. self.start_rx_pipeline()
  62. self.start_tx_pipeline()
  63.  
  64. def start_rx_pipeline(self):
  65. _start_pipeline(self.rx_pipeline)
  66.  
  67. def start_tx_pipeline(self):
  68. _start_pipeline(self.tx_pipeline)
  69.  
  70. def stop(self):
  71. def stop_pipeline(p):
  72. p.set_state(Gst.State.NULL)
  73. p.get_state(Gst.CLOCK_TIME_NONE)
  74. stop_pipeline(self.tx_pipeline)
  75. stop_pipeline(self.rx_pipeline)
  76.  
  77. blue = Endpoint("blue", is_client=True, tx_port=23000, rx_port=23002)
  78. red = Endpoint("red", is_client=False, tx_port=23002, rx_port=23000)
  79. print('Start')
  80.  
  81. red.start()
  82. blue.start()
  83.  
  84. _sleep_while_iterating_gloop(3)
  85.  
  86. red.stop()
  87. blue.stop()
  88. print('End')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement