audio streams finally working on client side

This commit is contained in:
tdv
2025-10-28 16:52:24 +02:00
parent aec6a51ae6
commit 4d055c343a
2 changed files with 213 additions and 103 deletions

View File

@@ -27,23 +27,20 @@ class AudioStreamService {
// WHIP
std::unique_ptr<WhipClient> m_whip;
std::mutex m_whipMutex;
std::string m_tmpKeyPath; // temp key extracted from keyctl (deleted on Stop)
public:
explicit AudioStreamService(std::shared_ptr<ConfigService> cfg)
: m_cfg(std::move(cfg)) {}
~AudioStreamService() {
StopWhip();
}
~AudioStreamService() { StopWhip(); }
// Feed raw PCM (float32 interleaved), frames = samples per channel
// Feed encoded Opus; frames = PCM samples per channel represented by this Opus frame
void OnOpus(const unsigned char* opusData, size_t opusBytes, int pcmFramesPerChannel) {
std::lock_guard lk(m_whipMutex);
if (m_whip) m_whip->PushOpus(opusData, opusBytes, pcmFramesPerChannel);
}
bool StartWhip(const std::string& whipUrl, int sampleRate=48000, int channels=1) {
bool StartWhip(const std::string& whipUrl, int sampleRate = 48000, int channels = 1) {
std::lock_guard lk(m_whipMutex);
if (m_whip) {
spdlog::info("WHIP already started");
@@ -59,30 +56,21 @@ public:
if (!std::filesystem::exists(ca)) ca = "/etc/iot/keys/ca_chain.pem";
std::filesystem::path crt = "/etc/iot/keys/device.crt.pem";
// extract client key via keyctl
// auto tmpKey = snoop::device_sec::ExtractClientKeyFromKernelKeyring();
// if (!tmpKey.string().empty()) {
// spdlog::error("Cannot extract client key for WHIP (keyctl user iot-client-key)");
// return false;
// }
WhipClient::Params p{
.whipUrl = whipUrl,
.caPath = ca.string(),
.crtPath = crt.string(),
// .keyPath = tmpKey,
.sampleRate= sampleRate,
.channels = channels
.whipUrl = whipUrl, // may include ?token=..., client will normalize path and set Bearer
.caPath = ca.string(),
.crtPath = crt.string(),
.sampleRate = sampleRate,
.channels = channels
};
m_whip = std::make_unique<WhipClient>(p);
try {
m_whip->Start();
spdlog::info("WHIP started");
// m_tmpKeyPath = tmpKey;
return true;
} catch (const std::exception& e) {
spdlog::error("WHIP start failed: {}", e.what());
// std::error_code ec; std::filesystem::remove(tmpKey, ec);
m_whip.reset();
return false;
}
@@ -94,14 +82,7 @@ public:
m_whip->Stop();
m_whip.reset();
}
if (!m_tmpKeyPath.empty()) {
std::error_code ec; std::filesystem::remove(m_tmpKeyPath, ec);
m_tmpKeyPath.clear();
}
}
private:
};
} // namespace snoop