-
Notifications
You must be signed in to change notification settings - Fork 96
/
_functions.php
262 lines (213 loc) · 9.27 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
<?php
function redirect($url, $time) {
echo "<script>
window.setTimeout(function(){
window.location.href = '" . $url ."';
}, " . $time .");
</script>";
}
if (isset($_GET['logout'])) {
session_destroy();
}
function logincheck() {
if (!isset($_SESSION['user_id'])){
header("location: index.php");
}
}
function stop_stream($id)
{
$stream = Stream::find($id);
$setting = Setting::first();
shell_exec("kill -9 " . $stream->pid);
shell_exec("/bin/rm -r /usr/local/nginx/html/" . $setting->hlsfolder . "/" . $stream->id . "*");
$stream->pid = "";
$stream->running = 0;
$stream->status = 0;
$stream->save();
}
function checkPid($pid) {
exec("ps $pid", $output, $result);
return count($output) >= 2 ? true : false;
}
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 -hls_base_url http://'.$setting->webip.':'.$setting->webport.'/'.$setting->hlsfolder.'/';
$endofffmpeg .= ' -hls_list_size 8 /usr/local/nginx/html/' . $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 "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 -user_agent "FOS-Streaming" -i "'.$url.'"';
$ffmpeg .= ' -c copy -c:a libvo_aacenc -b:a 128k';
$ffmpeg .= $endofffmpeg;
return $ffmpeg;
}
function start_stream($id)
{
$stream = Stream::find($id);
$setting = Setting::first();
$stream->checker = 0;
$checkstreamurl = shell_exec('/usr/bin/timeout 15s '.$setting->ffprobe_path.' -analyzeduration 10000000 -probesize 9000000 -i "'.$stream->streamurl.'" -v quiet -print_format json -show_streams 2>&1');
$streaminfo = (array) json_decode($checkstreamurl);
if(count($streaminfo) > 0) {
$pid = shell_exec(getTranscode($stream->id));
$stream->pid = $pid;
$stream->running = 1;
$stream->status = 1;
if(isset($streaminfo->streams)) {
foreach ((array)$streaminfo->streams as $info) {
if ($info->codec_type == 'video') {
$stream->video_codec_name = $info->codec_long_name;
} else if ($info->codec_type == 'audio') {
$stream->audio_codec_name = $info->codec_long_name;
}
}
}
} else {
$stream->running = 1;
$stream->status = 2;
//need to make recursive
////streamurl 2 checker
shell_exec("kill -9 " . $stream->pid);
shell_exec("/bin/rm -r /usr/local/nginx/html/" . $setting->hlsfolder . "/" . $stream->id . "*");
if($stream->streamurl2) {
$stream->checker = 2;
echo "checking stream 2";
$checkstreamurl = shell_exec('/usr/bin/timeout 15s '.$setting->ffprobe_path.' -analyzeduration 10000000 -probesize 9000000 -i "'.$stream->streamurl2.'" -v quiet -print_format json -show_streams 2>&1');
$streaminfo = (array) json_decode($checkstreamurl);
if(count($streaminfo) > 0) {
echo getTranscode($stream->id, 2);
$pid = shell_exec(getTranscode($stream->id, 2));
$stream->pid = $pid;
$stream->running = 1;
$stream->status = 1;
if(isset($streaminfo->streams)) {
foreach((array)$streaminfo->streams as $info ) {
if($info->codec_type == 'video') {
$stream->video_codec_name = $info->codec_long_name;
}
else if($info->codec_type == 'audio') {
$stream->audio_codec_name = $info->codec_long_name;
}
}
}
} else {
$stream->running = 1;
$stream->status = 2;
shell_exec("kill -9 " . $stream->pid);
shell_exec("/bin/rm -r /usr/local/nginx/html/" . $setting->hlsfolder . "/" . $stream->id . "*");
//streamurl 3 checker
if($stream->streamurl3) {
$stream->checker = 3;
$checkstreamurl = shell_exec('/usr/bin/timeout 15s ' . $setting->ffprobe_path . ' -analyzeduration 10000000 -probesize 9000000 -i "' . $stream->streamurl3 . '" -v quiet -print_format json -show_streams 2>&1');
$streaminfo = (array)json_decode($checkstreamurl);
if (count($streaminfo) > 0) {
$pid = shell_exec(getTranscode($stream->id, 3));
$stream->pid = $pid;
$stream->running = 1;
$stream->status = 1;
if(isset($streaminfo->streams)) {
foreach ((array)$streaminfo->streams as $info) {
if ($info->codec_type == 'video') {
$stream->video_codec_name = $info->codec_long_name;
} else if ($info->codec_type == 'audio') {
$stream->audio_codec_name = $info->codec_long_name;
}
}
}
} 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;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen '.$port.';
root /usr/local/nginx/html/;
index index.php index.html index.htm;
server_tokens off;
chunked_transfer_encoding off;
rewrite ^/(.*)/(.*)/(.*)$ /stream.php?username=$1&password=$2&stream=$3 break;
location ~ \.php$ {
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 {
server {
listen 1935;
ping 30s;
notify_method get;
application rtmp {
live on;
}
}
}';
$file ='/usr/local/nginx/conf/nginx.conf';
$current = ob_get_clean();
file_put_contents($file, $current);
}