def reposition_media(self): if self.status == self.IDLE: return self.pause_media() self.media_player.stop() self.media_player.setMedia(QMediaContent()) self.timer.stop() self.timer.disconnect() self.play_event.clear() self.status = self.READY self.current_time = self.progressSlider.value() self.set_play_time() # reset socket self.client_rtp_socket.shutdown(socket.SHUT_RDWR) self.client_rtp_socket.close() self.client_rtp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.client_rtp_socket.bind(('127.0.0.1', self.client_rtp_port)) # send PLAY request_dict = {'CSeq': str(self.seq), 'Session': self.client_session_id, 'Range': 'npt=%.3f-%.3f' % (self.current_time, self.media_duration)} request = rtsp.generate_request('PLAY', self.url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return self.seq += 1 self.current_time, self.media_duration = util.match_media_time(response) # reset cache file self.file.close() os.remove(self.cache_filename) self.file = open(self.cache_filename, 'wb') # resume self.status = self.PLAY self.play_event.set() self.timer.timeout.connect(self.start_play) self.timer.start(3000)
def stop_media(self): if self.status == self.IDLE: return request_dict = {'CSeq': str(self.seq), 'Session': self.client_session_id} request = rtsp.generate_request('TEARDOWN', self.url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return self.seq += 1 self.destroy_connection()
def play_media(self): if self.status == self.IDLE: # setup and play url = self.urlLineEdit.text() res = self.setup_play(url) if res != -1: # remove cache file if os.path.exists(self.cache_filename): os.remove(self.cache_filename) self.url = url self.client_rtp_thread = threading.Thread(target=self.recv_stream, args=(self.cache_filename,)) self.status = self.PLAY self.play_event = threading.Event() self.client_rtp_thread.start() self.timer.timeout.connect(self.start_play) self.timer.start(3000) elif self.status == self.READY: # send PLAY request_dict = {'CSeq': str(self.seq), 'Session': self.client_session_id, 'Range': 'npt=now-'} request = rtsp.generate_request('PLAY', self.url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return self.seq += 1 # resume self.status = self.PLAY self.play_event.set() self.media_player.play() self.timer.start(1000) else: return
def pause_media(self): if self.status != self.PLAY: return self.media_player.pause() self.timer.stop() self.play_event.clear() self.status = self.READY # send PAUSE request_dict = {'CSeq': str(self.seq), 'Session': self.client_session_id} request = rtsp.generate_request('PAUSE', self.url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return -1 response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return -1 self.seq += 1
def setup_play(self, url): url_tup = util.parse_url(url) if not url_tup: QMessageBox.warning(self, 'Warning', 'Invalid URL.') return -1 ip = url_tup[0] port = int(url_tup[1]) path = url_tup[2] try: self.client_rtsp_socket.connect((ip, port)) except Exception as e: QMessageBox.warning(self, 'Warning', 'Error: connect to media server failed.') return -1 # send OPTIONS request_dict = {'CSeq': str(self.seq)} request = rtsp.generate_request('OPTIONS', url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return -1 response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return -1 self.seq += 1 # send DESCRIBE request_dict = {'CSeq': str(self.seq), 'Accept': 'application/sdp'} request = rtsp.generate_request('DESCRIBE', url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return -1 response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return -1 self.client_rtp_port = util.match_rtp_port(response) if not self.client_rtp_port: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: can not specify RTP port.') return -1 self.client_rtcp_port = self.client_rtp_port+1 self.seq += 1 # setup RTP and RTCP socket self.client_rtp_socket.bind(('127.0.0.1', self.client_rtp_port)) self.client_rtcp_socket.bind(('127.0.0.1', self.client_rtcp_port)) self.status = self.READY # send SETUP request_dict = {'CSeq': str(self.seq), 'Transport': 'RTP/AVP;unicast;client_port=%d-%d' % (self.client_rtp_port, self.client_rtcp_port)} request = rtsp.generate_request('SETUP', url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return -1 response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return -1 self.client_session_id = int(response_dict.get('Session')) self.seq += 1 self.status = self.READY # send PLAY request_dict = {'CSeq': str(self.seq), 'Session': self.client_session_id, 'Range': 'npt=0.000-'} request = rtsp.generate_request('PLAY', url, request_dict) self.client_rtsp_socket.send(request.encode()) response = self.client_rtsp_socket.recv(1024).decode() if rtsp.get_status_code(response) != 200: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response code.') return -1 response_dict = rtsp.get_response_dict(response) if int(response_dict.get('CSeq')) != self.seq: # self.close_rtsp_connection() self.destroy_connection() QMessageBox.warning(self, 'Warning', 'Error: unexpected server response SN.') return -1 self.seq += 1 self.current_time, self.media_duration = util.match_media_time(response) return 0