2016-07-05 17:44:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-07-05 17:44:02 +00:00
|
|
|
*/
|
|
|
|
#include "media/media_child_ffmpeg_loader.h"
|
|
|
|
|
2017-12-12 12:03:51 +00:00
|
|
|
#include "core/crash_reports.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
constexpr AVSampleFormat AudioToFormat = AV_SAMPLE_FMT_S16;
|
|
|
|
constexpr int64_t AudioToChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
constexpr int32 AudioToChannels = 2;
|
|
|
|
|
2017-12-12 12:03:51 +00:00
|
|
|
bool IsPlanarFormat(int format) {
|
|
|
|
return (format == AV_SAMPLE_FMT_U8P)
|
|
|
|
|| (format == AV_SAMPLE_FMT_S16P)
|
|
|
|
|| (format == AV_SAMPLE_FMT_S32P)
|
|
|
|
|| (format == AV_SAMPLE_FMT_FLTP)
|
|
|
|
|| (format == AV_SAMPLE_FMT_DBLP)
|
|
|
|
|| (format == AV_SAMPLE_FMT_S64P);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
VideoSoundData::~VideoSoundData() {
|
|
|
|
if (context) {
|
|
|
|
avcodec_close(context);
|
|
|
|
avcodec_free_context(&context);
|
|
|
|
context = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
ChildFFMpegLoader::ChildFFMpegLoader(std::unique_ptr<VideoSoundData> &&data)
|
|
|
|
: AbstractAudioFFMpegLoader(
|
|
|
|
FileLocation(),
|
|
|
|
QByteArray(),
|
|
|
|
base::byte_vector())
|
2017-02-21 13:45:56 +00:00
|
|
|
, _parentData(std::move(data)) {
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 08:52:38 +00:00
|
|
|
bool ChildFFMpegLoader::open(TimeMs positionMs) {
|
2018-01-02 17:22:13 +00:00
|
|
|
return initUsingContext(
|
|
|
|
_parentData->context,
|
|
|
|
_parentData->length,
|
|
|
|
_parentData->frequency);
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
AudioPlayerLoader::ReadResult ChildFFMpegLoader::readMore(
|
|
|
|
QByteArray &result,
|
|
|
|
int64 &samplesAdded) {
|
|
|
|
const auto readResult = readFromReadyContext(
|
|
|
|
_parentData->context,
|
|
|
|
result,
|
|
|
|
samplesAdded);
|
|
|
|
if (readResult != ReadResult::Wait) {
|
|
|
|
return readResult;
|
2016-07-22 15:01:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
if (_queue.isEmpty()) {
|
2016-07-05 17:44:22 +00:00
|
|
|
return _eofReached ? ReadResult::EndOfFile : ReadResult::Wait;
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:02:39 +00:00
|
|
|
AVPacket packet;
|
|
|
|
FFMpeg::packetFromDataWrap(packet, _queue.dequeue());
|
|
|
|
|
2016-07-05 17:44:22 +00:00
|
|
|
_eofReached = FFMpeg::isNullPacket(packet);
|
|
|
|
if (_eofReached) {
|
2016-07-22 15:01:24 +00:00
|
|
|
avcodec_send_packet(_parentData->context, nullptr); // drain
|
|
|
|
return ReadResult::Ok;
|
2016-07-05 17:44:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
auto res = avcodec_send_packet(_parentData->context, &packet);
|
2016-07-22 15:01:24 +00:00
|
|
|
if (res < 0) {
|
2016-07-05 17:44:22 +00:00
|
|
|
FFMpeg::freePacket(&packet);
|
2016-07-22 15:01:24 +00:00
|
|
|
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
|
2017-05-03 11:36:39 +00:00
|
|
|
LOG(("Audio Error: Unable to avcodec_send_packet() file '%1', data size '%2', error %3, %4").arg(_file.name()).arg(_data.size()).arg(res).arg(av_make_error_string(err, sizeof(err), res)));
|
2016-08-14 18:57:23 +00:00
|
|
|
// There is a sample voice message where skipping such packet
|
|
|
|
// results in a crash (read_access to nullptr) in swr_convert().
|
2017-05-26 14:18:58 +00:00
|
|
|
if (res == AVERROR_INVALIDDATA) {
|
|
|
|
return ReadResult::NotYet; // try to skip bad packet
|
|
|
|
}
|
2016-07-05 17:44:02 +00:00
|
|
|
return ReadResult::Error;
|
|
|
|
}
|
2016-07-22 15:01:24 +00:00
|
|
|
FFMpeg::freePacket(&packet);
|
|
|
|
return ReadResult::Ok;
|
|
|
|
}
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2016-07-19 16:02:39 +00:00
|
|
|
void ChildFFMpegLoader::enqueuePackets(QQueue<FFMpeg::AVPacketDataWrap> &packets) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_queue += std::move(packets);
|
2016-07-05 17:44:02 +00:00
|
|
|
packets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildFFMpegLoader::~ChildFFMpegLoader() {
|
2018-01-02 17:22:13 +00:00
|
|
|
for (auto &packetData : base::take(_queue)) {
|
2016-07-19 16:02:39 +00:00
|
|
|
AVPacket packet;
|
|
|
|
FFMpeg::packetFromDataWrap(packet, packetData);
|
2016-07-05 17:44:22 +00:00
|
|
|
FFMpeg::freePacket(&packet);
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
}
|