#pragma once #include "Spotify/Client.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct GstObjectDeleter { void operator()(gpointer ptr) const noexcept { if (ptr) gst_object_unref(ptr); } }; struct GstMessageDeleter { void operator()(GstMessage *msg) const noexcept { if (msg) gst_message_unref(msg); } }; class VirtualMicrophone { public: // micName: what apps will see/select (e.g. "StudioMic") explicit VirtualMicrophone(std::string micName, bool createNew); ~VirtualMicrophone() noexcept; // Play Spotify track via gstspotify spotifyaudiosrc and route into the // virtual microphone. void PlayUri(const std::string &trackUri, const std::string &token, const ProxyConfig &proxy); void PlayUri(const std::string &trackUri, const std::optional proxy); // Playback control void Pause(); void Resume(); void StopPlayback(); // stops pipeline (does not destroy devices) void WaitUntilFinished(); // waits EOS or ERROR; throws on ERROR // Device control (normally you don't call these directly) void DestroyDevices() noexcept; // For debugging / integration [[nodiscard]] const std::string &MicName() const noexcept { return m_MicName; } [[nodiscard]] const std::string &BusSinkName() const noexcept { return m_BusSinkName; } private: std::string m_MicName; // virtual mic node name (Audio/Source/Virtual) std::string m_BusSinkName; // bus sink name (Audio/Sink) // Pulse/PipeWire module ids uint32_t m_ModBusSink = 0; uint32_t m_ModVirtualMic = 0; // pw-link ports we connected (for disconnection) std::optional m_LinkOutFL; std::optional m_LinkOutFR; std::optional m_LinkInFL; std::optional m_LinkInFR; // GStreamer pipeline std::unique_ptr m_Pipeline; std::jthread m_BusThread; // completion signalling std::mutex m_Mtx; std::condition_variable m_Cv; bool m_Finished = true; bool m_HadError = false; std::string m_LastError; private: // startup behavior void CleanupStaleDevicesByName(); // create routing: bus sink + virtual mic + pw-link void CreateDevices(); void CreateLinks(); // pipeline lifecycle void BuildAndStartPipeline(const std::string &pipelineDesc); void WatchBus(std::stop_token st); // utilities static std::string RunCommandCapture(const std::string &cmd); static int RunCommandNoCapture(const std::string &cmd); static uint32_t ParseFirstUint(std::string_view s); static std::vector SplitLines(const std::string &s); static bool Contains(std::string_view hay, std::string_view needle); // pw-link helpers static std::vector PwLinkListOutputs(); static std::vector PwLinkListInputs(); static std::optional FindPortPrefix(const std::vector &list, std::string_view prefix, std::string_view suffix); };