115 lines
3.2 KiB
Plaintext
115 lines
3.2 KiB
Plaintext
#pragma once
|
|
|
|
#include "Spotify/Client.hpp"
|
|
#include <gst/gst.h>
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <print>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
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<ProxyConfig> 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<std::string> m_LinkOutFL;
|
|
std::optional<std::string> m_LinkOutFR;
|
|
std::optional<std::string> m_LinkInFL;
|
|
std::optional<std::string> m_LinkInFR;
|
|
|
|
// GStreamer pipeline
|
|
std::unique_ptr<GstElement, GstObjectDeleter> 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<std::string> SplitLines(const std::string &s);
|
|
static bool Contains(std::string_view hay, std::string_view needle);
|
|
|
|
// pw-link helpers
|
|
static std::vector<std::string> PwLinkListOutputs();
|
|
static std::vector<std::string> PwLinkListInputs();
|
|
static std::optional<std::string>
|
|
FindPortPrefix(const std::vector<std::string> &list, std::string_view prefix,
|
|
std::string_view suffix);
|
|
};
|