icanhazscreenid
Posts: 2
Joined: Wed Nov 13, 2013 10:49 pm

Sending keys to omxplayer

Wed Nov 13, 2013 11:16 pm

I think I need to do something with pipes here but I am not familiar with the process. I'm looking for a way to send signals or key presses to a process (specifically omxplayer) and I can't piece it all together. The "TODO" comments below show what I am trying to accomplish. Video playback works. I get valid PIDs. I just don't know how to control omxplayer once the process has started. Any help? Thanks in advance.

Code: Select all

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(void){
	int pidChild;
	int pidParent;

	int pid = fork();
	if(pid == 0){
		pidChild = getpid();
		cout << "Child PID: " << pidChild << endl;
		execlp("/usr/bin/omxplayer", "-r ", "/mnt/disk1/Videos/002.mp4", NULL);

		//TODO: Send 'spacebar' to Omxplayer to pause playback
		cout << "Playback loaded" << endl;

		_exit(0);	
	}
	else{
		pidParent = getpid();
		cout << "Parent PID: " << pidParent << endl;
		//wait();
	} 

	//External events occur here, simulate with sleep
	sleep(5);

	//TODO: Send 'spacebar' to Omxplayer to start playback
	cout << "Playback start" << endl;

	//External events occur here, simulate with sleep
	sleep(5);

	//TODO: Send 'q' or kill omxplayer
	cout << "Playback end" << endl;
}

icanhazscreenid
Posts: 2
Joined: Wed Nov 13, 2013 10:49 pm

Re: Sending keys to omxplayer

Mon Nov 18, 2013 6:27 am

In case anyone ever has the same question, use ptty for this. I found this post amazingly helpful:

http://stackoverflow.com/questions/1168 ... -dup2-in-c

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Sending keys to omxplayer

Mon Nov 18, 2013 8:56 am

Actually, if the program is strictly tty-based (basically, although there are some caveats here, this means standard-IO based), then the canonical answer (and the one you should follow/use) is the tool called "Expect" (Google it).

However, if the program is X/GUI based, then Expect probably won't work (although note that Expect does have the ability to drive xterm - in a cute way described in the book), in which case you will need to use something called "xdotool" (or something like that - I've never used it - it seems to be a stripped down version of WinBatch for X).
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: Sending keys to omxplayer

Mon Nov 18, 2013 9:56 am

There is a pure python version of expect called pexpect from Noah. Works OK in my Pi Presents and in my TBOPlayer

Also look for pyomxplayer, there are two versions one from me using pipes (very newbie attempt) the other from Johannes Baiter using pexpect. Both will probably need modification to catch up with the latest omxplayer.
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

Return to “General discussion”