Skip to content

Commit

Permalink
Merge pull request #57 from Bandwidth/DX-2704
Browse files Browse the repository at this point in the history
DX-2704 Adding StartStream and StopStream Verbs
  • Loading branch information
brianluisgomez authored Aug 29, 2022
2 parents f160492 + 3a90b0e commit 75a6ea5
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/Voice/Bxml/StartStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* StartStream.php
*
* Implementation of the BXML StartStream verb
*
* @copyright Bandwidth INC
*/

namespace BandwidthLib\Voice\Bxml;

require_once "Verb.php";

class StartStream extends Verb {

/**
* Sets the destination attribute for StartStream
*
* @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.
*/
public function destination($destination) {
$this->destination = $destination;
}

/**
* Sets the name attribute for StartStream
*
* @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
*/
public function name($name) {
$this->name = $name;
}

/**
* Sets the tracks attribute for StartStream
*
* @param string $tracks The part of the call to send a stream from. `inbound`, `outbound` or `both`. Default is `inbound`.
*
*/
public function tracks($tracks) {
$this->tracks = $tracks;
}

/**
* Sets the username attribute for StartStream
*
* @param string $username The username to send in the HTTP request to `streamEventUrl`. If specified, the URLs must be TLS-encrypted (i.e., `https`).
*/
public function username($username) {
$this->username = $username;
}

/**
* Sets the password attribute for StartStream
*
* @param string $password The password to send in the HTTP request to `streamEventUrl`. If specified, the URLs must be TLS-encrypted (i.e., `https`).
*/
public function password($password) {
$this->password = $password;
}

/**
* Sets the streamEventUrl attribute for StartStream
*
* @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.
*/
public function streamEventUrl($streamEventUrl) {
$this->streamEventUrl = $streamEventUrl;
}

/**
* Sets the streamEventMethod attribute for StartStream
*
* @param bool $streamEventMethod The HTTP method to use for the request to `streamEventUrl`. GET or POST. Default value is POST.
*/
public function streamEventMethod($streamEventMethod) {
$this->streamEventMethod = $streamEventMethod;
}

public function toBxml($doc) {
$element = $doc->createElement("StartStream");

if(isset($this->destination)) {
$element->setattribute("destination", $this->destination);
}

if(isset($this->name)) {
$element->setattribute("name", $this->name);
}

if(isset($this->tracks)) {
$element->setattribute("tracks", $this->tracks);
}

if(isset($this->username)) {
$element->setattribute("username", $this->username);
}

if(isset($this->password)) {
$element->setattribute("password", $this->password);
}

if(isset($this->streamEventUrl)) {
$element->setattribute("streamEventUrl", $this->streamEventUrl);
}

if(isset($this->streamEventMethod)) {
$element->setattribute("streamEventMethod", $this->streamEventMethod);
}

return $element;
}
}
34 changes: 34 additions & 0 deletions src/Voice/Bxml/StopStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* StopStreamphp
*
* Implementation of the BXML StopStream verb
*
* @copyright Bandwidth INC
*/

namespace BandwidthLib\Voice\Bxml;

require_once "Verb.php";

class StopStream extends Verb {

/**
* Sets the name attribute for StopStream
*
* @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.
*/
public function name($name) {
$this->name = $name;
}

public function toBxml($doc) {
$element = $doc->createElement("StopStream");

if(isset($this->name)) {
$element->setAttribute("name", $this->name);
}

return $element;
}
}
29 changes: 29 additions & 0 deletions tests/BxmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,35 @@ public function testStopRecording() {
$responseXml = $response->toBxml();
$this->assertEquals($expectedXml, $responseXml);
}
public function testStartStream() {
$startStream = new BandwidthLib\Voice\Bxml\StartStream();
$startStream->name("test");
$startStream->tracks("inbound");
$startStream->destination("https://url.com");
$startStream->streamEventMethod("POST");
$startStream->username("user");
$startStream->password("pass");
$startStream->streamEventUrl("https://url.com");

$response = new BandwidthLib\Voice\Bxml\Response();
$response->addVerb($startStream);

$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>';
$responseXml = $response->toBxml();
$this->assertEquals($expectedXml, $responseXml);
}

public function testStopStream() {
$stopStream = new BandwidthLib\Voice\Bxml\StopStream();
$stopStream->name("test");

$response = new BandwidthLib\Voice\Bxml\Response();
$response->addVerb($stopStream);

$expectedXml = '<?xml version="1.0" encoding="UTF-8"?><Response><StopStream name="test"/></Response>';
$responseXml = $response->toBxml();
$this->assertEquals($expectedXml, $responseXml);
}

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

0 comments on commit 75a6ea5

Please sign in to comment.