Python Forum
start and stop omxlayer playback - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: start and stop omxlayer playback (/thread-23954.html)



start and stop omxlayer playback - dezmob - Jan-24-2020

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


RE: start and stop omxlayer playback - joe_momma - Jan-25-2020

use omxplayer wrapper here
read the docs...


RE: start and stop omxlayer playback - dezmob - Jan-27-2020

(Jan-25-2020, 01:19 AM)joe_momma Wrote: use omxplayer wrapper here
read the docs...

Hi thanks you for your help.

I've tried to find in the API you linked what might be helpful for me, but I'm having a really hard time to see how this wrapper can help me control omxplayer-sync that I'm currently using...

Can you highlight me?