Skip to content

Commit

Permalink
Added the seek event to version1 as well. Closes #23 entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
j-holub committed Aug 23, 2017
1 parent 38fd038 commit f44b07c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lib/mpv/mpv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Child Process to module to start mpv player
var spawn = require('child_process').spawn;
var exec = require('child_process').execSync;
// Socket
var net = require('net');
// EventEmitter
var eventEmitter = require('events').EventEmitter;

Expand Down Expand Up @@ -172,6 +174,66 @@ function mpv(options, mpv_args){
// emit unpaused event
this.emit("resumed");
break;
case "seek":
if(this.options.verbose){console.log("Event: seek")};
// socket to watch for the change after a seek has happened
let observeSocket = new net.Socket();
// startseek position
let seekstarttimepos = this.currentTimePos;
// timeout tracker
let timeout = 0;
// promise to watch the socket output
new Promise((resolve, reject) => {
// connect a tempoary socket to the mpv player
observeSocket.connect({path: this.options.socket}, () =>{
// receive the data
observeSocket.on('data', (data) => {
// increase timeout
timeout += 1;
// console.log(data.toJSON());
let messages = data.toString('utf-8').split('\n');
// check every message
messages.forEach((message) => {
// ignore empty messages
if(message.length > 0){
message = JSON.parse(message);
if("event" in message){
// after the seek is finished the playback-restart event is emitted
if(message.event === "playback-restart"){
// resolve the promise
resolve({
"start": seekstarttimepos,
"end": this.currentTimePos
});
}
// when the track has changed we don't need a seek event
else if (message.event === "tracks-changed"){
reject("Track changed after seek");
}
}
}
});
// reject the promise if it took to long until the playback-restart happens
// to prevent having sockets listening forever
if(timeout > 10){
reject("Seek event timeout");
}
});
});
})
// socket destruction and event emittion
.then((times) => {
observeSocket.destroy();
this.emit('seek', (times));
})
// handle any rejection of the promise
.catch((status) => {
observeSocket.destroy();
if(this.options.debug){
console.log(status);
}
});
break;
// observed properties
case "property-change":
// time position events are handled seperately
Expand Down

0 comments on commit f44b07c

Please sign in to comment.