-
Notifications
You must be signed in to change notification settings - Fork 5
/
_functions.php
344 lines (286 loc) · 12.5 KB
/
_functions.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
function redirect($url, $time) {
echo "<script>
window.setTimeout(function(){
window.location.href = '" . $url . "';
}, " . $time . ");
</script>";
}
if (isset($_GET['logout'])) {
session_destroy();
header("location: index.php");
}
function logincheck() {
if (!isset($_SESSION['user_id'])) {
header("location: index.php");
}
}
function checkPid($pid) {
exec("ps $pid", $output, $result);
return count($output) >= 2 ? true : false;
}
function stop_stream($id) {
$stream = Stream::find($id);
$setting = Setting::first();
if (checkPid($stream->pid)) {
shell_exec("kill -9 " . $stream->pid);
shell_exec("/bin/rm -r /home/fos-streaming/fos/www/" . $setting->hlsfolder . "/" . $stream->id . "*");
}
$stream->pid = "";
$stream->running = 0;
$stream->status = 0;
$stream->save();
sleep(2);
}
function getTranscode($id, $streamnumber = null) {
$stream = Stream::find($id);
$setting = Setting::first();
$trans = $stream->transcode;
$ffmpeg = $setting->ffmpeg_path;
$url = $stream->streamurl;
if ($streamnumber == 2) {
$url = $stream->streamurl2;
}
if ($streamnumber == 3) {
$url = $stream->streamurl3;
}
$endofffmpeg = "";
$endofffmpeg .= $stream->bitstreamfilter ? ' -bsf h264_mp4toannexb' : '';
$endofffmpeg .= ' -hls_flags delete_segments -hls_time 10';
$endofffmpeg .= ' -hls_list_size 8 /home/fos-streaming/fos/www/' . $setting->hlsfolder . '/' . $stream->id . '_.m3u8 > /dev/null 2>/dev/null & echo $! ';
if ($trans) {
$ffmpeg .= ' -y';
$ffmpeg .= ' -probesize ' . ($trans->probesize ? $trans->probesize : '10000000');
$ffmpeg .= ' -analyzeduration ' . ($trans->analyzeduration ? $trans->analyzeduration : '5000000');
$ffmpeg .= ' -i ' . '"' . "$url" . '"';
$ffmpeg .= ' -user_agent "' . ($setting->user_agent ? $setting->user_agent : 'FOS-Streaming') . '"';
$ffmpeg .= $trans->scale ? ' -vf scale=' . ($trans->scale ? $trans->scale : '') : '';
$ffmpeg .= $trans->audio_codec ? ' -acodec ' . $trans->audio_codec : '';
$ffmpeg .= $trans->video_codec ? ' -vcodec ' . $trans->video_codec : '';
$ffmpeg .= $trans->profile ? ' -profile:v ' . $trans->profile : '';
$ffmpeg .= $trans->preset ? ' -preset ' . $trans->preset_values : '';
$ffmpeg .= $trans->video_bitrate ? ' -b:v ' . $trans->video_bitrate . 'k' : '';
$ffmpeg .= $trans->audio_bitrate ? ' -b:a ' . $trans->audio_bitrate . 'k' : '';
$ffmpeg .= $trans->fps ? ' -r ' . $trans->fps : '';
$ffmpeg .= $trans->minrate ? ' -minrate ' . $trans->minrate . 'k' : '';
$ffmpeg .= $trans->maxrate ? ' -maxrate ' . $trans->maxrate . 'k' : '';
$ffmpeg .= $trans->bufsize ? ' -bufsize ' . $trans->bufsize . 'k' : '';
$ffmpeg .= $trans->aspect_ratio ? ' -aspect ' . $trans->aspect_ratio : '';
$ffmpeg .= $trans->audio_sampling_rate ? ' -ar ' . $trans->audio_sampling_rate : '';
$ffmpeg .= $trans->crf ? ' -crf ' . $trans->crf : '';
$ffmpeg .= $trans->audio_channel ? ' -ac ' . $trans->audio_channel : '';
$ffmpeg .= $stream->bitstreamfilter ? ' -bsf h264_mp4toannexb' : '';
$ffmpeg .= $trans->threads ? ' -threads ' . $trans->threads : '';
$ffmpeg .= $trans->deinterlance ? ' -vf yadif=0:-1:0' : '';
$ffmpeg .= $endofffmpeg;
return $ffmpeg;
}
$ffmpeg .= ' -probesize 10000000 -analyzeduration 5000000 -i "' . $url . '"';
$ffmpeg .= ' -user_agent "' . ($setting->user_agent ? $setting->user_agent : 'FOS-Streaming') . '"';
$ffmpeg .= ' -c copy -c:a libfaac -b:a 128k';
$ffmpeg .= $endofffmpeg;
return $ffmpeg;
}
function getTranscodedata($id) {
$trans = Transcode::find($id);
$setting = Setting::first();
$ffmpeg = "ffmpeg";
$ffmpeg .= ' -y';
$ffmpeg .= ' -probesize ' . ($trans->probesize ? $trans->probesize : '10000000');
$ffmpeg .= ' -analyzeduration ' . ($trans->analyzeduration ? $trans->analyzeduration : '5000000');
$ffmpeg .= ' -i ' . '"' . "[input]" . '"';
$ffmpeg .= ' -user_agent "' . ($setting->user_agent ? $setting->user_agent : 'FOS-Streaming') . '"';
$ffmpeg .= $trans->scale ? ' -vf scale=' . ($trans->scale ? $trans->scale : '') : '';
$ffmpeg .= $trans->audio_codec ? ' -acodec ' . $trans->audio_codec : '';
$ffmpeg .= $trans->video_codec ? ' -vcodec ' . $trans->video_codec : '';
$ffmpeg .= $trans->profile ? ' -profile:v ' . $trans->profile : '';
$ffmpeg .= $trans->preset ? ' -preset ' . $trans->preset_values : '';
$ffmpeg .= $trans->video_bitrate ? ' -b:v ' . $trans->video_bitrate . 'k' : '';
$ffmpeg .= $trans->audio_bitrate ? ' -b:a ' . $trans->audio_bitrate . 'k' : '';
$ffmpeg .= $trans->fps ? ' -r ' . $trans->fps : '';
$ffmpeg .= $trans->minrate ? ' -minrate ' . $trans->minrate . 'k' : '';
$ffmpeg .= $trans->maxrate ? ' -maxrate ' . $trans->maxrate . 'k' : '';
$ffmpeg .= $trans->bufsize ? ' -bufsize ' . $trans->bufsize . 'k' : '';
$ffmpeg .= $trans->aspect_ratio ? ' -aspect ' . $trans->aspect_ratio : '';
$ffmpeg .= $trans->audio_sampling_rate ? ' -ar ' . $trans->audio_sampling_rate : '';
$ffmpeg .= $trans->crf ? ' -crf ' . $trans->crf : '';
$ffmpeg .= $trans->audio_channel ? ' -ac ' . $trans->audio_channel : '';
$ffmpeg .= $trans->threads ? ' -threads ' . $trans->threads : '';
$ffmpeg .= $trans->deinterlance ? ' -vf yadif=0:-1:0' : '';
$ffmpeg .= " output[HLS]";
return $ffmpeg;
}
function start_stream($id) {
$stream = Stream::find($id);
$setting = Setting::first();
if ($stream->restream) {
$stream->checker = 0;
$stream->pid = null;
$stream->running = 1;
$stream->status = 1;
} else {
$stream->checker = 0;
$checkstreamurl = shell_exec('' . $setting->ffprobe_path . ' -analyzeduration 10000000 -probesize 5000000 -i "' . $stream->streamurl . '" -v quiet -print_format json -show_streams 2>&1');
$streaminfo = json_decode($checkstreamurl, true);
if ($streaminfo) {
$pid = shell_exec(getTranscode($stream->id));
$stream->pid = $pid;
$stream->running = 1;
$stream->status = 1;
$video = "";
$audio = "";
if (is_array($streaminfo)) {
foreach ($streaminfo['streams'] as $info) {
if ($video == '') {
$video = ($info['codec_type'] == 'video' ? $info['codec_name'] : '');
}
if ($audio == '') {
$audio = ($info['codec_type'] == 'audio' ? $info['codec_name'] : '');
}
}
$stream->video_codec_name = $video;
$stream->audio_codec_name = $audio;
}
} else {
$stream->running = 1;
$stream->status = 2;
//TODO: need to make recursive
if (checkPid($stream->pid)) {
shell_exec("kill -9 " . $stream->pid);
shell_exec("/bin/rm -r /home/fos-streaming/fos/www/" . $setting->hlsfolder . "/" . $stream->id . "*");
}
if ($stream->streamurl2) {
$stream->checker = 2;
$checkstreamurl = shell_exec('' . $setting->ffprobe_path . ' -analyzeduration 10000000 -probesize 5000000 -i "' . $stream->streamurl . '" -v quiet -print_format json -show_streams 2>&1');
$streaminfo = json_decode($checkstreamurl, true);
if ($streaminfo) {
$pid = shell_exec(getTranscode($stream->id, 2));
$stream->pid = $pid;
$stream->running = 1;
$stream->status = 1;
$video = "";
$audio = "";
if (is_array($streaminfo)) {
foreach ($streaminfo['streams'] as $info) {
if ($video == '') {
$video = ($info['codec_type'] == 'video' ? $info['codec_name'] : '');
}
if ($audio == '') {
$audio = ($info['codec_type'] == 'audio' ? $info['codec_name'] : '');
}
}
$stream->video_codec_name = $video;
$stream->audio_codec_name = $audio;
}
} else {
$stream->running = 1;
$stream->status = 2;
if (checkPid($stream->pid)) {
shell_exec("kill -9 " . $stream->pid);
shell_exec("/bin/rm -r /home/fos-streaming/fos/www/" . $setting->hlsfolder . "/" . $stream->id . "*");
}
//streamurl 3 checker
if ($stream->streamurl3) {
$stream->checker = 3;
$checkstreamurl = shell_exec('' . $setting->ffprobe_path . ' -analyzeduration 10000000 -probesize 5000000 -i "' . $stream->streamurl . '" -v quiet -print_format json -show_streams 2>&1');
$streaminfo = json_decode($checkstreamurl, true);
if ($streaminfo) {
$pid = shell_exec(getTranscode($stream->id, 3));
$stream->pid = $pid;
$stream->running = 1;
$stream->status = 1;
$video = "";
$audio = "";
if (is_array($streaminfo)) {
foreach ($streaminfo['streams'] as $info) {
if ($video == '') {
$video = ($info['codec_type'] == 'video' ? $info['codec_name'] : '');
}
if ($audio == '') {
$audio = ($info['codec_type'] == 'audio' ? $info['codec_name'] : '');
}
}
$stream->video_codec_name = $video;
$stream->audio_codec_name = $audio;
}
} else {
$stream->running = 1;
$stream->status = 2;
$stream->pid = null;
}
}
}
}
}
}
$stream->save();
}
function generatEginxConfPort($port) {
ob_start();
echo 'user root;
worker_processes auto;
error_log logs/error.log debug;
worker_rlimit_nofile 300000;
events {
multi_accept on;
worker_connections 16000;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
proxy_buffering off;
client_max_body_size 100m;
gzip on;
gzip_comp_level 5;
fastcgi_read_timeout 180;
server {
listen ' . $port . ';
root /home/fos-streaming/fos/www/;
index index.php index.html index.htm;
server_tokens off;
chunked_transfer_encoding off;
rewrite ^/live/(.*)/(.*)/(.*)$ /stream.php?username=$1&password=$2&stream=$3 break;
location ~ \.php$ {
if ($uri ~* /stream.php) {
gzip off;
}
if ($uri ~* /getfile.php) {
gzip off;
}
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_keep_conn on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp_auto_push on;
rtmp {
server {
listen 8546;
interleave on;
wait_video on;
idle_streams off;
max_streams 512;
application input{
live on;
allow play 127.0.0.1; # localhost
deny play all;
}
}
}';
$file = '/home/fos-streaming/fos/nginx/conf/nginx.conf';
$current = ob_get_clean();
file_put_contents($file, $current);
}