summaryrefslogtreecommitdiff
path: root/example-scripts/gstreamer/source-nostream-music-from-folder.py
blob: 8bd77c2c38b4dc76f32899f572d37639cbe805f0 (plain)
  1. #!/usr/bin/env python3
  2. import sys, gi, signal
  3. gi.require_version('Gst', '1.0')
  4. from gi.repository import Gst, GObject
  5. # init GObject & Co. before importing local classes
  6. GObject.threads_init()
  7. Gst.init([])
  8. class LoopSource(object):
  9. def __init__(self):
  10. pipeline = """
  11. audioresample name=join !
  12. audioconvert !
  13. audio/x-raw,format=S16LE,channels=2,layout=interleaved,rate=48000 !
  14. matroskamux !
  15. tcpclientsink host=localhost port=18000
  16. """
  17. self.pipeline = Gst.parse_launch(pipeline)
  18. # https://c3voc.mazdermind.de/testfiles/music-snippets.tar
  19. self.src = Gst.ElementFactory.make('uridecodebin', None)
  20. self.src.set_property('uri', 'file:///home/peter/Music/pieces/001 - Bruno Mars - Grenade.mp3');
  21. self.src.connect('pad-added', self.on_pad_added)
  22. self.pipeline.add(self.src)
  23. self.joinpad = self.pipeline.get_by_name('join').get_static_pad('sink')
  24. # Binding End-of-Stream-Signal on Source-Pipeline
  25. self.pipeline.bus.add_signal_watch()
  26. self.pipeline.bus.connect("message::eos", self.on_eos)
  27. self.pipeline.bus.connect("message::error", self.on_error)
  28. print("playing")
  29. self.pipeline.set_state(Gst.State.PLAYING)
  30. def on_pad_added(self, src, pad):
  31. print('New Pad: '+str(pad))
  32. pad.add_probe(Gst.PadProbeType.EVENT_DOWNSTREAM | Gst.PadProbeType.BLOCK, self.on_pad_event)
  33. if self.joinpad.is_linked():
  34. self.joinpad.unlink(self.joinpad.get_peer())
  35. pad.link(self.joinpad)
  36. def on_pad_event(self, pad, info):
  37. event = info.get_event()
  38. print('Pad Event: '+str(event.type)+' on Pad '+str(pad))
  39. if event.type == Gst.EventType.EOS:
  40. print('Is an EOS event, dropping & unlinking')
  41. GObject.idle_add(self.next_track)
  42. return Gst.PadProbeReturn.DROP
  43. return Gst.PadProbeReturn.PASS
  44. def next_track(self):
  45. print("next_track")
  46. self.pipeline.set_state(Gst.State.READY)
  47. self.src.set_property('uri', 'file:///home/peter/Music/pieces/003 - Taio Cruz feat. Kylie Minogue - Higher.mp3');
  48. self.pipeline.set_state(Gst.State.PLAYING)
  49. return False
  50. def on_eos(self, bus, message):
  51. print('Received EOS-Signal')
  52. sys.exit(1)
  53. def on_error(self, bus, message):
  54. print('Received Error-Signal')
  55. (error, debug) = message.parse_error()
  56. print('Error-Details: #%u: %s' % (error.code, debug))
  57. sys.exit(1)
  58. def main():
  59. signal.signal(signal.SIGINT, signal.SIG_DFL)
  60. src = LoopSource()
  61. mainloop = GObject.MainLoop()
  62. try:
  63. mainloop.run()
  64. except KeyboardInterrupt:
  65. print('Terminated via Ctrl-C')
  66. if __name__ == '__main__':
  67. main()