1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 08:54:44 +01:00
This commit is contained in:
Daniil Gentili 2020-11-29 20:02:50 +01:00
parent 1f608c0a4c
commit 91c10d05ad
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 146 additions and 17 deletions

View File

@ -20,6 +20,7 @@ use danog\MadelineProto\Settings\SecretChats;
use danog\MadelineProto\Settings\Serialization;
use danog\MadelineProto\Settings\Templates;
use danog\MadelineProto\Settings\TLSchema;
use danog\MadelineProto\Settings\VoIP;
/**
* Settings class used for configuring MadelineProto.
@ -82,6 +83,10 @@ class Settings extends SettingsAbstract
* Template settings.
*/
protected Templates $templates;
/**
* VoIP settings.
*/
protected VoIP $voip;
/**
* Create settings object from possibly legacy settings array.
@ -125,6 +130,13 @@ class Settings extends SettingsAbstract
$this->db = new DatabaseMemory;
$this->templates = new Templates;
$this->ipc = new IPc;
$this->voip = new VoIP;
}
public function __wakeup()
{
if (!isset($this->voip)) {
$this->voip = new VoIP;
}
}
/**
* Merge legacy array settings.
@ -149,6 +161,7 @@ class Settings extends SettingsAbstract
$this->serialization->mergeArray($settings);
$this->schema->mergeArray($settings);
$this->ipc->mergeArray($settings);
$this->voip->mergeArray($settings);
switch ($settings['db']['type'] ?? 'memory') {
case 'memory':
@ -203,6 +216,8 @@ class Settings extends SettingsAbstract
$this->ipc->merge($settings);
} elseif ($settings instanceof Templates) {
$this->templates->merge($settings);
} elseif ($settings instanceof VoIP) {
$this->voip->merge($settings);
} elseif ($settings instanceof DatabaseAbstract) {
if (!$this->db instanceof $settings) {
$this->db = $settings;
@ -225,6 +240,7 @@ class Settings extends SettingsAbstract
$this->schema->merge($settings->schema);
$this->ipc->merge($settings->ipc);
$this->templates->merge($settings->templates);
$this->voip->merge($settings->voip);
if (!$this->db instanceof $settings->db) {
$this->db = $settings->db;
@ -610,4 +626,28 @@ class Settings extends SettingsAbstract
return $this;
}
/**
* Get voIP settings.
*
* @return VoIP
*/
public function getVoip(): VoIP
{
return $this->voip;
}
/**
* Set voIP settings.
*
* @param VoIP $voip VoIP settings.
*
* @return self
*/
public function setVoip(VoIP $voip): self
{
$this->voip = $voip;
return $this;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* VoIP settings.
*/
class VoIP extends SettingsAbstract
{
/**
* Whether to preload all songs in memory.
*/
private bool $preloadAudio = true;
/**
* Get whether to preload all songs in memory.
*
* @return bool
*/
public function getPreloadAudio(): bool
{
return $this->preloadAudio;
}
/**
* Set whether to preload all songs in memory.
*
* @param bool $preloadAudio Whether to preload all songs in memory
*
* @return self
*/
public function setPreloadAudio(bool $preloadAudio): self
{
$this->preloadAudio = $preloadAudio;
return $this;
}
}

View File

@ -132,7 +132,7 @@ class VoIP
private $protocol;
private $visualization;
private $holdFiles = [];
private $inputFiles;
private $inputFiles = [];
private $outputFile;
private $isPlaying = false;
@ -247,6 +247,7 @@ class VoIP
if ($this->callState === self::CALL_STATE_ENDED || empty($this->configuration)) {
return false;
}
$this->callState = self::CALL_STATE_ENDED;
Logger::log("Now closing $this");
if (isset($this->timeoutWatcher)) {
Loop::cancel($this->timeoutWatcher);
@ -349,6 +350,17 @@ class VoIP
case self::PKT_INIT_ACK:
yield from $this->startWriteLoop($socket);
break;
case self::PKT_STREAM_DATA:
$cnt = 1;
break;
case self::PKT_STREAM_DATA_X2:
$cnt = 2;
break;
case self::PKT_STREAM_DATA_X3:
$cnt = 3;
break;
}
if (isset($cnt)) {
}
}
/**
@ -359,37 +371,74 @@ class VoIP
*/
private function startWriteLoop(Endpoint $socket): \Generator
{
if ($this->voip_state !== self::STATE_ESTABLISHED) {
$this->voip_state = self::STATE_ESTABLISHED;
if ($this->voip_state === self::STATE_ESTABLISHED) {
return;
}
$this->voip_state = self::STATE_ESTABLISHED;
$ctx = new ConnectionContext;
$ctx->addStream(FileBufferedStream::class, yield open('kda.opus', 'r'));
$stream = yield from $ctx->getStream();
$ogg = yield from Ogg::init($stream, 60000);
$it = $ogg->getEmitter()->iterate();
Tools::callFork($ogg->read());
Tools::callFork((function () use ($it, $socket) {
$timestamp = 0;
$frames = [];
$holdFiles = [];
$timestamp = 0;
while (true) {
$file = \array_shift($this->inputFiles);
if (!$file) {
if (empty($holdFiles)) {
$holdFiles = $this->holdFiles;
}
if (empty($holdFiles)) {
return;
}
$file = \array_shift($holdFiles);
}
$it = yield from $this->openFile($file);
$frames = [];
if ($this->MadelineProto->getSettings()->getVoip()->getPreloadAudio()) {
while (yield $it->advance()) {
$frames []= $it->getCurrent();
}
foreach ($frames as $k => $frame) {
foreach ($frames as $frame) {
$t = (\microtime(true) / 1000) + 60;
if (!yield $this->send_message(['_' => self::PKT_STREAM_DATA, 'stream_id' => 0, 'data' => $frame, 'timestamp' => $timestamp], $socket)) {
Logger::log("Exiting VoIP write loop in $this!");
return;
}
Logger::log("Writing $k in $this!");
//Logger::log("Writing $timestamp in $this!");
yield new Delayed((int) ($t - (\microtime(true) / 1000)));
$timestamp += 60;
}
})());
} else {
while (yield $it->advance()) {
$t = (\microtime(true) / 1000) + 60;
if (!yield $this->send_message(['_' => self::PKT_STREAM_DATA, 'stream_id' => 0, 'data' => $it->getCurrent(), 'timestamp' => $timestamp], $socket)) {
Logger::log("Exiting VoIP write loop in $this!");
return;
}
//Logger::log("Writing $timestamp in $this!");
yield new Delayed((int) ($t - (\microtime(true) / 1000)));
$timestamp += 60;
}
}
}
}
/**
* Open OGG file for reading.
*
* @param string $file
* @return \Generator
*/
private function openFile(string $file): \Generator
{
$ctx = new ConnectionContext;
$ctx->addStream(FileBufferedStream::class, yield open($file, 'r'));
$stream = yield from $ctx->getStream();
$ogg = yield from Ogg::init($stream, 60000);
$it = $ogg->getEmitter()->iterate();
Tools::callFork($ogg->read());
return $it;
}
/**
* Play file.
*

View File

@ -328,7 +328,7 @@ trait MessageHandler
}
}
if (!$this->received_packet($in_seq_no, $out_seq_no, $ack_mask)) {
return false;
return yield from $this->recv_message($endpoint);
}
switch ($result['_']) {
// streamTypeSimple codec:int8 = StreamType;