225 lines
7.0 KiB
C++
225 lines
7.0 KiB
C++
#include "google/live/live_session.h"
|
|
|
|
#include <chrono>
|
|
|
|
#include "google/errors.h"
|
|
#include "google/live/ws_error.h"
|
|
|
|
namespace molo::google::live {
|
|
|
|
namespace {
|
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
std::int64_t now_ms() {
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
Clock::now().time_since_epoch())
|
|
.count();
|
|
}
|
|
|
|
GoogleError map_error(const WsError& e) {
|
|
using K = WsError::Kind;
|
|
switch (e.kind()) {
|
|
case K::Invalid: return GoogleError(400, "INVALID_ARGUMENT", e.what(), "live");
|
|
case K::Timeout: return GoogleError(504, "DEADLINE_EXCEEDED", e.what(), "live");
|
|
case K::TooBig: return GoogleError(413, "RESOURCE_EXHAUSTED", e.what(), "live");
|
|
case K::Handshake:
|
|
switch (e.http_status()) {
|
|
case 400: return GoogleError(400, "INVALID_ARGUMENT", e.what(), "live");
|
|
case 401: return GoogleError(401, "UNAUTHENTICATED", e.what(), "live");
|
|
case 403: return GoogleError(403, "PERMISSION_DENIED", e.what(), "live");
|
|
case 404: return GoogleError(404, "NOT_FOUND", e.what(), "live");
|
|
case 429: return GoogleError(429, "RESOURCE_EXHAUSTED", e.what(), "live");
|
|
default: return GoogleError(502, "UNAVAILABLE", e.what(), "live");
|
|
}
|
|
case K::Closed: return GoogleError(409, "FAILED_PRECONDITION", e.what(), "live");
|
|
default: return GoogleError(502, "UNAVAILABLE", e.what(), "live");
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
LiveSession::LiveSession(LiveEndpoint endpoint, LiveConfig config,
|
|
LiveOptions options)
|
|
: endpoint_(std::move(endpoint)),
|
|
config_(std::move(config)),
|
|
options_(std::move(options)) {}
|
|
|
|
LiveSession::~LiveSession() {
|
|
try {
|
|
close();
|
|
} catch (...) {
|
|
}
|
|
}
|
|
|
|
void LiveSession::connect() {
|
|
if (connected_.exchange(true)) throw GoogleError::invalid("session already connected");
|
|
endpoint_.validate();
|
|
std::string setup = config_.to_setup(endpoint_).dump();
|
|
try {
|
|
WsUrl url = WsUrl::parse(endpoint_.url());
|
|
ws_ = std::make_unique<WsClient>(open_stream(
|
|
url, options_.connect_timeout_ms, LiveCaps::kIoTimeoutMs, options_.tls));
|
|
ws_->handshake(url, endpoint_.headers(), options_.handshake_timeout_ms);
|
|
ws_->send_text(setup);
|
|
ws_->set_read_timeout_ms(LiveCaps::kPollIntervalMs);
|
|
auto deadline = now_ms() + options_.setup_timeout_ms;
|
|
bool ready = false;
|
|
while (!ready) {
|
|
if (now_ms() > deadline) throw WsError(WsError::Kind::Timeout, "setupComplete not received in time");
|
|
auto m = ws_->receive();
|
|
if (!m) continue;
|
|
if (m->kind == WsMessage::Kind::Close)
|
|
throw WsError(WsError::Kind::Closed,
|
|
"server closed before setup completed (code " +
|
|
std::to_string(m->code) + "): " + m->data.substr(0, 500));
|
|
for (auto& e : LiveParser::parse(m->data)) {
|
|
if (e.kind == LiveEventKind::SetupComplete) ready = true;
|
|
push(std::move(e));
|
|
}
|
|
}
|
|
} catch (const WsError& e) {
|
|
if (ws_) ws_->shutdown();
|
|
ws_.reset();
|
|
throw map_error(e);
|
|
}
|
|
open_ = true;
|
|
reader_ = std::thread([this] { reader_loop(); });
|
|
}
|
|
|
|
void LiveSession::send_raw(const std::string& json) {
|
|
if (!open_ || !ws_) throw GoogleError(409, "FAILED_PRECONDITION", "session is not open", "live");
|
|
try {
|
|
ws_->send_text(json);
|
|
} catch (const WsError& e) {
|
|
throw map_error(e);
|
|
}
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
++stats_.sent;
|
|
}
|
|
|
|
void LiveSession::send_text(const std::string& t) { send_raw(LiveRequests::text(t)); }
|
|
void LiveSession::send_turn(const std::string& t, bool c) { send_raw(LiveRequests::turn(t, c)); }
|
|
void LiveSession::send_audio(const std::string& pcm, const std::string& mime) {
|
|
send_raw(LiveRequests::audio(pcm, mime));
|
|
}
|
|
void LiveSession::send_video(const std::string& b, const std::string& mime) {
|
|
send_raw(LiveRequests::video(b, mime));
|
|
}
|
|
void LiveSession::end_audio() { send_raw(LiveRequests::audio_stream_end()); }
|
|
void LiveSession::send_tool_responses(const std::vector<LiveToolResponse>& r) {
|
|
send_raw(LiveRequests::tool_responses(r));
|
|
}
|
|
|
|
void LiveSession::push(LiveEvent e) {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
e.seq = ++seq_;
|
|
if (e.kind == LiveEventKind::SessionResumption && e.resumable && !e.handle.empty())
|
|
handle_ = e.handle;
|
|
queued_bytes_ += e.weight();
|
|
queue_.push_back(std::move(e));
|
|
// Hard ceilings: drop oldest, count loudly in stats().dropped.
|
|
while (queue_.size() > 1 && (queue_.size() > LiveCaps::kMaxQueuedEvents ||
|
|
queued_bytes_ > LiveCaps::kMaxQueuedBytes)) {
|
|
queued_bytes_ -= queue_.front().weight();
|
|
queue_.pop_front();
|
|
++stats_.dropped;
|
|
}
|
|
cv_.notify_all();
|
|
}
|
|
|
|
void LiveSession::ingest(const WsMessage& m) {
|
|
{
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
++stats_.received;
|
|
}
|
|
for (auto& e : LiveParser::parse(m.data)) {
|
|
if (e.kind == LiveEventKind::Error) {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
++stats_.decode_errors;
|
|
}
|
|
push(std::move(e));
|
|
}
|
|
}
|
|
|
|
void LiveSession::reader_loop() {
|
|
const auto started = now_ms();
|
|
LiveEvent closed;
|
|
closed.kind = LiveEventKind::Closed;
|
|
try {
|
|
for (;;) {
|
|
if (now_ms() - started > (std::int64_t)options_.max_session_seconds * 1000) {
|
|
closed.text = "session lifetime cap reached";
|
|
closed.code = 1000;
|
|
ws_->close(1000, "lifetime cap");
|
|
break;
|
|
}
|
|
std::int64_t dl = close_deadline_ms_.load();
|
|
if (dl != 0 && now_ms() > dl) {
|
|
closed.text = "closed by client";
|
|
closed.code = 1000;
|
|
break;
|
|
}
|
|
auto m = ws_->receive();
|
|
if (!m) continue;
|
|
if (m->kind == WsMessage::Kind::Close) {
|
|
closed.code = m->code;
|
|
closed.text = m->data;
|
|
break;
|
|
}
|
|
// Gemini sends JSON in text and in binary frames.
|
|
ingest(*m);
|
|
}
|
|
} catch (const WsError& e) {
|
|
closed.kind = LiveEventKind::Error;
|
|
closed.text = e.what();
|
|
push(std::move(closed));
|
|
closed = LiveEvent{};
|
|
closed.kind = LiveEventKind::Closed;
|
|
closed.code = 1006;
|
|
closed.text = "transport error";
|
|
}
|
|
push(std::move(closed));
|
|
open_ = false;
|
|
cv_.notify_all();
|
|
}
|
|
|
|
std::optional<LiveEvent> LiveSession::next_event(int timeout_ms) {
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
cv_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
|
|
[this] { return !queue_.empty() || !open_; });
|
|
if (queue_.empty()) return std::nullopt;
|
|
LiveEvent e = std::move(queue_.front());
|
|
queue_.pop_front();
|
|
queued_bytes_ -= e.weight();
|
|
return e;
|
|
}
|
|
|
|
std::size_t LiveSession::pending() const {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
return queue_.size();
|
|
}
|
|
|
|
void LiveSession::close() {
|
|
if (!ws_) return;
|
|
if (open_.exchange(false) || reader_.joinable()) {
|
|
close_deadline_ms_ = now_ms() + LiveCaps::kCloseGraceMs;
|
|
ws_->close(1000, "client closing");
|
|
}
|
|
if (reader_.joinable()) reader_.join();
|
|
ws_->shutdown();
|
|
cv_.notify_all();
|
|
}
|
|
|
|
LiveSession::Stats LiveSession::stats() const {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
return stats_;
|
|
}
|
|
|
|
std::string LiveSession::resumption_handle() const {
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
return handle_;
|
|
}
|
|
|
|
} // namespace molo::google::live
|