Skip to content

Commit 75a6ea5

Browse files
Merge pull request #57 from Bandwidth/DX-2704
DX-2704 Adding StartStream and StopStream Verbs
2 parents f160492 + 3a90b0e commit 75a6ea5

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

src/Voice/Bxml/StartStream.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* StartStream.php
4+
*
5+
* Implementation of the BXML StartStream verb
6+
*
7+
* @copyright Bandwidth INC
8+
*/
9+
10+
namespace BandwidthLib\Voice\Bxml;
11+
12+
require_once "Verb.php";
13+
14+
class StartStream extends Verb {
15+
16+
/**
17+
* Sets the destination attribute for StartStream
18+
*
19+
* @param string $destination A websocket URI to send the stream to. The audio from the specified tracks will be sent via websocket to this URL encoded as base64 encoded PCMU/G711 audio. See below for more details on the websocket packet format.
20+
*/
21+
public function destination($destination) {
22+
$this->destination = $destination;
23+
}
24+
25+
/**
26+
* Sets the name attribute for StartStream
27+
*
28+
* @param string $name A name to refer to this stream by. Used when sending [`<StopStream>`][1]. If not provided, a random name will be generated and sent in the [`Media Stream Started`][2] webook
29+
*/
30+
public function name($name) {
31+
$this->name = $name;
32+
}
33+
34+
/**
35+
* Sets the tracks attribute for StartStream
36+
*
37+
* @param string $tracks The part of the call to send a stream from. `inbound`, `outbound` or `both`. Default is `inbound`.
38+
*
39+
*/
40+
public function tracks($tracks) {
41+
$this->tracks = $tracks;
42+
}
43+
44+
/**
45+
* Sets the username attribute for StartStream
46+
*
47+
* @param string $username The username to send in the HTTP request to `streamEventUrl`. If specified, the URLs must be TLS-encrypted (i.e., `https`).
48+
*/
49+
public function username($username) {
50+
$this->username = $username;
51+
}
52+
53+
/**
54+
* Sets the password attribute for StartStream
55+
*
56+
* @param string $password The password to send in the HTTP request to `streamEventUrl`. If specified, the URLs must be TLS-encrypted (i.e., `https`).
57+
*/
58+
public function password($password) {
59+
$this->password = $password;
60+
}
61+
62+
/**
63+
* Sets the streamEventUrl attribute for StartStream
64+
*
65+
* @param string $streamEventUrl URL to send the associated Webhook events to during this stream's lifetime. Does not accept BXML. May be a relative URL.
66+
*/
67+
public function streamEventUrl($streamEventUrl) {
68+
$this->streamEventUrl = $streamEventUrl;
69+
}
70+
71+
/**
72+
* Sets the streamEventMethod attribute for StartStream
73+
*
74+
* @param bool $streamEventMethod The HTTP method to use for the request to `streamEventUrl`. GET or POST. Default value is POST.
75+
*/
76+
public function streamEventMethod($streamEventMethod) {
77+
$this->streamEventMethod = $streamEventMethod;
78+
}
79+
80+
public function toBxml($doc) {
81+
$element = $doc->createElement("StartStream");
82+
83+
if(isset($this->destination)) {
84+
$element->setattribute("destination", $this->destination);
85+
}
86+
87+
if(isset($this->name)) {
88+
$element->setattribute("name", $this->name);
89+
}
90+
91+
if(isset($this->tracks)) {
92+
$element->setattribute("tracks", $this->tracks);
93+
}
94+
95+
if(isset($this->username)) {
96+
$element->setattribute("username", $this->username);
97+
}
98+
99+
if(isset($this->password)) {
100+
$element->setattribute("password", $this->password);
101+
}
102+
103+
if(isset($this->streamEventUrl)) {
104+
$element->setattribute("streamEventUrl", $this->streamEventUrl);
105+
}
106+
107+
if(isset($this->streamEventMethod)) {
108+
$element->setattribute("streamEventMethod", $this->streamEventMethod);
109+
}
110+
111+
return $element;
112+
}
113+
}

src/Voice/Bxml/StopStream.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* StopStreamphp
4+
*
5+
* Implementation of the BXML StopStream verb
6+
*
7+
* @copyright Bandwidth INC
8+
*/
9+
10+
namespace BandwidthLib\Voice\Bxml;
11+
12+
require_once "Verb.php";
13+
14+
class StopStream extends Verb {
15+
16+
/**
17+
* Sets the name attribute for StopStream
18+
*
19+
* @param float $name (required) The name of the stream to stop. This is either the user selected name when sending the [`<StartStream>`][1] verb, or the system generated name returned in the [Media Stream Started][2] webhook if `<StartStream>` was sent with no `name` attribute.
20+
*/
21+
public function name($name) {
22+
$this->name = $name;
23+
}
24+
25+
public function toBxml($doc) {
26+
$element = $doc->createElement("StopStream");
27+
28+
if(isset($this->name)) {
29+
$element->setAttribute("name", $this->name);
30+
}
31+
32+
return $element;
33+
}
34+
}

tests/BxmlTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,35 @@ public function testStopRecording() {
351351
$responseXml = $response->toBxml();
352352
$this->assertEquals($expectedXml, $responseXml);
353353
}
354+
public function testStartStream() {
355+
$startStream = new BandwidthLib\Voice\Bxml\StartStream();
356+
$startStream->name("test");
357+
$startStream->tracks("inbound");
358+
$startStream->destination("https://url.com");
359+
$startStream->streamEventMethod("POST");
360+
$startStream->username("user");
361+
$startStream->password("pass");
362+
$startStream->streamEventUrl("https://url.com");
363+
364+
$response = new BandwidthLib\Voice\Bxml\Response();
365+
$response->addVerb($startStream);
366+
367+
$expectedXml = '<?xml version="1.0" encoding="UTF-8"?><Response><StartStream destination="https://url.com" name="test" tracks="inbound" username="user" password="pass" streamEventUrl="https://url.com" streamEventMethod="POST"/></Response>';
368+
$responseXml = $response->toBxml();
369+
$this->assertEquals($expectedXml, $responseXml);
370+
}
371+
372+
public function testStopStream() {
373+
$stopStream = new BandwidthLib\Voice\Bxml\StopStream();
374+
$stopStream->name("test");
375+
376+
$response = new BandwidthLib\Voice\Bxml\Response();
377+
$response->addVerb($stopStream);
378+
379+
$expectedXml = '<?xml version="1.0" encoding="UTF-8"?><Response><StopStream name="test"/></Response>';
380+
$responseXml = $response->toBxml();
381+
$this->assertEquals($expectedXml, $responseXml);
382+
}
354383

355384
public function testRecord() {
356385
$record = new BandwidthLib\Voice\Bxml\Record();

0 commit comments

Comments
 (0)