-
Notifications
You must be signed in to change notification settings - Fork 0
/
omx-sync.js
135 lines (111 loc) · 3.66 KB
/
omx-sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var fs = require('fs');
var server = require('http').createServer();
var ip = process.argv.slice(2)[0]; //get ip
var socket = require('socket.io-client')('http://' + ip + ':3000');
var io = require('socket.io')(server, {'pingInterval': 10000, 'pingTimeout': 15000});
var dbus = require('dbus-native');
var exec = require('child_process').exec;
var file = process.argv.slice(2)[1]; //get a path to the video argument
var options = '-o hdmi -b --loop --no-osd ';
var currentPosition, totalDuration;
var bus; //main DBUS
var gate = true;
var omx;
server.listen(3000, function() { console.log( 'Listening on port 3000') });
// PARSE TERMINAL INPUT.
if (file == undefined) {
console.log("no video file specified");
return;
}
//kill previous player if the script needs to restart
var killall = exec('killall omxplayer.bin', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
console.log('current video path: ' + file);
//start omx player
omx = exec('omxplayer '+options+file, (error, stdout, stderr) => {
if (error) {
console.error(`omxplayer exec error: ${error}`);
return;
}
console.log(`exec omxplayer stdout: ${stdout}`);
console.log(`exec omxplayer stderr: ${stderr}`);
});
omx.on('exit', function(code){
console.log('EXIT CODE: '+ code +' @ ' + Date() );
// relaunch omxplayer
process.exit(0);
});
//SOCKET.IO HANDLING
io.on('connection', function(socket){
console.log("Listener connected: " + socket.id + ' @ ' + Date());
socket.on('disconnect', function(){
console.log("Listener disconnected: " + socket.id );
});
});
socket.on('connect', function(){
console.log("Connected to the broadcaster as: " + socket.id + ' @ ' + Date() );
});
socket.on('loopFlag', function(loopFlag){
console.log('loop flag recieved @ ' + Date());
seek( s2micro(1) );
setTimeout(function(){
gate = true;
},1000);
});
//DBUS HANDLING
setTimeout(function(){ //wait for dbus to become available.
bus = dbus.sessionBus({
busAddress: fs.readFileSync('/tmp/omxplayerdbus.pi', 'ascii').trim()
});
setTimeout(function(){
bus.invoke({
path: "/org/mpris/MediaPlayer2",
interface: "org.freedesktop.DBus.Properties",
member: "Duration",
destination: "org.mpris.MediaPlayer2.omxplayer",
}, function(err, duration) {
totalDuration = duration; //set to a global
console.log("Duration: " + totalDuration);
});
//send out loop flag
setInterval(function(){
bus.invoke({
path: "/org/mpris/MediaPlayer2",
interface: "org.freedesktop.DBus.Properties",
member: "Position",
destination: "org.mpris.MediaPlayer2.omxplayer",
}, function(err, position) {
currentPosition = position; //set to a global
// console.log("CP: " + currentPosition);
});
if (currentPosition >= totalDuration - s2micro(1) && gate == true) { //are we in the end range of the file?
gate = false;
console.log( "*File Ended @ " + Date() );
io.emit('loopFlag', { loopFlag : 'loop' }); //add one of these above outside the interval loop to reset when the server boots?
}
}, 250);
}, 1000)
}, 1000);
function seek(pos){
bus.invoke({
path: "/org/mpris/MediaPlayer2",
interface: "org.mpris.MediaPlayer2.Player",
member: "SetPosition",
destination: "org.mpris.MediaPlayer2.omxplayer",
signature: "ox",
body: [ '/not/used', pos ]
}, function(err) {
if(err != null){
console.log("ERROR: "+err);
}
});
}
function s2micro(seconds){
return seconds * 1000000;
}