Changeset 0fa70d99fc44dd20b66347158c7d926daeff7397

Show
Ignore:
Timestamp:
09/24/07 05:06:10 (6 years ago)
Author:
Guillaume Pellerin <yomguy@…>
Children:
f301a42ec88d17568c5e38c8fe8c40e8d96caaea
Parents:
17fad2a8ef3f22149fa58696d469a7a8b497fd0f
git-committer:
Guillaume Pellerin <yomguy@parisson.com> / 2007-09-24T03:06:10Z+0000
Message:

* Create Station class (producer)
* Modify Channel class (worker)
* Get the multi-channels work (Thread + Queue) !!
* Fix random playlist bug
* Cleanup

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • d-fuzz.py

    r17fad2a r0fa70d9  
    5353     
    5454    def __init__(self, conf_file): 
    55         #Thread.__init__(self) 
    56         self.status = -1 
    57         self.version = '0.1' 
    5855        self.conf_file = conf_file 
    5956 
     
    6966 
    7067    def run(self): 
    71         print "D-fuzz v"+self.version 
    7268        self.conf = self.get_conf_dict() 
    73         print self.conf 
     69        #print self.conf 
    7470 
    7571        # Fix wrong type data from xmltodict when one station (*) 
     
    8076        print 'Number of stations : ' + str(nb_stations) 
    8177         
    82         # Create a Queue: 
    83         #stream_pool = Queue.Queue ( 0 ) 
    84  
    8578        for i in range(0,nb_stations): 
     79            # Create a Queue 
     80            q = Queue.Queue(1) 
     81 
    8682            # (*) idem 
    8783            if isinstance(self.conf['d-fuzz']['station'], dict): 
     
    8985            else: 
    9086                station = self.conf['d-fuzz']['station'][i] 
    91             print station 
     87            #print station 
    9288            name = station['infos']['name'] 
    93             channels = int(station['infos']['channels']) 
    94             print 'Station %s has %s channels' % (name, str(channels)) 
    95             for channel_id in range(0,channels): 
     89            nb_channels = int(station['infos']['channels']) 
     90            print 'Station %s: %s has %s channels' % (str(i+1), name, str(nb_channels)) 
     91            s = Station(station, nb_channels, q) 
     92            s.start() 
     93            time.sleep(0.1) 
     94            for channel_id in range(0, nb_channels): 
    9695                #print channel_id 
    97                 Channel(station, channel_id + 1).start() 
    98                 #channel.start() 
    99                 time.sleep(0.5) 
     96                c = Channel(station, channel_id + 1, q) 
     97                c.start() 
     98                #time.sleep(0.5) 
     99 
     100 
     101class Station(Thread): 
     102    """A D-Fuzz Station thread""" 
     103 
     104    def __init__(self, station, nb_channels, station_q): 
     105        Thread.__init__(self) 
     106        self.station_q = station_q 
     107        self.station = station 
     108        self.nb_channels = nb_channels 
     109 
     110    def run(self): 
     111        #station_q = self.station_q 
     112        i=0 
     113        while 1 :  
     114            #print currentThread(),"Produced One Item:",i 
     115            self.station_q.put(i,1) 
     116            i+=1 
     117            #time.sleep(1) 
    100118 
    101119 
     
    103121    """A channel shouting thread""" 
    104122 
    105     def __init__(self, station, channel_id): 
     123    def __init__(self, station, channel_id, channel_q): 
    106124        Thread.__init__(self) 
     125        self.channel_q = channel_q 
    107126        self.station = station 
    108127        self.main_command = 'cat' 
    109         self.buffer_size = 0xFFFF 
     128        self.buffer_size = 16384 
    110129        self.channel_id = channel_id 
    111  
    112         # Pool Queue 
    113         #self.stream_pool = stream_pool 
    114          
    115130        self.channel = Shout() 
    116         #self.station = station 
    117131        self.id = 999999 
     132        self.counter = 0 
    118133        self.rand_list = [] 
    119           
    120134        # Media 
    121135        self.media_dir = self.station['media']['dir'] 
    122  
    123136        self.channel.format = self.station['media']['format'] 
    124137        self.mode_shuffle = int(self.station['media']['shuffle']) 
    125  
    126138        # Infos 
    127139        self.short_name = self.station['infos']['short_name'] + '_' + str(self.channel_id) 
     
    130142        self.channel.description = self.station['infos']['description'] 
    131143        self.channel.url = self.station['infos']['url'] 
    132          
    133144        # Server 
    134145        self.channel.protocol = 'http'     # | 'xaudiocast' | 'icy' 
     
    140151        #print self.channel.mount 
    141152        self.channel.public = int(self.station['server']['public']) 
    142  
    143153        # s.audio_info = { 'key': 'val', ... } 
    144154        #  (keys are shout.SHOUT_AI_BITRATE, shout.SHOUT_AI_SAMPLERATE, 
     
    164174        lp = len(playlist) 
    165175        if self.id >= (lp - 1): 
     176            #print 'Get random list...' 
    166177            playlist = self.get_playlist() 
    167178            lp_new = len(playlist) 
    168             if lp_new != lp: 
     179            if lp_new != lp or self.counter == 0: 
    169180                self.rand_list = range(0,lp_new) 
    170181                random.shuffle(self.rand_list) 
    171             #print self.rand_list 
     182                #print self.rand_list 
    172183            self.id = 0 
    173184        else: 
     
    180191        """Apply command and stream data through a generator.  
    181192        Taken from Telemeta...""" 
    182          
    183193        __chunk = 0 
    184194        try: 
     
    191201        except: 
    192202            raise DFuzzError('Command failure:', command, proc) 
    193  
    194203        # Core processing 
    195204        while True: 
     
    204213    def run(self): 
    205214        #print "Using libshout version %s" % shout.version() 
    206  
    207215        #__chunk = 0 
    208216        self.channel.open() 
     
    214222        lp = len(playlist) 
    215223        self.rand_list = range(0,lp) 
    216  
    217224         
    218225        while True: 
    219226            if lp == 0: 
    220227                break 
    221  
    222             # Get a client out of the queue 
    223             #client = self.stream_pool.get() 
    224  
    225             #if client != None: 
    226  
    227             time.sleep(0.1) 
    228228            if self.mode_shuffle == 1: 
     229                #print 'Shuffle mode' 
    229230                playlist, media = self.get_next_media_rand(playlist) 
    230231            else: 
    231232                playlist, media = self.get_next_media_lin(playlist) 
    232  
     233            self.counter += 1 
    233234            file_name = string.replace(media, self.media_dir + os.sep, '') 
    234235            #print 'Playlist (%s ch%s) : %s' % (self.short_name, self.channel_id, file_name) 
     
    242243 
    243244            for __chunk in stream: 
     245                # Get the queue 
     246                self.channel_q.get(1) 
    244247                self.channel.send(__chunk) 
    245248                self.channel.sync() 
    246249 
    247250        self.channel.close() 
    248  
    249251 
    250252 
     
    265267                                                error) 
    266268 
    267                                                  
    268269def main():     
    269270    if len(sys.argv) == 2: 
     271        print "D-fuzz v"+version 
    270272        dfuzz_main = DFuzz(sys.argv[1]) 
    271273        dfuzz_main.run()