Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const ErrorHandler = class {
6: 'MPV is already running',
7: 'Could not send IPC message',
8: 'MPV is not running',
9: 'Unsupported protocol'
9: 'Unsupported protocol',
10: 'IPC connection error'
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/ipcInterface/_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const events = {
if(message.length > 0){
const JSONmessage = JSON.parse(message);
// if there was a request_id it was a request message
if(JSONmessage.request_id && JSONmessage.request_id !== 0){
if(JSONmessage.request_id && JSONmessage.request_id !== 0
&& this.ipcRequests[JSONmessage.request_id] !== undefined){
// resolve promise
if(JSONmessage.error === 'success'){
// resolve the request
Expand Down
3 changes: 2 additions & 1 deletion lib/ipcInterface/ipcInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ ipcInterface.prototype = Object.assign({
return new Promise((resolve, reject) => {

// reject the promise if the socket is not running, this is only the case if the mpv player is not running
if (this.socket.destroyed){
// reject also if the socket is not writable
if (this.socket.destroyed || !this.socket.writable){
return reject(this.errorHandler.errorMessage(8, util.getCaller()));
}

Expand Down
8 changes: 8 additions & 0 deletions lib/mpv/_controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const controls = {
let started = false;
// socket to observe if the file is being played back
const observeSocket = new net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'play()'));
});
observeSocket.connect({path: this.options.socket}, async () => {
// this starts the playback when mpv is idle but songs are queued in the playlist
await this.setProperty('playlist-pos', 0);
Expand Down Expand Up @@ -112,6 +116,10 @@ const controls = {
// tracks if the seek event has been emitted
let seek_event_started = false;
const observeSocket = new net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'seek()'));
});
observeSocket.connect({path: this.options.socket}, async () => {
await this.command('seek', [seconds, mode, 'exact']);
}).on('data', (data) => {
Expand Down
4 changes: 4 additions & 0 deletions lib/mpv/_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ const events = {
let timeout = 0;
// promise to watch the socket output
new Promise((resolve, reject) => {
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'messageHandler()'));
});
// connect a tempoary socket to the mpv player
observeSocket.connect({path: this.options.socket}, () =>{
// receive the data
Expand Down
16 changes: 16 additions & 0 deletions lib/mpv/_playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const playlist = {
.then(() => { return new Promise((resolve, reject) => {
// socket to observe the command
let observeSocket = net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'loadPlaylist()'));
});
observeSocket.connect({path: this.options.socket}, () =>{
// send the command to mpv
this.command('loadlist', [playlist, mode]);
Expand Down Expand Up @@ -161,6 +165,10 @@ const playlist = {
return new Promise((resolve, reject) => {
// socket to observe the command
const observeSocket = net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'append()'));
});
observeSocket.connect({path: this.options.socket}, async () =>{
// send the skip command to mpv
await this.command('playlist-next', [mode]);
Expand Down Expand Up @@ -249,6 +257,10 @@ const playlist = {
return new Promise((resolve, reject) => {
// socket to observe the command
const observeSocket = net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'prev()'));
});
observeSocket.connect({path: this.options.socket}, async () =>{
// send the prev command to mpv
await this.command('playlist-prev', [mode]);
Expand Down Expand Up @@ -318,6 +330,10 @@ const playlist = {
return new Promise((resolve, reject) => {
// socket to observe the command
const observeSocket = net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'jump()'));
});
observeSocket.connect({path: this.options.socket}, async () =>{
// send the command to jump in the playlist to mpv
await this.setProperty('playlist-pos', position);
Expand Down
4 changes: 4 additions & 0 deletions lib/mpv/_startStop.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ const startStop = {
// socket to check for the idle event to check if mpv fully loaded and
// actually running
const observeSocket = net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'start()'));
});
observeSocket.connect({path: this.options.socket}, () => {
// send any message to see if there's a MPV instance responding
observeSocket.write(JSON.stringify({
Expand Down
4 changes: 4 additions & 0 deletions lib/mpv/mpv.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ mpv.prototype = Object.assign({
await new Promise ((resolve, reject) => {
// socket to observe the command
const observeSocket = net.Socket();
// catch EventEmitter error to avoid crash if socket is no longer available
observeSocket.on('error', (error) => {
return reject(this.errorHandler.errorMessage(10, error.message, 'load()'));
});
observeSocket.connect({path: this.options.socket}, async () =>{
// send the command to mpv
await this.command('loadfile', options ? [source, mode].concat(util.formatOptions(options)) : [source, mode]);
Expand Down