2019-02-13 18:10:18 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "media/streaming/media_streaming_common.h"
|
2019-02-17 06:54:57 +00:00
|
|
|
#include "media/streaming/media_streaming_utility.h"
|
2019-02-13 18:10:18 +00:00
|
|
|
#include "media/streaming/media_streaming_reader.h"
|
|
|
|
#include "base/bytes.h"
|
|
|
|
#include "base/weak_ptr.h"
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
class Session;
|
|
|
|
} // namespace Data
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
|
|
|
|
|
|
|
class Loader;
|
2019-02-17 06:54:57 +00:00
|
|
|
class FileDelegate;
|
2019-02-13 18:10:18 +00:00
|
|
|
|
|
|
|
class File final {
|
|
|
|
public:
|
|
|
|
File(not_null<Data::Session*> owner, std::unique_ptr<Loader> loader);
|
|
|
|
|
|
|
|
File(const File &other) = delete;
|
|
|
|
File &operator=(const File &other) = delete;
|
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
void start(not_null<FileDelegate*> delegate, crl::time position);
|
|
|
|
void wake();
|
|
|
|
void stop();
|
2019-02-13 18:10:18 +00:00
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
[[nodiscard]] bool isRemoteLoader() const;
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
~File();
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Context final : public base::has_weak_ptr {
|
|
|
|
public:
|
2019-02-17 06:54:57 +00:00
|
|
|
Context(not_null<FileDelegate*> delegate, not_null<Reader*> reader);
|
2019-02-13 18:10:18 +00:00
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
void start(crl::time position);
|
2019-02-13 18:10:18 +00:00
|
|
|
void readNextPacket();
|
|
|
|
|
|
|
|
void interrupt();
|
2019-02-20 13:28:48 +00:00
|
|
|
void wake();
|
2019-02-13 18:10:18 +00:00
|
|
|
[[nodiscard]] bool interrupted() const;
|
|
|
|
[[nodiscard]] bool failed() const;
|
|
|
|
[[nodiscard]] bool finished() const;
|
|
|
|
|
|
|
|
~Context();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int Read(void *opaque, uint8_t *buffer, int bufferSize);
|
|
|
|
static int64_t Seek(void *opaque, int64_t offset, int whence);
|
|
|
|
|
|
|
|
[[nodiscard]] int read(bytes::span buffer);
|
|
|
|
[[nodiscard]] int64_t seek(int64_t offset, int whence);
|
|
|
|
|
|
|
|
[[nodiscard]] bool unroll() const;
|
|
|
|
void logError(QLatin1String method);
|
|
|
|
void logError(QLatin1String method, AvErrorWrap error);
|
|
|
|
void logFatal(QLatin1String method);
|
|
|
|
void logFatal(QLatin1String method, AvErrorWrap error);
|
2019-03-05 13:56:27 +00:00
|
|
|
void fail(Error error);
|
2019-02-13 18:10:18 +00:00
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
Stream initStream(
|
|
|
|
not_null<AVFormatContext *> format,
|
|
|
|
AVMediaType type);
|
|
|
|
void seekToPosition(
|
|
|
|
not_null<AVFormatContext *> format,
|
|
|
|
const Stream &stream,
|
|
|
|
crl::time position);
|
2019-02-13 18:10:18 +00:00
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
// TODO base::expected.
|
2019-02-13 18:10:18 +00:00
|
|
|
[[nodiscard]] base::variant<Packet, AvErrorWrap> readPacket();
|
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
void handleEndOfFile();
|
|
|
|
|
|
|
|
const not_null<FileDelegate*> _delegate;
|
|
|
|
const not_null<Reader*> _reader;
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
int _offset = 0;
|
|
|
|
int _size = 0;
|
|
|
|
bool _failed = false;
|
|
|
|
bool _readTillEnd = false;
|
|
|
|
crl::semaphore _semaphore;
|
|
|
|
std::atomic<bool> _interrupted = false;
|
|
|
|
|
2019-03-04 11:28:52 +00:00
|
|
|
FormatPointer _format;
|
2019-02-13 18:10:18 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
std::optional<Context> _context;
|
2019-02-13 18:10:18 +00:00
|
|
|
Reader _reader;
|
2019-02-17 06:54:57 +00:00
|
|
|
std::thread _thread;
|
2019-02-13 18:10:18 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|