2018-11-07 14:07:20 +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
|
|
|
|
*/
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/clip/media_clip_check_streaming.h"
|
2018-11-07 14:07:20 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
#include "core/file_location.h"
|
|
|
|
#include "base/bytes.h"
|
|
|
|
#include "logs.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QtEndian>
|
2020-10-13 16:43:18 +00:00
|
|
|
#include <QtCore/QBuffer>
|
2021-10-19 13:00:21 +00:00
|
|
|
#include <QtCore/QFile>
|
2018-11-07 14:07:20 +00:00
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kHeaderSize = 8;
|
|
|
|
constexpr auto kFindMoovBefore = 128 * 1024;
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
Type ReadBigEndian(bytes::const_span data) {
|
|
|
|
const auto bytes = data.subspan(0, sizeof(Type)).data();
|
|
|
|
return qFromBigEndian(*reinterpret_cast<const Type*>(bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAtom(bytes::const_span header, const char (&atom)[5]) {
|
|
|
|
return bytes::compare(
|
|
|
|
header.subspan(4, 4),
|
|
|
|
bytes::make_span(atom).subspan(0, 4)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool CheckStreamingSupport(
|
2020-10-13 16:43:18 +00:00
|
|
|
const Core::FileLocation &location,
|
2018-11-07 14:07:20 +00:00
|
|
|
QByteArray data) {
|
|
|
|
QBuffer buffer;
|
|
|
|
QFile file;
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
file.setFileName(location.name());
|
|
|
|
} else {
|
|
|
|
buffer.setBuffer(&data);
|
|
|
|
}
|
|
|
|
const auto size = data.isEmpty()
|
|
|
|
? file.size()
|
|
|
|
: data.size();
|
|
|
|
const auto device = data.isEmpty()
|
|
|
|
? static_cast<QIODevice*>(&file)
|
|
|
|
: static_cast<QIODevice*>(&buffer);
|
|
|
|
|
|
|
|
if (size < kHeaderSize || !device->open(QIODevice::ReadOnly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lastReadPosition = 0;
|
|
|
|
char atomHeader[kHeaderSize] = { 0 };
|
|
|
|
auto atomHeaderBytes = bytes::make_span(atomHeader);
|
|
|
|
while (true) {
|
|
|
|
const auto position = device->pos();
|
|
|
|
if (device->read(atomHeader, kHeaderSize) != kHeaderSize) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastReadPosition >= kFindMoovBefore) {
|
|
|
|
return false;
|
|
|
|
} else if (IsAtom(atomHeaderBytes, "moov")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto length = [&] {
|
|
|
|
const auto result = ReadBigEndian<uint32>(atomHeaderBytes);
|
|
|
|
if (result != 1) {
|
|
|
|
return uint64(result);
|
|
|
|
}
|
|
|
|
char atomSize64[kHeaderSize] = { 0 };
|
|
|
|
if (device->read(atomSize64, kHeaderSize) != kHeaderSize) {
|
|
|
|
return uint64(-1);
|
|
|
|
}
|
|
|
|
auto atomSize64Bytes = bytes::make_span(atomSize64);
|
|
|
|
return ReadBigEndian<uint64>(atomSize64Bytes);
|
|
|
|
}();
|
|
|
|
if (position + length > size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
device->seek(position + length);
|
|
|
|
lastReadPosition = position;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|