From 2a1b30e1666dfebe6eb06877b654a172d3fe23f1 Mon Sep 17 00:00:00 2001 From: Hane Date: Tue, 17 Dec 2024 00:01:02 +0100 Subject: [PATCH] fix: sessionmanager life expired under my feet --- src/back/backlasses.cpp | 32 +++++++++++++++++++++++--------- src/back/backlasses.h | 6 +++--- src/cont/contclasses.cpp | 31 ++++++++++++++++++++++--------- src/cont/contclasses.h | 2 ++ src/qt/qtclasses.cpp | 7 +++++++ src/qt/qtclasses.h | 2 +- 6 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/back/backlasses.cpp b/src/back/backlasses.cpp index f003403..775f929 100644 --- a/src/back/backlasses.cpp +++ b/src/back/backlasses.cpp @@ -351,16 +351,12 @@ Endpoint::Endpoint(IMMDevice* ep, IPolicyConfig7* policyConfig, uint64_t idx){ endpoint->OpenPropertyStore(STGM_READ, &properties); this->updateName(); this->setFlow(); - if (this->flow == Flows::FLOW_PLAYBACK) { - activateEndpointSessions(); - log_debugcpp("plays back"); - } } void Endpoint::updateName() { PROPVARIANT pv; #define store_name(key, propvariant, wstr) do { \ - properties->GetValue(key , &propvariant); \ + properties->GetValue(key, &propvariant); \ if (pv.pwszVal == nullptr) wstr = L"Unnamed Not Present Endpoint"; \ else wstr = std::wstring(pv.pwszVal); \ } while (0) @@ -373,8 +369,16 @@ void Endpoint::updateName() { } void Endpoint::activateEndpointSessions() { - //sessionManager; - if (FAILED(endpoint->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**) &sessionManager))) { log_wdebugcpp(L"sesionbros..."); return; } + if (this->flow != Flows::FLOW_PLAYBACK) { + log_debugcpp("recording. No seshes for u :("); + return; + } + + if (FAILED(endpoint->Activate(__uuidof(IAudioSessionManager2), + CLSCTX_ALL, NULL, (void**) &sessionManager))) { + log_debugcpp("Couldn't open session manager2, huh"); + return; + } IAudioSessionEnumerator* sessionEnumerator = nullptr; if (FAILED(sessionManager->GetSessionEnumerator(&sessionEnumerator))) { log_wdebugcpp(L"sesEnumeratorBros..."); return; } @@ -382,7 +386,7 @@ void Endpoint::activateEndpointSessions() { endpointSessions.resize(1, nullptr); int sessionCount; sessionEnumerator->GetCount(&sessionCount); - for (int i = 0; i < sessionCount; i++) { + for (int i = 0; i < sessionCount; i++) { IAudioSessionControl* sessionControlTmp; sessionEnumerator->GetSession(i, (IAudioSessionControl**)&sessionControlTmp); /*todo: borrar when donezo @@ -390,7 +394,7 @@ void Endpoint::activateEndpointSessions() { * IAudioMeterInformation* ttmp = (IAudioMeterInformation*)sessionControlTmp; * ttmp->GetPeakValue(&test2); */ - //todo:: asegurar lo del dynamic_cast + IAudioSessionControl2* sessionControl; sessionControlTmp->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&sessionControl); sessionControl->AddRef(); @@ -593,12 +597,22 @@ void Endpoint::unregisterNewSessionNotification(EndpointNewSessionCallback* ensc sessionManager->UnregisterSessionNotification(ensc); } +void Endpoint::deleteSessions() { + for (auto session : endpointSessions) { + delete session; + } + endpointSessions.resize(0); +} + Endpoint::~Endpoint(){ log_wdebugcpp(L"murio endpoint-san uwu"); properties->Release(); endpointVolume->Release(); endpoint->Release(); sessionManager->Release(); + for (auto session : endpointSessions) { + delete session; + } } void Overseer::initCOMLibrary() { diff --git a/src/back/backlasses.h b/src/back/backlasses.h index 3a94707..4e67a09 100644 --- a/src/back/backlasses.h +++ b/src/back/backlasses.h @@ -80,19 +80,19 @@ class Endpoint { void addSession(Session* session); void registerNewSessionNotification(EndpointNewSessionCallback* ensc); void unregisterNewSessionNotification(EndpointNewSessionCallback* ensc); - + void deleteSessions(); + void activateEndpointSessions(); ~Endpoint(); private: void inline activateEndpointVolume(); - void inline activateEndpointSessions(); std::vector endpointSessions; uint32_t channelCount = 0; IMMDevice *endpoint; IAudioClient *audioClient; int64_t defTime, minTime; - IAudioSessionManager2 *sessionManager; + IAudioSessionManager2 *sessionManager = nullptr; Flows flow; IAudioEndpointVolume *endpointVolume = nullptr; IPropertyStore *properties; diff --git a/src/cont/contclasses.cpp b/src/cont/contclasses.cpp index c6a2149..067a32f 100644 --- a/src/cont/contclasses.cpp +++ b/src/cont/contclasses.cpp @@ -30,19 +30,10 @@ EndpointHandler::EndpointHandler(uint64_t idx, Flows flow) { this->ep = (flow == Flows::FLOW_PLAYBACK ? osh->getPlaybackEndpoints().at(idx) : osh->getCaptureEndpoints().at(idx)); epc = new EndpointVolumeCallback(ep); - ensc = new EndpointNewSessionCallback(this); this->callbackInfo.caller = osh->getGuid(); - ep->registerNewSessionNotification(ensc); //epName = ep->getName(); this->setBackEndpointVolumeCallbackInfoContent(this->getState()); osh->pushBackEndpointHandler(this, flow); - - if (this->flow == Flows::FLOW_PLAYBACK) { - for (int i = 0; i < this->getSessionCount(); i++) { - SessionHandler* sessionHandler = new SessionHandler(this, this->getSessions().at(i),i); - sessionHandlers.push_back(sessionHandler); - } - } } void OverseerHandler::pushBackEndpointHandler(EndpointHandler* eph, Flows flow) { @@ -215,6 +206,28 @@ void EndpointHandler::removeSessionFromFront(SessionHandler* sh) { this->removeSessionWidget(sh); } +void EndpointHandler::deleteSessions() { + ep->unregisterNewSessionNotification(ensc); + ensc->Release(); + for (auto sh : sessionHandlers) { + delete sh; + } + sessionHandlers.resize(0); + ep->deleteSessions(); +} + +void EndpointHandler::createSessionHandlers() { + ep->activateEndpointSessions(); + ensc = new EndpointNewSessionCallback(this); + ep->registerNewSessionNotification(ensc); + if (this->flow == Flows::FLOW_PLAYBACK) { + for (int i = 0; i < this->getSessionCount(); i++) { + SessionHandler* sessionHandler = new SessionHandler(this, this->getSessions().at(i),i); + sessionHandlers.push_back(sessionHandler); + } + } +} + EndpointHandler::~EndpointHandler() { ep->removeVolumeCallback(epc); ep->unregisterNewSessionNotification(ensc); diff --git a/src/cont/contclasses.h b/src/cont/contclasses.h index 0f5cedd..2a4a9f0 100644 --- a/src/cont/contclasses.h +++ b/src/cont/contclasses.h @@ -74,6 +74,8 @@ public: void setRemoveSessionWidgetFunction(std::function removeSessionWidget); void sendSessionToFront(SessionHandler* sh); void removeSessionFromFront(SessionHandler* sh); + void deleteSessions(); + void createSessionHandlers(); ~EndpointHandler(); private: diff --git a/src/qt/qtclasses.cpp b/src/qt/qtclasses.cpp index e7d7b35..c7b5cc7 100644 --- a/src/qt/qtclasses.cpp +++ b/src/qt/qtclasses.cpp @@ -531,6 +531,7 @@ EndpointWidget::EndpointWidget(EndpointHandler* eph, QWidget *parent, uint64_t i this->idx = idx; this->eph = eph; + eph->createSessionHandlers(); //todo: sussy this->eph->setState(EndpointState::ENDPOINT_ACTIVE, idx); this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); @@ -749,6 +750,10 @@ EndpointWidget::~EndpointWidget() { timer->stop(); delete timer; this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ALL, INT_MAX); + this->eph->deleteSessions(); + for(auto sw : sessionWidgets) { + delete sw; + } } void MainWindow::customEvent(QEvent* ev) { @@ -894,6 +899,8 @@ void EndpointWidget::updateMainVolume(int newValue){ */ void EndpointWidget::updateChannelsVisibility() { + if (!cw) return; + char* const channelSettings = set->getValue("show_channels"); if(channelSettings && !(strcmp(channelSettings, "true"))){ cw->setVisible(true); diff --git a/src/qt/qtclasses.h b/src/qt/qtclasses.h index 5531921..1ca937d 100644 --- a/src/qt/qtclasses.h +++ b/src/qt/qtclasses.h @@ -149,7 +149,7 @@ private: size_t defaultRolesVectorSize = 4; QTimer* timer = nullptr; uint64_t idx; - ChannelWidget* cw; + ChannelWidget* cw = nullptr; std::vector sessionWidgets; QSize minimum; //std::vector *ephs;