Fix Media::Audio::Instance destruction.

This commit is contained in:
John Preston 2019-02-01 10:09:55 +03:00
parent 918d58ef0a
commit b28e374e06
6 changed files with 35 additions and 26 deletions

View File

@ -111,7 +111,7 @@ void Application::run() {
anim::startManager(); anim::startManager();
Ui::InitTextOptions(); Ui::InitTextOptions();
Ui::Emoji::Init(); Ui::Emoji::Init();
Media::Player::start(); Media::Player::start(_audio.get());
DEBUG_LOG(("Application Info: inited...")); DEBUG_LOG(("Application Info: inited..."));
@ -1117,7 +1117,7 @@ Application::~Application() {
Window::Theme::Unload(); Window::Theme::Unload();
Media::Player::finish(); Media::Player::finish(_audio.get());
style::stopManager(); style::stopManager();
Local::finish(); Local::finish();

View File

@ -183,7 +183,7 @@ bool CreatePlaybackDevice() {
} }
// Thread: Main. Must be locked: AudioMutex. // Thread: Main. Must be locked: AudioMutex.
void ClosePlaybackDevice() { void ClosePlaybackDevice(not_null<Instance*> instance) {
if (!AudioDevice) return; if (!AudioDevice) return;
LOG(("Audio Info: Closing audio playback device.")); LOG(("Audio Info: Closing audio playback device."));
@ -201,7 +201,7 @@ void ClosePlaybackDevice() {
if (Player::mixer()) { if (Player::mixer()) {
Player::mixer()->detachTracks(); Player::mixer()->detachTracks();
} }
Current().detachTracks(); instance->detachTracks();
DestroyPlaybackDevice(); DestroyPlaybackDevice();
} }
@ -209,7 +209,7 @@ void ClosePlaybackDevice() {
} // namespace } // namespace
// Thread: Main. // Thread: Main.
void Start() { void Start(not_null<Instance*> instance) {
Assert(AudioDevice == nullptr); Assert(AudioDevice == nullptr);
qRegisterMetaType<AudioMsgId>(); qRegisterMetaType<AudioMsgId>();
@ -221,13 +221,13 @@ void Start() {
EnumeratePlaybackDevices(); EnumeratePlaybackDevices();
EnumerateCaptureDevices(); EnumerateCaptureDevices();
MixerInstance = new Player::Mixer(); MixerInstance = new Player::Mixer(instance);
Platform::Audio::Init(); Platform::Audio::Init();
} }
// Thread: Main. // Thread: Main.
void Finish() { void Finish(not_null<Instance*> instance) {
Platform::Audio::DeInit(); Platform::Audio::DeInit();
// MixerInstance variable should be modified under AudioMutex protection. // MixerInstance variable should be modified under AudioMutex protection.
@ -235,7 +235,7 @@ void Finish() {
delete MixerInstance; delete MixerInstance;
// No sync required already. // No sync required already.
ClosePlaybackDevice(); ClosePlaybackDevice(instance);
} }
// Thread: Main. Locks: AudioMutex. // Thread: Main. Locks: AudioMutex.
@ -489,8 +489,9 @@ void Mixer::Track::resetStream() {
Mixer::Track::~Track() = default; Mixer::Track::~Track() = default;
Mixer::Mixer() Mixer::Mixer(not_null<Audio::Instance*> instance)
: _volumeVideo(kVolumeRound) : _instance(instance)
, _volumeVideo(kVolumeRound)
, _volumeSong(kVolumeRound) , _volumeSong(kVolumeRound)
, _fader(new Fader(&_faderThread)) , _fader(new Fader(&_faderThread))
, _loader(new Loaders(&_loaderThread)) { , _loader(new Loaders(&_loaderThread)) {
@ -530,7 +531,7 @@ Mixer::~Mixer() {
} }
_videoTrack.clear(); _videoTrack.clear();
Audio::ClosePlaybackDevice(); Audio::ClosePlaybackDevice(_instance);
Audio::MixerInstance = nullptr; Audio::MixerInstance = nullptr;
} }
@ -1467,9 +1468,9 @@ bool CheckAudioDeviceConnected() {
} }
// Thread: Main. Locks: AudioMutex. // Thread: Main. Locks: AudioMutex.
void DetachFromDevice() { void DetachFromDevice(not_null<Audio::Instance*> instance) {
QMutexLocker lock(&AudioMutex); QMutexLocker lock(&AudioMutex);
Audio::ClosePlaybackDevice(); Audio::ClosePlaybackDevice(instance);
if (mixer()) { if (mixer()) {
mixer()->reattachIfNeeded(); mixer()->reattachIfNeeded();
} }

View File

@ -16,9 +16,11 @@ struct VideoSoundPart;
namespace Media { namespace Media {
namespace Audio { namespace Audio {
class Instance;
// Thread: Main. // Thread: Main.
void Start(); void Start(not_null<Instance*> instance);
void Finish(); void Finish(not_null<Instance*> instance);
// Thread: Main. Locks: AudioMutex. // Thread: Main. Locks: AudioMutex.
bool IsAttachedToDevice(); bool IsAttachedToDevice();
@ -104,7 +106,7 @@ class Mixer : public QObject, private base::Subscriber {
Q_OBJECT Q_OBJECT
public: public:
Mixer(); explicit Mixer(not_null<Audio::Instance*> instance);
void play(const AudioMsgId &audio, TimeMs positionMs = 0); void play(const AudioMsgId &audio, TimeMs positionMs = 0);
void play(const AudioMsgId &audio, std::unique_ptr<VideoSoundData> videoData, TimeMs positionMs = 0); void play(const AudioMsgId &audio, std::unique_ptr<VideoSoundData> videoData, TimeMs positionMs = 0);
@ -229,6 +231,8 @@ private:
int *currentIndex(AudioMsgId::Type type); int *currentIndex(AudioMsgId::Type type);
const int *currentIndex(AudioMsgId::Type type) const; const int *currentIndex(AudioMsgId::Type type) const;
not_null<Audio::Instance*> _instance;
int _audioCurrent = 0; int _audioCurrent = 0;
Track _audioTracks[kTogetherLimit]; Track _audioTracks[kTogetherLimit];
@ -309,7 +313,7 @@ namespace internal {
bool CheckAudioDeviceConnected(); bool CheckAudioDeviceConnected();
// Thread: Main. Locks: AudioMutex. // Thread: Main. Locks: AudioMutex.
void DetachFromDevice(); void DetachFromDevice(not_null<Audio::Instance*> instance);
// Thread: Any. // Thread: Any.
QMutex *audioPlayerMutex(); QMutex *audioPlayerMutex();

View File

@ -258,9 +258,9 @@ Instance::Instance() {
} }
}); });
_detachFromDeviceTimer.setCallback([this] { _detachFromDeviceTimer.setCallback([=] {
_detachFromDeviceForce = false; _detachFromDeviceForce = false;
Player::internal::DetachFromDevice(); Player::internal::DetachFromDevice(this);
}); });
} }

View File

@ -36,18 +36,18 @@ constexpr auto kIdsPreloadAfter = 28;
} // namespace } // namespace
void start() { void start(not_null<Audio::Instance*> instance) {
Audio::Start(); Audio::Start(instance);
Capture::Start(); Capture::Start();
SingleInstance = new Instance(); SingleInstance = new Instance();
} }
void finish() { void finish(not_null<Audio::Instance*> instance) {
delete base::take(SingleInstance); delete base::take(SingleInstance);
Capture::Finish(); Capture::Finish();
Audio::Finish(); Audio::Finish(instance);
} }
Instance::Instance() Instance::Instance()

View File

@ -12,10 +12,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
class AudioMsgId; class AudioMsgId;
namespace Media { namespace Media {
namespace Audio {
class Instance;
} // namespace Audio
namespace Player { namespace Player {
void start(); void start(not_null<Audio::Instance*> instance);
void finish(); void finish(not_null<Audio::Instance*> instance);
class Instance; class Instance;
Instance *instance(); Instance *instance();
@ -125,7 +129,7 @@ public:
private: private:
Instance(); Instance();
friend void start(); friend void start(not_null<Audio::Instance*> instance);
void setupShortcuts(); void setupShortcuts();