2019-04-22 18:20:04 +00:00
|
|
|
<?php
|
2019-10-03 19:41:54 +00:00
|
|
|
|
2019-04-22 18:20:04 +00:00
|
|
|
/**
|
|
|
|
* YoutubeChunkStream class.
|
|
|
|
*/
|
|
|
|
|
2019-04-22 19:06:05 +00:00
|
|
|
namespace Alltube\Stream;
|
2019-04-22 18:20:04 +00:00
|
|
|
|
2020-05-13 19:39:38 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2019-04-22 18:20:04 +00:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a wrapper around GuzzleHttp\Psr7\Stream.
|
|
|
|
* It is required because Youtube HTTP responses are buggy if we try to read further than the end of the response.
|
|
|
|
*/
|
|
|
|
class YoutubeChunkStream implements StreamInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* HTTP response containing the video chunk.
|
2019-04-22 19:06:05 +00:00
|
|
|
*
|
2020-05-13 19:39:38 +00:00
|
|
|
* @var ResponseInterface
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
|
|
|
private $response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* YoutubeChunkStream constructor.
|
|
|
|
*
|
2020-05-13 19:39:38 +00:00
|
|
|
* @param ResponseInterface $response HTTP response containing the video chunk
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
2020-05-13 19:39:38 +00:00
|
|
|
public function __construct(ResponseInterface $response)
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
$this->response = $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read data from the stream.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $length Read up to $length bytes from the object and return
|
2019-04-22 18:20:04 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function read($length): string
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
2020-05-13 20:28:05 +00:00
|
|
|
$size = intval($this->response->getHeader('Content-Length')[0]);
|
2019-04-22 18:20:04 +00:00
|
|
|
if ($size - $this->tell() < $length) {
|
|
|
|
// Don't try to read further than the end of the stream.
|
|
|
|
$length = $size - $this->tell();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->getBody()->read($length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads all data from the stream into a string, from the beginning to end.
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function __toString(): string
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
2020-05-13 20:28:05 +00:00
|
|
|
return (string)$this->response->getBody();
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the stream and any underlying resources.
|
|
|
|
*
|
2019-04-28 13:57:21 +00:00
|
|
|
* @return void
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
public function close(): void
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
2019-04-28 13:57:21 +00:00
|
|
|
$this->response->getBody()->close();
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Separates any underlying resources from the stream.
|
|
|
|
*
|
|
|
|
* @return resource|null
|
|
|
|
*/
|
|
|
|
public function detach()
|
|
|
|
{
|
|
|
|
return $this->response->getBody()->detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the stream if known.
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getSize(): ?int
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current position of the file read/write pointer.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function tell(): int
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->tell();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the stream is at the end of the stream.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function eof(): bool
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->eof();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the stream is seekable.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function isSeekable(): bool
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->isSeekable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seek to a position in the stream.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $offset Stream offset
|
2019-04-22 18:20:04 +00:00
|
|
|
* @param int $whence Specifies how the cursor position will be calculated
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @return void
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
public function seek($offset, $whence = SEEK_SET): void
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
2020-05-13 19:18:32 +00:00
|
|
|
$this->response->getBody()->seek($offset, $whence);
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seek to the beginning of the stream.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @return void
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
public function rewind(): void
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
2020-05-13 19:18:32 +00:00
|
|
|
$this->response->getBody()->rewind();
|
2019-04-22 18:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the stream is writable.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function isWritable(): bool
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->isWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data to the stream.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $string The string that is to be written
|
2019-04-22 18:20:04 +00:00
|
|
|
*
|
2022-02-03 19:21:25 +00:00
|
|
|
* @return int
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
2022-02-03 19:21:25 +00:00
|
|
|
public function write($string): int
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->write($string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the stream is readable.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function isReadable(): bool
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->isReadable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 19:06:05 +00:00
|
|
|
* Returns the remaining contents in a string.
|
2019-04-22 18:20:04 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getContents(): string
|
2019-04-22 18:20:04 +00:00
|
|
|
{
|
|
|
|
return $this->response->getBody()->getContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get stream metadata as an associative array or retrieve a specific key.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param string|null $key Specific metadata to retrieve.
|
2019-04-22 18:20:04 +00:00
|
|
|
*
|
2020-12-17 21:43:05 +00:00
|
|
|
* @return mixed|null
|
2019-04-22 18:20:04 +00:00
|
|
|
*/
|
|
|
|
public function getMetadata($key = null)
|
|
|
|
{
|
|
|
|
return $this->response->getBody()->getMetadata($key);
|
|
|
|
}
|
|
|
|
}
|