Commit ed8514e9 authored by Balasse QIIS's avatar Balasse QIIS
Browse files

first commit

parents
*.log
*.DS_Store
/.idea/
The MIT License (MIT)
Copyright (c) 2016 Ap0c
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Node-Omxplayer
A library for controlling the Raspberry Pi [omxplayer](https://github.com/popcornmix/omxplayer) from Node.js.
## Get Started
```js
// Import the module.
var Omx = require('node-omxplayer');
// Create an instance of the player with the source.
var player = Omx('my-video.mp4');
// Control video/audio playback.
player.pause();
player.volUp();
player.quit();
```
**Warning**: If you quit node before quitting the player, there is a chance of a zombie process being created, which will persist until the current audio/video track ends.
## Installation
```
npm install node-omxplayer
```
This module does not require any third party Node.js libraries, but does rely on omxplayer being installed. On the default version of Raspbian it is installed by default, but on the Lite version you will have to install it:
```
sudo apt-get install omxplayer
```
## API
### Omx(*[source]*, *[output]*, *[loop]*, *[initialVolume]*)
The constructor method, used to launch omxplayer with a source.
- `source` (optional): The playback source, any audio or video file (or stream) that omxplayer is capable of playing. If left blank, the player will initialise and wait for a source to be added later with the `newSource` method.
- `output` (optional): The audio output, if left blank will default to 'local', can be one of:
+ local - the analog output (3.5mm jack).
+ hdmi - the HDMI port audio output.
+ both - both of the above outputs.
- `loop` (optional): Loop state, if set to true, will loop file if it is seekable. If left blank will default to false.
**Warning**: As stated above, if you quit node before quitting the player, a zombie process may be created. If this occurs when the loop option is in place, the `omxplayer` process may run indefinitely.
- `initialVolume` (optional): The initial volume, omxplayer will start with this value (in millibels). If left blank will default to 0.
### player.newSource(*source*, *[output]*, *[loop]*, *[initialVolume]*)
Starts playback of a new source, the arguments are identical to those of the `Omx` constructor method described above. If a file is currently playing, ends this playback and begins the new source.
### player.play()
Resumes playback.
### player.pause()
Pauses playback.
### player.volUp()
Increases the volume.
### player.volDown()
Decreases the volume.
### player.fastFwd()
Fast forwards playback.
### player.rewind()
Rewinds playback.
### player.fwd30()
Skips playback forward by 30 seconds.
### player.back30()
Skips playback backward by 30 seconds.
### player.fwd600()
Skips playback forward by 600 seconds.
### player.back600()
Skips playback backward by 600 seconds.
### player.quit()
Quits the player.
### player.subtitles()
Toggle subtitles.
### player.info()
Provides info on the currently playing file.
### player.incSpeed()
Increases playback speed.
### player.decSpeed()
Decreases playback speed.
### player.prevChapter()
Skips to previous chapter.
### player.nextChapter()
Skips to next chapter.
### player.prevAudio()
Skips to previous audio stream.
### player.nextAudio()
Skips to next audio stream.
### player.prevSubtitle()
Skips to previous subtitle stream.
### player.nextSubtitle()
Skips to next subtitle stream.
### player.decSubDelay()
Decrease subtitle delay by 250ms.
### player.incSubDelay()
Increase subtitle delay by 250ms.
### player.running
Boolean giving the playback status, `true` if the player is still active, `false` if it has ended or the player has quit.
## Events
### 'close'
Fired when playback has finished.
### 'error'
Occurs when there is a problem with omxplayer. Includes a message with more information about the error.
## Errors
### 'Output <foo> not allowed.'
Incorrect audio output type passed to the player, see `Omx` in the API section above. Can occur for the `Omx` constructor and the `newSource` method.
### 'Player is closed.'
An attempt has been made to send a command to the player after it has closed. Prevent this from happening by checking if it is still running using the `running` getter method. Can occur for any of the player methods except `newSource`.
'use strict';
// ----- Requires ----- //
let spawn = require('child_process').spawn;
let EventEmitter = require('events');
// ----- Setup ----- //
// The permitted audio outputs, local means via the 3.5mm jack.
let ALLOWED_OUTPUTS = ['hdmi', 'local', 'both'];
// ----- Functions ----- //
// Creates an array of arguments to pass to omxplayer.
function buildArgs(source, givenOutput, loop, initialVolume, showOsd) {
let output = '';
if (givenOutput) {
if (ALLOWED_OUTPUTS.indexOf(givenOutput) === -1) {
throw new Error(`Output ${givenOutput} not allowed.`);
}
output = givenOutput;
} else {
output = 'local';
}
let osd = false;
if (showOsd) {
osd = showOsd;
}
let args = [source, '-o', output, '--blank', osd ? '' : '--no-osd'];
// Handle the loop argument, if provided
if (loop) {
args.push('--loop');
}
// Handle the initial volume argument, if provided
if (Number.isInteger(initialVolume)) {
args.push('--vol', initialVolume);
}
return args;
}
// ----- Omx Class ----- //
function Omx(source, output, loop, initialVolume, showOsd) {
// ----- Local Vars ----- //
let omxplayer = new EventEmitter();
let player = null;
let open = false;
// ----- Local Functions ----- //
// Marks player as closed.
function updateStatus() {
open = false;
omxplayer.emit('close');
}
// Emits an error event, with a given message.
function emitError(message) {
open = false;
omxplayer.emit('error', message);
}
// Spawns the omxplayer process.
function spawnPlayer(src, out, loop, initialVolume, showOsd) {
let args = buildArgs(src, out, loop, initialVolume, showOsd);
console.log('args for omxplayer:', args);
let omxProcess = spawn('omxplayer', args);
open = true;
omxProcess.stdin.setEncoding('utf-8');
omxProcess.on('close', updateStatus);
omxProcess.on('error', () => {
emitError('Problem running omxplayer, is it installed?.');
});
return omxProcess;
}
// Simulates keypress to provide control.
function writeStdin(value) {
if (open) {
player.stdin.write(value);
} else {
throw new Error('Player is closed.');
}
}
// ----- Setup ----- //
if (source) {
player = spawnPlayer(source, output, loop, initialVolume, showOsd);
}
// ----- Methods ----- //
// Restarts omxplayer with a new source.
omxplayer.newSource = (src, out, loop, initialVolume, showOsd) => {
if (open) {
player.on('close', () => {
player = spawnPlayer(src, out, loop, initialVolume, showOsd);
});
player.removeListener('close', updateStatus);
writeStdin('q');
} else {
player = spawnPlayer(src, out, loop, initialVolume, showOsd);
}
};
omxplayer.play = () => {
writeStdin('p');
};
omxplayer.pause = () => {
writeStdin('p');
};
omxplayer.volUp = () => {
writeStdin('+');
};
omxplayer.volDown = () => {
writeStdin('-');
};
omxplayer.fastFwd = () => {
writeStdin('>');
};
omxplayer.rewind = () => {
writeStdin('<');
};
omxplayer.fwd30 = () => {
writeStdin('\u001b[C');
};
omxplayer.back30 = () => {
writeStdin('\u001b[D');
};
omxplayer.fwd600 = () => {
writeStdin('\u001b[A');
};
omxplayer.back600 = () => {
writeStdin('\u001b[B');
};
omxplayer.quit = () => {
writeStdin('q');
};
omxplayer.subtitles = () => {
writeStdin('s');
};
omxplayer.info = () => {
writeStdin('z');
};
omxplayer.incSpeed = () => {
writeStdin('1');
};
omxplayer.decSpeed = () => {
writeStdin('2');
};
omxplayer.prevChapter = () => {
writeStdin('i');
};
omxplayer.nextChapter = () => {
writeStdin('o');
};
omxplayer.prevAudio = () => {
writeStdin('j');
};
omxplayer.nextAudio = () => {
writeStdin('k');
};
omxplayer.prevSubtitle = () => {
writeStdin('n');
};
omxplayer.nextSubtitle = () => {
writeStdin('m');
};
omxplayer.decSubDelay = () => {
writeStdin('d');
};
omxplayer.incSubDelay = () => {
writeStdin('f');
};
Object.defineProperty(omxplayer, 'running', {
get: () => {
return open;
}
});
// ----- Return Object ----- //
return omxplayer;
}
// ----- Module Exports ----- //
module.exports = Omx;
\ No newline at end of file
{
"name": "node-omxplayer",
"version": "0.4.1",
"description": "A library for controlling the Raspberry Pi omxplayer from Node.js.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ap0c",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/Ap0c/node-omxplayer"
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment