forked from ncb000gt/node-shoutcast
/
shoutcast.js
144 lines (117 loc) · 4.15 KB
/
shoutcast.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
135
136
137
138
139
140
141
142
143
144
var net = require("net")
,fs = require("fs")
,sys = require("sys")
,http = require('http')
,file = require('./lib/file');
var song_queue = {
songs: [],
idx: 0
};
var STATION = {
NAME: 'Node Shoutcasts R Us',
GENRE: 'Musak',
URL: 'http://github.com/ncb000gt/node-shoutcast/',
NOTICE: 'I\'m a little teapot...'
};
var config = require('/usr/local/etc/node-shoutcast/config');
sys.debug('config: ' + JSON.stringify(config));
if ('STATION' in config) {
var config_station = config.STATION;
if ('NAME' in config_station) {
STATION.NAME = config_station.NAME;
} else if ('GENRE' in config_station) {
STATION.GENRE = config_station.GENRE;
} else if ('URL' in config_station) {
STATION.URL = config_station.URL;
} else if ('NOTICE' in config_station) {
STATION.NOTICE = config_station.NOTICE;
}
}
var PLAYLIST = config.PLAYLIST;
var QUEUE_SIZE = config.QUEUE_SIZE || 10;
sys.debug('PLAYLIST: ' + PLAYLIST);
var stats = fs.statSync(PLAYLIST);
if (stats.isDirectory()) {
file.walkSync(PLAYLIST, function(p, dirs, files) {
var len = files.length;
for (var i = 0; i < len; i++) {
var file = files[i];
if (file.indexOf('.mp3') >= 0) {
song_queue.songs.push(p+'/'+file);
}
if (song_queue.songs.length > QUEUE_SIZE) {
break;
}
}
});
}
var headers = {
'icy-notice1': STATION.NOTICE,
'icy-notice2': "NodeJS Streaming Shoutcast Server/v0.1",
'icy-name': STATION.NAME,
'icy-genre': STATION.GENRE,
'icy-url': STATION.URL,
'Content-Type': 'audio/mpegurl',
'icy-pub':'0',
'icy-br':'56',
'icy-metaint': '0'//1024'
};
var currentSong = null;
var filetoread = "/home/ncampbell/Music/Amazon MP3/David Guetta/One Love (Deluxe Version)/Memories (Feat Kid Cudi).mp3";
var bytesOut = 0;
http.createServer(
function (req, res) {
var o = req.headers;
for (var p in o) {
sys.puts(p + ': ' + o[p]);
}
sys.puts('Starting stream...');
res.writeHead(200, headers);
//TODO: pop song and make read song as a stream separate from the request, requests should just tie into the already running stream
var song = song_queue.songs[song_queue.idx];
sys.debug('song: ' + song);
var fStream = fs.createReadStream(song, {bufferSize:1024});
pump(fStream, res, function() { sys.puts("No more data. Closing."); });
}).listen(7000);
setInterval(function() {
sys.debug('Total Bytes Written: ' + bytesOut);
}, 3000);
sys.puts('Server running at http://0.0.0.0:7000/');
function pump(readStream, writeStream, callback) {
var callbackCalled = false;
function call (a, b, c) {
if (callback && !callbackCalled) {
callback(a, b, c);
callbackCalled = true;
}
}
if (!readStream.pause) readStream.pause = function () {readStream.emit("pause")};
if (!readStream.resume) readStream.resume = function () {readStream.emit("resume")};
readStream.addListener("data", function (chunk) {
bytesOut+=chunk.length;
if (writeStream.write(chunk) === false) readStream.pause();
});
writeStream.addListener("pause", function () {
readStream.pause();
});
writeStream.addListener("drain", function () {
readStream.resume();
});
writeStream.addListener("resume", function () {
readStream.resume();
});
readStream.addListener("end", function () {
writeStream.end();
});
readStream.addListener("close", function () {
call();
});
readStream.addListener("error", function (err) {
writeStream.end();
call(err);
});
writeStream.addListener("error", function (err) {
readStream.destroy();
call(err);
});
};