2
2
const supportedPlatform = [ "Linux" ] ;
3
3
const { spawn } = require ( "child_process" ) ;
4
4
const openradio = require ( "../" ) ;
5
+ const daemon = require ( "./daemon" ) ;
6
+ const fs = require ( "fs" ) ;
5
7
const http = require ( "http" ) ;
6
8
const server = http . createServer ( ) ;
7
9
const argv = process . argv . slice ( 2 ) ;
@@ -21,7 +23,8 @@ const config = {
21
23
address : "0.0.0.0"
22
24
} ,
23
25
parec_path : process . env . PREFIX + "/bin/parec" ,
24
- log : 0
26
+ log : 0 ,
27
+ daemon : 1
25
28
}
26
29
27
30
if ( ! supportedPlatform . includes ( require ( "os" ) . type ( ) ) && ! [ "--force" , "-force" , "-f" ] . includes ( argv [ 0 ] ) ) {
@@ -30,9 +33,9 @@ if (!supportedPlatform.includes(require("os").type()) && !["--force", "-force",
30
33
process . exit ( 1 ) ;
31
34
}
32
35
33
- console . log ( "\nOpenradio Pulseaudio - v1.0 " ) ;
36
+ console . log ( "\nOpenradio Pulseaudio - v1.1 " ) ;
34
37
35
- argv . forEach ( ( key , index ) => {
38
+ argv . forEach ( async ( key , index ) => {
36
39
let value = argv [ index + 1 ] ;
37
40
if ( [ "--port" , "-port" , "-p" ] . includes ( key ) ) {
38
41
if ( isNaN ( value ) ) return console . error ( "Usage: openradio-pulse --port [Port Number]" ) ;
@@ -45,6 +48,9 @@ argv.forEach((key, index) => {
45
48
console . log ( " --parec-path [Path] - Path to parec binary (Default: $PREFIX/bin/parec)" ) ;
46
49
console . log ( " --help - Show this" ) ;
47
50
console . log ( " --log - Log every HTTP Traffic" ) ;
51
+ console . log ( "\nDaemonize Service:\n" ) ;
52
+ console . log ( " --daemon - Do not run as Daemonize Service" ) ;
53
+ console . log ( " --kill - Kill Daemonize service" ) ;
48
54
console . log ( "\nAudio Input Options:\n" ) ;
49
55
console . log ( " --input-samplerate [num] - Input Samplerate (Default: 44100)" ) ;
50
56
console . log ( " --input-channels [num] - Input Channels (Default: 2)" ) ;
@@ -77,10 +83,27 @@ argv.forEach((key, index) => {
77
83
config . server . address = value ;
78
84
} else if ( [ "--log" , "-log" , "-l" , "-verbose" , "--verbose" , "-v" ] . includes ( key ) ) {
79
85
config . log = 1 ;
86
+ } else if ( [ "--daemon" , "-daemon" , "-d" ] . includes ( key ) ) {
87
+ config . daemon = 0 ;
88
+ } else if ( [ "--kill" , "-kill" , "-k" ] . includes ( key ) ) {
89
+ try {
90
+ let daemons = JSON . parse ( fs . readFileSync ( process . env . TMPDIR + "/openradio-pulse-daemon.json" ) ) ;
91
+ console . log ( "Killing process " + daemons . pid + "...." ) ;
92
+ process . kill ( daemons . pid , 'SIGINT' ) ;
93
+ fs . rmSync ( process . env . TMPDIR + "/openradio-pulse-daemon.json" ) ;
94
+ } catch ( error ) {
95
+ if ( error . code === "ENOENT" ) {
96
+ console . error ( "No daemon was running or already killed.\nYou may should kill it manually by ran \"pkill -9 node\" if it's still running." ) ;
97
+ return process . exit ( 1 ) ;
98
+ }
99
+ console . error ( error ) ;
100
+ return process . exit ( 1 ) ;
101
+ }
102
+ process . exit ( ) ;
80
103
}
81
104
} ) ;
82
105
83
- server . on ( 'error' , console . error ) ;
106
+ server . on ( 'error' , err => console . error ( `[ ${ Date ( ) } ]` , err ) ) ;
84
107
server . on ( 'request' , ( req , res ) => {
85
108
let id = Math . random ( ) ;
86
109
let address = req . socket . address ( ) ;
@@ -105,22 +128,31 @@ console.log("\n- HTTP Server");
105
128
console . log ( " Address :" , config . server . address ) ;
106
129
console . log ( " Port :" , config . server . port ) ;
107
130
console . log ( "\nparec Binary path:" , config . parec_path ) ;
131
+ if ( config . daemon ) daemon ( ) ;
108
132
console . log ( "Log Incomming Traffic:" , config . log ? "Yes" : "No" ) ;
109
-
110
133
process . stdout . write ( `\n[${ Date ( ) } ] Launching Server.... ` ) ;
111
134
112
135
let listener = server . listen ( config . server . port , config . server . address , ( ) => {
113
136
process . stdout . write ( "Done" ) ;
114
137
console . log ( `\n[${ Date ( ) } ]` , "Now listening on port" , listener . address ( ) . port ) ;
115
138
116
139
let radio = openradio ( config . output ) ;
117
- radio . playPCM ( spawn ( config . parec_path , config . input ) . stdout ) ;
140
+ function play ( ) {
141
+ let parec = spawn ( config . parec_path , config . input ) ;
142
+ parec . on ( 'close' , play ) ;
143
+ parec . on ( 'error' , err => console . error ( `[${ Date ( ) } ]` , err ) ) ;
144
+ parec . stderr . pipe ( process . stderr ) ;
145
+ radio . playPCM ( parec . stdout , config . input ) ;
146
+ }
147
+
118
148
radio . on ( 'data' , chunk => {
119
149
sink . forEach ( ( res , id ) => {
120
150
res . write ( chunk , err => {
121
151
if ( err ) sink . delete ( id ) ;
122
152
} ) ;
123
153
} ) ;
124
154
} ) ;
155
+
156
+ play ( ) ;
125
157
} ) ;
126
158
0 commit comments