proper stream buffering behavior on paused audio
This commit is contained in:
parent
ae8de85c07
commit
90792a3a4f
2 changed files with 10 additions and 34 deletions
|
|
@ -17,8 +17,6 @@ var levelup = require('level');
|
|||
|
||||
module.exports = GrooveBasin;
|
||||
|
||||
var CONFIG_KEY_PREFIX = "Config.";
|
||||
|
||||
var defaultConfig = {
|
||||
host: '0.0.0.0',
|
||||
port: 16242,
|
||||
|
|
@ -139,33 +137,6 @@ GrooveBasin.prototype.start = function() {
|
|||
|
||||
}
|
||||
|
||||
GrooveBasin.prototype.restoreState = function(cb) {
|
||||
var self = this;
|
||||
|
||||
// override with values from db
|
||||
var stream = self.db.createReadStream({
|
||||
start: CONFIG_KEY_PREFIX,
|
||||
});
|
||||
stream.on('data', function(data) {
|
||||
if (data.key.indexOf(CONFIG_KEY_PREFIX) !== 0) {
|
||||
stream.removeAllListeners();
|
||||
stream.destroy();
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
var varName = data.key.substring(CONFIG_KEY_PREFIX.length);
|
||||
self.config[varName] = JSON.parse(data.value);
|
||||
});
|
||||
stream.on('error', function(err) {
|
||||
stream.removeAllListeners();
|
||||
stream.destroy();
|
||||
cb(err);
|
||||
});
|
||||
stream.on('close', function() {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
GrooveBasin.prototype.startServer = function() {
|
||||
var self = this;
|
||||
|
||||
|
|
|
|||
|
|
@ -86,6 +86,7 @@ function Player(db, musicDirectory) {
|
|||
this.encodedAudioStream = new PassThrough();
|
||||
this.lastEncodeItem = null;
|
||||
this.lastEncodePos = null;
|
||||
this.expectHeaders = true;
|
||||
}
|
||||
|
||||
Player.prototype.initialize = function(cb) {
|
||||
|
|
@ -102,6 +103,7 @@ Player.prototype.initialize = function(cb) {
|
|||
var grooveEncoder = groove.createEncoder();
|
||||
grooveEncoder.formatShortName = "ogg";
|
||||
grooveEncoder.codecShortName = "vorbis";
|
||||
grooveEncoder.bitRate = 256 * 1000;
|
||||
|
||||
var pend = new Pend();
|
||||
pend.go(function(cb) {
|
||||
|
|
@ -137,6 +139,7 @@ Player.prototype.initialize = function(cb) {
|
|||
if (buf.buffer) {
|
||||
if (buf.item) {
|
||||
if (self.expectHeaders) {
|
||||
console.log("encoder: got first non-header");
|
||||
self.headerBuffers = self.newHeaderBuffers;
|
||||
self.newHeaderBuffers = [];
|
||||
self.expectHeaders = false;
|
||||
|
|
@ -146,6 +149,7 @@ Player.prototype.initialize = function(cb) {
|
|||
self.lastEncodePos = buf.pos;
|
||||
} else if (self.expectHeaders) {
|
||||
// this is a header
|
||||
console.log("encoder: got header");
|
||||
self.newHeaderBuffers.push(buf.buffer);
|
||||
} else {
|
||||
// it's a footer, ignore the fuck out of it
|
||||
|
|
@ -153,6 +157,7 @@ Player.prototype.initialize = function(cb) {
|
|||
}
|
||||
} else {
|
||||
// end of playlist sentinel
|
||||
console.log("encoder: end of playlist sentinel");
|
||||
self.expectHeaders = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -267,7 +272,8 @@ Player.prototype.secondsIntoFuture = function(groovePlaylistItem, pos) {
|
|||
var item = this.grooveItems[groovePlaylistItem.id];
|
||||
|
||||
if (item === this.currentTrack) {
|
||||
var curPos = (new Date() - this.trackStartDate) / 1000.0;
|
||||
var curPos = this.isPlaying ?
|
||||
((new Date() - this.trackStartDate) / 1000.0) : this.pausedTime;
|
||||
return pos - curPos;
|
||||
} else {
|
||||
return pos;
|
||||
|
|
@ -281,9 +287,12 @@ Player.prototype.streamMiddleware = function(req, resp, next) {
|
|||
resp.setHeader('Content-Type', 'audio/vorbis');
|
||||
resp.statusCode = 200;
|
||||
|
||||
var count = 0;
|
||||
self.headerBuffers.forEach(function(headerBuffer) {
|
||||
count += headerBuffer.length;
|
||||
resp.write(headerBuffer);
|
||||
});
|
||||
console.log("sent", count, "bytes of headers");
|
||||
self.encodedAudioStream.pipe(resp);
|
||||
req.on('abort', function() {
|
||||
self.encodedAudioStream.unpipe(resp);
|
||||
|
|
@ -291,10 +300,6 @@ Player.prototype.streamMiddleware = function(req, resp, next) {
|
|||
});
|
||||
};
|
||||
|
||||
Player.prototype.stopStreamListening = function(cb) {
|
||||
this.streamServer.close(cb);
|
||||
};
|
||||
|
||||
Player.prototype.deleteFile = function(key) {
|
||||
var self = this;
|
||||
var dbFile = self.libraryIndex.trackTable[key];
|
||||
|
|
|
|||
Loading…
Reference in a new issue