# HG changeset patch # User Goffi # Date 1327802425 -3600 # Node ID fbe7c9118ce42c8ca741e740d9c93170be151882 # Parent ba6e1feda03e9aa9fd205c374f225153237720a5 plugin radiocol: working Radio Collective \o/ diff -r ba6e1feda03e -r fbe7c9118ce4 src/plugins/plugin_misc_radiocol.py --- a/src/plugins/plugin_misc_radiocol.py Tue Jan 24 00:22:11 2012 +0100 +++ b/src/plugins/plugin_misc_radiocol.py Sun Jan 29 03:00:25 2012 +0100 @@ -58,6 +58,8 @@ "description": _("""Implementation of radio collective""") } +QUEUE_LIMIT = 2 + class Radiocol(): @@ -71,6 +73,9 @@ host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') #room_jid, referee, profile host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') #room_jid, reason, profile host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') #room_jid, filename, title, artist, album, profile + host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss') #room_jid, filename, profile + host.bridge.addSignal("radiocolNoUpload", ".plugin", signature='ss') #room_jid, profile + host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') #room_jid, profile host.trigger.add("MUC user joined", self.userJoinedTrigger) def createRadiocolElt(self, to_jid, type="normal"): @@ -86,6 +91,16 @@ started_elt = domish.Element(('','started')) return started_elt + def __create_preload_elt(self, sender, filename, title, artist, album): + preload_elt = domish.Element(('','preload')) + preload_elt['sender'] = sender + preload_elt['filename'] = filename #XXX: the frontend should know the temporary directory where file is put + preload_elt['title'] = title + preload_elt['artist'] = artist + preload_elt['album'] = album + return preload_elt + + def userJoinedTrigger(self, room, user, profile): """This trigger is used to check if we are waiting people in this room, and to create a game if everybody is here""" @@ -159,7 +174,7 @@ referee = room_jid.userhost() + '/' + room_nick status = {} occupants_data = {} - self.radios[room_jid.userhost()] = {'referee':referee, 'occupants_data':occupants_data} + self.radios[room_jid.userhost()] = {'referee':referee, 'queue':[], 'upload':True, 'playing': False, 'occupants_data':occupants_data} mess = self.createRadiocolElt(jid.JID(room_jid.userhost())) mess.firstChildElement().addChild(self.__create_started_elt()) self.host.profiles[profile].xmlstream.send(mess) @@ -181,12 +196,12 @@ song = OggVorbis(song_path) except OggVorbisHeaderError: #this file is not ogg vorbis, we reject it - import pdb - pdb.set_trace() + self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), \ + "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile) """mess = self.createRadiocolElt(jid.JID(referee)) reject_elt = mess.firstChildElement().addElement(('','song_rejected')) reject_elt['sender'] = client.jid - reject_elt['reason'] = _("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable") + reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable" #FIXME: add an error code self.host.profiles[profile].xmlstream.send(mess)""" return @@ -204,14 +219,43 @@ self.host.profiles[profile].xmlstream.send(mess) return - + def playNext(self, room_jid, profile): + """"Play next sont in queue if exists, and put a timer + which trigger after the song has been played to play next one""" + #TODO: need to check that there are still peoples in the room + # and clean the datas/stop the playlist if it's not the case + radio_data = self.radios[room_jid.userhost()] + queue = radio_data['queue'] + if not queue: + #nothing left to play, we need to wait for uploads + radio_data['playing'] = False + return + + filename, length = queue.pop(0) + mess = self.createRadiocolElt(room_jid) + play_elt = mess.firstChildElement().addElement(('','play')) + play_elt['filename'] = filename + self.host.profiles[profile].xmlstream.send(mess) + + if not radio_data['upload'] and len(queue) < QUEUE_LIMIT: + #upload is blocked and we now have resources to get more, we reactivate it + mess = self.createRadiocolElt(room_jid) + no_upload_elt = mess.firstChildElement().addElement(('','upload_ok')) + self.host.profiles[profile].xmlstream.send(mess) + radio_data['upload'] = True + + print ("Playing next song in %s s" % length) + reactor.callLater(length, self.playNext, room_jid, profile) + def radiocol_game_cmd(self, mess_elt, profile): + #FIXME: we should check sender (is it referee ?) here before accepting commands from_jid = jid.JID(mess_elt['from']) room_jid = jid.JID(from_jid.userhost()) radio_elt = mess_elt.firstChildElement() radio_data = self.radios[room_jid.userhost()] occupants_data = radio_data['occupants_data'] + queue = radio_data['queue'] for elt in radio_elt.elements(): @@ -219,20 +263,53 @@ self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile) elif elt.name == 'preload': #a song is in queue and must be preloaded self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile) + elif elt.name == 'play': + self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile) elif elt.name == 'song_rejected': #a song has been refused - import pdb - pdb.set_trace() - elif elt.name == 'song_added': #a song has been refused - #FIXME: we are KISS for the PoC: every song is added. Need to manage some sort of rules to allow peoples to send songs + self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), elt['reason'], profile) + elif elt.name == 'no_upload': + self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile) + elif elt.name == 'upload_ok': + self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile) + elif elt.name == 'song_added': #a song has been added + #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. + # Need to manage some sort of rules to allow peoples to send songs + + if len(queue) >= QUEUE_LIMIT: + #there are already too many songs in queue, we reject this one + mess = self.createRadiocolElt(room_jid) + reject_elt = mess.firstChildElement().addElement(('','song_rejected')) + reject_elt['sender'] = from_jid.resource + reject_elt['reason'] = "Too many songs in queue" + #FIXME: add an error code + self.host.profiles[profile].xmlstream.send(mess) + return + + #The song is accepted and added in queue + queue.append((elt['filename'], float(elt['length']))) + + if len(queue) >= QUEUE_LIMIT: + #We are at the limit, we refuse new upload until next play + mess = self.createRadiocolElt(room_jid) + no_upload_elt = mess.firstChildElement().addElement(('','no_upload')) + #FIXME: add an error code + self.host.profiles[profile].xmlstream.send(mess) + radio_data['upload'] = False + + mess = self.createRadiocolElt(room_jid) - preload_elt = mess.firstChildElement().addElement(('','preload')) - preload_elt['sender'] = from_jid.resource - preload_elt['filename'] = elt['filename'] #XXX: the frontend should know the temporary directory where file is put - preload_elt['title'] = elt['title'] - preload_elt['artist'] = elt['artist'] - preload_elt['album'] = elt['album'] - preload_elt['length'] = elt['length'] + preload_elt = self.__create_preload_elt(from_jid.resource, + elt['filename'], + elt['title'], + elt['artist'], + elt['album']) + mess.firstChildElement().addChild(preload_elt) self.host.profiles[profile].xmlstream.send(mess) + if not radio_data['playing'] and len(queue) == 2: + #we have not started playing yet, and we have 2 songs in queue + #we can now start the party :) + radio_data['playing'] = True + self.playNext(room_jid, profile) else: error (_('Unmanaged game element: %s') % elt.name)