import gobject; gobject.threads_init() # gst needs this! import gst import daap import urlparse # stupid netloc thing of fail try: urlparse.uses_netloc.append('daap') except: pass class DaapSource(gst.BaseSrc, gst.URIHandler): __gsttemplates__ = ( gst.PadTemplate("src", gst.PAD_SRC, gst.PAD_ALWAYS, gst.caps_new_any()), ) __gstdetails__ = ("DAAP plugin", "Foo/Bar", "Read data on DAAP shares", "Philippe Normand , " "Alessandro Decina ") blocksize = 40960 def __init__(self): super(DaapSource, self).__init__() self.curoffset = 0 self._libs = {} self.client = None self.session = None self.response = None self.track = None self.data = None @classmethod def do_get_type_full(cls): return gst.URI_SRC @classmethod def do_get_protocols_full(cls): return ["daap"] def do_set_uri(self, uri): if not uri.startswith('daap://'): return False self.uri = uri return True def do_is_seekable(self): return True def do_get_uri(self): return self.uri # set_uri and get_uri are just syntactic sugar for do_set_uri and # do_get_uri def set_uri(self, uri): return self.do_set_uri(uri) def get_uri(self): return self.do_get_uri() def do_start(self): scheme, netloc, path, query, fragment = urlparse.urlsplit(self.uri) if netloc.find(":") == -1: host = netloc port = '3689' else: host, port = netloc.split(":") self.client = daap.DAAPClient() self.client.connect(host, port) self.session = self.client.login() library = self.session.library() track_id = int(path.split('/')[4].split('.')[0]) self.track = None for track in library.tracks(): if track.id == track_id: self.track = track break if track is None: return False return True def do_stop(self): self.session.logout() self.client.socket.close() self.client = None self.session = None self.track = None self.data = None return True def do_check_get_range(self): return False def do_create(self, offset, length): if self.data is None: self.data = self.track.request() data = self.data.read(self.blocksize) if data: return gst.FLOW_OK, gst.Buffer(data) else: self.data = None return gst.FLOW_UNEXPECTED, None # define this here so we can use it in the unittest without having to create the # media provider def register_daap_source (name): if gst.gst_version < (0, 10, 13): raise InitializeFailure(name, "The installed gst version doesn't support python plugins, " "daap support will not be available.") gobject.type_register(DaapSource) if gst.pygst_version <= (0, 10, 8): # use our workaround try: import _daap_uri_interface except ImportError: raise InitializeFailure(name, "The installed pygst version doesn't support the " "GstURIHandler interface and the custom GstURIInterface " "wrapper has not been built") # only register the first time (ComponentTestCase creates the daap # provider multiple times, so we have to check this) if gst.URIHandler.__gtype__ not in DaapSource.__gtype__.interfaces: _daap_uri_interface.bind_to(DaapSource) gst.element_register(type=DaapSource, elementname="daapsrc", rank=gst.RANK_PRIMARY) if __name__ == "__main__": import gtk register_daap_source("something") pipeline = gst.parse_launch("playbin uri=daap://tara.local:3689/databases/1/items/200.mp3") pipeline.set_state(gst.STATE_PLAYING) while 1: gtk.main_iteration()