2014-09-04 07:33:44 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-09-04 07:33:44 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
#include "storage/localimageloader.h"
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
struct VideoSoundData;
|
|
|
|
struct VideoSoundPart;
|
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
namespace Media {
|
2017-05-03 11:36:39 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
// Thread: Main.
|
|
|
|
void Start();
|
|
|
|
void Finish();
|
|
|
|
|
|
|
|
// Thread: Main. Locks: AudioMutex.
|
|
|
|
bool IsAttachedToDevice();
|
|
|
|
|
|
|
|
// Thread: Any. Must be locked: AudioMutex.
|
|
|
|
bool AttachToDevice();
|
|
|
|
|
|
|
|
// Thread: Any.
|
|
|
|
void ScheduleDetachFromDeviceSafe();
|
|
|
|
void ScheduleDetachIfNotUsedSafe();
|
|
|
|
void StopDetachIfNotUsedSafe();
|
|
|
|
|
2017-05-07 19:09:20 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void IterateSamples();
|
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
} // namespace Audio
|
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
namespace Player {
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
constexpr auto kDefaultFrequency = 48000; // 48 kHz
|
|
|
|
constexpr auto kTogetherLimit = 4;
|
2017-02-10 22:37:37 +00:00
|
|
|
constexpr auto kWaveformSamplesCount = 100;
|
2017-01-24 21:24:39 +00:00
|
|
|
|
|
|
|
class Fader;
|
|
|
|
class Loaders;
|
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
base::Observable<AudioMsgId> &Updated();
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
float64 ComputeVolume(AudioMsgId::Type type);
|
|
|
|
|
|
|
|
enum class State {
|
|
|
|
Stopped = 0x01,
|
|
|
|
StoppedAtEnd = 0x02,
|
|
|
|
StoppedAtError = 0x03,
|
|
|
|
StoppedAtStart = 0x04,
|
|
|
|
|
|
|
|
Starting = 0x08,
|
|
|
|
Playing = 0x10,
|
|
|
|
Finishing = 0x18,
|
|
|
|
Pausing = 0x20,
|
|
|
|
Paused = 0x28,
|
|
|
|
PausedAtEnd = 0x30,
|
|
|
|
Resuming = 0x38,
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool IsStopped(State state) {
|
|
|
|
return (state == State::Stopped)
|
|
|
|
|| (state == State::StoppedAtEnd)
|
|
|
|
|| (state == State::StoppedAtError)
|
|
|
|
|| (state == State::StoppedAtStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsPaused(State state) {
|
|
|
|
return (state == State::Paused)
|
|
|
|
|| (state == State::PausedAtEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsFading(State state) {
|
|
|
|
return (state == State::Starting)
|
|
|
|
|| (state == State::Finishing)
|
|
|
|
|| (state == State::Pausing)
|
|
|
|
|| (state == State::Resuming);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsActive(State state) {
|
|
|
|
return !IsStopped(state) && !IsPaused(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TrackState {
|
|
|
|
AudioMsgId id;
|
|
|
|
State state = State::Stopped;
|
|
|
|
int64 position = 0;
|
2017-05-03 13:01:15 +00:00
|
|
|
int64 length = 0;
|
2017-01-24 21:24:39 +00:00
|
|
|
int frequency = kDefaultFrequency;
|
|
|
|
};
|
2017-01-19 08:24:43 +00:00
|
|
|
|
|
|
|
class Mixer : public QObject, private base::Subscriber {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2017-01-19 08:24:43 +00:00
|
|
|
Mixer();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void play(const AudioMsgId &audio, int64 position = 0);
|
2017-05-18 20:18:59 +00:00
|
|
|
void play(const AudioMsgId &audio, std::unique_ptr<VideoSoundData> videoData, int64 position = 0);
|
|
|
|
void pause(const AudioMsgId &audio, bool fast = false);
|
|
|
|
void resume(const AudioMsgId &audio, bool fast = false);
|
2017-01-24 21:24:39 +00:00
|
|
|
void seek(AudioMsgId::Type type, int64 position); // type == AudioMsgId::Type::Song
|
2017-05-18 20:18:59 +00:00
|
|
|
void stop(const AudioMsgId &audio);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
// Video player audio stream interface.
|
|
|
|
void feedFromVideo(VideoSoundPart &&part);
|
2017-05-18 20:18:59 +00:00
|
|
|
int64 getVideoCorrectedTime(const AudioMsgId &id, TimeMs frameMs, TimeMs systemMs);
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2015-11-24 16:19:18 +00:00
|
|
|
void stopAndClear();
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
TrackState currentState(AudioMsgId::Type type);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
void clearStoppedAtStart(const AudioMsgId &audio);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
// Thread: Main. Must be locked: AudioMutex.
|
2017-01-24 21:24:39 +00:00
|
|
|
void detachTracks();
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
// Thread: Main. Must be locked: AudioMutex.
|
2017-01-24 21:24:39 +00:00
|
|
|
void reattachIfNeeded();
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
// Thread: Any. Must be locked: AudioMutex.
|
2017-01-24 21:24:39 +00:00
|
|
|
void reattachTracks();
|
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
// Thread: Any.
|
2017-05-18 20:18:59 +00:00
|
|
|
void setSongVolume(float64 volume);
|
|
|
|
float64 getSongVolume() const;
|
2017-04-15 19:51:53 +00:00
|
|
|
void setVideoVolume(float64 volume);
|
|
|
|
float64 getVideoVolume() const;
|
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
~Mixer();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
private slots:
|
2015-06-30 21:07:05 +00:00
|
|
|
void onError(const AudioMsgId &audio);
|
|
|
|
void onStopped(const AudioMsgId &audio);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-09-21 11:44:20 +00:00
|
|
|
void onUpdated(const AudioMsgId &audio);
|
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
signals:
|
2015-06-30 21:07:05 +00:00
|
|
|
void updated(const AudioMsgId &audio);
|
2015-07-03 08:47:16 +00:00
|
|
|
void stoppedOnError(const AudioMsgId &audio);
|
|
|
|
void loaderOnStart(const AudioMsgId &audio, qint64 position);
|
2015-06-30 21:07:05 +00:00
|
|
|
void loaderOnCancel(const AudioMsgId &audio);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
void faderOnTimer();
|
2015-07-03 08:47:16 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void suppressSong();
|
|
|
|
void unsuppressSong();
|
2017-05-03 13:01:15 +00:00
|
|
|
void suppressAll(qint64 duration);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
private:
|
2016-06-30 12:03:32 +00:00
|
|
|
bool fadedStop(AudioMsgId::Type type, bool *fadedStart = 0);
|
2017-01-24 21:24:39 +00:00
|
|
|
void resetFadeStartPosition(AudioMsgId::Type type, int positionInBuffered = -1);
|
2016-06-30 12:03:32 +00:00
|
|
|
bool checkCurrentALError(AudioMsgId::Type type);
|
|
|
|
|
2016-09-21 11:44:20 +00:00
|
|
|
void videoSoundProgress(const AudioMsgId &audio);
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
class Track {
|
|
|
|
public:
|
|
|
|
static constexpr int kBuffersCount = 3;
|
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
// Thread: Any. Must be locked: AudioMutex.
|
2017-01-24 21:24:39 +00:00
|
|
|
void reattach(AudioMsgId::Type type);
|
2017-05-18 20:18:59 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
void detach();
|
2016-06-30 12:03:32 +00:00
|
|
|
void clear();
|
2017-01-24 21:24:39 +00:00
|
|
|
void started();
|
|
|
|
|
|
|
|
bool isStreamCreated() const;
|
|
|
|
void ensureStreamCreated();
|
2016-06-30 12:03:32 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
int getNotQueuedBufferIndex();
|
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
~Track();
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
TrackState state;
|
2015-11-24 16:19:18 +00:00
|
|
|
|
2015-11-26 17:34:52 +00:00
|
|
|
FileLocation file;
|
2014-09-04 07:33:44 +00:00
|
|
|
QByteArray data;
|
2017-01-24 21:24:39 +00:00
|
|
|
int64 bufferedPosition = 0;
|
|
|
|
int64 bufferedLength = 0;
|
2016-06-30 12:03:32 +00:00
|
|
|
bool loading = false;
|
2017-01-24 21:24:39 +00:00
|
|
|
bool loaded = false;
|
|
|
|
int64 fadeStartPosition = 0;
|
|
|
|
|
|
|
|
int32 format = 0;
|
|
|
|
int32 frequency = kDefaultFrequency;
|
|
|
|
int samplesCount[kBuffersCount] = { 0 };
|
|
|
|
QByteArray bufferSamples[kBuffersCount];
|
2016-06-30 12:03:32 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
struct Stream {
|
|
|
|
uint32 source = 0;
|
|
|
|
uint32 buffers[kBuffersCount] = { 0 };
|
|
|
|
};
|
|
|
|
Stream stream;
|
2017-02-21 13:45:56 +00:00
|
|
|
std::unique_ptr<VideoSoundData> videoData;
|
2016-07-10 13:02:22 +00:00
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
TimeMs lastUpdateWhen = 0;
|
|
|
|
TimeMs lastUpdateCorrectedMs = 0;
|
|
|
|
|
2016-07-10 13:02:22 +00:00
|
|
|
private:
|
2017-01-24 21:24:39 +00:00
|
|
|
void createStream();
|
|
|
|
void destroyStream();
|
|
|
|
void resetStream();
|
2016-07-10 13:02:22 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
};
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
void setStoppedState(Track *current, State state = State::Stopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
Track *trackForType(AudioMsgId::Type type, int index = -1); // -1 uses currentIndex(type)
|
|
|
|
const Track *trackForType(AudioMsgId::Type type, int index = -1) const;
|
2016-06-30 12:03:32 +00:00
|
|
|
int *currentIndex(AudioMsgId::Type type);
|
|
|
|
const int *currentIndex(AudioMsgId::Type type) const;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
int _audioCurrent = 0;
|
2017-01-24 21:24:39 +00:00
|
|
|
Track _audioTracks[kTogetherLimit];
|
2016-06-30 12:03:32 +00:00
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
int _songCurrent = 0;
|
2017-01-24 21:24:39 +00:00
|
|
|
Track _songTracks[kTogetherLimit];
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
Track _videoTrack;
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
QAtomicInt _volumeVideo;
|
|
|
|
QAtomicInt _volumeSong;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
friend class Fader;
|
|
|
|
friend class Loaders;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
QThread _faderThread, _loaderThread;
|
2017-01-19 08:24:43 +00:00
|
|
|
Fader *_fader;
|
|
|
|
Loaders *_loader;
|
2015-05-29 18:52:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
Mixer *mixer();
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
class Fader : public QObject {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2017-01-19 08:24:43 +00:00
|
|
|
Fader(QThread *thread);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
signals:
|
2015-06-30 21:07:05 +00:00
|
|
|
void error(const AudioMsgId &audio);
|
|
|
|
void playPositionUpdated(const AudioMsgId &audio);
|
|
|
|
void audioStopped(const AudioMsgId &audio);
|
|
|
|
void needToPreload(const AudioMsgId &audio);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
public slots:
|
2014-09-04 07:33:44 +00:00
|
|
|
void onInit();
|
|
|
|
void onTimer();
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void onSuppressSong();
|
|
|
|
void onUnsuppressSong();
|
2017-05-03 13:01:15 +00:00
|
|
|
void onSuppressAll(qint64 duration);
|
2015-07-03 08:47:16 +00:00
|
|
|
void onSongVolumeChanged();
|
2016-07-12 14:11:59 +00:00
|
|
|
void onVideoVolumeChanged();
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
private:
|
2015-06-30 21:07:05 +00:00
|
|
|
enum {
|
2016-07-05 17:44:02 +00:00
|
|
|
EmitError = 0x01,
|
|
|
|
EmitStopped = 0x02,
|
2015-06-30 21:07:05 +00:00
|
|
|
EmitPositionUpdated = 0x04,
|
2016-07-05 17:44:02 +00:00
|
|
|
EmitNeedToPreload = 0x08,
|
2015-06-30 21:07:05 +00:00
|
|
|
};
|
2017-05-18 20:18:59 +00:00
|
|
|
int32 updateOnePlayback(Mixer::Track *track, bool &hasPlaying, bool &hasFading, float64 volumeMultiplier, bool volumeChanged);
|
2017-01-24 21:24:39 +00:00
|
|
|
void setStoppedState(Mixer::Track *track, State state = State::Stopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
QTimer _timer;
|
2016-10-12 19:34:25 +00:00
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
bool _volumeChangedSong = false;
|
|
|
|
bool _volumeChangedVideo = false;
|
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
bool _suppressAll = false;
|
|
|
|
bool _suppressAllAnim = false;
|
|
|
|
bool _suppressSong = false;
|
|
|
|
bool _suppressSongAnim = false;
|
2017-05-18 20:18:59 +00:00
|
|
|
anim::value _suppressVolumeAll;
|
|
|
|
anim::value _suppressVolumeSong;
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs _suppressAllStart = 0;
|
2017-05-03 13:01:15 +00:00
|
|
|
TimeMs _suppressAllEnd = 0;
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs _suppressSongStart = 0;
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
};
|
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
FileLoadTask::Song PrepareForSending(const QString &fname, const QByteArray &data);
|
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
namespace internal {
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
// Thread: Any. Must be locked: AudioMutex.
|
|
|
|
bool CheckAudioDeviceConnected();
|
|
|
|
|
|
|
|
// Thread: Main. Locks: AudioMutex.
|
|
|
|
void DetachFromDevice();
|
|
|
|
|
|
|
|
// Thread: Any.
|
2017-01-19 08:24:43 +00:00
|
|
|
QMutex *audioPlayerMutex();
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
// Thread: Any.
|
|
|
|
bool audioCheckError();
|
2017-01-24 21:24:39 +00:00
|
|
|
|
2017-01-19 08:24:43 +00:00
|
|
|
} // namespace internal
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
} // namespace Player
|
|
|
|
} // namespace Media
|
|
|
|
|
2016-02-12 16:35:06 +00:00
|
|
|
VoiceWaveform audioCountWaveform(const FileLocation &file, const QByteArray &data);
|
2017-05-07 19:09:20 +00:00
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
FORCE_INLINE uint16 ReadOneSample(uchar data) {
|
|
|
|
return qAbs((static_cast<int16>(data) - 0x80) * 0x100);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE uint16 ReadOneSample(int16 data) {
|
|
|
|
return qAbs(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SampleType, typename Callback>
|
|
|
|
void IterateSamples(base::const_byte_span bytes, Callback &&callback) {
|
|
|
|
auto samplesPointer = reinterpret_cast<const SampleType*>(bytes.data());
|
|
|
|
auto samplesCount = bytes.size() / sizeof(SampleType);
|
|
|
|
auto samplesData = gsl::make_span(samplesPointer, samplesCount);
|
|
|
|
for (auto sampleData : samplesData) {
|
|
|
|
callback(ReadOneSample(sampleData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Audio
|
|
|
|
} // namespace Media
|