Created
October 11, 2016 12:21
-
-
Save saltandvinegarcrisps/8f5218aa2e020c7eca13a261f4df07e3 to your computer and use it in GitHub Desktop.
PHP WowzaSecureToken Class for Wowza Streaming Engine 4.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WowzaSecureToken { | |
protected $prefix = ''; | |
protected $secret = ''; | |
protected $client = ''; | |
protected $host = 'localhost'; | |
protected $port = ''; | |
protected $stream = ''; | |
protected $application = ''; | |
protected $params = []; | |
public function __construct(string $prefix, string $secret) { | |
$this->prefix = $prefix; | |
$this->secret = $secret; | |
} | |
public function setClientIp(string $client) { | |
$this->client = $client; | |
} | |
public function setHost(string $host) { | |
$this->host = $host; | |
} | |
public function setPort(string $post) { | |
$this->post = $post; | |
} | |
public function setStreamName(string $stream) { | |
$this->stream = $stream; | |
} | |
public function setApplicationName(string $application) { | |
$this->application = $application; | |
} | |
public function setParams(array $params) { | |
$this->params = $params; | |
} | |
public function getParams(): array { | |
return $this->params; | |
} | |
public function getStringForHash(): string { | |
$params = []; | |
if($this->client) { | |
$params[] = $this->client; | |
} | |
$params[] = $this->secret; | |
foreach($this->getParams() as $key => $value) { | |
$params[] = $this->prefix . $key . '=' . $value; | |
} | |
sort($params, SORT_STRING); | |
return $this->application . '/' . $this->stream . '?' . implode('&', $params); | |
} | |
public function getHash(): string { | |
return hash('sha256', $this->getStringForHash(), true); | |
} | |
public function getQueryString(): string { | |
$params = $this->getParams(); | |
$hash = $this->getHash(); | |
$encodedHash = base64_encode($hash); | |
$urlSafeHash = str_replace(['+', '/'], ['-', '_'], $encodedHash); | |
$params['hash'] = $urlSafeHash; | |
$prefixParams = []; | |
foreach($params as $key => $value) { | |
$prefixParams[$this->prefix.$key] = $value; | |
} | |
$queryParts = []; | |
foreach($prefixParams as $key => $value) { | |
$queryParts[] = sprintf('%s=%s', $key, $value); | |
} | |
return implode('&', $queryParts); | |
} | |
public function getHttpUrl(): string { | |
return sprintf('http://%s/%s/%s/playlist.m3u8?%s', | |
$this->host . ($this->port ? ':' . $this->port : ''), | |
$this->application, | |
$this->stream, | |
$this->getQueryString() | |
); | |
} | |
public function getHttpsUrl(): string { | |
return sprintf('https://%s/%s/%s/playlist.m3u8?%s', | |
$this->host . ($this->port ? ':' . $this->port : ''), | |
$this->application, | |
$this->stream, | |
$this->getQueryString() | |
); | |
} | |
public function getHttpDvrUrl(): string { | |
return sprintf('http://%s/%s/%s/playlist.m3u8?DVR&%s', | |
$this->host . ($this->port ? ':' . $this->port : ''), | |
$this->application, | |
$this->stream, | |
$this->getQueryString() | |
); | |
} | |
public function getHttpsDvrUrl(): string { | |
return sprintf('https://%s/%s/%s/playlist.m3u8?DVR&%s', | |
$this->host . ($this->port ? ':' . $this->port : ''), | |
$this->application, | |
$this->stream, | |
$this->getQueryString() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment