forked from BenJaziaSadok/janus-gateway-php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
janus.php
129 lines (107 loc) · 3.78 KB
/
janus.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
/*
* This file contains client side REST calls for janus-gateway
* With this example you can use the admin-api in your janus infrastructure
* and call it securely
*
* This is a beta-version in php
* Feel free to update
*/
<?php
error_reporting(E_ALL ^ E_NOTICE);
function generateRandomString($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$action = !empty($_POST['action']) ? $_POST['action'] : false;
$sessionId = !empty($_POST['sessionId']) ? $_POST['sessionId'] : false;
$plugin = !empty($_POST['plugin']) ? $_POST['plugin'] : false;
$handleId = !empty($_POST['handleId']) ? $_POST['handleId'] : false;
$message = !empty($_POST['message']) ? $_POST['message'] : false;
$candidate = !empty($_POST['candidate']) ? $_POST['candidate'] : false;
$jsep = !empty($_POST['jsep']) ? $_POST['jsep'] : false;
$server='http://server/janus';
$password='janusrocks';
switch($action){
case 'CreateSession' :{
$data='{"janus":"create","transaction":"'.generateRandomString().'","apisecret":"'.$password.'"}';
$result=CallAPI('POST',$server,$data);
echo ($result);
}
break;
case 'Refresh' :{
$date = date_create();
date_timestamp_get($date);
$longpoll = $server."/".$sessionId."?rid=".date_timestamp_get($date);
$longpoll = $longpoll .'&maxev=1&_='.date_timestamp_get($date).'&apisecret='.$password;
$result=CallAPI('GET',$longpoll);
echo ($result);
}
break;
case 'createHandle' :{
$data = '{"janus": "attach", "plugin": "'.$plugin.'", "transaction": "'.generateRandomString().'","apisecret":"'.$password.'"}';
$server=$server.'/'.$sessionId;
$result=CallAPI('POST',$server,$data);
echo ($result);
}
break;
case 'destroySession' :{
$data = '{"janus": "destroy", "transaction": "'.generateRandomString().'","apisecret":"'.$password.'"}';
$server=$server.'/'.$sessionId;
$result=CallAPI('POST',$server,$data);
echo ($result);
}
break;
case 'sendMessage' :{
if ($jsep==''){$request = '{"janus": "message", "body": '.$message.', "transaction": "'.generateRandomString().'","apisecret":"'.$password.'"}';}
else{$request = '{"janus": "message", "body": '.$message.', "transaction": "'.generateRandomString().'","jsep":'.$jsep.',"apisecret":"'.$password.'"}';}
// echo $request;
$server=$server.'/'.$sessionId.'/'.$handleId;
$result=CallAPI('POST',$server,$request);
echo ($result);
}
break;
case 'sendTrickleCandidate' :{
$request = '{"janus": "trickle", "candidate": '.$candidate.', "transaction": "'.generateRandomString().'","apisecret":"'.$password.'"}';
$server=$server.'/'.$sessionId.'/'.$handleId;
$result=CallAPI('POST',$server,$request);
echo ($result);
}
break;
case 'destroyHandle' :{
$request = '{"janus":"detach","transaction":"'.generateRandomString().'","apisecret":"'.$password.'"}';
$server=$server.'/'.$sessionId.'/'.$handleId;
$result=CallAPI('POST',$server,$request);
echo ($result);
}
break;
;break;
default:{die();}
}