def server(port_rtsp, port_playlist, playlistLen): """ This function waits for RTSP/Playlist request and starts new thread. """ global stats global server_ip playlistLen = playlistLen server_ip = helpers.tcpLocalIp() print "Server: My IP: " + server_ip helpers.createDir("Wavs") helpers.createDir("Sockets") playlist.initSongs() inputs = [] rtspsocket = listen(port_rtsp) if rtspsocket is None: sys.exit(1) playlistsocket = listen(port_playlist) if playlistsocket is None: rtspsocket.close() sys.exit(1) inputs.append(rtspsocket) inputs.append(playlistsocket) while True: try: inputready,outputready,exceptready = select.select(inputs,[],[]) except KeyboardInterrupt: print '\r\nInterrupted by user' inputs.remove(rtspsocket) inputs.remove(playlistsocket) rtspsocket.close() playlistsocket.close() shutil.rmtree(os.getcwd() + '/Sockets', ignore_errors=True) # remove "Sockets" dir shutil.rmtree(os.getcwd() + '/Wavs', ignore_errors=True) # remove "Wavs" dir stats.printStats() os.kill(os.getpid(), signal.SIGTERM) # terminate itself for option in inputready: if option is rtspsocket: try: conn, addr = rtspsocket.accept() except socket.error as msg: print 'Server: RTSP ',msg continue print 'Server: RTSP connection from ', addr r = Accept_RTSP(conn,addr) r.start() elif option is playlistsocket: try: conn,addr = playlistsocket.accept() except socket.error as msg: print 'Server: Playlist ',msg continue print 'Server: Playlist request from ', addr p = Accept_PL(conn,addr,port_rtsp, playlistLen) p.start()
def bind(PORT): """ Create UDP socket and bind given port with it. """ # HOST = '127.0.0.1' # Local host HOST = helpers.tcpLocalIp() s = None for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_DGRAM): af, socktype, proto, canonname, sa = res try: s = socket.socket(af, socktype, proto) except socket.error as msg: print "Streamer: " + str(msg) s = None continue try: s.bind(sa) except socket.error as msg: print "Streamer: " + str(msg) s.close() s = None continue break return s
def bind(PORT): """ Create UDP socket and bind given port with it. """ #HOST = '127.0.0.1' # Local host HOST = helpers.tcpLocalIp() s = None for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_DGRAM): af, socktype, proto, canonname, sa = res try: s = socket.socket(af, socktype, proto) except socket.error as msg: print 'Streamer: ' + str(msg) s = None continue try: s.bind(sa) except socket.error as msg: print 'Streamer: ' + str(msg) s.close() s = None continue break return s
### # # SDP Message (RTSP version) # ### import NTP import helpers server_ip = helpers.tcpLocalIp() terminator = "\r\n" class SDPMessage: def __init__(self, program, subject, session): self.v = "0" self.s = subject self.o = program+" "+str(session)+" "+NTP.timestamp()+" IN IP4 "\ +server_ip self.m = "" #"audio 0 RTP/AVP 0" #u-law PCM! <-- Fix me for A-law # removed /2 self.mode = "" self.rtpmap = "" self.c = "" self.t = "" def setPort(self, port): self.m = "audio " + str(port) + " RTP/AVP 0" def setMode(self, mode): self.mode = mode
def server(port_rtsp, port_playlist, port_sip): """ This function waits for RTSP/Playlist/SIP request and starts new thread. """ global server_ip server_ip = helpers.tcpLocalIp() print "Server: My IP: " + server_ip helpers.createDir("Records") helpers.createDir("Sockets") inputs = [] rtsp_socket = listen(port_rtsp) # TCP socket if rtsp_socket is None: sys.exit(1) print "Server: RTSP socket listening" playlist_socket = listen(port_playlist) # TCP socket if playlist_socket is None: rtsp_socket.close() sys.exit(1) print "Server: Playlist socket listening" sip_socket = bind(port_sip) # UDP socket if sip_socket is None: rtsp_socket.close() playlist_socket.close() sys.exit(1) print "Server: SIP socket ready" inputs.append(rtsp_socket) inputs.append(playlist_socket) inputs.append(sip_socket) session = random.randint(1,10000) while True: try: inputready,outputready,exceptready = select.select(inputs, [], []) except KeyboardInterrupt: print '\r\nInterrupted by user' inputs.remove(rtsp_socket) inputs.remove(playlist_socket) inputs.remove(sip_socket) rtsp_socket.close() playlist_socket.close() sip_socket.close() shutil.rmtree(os.getcwd() + "/Sockets", ignore_errors=True) # remove "Sockets" dir shutil.rmtree(os.getcwd() + "/Records", ignore_errors=True) # remove "Records" dir os.kill(os.getpid(), signal.SIGTERM) # terminate itself for option in inputready: if option is rtsp_socket: try: conn, addr = rtsp_socket.accept() except socket.error as msg: print 'Server: RTSP ',msg continue print 'Server: RTSP request from ',addr r = Accept_RTSP(conn, addr) r.start() elif option is playlist_socket: try: conn,addr = playlist_socket.accept() except socket.error as msg: print 'Server: Playlist ', msg continue print 'Server: Playlist request from ',addr p = Accept_PL(conn, addr, port_rtsp) p.start() elif option is sip_socket: buff = '' try: buff, addr = sip_socket.recvfrom(1024) except socket.error as msg: print 'Server: SIP ', msg continue print 'Server: SIP message from ', addr, ':' sip_inst = SIP.SIPMessage(buff) if sip_inst.parse() is True: print sip_inst.SIPMsg client_ip = sip_inst.client_ip if sip_inst.SIPCommand == "INVITE": #Start receiving f = Flow() flows[addr] = f # Use port of remote end too # Create rtp and rtcp socket while True: while True: port = random.randint(10000,65000) rtp_socket = bind(port) if rtp_socket is None: continue else: break rtcp_socket = bind(port + 1) if rtcp_socket is None: rtp_socket.close() else: break r = Receiver(addr,rtp_socket,rtcp_socket, sip_inst.userId, sip_inst.callerId) r.start() sdp_inst = sdp.SDPMessage("MBox", "Talk", session) sdp_inst.setPort(port) sdp_inst.setRtpmap() sdp_inst.setC() sdp_inst.setT() reply = sip_inst.createInviteReplyMessage(sdp_inst.getMessage(), client_ip, server_ip, port_sip, sip_inst.userId) print "Sending invite reply:" print reply sent = sip_socket.sendto(reply, addr) print >>sys.stderr, "Sent %s bytes to %s" % (sent, addr) elif sip_inst.SIPCommand == "OPTIONS": sdp_inst = sdp.SDPMessage("MBox", "Talk", session) #sdp_inst.setPort(8078) sdp_inst.sip_port = port_sip sdp_inst.setRtpmap() sdp_inst.setC() sdp_inst.setT() reply = sip_inst.createOptionsReplyMessage(sdp_inst.getMessage(), client_ip, server_ip, port_sip, sip_inst.userId) print "Sending options reply:" print reply sent = sip_socket.sendto(reply, addr) print >>sys.stderr, "Sent %s bytes to %s" % (sent, addr) elif sip_inst.SIPCommand == "BYE": #Leave flows[addr].stop = True reply = sip_inst.createByeReplyMessage(server_ip, port_sip, sip_inst.userId) print "Sending bye reply:" print reply sent = sip_socket.sendto(reply, addr) print >>sys.stderr, "Sent %s bytes to %s" % (sent, addr) break
def server(port_rtsp, port_playlist, port_sip): """ This function waits for RTSP/Playlist/SIP request and starts new thread. """ global server_ip server_ip = helpers.tcpLocalIp() print "Server: My IP: " + server_ip helpers.createDir("Records") helpers.createDir("Sockets") inputs = [] rtsp_socket = listen(port_rtsp) # TCP socket if rtsp_socket is None: sys.exit(1) print "Server: RTSP socket listening" playlist_socket = listen(port_playlist) # TCP socket if playlist_socket is None: rtsp_socket.close() sys.exit(1) print "Server: Playlist socket listening" sip_socket = bind(port_sip) # UDP socket if sip_socket is None: rtsp_socket.close() playlist_socket.close() sys.exit(1) print "Server: SIP socket ready" inputs.append(rtsp_socket) inputs.append(playlist_socket) inputs.append(sip_socket) session = random.randint(1, 10000) while True: try: inputready, outputready, exceptready = select.select( inputs, [], []) except KeyboardInterrupt: print '\r\nInterrupted by user' inputs.remove(rtsp_socket) inputs.remove(playlist_socket) inputs.remove(sip_socket) rtsp_socket.close() playlist_socket.close() sip_socket.close() shutil.rmtree(os.getcwd() + "/Sockets", ignore_errors=True) # remove "Sockets" dir shutil.rmtree(os.getcwd() + "/Records", ignore_errors=True) # remove "Records" dir os.kill(os.getpid(), signal.SIGTERM) # terminate itself for option in inputready: if option is rtsp_socket: try: conn, addr = rtsp_socket.accept() except socket.error as msg: print 'Server: RTSP ', msg continue print 'Server: RTSP request from ', addr r = Accept_RTSP(conn, addr) r.start() elif option is playlist_socket: try: conn, addr = playlist_socket.accept() except socket.error as msg: print 'Server: Playlist ', msg continue print 'Server: Playlist request from ', addr p = Accept_PL(conn, addr, port_rtsp) p.start() elif option is sip_socket: buff = '' try: buff, addr = sip_socket.recvfrom(1024) except socket.error as msg: print 'Server: SIP ', msg continue print 'Server: SIP message from ', addr, ':' sip_inst = SIP.SIPMessage(buff) if sip_inst.parse() is True: print sip_inst.SIPMsg client_ip = sip_inst.client_ip if sip_inst.SIPCommand == "INVITE": #Start receiving f = Flow() flows[addr] = f # Use port of remote end too # Create rtp and rtcp socket while True: while True: port = random.randint(10000, 65000) rtp_socket = bind(port) if rtp_socket is None: continue else: break rtcp_socket = bind(port + 1) if rtcp_socket is None: rtp_socket.close() else: break r = Receiver(addr, rtp_socket, rtcp_socket, sip_inst.userId, sip_inst.callerId) r.start() sdp_inst = sdp.SDPMessage("MBox", "Talk", session) sdp_inst.setPort(port) sdp_inst.setRtpmap() sdp_inst.setC() sdp_inst.setT() reply = sip_inst.createInviteReplyMessage( sdp_inst.getMessage(), client_ip, server_ip, port_sip, sip_inst.userId) print "Sending invite reply:" print reply sent = sip_socket.sendto(reply, addr) print >> sys.stderr, "Sent %s bytes to %s" % (sent, addr) elif sip_inst.SIPCommand == "OPTIONS": sdp_inst = sdp.SDPMessage("MBox", "Talk", session) #sdp_inst.setPort(8078) sdp_inst.sip_port = port_sip sdp_inst.setRtpmap() sdp_inst.setC() sdp_inst.setT() reply = sip_inst.createOptionsReplyMessage( sdp_inst.getMessage(), client_ip, server_ip, port_sip, sip_inst.userId) print "Sending options reply:" print reply sent = sip_socket.sendto(reply, addr) print >> sys.stderr, "Sent %s bytes to %s" % (sent, addr) elif sip_inst.SIPCommand == "BYE": #Leave flows[addr].stop = True reply = sip_inst.createByeReplyMessage( server_ip, port_sip, sip_inst.userId) print "Sending bye reply:" print reply sent = sip_socket.sendto(reply, addr) print >> sys.stderr, "Sent %s bytes to %s" % (sent, addr) break
### # # SDP Message (RTSP version) # ### import NTP import helpers server_ip = helpers.tcpLocalIp() terminator = "\r\n" class SDPMessage: def __init__(self, program, subject, session): self.v = "0" self.s = subject self.o = program+" "+str(session)+" "+NTP.timestamp()+" IN IP4 "\ +server_ip self.m = ""#"audio 0 RTP/AVP 0" #u-law PCM! <-- Fix me for A-law # removed /2 self.mode = "" self.rtpmap = "" self.c = "" self.t = "" def setPort(self, port): self.m = "audio "+str(port)+" RTP/AVP 0" def setMode(self, mode): self.mode = mode