Changeset 0fa70d99fc44dd20b66347158c7d926daeff7397
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r17fad2a
|
r0fa70d9
|
|
| 53 | 53 | |
| 54 | 54 | def __init__(self, conf_file): |
| 55 | | #Thread.__init__(self) |
| 56 | | self.status = -1 |
| 57 | | self.version = '0.1' |
| 58 | 55 | self.conf_file = conf_file |
| 59 | 56 | |
| … |
… |
|
| 69 | 66 | |
| 70 | 67 | def run(self): |
| 71 | | print "D-fuzz v"+self.version |
| 72 | 68 | self.conf = self.get_conf_dict() |
| 73 | | print self.conf |
| | 69 | #print self.conf |
| 74 | 70 | |
| 75 | 71 | # Fix wrong type data from xmltodict when one station (*) |
| … |
… |
|
| 80 | 76 | print 'Number of stations : ' + str(nb_stations) |
| 81 | 77 | |
| 82 | | # Create a Queue: |
| 83 | | #stream_pool = Queue.Queue ( 0 ) |
| 84 | | |
| 85 | 78 | for i in range(0,nb_stations): |
| | 79 | # Create a Queue |
| | 80 | q = Queue.Queue(1) |
| | 81 | |
| 86 | 82 | # (*) idem |
| 87 | 83 | if isinstance(self.conf['d-fuzz']['station'], dict): |
| … |
… |
|
| 89 | 85 | else: |
| 90 | 86 | station = self.conf['d-fuzz']['station'][i] |
| 91 | | print station |
| | 87 | #print station |
| 92 | 88 | 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): |
| 96 | 95 | #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 | |
| | 101 | class 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) |
| 100 | 118 | |
| 101 | 119 | |
| … |
… |
|
| 103 | 121 | """A channel shouting thread""" |
| 104 | 122 | |
| 105 | | def __init__(self, station, channel_id): |
| | 123 | def __init__(self, station, channel_id, channel_q): |
| 106 | 124 | Thread.__init__(self) |
| | 125 | self.channel_q = channel_q |
| 107 | 126 | self.station = station |
| 108 | 127 | self.main_command = 'cat' |
| 109 | | self.buffer_size = 0xFFFF |
| | 128 | self.buffer_size = 16384 |
| 110 | 129 | self.channel_id = channel_id |
| 111 | | |
| 112 | | # Pool Queue |
| 113 | | #self.stream_pool = stream_pool |
| 114 | | |
| 115 | 130 | self.channel = Shout() |
| 116 | | #self.station = station |
| 117 | 131 | self.id = 999999 |
| | 132 | self.counter = 0 |
| 118 | 133 | self.rand_list = [] |
| 119 | | |
| 120 | 134 | # Media |
| 121 | 135 | self.media_dir = self.station['media']['dir'] |
| 122 | | |
| 123 | 136 | self.channel.format = self.station['media']['format'] |
| 124 | 137 | self.mode_shuffle = int(self.station['media']['shuffle']) |
| 125 | | |
| 126 | 138 | # Infos |
| 127 | 139 | self.short_name = self.station['infos']['short_name'] + '_' + str(self.channel_id) |
| … |
… |
|
| 130 | 142 | self.channel.description = self.station['infos']['description'] |
| 131 | 143 | self.channel.url = self.station['infos']['url'] |
| 132 | | |
| 133 | 144 | # Server |
| 134 | 145 | self.channel.protocol = 'http' # | 'xaudiocast' | 'icy' |
| … |
… |
|
| 140 | 151 | #print self.channel.mount |
| 141 | 152 | self.channel.public = int(self.station['server']['public']) |
| 142 | | |
| 143 | 153 | # s.audio_info = { 'key': 'val', ... } |
| 144 | 154 | # (keys are shout.SHOUT_AI_BITRATE, shout.SHOUT_AI_SAMPLERATE, |
| … |
… |
|
| 164 | 174 | lp = len(playlist) |
| 165 | 175 | if self.id >= (lp - 1): |
| | 176 | #print 'Get random list...' |
| 166 | 177 | playlist = self.get_playlist() |
| 167 | 178 | lp_new = len(playlist) |
| 168 | | if lp_new != lp: |
| | 179 | if lp_new != lp or self.counter == 0: |
| 169 | 180 | self.rand_list = range(0,lp_new) |
| 170 | 181 | random.shuffle(self.rand_list) |
| 171 | | #print self.rand_list |
| | 182 | #print self.rand_list |
| 172 | 183 | self.id = 0 |
| 173 | 184 | else: |
| … |
… |
|
| 180 | 191 | """Apply command and stream data through a generator. |
| 181 | 192 | Taken from Telemeta...""" |
| 182 | | |
| 183 | 193 | __chunk = 0 |
| 184 | 194 | try: |
| … |
… |
|
| 191 | 201 | except: |
| 192 | 202 | raise DFuzzError('Command failure:', command, proc) |
| 193 | | |
| 194 | 203 | # Core processing |
| 195 | 204 | while True: |
| … |
… |
|
| 204 | 213 | def run(self): |
| 205 | 214 | #print "Using libshout version %s" % shout.version() |
| 206 | | |
| 207 | 215 | #__chunk = 0 |
| 208 | 216 | self.channel.open() |
| … |
… |
|
| 214 | 222 | lp = len(playlist) |
| 215 | 223 | self.rand_list = range(0,lp) |
| 216 | | |
| 217 | 224 | |
| 218 | 225 | while True: |
| 219 | 226 | if lp == 0: |
| 220 | 227 | 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) |
| 228 | 228 | if self.mode_shuffle == 1: |
| | 229 | #print 'Shuffle mode' |
| 229 | 230 | playlist, media = self.get_next_media_rand(playlist) |
| 230 | 231 | else: |
| 231 | 232 | playlist, media = self.get_next_media_lin(playlist) |
| 232 | | |
| | 233 | self.counter += 1 |
| 233 | 234 | file_name = string.replace(media, self.media_dir + os.sep, '') |
| 234 | 235 | #print 'Playlist (%s ch%s) : %s' % (self.short_name, self.channel_id, file_name) |
| … |
… |
|
| 242 | 243 | |
| 243 | 244 | for __chunk in stream: |
| | 245 | # Get the queue |
| | 246 | self.channel_q.get(1) |
| 244 | 247 | self.channel.send(__chunk) |
| 245 | 248 | self.channel.sync() |
| 246 | 249 | |
| 247 | 250 | self.channel.close() |
| 248 | | |
| 249 | 251 | |
| 250 | 252 | |
| … |
… |
|
| 265 | 267 | error) |
| 266 | 268 | |
| 267 | | |
| 268 | 269 | def main(): |
| 269 | 270 | if len(sys.argv) == 2: |
| | 271 | print "D-fuzz v"+version |
| 270 | 272 | dfuzz_main = DFuzz(sys.argv[1]) |
| 271 | 273 | dfuzz_main.run() |