Python Forum
start and stop omxlayer playback
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
start and stop omxlayer playback
#1
Hi,

I'm trying to find an elegant way to start / stop the playback of a video from a cluster of raspberry pi

To playback the video in sync I use omxplayer-sync. I would have preferred to use VLC so I can use external sound cards but I didn't managed to find how to sync 2 vlc instances on the network.

To communicate on the local network the Pis use MQTT.

I'm using paho-mqtt as a MQTT client for my python script and my broker is a mosquito instance running on a master pi.

import paho.mqtt.client as mqtt
import multiprocessing, time, signal
import subprocess
import os

def player(video):
	print("Playing " + video)
	os.system("omxplayer-sync -m" + video)

# p = multiprocessing.Process(target=player, args=["synctest.mp4"])
# p = subprocess.Popen(['omxplayer-sync', '-m', 'synctest.mp4'])

class GracefulExit:
	kill_now = False
	def __init__(self):
		signal.signal(signal.SIGINT, self.exit_gracefully)
		signal.signal(signal.SIGTERM, self.exit_gracefully)

	def exit_gracefully(self,signum, frame):
		self.kill_now = True

def on_connect(client, userdata, flags, rc):
	print("Connected with result code "+str(rc))
	client.subscribe("player/#")

def on_message(client, userdata, msg):
	print(msg.topic+" "+str(msg.payload))
	if msg.topic == "player/PLAY":
		# TODO
		# PLay the video
		
		# proc = subprocess.Popen(['omxplayer-sync', '-m', str(msg.payload.decode())])
		# print("proc: " + proc)
		# print("pid: " + proc.pid)
		# print("Sleeping")
		# time.sleep(5)
		# print("killing pid: " + proc.pid)
		# os.kill(proc.pid, signal.SIGTERM)

	if msg.topic == "player/STOP":
		#TODO
		# Stop the video

		# print("Stopping video")
		# p.send_signal(signal.SIGTERM)
		# p.terminatel()
def main():
	app_killer = GracefulExit()
	while not app_killer.kill_now:
		try:
			time.sleep(0.5)
		except BaseException:
			print("Encountered an exeption.")
			break
	print ("End of the program.")

if __name__ == '__main__':
	print("Starting the program")
	client = mqtt.Client()
	client.on_connect = on_connect
	client.on_message = on_message
	client.connect("localhost", 1883, 60)
	client.loop_start()
	main()
This is my current code as you can see I've tried to initiate the playback of the video in a subprocess so I can kill it when I receive a message in the "stop" subtopic.

The problem that I have is that I can't change the args of the process once created or the terminate() method doesn't stop the video.

If you see a better way to achieve this I'll be more than happy to be educated on this, I'm pretty sure the way I'm doing this isn't really the most elegant solution but this is what I came up with

thanks you very much for your help
Reply


Messages In This Thread
start and stop omxlayer playback - by dezmob - Jan-24-2020, 01:36 PM
RE: start and stop omxlayer playback - by joe_momma - Jan-25-2020, 01:19 AM
RE: start and stop omxlayer playback - by dezmob - Jan-27-2020, 09:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ''.join and start:stop:step notation for lists ringgeest11 2 2,459 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Use Python to start/stop a server service via a webform? oakleaf2001 0 1,773 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  Field with dynamic id not found at playback pythonscripting 0 1,437 Feb-20-2020, 02:43 PM
Last Post: pythonscripting
  how to stop and start a script for 30 seconds laspaul 9 7,707 Jan-16-2020, 02:13 PM
Last Post: laspaul
  No module named Playback ABadLab 1 2,099 Dec-19-2018, 03:44 PM
Last Post: Larz60+
  What's the difference b/w assigning start=None and start=" " Madara 1 2,337 Aug-06-2018, 08:23 AM
Last Post: buran
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 3,373 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  win10 Service(script) start and stop hwa_rang098tkd 0 2,474 Jun-21-2018, 07:42 PM
Last Post: hwa_rang098tkd
  check process status and start if it stop linux inspirer 4 9,135 May-17-2018, 02:52 AM
Last Post: inspirer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020