setName('play')
->setDescription('Play a stream')
->addArgument('stream', InputArgument::REQUIRED, 'Name of the stream')
// ->addOption('bar', null, InputOption::VALUE_NONE, '')
->setHelp("The %command.name% ");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = $this->getApplication()->getConfigLoader()->getConfig();
$stream = strtolower(trim($input->getArgument('stream'), "/ \r\n"));
if (0 === preg_match('/^\w+$/', $stream)) {
$output->writeln('The stream name is not valid.');
return false;
}
$output->writeln(sprintf('Playing %s', $stream));
if (!empty($config['player']['before'])) {
$command = $config['player']['before'];
// $process = new Process($command);
// $process->run();
}
$hash = $this->getHash($this->getApplication()->buildUrl($stream.'/'));
$command = $this->getApplication()->getPlayer($stream.'?t='.$hash);
$process = new Process($command);
if ($this->getApplication()->isDebugMode()) {
$callback = function ($type, $buffer) use ($output) {
if ('err' !== $type) {
$output->writeln($buffer);
}
};
} else {
$callback = function ($type, $buffer) use ($output) {
if ('err' === $type) {
$output->writeln(sprintf('%s', $buffer));
} else {
$output->writeln($buffer);
}
};
}
$process->run($callback);
}
protected function getHash($streamUrl)
{
$page = file_get_contents($streamUrl);
preg_match('/var streamURL = ".+\?t=([^"]+)"/s', $page, $match);
if (!isset($match[1])) {
throw new RuntimeException('Hash is not accessible.');
}
return $match[1];
}
}