-
Notifications
You must be signed in to change notification settings - Fork 19
/
functions.php
331 lines (305 loc) · 13.1 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
<?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 lists($list, $column)
{
$columns = [];
foreach ($list->toArray() as $key => $value) {
array_push($columns, $value[$column]);
}
return $columns;
}
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 : '15000000');
$ffmpeg .= ' -analyzeduration ' . ($trans->analyzeduration ? $trans->analyzeduration : '12000000');
$ffmpeg .= ' -i ' . '"' . "$url" . '"';
$ffmpeg .= ' -user_agent "' . ($setting->user_agent ? $setting->user_agent : 'FOS-Streaming') . '"';
$ffmpeg .= ' -strict -2 -dn ';
$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' : '';
$ffmpeg .= $endofffmpeg;
return $ffmpeg;
}
$ffmpeg .= ' -probesize 15000000 -analyzeduration 9000000 -i "' . $url . '"';
$ffmpeg .= ' -user_agent "' . ($setting->user_agent ? $setting->user_agent : 'FOS-Streaming') . '"';
$ffmpeg .= ' -c copy -c:a aac -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 : '15000000');
$ffmpeg .= ' -analyzeduration ' . ($trans->analyzeduration ? $trans->analyzeduration : '12000000');
$ffmpeg .= ' -i ' . '"' . "[input]" . '"';
$ffmpeg .= ' -user_agent "' . ($setting->user_agent ? $setting->user_agent : 'FOS-Streaming') . '"';
$ffmpeg .= ' -strict -2 -dn ';
$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' : '';
$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 1000000 -probesize 9000000 -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;
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 1000000 -probesize 9000000 -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 . "*");
}
if ($stream->streamurl3) {
$stream->checker = 3;
$checkstreamurl = shell_exec('' . $setting->ffprobe_path . ' -analyzeduration 1000000 -probesize 9000000 -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 nginx;
worker_processes auto;
worker_rlimit_nofile 655350;
events {
worker_connections 65535;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
reset_timedout_connection on;
gzip off;
fastcgi_read_timeout 200;
access_log off;
keepalive_timeout 10;
client_max_body_size 999m;
send_timeout 120s;
sendfile_max_chunk 512k;
lingering_close off;
server {
listen ' . $port . ';
root /home/fos-streaming/fos/www1/;
server_tokens off;
chunked_transfer_encoding off;
rewrite ^/live/(.*)/(.*)/(.*)$ /stream.php?username=$1&password=$2&stream=$3 break;
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_buffering on;
fastcgi_buffers 96 32k;
fastcgi_buffer_size 32k;
fastcgi_max_temp_file_size 0;
fastcgi_keep_conn on;
fastcgi_param SCRIPT_FILENAME /home/fos-streaming/fos/www1/$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
}
}
server {
listen 7777;
root /home/fos-streaming/fos/www/;
index index.php index.html index.htm;
server_tokens off;
chunked_transfer_encoding off;
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_buffering on;
fastcgi_buffers 96 32k;
fastcgi_buffer_size 32k;
fastcgi_max_temp_file_size 0;
fastcgi_keep_conn on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
}
}
}';
$file = '/home/fos-streaming/fos/nginx/conf/nginx.conf';
$current = ob_get_clean();
file_put_contents($file, $current);
}