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
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
|
|
|
#include "media/audio/media_audio_loader.h"
|
2019-02-28 21:03:25 +00:00
|
|
|
#include "media/streaming/media_streaming_utility.h"
|
2016-07-05 17:44:02 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
#include <libswresample/swresample.h>
|
|
|
|
} // extern "C"
|
|
|
|
|
2020-03-31 22:04:33 +00:00
|
|
|
#include <al.h>
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
namespace Core {
|
|
|
|
class FileLocation;
|
|
|
|
} // namespace Core
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
namespace Media {
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
class AbstractFFMpegLoader : public AudioPlayerLoader {
|
|
|
|
public:
|
2018-01-02 16:18:53 +00:00
|
|
|
AbstractFFMpegLoader(
|
2020-10-13 16:43:18 +00:00
|
|
|
const Core::FileLocation &file,
|
2018-01-02 16:18:53 +00:00
|
|
|
const QByteArray &data,
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector &&buffer)
|
|
|
|
: AudioPlayerLoader(file, data, std::move(buffer)) {
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
bool open(crl::time positionMs) override;
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
int64 samplesCount() override {
|
|
|
|
return _samplesCount;
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 16:18:53 +00:00
|
|
|
int samplesFrequency() override {
|
2017-05-03 13:01:15 +00:00
|
|
|
return _samplesFrequency;
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 16:18:53 +00:00
|
|
|
static uint64_t ComputeChannelLayout(
|
|
|
|
uint64_t channel_layout,
|
|
|
|
int channels);
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
~AbstractFFMpegLoader();
|
|
|
|
|
|
|
|
protected:
|
2018-01-02 16:18:53 +00:00
|
|
|
static int64 Mul(int64 value, AVRational rational);
|
|
|
|
|
|
|
|
int _samplesFrequency = Media::Player::kDefaultFrequency;
|
2017-05-03 13:01:15 +00:00
|
|
|
int64 _samplesCount = 0;
|
2016-07-05 17:44:02 +00:00
|
|
|
|
|
|
|
uchar *ioBuffer = nullptr;
|
|
|
|
AVIOContext *ioContext = nullptr;
|
|
|
|
AVFormatContext *fmtContext = nullptr;
|
2022-02-08 15:20:27 +00:00
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR >= 59
|
|
|
|
const AVCodec *codec = nullptr;
|
|
|
|
#else
|
2016-07-05 17:44:02 +00:00
|
|
|
AVCodec *codec = nullptr;
|
2022-02-08 15:20:27 +00:00
|
|
|
#endif
|
2016-07-05 17:44:02 +00:00
|
|
|
int32 streamId = 0;
|
|
|
|
|
|
|
|
bool _opened = false;
|
|
|
|
|
|
|
|
private:
|
2022-09-03 15:07:11 +00:00
|
|
|
static int ReadData(void *opaque, uint8_t *buf, int buf_size);
|
|
|
|
static int64_t SeekData(void *opaque, int64_t offset, int whence);
|
|
|
|
static int ReadBytes(void *opaque, uint8_t *buf, int buf_size);
|
|
|
|
static int64_t SeekBytes(void *opaque, int64_t offset, int whence);
|
|
|
|
static int ReadFile(void *opaque, uint8_t *buf, int buf_size);
|
|
|
|
static int64_t SeekFile(void *opaque, int64_t offset, int whence);
|
2016-07-05 17:44:02 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
class AbstractAudioFFMpegLoader : public AbstractFFMpegLoader {
|
2016-07-05 17:44:02 +00:00
|
|
|
public:
|
2018-01-02 17:22:13 +00:00
|
|
|
AbstractAudioFFMpegLoader(
|
2020-10-13 16:43:18 +00:00
|
|
|
const Core::FileLocation &file,
|
2018-01-02 16:18:53 +00:00
|
|
|
const QByteArray &data,
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector &&buffer);
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2018-01-02 16:18:53 +00:00
|
|
|
int64 samplesCount() override {
|
2018-01-02 17:22:13 +00:00
|
|
|
return _outputSamplesCount;
|
2018-01-02 16:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int samplesFrequency() override {
|
|
|
|
return _swrDstRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int format() override {
|
2018-01-02 17:22:13 +00:00
|
|
|
return _outputFormat;
|
2016-07-05 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
~AbstractAudioFFMpegLoader();
|
2016-07-05 17:44:02 +00:00
|
|
|
|
|
|
|
protected:
|
2018-01-02 17:22:13 +00:00
|
|
|
bool initUsingContext(
|
2019-02-28 21:03:25 +00:00
|
|
|
not_null<AVCodecContext *> context,
|
2018-01-02 17:22:13 +00:00
|
|
|
int64 initialCount,
|
|
|
|
int initialFrequency);
|
|
|
|
ReadResult readFromReadyContext(
|
2019-02-28 21:03:25 +00:00
|
|
|
not_null<AVCodecContext *> context,
|
2018-01-02 17:22:13 +00:00
|
|
|
QByteArray &result,
|
|
|
|
int64 &samplesAdded);
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
// Streaming player provides the first frame to the ChildFFMpegLoader
|
|
|
|
// so we replace our allocated frame with the one provided.
|
|
|
|
ReadResult replaceFrameAndRead(
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::FramePointer frame,
|
2019-02-17 11:08:29 +00:00
|
|
|
QByteArray &result,
|
|
|
|
int64 &samplesAdded);
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
int sampleSize() const {
|
|
|
|
return _outputSampleSize;
|
|
|
|
}
|
2016-07-05 17:44:02 +00:00
|
|
|
|
|
|
|
private:
|
2016-07-22 15:01:24 +00:00
|
|
|
ReadResult readFromReadyFrame(QByteArray &result, int64 &samplesAdded);
|
2018-01-02 16:18:53 +00:00
|
|
|
bool frameHasDesiredFormat() const;
|
|
|
|
bool initResampleForFrame();
|
|
|
|
bool initResampleUsingFormat();
|
|
|
|
bool ensureResampleSpaceAvailable(int samples);
|
|
|
|
|
2018-01-02 17:22:13 +00:00
|
|
|
void appendSamples(
|
|
|
|
QByteArray &result,
|
|
|
|
int64 &samplesAdded,
|
|
|
|
uint8_t **data,
|
|
|
|
int count) const;
|
|
|
|
|
2019-06-26 15:04:38 +00:00
|
|
|
FFmpeg::FramePointer _frame;
|
2018-01-02 17:22:13 +00:00
|
|
|
int _outputFormat = AL_FORMAT_STEREO16;
|
|
|
|
int _outputChannels = 2;
|
|
|
|
int _outputSampleSize = 2 * sizeof(uint16);
|
|
|
|
int64 _outputSamplesCount = 0;
|
2018-01-02 16:18:53 +00:00
|
|
|
|
|
|
|
SwrContext *_swrContext = nullptr;
|
|
|
|
|
|
|
|
int _swrSrcRate = 0;
|
2018-01-02 17:22:13 +00:00
|
|
|
AVSampleFormat _swrSrcSampleFormat = AV_SAMPLE_FMT_NONE;
|
2018-01-02 16:18:53 +00:00
|
|
|
uint64_t _swrSrcChannelLayout = 0;
|
|
|
|
|
|
|
|
const int _swrDstRate = Media::Player::kDefaultFrequency;
|
2018-01-02 17:22:13 +00:00
|
|
|
AVSampleFormat _swrDstSampleFormat = AV_SAMPLE_FMT_S16;
|
2018-01-02 16:18:53 +00:00
|
|
|
uint64_t _swrDstChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
uint8_t **_swrDstData = nullptr;
|
|
|
|
int _swrDstDataCapacity = 0;
|
2016-07-05 17:44:02 +00:00
|
|
|
|
|
|
|
};
|
2018-01-02 17:22:13 +00:00
|
|
|
|
|
|
|
class FFMpegLoader : public AbstractAudioFFMpegLoader {
|
|
|
|
public:
|
|
|
|
FFMpegLoader(
|
2020-10-13 16:43:18 +00:00
|
|
|
const Core::FileLocation &file,
|
2018-01-02 17:22:13 +00:00
|
|
|
const QByteArray &data,
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector &&buffer);
|
2018-01-02 17:22:13 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
bool open(crl::time positionMs) override;
|
2018-01-02 17:22:13 +00:00
|
|
|
|
|
|
|
ReadResult readMore(QByteArray &result, int64 &samplesAdded) override;
|
|
|
|
|
|
|
|
~FFMpegLoader();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool openCodecContext();
|
2019-02-19 06:57:53 +00:00
|
|
|
bool seekTo(crl::time positionMs);
|
2018-01-02 17:22:13 +00:00
|
|
|
|
|
|
|
AVCodecContext *_codecContext = nullptr;
|
|
|
|
AVPacket _packet;
|
|
|
|
|
|
|
|
};
|
2019-02-28 21:03:25 +00:00
|
|
|
|
|
|
|
} // namespace Media
|