#!/usr/bin/python

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst

timeout=250

def on_error (bus, msg):
    print('error:', msg.parse_error())

def pause(playbin):
    print ("pausing for %d secs" % timeout) 
    print ("go get some coffee")
    playbin.set_state(Gst.State.PAUSED)
    
def resume(playbin):
    print "resuming... wait until we run out of buffer"
    playbin.set_state(Gst.State.PLAYING)
   
Gst.init (None)

playbin = Gst.ElementFactory.make ('playbin', None)
# some cheesy 80s rock streaming service
playbin.set_property('uri','http://64.202.98.133:9130')
playbin.set_state(Gst.State.PLAYING)

bus = playbin.get_bus ()
bus.add_signal_watch()
bus.connect('message::error', on_error)

# after 5 seconds, pause
GObject.timeout_add_seconds (5, pause, playbin)

# after 120 seconds, resume
GObject.timeout_add_seconds (timeout, resume, playbin)

loop = GObject.MainLoop()
loop.run ()


#Q output:
#Q  
#Q pausing for 250 secs
#Q go get some coffee
#Q resuming...
#Q ('error:', (GError('Server does not support seeking.',), 'gstsouphttpsrc.c(888): gst_soup_http_src_got_headers_cb (): /GstPlayBin:playbin0/GstURIDecodeBin:uridecodebin0/GstSoupHTTPSrc:source:\nServer does not accept Range HTTP header, URL: http://64.202.98.133:9130'))
#Q ('error:', (GError('Internal data flow error.',), 'gstbasesrc.c(2865): gst_base_src_loop (): /GstPlayBin:playbin0/GstURIDecodeBin:uridecodebin0/GstSoupHTTPSrc:source:\nstreaming task paused, reason error (-5)'))


