summaryrefslogtreecommitdiff
path: root/bin/session.php
blob: 34096880ef2775325db4a47e309659dba985c070 (plain)
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
<?php

function sessioncreate($type, $url, $mode)
{
	global $httppath, $ffmpegpath, $segmenterpath, $quality;

	// Get a free session
	$i=0;
	for ($i=0; $i<1000; $i++)
	{
		$session = "session" .$i;
		if (!file_exists('../ram/' .$session))
			break;
	}

	// Default
	$qparams = $quality[0];

	// Get parameters
	foreach ($quality as $qn => $qp)
	{
		if ($qn == $mode)
		{
			$qparams = $qp;
			break;
		}
	}

	// Create session
	exec('mkdir ../ram/' .$session);
	$url = str_replace("\\'", "'", $url);
	switch ($type)
	{
		case 'tv':
			$cmd = "export SHELL=\"/bin/sh\";printf \"./istream.sh \\\"" .$url ."\\\" " .$qparams ." " .$httppath ." 2 " .$ffmpegpath ." " .$segmenterpath ." " .$session ." \" | at now";
			break;
		case 'rec':
			$cmd = "export SHELL=\"/bin/sh\";printf \"cat \\\"" .$url ."\\\"/0* | ./istream.sh - " .$qparams ." " .$httppath ." 1260 " .$ffmpegpath ." " .$segmenterpath ." " .$session ." \" | at now";
			break;
		case 'vid':
			$cmd = "export SHELL=\"/bin/sh\";printf \"./istream.sh \\\"" .$url ."\\\" " .$qparams ." " .$httppath ." 1260 " .$ffmpegpath ." " .$segmenterpath ." " .$session ." \" | at now";
                        break;
		default:
			$cmd = "";
	}
	
	$cmd = str_replace('%', '%%', $cmd);
	exec ('echo "' .$cmd .'" > /tmp/a');
	exec ($cmd);
	
	// Extract $channame if needed
	switch ($type)
	{
		case 'tv':
			$urlarray = explode("/", $url);
			$channum = $urlarray[count($urlarray)-1];
			$channame = vdrgetchanname($channum);
			break;
		case 'rec':
			list($channame, $title, $desc) = vdrgetrecinfo($url);
			break;
		default:
			$channame = "";
			break;
	}

	// Write streaminfo
	writeinfostream($session, $type, $mode, $url, $channame);

	// Create logo
	if ($type == 'vid')
		generatelogo($type, $url, '../ram/' .$session .'/thumb.png');
	else
		generatelogo($type, $channame, '../ram/' .$session .'/thumb.png');

	return $session;
}

function sessiondelete($session)
{
	$ret = array();

	if ($session == 'all')
	{
		$dir_handle = @opendir('../ram/');
		if ($dir_handle)
		{
			while ($session = readdir($dir_handle))
			{
				if($session == "." || $session == ".." || $session == 'lost+found')
					continue;

				if (!is_dir('../ram/' .$session))
					continue;

				// Get info
				list($type, $mode, $url, $channame) = readinfostream($session);

				if ($type)
					sessiondeletesingle($session);
			}
		}
	}
	else
		sessiondeletesingle($session);

	$ret['status'] = "ok";
	$ret['message'] = "Successfully stopped broadcast";

	return $ret;

}

function sessiongetinfo($session)
{
	$info = array();

	// Get some info
	list($type, $mode, $url, $channame) = readinfostream($session);
	
	// Fill common info
	$info['session'] = $session;
	$info['type'] = $type;
	$info['mode'] = $mode;

	// Type info
	switch ($type)
	{
		case 'tv':
			$info['name'] = $channame;
			$info['thumbwidth'] = 80;
			$info['thumbheight'] = 80;
			$channum = vdrgetchannum($channame);
			list($info['now_time'], $info['now_title'], $info['now_desc']) = vdrgetchanepg($channum, 1);
			list($info['next_time'], $info['next_title'], $info['next_desc']) = vdrgetchanepg($channum, 0);

			break;
		case 'rec':
			$info['channel'] = $channame;
			$info['thumbwidth'] = 80;
			$info['thumbheight'] = 80;
			list($channame, $info['name'], $info['desc']) = vdrgetrecinfo($url);
			break;
		case 'vid':
			break;
	}

	return $info;
}


function sessiondeletesingle($session)
{
	$ram = "../ram/" .$session ."/";

	// Get segmenter PID if any
	if (file_exists($ram ."segmenter.pid"))
		$cmd = "/usr/local/bin/fw;kill `cat " .$ram ."segmenter.pid`; rm " .$ram ."segmenter.pid; ";

	$cmd .= "rm -rf " .$ram;
	exec ($cmd);
}

function sessiongetstatus($session)
{
	global $maxencodingprocesses, $httppath;

	$status = array();

	$path = '../ram/' .$session;

	// Check that session exists
	if (($session == "") || !count(glob($path)))
	{
		$status['status'] = "error";

	        $nbencprocess = exec("find ../ram/ -name segmenter.pid | wc | awk '{ print $1 }'");
	        if ($nbencprocess >= $maxencodingprocesses)
			$status['message'] = "Error: maximun number of sessions reached";
	        else
			$status['message'] = "Error: cannot create session";
	}
	else
	{
		// Get stream info
		list($type, $mode, $url, $channame) = readinfostream($session);

		if (count(glob($path . '/*.ts')) < 2)
		{
			$status['status'] = "wait";
			switch ($type)
			{
				case 'tv':
					$status['message'] = "Requesting live channel " .$channame;
					break;
				case 'rec':
					$status['message'] = "Requesting recording channel " .$channame;
					break;
				case 'vid':
					$status['message'] = "Requesting video file " .$url;
					break;
			}
		}
		else
		{
			$status['status'] = "ready";
			$status['message'] = $mode ." Broadcast ready";

			$status['url'] = $httppath ."ram/" .$session ."/stream.m3u8";

		}
	}

	return $status;
}


?>