1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2014 Brian Smith <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace phparia\Api; |
20
|
|
|
|
21
|
|
|
use GuzzleHttp\Exception\RequestException; |
22
|
|
|
use phparia\Client\AriClientAware; |
23
|
|
|
use phparia\Exception\UnprocessableEntityException; |
24
|
|
|
use phparia\Resources\LiveRecording; |
25
|
|
|
use phparia\Resources\Playback; |
26
|
|
|
use phparia\Exception\ConflictException; |
27
|
|
|
use phparia\Exception\InvalidParameterException; |
28
|
|
|
use phparia\Exception\NotFoundException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Base class for playing media on channels and bridges |
32
|
|
|
* |
33
|
|
|
* @author Brian Smith <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
abstract class MediaBase extends AriClientAware |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @return string 'channels' or 'bridges' |
39
|
|
|
*/ |
40
|
|
|
abstract public function getType(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Play music on hold to a channel. Using media operations such as /play on a channel playing MOH in |
44
|
|
|
* this manner will suspend MOH without resuming automatically. If continuing music on hold is |
45
|
|
|
* desired, the stasis application must reinitiate music on hold. |
46
|
|
|
* |
47
|
|
|
* @param string $id Bridge/Channel's id |
48
|
|
|
* @param string $mohClass Music on hold class to use |
49
|
|
|
* @throws NotFoundException |
50
|
|
|
* @throws ConflictException |
51
|
|
|
*/ |
52
|
1 |
|
public function startMusicOnHold($id, $mohClass) |
53
|
|
|
{ |
54
|
1 |
|
$uri = "{$this->getType()}/$id/moh"; |
55
|
|
|
try { |
56
|
1 |
|
$this->client->getEndpoint()->post($uri, [ |
57
|
|
|
'form_params' => [ |
58
|
1 |
|
'mohClass' => $mohClass, |
59
|
|
|
] |
60
|
1 |
|
]); |
61
|
1 |
|
} catch (RequestException $e) { |
62
|
|
|
$this->processRequestException($e); |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Stop playing music on hold to a channel. |
68
|
|
|
* |
69
|
|
|
* @param string $id Bridge/Channel's id |
70
|
|
|
* @throws NotFoundException |
71
|
|
|
* @throws ConflictException |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function stopMusicOnHold($id) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$uri = "{$this->getType()}/$id/moh"; |
76
|
|
|
try { |
77
|
|
|
$this->client->getEndpoint()->delete($uri); |
78
|
|
|
} catch (RequestException $e) { |
79
|
|
|
$this->processRequestException($e); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Start playback of media. The media URI may be any of a number of URI's. Currently sound:, |
85
|
|
|
* recording:, number:, digits:, characters:, and tone: URI's are supported. This operation creates a |
86
|
|
|
* playback resource that can be used to control the playback of media (pause, rewind, fast forward, |
87
|
|
|
* etc.) |
88
|
|
|
* |
89
|
|
|
* @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
90
|
|
|
* |
91
|
|
|
* @param string $id Bridge/Channel's id |
92
|
|
|
* @param string $media (required) Media's URI to play. |
93
|
|
|
* @param string $lang For sounds, selects language for sound. |
94
|
|
|
* @param int $offsetms Number of media to skip before playing. |
95
|
|
|
* @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
96
|
|
|
* @param string $playbackId Playback Id. |
97
|
|
|
* @return Playback |
98
|
|
|
* @throws NotFoundException |
99
|
|
|
* @throws ConflictException |
100
|
|
|
*/ |
101
|
2 |
View Code Duplication |
public function playMedia($id, $media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null) |
|
|
|
|
102
|
|
|
{ |
103
|
2 |
|
$uri = "{$this->getType()}/$id/play"; |
104
|
|
|
|
105
|
|
|
try { |
106
|
2 |
|
$response = $this->client->getEndpoint()->post($uri, [ |
107
|
|
|
'form_params' => [ |
108
|
2 |
|
'media' => $media, |
109
|
2 |
|
'lang' => $lang, |
110
|
2 |
|
'offsetms' => $offsetms, |
111
|
2 |
|
'skipms' => $skipms, |
112
|
2 |
|
'playbackId' => $playbackId, |
113
|
|
|
] |
114
|
2 |
|
]); |
115
|
2 |
|
} catch (RequestException $e) { |
116
|
|
|
$this->processRequestException($e); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
return new Playback($this->client, \GuzzleHttp\json_decode($response->getBody())); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Start playback of media and specify the playbackId. The media URI may be any of a number of URI's. |
124
|
|
|
* Currently sound: and recording: URI's are supported. This operation creates a playback resource |
125
|
|
|
* that can be used to control the playback of media (pause, rewind, fast forward, etc.) |
126
|
|
|
* |
127
|
|
|
* @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
128
|
|
|
* |
129
|
|
|
* @param string $id Channel's id |
130
|
|
|
* @param string $media (required) Media's URI to play. |
131
|
|
|
* @param string $lang For sounds, selects language for sound. |
132
|
|
|
* @param int $offsetms Number of media to skip before playing. |
133
|
|
|
* @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
134
|
|
|
* @param string $playbackId Playback Id. |
135
|
|
|
* @return Playback |
136
|
|
|
* @throws NotFoundException |
137
|
|
|
* @throws ConflictException |
138
|
|
|
*/ |
139
|
4 |
View Code Duplication |
public function playMediaWithId( |
|
|
|
|
140
|
|
|
$id, |
141
|
|
|
$media, |
142
|
|
|
$lang = null, |
143
|
|
|
$offsetms = null, |
144
|
|
|
$skipms = null, |
145
|
|
|
$playbackId = null |
146
|
|
|
) { |
147
|
4 |
|
$uri = "{$this->getType()}/$id/play/$playbackId"; |
148
|
|
|
try { |
149
|
4 |
|
$response = $this->client->getEndpoint()->post($uri, [ |
150
|
|
|
'form_params' => [ |
151
|
4 |
|
'media' => $media, |
152
|
4 |
|
'lang' => $lang, |
153
|
4 |
|
'offsetms' => $offsetms, |
154
|
4 |
|
'skipms' => $skipms, |
155
|
|
|
] |
156
|
4 |
|
]); |
157
|
4 |
|
} catch (RequestException $e) { |
158
|
|
|
$this->processRequestException($e); |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
return new Playback($this->client, \GuzzleHttp\json_decode($response->getBody())); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Start a recording. Record audio from a channel. Note that this will not capture audio sent to the |
166
|
|
|
* channel. The bridge itself has a record feature if that's what you want. |
167
|
|
|
* |
168
|
|
|
* @param string $id Channel/Bridge ID |
169
|
|
|
* @param string $name (required) Recording's filename |
170
|
|
|
* @param string $format (required) Format to encode audio in |
171
|
|
|
* @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit |
172
|
|
|
* @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit |
173
|
|
|
* @param string $ifExists = fail - Action to take if a recording with the same name already exists. |
174
|
|
|
* @param boolean $beep string = fail - Action to take if a recording with the same name already exists. |
175
|
|
|
* @param string $terminateOn none - DTMF input to terminate recording |
176
|
|
|
* @return LiveRecording |
177
|
|
|
* @throws InvalidParameterException |
178
|
|
|
* @throws NotFoundException |
179
|
|
|
* @throws ConflictException |
180
|
|
|
* @throws UnprocessableEntityException |
181
|
|
|
*/ |
182
|
1 |
|
public function record( |
183
|
|
|
$id, |
184
|
|
|
$name, |
185
|
|
|
$format, |
186
|
|
|
$maxDurationSeconds = null, |
187
|
|
|
$maxSilenceSeconds = null, |
188
|
|
|
$ifExists = null, |
189
|
|
|
$beep = null, |
190
|
|
|
$terminateOn = null |
191
|
|
|
) { |
192
|
1 |
|
$uri = "{$this->getType()}/$id/record"; |
193
|
|
|
try { |
194
|
1 |
|
$response = $this->client->getEndpoint()->post($uri, [ |
195
|
|
|
'form_params' => [ |
196
|
1 |
|
'name' => $name, |
197
|
1 |
|
'format' => $format, |
198
|
1 |
|
'maxDurationSeconds' => $maxDurationSeconds, |
199
|
1 |
|
'maxSilenceSeconds' => $maxSilenceSeconds, |
200
|
1 |
|
'ifExists' => $ifExists, |
201
|
1 |
|
'beep' => $beep, |
202
|
1 |
|
'terminateOn' => $terminateOn, |
203
|
|
|
] |
204
|
1 |
|
]); |
205
|
1 |
|
} catch (RequestException $e) { |
206
|
|
|
$this->processRequestException($e); |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
return new LiveRecording($this->client, \GuzzleHttp\json_decode($response->getBody())); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.