All pastes #933910 Raw Edit

client_play.py

public python v1 · immutable
·published 2008-03-08 15:11 UTC
rendered paste body
import gobjectimport pygstpygst.require("0.10")import gstimport socketimport client_settingsimport reimport timeclass ClientPlay ():	def __init__ (self, categoryid, artworkid, usertypeid, genderid, ageid, init_volume, report_number_callback):		self.categoryid = categoryid		self.artworkid = artworkid		self.usertypeid = usertypeid		self.genderid = genderid		self.ageid = ageid		self.pipeline = None		self.report_number_callback = report_number_callback		self.dspmp3sink = None		self.init_volume = init_volume		try:			print "sending"			first_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)			first_socket.connect((client_settings.SERVER, client_settings.PORT))			if client_settings.USE_ICE:				play_str = "Play"			else:				play_str = "play"			first_socket.send(play_str + " %i,%i,%i,%i,%i" \				% (self.categoryid, self.artworkid, self.usertypeid, self.ageid, self.genderid))			first_socket.close()			print "listening"			self.listener = socket.socket (socket.AF_INET, socket.SOCK_STREAM)			self.listener.bind(("",8889))			self.listener.listen(1)			client, addr = self.listener.accept()			print "accepted"			self.socket = client			self.listener.close()			self.io_watch_id = gobject.io_add_watch(self.socket, gobject.IO_IN, self.begin_playing)		except socket, e:			print e			self.error("Could not send message to server error")			return	def set_report_number_callback (self, callback):		self.report_number_callback = callback	def begin_playing(self, source, condition):		data = self.socket.recv(1024)		r = re.compile("(\d+),(\d+)")		m = r.match(data)		port = int(m.group(1))		number_of_files = int(m.group(2))		if self.report_number_callback:			self.report_number_callback(number_of_files)		self.pipeline = gst.Pipeline()		if client_settings.USE_ICE:			src = gst.element_factory_make("gnomevfssrc")			loc = "http://" + client_settings.SERVER + \				":8000/stream" + str(port)			print loc			src.set_property("location", loc)			#src.set_property("num-buffers", 4096)			mad = gst.element_factory_make("mad")			audioconvert = gst.element_factory_make("audioconvert")			sink = gst.element_factory_make("alsasink")			self.pipeline.add(src, mad, audioconvert, sink)			gst.element_link_many(src, mad, audioconvert, sink)		else:			src = gst.element_factory_make("tcpclientsrc")			src.set_property("host", client_settings.SERVER)			src.set_property("port", port)			self.dspmp3sink = gst.element_factory_make("dspmp3sink")			self.dspmp3sink.set_property("fvolume", self.init_volume)			self.pipeline.add(src, self.dspmp3sink)			gst.element_link_many(src, self.dspmp3sink)		self.bus = self.pipeline.get_bus()		self.bus.add_signal_watch()		self.watch_id = self.bus.connect("message", self.get_message)		self.pipeline.set_state(gst.STATE_PLAYING)		self.io_watch_id = gobject.io_add_watch(self.socket, gobject.IO_IN, self.respond_to_ping)		# Currently no pipeline messages watched		self.watch_id = None		return False	def stop (self):		self.cleanup()	def get_message (self, bus, message):		print message.src.get_name() + str(message.type)		if message.type == gst.MESSAGE_ERROR:			err, debug = message.parse_error()                        print "Error from " + message.src.get_name() \				+ ": " + str(err) + " debug: " + debug	def cleanup (self):		self.cleanup_pipeline()		self.socket.close()		self.listener.close()	def cleanup_pipeline (self):		if self.io_watch_id:			gobject.source_remove(self.io_watch_id)		if self.pipeline:			if self.watch_id:				self.pipeline.get_bus().remove_signal_watch()				self.pipeline.get_bus().disconnect(self.watch_id)				self.watch_id = None			self.pipeline.set_state(gst.STATE_NULL)			self.pipeline = None	def respond_to_ping(self, source, condition):		self.socket.recv(1024)		self.socket.send("pong!")		return True	def set_volume (self, volume):		if self.dspmp3sink:			self.dspmp3sink.set_property("fvolume", volume)	def error (self, message):		print message