def play_some_commercials(max_commercials_per_break):
	try:
		comm_source = get_random_commercial()
		comm_player = OMXPlayer(comm_source, args=['--no-osd', '--blank'], dbus_name="omxplayer.player1")
		comm_player.set_video_pos(40,10,660,470);
		comm_player.set_aspect_mode('fill');
		comm_player.hide_video()
		comm_player.pause()
		#comm_player.set_volume(-1)
		comm_i = max_commercials_per_break
		while(comm_i>=0):
			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
			while (1):
				try:
					comm_position = math.floor(comm_player.position())
				except:
					break
			comm_i = comm_i - 1
			sleep(1)
	except:
		print("Error playing commercial from function")
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