2014-06-04 21:37:21 +02:00
|
|
|
<?php
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Radio\Frontend;
|
2014-06-04 21:37:21 +02:00
|
|
|
|
2016-11-22 06:09:18 +01:00
|
|
|
use App\Utilities;
|
2017-04-17 10:57:42 +02:00
|
|
|
use Doctrine\ORM\EntityManager;
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Entity;
|
2014-06-04 21:37:21 +02:00
|
|
|
|
2018-02-04 13:33:46 +01:00
|
|
|
class SHOUTcast extends FrontendAbstract
|
2014-06-04 21:37:21 +02:00
|
|
|
{
|
2018-09-07 03:08:47 +02:00
|
|
|
public function getWatchCommand(): ?string
|
2017-09-07 05:53:25 +02:00
|
|
|
{
|
|
|
|
$fe_config = (array)$this->station->getFrontendConfig();
|
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
$mount_name = $this->_getDefaultMountSid();
|
|
|
|
|
2017-09-07 05:53:25 +02:00
|
|
|
return $this->_getStationWatcherCommand(
|
2017-09-27 06:33:00 +02:00
|
|
|
'shoutcast2',
|
2018-09-13 19:45:56 +02:00
|
|
|
'http://localhost:' . $fe_config['port'] . '/stats?sid='.$mount_name
|
2017-09-07 05:53:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
public function getNowPlaying($payload = null, $include_clients = true): array
|
2017-01-22 21:58:40 +01:00
|
|
|
{
|
2017-08-17 20:28:48 +02:00
|
|
|
$fe_config = (array)$this->station->getFrontendConfig();
|
2017-01-24 01:17:50 +01:00
|
|
|
$radio_port = $fe_config['port'];
|
2014-06-05 08:48:36 +02:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
$base_url = 'http://' . (APP_INSIDE_DOCKER ? 'stations' : 'localhost') . ':' . $radio_port;
|
2017-09-07 05:53:25 +02:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
$np_adapter = new \NowPlaying\Adapter\SHOUTcast2($base_url, $this->http_client);
|
|
|
|
$np_adapter->setAdminPassword($fe_config['admin_pw']);
|
2017-01-24 01:17:50 +01:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
$mount_name = $this->_getDefaultMountSid();
|
2017-05-18 09:24:54 +02:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
try {
|
|
|
|
$np = $np_adapter->getNowPlaying($mount_name, $payload);
|
2017-08-17 20:28:48 +02:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
$this->logger->debug('NowPlaying adapter response', ['response' => $np]);
|
2017-05-18 09:24:54 +02:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
if ($include_clients) {
|
|
|
|
$np['listeners']['clients'] = $np_adapter->getClients($mount_name, true);
|
|
|
|
$np['listeners']['unique'] = count($np['listeners']['clients']);
|
2017-05-18 09:24:54 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
return $np;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
$this->logger->error(sprintf('NowPlaying adapter error: %s', $e->getMessage()));
|
|
|
|
return \NowPlaying\Adapter\AdapterAbstract::NOWPLAYING_EMPTY;
|
2017-05-16 09:46:43 +02:00
|
|
|
}
|
2014-06-05 08:48:36 +02:00
|
|
|
}
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2018-05-26 23:26:34 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2018-09-07 03:08:47 +02:00
|
|
|
public function read(): bool
|
2016-11-22 06:09:18 +01:00
|
|
|
{
|
2017-01-24 01:17:50 +01:00
|
|
|
$config = $this->_getConfig();
|
2018-03-10 16:37:35 +01:00
|
|
|
$this->station->setFrontendConfigDefaults($this->_loadFromConfig($config));
|
2017-01-24 01:17:50 +01:00
|
|
|
return true;
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 03:08:47 +02:00
|
|
|
public function write(): bool
|
2016-11-22 06:09:18 +01:00
|
|
|
{
|
|
|
|
$config = $this->_getDefaults();
|
|
|
|
|
2017-08-17 20:28:48 +02:00
|
|
|
$frontend_config = (array)$this->station->getFrontendConfig();
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2017-01-24 01:35:16 +01:00
|
|
|
if (!empty($frontend_config['port'])) {
|
2016-11-22 06:09:18 +01:00
|
|
|
$config['portbase'] = $frontend_config['port'];
|
2017-01-24 01:35:16 +01:00
|
|
|
}
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2017-01-24 01:35:16 +01:00
|
|
|
if (!empty($frontend_config['source_pw'])) {
|
2016-11-22 06:09:18 +01:00
|
|
|
$config['password'] = $frontend_config['source_pw'];
|
2017-01-24 01:35:16 +01:00
|
|
|
}
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2017-01-24 01:35:16 +01:00
|
|
|
if (!empty($frontend_config['admin_pw'])) {
|
2016-11-22 06:09:18 +01:00
|
|
|
$config['adminpassword'] = $frontend_config['admin_pw'];
|
2017-01-24 01:35:16 +01:00
|
|
|
}
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2017-05-24 01:18:27 +02:00
|
|
|
if (!empty($frontend_config['max_listeners'])) {
|
|
|
|
$config['maxuser'] = $frontend_config['max_listeners'];
|
|
|
|
}
|
|
|
|
|
2017-01-24 01:35:16 +01:00
|
|
|
if (!empty($frontend_config['custom_config'])) {
|
2016-11-22 06:09:18 +01:00
|
|
|
$custom_conf = $this->_processCustomConfig($frontend_config['custom_config']);
|
2017-01-24 01:35:16 +01:00
|
|
|
if (!empty($custom_conf)) {
|
2016-11-22 06:09:18 +01:00
|
|
|
$config = array_merge($config, $custom_conf);
|
2017-01-24 01:35:16 +01:00
|
|
|
}
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:57:42 +02:00
|
|
|
$i = 0;
|
2017-08-17 20:28:48 +02:00
|
|
|
foreach ($this->station->getMounts() as $mount_row) {
|
|
|
|
/** @var Entity\StationMount $mount_row */
|
2017-04-17 10:57:42 +02:00
|
|
|
$i++;
|
|
|
|
$config['streamid_'.$i] = $i;
|
2017-08-17 20:28:48 +02:00
|
|
|
$config['streampath_'.$i] = $mount_row->getName();
|
2017-04-17 10:57:42 +02:00
|
|
|
|
2017-08-17 20:28:48 +02:00
|
|
|
if ($mount_row->getRelayUrl()) {
|
|
|
|
$config['streamrelayurl_'.$i] = $mount_row->getRelayUrl();
|
2017-04-17 10:57:42 +02:00
|
|
|
}
|
2017-06-19 06:44:07 +02:00
|
|
|
|
2017-08-17 20:28:48 +02:00
|
|
|
if ($mount_row->getAuthhash()) {
|
|
|
|
$config['streamauthhash_' . $i] = $mount_row->getAuthhash();
|
2017-06-19 06:44:07 +02:00
|
|
|
}
|
2017-04-17 10:57:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 06:09:18 +01:00
|
|
|
// Set any unset values back to the DB config.
|
2018-03-10 17:01:03 +01:00
|
|
|
$this->station->setFrontendConfigDefaults($this->_loadFromConfig($config));
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2018-03-06 18:42:01 +01:00
|
|
|
$this->em->persist($this->station);
|
|
|
|
$this->em->flush();
|
2016-11-22 06:09:18 +01:00
|
|
|
|
|
|
|
$config_path = $this->station->getRadioConfigDir();
|
2017-01-24 01:35:16 +01:00
|
|
|
$sc_path = $config_path . '/sc_serv.conf';
|
2016-11-22 06:09:18 +01:00
|
|
|
|
|
|
|
$sc_file = '';
|
2017-01-24 01:35:16 +01:00
|
|
|
foreach ($config as $config_key => $config_value) {
|
|
|
|
$sc_file .= $config_key . '=' . str_replace("\n", "", $config_value) . "\n";
|
|
|
|
}
|
2016-11-22 06:09:18 +01:00
|
|
|
|
|
|
|
file_put_contents($sc_path, $sc_file);
|
2018-09-07 03:39:49 +02:00
|
|
|
return true;
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 01:17:50 +01:00
|
|
|
/*
|
|
|
|
* Process Management
|
|
|
|
*/
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2018-09-07 03:08:47 +02:00
|
|
|
public function getCommand(): ?string
|
2016-11-22 06:09:18 +01:00
|
|
|
{
|
2017-06-12 22:29:03 +02:00
|
|
|
if ($binary = self::getBinary()) {
|
|
|
|
$config_path = $this->station->getRadioConfigDir();
|
|
|
|
$sc_config = $config_path . '/sc_serv.conf';
|
2016-11-22 06:09:18 +01:00
|
|
|
|
2017-06-12 22:29:03 +02:00
|
|
|
return $binary . ' ' . $sc_config;
|
|
|
|
}
|
2018-02-04 13:33:46 +01:00
|
|
|
|
|
|
|
return '/bin/false';
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 03:08:47 +02:00
|
|
|
public function getAdminUrl(): string
|
2017-01-24 01:17:50 +01:00
|
|
|
{
|
2017-01-24 01:35:16 +01:00
|
|
|
return $this->getPublicUrl() . '/admin.cgi';
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configuration
|
|
|
|
*/
|
|
|
|
|
2017-01-24 01:17:50 +01:00
|
|
|
protected function _getConfig()
|
2016-11-22 06:09:18 +01:00
|
|
|
{
|
2017-01-24 01:17:50 +01:00
|
|
|
$config_dir = $this->station->getRadioConfigDir();
|
2018-02-04 13:33:46 +01:00
|
|
|
return @parse_ini_file($config_dir . '/sc_serv.conf', false, INI_SCANNER_RAW);
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 03:39:49 +02:00
|
|
|
protected function _loadFromConfig($config): array
|
2016-11-22 06:09:18 +01:00
|
|
|
{
|
2017-01-24 01:17:50 +01:00
|
|
|
return [
|
|
|
|
'port' => $config['portbase'],
|
|
|
|
'source_pw' => $config['password'],
|
|
|
|
'admin_pw' => $config['adminpassword'],
|
2017-05-24 01:18:27 +02:00
|
|
|
'max_listeners' => $config['maxuser'],
|
2017-01-24 01:17:50 +01:00
|
|
|
];
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 03:39:49 +02:00
|
|
|
protected function _getDefaults(): array
|
2016-11-22 06:09:18 +01:00
|
|
|
{
|
2017-01-24 01:17:50 +01:00
|
|
|
$config_path = $this->station->getRadioConfigDir();
|
2017-01-22 21:58:40 +01:00
|
|
|
|
2017-01-24 01:17:50 +01:00
|
|
|
$defaults = [
|
2017-01-24 01:35:16 +01:00
|
|
|
'password' => Utilities::generatePassword(),
|
2017-01-24 01:17:50 +01:00
|
|
|
'adminpassword' => Utilities::generatePassword(),
|
2017-01-24 01:35:16 +01:00
|
|
|
'logfile' => $config_path . '/sc_serv.log',
|
|
|
|
'w3clog' => $config_path . '/sc_w3c.log',
|
|
|
|
'banfile' => $config_path . '/sc_serv.ban',
|
|
|
|
'ripfile' => $config_path . '/sc_serv.rip',
|
2017-05-24 01:18:27 +02:00
|
|
|
'maxuser' => 250,
|
2017-01-24 01:35:16 +01:00
|
|
|
'portbase' => $this->_getRadioPort(),
|
2017-04-23 03:48:19 +02:00
|
|
|
'requirestreamconfigs' => 1,
|
2016-11-22 06:09:18 +01:00
|
|
|
];
|
|
|
|
|
2017-01-24 01:17:50 +01:00
|
|
|
return $defaults;
|
2016-11-22 06:09:18 +01:00
|
|
|
}
|
2017-05-19 08:38:17 +02:00
|
|
|
|
2018-09-13 19:45:56 +02:00
|
|
|
protected function _getDefaultMountSid(): int
|
|
|
|
{
|
|
|
|
$default_sid = null;
|
|
|
|
$sid = 0;
|
|
|
|
foreach($this->station->getMounts() as $mount) {
|
|
|
|
/** @var Entity\StationMount $mount */
|
|
|
|
$sid++;
|
|
|
|
|
|
|
|
if ($mount->getIsDefault()) {
|
|
|
|
$default_sid = $sid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default_sid ?? 1;
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:29:03 +02:00
|
|
|
public static function getBinary()
|
2017-05-19 08:38:17 +02:00
|
|
|
{
|
2018-02-04 13:33:46 +01:00
|
|
|
$new_path = dirname(APP_INCLUDE_ROOT) . '/servers/shoutcast2/sc_serv';
|
2017-06-12 22:29:03 +02:00
|
|
|
|
|
|
|
if (APP_INSIDE_DOCKER || file_exists($new_path)) {
|
|
|
|
return $new_path;
|
|
|
|
}
|
2018-02-04 13:33:46 +01:00
|
|
|
|
|
|
|
return false;
|
2017-05-19 08:38:17 +02:00
|
|
|
}
|
2018-08-05 00:05:14 +02:00
|
|
|
}
|