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
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-09-04 07:33:44 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "core/basic_types.h"
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
void audioInit();
|
|
|
|
bool audioWorks();
|
|
|
|
void audioPlayNotify();
|
|
|
|
void audioFinish();
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
enum AudioPlayerState {
|
2016-07-05 17:44:02 +00:00
|
|
|
AudioPlayerStopped = 0x01,
|
|
|
|
AudioPlayerStoppedAtEnd = 0x02,
|
2015-07-03 08:47:16 +00:00
|
|
|
AudioPlayerStoppedAtError = 0x03,
|
|
|
|
AudioPlayerStoppedAtStart = 0x04,
|
2016-07-05 17:44:02 +00:00
|
|
|
AudioPlayerStoppedMask = 0x07,
|
|
|
|
|
|
|
|
AudioPlayerStarting = 0x08,
|
|
|
|
AudioPlayerPlaying = 0x10,
|
|
|
|
AudioPlayerFinishing = 0x18,
|
|
|
|
AudioPlayerPausing = 0x20,
|
|
|
|
AudioPlayerPaused = 0x28,
|
|
|
|
AudioPlayerPausedAtEnd = 0x30,
|
|
|
|
AudioPlayerResuming = 0x38,
|
2014-09-04 07:33:44 +00:00
|
|
|
};
|
|
|
|
|
2015-05-30 16:30:47 +00:00
|
|
|
class AudioPlayerFader;
|
|
|
|
class AudioPlayerLoaders;
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
struct VideoSoundData;
|
|
|
|
struct VideoSoundPart;
|
2016-07-10 13:02:22 +00:00
|
|
|
struct AudioPlaybackState {
|
|
|
|
AudioPlayerState state = AudioPlayerStopped;
|
|
|
|
int64 position = 0;
|
|
|
|
int64 duration = 0;
|
|
|
|
int32 frequency = 0;
|
|
|
|
};
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioPlayer : public QObject {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2015-05-29 18:52:43 +00:00
|
|
|
AudioPlayer();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void play(const AudioMsgId &audio, int64 position = 0);
|
2016-06-30 12:03:32 +00:00
|
|
|
void pauseresume(AudioMsgId::Type type, bool fast = false);
|
|
|
|
void seek(int64 position); // type == AudioMsgId::Type::Song
|
|
|
|
void stop(AudioMsgId::Type type);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
// Video player audio stream interface.
|
2016-07-10 19:44:55 +00:00
|
|
|
void playFromVideo(const AudioMsgId &audio, uint64 videoPlayId, std_::unique_ptr<VideoSoundData> &&data, int64 position);
|
2016-07-05 17:44:02 +00:00
|
|
|
void feedFromVideo(VideoSoundPart &&part);
|
2016-07-10 19:44:55 +00:00
|
|
|
int64 getVideoCorrectedTime(uint64 playId, uint64 systemMs);
|
|
|
|
void videoSoundProgress(const AudioMsgId &audio);
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2015-11-24 16:19:18 +00:00
|
|
|
void stopAndClear();
|
|
|
|
|
2016-07-10 13:02:22 +00:00
|
|
|
AudioPlaybackState currentState(AudioMsgId *audio, AudioMsgId::Type type);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
void clearStoppedAtStart(const AudioMsgId &audio);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-03 12:18:46 +00:00
|
|
|
void resumeDevice();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
~AudioPlayer();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
public 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
|
|
|
|
|
|
|
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();
|
|
|
|
void suppressAll();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void songVolumeChanged();
|
|
|
|
|
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);
|
|
|
|
bool updateCurrentStarted(AudioMsgId::Type type, int32 pos = -1);
|
|
|
|
bool checkCurrentALError(AudioMsgId::Type type);
|
|
|
|
|
|
|
|
struct AudioMsg {
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
AudioMsgId audio;
|
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;
|
2016-07-10 13:02:22 +00:00
|
|
|
AudioPlaybackState playbackState = defaultState();
|
2016-06-30 12:03:32 +00:00
|
|
|
int64 skipStart = 0;
|
|
|
|
int64 skipEnd = 0;
|
|
|
|
bool loading = false;
|
|
|
|
int64 started = 0;
|
|
|
|
|
|
|
|
uint32 source = 0;
|
|
|
|
int32 nextBuffer = 0;
|
|
|
|
uint32 buffers[3] = { 0 };
|
|
|
|
int64 samplesCount[3] = { 0 };
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2016-07-10 19:44:55 +00:00
|
|
|
uint64 videoPlayId = 0;
|
2016-07-05 17:44:02 +00:00
|
|
|
std_::unique_ptr<VideoSoundData> videoData;
|
2016-07-10 13:02:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static AudioPlaybackState defaultState() {
|
|
|
|
AudioPlaybackState result;
|
|
|
|
result.frequency = AudioVoiceMsgFrequency;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
};
|
|
|
|
|
2016-06-30 12:03:32 +00:00
|
|
|
void setStoppedState(AudioMsg *current, AudioPlayerState state = AudioPlayerStopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2016-06-30 12:03:32 +00:00
|
|
|
AudioMsg *dataForType(AudioMsgId::Type type, int index = -1); // -1 uses currentIndex(type)
|
|
|
|
const AudioMsg *dataForType(AudioMsgId::Type type, int index = -1) const;
|
|
|
|
int *currentIndex(AudioMsgId::Type type);
|
|
|
|
const int *currentIndex(AudioMsgId::Type type) const;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-06-30 12:03:32 +00:00
|
|
|
int _audioCurrent;
|
|
|
|
AudioMsg _audioData[AudioSimultaneousLimit];
|
|
|
|
|
|
|
|
int _songCurrent;
|
|
|
|
AudioMsg _songData[AudioSimultaneousLimit];
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
AudioMsg _videoData;
|
2016-07-10 19:44:55 +00:00
|
|
|
uint64 _lastVideoPlayId = 0;
|
|
|
|
uint64 _lastVideoPlaybackWhen = 0;
|
|
|
|
uint64 _lastVideoPlaybackCorrectedMs = 0;
|
|
|
|
QMutex _lastVideoMutex;
|
2016-07-05 17:44:02 +00:00
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
QMutex _mutex;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
friend class AudioPlayerFader;
|
|
|
|
friend class AudioPlayerLoaders;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
QThread _faderThread, _loaderThread;
|
|
|
|
AudioPlayerFader *_fader;
|
|
|
|
AudioPlayerLoaders *_loader;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
QMutex *audioPlayerMutex();
|
|
|
|
float64 audioSuppressGain();
|
|
|
|
float64 audioSuppressSongGain();
|
|
|
|
bool audioCheckError();
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2015-05-30 16:30:47 +00:00
|
|
|
class AudioCaptureInner;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioCapture : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AudioCapture();
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void stop(bool needResult);
|
|
|
|
|
2015-06-01 10:58:46 +00:00
|
|
|
bool check();
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
~AudioCapture();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
void captureOnStart();
|
|
|
|
void captureOnStop(bool needResult);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-02-12 16:35:06 +00:00
|
|
|
void onDone(QByteArray data, VoiceWaveform waveform, qint32 samples);
|
|
|
|
void onUpdate(quint16 level, qint32 samples);
|
2015-05-29 18:52:43 +00:00
|
|
|
void onError();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend class AudioCaptureInner;
|
|
|
|
|
|
|
|
QThread _captureThread;
|
|
|
|
AudioCaptureInner *_capture;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioPlayer *audioPlayer();
|
|
|
|
AudioCapture *audioCapture();
|
|
|
|
|
|
|
|
class AudioPlayerFader : public QObject {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
AudioPlayerFader(QThread *thread);
|
2015-06-03 12:18:46 +00:00
|
|
|
void resumeDevice();
|
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
|
|
|
|
2015-06-03 12:18:46 +00:00
|
|
|
void stopPauseDevice();
|
2015-01-10 13:08:30 +00:00
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
public slots:
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
void onInit();
|
|
|
|
void onTimer();
|
2015-06-03 12:18:46 +00:00
|
|
|
void onPauseTimer();
|
|
|
|
void onPauseTimerStop();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void onSuppressSong();
|
|
|
|
void onUnsuppressSong();
|
|
|
|
void onSuppressAll();
|
2015-07-03 08:47:16 +00:00
|
|
|
void onSongVolumeChanged();
|
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
|
|
|
};
|
2016-06-30 12:03:32 +00:00
|
|
|
int32 updateOnePlayback(AudioPlayer::AudioMsg *m, bool &hasPlaying, bool &hasFading, float64 suppressGain, bool suppressGainChanged);
|
|
|
|
void setStoppedState(AudioPlayer::AudioMsg *m, AudioPlayerState state = AudioPlayerStopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2015-06-03 12:18:46 +00:00
|
|
|
QTimer _timer, _pauseTimer;
|
|
|
|
QMutex _pauseMutex;
|
|
|
|
bool _pauseFlag, _paused;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
bool _suppressAll, _suppressAllAnim, _suppressSong, _suppressSongAnim, _songVolumeChanged;
|
2015-06-30 21:07:05 +00:00
|
|
|
anim::fvalue _suppressAllGain, _suppressSongGain;
|
|
|
|
uint64 _suppressAllStart, _suppressSongStart;
|
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
};
|
|
|
|
|
2015-05-30 16:30:47 +00:00
|
|
|
struct AudioCapturePrivate;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioCaptureInner : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AudioCaptureInner(QThread *thread);
|
|
|
|
~AudioCaptureInner();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
void error();
|
2016-02-12 16:35:06 +00:00
|
|
|
void update(quint16 level, qint32 samples);
|
|
|
|
void done(QByteArray data, VoiceWaveform waveform, qint32 samples);
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2016-07-05 17:44:02 +00:00
|
|
|
public slots:
|
2015-05-29 18:52:43 +00:00
|
|
|
|
|
|
|
void onInit();
|
|
|
|
void onStart();
|
|
|
|
void onStop(bool needResult);
|
|
|
|
|
|
|
|
void onTimeout();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void writeFrame(int32 offset, int32 framesize);
|
|
|
|
|
|
|
|
AudioCapturePrivate *d;
|
|
|
|
QTimer _timer;
|
|
|
|
QByteArray _captured;
|
|
|
|
|
|
|
|
};
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
MTPDocumentAttribute audioReadSongAttributes(const QString &fname, const QByteArray &data, QImage &cover, QByteArray &coverBytes, QByteArray &coverFormat);
|
2016-02-12 16:35:06 +00:00
|
|
|
VoiceWaveform audioCountWaveform(const FileLocation &file, const QByteArray &data);
|