|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SquareetLabs\LaravelOpenVidu; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\RequestOptions; |
|
8
|
|
|
use Illuminate\Support\Facades\Cache; |
|
9
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
|
10
|
|
|
use SquareetLabs\LaravelOpenVidu\Builders\RecordingBuilder; |
|
11
|
|
|
use SquareetLabs\LaravelOpenVidu\Enums\Uri; |
|
12
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduException; |
|
13
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduProblemWithBodyParameterException; |
|
14
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduRecordingNotFoundException; |
|
15
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduRecordingResolutionException; |
|
16
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduRecordingStatusException; |
|
17
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduServerRecordingIsDisabledException; |
|
18
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduSessionCantRecordingException; |
|
19
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduSessionHasNotConnectedParticipantsException; |
|
20
|
|
|
use SquareetLabs\LaravelOpenVidu\Exceptions\OpenViduSessionNotFoundException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class OpenVidu |
|
24
|
|
|
* @package App\SquareetLabs\LaravelOpenVidu |
|
25
|
|
|
*/ |
|
26
|
|
|
class OpenVidu |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var |
|
31
|
|
|
*/ |
|
32
|
|
|
private $config; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* SmsUp constructor. |
|
37
|
|
|
* @param array $config |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(array $config) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->config = $config; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param SessionProperties|null $properties |
|
46
|
|
|
* @return Session |
|
47
|
|
|
* @throws Exceptions\OpenViduException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function createSession(?SessionProperties $properties = null): Session |
|
50
|
|
|
{ |
|
51
|
|
|
$session = new Session($this->client(), $properties); |
|
52
|
|
|
Cache::store('openvidu')->forever($session->getSessionId(), $session->toJson()); |
|
53
|
|
|
return $session; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Client |
|
58
|
|
|
*/ |
|
59
|
|
|
private function client(): Client |
|
60
|
|
|
{ |
|
61
|
|
|
$client = new Client([ |
|
62
|
|
|
'headers' => [ |
|
63
|
|
|
'Content-Type' => 'application/json', |
|
64
|
|
|
'Accept' => 'application/json', |
|
65
|
|
|
], |
|
66
|
|
|
'auth' => [ |
|
67
|
|
|
$this->config['app'], $this->config['secret'] |
|
68
|
|
|
], |
|
69
|
|
|
'base_uri' => $this->config['domain'].':'.$this->config['port'], |
|
70
|
|
|
'debug' => $this->config['debug'], |
|
71
|
|
|
'http_errors' => false, |
|
72
|
|
|
'verify' => false |
|
73
|
|
|
]); |
|
74
|
|
|
return $client; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Starts the recording of a {@see Session} |
|
79
|
|
|
* @param RecordingProperties $properties |
|
80
|
|
|
* @return Recording |
|
81
|
|
|
* @throws OpenViduException |
|
82
|
|
|
* @throws OpenViduRecordingResolutionException |
|
83
|
|
|
* @throws OpenViduServerRecordingIsDisabledException |
|
84
|
|
|
* @throws OpenViduSessionCantRecordingException |
|
85
|
|
|
* @throws OpenViduSessionHasNotConnectedParticipantsException |
|
86
|
|
|
* @throws OpenViduSessionNotFoundException |
|
87
|
|
|
* @throws InvalidArgumentException |
|
88
|
|
|
* @throws Exceptions\OpenViduInvalidArgumentException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function startRecording(RecordingProperties $properties): Recording |
|
91
|
|
|
{ |
|
92
|
|
|
$activeSession = $this->getSession($properties->session()); |
|
93
|
|
|
if ($activeSession->isBeingRecorded()) { |
|
94
|
|
|
$lastRecordingId = $activeSession->getLastRecordingId(); |
|
95
|
|
|
if (!$lastRecordingId) { |
|
96
|
|
|
throw new OpenViduSessionCantRecordingException("The session is not configured for using media routed or has recording problems"); |
|
97
|
|
|
} |
|
98
|
|
|
return $this->getRecording($lastRecordingId); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
$response = $this->client()->post(Uri::RECORDINGS_START, [ |
|
101
|
|
|
RequestOptions::JSON => $properties->toArray() ?? null |
|
102
|
|
|
]); |
|
103
|
|
|
switch ($response->getStatusCode()) { |
|
104
|
|
|
case 200: |
|
105
|
|
|
$recording = RecordingBuilder::build(json_decode($response->getBody()->getContents(), true)); |
|
106
|
|
|
|
|
107
|
|
|
if ($activeSession != null) { |
|
108
|
|
|
$activeSession->setIsBeingRecorded(true); |
|
109
|
|
|
$activeSession->setLastRecordingId($recording->id); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
return $recording; |
|
112
|
|
|
case 404: |
|
113
|
|
|
throw new OpenViduSessionNotFoundException(); |
|
114
|
|
|
break; |
|
115
|
|
|
case 406: |
|
116
|
|
|
throw new OpenViduSessionHasNotConnectedParticipantsException(); |
|
117
|
|
|
break; |
|
118
|
|
|
case 409: |
|
119
|
|
|
throw new OpenViduSessionCantRecordingException("The session is not configured for using media routed or it is already being recorded"); |
|
120
|
|
|
break; |
|
121
|
|
|
case 422: |
|
122
|
|
|
throw new OpenViduRecordingResolutionException(); |
|
123
|
|
|
break; |
|
124
|
|
|
case 501: |
|
125
|
|
|
throw new OpenViduServerRecordingIsDisabledException(); |
|
126
|
|
|
break; |
|
127
|
|
|
default: |
|
128
|
|
|
throw new OpenViduException("Invalid response status code ".$response->getStatusCode(), $response->getStatusCode()); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Gets an existing {@see Session} |
|
134
|
|
|
* @param string $sessionId |
|
135
|
|
|
* @return Session |
|
136
|
|
|
* @throws OpenViduException |
|
137
|
|
|
* @throws OpenViduSessionNotFoundException |
|
138
|
|
|
* @throws InvalidArgumentException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getSession(string $sessionId): Session |
|
141
|
|
|
{ |
|
142
|
|
|
if (Cache::store('openvidu')->has($sessionId)) { |
|
143
|
|
|
return (new Session($this->client()))->fromJson(Cache::store('openvidu')->get($sessionId)); |
|
144
|
|
|
} |
|
145
|
|
|
throw new OpenViduSessionNotFoundException(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Check if exists {@see Session} |
|
151
|
|
|
* @param string $sessionId |
|
152
|
|
|
* @return bool |
|
153
|
|
|
* @throws InvalidArgumentException |
|
154
|
|
|
*/ |
|
155
|
|
|
public function existsSession(string $sessionId): bool |
|
156
|
|
|
{ |
|
157
|
|
|
return Cache::store('openvidu')->has($sessionId); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Stops the recording of a {@see Session} |
|
162
|
|
|
* @param string $recordingId The `id` property of the {@see Recording} you want to stop |
|
163
|
|
|
* @return Recording |
|
164
|
|
|
* @throws OpenViduException |
|
165
|
|
|
* @throws OpenViduRecordingNotFoundException |
|
166
|
|
|
* @throws OpenViduRecordingStatusException |
|
167
|
|
|
* @throws OpenViduSessionNotFoundException |
|
168
|
|
|
* @throws InvalidArgumentException |
|
169
|
|
|
* @throws Exceptions\OpenViduInvalidArgumentException |
|
170
|
|
|
*/ |
|
171
|
|
|
public function stopRecording(string $recordingId): Recording |
|
172
|
|
|
{ |
|
173
|
|
|
$response = $this->client()->post(Uri::RECORDINGS_STOP.'/'.$recordingId); |
|
174
|
|
|
switch ($response->getStatusCode()) { |
|
175
|
|
|
case 200: |
|
176
|
|
|
$recording = RecordingBuilder::build(json_decode($response->getBody()->getContents(), true)); |
|
177
|
|
|
$activeSession = $this->getSession($recording->getSessionId()); |
|
178
|
|
|
if ($activeSession != null) { |
|
179
|
|
|
$activeSession->setIsBeingRecorded(false); |
|
180
|
|
|
$activeSession->setLastRecordingId(null); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
return $recording; |
|
183
|
|
|
case 404: |
|
184
|
|
|
throw new OpenViduRecordingNotFoundException(); |
|
185
|
|
|
break; |
|
186
|
|
|
case 406: |
|
187
|
|
|
throw new OpenViduRecordingStatusException("The recording has `starting` status. Wait until `started` status before stopping the recording."); |
|
188
|
|
|
break; |
|
189
|
|
|
default: |
|
190
|
|
|
throw new OpenViduException("Invalid response status code ".$response->getStatusCode(), $response->getStatusCode()); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Gets an existing {@see Recording} |
|
196
|
|
|
* @param string $recordingId The `id` property of the {@see Recording} you want to retrieve |
|
197
|
|
|
* @return string |
|
198
|
|
|
* @throws OpenViduException |
|
199
|
|
|
* @throws OpenViduRecordingNotFoundException |
|
200
|
|
|
* @throws Exceptions\OpenViduInvalidArgumentException |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getRecording(string $recordingId) |
|
203
|
|
|
{ |
|
204
|
|
|
$response = $this->client()->get(Uri::RECORDINGS_URI.'/'.$recordingId); |
|
205
|
|
|
switch ($response->getStatusCode()) { |
|
206
|
|
|
case 200: |
|
207
|
|
|
$recording = RecordingBuilder::build(json_decode($response->getBody()->getContents(), true)); |
|
208
|
|
|
return $recording; |
|
209
|
|
|
case 404: |
|
210
|
|
|
throw new OpenViduRecordingNotFoundException(); |
|
211
|
|
|
break; |
|
212
|
|
|
default: |
|
213
|
|
|
throw new OpenViduException("Invalid response status code ".$response->getStatusCode(), $response->getStatusCode()); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Gets an array with all existing recordings |
|
219
|
|
|
* @return array |
|
220
|
|
|
* @throws OpenViduException |
|
221
|
|
|
* @throws Exceptions\OpenViduInvalidArgumentException |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getRecordings(): array |
|
224
|
|
|
{ |
|
225
|
|
|
$recordings = []; |
|
226
|
|
|
$response = $this->client()->get(Uri::RECORDINGS_URI); |
|
227
|
|
|
switch ($response->getStatusCode()) { |
|
228
|
|
|
case 200: |
|
229
|
|
|
$items = json_decode($response->getBody()->getContents(), true); |
|
230
|
|
|
foreach ($items as $item) { |
|
231
|
|
|
$recordings[] = RecordingBuilder::build($item); |
|
232
|
|
|
} |
|
233
|
|
|
return $recordings; |
|
234
|
|
|
default: |
|
235
|
|
|
throw new OpenViduException("Invalid response status code ".$response->getStatusCode(), $response->getStatusCode()); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Deletes a {@see Recording}. The recording must have status `stopped`, `ready` or `failed` |
|
242
|
|
|
* @param string $recordingId The `id` property of the {@see Recording} you want to delete |
|
243
|
|
|
* @return bool |
|
244
|
|
|
* @throws OpenViduException |
|
245
|
|
|
* @throws OpenViduRecordingNotFoundException |
|
246
|
|
|
* @throws OpenViduRecordingStatusException |
|
247
|
|
|
*/ |
|
248
|
|
|
public function deleteRecording(string $recordingId): bool |
|
249
|
|
|
{ |
|
250
|
|
|
$response = $this->client()->delete(Uri::RECORDINGS_URI.'/'.$recordingId); |
|
251
|
|
|
|
|
252
|
|
|
switch ($response->getStatusCode()) { |
|
253
|
|
|
case 200: |
|
254
|
|
|
return true; |
|
255
|
|
|
case 404: |
|
256
|
|
|
throw new OpenViduRecordingNotFoundException(); |
|
257
|
|
|
break; |
|
258
|
|
|
case 409: |
|
259
|
|
|
throw new OpenViduRecordingStatusException("The recording has `started` status. Stop it before deletion."); |
|
260
|
|
|
break; |
|
261
|
|
|
default: |
|
262
|
|
|
throw new OpenViduException("Invalid response status code ".$response->getStatusCode(), $response->getStatusCode()); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Returns the list of active sessions. <strong>This value will remain unchanged |
|
268
|
|
|
* since the last time method {@link SquareetLabs\LaravelOpenVidu#fetch()} |
|
269
|
|
|
* was called</strong>. Exceptions to this rule are: |
|
270
|
|
|
* <ul> |
|
271
|
|
|
* <li>Calling {@see Session::fetch} updates that |
|
272
|
|
|
* specific Session status</li> |
|
273
|
|
|
* <li>Calling {@see Session::close()} automatically |
|
274
|
|
|
* removes the Session from the list of active Sessions</li> |
|
275
|
|
|
* <li>Calling |
|
276
|
|
|
* {@see Session::forceDisconnect(string)} |
|
277
|
|
|
* automatically updates the inner affected connections for that specific |
|
278
|
|
|
* Session</li> |
|
279
|
|
|
* <li>Calling {@see Session::forceUnpublish(string)} |
|
280
|
|
|
* also automatically updates the inner affected connections for that specific |
|
281
|
|
|
* Session</li> |
|
282
|
|
|
* <li>Calling {@see OpenVidu::startRecording(string)} |
|
283
|
|
|
* and {@see LaravelOpenVidu::stopRecording(string)} |
|
284
|
|
|
* automatically updates the recording status of the Session |
|
285
|
|
|
* </ul> |
|
286
|
|
|
* <br> |
|
287
|
|
|
* To get the list of active sessions with their current actual value, you must |
|
288
|
|
|
* call first {@see OpenVidu::fetch()} and then |
|
289
|
|
|
* {@see OpenVidu::getActiveSessions()} |
|
290
|
|
|
* @throws OpenViduException |
|
291
|
|
|
*/ |
|
292
|
|
|
public function getActiveSessions(): array |
|
293
|
|
|
{ |
|
294
|
|
|
try { |
|
295
|
|
|
return Cache::store('openvidu')->getAll(); |
|
296
|
|
|
} catch (Exception $e) { |
|
297
|
|
|
throw new OpenViduException("Make sure you have correctly configured the openvidu cache driver.", 500); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Sends signal to session with given SignalProperties |
|
303
|
|
|
* @param SignalProperties $properties |
|
304
|
|
|
* @return bool |
|
305
|
|
|
* @throws OpenViduException |
|
306
|
|
|
* @throws OpenViduProblemWithBodyParameterException |
|
307
|
|
|
* @throws OpenViduSessionHasNotConnectedParticipantsException |
|
308
|
|
|
* @throws OpenViduSessionNotFoundException |
|
309
|
|
|
*/ |
|
310
|
|
|
public function sendSignal(SignalProperties $properties): bool |
|
311
|
|
|
{ |
|
312
|
|
|
$activeSession = $this->getSession($properties->session()); |
|
|
|
|
|
|
313
|
|
|
$response = $this->client()->post(Uri::SIGNAL_URI, [ |
|
314
|
|
|
RequestOptions::JSON => $properties->toArray() ?? null |
|
315
|
|
|
]); |
|
316
|
|
|
switch ($response->getStatusCode()) { |
|
317
|
|
|
case 200: |
|
318
|
|
|
return true; |
|
319
|
|
|
case 400: |
|
320
|
|
|
throw new OpenViduProblemWithBodyParameterException(); |
|
321
|
|
|
break; |
|
322
|
|
|
case 404: |
|
323
|
|
|
throw new OpenViduSessionNotFoundException(); |
|
324
|
|
|
break; |
|
325
|
|
|
case 406: |
|
326
|
|
|
throw new OpenViduSessionHasNotConnectedParticipantsException(); |
|
327
|
|
|
break; |
|
328
|
|
|
default: |
|
329
|
|
|
throw new OpenViduException("Invalid response status code " . $response->getStatusCode(), $response->getStatusCode()); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
|