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();
Ui::InitTextOptions();
Ui::Emoji::Init();
Media::Player::start();
Media::Player::start(_audio.get());
DEBUG_LOG(("Application Info: inited..."));
@ -1117,7 +1117,7 @@ Application::~Application() {
Window::Theme::Unload();
Media::Player::finish();
Media::Player::finish(_audio.get());
style::stopManager();
Local::finish();

View File

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

View File

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

View File

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

View File

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

View File

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