Exemple #1
0
class Player:
    def __init__(self, appli):
        self.currVideo = None
        self.formatId = 0
        self.subtitleId = -1
        self.audioStreamId = 0
        self.omxProcess = None
        self.currTotViewed = 0
        self.lastPos = 0
        self.appli = appli
        self.wasPlaying = False

    def LoadVideo(self, video):
        if self.isStarted():
            self.stop()
        self.wasPlaying = False
        self.currVideo = video
        self.formatId = 0
        self.subtitleId = -1
        self.audioStreamId = 0
        self.currTotViewed = 0
        self.lastPos = 0

    # Set the format of the video
    def setVideoFormat(self, formatId):
        if self.currVideo and formatId != self.formatId:
            # If the video has this format
            if self.currVideo.getFormat(formatId):
                oldFormat = self.formatId
                self.formatId = formatId
                if self.isStarted():
                    self.wasPlaying = False
                    oldPos = self.getPosition()
                    wasPlaying = self.isPlaying()
                    # Try to play the new format but fallback on the previous one if it fails
                    if self.tryPlayingFormat(
                            formatId) or self.tryPlayingFormat(oldFormat):
                        self.setPosition(oldPos)
                        if not wasPlaying:
                            self.playPause()
                    else:
                        return False
                return True
        return False

    # Set a different audio stream
    def setAudioFormat(self, formatId):
        try:
            if self.isStarted():
                if self.omxProcess.select_audio(formatId):
                    self.audioStreamId = formatId
                    return True
                else:
                    return False
            else:
                return False
        except:
            self.clearPlayer()
            return False

    # Set a subtitle track
    def setSubtitlesFormat(self, formatId):
        try:
            if self.isStarted():
                if formatId > -1:
                    self.omxProcess.show_subtitles()
                    if self.omxProcess.select_subtitle(formatId):
                        self.subtitleId = formatId
                        return True
                    else:
                        return False
                else:
                    self.subtitleId = -1
                    return self.omxProcess.hide_subtitles()
            else:
                return False
        except:
            self.clearPlayer()
            return False

    # Tries to play or pause the current video
    def playPause(self):
        if self.currVideo:
            if self.isStarted():
                try:
                    if self.isPlaying():
                        self.omxProcess.pause()
                    else:
                        self.omxProcess.play()
                    return True
                except:
                    self.clearPlayer()
                    return False
            else:
                ok = False
                if self.formatId != 0:
                    ok = self.tryPlayingFormat(self.formatId)
                if not ok:
                    # Try to play formats starting from the highest resolution one
                    for fid in range(1, len(self.currVideo.getFormatList())):
                        if self.tryPlayingFormat(fid):
                            ok = True
                            break
                return ok
        return False

    def stop(self):
        self.wasPlaying = False
        try:
            if self.omxProcess:
                self.omxProcess.quit()
                self.omxProcess = None
                return True
            return False
        except:
            self.clearPlayer()
            return False

    # Tries to play a given format of the video
    def tryPlayingFormat(self, formatId):
        if self.isStarted():
            self.stop()
        try:
            self.formatId = formatId
            print('Trying to play ', formatId, 'path:',
                  self.currVideo.getRessourcePath(formatId))
            self.omxProcess = OMXPlayer(
                self.currVideo.getRessourcePath(formatId), args=['-b'])
            # Wait a bit for loading before disqualifying the format
            while self.omxProcess.playback_status() == 'Paused':
                sleep(0.01)
            if self.isPlaying():
                print('isplaying:True')
                return True
        except Exception as e:
            self.clearPlayer()
            print(str(e), str(self.currVideo.getFormatList()))
        # Handle the case in which the format couldn't be played
        self.stop()
        self.formatId = 0
        return False

    def isPlaying(self):
        try:
            return self.omxProcess and self.omxProcess.is_playing()
        except:
            self.clearPlayer()
            return False

    def isStarted(self):
        try:
            return self.omxProcess and self.omxProcess.can_play()
        except:
            self.clearPlayer()
            return False

    def isPaused(self):
        return self.isStarted() and not self.isPlaying()

    def getPosition(self):
        try:
            if self.isStarted():
                return self.omxProcess.position()
            else:
                return 0
        except:
            self.clearPlayer()
            return 0

    def getDuration(self):
        try:
            if self.isStarted():
                return int(self.omxProcess.duration())
            elif self.currVideo:
                return self.currVideo.duration
            else:
                return 1
        except:
            self.clearPlayer()
            return 1

    def getSubtitles(self):
        subs = {-1: 'None'}
        try:
            if self.isStarted():
                for substr in self.omxProcess.list_subtitles():
                    idx, lng, name, cdc, actv = substr.split(':')
                    subs[idx] = lng + (('-' + name) if len(name) > 0 else '')
                    if actv:
                        self.subtitleId = idx
            return subs
        except:
            self.clearPlayer()
            return subs

    def hasSubtitles(self):
        try:
            return self.isStarted() and len(
                self.omxProcess.list_subtitles()) > 0
        except:
            self.clearPlayer()
            return False

    def getAudioStreams(self):
        auds = {}
        try:
            if self.isStarted():
                for audstr in self.omxProcess.list_audio():
                    idx, lng, name, cdc, actv = audstr.split(':')
                    auds[idx] = lng + (('-' + name) if len(name) > 0 else '')
                    if actv:
                        self.audioStreamId = idx
            return auds
        except:
            self.clearPlayer()
            return auds

    def hasAudioStreams(self):
        try:
            return self.isStarted() and len(self.omxProcess.list_audio()) > 1
        except:
            self.clearPlayer()
            return False

    def hasVideoStreams(self):
        if self.currVideo:
            okFormatList = json.loads(self.currVideo.okFormatsList)
            return len(okFormatList) > 2
        else:
            return False

    def getStatus(self):
        isPlaying = self.isPlaying()
        isPaused = self.isPaused()
        currPos = self.getPosition()
        dur = self.getDuration()
        if isPlaying or isPaused:
            self.wasPlaying = True
            self.currTotViewed += currPos - self.lastPos
            self.lastPos = currPos
            with self.appli.threadLock:
                if self.currTotViewed / dur > Parameters.get().viewedThreshold:
                    self.currVideo.viewed = True
                    self.currVideo.save()
                    self.appli.updatePart('playlist')
        else:
            with self.appli.threadLock:
                pt = Parameters.get().autoRestartPosThresh
            if self.wasPlaying and pt < self.lastPos < self.getDuration() - pt:
                self.tryPlayingFormat(self.formatId)
                self.setPosition(self.lastPos)
        return {
            'position': currPos,
            'duration': dur,
            'isPlaying': isPlaying,
            'isPaused': isPaused
        }

    def setPosition(self, newPos):
        try:
            # If the video is not started, start it and jump to the position
            if self.isStarted() or self.playPause():
                self.omxProcess.set_position(newPos)
                return True
            else:
                return False
        except:
            self.clearPlayer()
            return False

    def getFormatList(self):
        return self.currVideo.getFormatList() if self.currVideo else []

    def getFormatListItems(self):
        return [(fid, f['name']) for fid, f in enumerate(self.getFormatList())]

    def clearPlayer(self):
        if self.omxProcess:
            try:
                self.omxProcess.quit()
            except:
                pass
        self.omxProcess = None
def play_video(source, commercials, max_commercials_per_break):
	if source==None:
		return
	#os.system("killall -9 omxplayer");
	try:
		global next_video_to_play
		global last_video_played
		
		err_pos = -1.0
		
		if next_video_to_play=='':
			print("Last video played: " + last_video_played)
			urlcontents = urllib2.urlopen("http://127.0.0.1/?getshowname=" + urllib.quote_plus(source)).read()
			print("Response: ")
			print(urlcontents)
			acontents = urlcontents.split("|")
			#if last_video_played == contents and contents!="News":
			if acontents[1]!="0" and acontents[0]!="News":
				print("Just played this show, skipping")
				return
			last_video_played = acontents[0]
			print("Last video played: " + last_video_played)
		
		next_video_to_play=''
		
		err_pos = 0.0

		comm_source = get_random_commercial()
		err_pos = 0.1
		comm_player = OMXPlayer(comm_source, args=['--no-osd', '--blank'], dbus_name="omxplayer.player1")
		err_pos = 0.12
		comm_player.set_video_pos(40,10,660,470);
		comm_player.set_aspect_mode('fill');
		err_pos = 0.13
		comm_player.hide_video()
		err_pos = 0.14
		comm_player.pause()
		err_pos = 0.15
		#comm_player.set_volume(1)
		err_pos = 0.2
		
		print('Main video file:' + source)
		err_pos = 0.21
		contents = urllib2.urlopen("http://127.0.0.1/?current_video=" + urllib.quote_plus(source)).read()
		err_pos = 0.23
		player = OMXPlayer(source, args=['--no-osd', '--blank'], dbus_name="omxplayer.player0")
		err_pos = 0.3
		print('Boosting Volume by ' + str(get_volume(source)) + 'db')
		err_pos = 0.31
		player.set_video_pos(40,10,660,470);
		player.set_aspect_mode('fill');
		err_pos = 0.32
		#player.set_volume(get_volume(source))
		err_pos = 0.33
		sleep(1)
		err_pos = 1.0
		player.pause()
		err_pos = 1.1
		strSubtitles = player.list_subtitles()
		if(strSubtitles):
			player.hide_subtitles()
		err_pos = 1.2
		player.play()
		err_pos = 1.3
		lt = 0
		while (1):
			err_pos = 2.0
			try:
				position = player.position()
			except:
				break

			if check_video()==True:
				#check if an outside source wants to play a new video
				print('outside source video return')
				err_pos = 8.0
				player.hide_video()
				player.quit()
				comm_player.hide_video()
				comm_player.quit()
				sleep(0.5)
				return
			
			if len(commercials) > 0:
				#found a commercial break, play some commercials
				if math.floor(position)==commercials[0]:
					commercials.pop(0)
					player.hide_video()
					player.pause()
					sleep(0.5)
					comm_i = max_commercials_per_break
					err_pos = 3.0
					while(comm_i>=0):
						if check_video()==True or next_video_to_play!='':
							#check if an outside source wants to play a new video
							print('outside source video return')
							err_pos = 6.0
							player.hide_video()
							player.quit()
							comm_player.hide_video()
							comm_player.quit()
							sleep(0.5)
							return
					
						comm_source = get_random_commercial()
						print('Playing commercial #' + str(comm_i), comm_source)
						contents = urllib2.urlopen("http://127.0.0.1/?current_comm=" + urllib.quote_plus(comm_source)).read()
						comm_player.load(comm_source)
						comm_player.pause()
						sleep(0.1)
						if comm_i==4:
							comm_player.show_video()
							
						comm_player.play()
						err_pos = 4.0
						while (1):
							try:
								comm_position = math.floor(comm_player.position())
							except:
								break
						comm_i = comm_i - 1
						sleep(1)
					
					err_pos = 5.0
					player.show_video()
					player.play()

		err_pos = 7.0
		player.hide_video()
		sleep(0.5)
	except Exception as e:
		if(err_pos!=7.0):
			contents = urllib2.urlopen("http://127.0.0.1/?error=MAIN_" + str(err_pos) + "_" + urllib.quote_plus(str(e))).read()
		print("error main " + str(e))

	try:
		comm_player.quit()
	except Exception as ex:
		#contents = urllib2.urlopen("http://127.0.0.1/?error=COMMERCIAL_" + str(err_pos) + "_" + urllib.quote_plus(str(ex))).read()
		print("error comm quit " + str(ex))
	try:
		player.quit()
	except Exception as exx:
		#contents = urllib2.urlopen("http://127.0.0.1/?error=PLAYER_" + str(err_pos) + "_" + urllib.quote_plus(str(exx))).read()
		print("error player quit " + str(exx))
	
	return